2 // Floating point to IEEE-754 conversion routines
5 // (C) 2019 Underground Software
7 // Since there are no guarantees vis-a-vis floating point numbers in C, we have
8 // to utilize routines like the following in order to guarantee that the thing
9 // we get out of the C compiler is an honest-to-God IEEE-754 style floating
10 // point number (since that's what the Motorola processors that we target
21 // Check for IEEE-754 conformance (C99 compilers should be OK here)
23 // The reason we do this is mainly to ensure consistency across all platforms,
24 // even those that still haven't implemented C99 compliance after other
25 // compilers have had them for decades. The long and the short of it is, there
26 // are no guarantees for floating point implementations across platforms the
27 // way there is for ints (in <stdint.h>, for example) and so we have to be
28 // careful that bad assumptions vis-a-vis floating point numbers don't creep
29 // into the codebase and cause problems similar to the ones we had when adding
30 // proper 64-bit support. Hence, the following ugliness...
32 // IEEE-745 expects the following for floats and doubles:
33 // float: exponent is 8 bits, mantissa is 24 bits
34 // double: exponent is 11 bits, mantissa is 53 bits
35 // FLT_RADIX should be 2
38 #error "FLT_RADIX: Your compiler sucks. Get a real one."
42 #if FLT_MANT_DIG != 24
43 #error "FLT_MANT_DIG: Your compiler sucks. Get a real one."
47 #if DBL_MANT_DIG != 53
48 #error "DBL_MANT_DIG: Your compiler sucks. Get a real one."
52 #if FLT_MAX_EXP != 128
53 #error "FLT_MAX_EXP: Your compiler sucks. Get a real one."
57 #if DBL_MAX_EXP != 1024
58 #error "DBL_MAX_EXP: Your compiler sucks. Get a real one."
62 // So if we get here, we can be pretty sure that a float is 4 bytes and a
63 // double is 8. IEEE-754? Maaaaaaaaybe. But we don't have to worry about that
64 // so much, as long as the token stream is OK (floats are 4 bytes, doubles are
69 uint32_t FloatToIEEE754(float f)
71 uint32_t sign = (signbit(f) ? 0x80000000 : 0);
73 // Split the float into normalized mantissa (range: (-1, -0.5], 0,
74 // [+0.5, +1)) and base-2 exponent
75 // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
76 // Also, since we want the mantissa to be non-inverted (2's complemented),
77 // we make sure to pass in a positive number (floats/doubles are *not* 2's
78 // complemented) as we already captured the sign bit above.
80 float mantissa = frexpf((f < 0 ? -f : f), &exponent);
82 // Set the exponent bias for IEEE-754 floats
85 // Check for zero, set the proper exponent if so (zero exponent means no
86 // implied leading one)
90 // Extract most significant 24 bits of mantissa
91 mantissa = ldexpf(mantissa, 24);
93 // Convert to an unsigned int
94 uint32_t ieeeVal = truncf(mantissa);
96 // ieeeVal now has the mantissa in binary format, *including* the leading 1
97 // bit; so we have to strip that bit out, since in IEEE-754, it's implied.
98 ieeeVal &= 0x007FFFFF;
100 // Finally, add in the other parts to make a proper IEEE-754 float
101 ieeeVal |= sign | ((exponent & 0xFF) << 23);
107 uint64_t DoubleToIEEE754(double d)
109 uint64_t sign = (signbit(d) ? 0x8000000000000000LL : 0);
112 // Split double into normalized mantissa (range: (-1, -0.5], 0, [+0.5, +1))
113 // and base-2 exponent
114 // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
115 // Also, since we want the mantissa to be non-inverted (2's complemented),
116 // we make sure to pass in a positive number (floats/doubles are *not* 2's
117 // complemented) as we already captured the sign bit above.
118 double mantissa = frexp((d < 0 ? -d : d), &exponent);
120 // Set the exponent bias for IEEE-754 doubles
123 // Check for zero, set the proper exponent if so
127 // Extract most significant 53 bits of mantissa
128 mantissa = ldexp(mantissa, 53);
130 // Convert to an unsigned int
131 uint64_t ieeeVal = trunc(mantissa);
133 // ieeeVal now has the mantissa in binary format, *including* the leading 1
134 // bit; so we have to strip that bit out, since in IEEE-754, it's implied.
135 ieeeVal &= 0x000FFFFFFFFFFFFF;
137 // Finally, add in the other parts to make a proper IEEE-754 double
138 ieeeVal |= sign | ((uint64_t)(exponent & 0x7FF) << 52);
144 void DoubleToExtended(double d, uint8_t out[])
146 int8_t sign = (signbit(d) ? 0x80 : 0);
148 double mantissa = frexp((d < 0 ? -d : d), &exponent);
154 mantissa = ldexp(mantissa, 64);
155 uint64_t intMant = trunc(mantissa);
157 // Motorola extended floating point is 96 bits, so we pack it into the
158 // 12-byte array that's passed in. The format is as follows: 1 bit (sign),
159 // 15 bits (exponent w/$3FFF bias), 16 bits of zero, 64 bits of mantissa.
160 out[0] = sign | ((exponent >> 8) & 0x7F);
161 out[1] = exponent & 0xFF;
164 out[4] = (intMant >> 56) & 0xFF;
165 out[5] = (intMant >> 48) & 0xFF;
166 out[6] = (intMant >> 40) & 0xFF;
167 out[7] = (intMant >> 32) & 0xFF;
168 out[8] = (intMant >> 24) & 0xFF;
169 out[9] = (intMant >> 16) & 0xFF;
170 out[10] = (intMant >> 8) & 0xFF;
171 out[11] = intMant & 0xFF;
176 // Convert a double to a DSP56001 style fixed point float.
177 // Seems to be 23 bits of float value with 1 bit (MSB) for the sign.
179 uint32_t DoubleToDSPFloat(double d)
183 warn("DSP value clamped to +1.");
188 warn("DSP value clamped to -1.");
192 // The casts are here because some compilers do weird shit. See bug #149.
193 return (uint32_t)((int32_t)trunc(round(ldexp(d, 23))));
198 // Convert a host native floating point number to a fixed point number.
200 uint64_t DoubleToFixedPoint(double d, int intBits, int fracBits)
202 uint8_t signBit = (signbit(d) ? 1 : 0);
204 // Ensure what we're working on is positive...
208 double scaleFactor = (double)(1 << fracBits);
209 uint64_t result = (uint64_t)(d * scaleFactor);
211 // Invert the result, if necessary
213 result = (result = 0xFFFFFFFFFFFFFFFFLL) + 1;