1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-28 05:33:53 +00:00

bot review

This commit is contained in:
J. Nick Koston
2025-10-22 00:05:06 -10:00
parent 50eaf522b9
commit 3fda73bcf2
2 changed files with 13 additions and 9 deletions

View File

@@ -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<uint8_t>(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

View File

@@ -151,7 +151,7 @@ template<typename EnumType, int MaxBits = 16> 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};
};