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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* An internal strtod() implementation based upon V8 src/strtod.cc
* without bignum support.
*
* Copyright 2012 the V8 project authors. All rights reserved.
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file.
*/
#include <njs_main.h>
#include <njs_diyfp.h>
/*
* Max double: 1.7976931348623157 x 10^308
* Min non-zero double: 4.9406564584124654 x 10^-324
* Any x >= 10^309 is interpreted as +infinity.
* Any x <= 10^-324 is interpreted as 0.
* Note that 2.5e-324 (despite being smaller than the min double)
* will be read as non-zero (equal to the min non-zero double).
*/
#define NJS_DECIMAL_POWER_MAX 309
#define NJS_DECIMAL_POWER_MIN (-324)
#define NJS_UINT64_MAX njs_uint64(0xFFFFFFFF, 0xFFFFFFFF)
#define NJS_UINT64_DECIMAL_DIGITS_MAX 19
/*
* Reads digits from the buffer and converts them to a uint64.
* Reads in as many digits as fit into a uint64.
* When the string starts with "1844674407370955161" no further digit is read.
* Since 2^64 = 18446744073709551616 it would still be possible read another
* digit if it was less or equal than 6, but this would complicate the code.
*/
njs_inline uint64_t
njs_read_uint64(const u_char *start, size_t length, size_t *ndigits)
{
u_char d;
uint64_t value;
const u_char *p, *e;
value = 0;
p = start;
e = p + length;
while (p < e && value <= (NJS_UINT64_MAX / 10 - 1)) {
d = *p++ - '0';
value = 10 * value + d;
}
*ndigits = p - start;
return value;
}
/*
* Reads a njs_diyfp_t from the buffer.
* The returned njs_diyfp_t is not necessarily normalized.
* If remaining is zero then the returned njs_diyfp_t is accurate.
* Otherwise it has been rounded and has error of at most 1/2 ulp.
*/
static njs_diyfp_t
njs_diyfp_read(const u_char *start, size_t length, int *remaining)
{
size_t read;
uint64_t significand;
significand = njs_read_uint64(start, length, &read);
/* Round the significand. */
if (length != read) {
if (start[read] >= '5') {
significand++;
}
}
*remaining = length - read;
return njs_diyfp(significand, 0);
}
/*
* Returns 10^exp as an exact njs_diyfp_t.
* The given exp must be in the range [1; NJS_DECIMAL_EXPONENT_DIST[.
*/
njs_inline njs_diyfp_t
njs_adjust_pow10(int exp)
{
switch (exp) {
case 1:
return njs_diyfp(njs_uint64(0xa0000000, 00000000), -60);
case 2:
return njs_diyfp(njs_uint64(0xc8000000, 00000000), -57);
case 3:
return njs_diyfp(njs_uint64(0xfa000000, 00000000), -54);
case 4:
return njs_diyfp(njs_uint64(0x9c400000, 00000000), -50);
case 5:
return njs_diyfp(njs_uint64(0xc3500000, 00000000), -47);
case 6:
return njs_diyfp(njs_uint64(0xf4240000, 00000000), -44);
case 7:
return njs_diyfp(njs_uint64(0x98968000, 00000000), -40);
default:
njs_unreachable();
return njs_diyfp(0, 0);
}
}
/*
* Returns the significand size for a given order of magnitude.
* If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
* This function returns the number of significant binary digits v will have
* once its encoded into a double. In almost all cases this is equal to
* NJS_SIGNIFICAND_SIZE. The only exception are denormals. They start with
* leading zeroes and their effective significand-size is hence smaller.
*/
njs_inline int
njs_diyfp_sgnd_size(int order)
{
if (order >= (NJS_DBL_EXPONENT_DENORMAL + NJS_SIGNIFICAND_SIZE)) {
return NJS_SIGNIFICAND_SIZE;
}
if (order <= NJS_DBL_EXPONENT_DENORMAL) {
return 0;
}
return order - NJS_DBL_EXPONENT_DENORMAL;
}
#define NJS_DENOM_LOG 3
#define NJS_DENOM (1 << NJS_DENOM_LOG)
/*
* Returns either the correct double or the double that is just below
* the correct double.
*/
static double
njs_diyfp_strtod(const u_char *start, size_t length, int exp)
{
int magnitude, prec_digits;
int remaining, dec_exp, adj_exp, orig_e, shift;
int64_t error;
uint64_t prec_bits, half_way;
njs_diyfp_t value, pow, adj_pow, rounded;
value = njs_diyfp_read(start, length, &remaining);
exp += remaining;
/*
* Since some digits may have been dropped the value is not accurate.
* If remaining is different than 0 than the error is at most .5 ulp
* (unit in the last place).
* Using a common denominator to avoid dealing with fractions.
*/
error = (remaining == 0 ? 0 : NJS_DENOM / 2);
orig_e = value.exp;
value = njs_diyfp_normalize(value);
error <<= orig_e - value.exp;
if (exp < NJS_DECIMAL_EXPONENT_MIN) {
return 0.0;
}
pow = njs_cached_power_dec(exp, &dec_exp);
if (dec_exp != exp) {
adj_exp = exp - dec_exp;
adj_pow = njs_adjust_pow10(exp - dec_exp);
value = njs_diyfp_mul(value, adj_pow);
if (NJS_UINT64_DECIMAL_DIGITS_MAX - (int) length < adj_exp) {
/*
* The adjustment power is exact. There is hence only
* an error of 0.5.
*/
error += NJS_DENOM / 2;
}
}
value = njs_diyfp_mul(value, pow);
/*
* The error introduced by a multiplication of a * b equals
* error_a + error_b + error_a * error_b / 2^64 + 0.5
* Substituting a with 'value' and b with 'pow':
* error_b = 0.5 (all cached powers have an error of less than 0.5 ulp),
* error_ab = 0 or 1 / NJS_DENOM > error_a * error_b / 2^64.
*/
error += NJS_DENOM + (error != 0 ? 1 : 0);
orig_e = value.exp;
value = njs_diyfp_normalize(value);
error <<= orig_e - value.exp;
/*
* Check whether the double's significand changes when the error is added
* or substracted.
*/
magnitude = NJS_DIYFP_SIGNIFICAND_SIZE + value.exp;
prec_digits = NJS_DIYFP_SIGNIFICAND_SIZE - njs_diyfp_sgnd_size(magnitude);
if (prec_digits + NJS_DENOM_LOG >= NJS_DIYFP_SIGNIFICAND_SIZE) {
/*
* This can only happen for very small denormals. In this case the
* half-way multiplied by the denominator exceeds the range of uint64.
* Simply shift everything to the right.
*/
shift = prec_digits + NJS_DENOM_LOG - NJS_DIYFP_SIGNIFICAND_SIZE + 1;
value = njs_diyfp_shift_right(value, shift);
/*
* Add 1 for the lost precision of error, and NJS_DENOM
* for the lost precision of value.significand.
*/
error = (error >> shift) + 1 + NJS_DENOM;
prec_digits -= shift;
}
prec_bits = value.significand & (((uint64_t) 1 << prec_digits) - 1);
prec_bits *= NJS_DENOM;
half_way = (uint64_t) 1 << (prec_digits - 1);
half_way *= NJS_DENOM;
rounded = njs_diyfp_shift_right(value, prec_digits);
if (prec_bits >= half_way + error) {
rounded.significand++;
}
return njs_diyfp2d(rounded);
}
static double
njs_strtod_internal(const u_char *start, size_t length, int exp)
{
int shift;
size_t left, right;
const u_char *p, *e, *b;
/* Trim leading zeroes. */
p = start;
e = p + length;
while (p < e) {
if (*p != '0') {
start = p;
break;
}
p++;
}
left = e - p;
/* Trim trailing zeroes. */
b = start;
p = b + left - 1;
while (p > b) {
if (*p != '0') {
break;
}
p--;
}
right = p - b + 1;
length = right;
if (length == 0) {
return 0.0;
}
shift = (int) (left - right);
if (exp >= NJS_DECIMAL_POWER_MAX - shift - (int) length + 1) {
return INFINITY;
}
if (exp <= NJS_DECIMAL_POWER_MIN - shift - (int) length) {
return 0.0;
}
return njs_diyfp_strtod(start, length, exp + shift);
}
double
njs_strtod(const u_char **start, const u_char *end, njs_bool_t literal)
{
int exponent, exp, insignf;
u_char c, *pos;
njs_bool_t minus;
const u_char *e, *p, *last, *_;
u_char data[128];
exponent = 0;
insignf = 0;
pos = data;
last = data + sizeof(data);
p = *start;
_ = p - 2;
for (; p < end; p++) {
/* Values less than '0' become >= 208. */
c = *p - '0';
if (njs_slow_path(c > 9)) {
if (literal) {
if ((p - _) == 1) {
goto done;
}
if (*p == '_') {
_ = p;
continue;
}
}
break;
}
if (pos < last) {
*pos++ = *p;
} else {
insignf++;
}
}
/* Do not emit a '.', but adjust the exponent instead. */
if (p < end && *p == '.') {
_ = p;
for (p++; p < end; p++) {
/* Values less than '0' become >= 208. */
c = *p - '0';
if (njs_slow_path(c > 9)) {
if (literal && *p == '_' && (p - _) > 1) {
_ = p;
continue;
}
break;
}
if (pos < last) {
*pos++ = *p;
exponent--;
} else {
/* Ignore insignificant digits in the fractional part. */
}
}
}
if (pos == data) {
return NAN;
}
e = p + 1;
if (e < end && (*p == 'e' || *p == 'E')) {
minus = 0;
if (e + 1 < end) {
if (*e == '-') {
e++;
minus = 1;
} else if (*e == '+') {
e++;
}
}
/* Values less than '0' become >= 208. */
c = *e - '0';
if (njs_fast_path(c <= 9)) {
exp = c;
for (p = e + 1; p < end; p++) {
/* Values less than '0' become >= 208. */
c = *p - '0';
if (njs_slow_path(c > 9)) {
if (literal && *p == '_' && (p - _) > 1) {
_ = p;
continue;
}
break;
}
if (exp < (INT_MAX - 9) / 10) {
exp = exp * 10 + c;
}
}
exponent += minus ? -exp : exp;
} else if (literal && *e == '_') {
p = e;
}
}
done:
*start = p;
exponent += insignf;
return njs_strtod_internal(data, pos - data, exponent);
}
|