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 // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
22 //     depending on your compiler.
23 //
24 
25 #include "stdio_private.h"
26 
27 #ifdef FLOAT64
28 
29 #define _NEED_IO_FLOAT64
30 
31 #include "dtoa.h"
32 
33 #include "ryu/ryu.h"
34 
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #ifdef RYU_DEBUG
41 #include <inttypes.h>
42 #include <stdio.h>
43 #endif
44 
45 #include "ryu/common.h"
46 #include "ryu/d2s_intrinsics.h"
47 
48 #define DOUBLE_MANTISSA_BITS 52
49 #define DOUBLE_EXPONENT_BITS 11
50 #define DOUBLE_BIAS 1023
51 
decimalLength17(const uint64_t v)52 static int decimalLength17(const uint64_t v) {
53 	int len = 1;
54 	uint64_t c = 10;
55 	while (c <= v) {
56 		len++;
57 		c = (c << 3) + (c << 1);
58 	}
59 	return len;
60 }
61 
62 // A floating decimal representing m * 10^e.
63 typedef struct floating_decimal_64 {
64 	uint64_t mantissa;
65 	// Decimal exponent's range is -324 to 308
66 	// inclusive, and can fit in a short if needed.
67 	int16_t exponent;
68 	int16_t olength;
69 } floating_decimal_64;
70 
71 static inline floating_decimal_64
d2d(const uint64_t ieeeMantissa,const uint32_t ieeeExponent,int max_digits,bool fmode,int max_decimals)72 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent, int max_digits, bool fmode, int max_decimals)
73 {
74 	int32_t e2;
75 	uint64_t m2;
76 	if (ieeeExponent == 0) {
77 		// We subtract 2 so that the bounds computation has 2 additional bits.
78 		e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
79 		m2 = ieeeMantissa;
80 	} else {
81 		e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
82 		m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
83 	}
84 	const bool even = (m2 & 1) == 0;
85 	const bool acceptBounds = even;
86 	/* Set if we're truncating more digits to meet format request */
87 	bool truncate_max = false;
88 
89 #ifdef RYU_DEBUG
90 	printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
91 #endif
92 
93 	// Step 2: Determine the interval of valid decimal representations.
94 	const uint64_t mv = 4 * m2;
95 	// Implicit bool -> int conversion. True is 1, false is 0.
96 	const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
97 	// We would compute mp and mm like this:
98 	// uint64_t mp = 4 * m2 + 2;
99 	// uint64_t mm = mv - 1 - mmShift;
100 
101 	// Step 3: Convert to a decimal power base using 128-bit arithmetic.
102 	uint64_t vr, vp, vm;
103 	int32_t e10;
104 	bool vmIsTrailingZeros = false;
105 	bool vrIsTrailingZeros = false;
106 	if (e2 >= 0) {
107 		// I tried special-casing q == 0, but there was no effect on performance.
108 		// This expression is slightly faster than max_int(0, log10Pow2(e2) - 1).
109 		const uint32_t q = log10Pow2(e2) - (e2 > 3);
110 		e10 = (int32_t) q;
111 		const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
112 		const int32_t i = -e2 + (int32_t) q + k;
113 		uint64_t pow5[2];
114 		__double_computeInvPow5(q, pow5);
115 		vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift);
116 #ifdef RYU_DEBUG
117 		printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
118 		printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
119 #endif
120 		if (q <= 21) {
121 			// This should use q <= 22, but I think 21 is also safe. Smaller values
122 			// may still be safe, but it's more difficult to reason about them.
123 			// Only one of mp, mv, and mm can be a multiple of 5, if any.
124 			const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv));
125 			if (mvMod5 == 0) {
126 				vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
127 			} else if (acceptBounds) {
128 				// Same as min_int(e2 + (~mm & 1), pow5Factor(mm)) >= q
129 				// <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
130 				// <=> true && pow5Factor(mm) >= q, since e2 >= q.
131 				vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
132 			} else {
133 				// Same as min_int(e2 + 1, pow5Factor(mp)) >= q.
134 				vp -= multipleOfPowerOf5(mv + 2, q);
135 			}
136 		}
137 	} else {
138 		// This expression is slightly faster than max_int(0, log10Pow5(-e2) - 1).
139 		const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
140 		e10 = (int32_t) q + e2;
141 		const int32_t i = -e2 - (int32_t) q;
142 		const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
143 		const int32_t j = (int32_t) q - k;
144 		uint64_t pow5[2];
145 		__double_computePow5(i, pow5);
146 		vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift);
147 
148 #ifdef RYU_DEBUG
149 		printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
150 		printf("%u %d %d %d\n", q, i, k, j);
151 		printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
152 #endif
153 		if (q <= 1) {
154 			// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
155 			// mv = 4 * m2, so it always has at least two trailing 0 bits.
156 			vrIsTrailingZeros = true;
157 			if (acceptBounds) {
158 				// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
159 				vmIsTrailingZeros = mmShift == 1;
160 			} else {
161 				// mp = mv + 2, so it always has at least one trailing 0 bit.
162 				--vp;
163 			}
164 		} else if (q < 63) { // TODO(ulfjack): Use a tighter bound here.
165 			// We want to know if the full product has at least q trailing zeros.
166 			// We need to compute min_int(p2(mv), p5(mv) - e2) >= q
167 			// <=> p2(mv) >= q && p5(mv) - e2 >= q
168 			// <=> p2(mv) >= q (because -e2 >= q)
169 			vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
170 #ifdef RYU_DEBUG
171 			printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
172 #endif
173 		}
174 	}
175 #ifdef RYU_DEBUG
176 	printf("e10=%d\n", e10);
177 	printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
178 	printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
179 	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
180 #endif
181 
182 	// Step 4: Find the shortest decimal representation in the interval of valid representations.
183 	int32_t removed = 0;
184 	uint8_t lastRemovedDigit = 0;
185 	uint64_t output;
186 	// On average, we remove ~2 digits.
187 	// General case, which happens rarely (~0.7%).
188 
189 	/* If limiting decimals, then limit the max digits
190 	 * to no more than the number of digits left of the decimal
191 	 * plus the number of digits right of the decimal
192 	 *
193 	 * exp:          exponent value. If negative, there are
194 	 *		 -exp - 1 zeros left of the first non-zero
195 	 *               digit in 'f' format. If non-negative,
196 	 *               there are exp digits to the left of
197 	 *               the decimal point
198 	 *
199 	 * max_decimals: Only used in 'f' format. Round to this many
200 	 *               digits to the right of the decimal point
201 	 *               (left if negative)
202 	 *
203 	 * max_digits:	 We can't convert more than this number of digits given
204 	 *               the limits of the buffer
205 	 */
206 
207 	int save_max_digits = max_digits;
208 	if(fmode) {
209 		int exp = e10 + decimalLength17(vr) - 1;
210 		/*
211 		 * This covers two cases:
212 		 *
213 		 * When exp is < 0, there are -exp-1 zeros taking up
214 		 * space before we can display any of the real digits,
215 		 * so we have to subtract those off max_decimals before
216 		 * we round that (max_decimals - (-exp - 1)). This
217 		 * may end up less than zero, in which case we have
218 		 * no digits to display.
219 		 *
220 		 * When exp >= 0, there are exp + 1 digits left of the
221 		 * decimal point *plus* max_decimals right of the
222 		 * decimal point that need to be generated
223 		 *
224 		 * A single expression gives the right answer in both
225 		 * cases, which is kinda cool
226 		 */
227 		max_digits = min_int(max_digits, max_int(1, max_decimals + exp + 1));
228 	}
229 
230 	for (;;) {
231 		const uint64_t vpDiv10 = div10(vp);
232 		const uint64_t vmDiv10 = div10(vm);
233 		if (vpDiv10 <= vmDiv10) {
234 			if (decimalLength17(vr) <= max_digits || (max_digits == 0 && vr == 0))
235 				break;
236 			else
237 				truncate_max = true;
238 		}
239 		const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
240 		const uint64_t vrDiv10 = div10(vr);
241 		const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
242 		vmIsTrailingZeros &= vmMod10 == 0;
243 		vrIsTrailingZeros &= lastRemovedDigit == 0;
244 		lastRemovedDigit = (uint8_t) vrMod10;
245 		vr = vrDiv10;
246 		vp = vpDiv10;
247 		vm = vmDiv10;
248 		++removed;
249 	}
250 #ifdef RYU_DEBUG
251 	printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
252 	printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
253 #endif
254 	if (vmIsTrailingZeros) {
255 		for (;;) {
256 			const uint64_t vmDiv10 = div10(vm);
257 			const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
258 			if (vmMod10 != 0) {
259 				break;
260 			}
261 			const uint64_t vpDiv10 = div10(vp);
262 			const uint64_t vrDiv10 = div10(vr);
263 			const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
264 			vrIsTrailingZeros &= lastRemovedDigit == 0;
265 			lastRemovedDigit = (uint8_t) vrMod10;
266 			vr = vrDiv10;
267 			vp = vpDiv10;
268 			vm = vmDiv10;
269 			++removed;
270 		}
271 	}
272 #ifdef RYU_DEBUG
273 	printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
274 	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
275 #endif
276 	if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
277 		// Round even if the exact number is .....50..0.
278 		lastRemovedDigit = 4;
279 	}
280 
281 	output = vr;
282 	e10 += removed;
283 
284 	// We need to take vr + 1 if vr is outside bounds or we need to round up.
285 	// I don't know if the 'truncate_max' case is entirely correct; need some tests
286 	uint8_t carry = ((!truncate_max && vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
287 	output += carry;
288 
289 	int len = decimalLength17(output);
290 
291 	if (carry) {
292 		/* This can only happen if output has carried out of the top digit */
293 		if (len > max_digits) {
294 
295 			/* Recompute max digits in this case */
296                         if(fmode) {
297 				int exp = e10 + len - 1;
298 				/* max_decimals comes in biased by 1 to flag the 'f' case */
299 				max_digits = min_int(save_max_digits, max_int(0, max_decimals + exp + 1));
300 			}
301 
302 			if (len > max_digits) {
303 				output += 5;
304 				output /= 10;
305 				e10++;
306 				len--;
307 			}
308 		}
309 	}
310 	if (len > max_digits)
311 		len = max_digits;
312 
313 #ifdef RYU_DEBUG
314 	printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
315 	printf("O=%" PRIu64 "\n", output);
316 	printf("EXP=%d\n", exp);
317 #endif
318 
319 	floating_decimal_64 fd;
320 	fd.exponent = e10;
321 	fd.olength = len;
322 	fd.mantissa = output;
323 	return fd;
324 }
325 
d2d_small_int(const uint64_t ieeeMantissa,const uint32_t ieeeExponent,floating_decimal_64 * const v)326 static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
327 				 floating_decimal_64* const v) {
328 	const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
329 	const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
330 
331 	if (e2 > 0) {
332 		// f = m2 * 2^e2 >= 2^53 is an integer.
333 		// Ignore this case for now.
334 		return false;
335 	}
336 
337 	if (e2 < -52) {
338 		// f < 1.
339 		return false;
340 	}
341 
342 	// Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
343 	// Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
344 	const uint64_t mask = (1ull << -e2) - 1;
345 	const uint64_t fraction = m2 & mask;
346 	if (fraction != 0) {
347 		return false;
348 	}
349 
350 	// f is an integer in the range [1, 2^53).
351 	// Note: mantissa might contain trailing (decimal) 0's.
352 	// Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
353 	v->mantissa = m2 >> -e2;
354 	v->exponent = 0;
355 	return true;
356 }
357 
358 int
__dtoa_engine(FLOAT64 x,struct dtoa * dtoa,int max_digits,bool fmode,int max_decimals)359 __dtoa_engine(FLOAT64 x, struct dtoa *dtoa, int max_digits, bool fmode, int max_decimals)
360 {
361 	// Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
362 	const uint64_t bits = ryu64_to_bits(x);
363 
364 #ifdef RYU_DEBUG
365 	printf("IN=");
366 	for (int32_t bit = 63; bit >= 0; --bit) {
367 		printf("%d", (int) ((bits >> bit) & 1));
368 	}
369 	printf("\n");
370 #endif
371 
372 	// Decode bits into sign, mantissa, and exponent.
373 	const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
374 	const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
375 	const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
376 
377 	uint8_t	flags = 0;
378 
379 	if (ieeeSign)
380 		flags |= DTOA_MINUS;
381 	if (ieeeExponent == 0 && ieeeMantissa == 0) {
382 		flags |= DTOA_ZERO;
383 		dtoa->digits[0] = '0';
384 		dtoa->flags = flags;
385 		dtoa->exp = 0;
386 		return 1;
387 	}
388 	if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u)) {
389 		if (ieeeMantissa) {
390 			flags |= DTOA_NAN;
391 		} else {
392 			flags |= DTOA_INF;
393 		}
394 		dtoa->flags = flags;
395 		return 0;
396 	}
397 
398 	floating_decimal_64 v;
399 	const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
400 	if (isSmallInt && 0) {
401 		// For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
402 		// For scientific notation we need to move these zeros into the exponent.
403 		// (This is not needed for fixed-point notation, so it might be beneficial to trim
404 		// trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
405 		for (;;) {
406 			const uint64_t q = div10(v.mantissa);
407 			const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q);
408 			if (r != 0) {
409 				break;
410 			}
411 			v.mantissa = q;
412 			++v.exponent;
413 		}
414 	} else {
415 		v = d2d(ieeeMantissa, ieeeExponent, max_digits, fmode, max_decimals);
416 	}
417 
418 	uint64_t mant = v.mantissa;
419 	int32_t olength = v.olength;
420 	int32_t exp = v.exponent + olength - 1;
421 
422 	int i;
423 
424 	for (i = 0; i < olength; i++) {
425 		dtoa->digits[olength - i - 1] = (mant % 10) + '0';
426 		mant /= 10;
427 	}
428 
429 	dtoa->exp = exp;
430 	dtoa->flags = flags;
431 	return olength;
432 }
433 
434 #endif /* FLOAT64 */
435