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

◆ log2_fp()

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

Definition at line 321 of file s4x12.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 20 fractional bits. Max product ~2^33, fits i64 intermediate.
335 constexpr int IFRAC = 20;
336 constexpr i32 c0 = 1512456; // 1.44179 * 2^20
337 constexpr i32 c1 = -733024; // -0.69907 * 2^20
338 constexpr i32 c2 = 381136; // 0.36348 * 2^20
339 constexpr i32 c3 = -111776; // -0.10660 * 2^20
340 // Extend t from 12 to 20 frac bits.
341 i32 t20 = 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) * t20) >> IFRAC);
345 acc = c1 + static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
346 acc = c0 + static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
347 i32 frac_part = static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
348 // Convert from 20 frac bits back to 12.
349 i16 frac12 = static_cast<i16>(frac_part >> (IFRAC - FRAC_BITS));
350 return from_raw(static_cast<i16>(static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS) + frac12));
351 }
static constexpr int FRAC_BITS
Definition s4x12.h:22
static constexpr i32 SCALE
Definition s4x12.h:23
static constexpr FASTLED_FORCE_INLINE s4x12 from_raw(i16 raw) FL_NOEXCEPT
Definition s4x12.h:53
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition s4x12.h:304
fl::i64 i64
Definition s16x16x4.h:222

References s4x12(), 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: