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