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

◆ log2_fp()

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

Definition at line 310 of file s12x4.h.

310 {
311 u32 val = static_cast<u32>(x.mValue);
312 int msb = highest_bit(val);
313 i32 int_part = msb - FRAC_BITS;
314 i32 t;
315 if (msb >= FRAC_BITS) {
316 t = static_cast<i32>(
317 (val >> (msb - FRAC_BITS)) - (SCALE));
318 } else {
319 t = static_cast<i32>(
320 (val << (FRAC_BITS - msb)) - (SCALE));
321 }
322 // 4-term minimax coefficients for log2(1+t), t in [0,1).
323 // Stored as i32 with 12 fractional bits. Max product ~2^21, fits i32 comfortably.
324 constexpr int IFRAC = 12;
325 constexpr i32 c0 = 5907; // 1.44179 * 2^12
326 constexpr i32 c1 = -2864; // -0.69907 * 2^12
327 constexpr i32 c2 = 1489; // 0.36348 * 2^12
328 constexpr i32 c3 = -437; // -0.10660 * 2^12
329 // Extend t from 4 to 12 frac bits.
330 i32 t12 = static_cast<i32>(t) << (IFRAC - FRAC_BITS);
331 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
332 i32 acc = c3;
333 acc = c2 + ((acc * t12) >> IFRAC);
334 acc = c1 + ((acc * t12) >> IFRAC);
335 acc = c0 + ((acc * t12) >> IFRAC);
336 i32 frac_part = (acc * t12) >> IFRAC;
337 // Convert from 12 frac bits back to 4.
338 i16 frac4 = static_cast<i16>(frac_part >> (IFRAC - FRAC_BITS));
339 return from_raw(static_cast<i16>((static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS)) + frac4));
340 }
static constexpr int FRAC_BITS
Definition s12x4.h:22
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition s12x4.h:293
static constexpr i32 SCALE
Definition s12x4.h:23
static constexpr FASTLED_FORCE_INLINE s12x4 from_raw(i16 raw) FL_NOEXCEPT
Definition s12x4.h:42

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