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 * When called from fcvt, max_decimals may be less
204 * than zero, indicating that we want to round left of
205 * the decimal point. In that case, make sure we generate
206 * at least one digit
207 */
208 max_digits = min_int(max_digits, max_int(max_decimals<0, max_decimals + exp + 1));
209 }
210
211 for (;;) {
212 if (vp / 10 <= vm / 10) {
213 if (decimalLength9(vr) <= max_digits || (max_digits == 0 && vr == 0))
214 break;
215 else
216 truncate_max = true;
217 }
218 #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=23106
219 // The compiler does not realize that vm % 10 can be computed from vm / 10
220 // as vm - (vm / 10) * 10.
221 vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0;
222 #else
223 vmIsTrailingZeros &= vm % 10 == 0;
224 #endif
225 vrIsTrailingZeros &= lastRemovedDigit == 0;
226 lastRemovedDigit = (uint8_t) (vr % 10);
227 vr /= 10;
228 vp /= 10;
229 vm /= 10;
230 ++removed;
231 }
232 #ifdef RYU_DEBUG
233 printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
234 printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
235 #endif
236 if (vmIsTrailingZeros) {
237 while (vm % 10 == 0) {
238 vrIsTrailingZeros &= lastRemovedDigit == 0;
239 lastRemovedDigit = (uint8_t) (vr % 10);
240 vr /= 10;
241 vp /= 10;
242 vm /= 10;
243 ++removed;
244 }
245 }
246 #ifdef RYU_DEBUG
247 printf("%u %d\n", vr, lastRemovedDigit);
248 printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
249 #endif
250 if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
251 // Round even if the exact number is .....50..0.
252 lastRemovedDigit = 4;
253 }
254 // We need to take vr + 1 if vr is outside bounds or we need to round up.
255 output = vr;
256 e10 += removed;
257
258 uint8_t carry = ((!truncate_max && vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
259 output += carry;
260
261 int len = decimalLength9(output);
262
263 if (carry) {
264 /* This can only happen if output has carried out of the top digit */
265 if (len > max_digits) {
266
267 /* Recompute max digits in this case */
268 if(fmode) {
269 int exp = e10 + len - 1;
270 max_digits = min_int(save_max_digits, max_int(1, max_decimals + exp + 1));
271 }
272
273 if (len > max_digits) {
274 output += 5;
275 output /= 10;
276 e10++;
277 len--;
278 }
279 }
280 }
281 if (len > max_digits)
282 len = max_digits;
283
284
285 #ifdef RYU_DEBUG
286 printf("V+=%u\nV =%u\nV-=%u\n", vp, vr, vm);
287 printf("O=%u\n", output);
288 printf("EXP=%d\n", exp);
289 #endif
290
291 floating_decimal_32 fd;
292 fd.exponent = e10;
293 fd.olength = len;
294 fd.mantissa = output;
295 return fd;
296 }
297
298 int
__ftoa_engine(float x,struct dtoa * dtoa,int max_digits,bool fmode,int max_decimals)299 __ftoa_engine(float x, struct dtoa *dtoa, int max_digits, bool fmode, int max_decimals)
300 {
301 // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
302 const uint32_t bits = float_to_bits(x);
303
304 // Decode bits into sign, mantissa, and exponent.
305 const bool ieeeSign = ((bits >> (FLOAT_MANTISSA_BITS + FLOAT_EXPONENT_BITS)) & 1) != 0;
306 const uint64_t ieeeMantissa = bits & ((1ull << FLOAT_MANTISSA_BITS) - 1);
307 const uint32_t ieeeExponent = (uint32_t) ((bits >> FLOAT_MANTISSA_BITS) & ((1u << FLOAT_EXPONENT_BITS) - 1));
308
309 uint8_t flags = 0;
310
311 if (ieeeSign)
312 flags |= DTOA_MINUS;
313
314 if (ieeeExponent == 0 && ieeeMantissa == 0) {
315 flags |= DTOA_ZERO;
316 dtoa->digits[0] = '0';
317 dtoa->flags = flags;
318 dtoa->exp = 0;
319 return 1;
320 }
321 if (ieeeExponent == ((1u << FLOAT_EXPONENT_BITS) - 1u)) {
322 if (ieeeMantissa) {
323 flags |= DTOA_NAN;
324 } else {
325 flags |= DTOA_INF;
326 }
327 dtoa->flags = flags;
328 return 0;
329 }
330
331 floating_decimal_32 v;
332
333 v = f2d(ieeeMantissa, ieeeExponent, max_digits, fmode, max_decimals);
334
335 uint32_t mant = v.mantissa;
336 int32_t olength = v.olength;
337 int32_t exp = v.exponent + olength - 1;
338
339 int i;
340
341 for (i = 0; i < olength; i++) {
342 dtoa->digits[olength - i - 1] = (mant % 10) + '0';
343 mant /= 10;
344 }
345
346 dtoa->exp = exp;
347 dtoa->flags = flags;
348 return olength;
349 }
350