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

◆ exp2_fp()

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

Definition at line 354 of file s24x8.h.

354 {
355 s24x8 fl_val = floor(x);
356 s24x8 fr = x - fl_val;
357 i32 n = fl_val.mValue >> FRAC_BITS;
358 if (n >= INT_BITS - 1) return from_raw(0x7FFFFFFF);
359 if (n < -FRAC_BITS) return s24x8();
360 i32 int_pow = 0;
361 if (n >= 0) {
362 int_pow = static_cast<i32>(SCALE) << n;
363 } else {
364 int_pow = static_cast<i32>(SCALE) >> (-n);
365 }
366 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
367 // Stored as i64 with 16 fractional bits.
368 constexpr int IFRAC = 16;
369 constexpr i64 d0 = 45427LL; // 0.69316 * 2^16
370 constexpr i64 d1 = 15775LL; // 0.24071 * 2^16
371 constexpr i64 d2 = 3497LL; // 0.05336 * 2^16
372 constexpr i64 d3 = 836LL; // 0.01276 * 2^16
373 // Extend fr from 8 to 16 frac bits.
374 i64 fr16 = static_cast<i64>(fr.mValue) << (IFRAC - FRAC_BITS);
375 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
376 i64 acc = d3;
377 acc = d2 + ((acc * fr16) >> IFRAC);
378 acc = d1 + ((acc * fr16) >> IFRAC);
379 acc = d0 + ((acc * fr16) >> IFRAC);
380 constexpr i64 one16 = 1LL << IFRAC;
381 i64 frac_pow16 = one16 + ((acc * fr16) >> IFRAC);
382 // Convert from 16 frac bits to 8 frac bits, then scale by int_pow.
383 i32 frac_pow8 = static_cast<i32>(frac_pow16 >> (IFRAC - FRAC_BITS));
384 i64 result =
385 (static_cast<i64>(int_pow) * frac_pow8) >> FRAC_BITS;
386 return from_raw(static_cast<i32>(result));
387 }
constexpr s24x8() FL_NOEXCEPT=default
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 INT_BITS
Definition s24x8.h:21
static constexpr int FRAC_BITS
Definition s24x8.h:22
constexpr FASTLED_FORCE_INLINE s24x8 floor() const FL_NOEXCEPT
Definition s24x8.h:232
fl::i64 i64
Definition s16x16x4.h:222
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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