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

Function Dragon4_PrintFloat_IEEE_binary32

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

* IEEE binary32 floating-point format * * sign: 1 bit * exponent: 8 bits * mantissa: 23 bits */

Source from the content-addressed store, hash-verified

2296 * mantissa: 23 bits
2297 */
2298static npy_uint32
2299Dragon4_PrintFloat_IEEE_binary32(
2300 Dragon4_Scratch *scratch, npy_float32 *value,
2301 Dragon4_Options *opt)
2302{
2303 char *buffer = scratch->repr;
2304 const npy_uint32 bufferSize = sizeof(scratch->repr);
2305 BigInt *bigints = scratch->bigints;
2306
2307 union
2308 {
2309 npy_float32 floatingPoint;
2310 npy_uint32 integer;
2311 } floatUnion;
2312 npy_uint32 floatExponent, floatMantissa, floatSign;
2313
2314 npy_uint32 mantissa;
2315 npy_int32 exponent;
2316 npy_uint32 mantissaBit;
2317 npy_bool hasUnequalMargins;
2318 char signbit = '\0';
2319
2320 /* deconstruct the floating point value */
2321 floatUnion.floatingPoint = *value;
2322 floatMantissa = floatUnion.integer & bitmask_u32(23);
2323 floatExponent = (floatUnion.integer >> 23) & bitmask_u32(8);
2324 floatSign = floatUnion.integer >> 31;
2325
2326 /* output the sign */
2327 if (floatSign != 0) {
2328 signbit = '-';
2329 }
2330 else if (opt->sign) {
2331 signbit = '+';
2332 }
2333
2334 /* if this is a special value */
2335 if (floatExponent == bitmask_u32(8)) {
2336 return PrintInfNan(buffer, bufferSize, floatMantissa, 6, signbit);
2337 }
2338 /* else this is a number */
2339
2340 /* factor the value into its parts */
2341 if (floatExponent != 0) {
2342 /*
2343 * normalized
2344 * The floating point equation is:
2345 * value = (1 + mantissa/2^23) * 2 ^ (exponent-127)
2346 * We convert the integer equation by factoring a 2^23 out of the
2347 * exponent
2348 * value = (1 + mantissa/2^23) * 2^23 * 2 ^ (exponent-127-23)
2349 * value = (2^23 + mantissa) * 2 ^ (exponent-127-23)
2350 * Because of the implied 1 in front of the mantissa we have 24 bits of
2351 * precision.
2352 * m = (2^23 + mantissa)
2353 * e = (exponent-127-23)
2354 */
2355 mantissa = (1UL << 23) | floatMantissa;

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