Fixed-point log base 2 for positive values. Uses 4-term minimax polynomial for log2(1+t), t in [0,1). Horner evaluation uses intermediate precision (IFRAC) to minimize rounding error, then converts back to FRAC_BITS.
| 342 | // Horner evaluation uses intermediate precision (IFRAC) to minimize |
| 343 | // rounding error, then converts back to FRAC_BITS. |
| 344 | static FASTLED_FORCE_INLINE Derived log2_fp(Derived x) { |
| 345 | constexpr int IFRAC = traits::IFRAC; |
| 346 | |
| 347 | unsigned_raw_type val = static_cast<unsigned_raw_type>(x.mValue); |
| 348 | int msb = highest_bit(static_cast<u32>(val)); |
| 349 | raw_type int_part = msb - FRAC_BITS; |
| 350 | raw_type t; |
| 351 | if (msb >= FRAC_BITS) { |
| 352 | t = static_cast<raw_type>( |
| 353 | (val >> (msb - FRAC_BITS)) - (static_cast<unsigned_raw_type>(1) << FRAC_BITS)); |
| 354 | } else { |
| 355 | t = static_cast<raw_type>( |
| 356 | (val << (FRAC_BITS - msb)) - (static_cast<unsigned_raw_type>(1) << FRAC_BITS)); |
| 357 | } |
| 358 | |
| 359 | // 4-term minimax coefficients for log2(1+t), t in [0,1). |
| 360 | // Coefficients scaled by 2^IFRAC. |
| 361 | // Use poly_intermediate_type (i32 or i64 based on IFRAC). |
| 362 | using poly_type = poly_intermediate_type; |
| 363 | constexpr poly_type c0 = static_cast<poly_type>(1.44179 * (1LL << IFRAC)); |
| 364 | constexpr poly_type c1 = static_cast<poly_type>(-0.69907 * (1LL << IFRAC)); |
| 365 | constexpr poly_type c2 = static_cast<poly_type>(0.36348 * (1LL << IFRAC)); |
| 366 | constexpr poly_type c3 = static_cast<poly_type>(-0.10660 * (1LL << IFRAC)); |
| 367 | |
| 368 | // Extend t to IFRAC fractional bits |
| 369 | poly_type t_ifrac = static_cast<poly_type>(t) << (IFRAC - FRAC_BITS); |
| 370 | |
| 371 | // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3))) |
| 372 | return log2_horner(int_part, t_ifrac, c0, c1, c2, c3, fl::bool_constant<(IFRAC <= 16)>()); |
| 373 | } |
| 374 | |
| 375 | // log2 Horner evaluation for i32 intermediates (IFRAC <= 16) |
| 376 | template<typename PolyType> |
nothing calls this directly
no test coverage detected