MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ decode_small_float()

MAYAFLUX_API double MayaFlux::Kakshya::decode_small_float ( uint32_t bits,
int exponent_bits,
int mantissa_bits )
nodiscardnoexcept

Decode a binary floating point pattern with the given field widths.

The pattern is sign, exponent and mantissa from the top, with an IEEE style bias and an all-ones exponent for infinity and NaN. Half is 5 and 10, bfloat16 is 8 and 7.

Definition at line 74 of file SmallFloat.cpp.

75{
76 const int bias = (1 << (exponent_bits - 1)) - 1;
77 const uint32_t exponent_all = (1U << exponent_bits) - 1U;
78 const bool negative = ((bits >> (exponent_bits + mantissa_bits)) & 1U) != 0U;
79 const uint32_t exponent = (bits >> mantissa_bits) & exponent_all;
80 const uint32_t mantissa = bits & ((1U << mantissa_bits) - 1U);
81
82 double value {};
83 if (exponent == exponent_all) {
84 value = mantissa != 0U ? std::numeric_limits<double>::quiet_NaN() : std::numeric_limits<double>::infinity();
85 } else if (exponent == 0U) {
86 value = std::ldexp(static_cast<double>(mantissa), 1 - bias - mantissa_bits);
87 } else {
88 value = std::ldexp(static_cast<double>(mantissa | (1U << mantissa_bits)),
89 static_cast<int>(exponent) - bias - mantissa_bits);
90 }
91 return negative ? -value : value;
92}