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

◆ exp2_fp()

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

Definition at line 251 of file u8x8.h.

251 {
252 u8x8 fl_val = floor(x);
253 u8x8 fr = x - fl_val;
254 i32 n = fl_val.mValue >> FRAC_BITS;
255 // Overflow check for unsigned type
256 if (n >= INT_BITS) return from_raw(0xFFFF);
257 if (n < 0) return u8x8();
258 i32 int_pow = static_cast<i32>(SCALE) << n;
259 // 4-term minimax coefficients for 2^t - 1, t in [0,1).
260 // Stored as i32 with 16 fractional bits.
261 constexpr int IFRAC = 16;
262 constexpr i32 d0 = 45427; // 0.69316 * 2^16
263 constexpr i32 d1 = 15775; // 0.24071 * 2^16
264 constexpr i32 d2 = 3497; // 0.05336 * 2^16
265 constexpr i32 d3 = 836; // 0.01276 * 2^16
266 // Extend fr from 8 to 16 frac bits.
267 i32 fr16 = static_cast<i32>(fr.mValue) << (IFRAC - FRAC_BITS);
268 // Horner: 1 + fr * (d0 + fr * (d1 + fr * (d2 + fr * d3)))
269 i32 acc = d3;
270 acc = d2 + static_cast<i32>((static_cast<i64>(acc) * fr16) >> IFRAC);
271 acc = d1 + static_cast<i32>((static_cast<i64>(acc) * fr16) >> IFRAC);
272 acc = d0 + static_cast<i32>((static_cast<i64>(acc) * fr16) >> IFRAC);
273 constexpr i32 one16 = static_cast<i32>(1) << IFRAC;
274 i32 frac_pow16 = one16 + static_cast<i32>((static_cast<i64>(acc) * fr16) >> IFRAC);
275 // Convert from 16 frac bits to 8 frac bits, then scale by int_pow.
276 i32 frac_pow8 = frac_pow16 >> (IFRAC - FRAC_BITS);
277 i32 result =
278 (int_pow * frac_pow8) >> FRAC_BITS;
279 // Clamp to u16 range
280 if (result > 0xFFFF) return from_raw(0xFFFF);
281 return from_raw(static_cast<u16>(result));
282 }
static constexpr int INT_BITS
Definition u8x8.h:21
static constexpr FASTLED_FORCE_INLINE u8x8 floor(u8x8 x) FL_NOEXCEPT
Definition u8x8.h:117
static constexpr int FRAC_BITS
Definition u8x8.h:22
static constexpr FASTLED_FORCE_INLINE u8x8 from_raw(u16 raw) FL_NOEXCEPT
Definition u8x8.h:53
static constexpr i32 SCALE
Definition u8x8.h:23
constexpr u8x8() FL_NOEXCEPT=default
fl::i64 i64
Definition s16x16x4.h:222
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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