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

◆ log2_fp()

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

Definition at line 222 of file u12x4.h.

222 {
223 u32 val = static_cast<u32>(x.mValue);
224 int msb = highest_bit(val);
225 u32 int_part = msb - FRAC_BITS;
226 u32 t;
227 if (msb >= FRAC_BITS) {
228 t = static_cast<u32>(
229 (val >> (msb - FRAC_BITS)) - (SCALE));
230 } else {
231 t = static_cast<u32>(
232 (val << (FRAC_BITS - msb)) - (SCALE));
233 }
234 // 4-term minimax coefficients for log2(1+t), t in [0,1).
235 // Stored as u32 with 12 fractional bits. Max product ~2^21, fits u32 comfortably.
236 constexpr int IFRAC = 12;
237 constexpr u32 c0 = 5907; // 1.44179 * 2^12
238 constexpr u32 c1 = 2864; // 0.69907 * 2^12 (no negation for unsigned)
239 constexpr u32 c2 = 1489; // 0.36348 * 2^12
240 constexpr u32 c3 = 437; // 0.10660 * 2^12 (no negation for unsigned)
241 // Extend t from 4 to 12 frac bits.
242 u32 t12 = static_cast<u32>(t) << (IFRAC - FRAC_BITS);
243 // Simplified Horner for unsigned: t * (c0 + t * c1 + t^2 * c2 + t^3 * c3)
244 u32 acc = c3;
245 acc = c2 + ((acc * t12) >> IFRAC);
246 acc = c1 + ((acc * t12) >> IFRAC);
247 acc = c0 + ((acc * t12) >> IFRAC);
248 u32 frac_part = (acc * t12) >> IFRAC;
249 // Convert from 12 frac bits back to 4.
250 u16 frac4 = static_cast<u16>(frac_part >> (IFRAC - FRAC_BITS));
251 return from_raw(static_cast<u16>((int_part << FRAC_BITS) + frac4));
252 }
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 FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition u12x4.h:205

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