line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
// Copyright 2019 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
|
|
|
|
|
|
|
/* The location of the headers, relative to this file, has been changed by |
19
|
|
|
|
|
|
|
* Sisyphus */ |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include "ryu_headers/ryu_parse.h" |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
#include |
24
|
|
|
|
|
|
|
#include |
25
|
|
|
|
|
|
|
#include |
26
|
|
|
|
|
|
|
#include |
27
|
|
|
|
|
|
|
#include |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#ifdef RYU_DEBUG |
30
|
|
|
|
|
|
|
#include |
31
|
|
|
|
|
|
|
#include |
32
|
|
|
|
|
|
|
#endif |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#include "ryu_headers/common.h" |
35
|
|
|
|
|
|
|
#include "ryu_headers/d2s_intrinsics.h" |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#if defined(RYU_OPTIMIZE_SIZE) |
38
|
|
|
|
|
|
|
#include "ryu_headers/d2s_small_table.h" |
39
|
|
|
|
|
|
|
#else |
40
|
|
|
|
|
|
|
#include "ryu_headers/d2s_full_table.h" |
41
|
|
|
|
|
|
|
#endif |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#define DOUBLE_MANTISSA_BITS 52 |
44
|
|
|
|
|
|
|
#define DOUBLE_EXPONENT_BITS 11 |
45
|
|
|
|
|
|
|
#define DOUBLE_EXPONENT_BIAS 1023 |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#if defined(_MSC_VER) |
48
|
|
|
|
|
|
|
#include |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
static inline uint32_t floor_log2(const uint64_t value) { |
51
|
|
|
|
|
|
|
long index; |
52
|
|
|
|
|
|
|
return _BitScanReverse64(&index, value) ? index : 64; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#else |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
static inline uint32_t floor_log2(const uint64_t value) { |
58
|
0
|
|
|
|
|
|
return 63 - __builtin_clzll(value); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
#endif |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
// The max function is already defined on Windows. |
64
|
0
|
|
|
|
|
|
static inline int32_t max32(int32_t a, int32_t b) { |
65
|
0
|
|
|
|
|
|
return a < b ? b : a; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
static inline double int64Bits2Double(uint64_t bits) { |
69
|
|
|
|
|
|
|
double f; |
70
|
0
|
|
|
|
|
|
memcpy(&f, &bits, sizeof(double)); |
71
|
0
|
|
|
|
|
|
return f; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
enum Status s2d_n(const char * buffer, const int len, double * result) { |
75
|
0
|
0
|
|
|
|
|
if (len == 0) { |
76
|
0
|
|
|
|
|
|
return INPUT_TOO_SHORT; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
int m10digits = 0; |
79
|
0
|
|
|
|
|
|
int e10digits = 0; |
80
|
0
|
|
|
|
|
|
int dotIndex = len; |
81
|
0
|
|
|
|
|
|
int eIndex = len; |
82
|
0
|
|
|
|
|
|
uint64_t m10 = 0; |
83
|
0
|
|
|
|
|
|
int32_t e10 = 0; |
84
|
0
|
|
|
|
|
|
bool signedM = false; |
85
|
0
|
|
|
|
|
|
bool signedE = false; |
86
|
0
|
|
|
|
|
|
int i = 0; |
87
|
0
|
0
|
|
|
|
|
if (buffer[i] == '-') { |
88
|
0
|
|
|
|
|
|
signedM = true; |
89
|
0
|
|
|
|
|
|
i++; |
90
|
|
|
|
|
|
|
} |
91
|
0
|
0
|
|
|
|
|
for (; i < len; i++) { |
92
|
0
|
|
|
|
|
|
char c = buffer[i]; |
93
|
0
|
0
|
|
|
|
|
if (c == '.') { |
94
|
0
|
0
|
|
|
|
|
if (dotIndex != len) { |
95
|
0
|
|
|
|
|
|
return MALFORMED_INPUT; |
96
|
|
|
|
|
|
|
} |
97
|
0
|
|
|
|
|
|
dotIndex = i; |
98
|
0
|
|
|
|
|
|
continue; |
99
|
|
|
|
|
|
|
} |
100
|
0
|
0
|
|
|
|
|
if ((c < '0') || (c > '9')) { |
|
|
0
|
|
|
|
|
|
101
|
|
|
|
|
|
|
break; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
0
|
|
|
|
|
if (m10digits >= 17) { |
104
|
0
|
|
|
|
|
|
return INPUT_TOO_LONG; |
105
|
|
|
|
|
|
|
} |
106
|
0
|
|
|
|
|
|
m10 = 10 * m10 + (c - '0'); |
107
|
0
|
0
|
|
|
|
|
if (m10 != 0) { |
108
|
0
|
|
|
|
|
|
m10digits++; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
0
|
0
|
|
|
|
|
if (i < len && ((buffer[i] == 'e') || (buffer[i] == 'E'))) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
eIndex = i; |
113
|
0
|
|
|
|
|
|
i++; |
114
|
0
|
0
|
|
|
|
|
if (i < len && ((buffer[i] == '-') || (buffer[i] == '+'))) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
signedE = buffer[i] == '-'; |
116
|
0
|
|
|
|
|
|
i++; |
117
|
|
|
|
|
|
|
} |
118
|
0
|
0
|
|
|
|
|
for (; i < len; i++) { |
119
|
0
|
|
|
|
|
|
char c = buffer[i]; |
120
|
0
|
0
|
|
|
|
|
if ((c < '0') || (c > '9')) { |
|
|
0
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return MALFORMED_INPUT; |
122
|
|
|
|
|
|
|
} |
123
|
0
|
0
|
|
|
|
|
if (e10digits > 3) { |
124
|
|
|
|
|
|
|
// TODO: Be more lenient. Return +/-Infinity or +/-0 instead. |
125
|
0
|
|
|
|
|
|
return INPUT_TOO_LONG; |
126
|
|
|
|
|
|
|
} |
127
|
0
|
|
|
|
|
|
e10 = 10 * e10 + (c - '0'); |
128
|
0
|
0
|
|
|
|
|
if (e10 != 0) { |
129
|
0
|
|
|
|
|
|
e10digits++; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
0
|
0
|
|
|
|
|
if (i < len) { |
134
|
0
|
|
|
|
|
|
return MALFORMED_INPUT; |
135
|
|
|
|
|
|
|
} |
136
|
0
|
0
|
|
|
|
|
if (signedE) { |
137
|
0
|
|
|
|
|
|
e10 = -e10; |
138
|
|
|
|
|
|
|
} |
139
|
0
|
0
|
|
|
|
|
e10 -= dotIndex < eIndex ? eIndex - dotIndex - 1 : 0; |
140
|
0
|
0
|
|
|
|
|
if (m10 == 0) { |
141
|
0
|
0
|
|
|
|
|
*result = signedM ? -0.0 : 0.0; |
142
|
0
|
|
|
|
|
|
return SUCCESS; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
#ifdef RYU_DEBUG |
146
|
0
|
|
|
|
|
|
printf("Input=%s\n", buffer); |
147
|
0
|
|
|
|
|
|
printf("m10digits = %d\n", m10digits); |
148
|
0
|
|
|
|
|
|
printf("e10digits = %d\n", e10digits); |
149
|
0
|
|
|
|
|
|
printf("m10 * 10^e10 = %" PRIu64 " * 10^%d\n", m10, e10); |
150
|
|
|
|
|
|
|
#endif |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
|
if ((m10digits + e10 <= -324) || (m10 == 0)) { |
|
|
0
|
|
|
|
|
|
153
|
|
|
|
|
|
|
// Number is less than 1e-324, which should be rounded down to 0; return +/-0.0. |
154
|
0
|
|
|
|
|
|
uint64_t ieee = ((uint64_t) signedM) << (DOUBLE_EXPONENT_BITS + DOUBLE_MANTISSA_BITS); |
155
|
0
|
|
|
|
|
|
*result = int64Bits2Double(ieee); |
156
|
0
|
|
|
|
|
|
return SUCCESS; |
157
|
|
|
|
|
|
|
} |
158
|
0
|
0
|
|
|
|
|
if (m10digits + e10 >= 310) { |
159
|
|
|
|
|
|
|
// Number is larger than 1e+309, which should be rounded to +/-Infinity. |
160
|
0
|
|
|
|
|
|
uint64_t ieee = (((uint64_t) signedM) << (DOUBLE_EXPONENT_BITS + DOUBLE_MANTISSA_BITS)) | (0x7ffull << DOUBLE_MANTISSA_BITS); |
161
|
0
|
|
|
|
|
|
*result = int64Bits2Double(ieee); |
162
|
0
|
|
|
|
|
|
return SUCCESS; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
// Convert to binary float m2 * 2^e2, while retaining information about whether the conversion |
166
|
|
|
|
|
|
|
// was exact (trailingZeros). |
167
|
|
|
|
|
|
|
int32_t e2; |
168
|
|
|
|
|
|
|
uint64_t m2; |
169
|
|
|
|
|
|
|
bool trailingZeros; |
170
|
0
|
0
|
|
|
|
|
if (e10 >= 0) { |
171
|
|
|
|
|
|
|
// The length of m * 10^e in bits is: |
172
|
|
|
|
|
|
|
// log2(m10 * 10^e10) = log2(m10) + e10 log2(10) = log2(m10) + e10 + e10 * log2(5) |
173
|
|
|
|
|
|
|
// |
174
|
|
|
|
|
|
|
// We want to compute the DOUBLE_MANTISSA_BITS + 1 top-most bits (+1 for the implicit leading |
175
|
|
|
|
|
|
|
// one in IEEE format). We therefore choose a binary output exponent of |
176
|
|
|
|
|
|
|
// log2(m10 * 10^e10) - (DOUBLE_MANTISSA_BITS + 1). |
177
|
|
|
|
|
|
|
// |
178
|
|
|
|
|
|
|
// We use floor(log2(5^e10)) so that we get at least this many bits; better to |
179
|
|
|
|
|
|
|
// have an additional bit than to not have enough bits. |
180
|
0
|
|
|
|
|
|
e2 = floor_log2(m10) + e10 + log2pow5(e10) - (DOUBLE_MANTISSA_BITS + 1); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
// We now compute [m10 * 10^e10 / 2^e2] = [m10 * 5^e10 / 2^(e2-e10)]. |
183
|
|
|
|
|
|
|
// To that end, we use the DOUBLE_POW5_SPLIT table. |
184
|
0
|
|
|
|
|
|
int j = e2 - e10 - ceil_log2pow5(e10) + DOUBLE_POW5_BITCOUNT; |
185
|
0
|
0
|
|
|
|
|
assert(j >= 0); |
186
|
|
|
|
|
|
|
#if defined(RYU_OPTIMIZE_SIZE) |
187
|
|
|
|
|
|
|
uint64_t pow5[2]; |
188
|
|
|
|
|
|
|
double_computePow5(e10, pow5); |
189
|
|
|
|
|
|
|
m2 = mulShift64(m10, pow5, j); |
190
|
|
|
|
|
|
|
#else |
191
|
0
|
0
|
|
|
|
|
assert(e10 < DOUBLE_POW5_TABLE_SIZE); |
192
|
0
|
|
|
|
|
|
m2 = mulShift64(m10, DOUBLE_POW5_SPLIT[e10], j); |
193
|
|
|
|
|
|
|
#endif |
194
|
|
|
|
|
|
|
// We also compute if the result is exact, i.e., |
195
|
|
|
|
|
|
|
// [m10 * 10^e10 / 2^e2] == m10 * 10^e10 / 2^e2. |
196
|
|
|
|
|
|
|
// This can only be the case if 2^e2 divides m10 * 10^e10, which in turn requires that the |
197
|
|
|
|
|
|
|
// largest power of 2 that divides m10 + e10 is greater than e2. If e2 is less than e10, then |
198
|
|
|
|
|
|
|
// the result must be exact. Otherwise we use the existing multipleOfPowerOf2 function. |
199
|
0
|
0
|
|
|
|
|
trailingZeros = e2 < e10 || (e2 - e10 < 64 && multipleOfPowerOf2(m10, e2 - e10)); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} else { |
201
|
0
|
|
|
|
|
|
e2 = floor_log2(m10) + e10 - ceil_log2pow5(-e10) - (DOUBLE_MANTISSA_BITS + 1); |
202
|
0
|
|
|
|
|
|
int j = e2 - e10 + ceil_log2pow5(-e10) - 1 + DOUBLE_POW5_INV_BITCOUNT; |
203
|
|
|
|
|
|
|
#if defined(RYU_OPTIMIZE_SIZE) |
204
|
|
|
|
|
|
|
uint64_t pow5[2]; |
205
|
|
|
|
|
|
|
double_computeInvPow5(-e10, pow5); |
206
|
|
|
|
|
|
|
m2 = mulShift64(m10, pow5, j); |
207
|
|
|
|
|
|
|
#else |
208
|
0
|
0
|
|
|
|
|
assert(-e10 < DOUBLE_POW5_INV_TABLE_SIZE); |
209
|
0
|
|
|
|
|
|
m2 = mulShift64(m10, DOUBLE_POW5_INV_SPLIT[-e10], j); |
210
|
|
|
|
|
|
|
#endif |
211
|
0
|
|
|
|
|
|
trailingZeros = multipleOfPowerOf5(m10, -e10); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
#ifdef RYU_DEBUG |
215
|
0
|
|
|
|
|
|
printf("m2 * 2^e2 = %" PRIu64 " * 2^%d\n", m2, e2); |
216
|
|
|
|
|
|
|
#endif |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
// Compute the final IEEE exponent. |
219
|
0
|
|
|
|
|
|
uint32_t ieee_e2 = (uint32_t) max32(0, e2 + DOUBLE_EXPONENT_BIAS + floor_log2(m2)); |
220
|
|
|
|
|
|
|
|
221
|
0
|
0
|
|
|
|
|
if (ieee_e2 > 0x7fe) { |
222
|
|
|
|
|
|
|
// Final IEEE exponent is larger than the maximum representable; return +/-Infinity. |
223
|
0
|
|
|
|
|
|
uint64_t ieee = (((uint64_t) signedM) << (DOUBLE_EXPONENT_BITS + DOUBLE_MANTISSA_BITS)) | (0x7ffull << DOUBLE_MANTISSA_BITS); |
224
|
0
|
|
|
|
|
|
*result = int64Bits2Double(ieee); |
225
|
0
|
|
|
|
|
|
return SUCCESS; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
// We need to figure out how much we need to shift m2. The tricky part is that we need to take |
229
|
|
|
|
|
|
|
// the final IEEE exponent into account, so we need to reverse the bias and also special-case |
230
|
|
|
|
|
|
|
// the value 0. |
231
|
0
|
0
|
|
|
|
|
int32_t shift = (ieee_e2 == 0 ? 1 : ieee_e2) - e2 - DOUBLE_EXPONENT_BIAS - DOUBLE_MANTISSA_BITS; |
232
|
0
|
0
|
|
|
|
|
assert(shift >= 0); |
233
|
|
|
|
|
|
|
#ifdef RYU_DEBUG |
234
|
0
|
|
|
|
|
|
printf("ieee_e2 = %d\n", ieee_e2); |
235
|
0
|
|
|
|
|
|
printf("shift = %d\n", shift); |
236
|
|
|
|
|
|
|
#endif |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
// We need to round up if the exact value is more than 0.5 above the value we computed. That's |
239
|
|
|
|
|
|
|
// equivalent to checking if the last removed bit was 1 and either the value was not just |
240
|
|
|
|
|
|
|
// trailing zeros or the result would otherwise be odd. |
241
|
|
|
|
|
|
|
// |
242
|
|
|
|
|
|
|
// We need to update trailingZeros given that we have the exact output exponent ieee_e2 now. |
243
|
0
|
|
|
|
|
|
trailingZeros &= (m2 & ((1ull << (shift - 1)) - 1)) == 0; |
244
|
0
|
|
|
|
|
|
uint64_t lastRemovedBit = (m2 >> (shift - 1)) & 1; |
245
|
0
|
0
|
|
|
|
|
bool roundUp = (lastRemovedBit != 0) && (!trailingZeros || (((m2 >> shift) & 1) != 0)); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
#ifdef RYU_DEBUG |
248
|
0
|
|
|
|
|
|
printf("roundUp = %d\n", roundUp); |
249
|
0
|
|
|
|
|
|
printf("ieee_m2 = %" PRIu64 "\n", (m2 >> shift) + roundUp); |
250
|
|
|
|
|
|
|
#endif |
251
|
0
|
|
|
|
|
|
uint64_t ieee_m2 = (m2 >> shift) + roundUp; |
252
|
0
|
0
|
|
|
|
|
assert(ieee_m2 <= (1ull << (DOUBLE_MANTISSA_BITS + 1))); |
253
|
0
|
|
|
|
|
|
ieee_m2 &= (1ull << DOUBLE_MANTISSA_BITS) - 1; |
254
|
0
|
0
|
|
|
|
|
if (ieee_m2 == 0 && roundUp) { |
|
|
0
|
|
|
|
|
|
255
|
|
|
|
|
|
|
// Due to how the IEEE represents +/-Infinity, we don't need to check for overflow here. |
256
|
0
|
|
|
|
|
|
ieee_e2++; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
0
|
|
|
|
|
|
uint64_t ieee = (((((uint64_t) signedM) << DOUBLE_EXPONENT_BITS) | (uint64_t)ieee_e2) << DOUBLE_MANTISSA_BITS) | ieee_m2; |
260
|
0
|
|
|
|
|
|
*result = int64Bits2Double(ieee); |
261
|
0
|
|
|
|
|
|
return SUCCESS; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
enum Status s2d(const char * buffer, double * result) { |
265
|
0
|
|
|
|
|
|
return s2d_n(buffer, strlen(buffer), result); |
266
|
|
|
|
|
|
|
} |