1 // Copyright 2018 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 // Runtime compiler options:
19 // -DRYU_DEBUG Generate verbose debugging output to stdout.
20 
21 #define _NEED_IO_FLOAT32
22 
23 #include "dtoa.h"
24 #include "ryu/ryu.h"
25 
26 #include "ryu/common.h"
27 #include "ryu/f2s_intrinsics.h"
28 #include "ryu/digit_table.h"
29 
30 #define FLOAT_MANTISSA_BITS 23
31 #define FLOAT_EXPONENT_BITS 8
32 #define FLOAT_BIAS 127
33 
34 // Returns the number of decimal digits in v, which must not contain more than 9 digits.
decimalLength9(const uint32_t v)35 static int decimalLength9(const uint32_t v) {
36 	int len = 1;
37 	uint32_t c = 10;
38 	while (c <= v) {
39 		len++;
40 		c = (c << 3) + (c << 1);
41 	}
42 	return len;
43 }
44 
45 // A floating decimal representing m * 10^e.
46 typedef struct floating_decimal_32 {
47 	uint32_t mantissa;
48 	// Decimal exponent's range is -45 to 38
49 	// inclusive, and can fit in a short if needed.
50 	int16_t exponent;
51 	int16_t olength;
52 } floating_decimal_32;
53 
54 static inline floating_decimal_32
f2d(const uint32_t ieeeMantissa,const uint32_t ieeeExponent,int max_digits,bool fmode,int max_decimals)55 f2d(const uint32_t ieeeMantissa, const uint32_t ieeeExponent, int max_digits, bool fmode, int max_decimals)
56 {
57 	int32_t e2;
58 	uint32_t m2;
59 	if (ieeeExponent == 0) {
60 		// We subtract 2 so that the bounds computation has 2 additional bits.
61 		e2 = 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
62 		m2 = ieeeMantissa;
63 	} else {
64 		e2 = (int32_t) ieeeExponent - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
65 		m2 = ((uint32_t)1u << FLOAT_MANTISSA_BITS) | ieeeMantissa;
66 	}
67 	const bool even = (m2 & 1) == 0;
68 	const bool acceptBounds = even;
69 	bool truncate_max = false;
70 
71 #ifdef RYU_DEBUG
72 	printf("-> %u * 2^%d\n", m2, e2 + 2);
73 #endif
74 
75 	// Step 2: Determine the interval of valid decimal representations.
76 	const uint32_t mv = 4 * m2;
77 	const uint32_t mp = 4 * m2 + 2;
78 	// Implicit bool -> int conversion. True is 1, false is 0.
79 	const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
80 	const uint32_t mm = 4 * m2 - 1 - mmShift;
81 
82 	// Step 3: Convert to a decimal power base using 64-bit arithmetic.
83 	uint32_t vr, vp, vm;
84 	int32_t e10;
85 	bool vmIsTrailingZeros = false;
86 	bool vrIsTrailingZeros = false;
87 	uint8_t lastRemovedDigit = 0;
88 	if (e2 >= 0) {
89 		const uint32_t q = log10Pow2(e2);
90 		e10 = (int32_t) q;
91 		const int32_t k = FLOAT_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
92 		const int32_t i = -e2 + (int32_t) q + k;
93 		vr = mulPow5InvDivPow2(mv, q, i);
94 		vp = mulPow5InvDivPow2(mp, q, i);
95 		vm = mulPow5InvDivPow2(mm, q, i);
96 #ifdef RYU_DEBUG
97 		printf("%u * 2^%d / 10^%u\n", mv, e2, q);
98 		printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
99 #endif
100 		if (q != 0 && (vp - 1) / 10 <= vm / 10) {
101 			// We need to know one removed digit even if we are not going to loop below. We could use
102 			// q = X - 1 above, except that would require 33 bits for the result, and we've found that
103 			// 32-bit arithmetic is faster even on 64-bit machines.
104 			const int32_t l = FLOAT_POW5_INV_BITCOUNT + pow5bits((int32_t) (q - 1)) - 1;
105 			lastRemovedDigit = (uint8_t) (mulPow5InvDivPow2(mv, q - 1, -e2 + (int32_t) q - 1 + l) % 10);
106 		}
107 		if (q <= 9) {
108 			// The largest power of 5 that fits in 24 bits is 5^10, but q <= 9 seems to be safe as well.
109 			// Only one of mp, mv, and mm can be a multiple of 5, if any.
110 			if (mv % 5 == 0) {
111 				vrIsTrailingZeros = multipleOfPowerOf5_32(mv, q);
112 			} else if (acceptBounds) {
113 				vmIsTrailingZeros = multipleOfPowerOf5_32(mm, q);
114 			} else {
115 				vp -= multipleOfPowerOf5_32(mp, q);
116 			}
117 		}
118 	} else {
119 		const uint32_t q = log10Pow5(-e2);
120 		e10 = (int32_t) q + e2;
121 		const int32_t i = -e2 - (int32_t) q;
122 		const int32_t k = pow5bits(i) - FLOAT_POW5_BITCOUNT;
123 		int32_t j = (int32_t) q - k;
124 		vr = mulPow5divPow2(mv, (uint32_t) i, j);
125 		vp = mulPow5divPow2(mp, (uint32_t) i, j);
126 		vm = mulPow5divPow2(mm, (uint32_t) i, j);
127 #ifdef RYU_DEBUG
128 		printf("%u * 5^%d / 10^%u\n", mv, -e2, q);
129 		printf("%u %d %d %d\n", q, i, k, j);
130 		printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
131 #endif
132 		if (q != 0 && (vp - 1) / 10 <= vm / 10) {
133 			j = (int32_t) q - 1 - (pow5bits(i + 1) - FLOAT_POW5_BITCOUNT);
134 			lastRemovedDigit = (uint8_t) (mulPow5divPow2(mv, (uint32_t) (i + 1), j) % 10);
135 		}
136 		if (q <= 1) {
137 			// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
138 			// mv = 4 * m2, so it always has at least two trailing 0 bits.
139 			vrIsTrailingZeros = true;
140 			if (acceptBounds) {
141 				// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
142 				vmIsTrailingZeros = mmShift == 1;
143 			} else {
144 				// mp = mv + 2, so it always has at least one trailing 0 bit.
145 				--vp;
146 			}
147 		} else if (q < 31) { // TODO(ulfjack): Use a tighter bound here.
148 			vrIsTrailingZeros = multipleOfPowerOf2_32(mv, q - 1);
149 #ifdef RYU_DEBUG
150 			printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
151 #endif
152 		}
153 	}
154 #ifdef RYU_DEBUG
155 	printf("e10=%d\n", e10);
156 	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
157 	printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
158 	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
159 #endif
160 
161 	// Step 4: Find the shortest decimal representation in the interval of valid representations.
162 	int32_t removed = 0;
163 	uint32_t output;
164 
165 	/* If limiting decimals, then limit the max digits
166 	 * to no more than the number of digits left of the decimal
167 	 * plus the number of digits right of the decimal
168 	 *
169 	 * exp:          exponent value. If negative, there are
170 	 *		 -exp - 1 zeros left of the first non-zero
171 	 *               digit in 'f' format. If non-negative,
172 	 *               there are exp digits to the left of
173 	 *               the decimal point
174 	 *
175 	 * max_decimals: Only used in 'f' format. Round to this many
176 	 *               digits to the right of the decimal point
177 	 *               (left if negative)
178 	 *
179 	 * max_digits:	 We can't convert more than this number of digits given
180 	 *               the limits of the buffer
181 	 */
182 
183 	int save_max_digits = max_digits;
184 	if(fmode) {
185 		int exp = e10 + decimalLength9(vr) - 1;
186 		/*
187 		 * This covers two cases:
188 		 *
189 		 * When exp is < 0, there are -exp-1 zeros taking up
190 		 * space before we can display any of the real digits,
191 		 * so we have to subtract those off max_decimals before
192 		 * we round that (max_decimals - (-exp - 1)). This
193 		 * may end up less than zero, in which case we have
194 		 * no digits to display.
195 		 *
196 		 * When exp >= 0, there are exp + 1 digits left of the
197 		 * decimal point *plus* max_decimals right of the
198 		 * decimal point that need to be generated
199 		 *
200 		 * A single expression gives the right answer in both
201 		 * cases, which is kinda cool
202 		 */
203 		max_digits = min_int(max_digits, max_int(1, max_decimals + exp + 1));
204 	}
205 
206 	for (;;) {
207 		if (vp / 10 <= vm / 10) {
208 			if (decimalLength9(vr) <= max_digits || (max_digits == 0 && vr == 0))
209 				break;
210 			else
211 				truncate_max = true;
212 		}
213 #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=23106
214 		// The compiler does not realize that vm % 10 can be computed from vm / 10
215 		// as vm - (vm / 10) * 10.
216 		vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0;
217 #else
218 		vmIsTrailingZeros &= vm % 10 == 0;
219 #endif
220 		vrIsTrailingZeros &= lastRemovedDigit == 0;
221 		lastRemovedDigit = (uint8_t) (vr % 10);
222 		vr /= 10;
223 		vp /= 10;
224 		vm /= 10;
225 		++removed;
226 	}
227 #ifdef RYU_DEBUG
228 	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
229 	printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
230 #endif
231 	if (vmIsTrailingZeros) {
232 		while (vm % 10 == 0) {
233 			vrIsTrailingZeros &= lastRemovedDigit == 0;
234 			lastRemovedDigit = (uint8_t) (vr % 10);
235 			vr /= 10;
236 			vp /= 10;
237 			vm /= 10;
238 			++removed;
239 		}
240 	}
241 #ifdef RYU_DEBUG
242 	printf("%u %d\n", vr, lastRemovedDigit);
243 	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
244 #endif
245 	if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
246 		// Round even if the exact number is .....50..0.
247 		lastRemovedDigit = 4;
248 	}
249 	// We need to take vr + 1 if vr is outside bounds or we need to round up.
250 	output = vr;
251 	e10 += removed;
252 
253 	uint8_t carry = ((!truncate_max && vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
254 	output += carry;
255 
256 	int len = decimalLength9(output);
257 
258 	if (carry) {
259 		/* This can only happen if output has carried out of the top digit */
260 		if (len > max_digits) {
261 
262 			/* Recompute max digits in this case */
263                         if(fmode) {
264 				int exp = e10 + len - 1;
265 				max_digits = min_int(save_max_digits, max_int(1, max_decimals + exp + 1));
266 			}
267 
268 			if (len > max_digits) {
269 				output += 5;
270 				output /= 10;
271 				e10++;
272 				len--;
273 			}
274 		}
275 	}
276 	if (len > max_digits)
277 		len = max_digits;
278 
279 
280 #ifdef RYU_DEBUG
281 	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
282 	printf("O=%u\n", output);
283 	printf("EXP=%d\n", exp);
284 #endif
285 
286 	floating_decimal_32 fd;
287 	fd.exponent = e10;
288 	fd.olength = len;
289 	fd.mantissa = output;
290 	return fd;
291 }
292 
293 int
__ftoa_engine(float x,struct dtoa * dtoa,int max_digits,bool fmode,int max_decimals)294 __ftoa_engine(float x, struct dtoa *dtoa, int max_digits, bool fmode, int max_decimals)
295 {
296 	// Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
297 	const uint32_t bits = float_to_bits(x);
298 
299 	// Decode bits into sign, mantissa, and exponent.
300 	const bool ieeeSign = ((bits >> (FLOAT_MANTISSA_BITS + FLOAT_EXPONENT_BITS)) & 1) != 0;
301 	const uint64_t ieeeMantissa = bits & ((1ull << FLOAT_MANTISSA_BITS) - 1);
302 	const uint32_t ieeeExponent = (uint32_t) ((bits >> FLOAT_MANTISSA_BITS) & ((1u << FLOAT_EXPONENT_BITS) - 1));
303 
304 	uint8_t	flags = 0;
305 
306 	if (ieeeSign)
307 		flags |= DTOA_MINUS;
308 
309 	if (ieeeExponent == 0 && ieeeMantissa == 0) {
310 		flags |= DTOA_ZERO;
311 		dtoa->digits[0] = '0';
312 		dtoa->flags = flags;
313 		dtoa->exp = 0;
314 		return 1;
315 	}
316 	if (ieeeExponent == ((1u << FLOAT_EXPONENT_BITS) - 1u)) {
317 		if (ieeeMantissa) {
318 			flags |= DTOA_NAN;
319 		} else {
320 			flags |= DTOA_INF;
321 		}
322 		dtoa->flags = flags;
323 		return 0;
324 	}
325 
326 	floating_decimal_32 v;
327 
328 	v = f2d(ieeeMantissa, ieeeExponent, max_digits, fmode, max_decimals);
329 
330 	uint32_t mant = v.mantissa;
331 	int32_t olength = v.olength;
332 	int32_t exp = v.exponent + olength - 1;
333 
334 	int i;
335 
336 	for (i = 0; i < olength; i++) {
337 		dtoa->digits[olength - i - 1] = (mant % 10) + '0';
338 		mant /= 10;
339 	}
340 
341 	dtoa->exp = exp;
342 	dtoa->flags = flags;
343 	return olength;
344 }
345