FastLED 3.10.6
Loading...
Searching...
No Matches

◆ q16FromFloatBits()

bool fl::q16FromFloatBits ( float value,
i32 * out )

s16.16 of a float, rounded to nearest, computed from its IEEE-754 bits with integer arithmetic only – no soft-float helper on a part without an FPU (FastLED#4458).

False for NaN, infinity or |value| >= 32768.

Definition at line 292 of file device_solve.cpp.hpp.

292 {
293 if (out == nullptr) {
294 return false;
295 }
296 // Integer arithmetic on the IEEE-754 single-precision bits: sign, 8-bit
297 // biased exponent, 23-bit mantissa. A float->int cast would be a call to
298 // a soft-float helper (__aeabi_f2iz) on a part without an FPU, which is
299 // exactly what the bind path must not reach (FastLED#4458).
300 const u32 bits = fl::bit_cast<u32>(value);
301 const bool negative = (bits >> 31) != 0;
302 const i32 exponent = static_cast<i32>((bits >> 23) & 0xFFu);
303 const u32 fraction = bits & 0x7FFFFFu;
304 if (exponent == 0xFF) {
305 return false; // infinity or NaN
306 }
307 if (exponent == 0) {
308 *out = 0; // zero or subnormal: far below one s16.16 step
309 return true;
310 }
311 // value = mantissa * 2^(exponent - 150); in s16.16 that is
312 // mantissa * 2^(exponent - 134).
313 const u64 mantissa = static_cast<u64>(fraction | 0x800000u);
314 const i32 shift = exponent - 134;
315 u64 magnitude = 0;
316 if (shift >= 0) {
317 if (shift > 7) {
318 return false; // |value| >= 32768: outside s16.16
319 }
320 magnitude = mantissa << shift;
321 } else {
322 const i32 right = -shift;
323 if (right > 40) {
324 magnitude = 0;
325 } else {
326 // Round to nearest, halves away from zero -- the same rounding
327 // the float quantizers this replaces used.
328 magnitude = (mantissa + (static_cast<u64>(1) << (right - 1))) >> right;
329 }
330 }
331 if (magnitude > 0x7FFFFFFFull) {
332 return false;
333 }
334 *out = negative ? -static_cast<i32>(magnitude) : static_cast<i32>(magnitude);
335 return true;
336}
constexpr int type_rank< T >::value
To bit_cast(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:48
fl::u64 u64
Definition stdint.h:220

References bit_cast(), FL_NO_EXCEPT, and type_rank< T >::value.

Referenced by fl::anonymous_namespace{chromatic_adaptation.cpp.hpp}::adaptationWhiteQ16(), buildRgbSolveMatrixQ16(), buildStreamingPipelineQ16(), buildWhiteAllocationFromSolveQ16(), fl::anonymous_namespace{source_xyz.cpp.hpp}::sourceChromaticityQ16(), and fl::anonymous_namespace{pipeline.cpp.hpp}::wideWhiteColumnQ16().

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