From 3fda73bcf251e334fd85a881c4c067fad6ffffa4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 22 Oct 2025 00:05:06 -1000 Subject: [PATCH] bot review --- esphome/components/light/color_mode.h | 20 ++++++++++++-------- esphome/core/enum_bitmask.h | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/esphome/components/light/color_mode.h b/esphome/components/light/color_mode.h index 03132f54bf..77c5a13a6f 100644 --- a/esphome/components/light/color_mode.h +++ b/esphome/components/light/color_mode.h @@ -159,16 +159,13 @@ constexpr uint16_t CAPABILITY_BITMASKS[] = { compute_capability_bitmask(ColorCapability::RGB), // 1 << 5 }; -/// Check if any mode in the bitmask has a specific capability -/// Used for checking if a light supports a capability (e.g., BRIGHTNESS, RGB) -inline bool has_capability(const ColorModeMask &mask, ColorCapability capability) { - // Lookup the pre-computed bitmask for this capability and check intersection with our mask - // ColorCapability values: 1, 2, 4, 8, 16, 32 -> array indices: 0, 1, 2, 3, 4, 5 - // We need to convert the power-of-2 value to an index +/// Convert a power-of-2 ColorCapability value to an array index +/// ColorCapability values: 1, 2, 4, 8, 16, 32 -> array indices: 0, 1, 2, 3, 4, 5 +inline int capability_to_index(ColorCapability capability) { uint8_t cap_val = static_cast(capability); #if defined(__GNUC__) || defined(__clang__) // Use compiler intrinsic for efficient bit position lookup (O(1) vs O(log n)) - int index = __builtin_ctz(cap_val); + return __builtin_ctz(cap_val); #else // Fallback for compilers without __builtin_ctz int index = 0; @@ -176,8 +173,15 @@ inline bool has_capability(const ColorModeMask &mask, ColorCapability capability cap_val >>= 1; ++index; } + return index; #endif - return (mask.get_mask() & CAPABILITY_BITMASKS[index]) != 0; +} + +/// Check if any mode in the bitmask has a specific capability +/// Used for checking if a light supports a capability (e.g., BRIGHTNESS, RGB) +inline bool has_capability(const ColorModeMask &mask, ColorCapability capability) { + // Lookup the pre-computed bitmask for this capability and check intersection with our mask + return (mask.get_mask() & CAPABILITY_BITMASKS[capability_to_index(capability)]) != 0; } } // namespace light diff --git a/esphome/core/enum_bitmask.h b/esphome/core/enum_bitmask.h index b3112c610b..f9cda7ca2d 100644 --- a/esphome/core/enum_bitmask.h +++ b/esphome/core/enum_bitmask.h @@ -151,7 +151,7 @@ template class EnumBitmask { // Must be provided by template specialization // These convert between enum values and bit positions (0, 1, 2, ...) static constexpr int enum_to_bit(EnumType value); - static EnumType bit_to_enum(int bit); // Not constexpr due to static array limitation in C++20 + static EnumType bit_to_enum(int bit); // Not constexpr: array indexing with runtime bounds checking bitmask_t mask_{0}; };