1 // Copyright 2019 Ulf Adams
2 //
3 // The contents of this file may be used under the terms of the Apache License,
4 // Version 2.0.
5 //
6 // (See accompanying file LICENSE-Apache or copy at
7 // http://www.apache.org/licenses/LICENSE-2.0)
8 //
9 // Alternatively, the contents of this file may be used under the terms of
10 // the Boost Software License, Version 1.0.
11 // (See accompanying file LICENSE-Boost or copy at
12 // https://www.boost.org/LICENSE_1_0.txt)
13 //
14 // Unless required by applicable law or agreed to in writing, this software
15 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, either express or implied.
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #ifdef RYU_DEBUG
24 #include <inttypes.h>
25 #include <stdio.h>
26 #endif
27
28 #include "ryu/common.h"
29 #include "ryu/f2s_intrinsics.h"
30
31 #define FLOAT_MANTISSA_BITS 23
32 #define FLOAT_EXPONENT_BITS 8
33 #define FLOAT_EXPONENT_BIAS 127
34
35 #if defined(_MSC_VER)
36 #include <intrin.h>
37
floor_log2(const uint32_t value)38 static inline uint32_t floor_log2(const uint32_t value) {
39 unsigned long index;
40 return _BitScanReverse(&index, value) ? index : 32;
41 }
42
43 #else
44
floor_log2(const uint32_t value)45 static inline uint32_t floor_log2(const uint32_t value) {
46 return 31 - __builtin_clz(value);
47 }
48
49 #endif
50
51 // The max function is already defined on Windows.
max32(int32_t a,int32_t b)52 static inline int32_t max32(int32_t a, int32_t b) {
53 return a < b ? b : a;
54 }
55
int32Bits2Float(uint32_t bits)56 static inline float int32Bits2Float(uint32_t bits) {
57 float f;
58 memcpy(&f, &bits, sizeof(float));
59 return f;
60 }
61
62 float
__atof_engine(uint32_t m10,int e10)63 __atof_engine(uint32_t m10, int e10)
64 {
65 #ifdef RYU_DEBUG
66 printf("m10digits = %d\n", m10);
67 printf("e10digits = %d\n", e10);
68 printf("m10 * 10^e10 = %u * 10^%d\n", m10, e10);
69 #endif
70
71 // Convert to binary float m2 * 2^e2, while retaining information about whether the conversion
72 // was exact (trailingZeros).
73 int32_t e2;
74 uint32_t m2;
75 bool trailingZeros;
76 if (e10 >= 0) {
77 // The length of m * 10^e in bits is:
78 // log2(m10 * 10^e10) = log2(m10) + e10 log2(10) = log2(m10) + e10 + e10 * log2(5)
79 //
80 // We want to compute the FLOAT_MANTISSA_BITS + 1 top-most bits (+1 for the implicit leading
81 // one in IEEE format). We therefore choose a binary output exponent of
82 // log2(m10 * 10^e10) - (FLOAT_MANTISSA_BITS + 1).
83 //
84 // We use floor(log2(5^e10)) so that we get at least this many bits; better to
85 // have an additional bit than to not have enough bits.
86 e2 = floor_log2(m10) + e10 + log2pow5(e10) - (FLOAT_MANTISSA_BITS + 1);
87
88 // We now compute [m10 * 10^e10 / 2^e2] = [m10 * 5^e10 / 2^(e2-e10)].
89 // To that end, we use the FLOAT_POW5_SPLIT table.
90 int j = e2 - e10 - ceil_log2pow5(e10) + FLOAT_POW5_BITCOUNT;
91 assert(j >= 0);
92 m2 = mulPow5divPow2(m10, e10, j);
93
94 // We also compute if the result is exact, i.e.,
95 // [m10 * 10^e10 / 2^e2] == m10 * 10^e10 / 2^e2.
96 // This can only be the case if 2^e2 divides m10 * 10^e10, which in turn requires that the
97 // largest power of 2 that divides m10 + e10 is greater than e2. If e2 is less than e10, then
98 // the result must be exact. Otherwise we use the existing multipleOfPowerOf2 function.
99 trailingZeros = e2 < e10 || (e2 - e10 < 32 && multipleOfPowerOf2_32(m10, e2 - e10));
100 } else {
101 e2 = floor_log2(m10) + e10 - ceil_log2pow5(-e10) - (FLOAT_MANTISSA_BITS + 1);
102
103 // We now compute [m10 * 10^e10 / 2^e2] = [m10 / (5^(-e10) 2^(e2-e10))].
104 int j = e2 - e10 + ceil_log2pow5(-e10) - 1 + FLOAT_POW5_INV_BITCOUNT;
105 m2 = mulPow5InvDivPow2(m10, -e10, j);
106
107 // We also compute if the result is exact, i.e.,
108 // [m10 / (5^(-e10) 2^(e2-e10))] == m10 / (5^(-e10) 2^(e2-e10))
109 //
110 // If e2-e10 >= 0, we need to check whether (5^(-e10) 2^(e2-e10)) divides m10, which is the
111 // case iff pow5(m10) >= -e10 AND pow2(m10) >= e2-e10.
112 //
113 // If e2-e10 < 0, we have actually computed [m10 * 2^(e10 e2) / 5^(-e10)] above,
114 // and we need to check whether 5^(-e10) divides (m10 * 2^(e10-e2)), which is the case iff
115 // pow5(m10 * 2^(e10-e2)) = pow5(m10) >= -e10.
116 trailingZeros = (e2 < e10 || (e2 - e10 < 32 && multipleOfPowerOf2_32(m10, e2 - e10)))
117 && multipleOfPowerOf5_32(m10, -e10);
118 }
119
120 #ifdef RYU_DEBUG
121 printf("m2 * 2^e2 = %u * 2^%d\n", m2, e2);
122 #endif
123
124 // Compute the final IEEE exponent.
125 uint32_t ieee_e2 = (uint32_t) max32(0, e2 + FLOAT_EXPONENT_BIAS + floor_log2(m2));
126
127 if (ieee_e2 > 0xfe) {
128 // Final IEEE exponent is larger than the maximum representable; return Infinity.
129 uint32_t ieee = ((uint32_t)0xffu << FLOAT_MANTISSA_BITS);
130 return int32Bits2Float(ieee);
131 }
132
133 // We need to figure out how much we need to shift m2. The tricky part is that we need to take
134 // the final IEEE exponent into account, so we need to reverse the bias and also special-case
135 // the value 0.
136 int32_t shift = (ieee_e2 == 0 ? 1 : ieee_e2) - e2 - FLOAT_EXPONENT_BIAS - FLOAT_MANTISSA_BITS;
137 assert(shift >= 0);
138 #ifdef RYU_DEBUG
139 printf("ieee_e2 = %d\n", ieee_e2);
140 printf("shift = %d\n", shift);
141 #endif
142
143 // We need to round up if the exact value is more than 0.5 above the value we computed. That's
144 // equivalent to checking if the last removed bit was 1 and either the value was not just
145 // trailing zeros or the result would otherwise be odd.
146 //
147 // We need to update trailingZeros given that we have the exact output exponent ieee_e2 now.
148 trailingZeros &= (m2 & ((1u << (shift - 1)) - 1)) == 0;
149 uint32_t lastRemovedBit = (m2 >> (shift - 1)) & 1;
150 bool roundUp = (lastRemovedBit != 0) && (!trailingZeros || (((m2 >> shift) & 1) != 0));
151
152 #ifdef RYU_DEBUG
153 printf("roundUp = %d\n", roundUp);
154 printf("ieee_m2 = %u\n", (m2 >> shift) + roundUp);
155 #endif
156 uint32_t ieee_m2 = (m2 >> shift) + roundUp;
157 assert(ieee_m2 <= (1u << (FLOAT_MANTISSA_BITS + 1)));
158 ieee_m2 &= ((uint32_t)1u << FLOAT_MANTISSA_BITS) - 1;
159 if (ieee_m2 == 0 && roundUp) {
160 // Rounding up may overflow the mantissa.
161 // In this case we move a trailing zero of the mantissa into the exponent.
162 // Due to how the IEEE represents +/-Infinity, we don't need to check for overflow here.
163 ieee_e2++;
164 }
165 uint32_t ieee = (((uint32_t)ieee_e2) << FLOAT_MANTISSA_BITS) | ieee_m2;
166 return int32Bits2Float(ieee);
167 }
168