FastLED 3.10.6
Loading...
Searching...
No Matches

◆ log2_fp()

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

Definition at line 384 of file s16x16.h.

384 {
385 u32 val = static_cast<u32>(x.mValue);
386 int msb = highest_bit(val);
387 i32 int_part = msb - FRAC_BITS;
388 i32 t;
389 if (msb >= FRAC_BITS) {
390 t = static_cast<i32>(
391 (val >> (msb - FRAC_BITS)) - (SCALE));
392 } else {
393 t = static_cast<i32>(
394 (val << (FRAC_BITS - msb)) - (SCALE));
395 }
396 // 4-term minimax coefficients for log2(1+t), t in [0,1).
397 // Stored as i64 with 24 fractional bits. Max product ~2^49, fits i64.
398 constexpr int IFRAC = 24;
399 constexpr i64 c0 = 24189248LL; // 1.44179 * 2^24
400 constexpr i64 c1 = -11728384LL; // -0.69907 * 2^24
401 constexpr i64 c2 = 6098176LL; // 0.36348 * 2^24
402 constexpr i64 c3 = -1788416LL; // -0.10660 * 2^24
403 // Extend t from 16 to 24 frac bits.
404 i64 t24 = static_cast<i64>(t) << (IFRAC - FRAC_BITS);
405 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
406 i64 acc = c3;
407 acc = c2 + ((acc * t24) >> IFRAC);
408 acc = c1 + ((acc * t24) >> IFRAC);
409 acc = c0 + ((acc * t24) >> IFRAC);
410 i64 frac_part = (acc * t24) >> IFRAC;
411 // Convert from 24 frac bits back to 16.
412 i32 frac16 = static_cast<i32>(frac_part >> (IFRAC - FRAC_BITS));
413 return from_raw(static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS) + frac16);
414 }
static constexpr i32 SCALE
Definition s16x16.h:25
static constexpr int FRAC_BITS
Definition s16x16.h:24
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NO_EXCEPT
Definition s16x16.h:367
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
Definition s16x16.h:56
fl::i64 i64
Definition stdint.h:221

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