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

◆ log2_fp()

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

Definition at line 340 of file s16x16.h.

340 {
341 u32 val = static_cast<u32>(x.mValue);
342 int msb = highest_bit(val);
343 i32 int_part = msb - FRAC_BITS;
344 i32 t;
345 if (msb >= FRAC_BITS) {
346 t = static_cast<i32>(
347 (val >> (msb - FRAC_BITS)) - (SCALE));
348 } else {
349 t = static_cast<i32>(
350 (val << (FRAC_BITS - msb)) - (SCALE));
351 }
352 // 4-term minimax coefficients for log2(1+t), t in [0,1).
353 // Stored as i64 with 24 fractional bits. Max product ~2^49, fits i64.
354 constexpr int IFRAC = 24;
355 constexpr i64 c0 = 24189248LL; // 1.44179 * 2^24
356 constexpr i64 c1 = -11728384LL; // -0.69907 * 2^24
357 constexpr i64 c2 = 6098176LL; // 0.36348 * 2^24
358 constexpr i64 c3 = -1788416LL; // -0.10660 * 2^24
359 // Extend t from 16 to 24 frac bits.
360 i64 t24 = static_cast<i64>(t) << (IFRAC - FRAC_BITS);
361 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
362 i64 acc = c3;
363 acc = c2 + ((acc * t24) >> IFRAC);
364 acc = c1 + ((acc * t24) >> IFRAC);
365 acc = c0 + ((acc * t24) >> IFRAC);
366 i64 frac_part = (acc * t24) >> IFRAC;
367 // Convert from 24 frac bits back to 16.
368 i32 frac16 = static_cast<i32>(frac_part >> (IFRAC - FRAC_BITS));
369 return from_raw(static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS) + frac16);
370 }
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition s16x16.h:323
static constexpr i32 SCALE
Definition s16x16.h:23
static constexpr int FRAC_BITS
Definition s16x16.h:22
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NOEXCEPT
Definition s16x16.h:54
fl::i64 i64
Definition s16x16x4.h:222

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