MCPcopy Create free account
hub / github.com/numpy/numpy / Dragon4_PrintFloat_IEEE_binary16

Function Dragon4_PrintFloat_IEEE_binary16

numpy/core/src/multiarray/dragon4.c:2211–2289  ·  view source on GitHub ↗

* IEEE binary16 floating-point format * * sign: 1 bit * exponent: 5 bits * mantissa: 10 bits */

Source from the content-addressed store, hash-verified

2209 * mantissa: 10 bits
2210 */
2211static npy_uint32
2212Dragon4_PrintFloat_IEEE_binary16(
2213 Dragon4_Scratch *scratch, npy_half *value, Dragon4_Options *opt)
2214{
2215 char *buffer = scratch->repr;
2216 const npy_uint32 bufferSize = sizeof(scratch->repr);
2217 BigInt *bigints = scratch->bigints;
2218
2219 npy_uint16 val = *value;
2220 npy_uint32 floatExponent, floatMantissa, floatSign;
2221
2222 npy_uint32 mantissa;
2223 npy_int32 exponent;
2224 npy_uint32 mantissaBit;
2225 npy_bool hasUnequalMargins;
2226 char signbit = '\0';
2227
2228 /* deconstruct the floating point value */
2229 floatMantissa = val & bitmask_u32(10);
2230 floatExponent = (val >> 10) & bitmask_u32(5);
2231 floatSign = val >> 15;
2232
2233 /* output the sign */
2234 if (floatSign != 0) {
2235 signbit = '-';
2236 }
2237 else if (opt->sign) {
2238 signbit = '+';
2239 }
2240
2241 /* if this is a special value */
2242 if (floatExponent == bitmask_u32(5)) {
2243 return PrintInfNan(buffer, bufferSize, floatMantissa, 3, signbit);
2244 }
2245 /* else this is a number */
2246
2247 /* factor the value into its parts */
2248 if (floatExponent != 0) {
2249 /*
2250 * normalized
2251 * The floating point equation is:
2252 * value = (1 + mantissa/2^10) * 2 ^ (exponent-15)
2253 * We convert the integer equation by factoring a 2^10 out of the
2254 * exponent
2255 * value = (1 + mantissa/2^10) * 2^10 * 2 ^ (exponent-15-10)
2256 * value = (2^10 + mantissa) * 2 ^ (exponent-15-10)
2257 * Because of the implied 1 in front of the mantissa we have 10 bits of
2258 * precision.
2259 * m = (2^10 + mantissa)
2260 * e = (exponent-15-10)
2261 */
2262 mantissa = (1UL << 10) | floatMantissa;
2263 exponent = floatExponent - 15 - 10;
2264 mantissaBit = 10;
2265 hasUnequalMargins = (floatExponent != 1) && (floatMantissa == 0);
2266 }
2267 else {
2268 /*

Callers

nothing calls this directly

Calls 5

bitmask_u32Function · 0.85
PrintInfNanFunction · 0.85
LogBase2_32Function · 0.85
BigInt_Set_uint32Function · 0.85
Format_floatbitsFunction · 0.85

Tested by

no test coverage detected