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