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

◆ exp2_fp()

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

Definition at line 375 of file s16x16.h.

375 {
376 s16x16 fl_val = floor(x);
377 s16x16 fr = x - fl_val;
378 i32 n = fl_val.mValue >> FRAC_BITS;
379 if (n >= INT_BITS - 1) return from_raw(0x7FFFFFFF);
380 if (n < -FRAC_BITS) return s16x16();
381 i32 int_pow;
382 if (n >= 0) {
383 int_pow = static_cast<i32>(SCALE) << n;
384 } else {
385 int_pow = static_cast<i32>(SCALE) >> (-n);
386 }
387 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
388 // Stored as i64 with 24 fractional bits. Max product ~2^48, fits i64.
389 constexpr int IFRAC = 24;
390 constexpr i64 d0 = 11629376LL; // 0.69316 * 2^24
391 constexpr i64 d1 = 4038400LL; // 0.24071 * 2^24
392 constexpr i64 d2 = 895232LL; // 0.05336 * 2^24
393 constexpr i64 d3 = 214016LL; // 0.01276 * 2^24
394 // Extend fr from 16 to 24 frac bits.
395 i64 fr24 = static_cast<i64>(fr.mValue) << (IFRAC - FRAC_BITS);
396 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
397 i64 acc = d3;
398 acc = d2 + ((acc * fr24) >> IFRAC);
399 acc = d1 + ((acc * fr24) >> IFRAC);
400 acc = d0 + ((acc * fr24) >> IFRAC);
401 constexpr i64 one24 = 1LL << IFRAC;
402 i64 frac_pow24 = one24 + ((acc * fr24) >> IFRAC);
403 // Convert from 24 frac bits to 16 frac bits, then scale by int_pow.
404 i32 frac_pow16 = static_cast<i32>(frac_pow24 >> (IFRAC - FRAC_BITS));
405 i64 result =
406 (static_cast<i64>(int_pow) * frac_pow16) >> FRAC_BITS;
407 return from_raw(static_cast<i32>(result));
408 }
static constexpr i32 SCALE
Definition s16x16.h:23
constexpr FASTLED_FORCE_INLINE s16x16 floor() const FL_NOEXCEPT
Definition s16x16.h:251
static constexpr int INT_BITS
Definition s16x16.h:21
constexpr s16x16() FL_NOEXCEPT=default
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
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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