File: ftoa_ryu.c

package info (click to toggle)
picolibc 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 31,616 kB
  • sloc: ansic: 312,308; asm: 22,739; perl: 2,414; sh: 1,619; python: 1,019; pascal: 329; exp: 287; makefile: 164; xml: 40; cpp: 10
file content (355 lines) | stat: -rw-r--r-- 10,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2018 Ulf Adams
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
//    (See accompanying file LICENSE-Apache or copy at
//     http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

// Runtime compiler options:
// -DRYU_DEBUG Generate verbose debugging output to stdout.

#include "ryu/ryu.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#ifdef RYU_DEBUG
#include <stdio.h>
#endif

#include "ryu/common.h"
#include "ryu/f2s_intrinsics.h"
#include "ryu/digit_table.h"

#define FLOAT_MANTISSA_BITS 23
#define FLOAT_EXPONENT_BITS 8
#define FLOAT_BIAS 127

// Returns the number of decimal digits in v, which must not contain more than 9 digits.
static int decimalLength9(const uint32_t v) {
	int len = 1;
	uint32_t c = 10;
	while (c <= v) {
		len++;
		c = (c << 3) + (c << 1);
	}
	return len;
}

// A floating decimal representing m * 10^e.
typedef struct floating_decimal_32 {
	uint32_t mantissa;
	// Decimal exponent's range is -45 to 38
	// inclusive, and can fit in a short if needed.
	int16_t exponent;
	int16_t olength;
} floating_decimal_32;

