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

◆ log2_fp()

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

Definition at line 221 of file u16x16.h.

221 {
222 u32 val = x.mValue;
223 int msb = highest_bit(val);
224 if (msb < 0) return u16x16(); // log2(0) = undefined, return 0
225
226 u32 int_part = msb - FRAC_BITS;
227 u32 t;
228 if (msb >= FRAC_BITS) {
229 t = (val >> (msb - FRAC_BITS)) - (SCALE);
230 } else {
231 t = (val << (FRAC_BITS - msb)) - (SCALE);
232 }
233
234 // 2-term polynomial coefficients for log2(1+t), t in [0,1).
235 // Stored as u64 with 24 fractional bits.
236 constexpr int IFRAC = 24;
237 constexpr u64 c0 = 24189248ULL; // 1.44179 * 2^24
238 constexpr u64 c1 = 5049984ULL; // 0.30093 * 2^24
239
240 // Extend t from 16 to 24 frac bits.
241 u64 t24 = static_cast<u64>(t) << (IFRAC - FRAC_BITS);
242
243 // Simplified Horner: c0 + c1*t, then multiply by t
244 u64 acc = c0 + ((c1 * t24) >> IFRAC);
245 u64 frac_part = (acc * t24) >> IFRAC;
246
247 // Convert from 24 frac bits back to 16.
248 u32 frac16 = static_cast<u32>(frac_part >> (IFRAC - FRAC_BITS));
249 return from_raw((int_part << FRAC_BITS) + frac16);
250 }
static constexpr FASTLED_FORCE_INLINE int highest_bit(u32 v) FL_NOEXCEPT
Definition u16x16.h:204
constexpr u16x16() FL_NOEXCEPT=default
static constexpr FASTLED_FORCE_INLINE u16x16 from_raw(u32 raw) FL_NOEXCEPT
Definition u16x16.h:53
static constexpr i32 SCALE
Definition u16x16.h:22
static constexpr int FRAC_BITS
Definition u16x16.h:21
fl::u64 u64
Definition s16x16x4.h:221

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