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

◆ log2_fp()

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

Definition at line 319 of file s24x8.h.

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

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