static inline floating_decimal_32
f2d(const uint32_t ieeeMantissa, const uint32_t ieeeExponent, int max_digits, bool fmode, int max_decimals)
{
	int32_t e2;
	uint32_t m2;
	if (ieeeExponent == 0) {
		// We subtract 2 so that the bounds computation has 2 additional bits.
		e2 = 1 - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
		m2 = ieeeMantissa;
	} else {
		e2 = (int32_t) ieeeExponent - FLOAT_BIAS - FLOAT_MANTISSA_BITS - 2;
		m2 = ((uint32_t)1u << FLOAT_MANTISSA_BITS) | ieeeMantissa;
	}
	const bool even = (m2 & 1) == 0;
	const bool acceptBounds = even;
	bool truncate_max = false;

#ifdef RYU_DEBUG
	printf("-> %u * 2^%d\n", m2, e2 + 2);
#endif

	// Step 2: Determine the interval of valid decimal representations.
	const uint32_t mv = 4 * m2;
	const uint32_t mp = 4 * m2 + 2;
	// Implicit bool -> int conversion. True is 1, false is 0.
	const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
	const uint32_t mm = 4 * m2 - 1 - mmShift;

	// Step 3: Convert to a decimal power base using 64-bit arithmetic.
	uint32_t vr, vp, vm;
	int32_t e10;
	bool vmIsTrailingZeros = false;
	bool vrIsTrailingZeros = false;
	uint8_t lastRemovedDigit = 0;
	if (e2 >= 0) {
		const uint32_t q = log10Pow2(e2);
		e10 = (int32_t) q;
		const int32_t k = FLOAT_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
		const int32_t i = -e2 + (int32_t) q + k;
		vr = mulPow5InvDivPow2(mv, q, i);
		vp = mulPow5InvDivPow2(mp, q, i);
		vm = mulPow5InvDivPow2(mm, q, i);
#ifdef RYU_DEBUG
		printf("%u * 2^%d / 10^%u\n", mv, e2, q);
		printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
#endif
		if (q != 0 && (vp - 1) / 10 <= vm / 10) {
			// We need to know one removed digit even if we are not going to loop below. We could use
			// q = X - 1 above, except that would require 33 bits for the result, and we've found that
			// 32-bit arithmetic is faster even on 64-bit machines.
			const int32_t l = FLOAT_POW5_INV_BITCOUNT + pow5bits((int32_t) (q - 1)) - 1;
			lastRemovedDigit = (uint8_t) (mulPow5InvDivPow2(mv, q - 1, -e2 + (int32_t) q - 1 + l) % 10);
		}
		if (q <= 9) {
			// The largest power of 5 that fits in 24 bits is 5^10, but q <= 9 seems to be safe as well.
			// Only one of mp, mv, and mm can be a multiple of 5, if any.
			if (mv % 5 == 0) {
				vrIsTrailingZeros = multipleOfPowerOf5_32(mv, q);
			} else if (acceptBounds) {
				vmIsTrailingZeros = multipleOfPowerOf5_32(mm, q);
			} else {
				vp -= multipleOfPowerOf5_32(mp, q);
			}
		}
	} else {
		const uint32_t q = log10Pow5(-e2);
		e10 = (int32_t) q + e2;
		const int32_t i = -e2 - (int32_t) q;
		const int32_t k = pow5bits(i) - FLOAT_POW5_BITCOUNT;
		int32_t j = (int32_t) q - k;
		vr = mulPow5divPow2(mv, (uint32_t) i, j);
		vp = mulPow5divPow2(mp, (uint32_t) i, j);
		vm = mulPow5divPow2(mm, (uint32_t) i, j);
#ifdef RYU_DEBUG
		printf("%u * 5^%d / 10^%u\n", mv, -e2, q);
		printf("%u %d %d %d\n", q, i, k, j);
		printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
#endif
		if (q != 0 && (vp - 1) / 10 <= vm / 10) {
			j = (int32_t) q - 1 - (pow5bits(i + 1) - FLOAT_POW5_BITCOUNT);
			lastRemovedDigit = (uint8_t) (mulPow5divPow2(mv, (uint32_t) (i + 1), j) % 10);
		}
		if (q <= 1) {
			// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
			// mv = 4 * m2, so it always has at least two trailing 0 bits.
			vrIsTrailingZeros = true;
			if (acceptBounds) {
				// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
				vmIsTrailingZeros = mmShift == 1;
			} else {
				// mp = mv + 2, so it always has at least one trailing 0 bit.
				--vp;
			}
		} else if (q < 31) { // TODO(ulfjack): Use a tighter bound here.
			vrIsTrailingZeros = multipleOfPowerOf2_32(mv, q - 1);
#ifdef RYU_DEBUG
			printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif
		}
	}
#ifdef RYU_DEBUG
	printf("e10=%d\n", e10);
	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
	printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif

	// Step 4: Find the shortest decimal representation in the interval of valid representations.
	int32_t removed = 0;
	uint32_t output;

	/* If limiting decimals, then limit the max digits
	 * to no more than the number of digits left of the decimal
	 * plus the number of digits right of the decimal
	 *
	 * exp:          exponent value. If negative, there are
	 *		 -exp - 1 zeros left of the first non-zero
	 *               digit in 'f' format. If non-negative,
	 *               there are exp digits to the left of
	 *               the decimal point
	 *
	 * max_decimals: Only used in 'f' format. Round to this many
	 *               digits to the right of the decimal point
	 *               (left if negative)
	 *
	 * max_digits:	 We can't convert more than this number of digits given
	 *               the limits of the buffer
	 */

	int save_max_digits = max_digits;
	if(fmode) {
		int exp = e10 + decimalLength9(vr) - 1;
		/*
		 * This covers two cases:
		 *
		 * When exp is < 0, there are -exp-1 zeros taking up
		 * space before we can display any of the real digits,
		 * so we have to subtract those off max_decimals before
		 * we round that (max_decimals - (-exp - 1)). This
		 * may end up less than zero, in which case we have
		 * no digits to display.
		 *
		 * When exp >= 0, there are exp + 1 digits left of the
		 * decimal point *plus* max_decimals right of the
		 * decimal point that need to be generated
		 *
		 * A single expression gives the right answer in both
		 * cases, which is kinda cool
		 */
		max_digits = min_int(max_digits, max_int(1, max_decimals + exp + 1));
	}

	for (;;) {
		if (vp / 10 <= vm / 10) {
			if (decimalLength9(vr) <= max_digits || (max_digits == 0 && vr == 0))
				break;
			else
				truncate_max = true;
		}
#ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=23106
		// The compiler does not realize that vm % 10 can be computed from vm / 10
		// as vm - (vm / 10) * 10.
		vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0;
#else
		vmIsTrailingZeros &= vm % 10 == 0;
#endif
		vrIsTrailingZeros &= lastRemovedDigit == 0;
		lastRemovedDigit = (uint8_t) (vr % 10);
		vr /= 10;
		vp /= 10;
		vm /= 10;
		++removed;
	}
#ifdef RYU_DEBUG
	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
	printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
#endif
	if (vmIsTrailingZeros) {
		while (vm % 10 == 0) {
			vrIsTrailingZeros &= lastRemovedDigit == 0;
			lastRemovedDigit = (uint8_t) (vr % 10);
			vr /= 10;
			vp /= 10;
			vm /= 10;
			++removed;
		}
	}
#ifdef RYU_DEBUG
	printf("%u %d\n", vr, lastRemovedDigit);
	printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif
	if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
		// Round even if the exact number is .....50..0.
		lastRemovedDigit = 4;
	}
	// We need to take vr + 1 if vr is outside bounds or we need to round up.
	output = vr;
	e10 += removed;

	uint8_t carry = ((!truncate_max && vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
	output += carry;

	int len = decimalLength9(output);

	if (carry) {
		/* This can only happen if output has carried out of the top digit */
		if (len > max_digits) {

			/* Recompute max digits in this case */
                        if(fmode) {
				int exp = e10 + len - 1;
				max_digits = min_int(save_max_digits, max_int(1, max_decimals + exp + 1));
			}

			if (len > max_digits) {
				output += 5;
				output /= 10;
				e10++;
				len--;
			}
		}
	}
	if (len > max_digits)
		len = max_digits;


#ifdef RYU_DEBUG
	printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
	printf("O=%u\n", output);
	printf("EXP=%d\n", exp);
#endif

	floating_decimal_32 fd;
	fd.exponent = e10;
	fd.olength = len;
	fd.mantissa = output;
	return fd;
}

#include "ftoa_engine.h"

int
__ftoa_engine(float x, struct ftoa *ftoa, int max_digits, bool fmode, int max_decimals)
{
	// Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
	const uint32_t bits = float_to_bits(x);

	// Decode bits into sign, mantissa, and exponent.
	const bool ieeeSign = ((bits >> (FLOAT_MANTISSA_BITS + FLOAT_EXPONENT_BITS)) & 1) != 0;
	const uint64_t ieeeMantissa = bits & ((1ull << FLOAT_MANTISSA_BITS) - 1);
	const uint32_t ieeeExponent = (uint32_t) ((bits >> FLOAT_MANTISSA_BITS) & ((1u << FLOAT_EXPONENT_BITS) - 1));

	uint8_t	flags = 0;

	if (ieeeSign)
		flags |= FTOA_MINUS;

	if (ieeeExponent == 0 && ieeeMantissa == 0) {
		flags |= FTOA_ZERO;
		ftoa->digits[0] = '0';
		ftoa->digits[1] = '\0';
		ftoa->flags = flags;
		ftoa->exp = 0;
		return 1;
	}
	if (ieeeExponent == ((1u << FLOAT_EXPONENT_BITS) - 1u)) {
		if (ieeeMantissa) {
			flags |= FTOA_NAN;
		} else {
			flags |= FTOA_INF;
		}
		ftoa->flags = flags;
		return 0;
	}

	floating_decimal_32 v;

	v = f2d(ieeeMantissa, ieeeExponent, max_digits, fmode, max_decimals);

	uint32_t mant = v.mantissa;
	int32_t olength = v.olength;
	int32_t exp = v.exponent + olength - 1;

	int i;

	for (i = 0; i < olength; i++) {
		ftoa->digits[olength - i - 1] = (mant % 10) + '0';
		mant /= 10;
	}
	ftoa->digits[olength] = '\0';

	ftoa->exp = exp;
	ftoa->flags = flags;
	return olength;
}