FastLED 3.9.15
Loading...
Searching...
No Matches

◆ log2_fp()

static FASTLED_FORCE_INLINE s8x8 fl::s8x8::log2_fp ( s8x8 x)
inlinestaticprivate

Definition at line 321 of file s8x8.h.

321 {
322 u32 val = static_cast<u32>(x.mValue);
323 int msb = highest_bit(val);
324 i32 int_part = msb - FRAC_BITS;
325 i32 t;
326 if (msb >= FRAC_BITS) {
327 t = static_cast<i32>(
328 (val >> (msb - FRAC_BITS)) - (SCALE));
329 } else {
330 t = static_cast<i32>(
331 (val << (FRAC_BITS - msb)) - (SCALE));
332 }
333 // 4-term minimax coefficients for log2(1+t), t in [0,1).
334 // Stored as i32 with 16 fractional bits. Max product ~2^29, fits i32 after shift.
335 constexpr int IFRAC = 16;
336 constexpr i32 c0 = 94528; // 1.44179 * 2^16
337 constexpr i32 c1 = -45814; // -0.69907 * 2^16
338 constexpr i32 c2 = 23821; // 0.36348 * 2^16
339 constexpr i32 c3 = -6986; // -0.10660 * 2^16
340 // Extend t from 8 to 16 frac bits.
341 i32 t16 = static_cast<i32>(t) << (IFRAC - FRAC_BITS);
342 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
343 i32 acc = c3;
344 acc = c2 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
345 acc = c1 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
346 acc = c0 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
347 i32 frac_part = static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
348 // Convert from 16 frac bits back to 8.
349 i16 frac8 = static_cast<i16>(frac_part >> (IFRAC - FRAC_BITS));
350 // Use multiplication instead of shift to avoid UB with negative int_part
351 return from_raw(static_cast<i16>(int_part * SCALE + frac8));
352 }
static constexpr int FRAC_BITS
Definition s8x8.h:22
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition s8x8.h:304
static constexpr FASTLED_FORCE_INLINE s8x8 from_raw(i16 raw) FL_NOEXCEPT
Definition s8x8.h:53
static constexpr i32 SCALE
Definition s8x8.h:23
fl::i64 i64
Definition s16x16x4.h:222

References s8x8(), FASTLED_FORCE_INLINE, FL_NOEXCEPT, FRAC_BITS, from_raw(), highest_bit(), SCALE, fl::t, and fl::x.

Referenced by pow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: