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

◆ exp2_fp()

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

Definition at line 419 of file s16x16.h.

419 {
420 s16x16 fl_val = floor(x);
421 s16x16 fr = x - fl_val;
422 i32 n = fl_val.mValue >> FRAC_BITS;
423 if (n >= INT_BITS - 1) return from_raw(0x7FFFFFFF);
424 if (n < -FRAC_BITS) return s16x16();
425 i32 int_pow;
426 if (n >= 0) {
427 int_pow = static_cast<i32>(SCALE) << n;
428 } else {
429 int_pow = static_cast<i32>(SCALE) >> (-n);
430 }
431 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
432 // Stored as i64 with 24 fractional bits. Max product ~2^48, fits i64.
433 constexpr int IFRAC = 24;
434 constexpr i64 d0 = 11629376LL; // 0.69316 * 2^24
435 constexpr i64 d1 = 4038400LL; // 0.24071 * 2^24
436 constexpr i64 d2 = 895232LL; // 0.05336 * 2^24
437 constexpr i64 d3 = 214016LL; // 0.01276 * 2^24
438 // Extend fr from 16 to 24 frac bits.
439 i64 fr24 = static_cast<i64>(fr.mValue) << (IFRAC - FRAC_BITS);
440 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
441 i64 acc = d3;
442 acc = d2 + ((acc * fr24) >> IFRAC);
443 acc = d1 + ((acc * fr24) >> IFRAC);
444 acc = d0 + ((acc * fr24) >> IFRAC);
445 constexpr i64 one24 = 1LL << IFRAC;
446 i64 frac_pow24 = one24 + ((acc * fr24) >> IFRAC);
447 // Convert from 24 frac bits to 16 frac bits, then scale by int_pow.
448 i32 frac_pow16 = static_cast<i32>(frac_pow24 >> (IFRAC - FRAC_BITS));
449 i64 result =
450 (static_cast<i64>(int_pow) * frac_pow16) >> FRAC_BITS;
451 return from_raw(static_cast<i32>(result));
452 }
static constexpr i32 SCALE
Definition s16x16.h:25
static constexpr int INT_BITS
Definition s16x16.h:23
constexpr FASTLED_FORCE_INLINE s16x16 floor() const FL_NO_EXCEPT
Definition s16x16.h:295
static constexpr int FRAC_BITS
Definition s16x16.h:24
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
Definition s16x16.h:56
constexpr s16x16() FL_NO_EXCEPT=default
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
fl::i64 i64
Definition stdint.h:221

References s16x16(), FASTLED_FORCE_INLINE, fl::FL_NO_EXCEPT, floor(), FRAC_BITS, from_raw(), INT_BITS, mValue, SCALE, and fl::x.

Referenced by pow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: