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

◆ exp2_fp()

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

Definition at line 345 of file s12x4.h.

345 {
346 s12x4 fl_val = floor(x);
347 s12x4 fr = x - fl_val;
348 i32 n = fl_val.mValue >> FRAC_BITS;
349 if (n >= INT_BITS - 1) return from_raw(0x7FFF);
350 if (n < -FRAC_BITS) return s12x4();
351 i32 int_pow;
352 if (n >= 0) {
353 int_pow = static_cast<i32>(SCALE) << n;
354 } else {
355 int_pow = static_cast<i32>(SCALE) >> (-n);
356 }
357 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
358 // Stored as i32 with 12 fractional bits.
359 constexpr int IFRAC = 12;
360 constexpr i32 d0 = 2839; // 0.69316 * 2^12
361 constexpr i32 d1 = 986; // 0.24071 * 2^12
362 constexpr i32 d2 = 219; // 0.05336 * 2^12
363 constexpr i32 d3 = 52; // 0.01276 * 2^12
364 // Extend fr from 4 to 12 frac bits.
365 i32 fr12 = static_cast<i32>(fr.mValue) << (IFRAC - FRAC_BITS);
366 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
367 i32 acc = d3;
368 acc = d2 + ((acc * fr12) >> IFRAC);
369 acc = d1 + ((acc * fr12) >> IFRAC);
370 acc = d0 + ((acc * fr12) >> IFRAC);
371 constexpr i32 one12 = 1 << IFRAC;
372 i32 frac_pow12 = one12 + ((acc * fr12) >> IFRAC);
373 // Convert from 12 frac bits to 4 frac bits, then scale by int_pow.
374 i32 frac_pow4 = frac_pow12 >> (IFRAC - FRAC_BITS);
375 i32 result =
376 (int_pow * frac_pow4) >> FRAC_BITS;
377 return from_raw(static_cast<i16>(result));
378 }
static constexpr int FRAC_BITS
Definition s12x4.h:22
static constexpr int INT_BITS
Definition s12x4.h:21
constexpr FASTLED_FORCE_INLINE s12x4 floor() const FL_NOEXCEPT
Definition s12x4.h:223
static constexpr i32 SCALE
Definition s12x4.h:23
constexpr s12x4() FL_NOEXCEPT=default
static constexpr FASTLED_FORCE_INLINE s12x4 from_raw(i16 raw) FL_NOEXCEPT
Definition s12x4.h:42
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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