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

◆ log2_fp()

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

Definition at line 211 of file u8x8.h.

211 {
212 u32 val = static_cast<u32>(x.mValue);
213 int msb = highest_bit(val);
214 if (msb < 0) return u8x8();
215 i32 int_part = msb - FRAC_BITS;
216 i32 t;
217 if (msb >= FRAC_BITS) {
218 t = static_cast<i32>(
219 (val >> (msb - FRAC_BITS)) - (SCALE));
220 } else {
221 t = static_cast<i32>(
222 (val << (FRAC_BITS - msb)) - (SCALE));
223 }
224 // 4-term minimax coefficients for log2(1+t), t in [0,1).
225 // Stored as i32 with 16 fractional bits. Max product ~2^29, fits i32 after shift.
226 constexpr int IFRAC = 16;
227 constexpr i32 c0 = 94528; // 1.44179 * 2^16
228 constexpr i32 c1 = -45814; // -0.69907 * 2^16
229 constexpr i32 c2 = 23821; // 0.36348 * 2^16
230 constexpr i32 c3 = -6986; // -0.10660 * 2^16
231 // Extend t from 8 to 16 frac bits.
232 i32 t16 = static_cast<i32>(t) << (IFRAC - FRAC_BITS);
233 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
234 i32 acc = c3;
235 acc = c2 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
236 acc = c1 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
237 acc = c0 + static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
238 i32 frac_part = static_cast<i32>((static_cast<i64>(acc) * t16) >> IFRAC);
239 // Convert from 16 frac bits back to 8.
240 i16 frac8 = static_cast<i16>(frac_part >> (IFRAC - FRAC_BITS));
241 // Combine integer and fractional parts (handle negative integer part)
242 i32 result_raw = static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS) + frac8;
243 // Clamp to valid unsigned range
244 if (result_raw < 0) return u8x8();
245 return from_raw(static_cast<u16>(result_raw));
246 }
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
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition u8x8.h:194
constexpr u8x8() FL_NOEXCEPT=default
fl::i64 i64
Definition s16x16x4.h:222

References u8x8(), FASTLED_FORCE_INLINE, FL_NOEXCEPT, FRAC_BITS, from_raw(), highest_bit(), SCALE, fl::t, and fl::x.

Referenced by pow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: