From b4fa217ab3cc1a9ab8bad267b577cc4d9a074165 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Apr 2026 13:33:42 -1000 Subject: [PATCH] [core] Use __builtin_ctz for find_lowest_set_bit in FiniteSetMask Replace the linear bit-scanning loop in find_next_set_bit with __builtin_ctz (compiles to single-cycle NSAU on Xtensa). Also rename to find_lowest_set_bit and drop the unused start_bit parameter since all call sites pass 0. This eliminates the standalone find_next_set_bit function and replaces 3 function calls + loops in compute_color_mode_ with inline NSAU instructions. --- esphome/core/finite_set_mask.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/esphome/core/finite_set_mask.h b/esphome/core/finite_set_mask.h index f9cd0377c7..02135c49dc 100644 --- a/esphome/core/finite_set_mask.h +++ b/esphome/core/finite_set_mask.h @@ -119,7 +119,7 @@ template(mask)); + else + return __builtin_ctzl(static_cast(mask)); +#else + int bit = 0; while (bit < BitPolicy::MAX_BITS && !(mask & (static_cast(1) << bit))) { ++bit; } return bit; +#endif } protected: