1 /* Conversion routines for platforms that do not support 'double' directly. */
3 #include "double_conversion.h"
11 /* Note: IEE 754 standard specifies float formats as follows:
12 * Single precision: sign, 8-bit exp, 23-bit frac.
13 * Double precision: sign, 11-bit exp, 52-bit frac.
16 uint64_t float_to_double(float value)
24 /* Decompose input value */
25 sign = (in.i >> 31) & 1;
26 exponent = ((in.i >> 23) & 0xFF) - 127;
27 mantissa = in.i & 0x7FFFFF;
31 /* Special value (NaN etc.) */
34 else if (exponent == -127)
45 while (!(mantissa & 0x800000))
56 mantissa |= (uint64_t)(exponent + 1023) << 52;
57 mantissa |= (uint64_t)sign << 63;
62 float double_to_float(uint64_t value)
69 /* Decompose input value */
70 sign = (value >> 63) & 1;
71 exponent = ((value >> 52) & 0x7FF) - 1023;
72 mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
74 /* Figure if value is in range representable by floats. */
80 else if (exponent > 127)
88 else if (exponent < -150)
96 else if (exponent < -126)
99 mantissa |= 0x1000000;
100 mantissa >>= (-126 - exponent);
104 /* Round off mantissa */
105 mantissa = (mantissa + 1) >> 1;
107 /* Check if mantissa went over 2.0 */
108 if (mantissa & 0x800000)
111 mantissa &= 0x7FFFFF;
117 out.i |= (uint32_t)(exponent + 127) << 23;
118 out.i |= (uint32_t)sign << 31;