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

◆ log2_fp()

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

Definition at line 209 of file u4x12.h.

209 {
210 u32 val = static_cast<u32>(x.mValue);
211 int msb = highest_bit(val);
212 i32 int_part = msb - FRAC_BITS;
213 i32 t;
214 if (msb >= FRAC_BITS) {
215 t = static_cast<i32>(
216 (val >> (msb - FRAC_BITS)) - (SCALE));
217 } else {
218 t = static_cast<i32>(
219 (val << (FRAC_BITS - msb)) - (SCALE));
220 }
221 // 4-term minimax coefficients for log2(1+t), t in [0,1).
222 // Stored as i32 with 20 fractional bits. Max product ~2^33, fits i64 intermediate.
223 constexpr int IFRAC = 20;
224 constexpr i32 c0 = 1512456; // 1.44179 * 2^20
225 constexpr i32 c1 = -733024; // -0.69907 * 2^20
226 constexpr i32 c2 = 381136; // 0.36348 * 2^20
227 constexpr i32 c3 = -111776; // -0.10660 * 2^20
228 // Extend t from 12 to 20 frac bits.
229 i32 t20 = static_cast<i32>(t) << (IFRAC - FRAC_BITS);
230 // Horner: t * (c0 + t * (c1 + t * (c2 + t * c3)))
231 i32 acc = c3;
232 acc = c2 + static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
233 acc = c1 + static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
234 acc = c0 + static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
235 i32 frac_part = static_cast<i32>((static_cast<i64>(acc) * t20) >> IFRAC);
236 // Convert from 20 frac bits back to 12.
237 u16 frac12 = static_cast<u16>(frac_part >> (IFRAC - FRAC_BITS));
238 return from_raw(static_cast<u16>((static_cast<i32>(static_cast<u32>(int_part) << FRAC_BITS)) + frac12));
239 }
static constexpr i32 SCALE
Definition u4x12.h:22
static constexpr int FRAC_BITS
Definition u4x12.h:21
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition u4x12.h:192
static constexpr FASTLED_FORCE_INLINE u4x12 from_raw(u16 raw) FL_NOEXCEPT
Definition u4x12.h:52
fl::i64 i64
Definition s16x16x4.h:222

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