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

◆ exp2_fp()

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

Definition at line 257 of file u12x4.h.

257 {
258 u12x4 fl_val = floor(x);
259 u12x4 fr = x - fl_val;
260 u32 n = fl_val.mValue >> FRAC_BITS;
261 if (n >= INT_BITS - 1) return from_raw(0xFFFF);
262 u32 int_pow = static_cast<u32>(SCALE) << n;
263 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
264 // Stored as u32 with 12 fractional bits.
265 constexpr int IFRAC = 12;
266 constexpr u32 d0 = 2839; // 0.69316 * 2^12
267 constexpr u32 d1 = 986; // 0.24071 * 2^12
268 constexpr u32 d2 = 219; // 0.05336 * 2^12
269 constexpr u32 d3 = 52; // 0.01276 * 2^12
270 // Extend fr from 4 to 12 frac bits.
271 u32 fr12 = static_cast<u32>(fr.mValue) << (IFRAC - FRAC_BITS);
272 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
273 u32 acc = d3;
274 acc = d2 + ((acc * fr12) >> IFRAC);
275 acc = d1 + ((acc * fr12) >> IFRAC);
276 acc = d0 + ((acc * fr12) >> IFRAC);
277 constexpr u32 one12 = 1 << IFRAC;
278 u32 frac_pow12 = one12 + ((acc * fr12) >> IFRAC);
279 // Convert from 12 frac bits to 4 frac bits, then scale by int_pow.
280 u32 frac_pow4 = frac_pow12 >> (IFRAC - FRAC_BITS);
281 u32 result =
282 (int_pow * frac_pow4) >> FRAC_BITS;
283 return from_raw(static_cast<u16>(result));
284 }
static constexpr FASTLED_FORCE_INLINE u12x4 floor(u12x4 x) FL_NOEXCEPT
Definition u12x4.h:128
constexpr u12x4() FL_NOEXCEPT=default
static constexpr i32 SCALE
Definition u12x4.h:22
static constexpr int FRAC_BITS
Definition u12x4.h:21
static constexpr FASTLED_FORCE_INLINE u12x4 from_raw(u16 raw) FL_NOEXCEPT
Definition u12x4.h:41
static constexpr int INT_BITS
Definition u12x4.h:20
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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