1*b1e83836Smrg // Copyright 2018 Ulf Adams
2*b1e83836Smrg //
3*b1e83836Smrg // The contents of this file may be used under the terms of the Apache License,
4*b1e83836Smrg // Version 2.0.
5*b1e83836Smrg //
6*b1e83836Smrg // (See accompanying file LICENSE-Apache or copy at
7*b1e83836Smrg // http://www.apache.org/licenses/LICENSE-2.0)
8*b1e83836Smrg //
9*b1e83836Smrg // Alternatively, the contents of this file may be used under the terms of
10*b1e83836Smrg // the Boost Software License, Version 1.0.
11*b1e83836Smrg // (See accompanying file LICENSE-Boost or copy at
12*b1e83836Smrg // https://www.boost.org/LICENSE_1_0.txt)
13*b1e83836Smrg //
14*b1e83836Smrg // Unless required by applicable law or agreed to in writing, this software
15*b1e83836Smrg // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1e83836Smrg // KIND, either express or implied.
17*b1e83836Smrg
18*b1e83836Smrg // Runtime compiler options:
19*b1e83836Smrg // -DRYU_DEBUG Generate verbose debugging output to stdout.
20*b1e83836Smrg //
21*b1e83836Smrg // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
22*b1e83836Smrg // depending on your compiler.
23*b1e83836Smrg //
24*b1e83836Smrg // -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
25*b1e83836Smrg // required power of 5, only store every 26th entry, and compute
26*b1e83836Smrg // intermediate values with a multiplication. This reduces the lookup table
27*b1e83836Smrg // size by about 10x (only one case, and only double) at the cost of some
28*b1e83836Smrg // performance. Currently requires MSVC intrinsics.
29*b1e83836Smrg
30*b1e83836Smrg
31*b1e83836Smrg
32*b1e83836Smrg #ifdef RYU_DEBUG
33*b1e83836Smrg #endif
34*b1e83836Smrg
35*b1e83836Smrg
36*b1e83836Smrg // Include either the small or the full lookup tables depending on the mode.
37*b1e83836Smrg #if defined(RYU_OPTIMIZE_SIZE)
38*b1e83836Smrg #else
39*b1e83836Smrg #endif
40*b1e83836Smrg
41*b1e83836Smrg #define DOUBLE_MANTISSA_BITS 52
42*b1e83836Smrg #define DOUBLE_EXPONENT_BITS 11
43*b1e83836Smrg #define DOUBLE_BIAS 1023
44*b1e83836Smrg
decimalLength17(const uint64_t v)45*b1e83836Smrg static inline uint32_t decimalLength17(const uint64_t v) {
46*b1e83836Smrg // This is slightly faster than a loop.
47*b1e83836Smrg // The average output length is 16.38 digits, so we check high-to-low.
48*b1e83836Smrg // Function precondition: v is not an 18, 19, or 20-digit number.
49*b1e83836Smrg // (17 digits are sufficient for round-tripping.)
50*b1e83836Smrg assert(v < 100000000000000000L);
51*b1e83836Smrg if (v >= 10000000000000000L) { return 17; }
52*b1e83836Smrg if (v >= 1000000000000000L) { return 16; }
53*b1e83836Smrg if (v >= 100000000000000L) { return 15; }
54*b1e83836Smrg if (v >= 10000000000000L) { return 14; }
55*b1e83836Smrg if (v >= 1000000000000L) { return 13; }
56*b1e83836Smrg if (v >= 100000000000L) { return 12; }
57*b1e83836Smrg if (v >= 10000000000L) { return 11; }
58*b1e83836Smrg if (v >= 1000000000L) { return 10; }
59*b1e83836Smrg if (v >= 100000000L) { return 9; }
60*b1e83836Smrg if (v >= 10000000L) { return 8; }
61*b1e83836Smrg if (v >= 1000000L) { return 7; }
62*b1e83836Smrg if (v >= 100000L) { return 6; }
63*b1e83836Smrg if (v >= 10000L) { return 5; }
64*b1e83836Smrg if (v >= 1000L) { return 4; }
65*b1e83836Smrg if (v >= 100L) { return 3; }
66*b1e83836Smrg if (v >= 10L) { return 2; }
67*b1e83836Smrg return 1;
68*b1e83836Smrg }
69*b1e83836Smrg
70*b1e83836Smrg // A floating decimal representing m * 10^e.
71*b1e83836Smrg typedef struct floating_decimal_64 {
72*b1e83836Smrg uint64_t mantissa;
73*b1e83836Smrg // Decimal exponent's range is -324 to 308
74*b1e83836Smrg // inclusive, and can fit in a short if needed.
75*b1e83836Smrg int32_t exponent;
76*b1e83836Smrg bool sign;
77*b1e83836Smrg } floating_decimal_64;
78*b1e83836Smrg
d2d(const uint64_t ieeeMantissa,const uint32_t ieeeExponent,const bool ieeeSign)79*b1e83836Smrg static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent, const bool ieeeSign) {
80*b1e83836Smrg int32_t e2;
81*b1e83836Smrg uint64_t m2;
82*b1e83836Smrg if (ieeeExponent == 0) {
83*b1e83836Smrg // We subtract 2 so that the bounds computation has 2 additional bits.
84*b1e83836Smrg e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
85*b1e83836Smrg m2 = ieeeMantissa;
86*b1e83836Smrg } else {
87*b1e83836Smrg e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
88*b1e83836Smrg m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
89*b1e83836Smrg }
90*b1e83836Smrg const bool even = (m2 & 1) == 0;
91*b1e83836Smrg const bool acceptBounds = even;
92*b1e83836Smrg
93*b1e83836Smrg #ifdef RYU_DEBUG
94*b1e83836Smrg printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
95*b1e83836Smrg #endif
96*b1e83836Smrg
97*b1e83836Smrg // Step 2: Determine the interval of valid decimal representations.
98*b1e83836Smrg const uint64_t mv = 4 * m2;
99*b1e83836Smrg // Implicit bool -> int conversion. True is 1, false is 0.
100*b1e83836Smrg const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
101*b1e83836Smrg // We would compute mp and mm like this:
102*b1e83836Smrg // uint64_t mp = 4 * m2 + 2;
103*b1e83836Smrg // uint64_t mm = mv - 1 - mmShift;
104*b1e83836Smrg
105*b1e83836Smrg // Step 3: Convert to a decimal power base using 128-bit arithmetic.
106*b1e83836Smrg uint64_t vr, vp, vm;
107*b1e83836Smrg int32_t e10;
108*b1e83836Smrg bool vmIsTrailingZeros = false;
109*b1e83836Smrg bool vrIsTrailingZeros = false;
110*b1e83836Smrg if (e2 >= 0) {
111*b1e83836Smrg // I tried special-casing q == 0, but there was no effect on performance.
112*b1e83836Smrg // This expression is slightly faster than max(0, log10Pow2(e2) - 1).
113*b1e83836Smrg const uint32_t q = log10Pow2(e2) - (e2 > 3);
114*b1e83836Smrg e10 = (int32_t) q;
115*b1e83836Smrg const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
116*b1e83836Smrg const int32_t i = -e2 + (int32_t) q + k;
117*b1e83836Smrg #if defined(RYU_OPTIMIZE_SIZE)
118*b1e83836Smrg uint64_t pow5[2];
119*b1e83836Smrg double_computeInvPow5(q, pow5);
120*b1e83836Smrg vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift);
121*b1e83836Smrg #else
122*b1e83836Smrg vr = mulShiftAll64(m2, DOUBLE_POW5_INV_SPLIT[q], i, &vp, &vm, mmShift);
123*b1e83836Smrg #endif
124*b1e83836Smrg #ifdef RYU_DEBUG
125*b1e83836Smrg printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
126*b1e83836Smrg printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
127*b1e83836Smrg #endif
128*b1e83836Smrg if (q <= 21) {
129*b1e83836Smrg // This should use q <= 22, but I think 21 is also safe. Smaller values
130*b1e83836Smrg // may still be safe, but it's more difficult to reason about them.
131*b1e83836Smrg // Only one of mp, mv, and mm can be a multiple of 5, if any.
132*b1e83836Smrg const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv));
133*b1e83836Smrg if (mvMod5 == 0) {
134*b1e83836Smrg vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
135*b1e83836Smrg } else if (acceptBounds) {
136*b1e83836Smrg // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
137*b1e83836Smrg // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
138*b1e83836Smrg // <=> true && pow5Factor(mm) >= q, since e2 >= q.
139*b1e83836Smrg vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
140*b1e83836Smrg } else {
141*b1e83836Smrg // Same as min(e2 + 1, pow5Factor(mp)) >= q.
142*b1e83836Smrg vp -= multipleOfPowerOf5(mv + 2, q);
143*b1e83836Smrg }
144*b1e83836Smrg }
145*b1e83836Smrg } else {
146*b1e83836Smrg // This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
147*b1e83836Smrg const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
148*b1e83836Smrg e10 = (int32_t) q + e2;
149*b1e83836Smrg const int32_t i = -e2 - (int32_t) q;
150*b1e83836Smrg const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
151*b1e83836Smrg const int32_t j = (int32_t) q - k;
152*b1e83836Smrg #if defined(RYU_OPTIMIZE_SIZE)
153*b1e83836Smrg uint64_t pow5[2];
154*b1e83836Smrg double_computePow5(i, pow5);
155*b1e83836Smrg vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift);
156*b1e83836Smrg #else
157*b1e83836Smrg vr = mulShiftAll64(m2, DOUBLE_POW5_SPLIT[i], j, &vp, &vm, mmShift);
158*b1e83836Smrg #endif
159*b1e83836Smrg #ifdef RYU_DEBUG
160*b1e83836Smrg printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
161*b1e83836Smrg printf("%u %d %d %d\n", q, i, k, j);
162*b1e83836Smrg printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
163*b1e83836Smrg #endif
164*b1e83836Smrg if (q <= 1) {
165*b1e83836Smrg // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
166*b1e83836Smrg // mv = 4 * m2, so it always has at least two trailing 0 bits.
167*b1e83836Smrg vrIsTrailingZeros = true;
168*b1e83836Smrg if (acceptBounds) {
169*b1e83836Smrg // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
170*b1e83836Smrg vmIsTrailingZeros = mmShift == 1;
171*b1e83836Smrg } else {
172*b1e83836Smrg // mp = mv + 2, so it always has at least one trailing 0 bit.
173*b1e83836Smrg --vp;
174*b1e83836Smrg }
175*b1e83836Smrg } else if (q < 63) { // TODO(ulfjack): Use a tighter bound here.
176*b1e83836Smrg // We want to know if the full product has at least q trailing zeros.
177*b1e83836Smrg // We need to compute min(p2(mv), p5(mv) - e2) >= q
178*b1e83836Smrg // <=> p2(mv) >= q && p5(mv) - e2 >= q
179*b1e83836Smrg // <=> p2(mv) >= q (because -e2 >= q)
180*b1e83836Smrg vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
181*b1e83836Smrg #ifdef RYU_DEBUG
182*b1e83836Smrg printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
183*b1e83836Smrg #endif
184*b1e83836Smrg }
185*b1e83836Smrg }
186*b1e83836Smrg #ifdef RYU_DEBUG
187*b1e83836Smrg printf("e10=%d\n", e10);
188*b1e83836Smrg printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
189*b1e83836Smrg printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
190*b1e83836Smrg printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
191*b1e83836Smrg #endif
192*b1e83836Smrg
193*b1e83836Smrg // Step 4: Find the shortest decimal representation in the interval of valid representations.
194*b1e83836Smrg int32_t removed = 0;
195*b1e83836Smrg uint8_t lastRemovedDigit = 0;
196*b1e83836Smrg uint64_t output;
197*b1e83836Smrg // On average, we remove ~2 digits.
198*b1e83836Smrg if (vmIsTrailingZeros || vrIsTrailingZeros) {
199*b1e83836Smrg // General case, which happens rarely (~0.7%).
200*b1e83836Smrg for (;;) {
201*b1e83836Smrg const uint64_t vpDiv10 = div10(vp);
202*b1e83836Smrg const uint64_t vmDiv10 = div10(vm);
203*b1e83836Smrg if (vpDiv10 <= vmDiv10) {
204*b1e83836Smrg break;
205*b1e83836Smrg }
206*b1e83836Smrg const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
207*b1e83836Smrg const uint64_t vrDiv10 = div10(vr);
208*b1e83836Smrg const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
209*b1e83836Smrg vmIsTrailingZeros &= vmMod10 == 0;
210*b1e83836Smrg vrIsTrailingZeros &= lastRemovedDigit == 0;
211*b1e83836Smrg lastRemovedDigit = (uint8_t) vrMod10;
212*b1e83836Smrg vr = vrDiv10;
213*b1e83836Smrg vp = vpDiv10;
214*b1e83836Smrg vm = vmDiv10;
215*b1e83836Smrg ++removed;
216*b1e83836Smrg }
217*b1e83836Smrg #ifdef RYU_DEBUG
218*b1e83836Smrg printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
219*b1e83836Smrg printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
220*b1e83836Smrg #endif
221*b1e83836Smrg if (vmIsTrailingZeros) {
222*b1e83836Smrg for (;;) {
223*b1e83836Smrg const uint64_t vmDiv10 = div10(vm);
224*b1e83836Smrg const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
225*b1e83836Smrg if (vmMod10 != 0) {
226*b1e83836Smrg break;
227*b1e83836Smrg }
228*b1e83836Smrg const uint64_t vpDiv10 = div10(vp);
229*b1e83836Smrg const uint64_t vrDiv10 = div10(vr);
230*b1e83836Smrg const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
231*b1e83836Smrg vrIsTrailingZeros &= lastRemovedDigit == 0;
232*b1e83836Smrg lastRemovedDigit = (uint8_t) vrMod10;
233*b1e83836Smrg vr = vrDiv10;
234*b1e83836Smrg vp = vpDiv10;
235*b1e83836Smrg vm = vmDiv10;
236*b1e83836Smrg ++removed;
237*b1e83836Smrg }
238*b1e83836Smrg }
239*b1e83836Smrg #ifdef RYU_DEBUG
240*b1e83836Smrg printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
241*b1e83836Smrg printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
242*b1e83836Smrg #endif
243*b1e83836Smrg if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
244*b1e83836Smrg // Round even if the exact number is .....50..0.
245*b1e83836Smrg lastRemovedDigit = 4;
246*b1e83836Smrg }
247*b1e83836Smrg // We need to take vr + 1 if vr is outside bounds or we need to round up.
248*b1e83836Smrg output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
249*b1e83836Smrg } else {
250*b1e83836Smrg // Specialized for the common case (~99.3%). Percentages below are relative to this.
251*b1e83836Smrg bool roundUp = false;
252*b1e83836Smrg const uint64_t vpDiv100 = div100(vp);
253*b1e83836Smrg const uint64_t vmDiv100 = div100(vm);
254*b1e83836Smrg if (vpDiv100 > vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
255*b1e83836Smrg const uint64_t vrDiv100 = div100(vr);
256*b1e83836Smrg const uint32_t vrMod100 = ((uint32_t) vr) - 100 * ((uint32_t) vrDiv100);
257*b1e83836Smrg roundUp = vrMod100 >= 50;
258*b1e83836Smrg vr = vrDiv100;
259*b1e83836Smrg vp = vpDiv100;
260*b1e83836Smrg vm = vmDiv100;
261*b1e83836Smrg removed += 2;
262*b1e83836Smrg }
263*b1e83836Smrg // Loop iterations below (approximately), without optimization above:
264*b1e83836Smrg // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
265*b1e83836Smrg // Loop iterations below (approximately), with optimization above:
266*b1e83836Smrg // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
267*b1e83836Smrg for (;;) {
268*b1e83836Smrg const uint64_t vpDiv10 = div10(vp);
269*b1e83836Smrg const uint64_t vmDiv10 = div10(vm);
270*b1e83836Smrg if (vpDiv10 <= vmDiv10) {
271*b1e83836Smrg break;
272*b1e83836Smrg }
273*b1e83836Smrg const uint64_t vrDiv10 = div10(vr);
274*b1e83836Smrg const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
275*b1e83836Smrg roundUp = vrMod10 >= 5;
276*b1e83836Smrg vr = vrDiv10;
277*b1e83836Smrg vp = vpDiv10;
278*b1e83836Smrg vm = vmDiv10;
279*b1e83836Smrg ++removed;
280*b1e83836Smrg }
281*b1e83836Smrg #ifdef RYU_DEBUG
282*b1e83836Smrg printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
283*b1e83836Smrg printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
284*b1e83836Smrg #endif
285*b1e83836Smrg // We need to take vr + 1 if vr is outside bounds or we need to round up.
286*b1e83836Smrg output = vr + (vr == vm || roundUp);
287*b1e83836Smrg }
288*b1e83836Smrg const int32_t exp = e10 + removed;
289*b1e83836Smrg
290*b1e83836Smrg #ifdef RYU_DEBUG
291*b1e83836Smrg printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
292*b1e83836Smrg printf("O=%" PRIu64 "\n", output);
293*b1e83836Smrg printf("EXP=%d\n", exp);
294*b1e83836Smrg #endif
295*b1e83836Smrg
296*b1e83836Smrg floating_decimal_64 fd;
297*b1e83836Smrg fd.exponent = exp;
298*b1e83836Smrg fd.mantissa = output;
299*b1e83836Smrg fd.sign = ieeeSign;
300*b1e83836Smrg return fd;
301*b1e83836Smrg }
302*b1e83836Smrg
to_chars(const floating_decimal_64 v,char * const result)303*b1e83836Smrg static inline int to_chars(const floating_decimal_64 v, char* const result) {
304*b1e83836Smrg // Step 5: Print the decimal representation.
305*b1e83836Smrg int index = 0;
306*b1e83836Smrg if (v.sign) {
307*b1e83836Smrg result[index++] = '-';
308*b1e83836Smrg }
309*b1e83836Smrg
310*b1e83836Smrg uint64_t output = v.mantissa;
311*b1e83836Smrg const uint32_t olength = decimalLength17(output);
312*b1e83836Smrg
313*b1e83836Smrg #ifdef RYU_DEBUG
314*b1e83836Smrg printf("DIGITS=%" PRIu64 "\n", v.mantissa);
315*b1e83836Smrg printf("OLEN=%u\n", olength);
316*b1e83836Smrg printf("EXP=%u\n", v.exponent + olength);
317*b1e83836Smrg #endif
318*b1e83836Smrg
319*b1e83836Smrg // Print the decimal digits.
320*b1e83836Smrg // The following code is equivalent to:
321*b1e83836Smrg // for (uint32_t i = 0; i < olength - 1; ++i) {
322*b1e83836Smrg // const uint32_t c = output % 10; output /= 10;
323*b1e83836Smrg // result[index + olength - i] = (char) ('0' + c);
324*b1e83836Smrg // }
325*b1e83836Smrg // result[index] = '0' + output % 10;
326*b1e83836Smrg
327*b1e83836Smrg uint32_t i = 0;
328*b1e83836Smrg // We prefer 32-bit operations, even on 64-bit platforms.
329*b1e83836Smrg // We have at most 17 digits, and uint32_t can store 9 digits.
330*b1e83836Smrg // If output doesn't fit into uint32_t, we cut off 8 digits,
331*b1e83836Smrg // so the rest will fit into uint32_t.
332*b1e83836Smrg if ((output >> 32) != 0) {
333*b1e83836Smrg // Expensive 64-bit division.
334*b1e83836Smrg const uint64_t q = div1e8(output);
335*b1e83836Smrg uint32_t output2 = ((uint32_t) output) - 100000000 * ((uint32_t) q);
336*b1e83836Smrg output = q;
337*b1e83836Smrg
338*b1e83836Smrg const uint32_t c = output2 % 10000;
339*b1e83836Smrg output2 /= 10000;
340*b1e83836Smrg const uint32_t d = output2 % 10000;
341*b1e83836Smrg const uint32_t c0 = (c % 100) << 1;
342*b1e83836Smrg const uint32_t c1 = (c / 100) << 1;
343*b1e83836Smrg const uint32_t d0 = (d % 100) << 1;
344*b1e83836Smrg const uint32_t d1 = (d / 100) << 1;
345*b1e83836Smrg memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
346*b1e83836Smrg memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
347*b1e83836Smrg memcpy(result + index + olength - i - 5, DIGIT_TABLE + d0, 2);
348*b1e83836Smrg memcpy(result + index + olength - i - 7, DIGIT_TABLE + d1, 2);
349*b1e83836Smrg i += 8;
350*b1e83836Smrg }
351*b1e83836Smrg uint32_t output2 = (uint32_t) output;
352*b1e83836Smrg while (output2 >= 10000) {
353*b1e83836Smrg #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
354*b1e83836Smrg const uint32_t c = output2 - 10000 * (output2 / 10000);
355*b1e83836Smrg #else
356*b1e83836Smrg const uint32_t c = output2 % 10000;
357*b1e83836Smrg #endif
358*b1e83836Smrg output2 /= 10000;
359*b1e83836Smrg const uint32_t c0 = (c % 100) << 1;
360*b1e83836Smrg const uint32_t c1 = (c / 100) << 1;
361*b1e83836Smrg memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
362*b1e83836Smrg memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
363*b1e83836Smrg i += 4;
364*b1e83836Smrg }
365*b1e83836Smrg if (output2 >= 100) {
366*b1e83836Smrg const uint32_t c = (output2 % 100) << 1;
367*b1e83836Smrg output2 /= 100;
368*b1e83836Smrg memcpy(result + index + olength - i - 1, DIGIT_TABLE + c, 2);
369*b1e83836Smrg i += 2;
370*b1e83836Smrg }
371*b1e83836Smrg if (output2 >= 10) {
372*b1e83836Smrg const uint32_t c = output2 << 1;
373*b1e83836Smrg // We can't use memcpy here: the decimal dot goes between these two digits.
374*b1e83836Smrg result[index + olength - i] = DIGIT_TABLE[c + 1];
375*b1e83836Smrg result[index] = DIGIT_TABLE[c];
376*b1e83836Smrg } else {
377*b1e83836Smrg result[index] = (char) ('0' + output2);
378*b1e83836Smrg }
379*b1e83836Smrg
380*b1e83836Smrg // Print decimal point if needed.
381*b1e83836Smrg if (olength > 1) {
382*b1e83836Smrg result[index + 1] = '.';
383*b1e83836Smrg index += olength + 1;
384*b1e83836Smrg } else {
385*b1e83836Smrg ++index;
386*b1e83836Smrg }
387*b1e83836Smrg
388*b1e83836Smrg // Print the exponent.
389*b1e83836Smrg result[index++] = 'e';
390*b1e83836Smrg int32_t exp = v.exponent + (int32_t) olength - 1;
391*b1e83836Smrg if (exp < 0) {
392*b1e83836Smrg result[index++] = '-';
393*b1e83836Smrg exp = -exp;
394*b1e83836Smrg } else
395*b1e83836Smrg result[index++] = '+';
396*b1e83836Smrg
397*b1e83836Smrg if (exp >= 100) {
398*b1e83836Smrg const int32_t c = exp % 10;
399*b1e83836Smrg memcpy(result + index, DIGIT_TABLE + 2 * (exp / 10), 2);
400*b1e83836Smrg result[index + 2] = (char) ('0' + c);
401*b1e83836Smrg index += 3;
402*b1e83836Smrg } else {
403*b1e83836Smrg memcpy(result + index, DIGIT_TABLE + 2 * exp, 2);
404*b1e83836Smrg index += 2;
405*b1e83836Smrg }
406*b1e83836Smrg
407*b1e83836Smrg return index;
408*b1e83836Smrg }
409*b1e83836Smrg
d2d_small_int(const uint64_t ieeeMantissa,const uint32_t ieeeExponent,const bool ieeeSign,floating_decimal_64 * const v)410*b1e83836Smrg static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent, const bool ieeeSign,
411*b1e83836Smrg floating_decimal_64* const v) {
412*b1e83836Smrg const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
413*b1e83836Smrg const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
414*b1e83836Smrg
415*b1e83836Smrg if (e2 > 0) {
416*b1e83836Smrg // f = m2 * 2^e2 >= 2^53 is an integer.
417*b1e83836Smrg // Ignore this case for now.
418*b1e83836Smrg return false;
419*b1e83836Smrg }
420*b1e83836Smrg
421*b1e83836Smrg if (e2 < -52) {
422*b1e83836Smrg // f < 1.
423*b1e83836Smrg return false;
424*b1e83836Smrg }
425*b1e83836Smrg
426*b1e83836Smrg // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
427*b1e83836Smrg // Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
428*b1e83836Smrg const uint64_t mask = (1ull << -e2) - 1;
429*b1e83836Smrg const uint64_t fraction = m2 & mask;
430*b1e83836Smrg if (fraction != 0) {
431*b1e83836Smrg return false;
432*b1e83836Smrg }
433*b1e83836Smrg
434*b1e83836Smrg // f is an integer in the range [1, 2^53).
435*b1e83836Smrg // Note: mantissa might contain trailing (decimal) 0's.
436*b1e83836Smrg // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
437*b1e83836Smrg v->mantissa = m2 >> -e2;
438*b1e83836Smrg v->exponent = 0;
439*b1e83836Smrg v->sign = ieeeSign;
440*b1e83836Smrg return true;
441*b1e83836Smrg }
442*b1e83836Smrg
floating_to_fd64(double f)443*b1e83836Smrg floating_decimal_64 floating_to_fd64(double f) {
444*b1e83836Smrg // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
445*b1e83836Smrg const uint64_t bits = double_to_bits(f);
446*b1e83836Smrg
447*b1e83836Smrg #ifdef RYU_DEBUG
448*b1e83836Smrg printf("IN=");
449*b1e83836Smrg for (int32_t bit = 63; bit >= 0; --bit) {
450*b1e83836Smrg printf("%d", (int) ((bits >> bit) & 1));
451*b1e83836Smrg }
452*b1e83836Smrg printf("\n");
453*b1e83836Smrg #endif
454*b1e83836Smrg
455*b1e83836Smrg // Decode bits into sign, mantissa, and exponent.
456*b1e83836Smrg const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
457*b1e83836Smrg const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
458*b1e83836Smrg const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
459*b1e83836Smrg // Case distinction; exit early for the easy cases.
460*b1e83836Smrg if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
461*b1e83836Smrg __builtin_abort();
462*b1e83836Smrg }
463*b1e83836Smrg
464*b1e83836Smrg floating_decimal_64 v;
465*b1e83836Smrg const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, ieeeSign, &v);
466*b1e83836Smrg if (isSmallInt) {
467*b1e83836Smrg // For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
468*b1e83836Smrg // For scientific notation we need to move these zeros into the exponent.
469*b1e83836Smrg // (This is not needed for fixed-point notation, so it might be beneficial to trim
470*b1e83836Smrg // trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
471*b1e83836Smrg for (;;) {
472*b1e83836Smrg const uint64_t q = div10(v.mantissa);
473*b1e83836Smrg const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q);
474*b1e83836Smrg if (r != 0) {
475*b1e83836Smrg break;
476*b1e83836Smrg }
477*b1e83836Smrg v.mantissa = q;
478*b1e83836Smrg ++v.exponent;
479*b1e83836Smrg }
480*b1e83836Smrg } else {
481*b1e83836Smrg v = d2d(ieeeMantissa, ieeeExponent, ieeeSign);
482*b1e83836Smrg }
483*b1e83836Smrg
484*b1e83836Smrg return v;
485*b1e83836Smrg }
486