1 | /****************************************************************
|
---|
2 | *
|
---|
3 | * The author of this software is David M. Gay.
|
---|
4 | *
|
---|
5 | * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
|
---|
6 | * Copyright (C) 2002, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
7 | *
|
---|
8 | * Permission to use, copy, modify, and distribute this software for any
|
---|
9 | * purpose without fee is hereby granted, provided that this entire notice
|
---|
10 | * is included in all copies of any software which is or includes a copy
|
---|
11 | * or modification of this software and in all copies of the supporting
|
---|
12 | * documentation for such software.
|
---|
13 | *
|
---|
14 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
|
---|
15 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
|
---|
16 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
|
---|
17 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
|
---|
18 | *
|
---|
19 | ***************************************************************/
|
---|
20 |
|
---|
21 | /* Please send bug reports to
|
---|
22 | David M. Gay
|
---|
23 | Bell Laboratories, Room 2C-463
|
---|
24 | 600 Mountain Avenue
|
---|
25 | Murray Hill, NJ 07974-0636
|
---|
26 | U.S.A.
|
---|
27 | [email protected]
|
---|
28 | */
|
---|
29 |
|
---|
30 | /* On a machine with IEEE extended-precision registers, it is
|
---|
31 | * necessary to specify double-precision (53-bit) rounding precision
|
---|
32 | * before invoking strtod or dtoa. If the machine uses (the equivalent
|
---|
33 | * of) Intel 80x87 arithmetic, the call
|
---|
34 | * _control87(PC_53, MCW_PC);
|
---|
35 | * does this with many compilers. Whether this or another call is
|
---|
36 | * appropriate depends on the compiler; for this to work, it may be
|
---|
37 | * necessary to #include "float.h" or another system-dependent header
|
---|
38 | * file.
|
---|
39 | */
|
---|
40 |
|
---|
41 | /* strtod for IEEE-arithmetic machines.
|
---|
42 | *
|
---|
43 | * This strtod returns a nearest machine number to the input decimal
|
---|
44 | * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
|
---|
45 | * broken by the IEEE round-even rule. Otherwise ties are broken by
|
---|
46 | * biased rounding (add half and chop).
|
---|
47 | *
|
---|
48 | * Inspired loosely by William D. Clinger's paper "How to Read Floating
|
---|
49 | * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
|
---|
50 | *
|
---|
51 | * Modifications:
|
---|
52 | *
|
---|
53 | * 1. We only require IEEE.
|
---|
54 | * 2. We get by with floating-point arithmetic in a case that
|
---|
55 | * Clinger missed -- when we're computing d * 10^n
|
---|
56 | * for a small integer d and the integer n is not too
|
---|
57 | * much larger than 22 (the maximum integer k for which
|
---|
58 | * we can represent 10^k exactly), we may be able to
|
---|
59 | * compute (d*10^k) * 10^(e-k) with just one roundoff.
|
---|
60 | * 3. Rather than a bit-at-a-time adjustment of the binary
|
---|
61 | * result in the hard case, we use floating-point
|
---|
62 | * arithmetic to determine the adjustment to within
|
---|
63 | * one bit; only in really hard cases do we need to
|
---|
64 | * compute a second residual.
|
---|
65 | * 4. Because of 3., we don't need a large table of powers of 10
|
---|
66 | * for ten-to-e (just some small tables, e.g. of 10^k
|
---|
67 | * for 0 <= k <= 22).
|
---|
68 | */
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * #define IEEE_8087 for IEEE-arithmetic machines where the least
|
---|
72 | * significant byte has the lowest address.
|
---|
73 | * #define IEEE_MC68k for IEEE-arithmetic machines where the most
|
---|
74 | * significant byte has the lowest address.
|
---|
75 | * #define No_leftright to omit left-right logic in fast floating-point
|
---|
76 | * computation of dtoa.
|
---|
77 | * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
|
---|
78 | * and Honor_FLT_ROUNDS is not #defined.
|
---|
79 | * #define Inaccurate_Divide for IEEE-format with correctly rounded
|
---|
80 | * products but inaccurate quotients, e.g., for Intel i860.
|
---|
81 | * #define USE_LONG_LONG on machines that have a "long long"
|
---|
82 | * integer type (of >= 64 bits), and performance testing shows that
|
---|
83 | * it is faster than 32-bit fallback (which is often not the case
|
---|
84 | * on 32-bit machines). On such machines, you can #define Just_16
|
---|
85 | * to store 16 bits per 32-bit int32_t when doing high-precision integer
|
---|
86 | * arithmetic. Whether this speeds things up or slows things down
|
---|
87 | * depends on the machine and the number being converted.
|
---|
88 | * #define Bad_float_h if your system lacks a float.h or if it does not
|
---|
89 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
|
---|
90 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
|
---|
91 | * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
|
---|
92 | * Infinity and NaN (case insensitively). On some systems (e.g.,
|
---|
93 | * some HP systems), it may be necessary to #define NAN_WORD0
|
---|
94 | * appropriately -- to the most significant word of a quiet NaN.
|
---|
95 | * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
|
---|
96 | * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
|
---|
97 | * strtod also accepts (case insensitively) strings of the form
|
---|
98 | * NaN(x), where x is a string of hexadecimal digits and spaces;
|
---|
99 | * if there is only one string of hexadecimal digits, it is taken
|
---|
100 | * for the 52 fraction bits of the resulting NaN; if there are two
|
---|
101 | * or more strings of hex digits, the first is for the high 20 bits,
|
---|
102 | * the second and subsequent for the low 32 bits, with intervening
|
---|
103 | * white space ignored; but if this results in none of the 52
|
---|
104 | * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
|
---|
105 | * and NAN_WORD1 are used instead.
|
---|
106 | * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
|
---|
107 | * avoids underflows on inputs whose result does not underflow.
|
---|
108 | * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
|
---|
109 | * floating-point numbers and flushes underflows to zero rather
|
---|
110 | * than implementing gradual underflow, then you must also #define
|
---|
111 | * Sudden_Underflow.
|
---|
112 | * #define YES_ALIAS to permit aliasing certain double values with
|
---|
113 | * arrays of ULongs. This leads to slightly better code with
|
---|
114 | * some compilers and was always used prior to 19990916, but it
|
---|
115 | * is not strictly legal and can cause trouble with aggressively
|
---|
116 | * optimizing compilers (e.g., gcc 2.95.1 under -O2).
|
---|
117 | * #define SET_INEXACT if IEEE arithmetic is being used and extra
|
---|
118 | * computation should be done to set the inexact flag when the
|
---|
119 | * result is inexact and avoid setting inexact when the result
|
---|
120 | * is exact. In this case, dtoa.c must be compiled in
|
---|
121 | * an environment, perhaps provided by #include "dtoa.c" in a
|
---|
122 | * suitable wrapper, that defines two functions,
|
---|
123 | * int get_inexact(void);
|
---|
124 | * void clear_inexact(void);
|
---|
125 | * such that get_inexact() returns a nonzero value if the
|
---|
126 | * inexact bit is already set, and clear_inexact() sets the
|
---|
127 | * inexact bit to 0. When SET_INEXACT is #defined, strtod
|
---|
128 | * also does extra computations to set the underflow and overflow
|
---|
129 | * flags when appropriate (i.e., when the result is tiny and
|
---|
130 | * inexact or when it is a numeric value rounded to +-infinity).
|
---|
131 | * #define NO_ERRNO if strtod should not assign errno = ERANGE when
|
---|
132 | * the result overflows to +-Infinity or underflows to 0.
|
---|
133 | */
|
---|
134 |
|
---|
135 | #include "config.h"
|
---|
136 | #include "dtoa.h"
|
---|
137 |
|
---|
138 | #if HAVE(ERRNO_H)
|
---|
139 | #include <errno.h>
|
---|
140 | #else
|
---|
141 | #define NO_ERRNO
|
---|
142 | #endif
|
---|
143 | #include <float.h>
|
---|
144 | #include <math.h>
|
---|
145 | #include <stdint.h>
|
---|
146 | #include <stdlib.h>
|
---|
147 | #include <string.h>
|
---|
148 | #include <wtf/AlwaysInline.h>
|
---|
149 | #include <wtf/Assertions.h>
|
---|
150 | #include <wtf/FastMalloc.h>
|
---|
151 | #include <wtf/MathExtras.h>
|
---|
152 | #include <wtf/Vector.h>
|
---|
153 | #include <wtf/Threading.h>
|
---|
154 |
|
---|
155 | #include <stdio.h>
|
---|
156 |
|
---|
157 | #if COMPILER(MSVC)
|
---|
158 | #pragma warning(disable: 4244)
|
---|
159 | #pragma warning(disable: 4245)
|
---|
160 | #pragma warning(disable: 4554)
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #if PLATFORM(BIG_ENDIAN)
|
---|
164 | #define IEEE_MC68k
|
---|
165 | #elif PLATFORM(MIDDLE_ENDIAN)
|
---|
166 | #define IEEE_ARM
|
---|
167 | #else
|
---|
168 | #define IEEE_8087
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | #define INFNAN_CHECK
|
---|
172 |
|
---|
173 | #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
|
---|
174 | Exactly one of IEEE_8087, IEEE_ARM or IEEE_MC68k should be defined.
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | namespace WTF {
|
---|
178 |
|
---|
179 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
180 | Mutex* s_dtoaP5Mutex;
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | typedef union { double d; uint32_t L[2]; } U;
|
---|
184 |
|
---|
185 | #ifdef YES_ALIAS
|
---|
186 | #define dval(x) x
|
---|
187 | #ifdef IEEE_8087
|
---|
188 | #define word0(x) ((uint32_t*)&x)[1]
|
---|
189 | #define word1(x) ((uint32_t*)&x)[0]
|
---|
190 | #else
|
---|
191 | #define word0(x) ((uint32_t*)&x)[0]
|
---|
192 | #define word1(x) ((uint32_t*)&x)[1]
|
---|
193 | #endif
|
---|
194 | #else
|
---|
195 | #ifdef IEEE_8087
|
---|
196 | #define word0(x) (x)->L[1]
|
---|
197 | #define word1(x) (x)->L[0]
|
---|
198 | #else
|
---|
199 | #define word0(x) (x)->L[0]
|
---|
200 | #define word1(x) (x)->L[1]
|
---|
201 | #endif
|
---|
202 | #define dval(x) (x)->d
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | /* The following definition of Storeinc is appropriate for MIPS processors.
|
---|
206 | * An alternative that might be better on some machines is
|
---|
207 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
|
---|
208 | */
|
---|
209 | #if defined(IEEE_8087) || defined(IEEE_ARM)
|
---|
210 | #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++)
|
---|
211 | #else
|
---|
212 | #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++)
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | #define Exp_shift 20
|
---|
216 | #define Exp_shift1 20
|
---|
217 | #define Exp_msk1 0x100000
|
---|
218 | #define Exp_msk11 0x100000
|
---|
219 | #define Exp_mask 0x7ff00000
|
---|
220 | #define P 53
|
---|
221 | #define Bias 1023
|
---|
222 | #define Emin (-1022)
|
---|
223 | #define Exp_1 0x3ff00000
|
---|
224 | #define Exp_11 0x3ff00000
|
---|
225 | #define Ebits 11
|
---|
226 | #define Frac_mask 0xfffff
|
---|
227 | #define Frac_mask1 0xfffff
|
---|
228 | #define Ten_pmax 22
|
---|
229 | #define Bletch 0x10
|
---|
230 | #define Bndry_mask 0xfffff
|
---|
231 | #define Bndry_mask1 0xfffff
|
---|
232 | #define LSB 1
|
---|
233 | #define Sign_bit 0x80000000
|
---|
234 | #define Log2P 1
|
---|
235 | #define Tiny0 0
|
---|
236 | #define Tiny1 1
|
---|
237 | #define Quick_max 14
|
---|
238 | #define Int_max 14
|
---|
239 |
|
---|
240 | #if !defined(NO_IEEE_Scale)
|
---|
241 | #undef Avoid_Underflow
|
---|
242 | #define Avoid_Underflow
|
---|
243 | #endif
|
---|
244 |
|
---|
245 | #if !defined(Flt_Rounds)
|
---|
246 | #if defined(FLT_ROUNDS)
|
---|
247 | #define Flt_Rounds FLT_ROUNDS
|
---|
248 | #else
|
---|
249 | #define Flt_Rounds 1
|
---|
250 | #endif
|
---|
251 | #endif /*Flt_Rounds*/
|
---|
252 |
|
---|
253 |
|
---|
254 | #define rounded_product(a,b) a *= b
|
---|
255 | #define rounded_quotient(a,b) a /= b
|
---|
256 |
|
---|
257 | #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
|
---|
258 | #define Big1 0xffffffff
|
---|
259 |
|
---|
260 |
|
---|
261 | // FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
|
---|
262 | #ifndef Pack_32
|
---|
263 | #define Pack_32
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | #if PLATFORM(PPC64) || PLATFORM(X86_64)
|
---|
267 | // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
|
---|
268 | #define USE_LONG_LONG
|
---|
269 | #endif
|
---|
270 |
|
---|
271 | #ifndef USE_LONG_LONG
|
---|
272 | #ifdef Just_16
|
---|
273 | #undef Pack_32
|
---|
274 | /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
|
---|
275 | * This makes some inner loops simpler and sometimes saves work
|
---|
276 | * during multiplications, but it often seems to make things slightly
|
---|
277 | * slower. Hence the default is now to store 32 bits per int32_t.
|
---|
278 | */
|
---|
279 | #endif
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | #define Kmax 15
|
---|
283 |
|
---|
284 | struct BigInt {
|
---|
285 | BigInt() : sign(0) { }
|
---|
286 | int sign;
|
---|
287 |
|
---|
288 | void clear()
|
---|
289 | {
|
---|
290 | sign = 0;
|
---|
291 | m_words.clear();
|
---|
292 | }
|
---|
293 |
|
---|
294 | size_t size() const
|
---|
295 | {
|
---|
296 | return m_words.size();
|
---|
297 | }
|
---|
298 |
|
---|
299 | void resize(size_t s)
|
---|
300 | {
|
---|
301 | m_words.resize(s);
|
---|
302 | }
|
---|
303 |
|
---|
304 | uint32_t* words()
|
---|
305 | {
|
---|
306 | return m_words.data();
|
---|
307 | }
|
---|
308 |
|
---|
309 | const uint32_t* words() const
|
---|
310 | {
|
---|
311 | return m_words.data();
|
---|
312 | }
|
---|
313 |
|
---|
314 | void append(uint32_t w)
|
---|
315 | {
|
---|
316 | m_words.append(w);
|
---|
317 | }
|
---|
318 |
|
---|
319 | Vector<uint32_t, 16> m_words;
|
---|
320 | };
|
---|
321 |
|
---|
322 | static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */
|
---|
323 | {
|
---|
324 | #ifdef USE_LONG_LONG
|
---|
325 | unsigned long long carry;
|
---|
326 | #else
|
---|
327 | uint32_t carry;
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | int wds = b.size();
|
---|
331 | uint32_t* x = b.words();
|
---|
332 | int i = 0;
|
---|
333 | carry = a;
|
---|
334 | do {
|
---|
335 | #ifdef USE_LONG_LONG
|
---|
336 | unsigned long long y = *x * (unsigned long long)m + carry;
|
---|
337 | carry = y >> 32;
|
---|
338 | *x++ = (uint32_t)y & 0xffffffffUL;
|
---|
339 | #else
|
---|
340 | #ifdef Pack_32
|
---|
341 | uint32_t xi = *x;
|
---|
342 | uint32_t y = (xi & 0xffff) * m + carry;
|
---|
343 | uint32_t z = (xi >> 16) * m + (y >> 16);
|
---|
344 | carry = z >> 16;
|
---|
345 | *x++ = (z << 16) + (y & 0xffff);
|
---|
346 | #else
|
---|
347 | uint32_t y = *x * m + carry;
|
---|
348 | carry = y >> 16;
|
---|
349 | *x++ = y & 0xffff;
|
---|
350 | #endif
|
---|
351 | #endif
|
---|
352 | } while (++i < wds);
|
---|
353 |
|
---|
354 | if (carry)
|
---|
355 | b.append((uint32_t)carry);
|
---|
356 | }
|
---|
357 |
|
---|
358 | static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9)
|
---|
359 | {
|
---|
360 | int k;
|
---|
361 | int32_t y;
|
---|
362 | int32_t x = (nd + 8) / 9;
|
---|
363 |
|
---|
364 | for (k = 0, y = 1; x > y; y <<= 1, k++) { }
|
---|
365 | #ifdef Pack_32
|
---|
366 | b.sign = 0;
|
---|
367 | b.resize(1);
|
---|
368 | b.words()[0] = y9;
|
---|
369 | #else
|
---|
370 | b.sign = 0;
|
---|
371 | b.resize((b->x[1] = y9 >> 16) ? 2 : 1);
|
---|
372 | b.words()[0] = y9 & 0xffff;
|
---|
373 | #endif
|
---|
374 |
|
---|
375 | int i = 9;
|
---|
376 | if (9 < nd0) {
|
---|
377 | s += 9;
|
---|
378 | do {
|
---|
379 | multadd(b, 10, *s++ - '0');
|
---|
380 | } while (++i < nd0);
|
---|
381 | s++;
|
---|
382 | } else
|
---|
383 | s += 10;
|
---|
384 | for (; i < nd; i++)
|
---|
385 | multadd(b, 10, *s++ - '0');
|
---|
386 | }
|
---|
387 |
|
---|
388 | static int hi0bits(uint32_t x)
|
---|
389 | {
|
---|
390 | int k = 0;
|
---|
391 |
|
---|
392 | if (!(x & 0xffff0000)) {
|
---|
393 | k = 16;
|
---|
394 | x <<= 16;
|
---|
395 | }
|
---|
396 | if (!(x & 0xff000000)) {
|
---|
397 | k += 8;
|
---|
398 | x <<= 8;
|
---|
399 | }
|
---|
400 | if (!(x & 0xf0000000)) {
|
---|
401 | k += 4;
|
---|
402 | x <<= 4;
|
---|
403 | }
|
---|
404 | if (!(x & 0xc0000000)) {
|
---|
405 | k += 2;
|
---|
406 | x <<= 2;
|
---|
407 | }
|
---|
408 | if (!(x & 0x80000000)) {
|
---|
409 | k++;
|
---|
410 | if (!(x & 0x40000000))
|
---|
411 | return 32;
|
---|
412 | }
|
---|
413 | return k;
|
---|
414 | }
|
---|
415 |
|
---|
416 | static int lo0bits (uint32_t* y)
|
---|
417 | {
|
---|
418 | int k;
|
---|
419 | uint32_t x = *y;
|
---|
420 |
|
---|
421 | if (x & 7) {
|
---|
422 | if (x & 1)
|
---|
423 | return 0;
|
---|
424 | if (x & 2) {
|
---|
425 | *y = x >> 1;
|
---|
426 | return 1;
|
---|
427 | }
|
---|
428 | *y = x >> 2;
|
---|
429 | return 2;
|
---|
430 | }
|
---|
431 | k = 0;
|
---|
432 | if (!(x & 0xffff)) {
|
---|
433 | k = 16;
|
---|
434 | x >>= 16;
|
---|
435 | }
|
---|
436 | if (!(x & 0xff)) {
|
---|
437 | k += 8;
|
---|
438 | x >>= 8;
|
---|
439 | }
|
---|
440 | if (!(x & 0xf)) {
|
---|
441 | k += 4;
|
---|
442 | x >>= 4;
|
---|
443 | }
|
---|
444 | if (!(x & 0x3)) {
|
---|
445 | k += 2;
|
---|
446 | x >>= 2;
|
---|
447 | }
|
---|
448 | if (!(x & 1)) {
|
---|
449 | k++;
|
---|
450 | x >>= 1;
|
---|
451 | if (!x & 1)
|
---|
452 | return 32;
|
---|
453 | }
|
---|
454 | *y = x;
|
---|
455 | return k;
|
---|
456 | }
|
---|
457 |
|
---|
458 | static void i2b(BigInt& b, int i)
|
---|
459 | {
|
---|
460 | b.sign = 0;
|
---|
461 | b.resize(1);
|
---|
462 | b.words()[0] = i;
|
---|
463 | }
|
---|
464 |
|
---|
465 | static void mult(BigInt& aRef, const BigInt& bRef)
|
---|
466 | {
|
---|
467 | const BigInt* a = &aRef;
|
---|
468 | const BigInt* b = &bRef;
|
---|
469 | BigInt c;
|
---|
470 | int wa, wb, wc;
|
---|
471 | const uint32_t *x = 0, *xa, *xb, *xae, *xbe;
|
---|
472 | uint32_t *xc, *xc0;
|
---|
473 | uint32_t y;
|
---|
474 | #ifdef USE_LONG_LONG
|
---|
475 | unsigned long long carry, z;
|
---|
476 | #else
|
---|
477 | uint32_t carry, z;
|
---|
478 | #endif
|
---|
479 |
|
---|
480 | if (a->size() < b->size()) {
|
---|
481 | const BigInt* tmp = a;
|
---|
482 | a = b;
|
---|
483 | b = tmp;
|
---|
484 | }
|
---|
485 |
|
---|
486 | wa = a->size();
|
---|
487 | wb = b->size();
|
---|
488 | wc = wa + wb;
|
---|
489 | c.resize(wc);
|
---|
490 |
|
---|
491 | for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
|
---|
492 | *xc = 0;
|
---|
493 | xa = a->words();
|
---|
494 | xae = xa + wa;
|
---|
495 | xb = b->words();
|
---|
496 | xbe = xb + wb;
|
---|
497 | xc0 = c.words();
|
---|
498 | #ifdef USE_LONG_LONG
|
---|
499 | for (; xb < xbe; xc0++) {
|
---|
500 | if ((y = *xb++)) {
|
---|
501 | x = xa;
|
---|
502 | xc = xc0;
|
---|
503 | carry = 0;
|
---|
504 | do {
|
---|
505 | z = *x++ * (unsigned long long)y + *xc + carry;
|
---|
506 | carry = z >> 32;
|
---|
507 | *xc++ = (uint32_t)z & 0xffffffffUL;
|
---|
508 | } while (x < xae);
|
---|
509 | *xc = (uint32_t)carry;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | #else
|
---|
513 | #ifdef Pack_32
|
---|
514 | for (; xb < xbe; xb++, xc0++) {
|
---|
515 | if ((y = *xb & 0xffff)) {
|
---|
516 | x = xa;
|
---|
517 | xc = xc0;
|
---|
518 | carry = 0;
|
---|
519 | do {
|
---|
520 | z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
|
---|
521 | carry = z >> 16;
|
---|
522 | uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
|
---|
523 | carry = z2 >> 16;
|
---|
524 | Storeinc(xc, z2, z);
|
---|
525 | } while (x < xae);
|
---|
526 | *xc = carry;
|
---|
527 | }
|
---|
528 | if ((y = *xb >> 16)) {
|
---|
529 | x = xa;
|
---|
530 | xc = xc0;
|
---|
531 | carry = 0;
|
---|
532 | uint32_t z2 = *xc;
|
---|
533 | do {
|
---|
534 | z = (*x & 0xffff) * y + (*xc >> 16) + carry;
|
---|
535 | carry = z >> 16;
|
---|
536 | Storeinc(xc, z, z2);
|
---|
537 | z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
|
---|
538 | carry = z2 >> 16;
|
---|
539 | } while (x < xae);
|
---|
540 | *xc = z2;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | #else
|
---|
544 | for(; xb < xbe; xc0++) {
|
---|
545 | if ((y = *xb++)) {
|
---|
546 | x = xa;
|
---|
547 | xc = xc0;
|
---|
548 | carry = 0;
|
---|
549 | do {
|
---|
550 | z = *x++ * y + *xc + carry;
|
---|
551 | carry = z >> 16;
|
---|
552 | *xc++ = z & 0xffff;
|
---|
553 | } while (x < xae);
|
---|
554 | *xc = carry;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | #endif
|
---|
558 | #endif
|
---|
559 | for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
|
---|
560 | c.resize(wc);
|
---|
561 | aRef = c;
|
---|
562 | }
|
---|
563 |
|
---|
564 | struct P5Node {
|
---|
565 | BigInt val;
|
---|
566 | P5Node* next;
|
---|
567 | };
|
---|
568 |
|
---|
569 | static P5Node* p5s;
|
---|
570 | static int p5s_count;
|
---|
571 |
|
---|
572 | static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
|
---|
573 | {
|
---|
574 | static int p05[3] = { 5, 25, 125 };
|
---|
575 |
|
---|
576 | if (int i = k & 3)
|
---|
577 | multadd(b, p05[i - 1], 0);
|
---|
578 |
|
---|
579 | if (!(k >>= 2))
|
---|
580 | return;
|
---|
581 |
|
---|
582 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
583 | s_dtoaP5Mutex->lock();
|
---|
584 | #endif
|
---|
585 | P5Node* p5 = p5s;
|
---|
586 |
|
---|
587 | if (!p5) {
|
---|
588 | /* first time */
|
---|
589 | p5 = new P5Node;
|
---|
590 | i2b(p5->val, 625);
|
---|
591 | p5->next = 0;
|
---|
592 | p5s = p5;
|
---|
593 | p5s_count = 1;
|
---|
594 | }
|
---|
595 |
|
---|
596 | int p5s_count_local = p5s_count;
|
---|
597 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
598 | s_dtoaP5Mutex->unlock();
|
---|
599 | #endif
|
---|
600 | int p5s_used = 0;
|
---|
601 |
|
---|
602 | for (;;) {
|
---|
603 | if (k & 1)
|
---|
604 | mult(b, p5->val);
|
---|
605 |
|
---|
606 | if (!(k >>= 1))
|
---|
607 | break;
|
---|
608 |
|
---|
609 | if (++p5s_used == p5s_count_local) {
|
---|
610 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
611 | s_dtoaP5Mutex->lock();
|
---|
612 | #endif
|
---|
613 | if (p5s_used == p5s_count) {
|
---|
614 | ASSERT(!p5->next);
|
---|
615 | p5->next = new P5Node;
|
---|
616 | p5->next->next = 0;
|
---|
617 | p5->next->val = p5->val;
|
---|
618 | mult(p5->next->val, p5->next->val);
|
---|
619 | ++p5s_count;
|
---|
620 | }
|
---|
621 |
|
---|
622 | p5s_count_local = p5s_count;
|
---|
623 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
624 | s_dtoaP5Mutex->unlock();
|
---|
625 | #endif
|
---|
626 | }
|
---|
627 | p5 = p5->next;
|
---|
628 | }
|
---|
629 | }
|
---|
630 |
|
---|
631 | static ALWAYS_INLINE void lshift(BigInt& b, int k)
|
---|
632 | {
|
---|
633 | #ifdef Pack_32
|
---|
634 | int n = k >> 5;
|
---|
635 | #else
|
---|
636 | int n = k >> 4;
|
---|
637 | #endif
|
---|
638 |
|
---|
639 | int origSize = b.size();
|
---|
640 | int n1 = n + origSize + 1;
|
---|
641 |
|
---|
642 | if (k &= 0x1f)
|
---|
643 | b.resize(b.size() + n + 1);
|
---|
644 | else
|
---|
645 | b.resize(b.size() + n);
|
---|
646 |
|
---|
647 | const uint32_t* srcStart = b.words();
|
---|
648 | uint32_t* dstStart = b.words();
|
---|
649 | const uint32_t* src = srcStart + origSize - 1;
|
---|
650 | uint32_t* dst = dstStart + n1 - 1;
|
---|
651 | #ifdef Pack_32
|
---|
652 | if (k) {
|
---|
653 | uint32_t hiSubword = 0;
|
---|
654 | int s = 32 - k;
|
---|
655 | for (; src >= srcStart; --src) {
|
---|
656 | *dst-- = hiSubword | *src >> s;
|
---|
657 | hiSubword = *src << k;
|
---|
658 | }
|
---|
659 | *dst = hiSubword;
|
---|
660 | ASSERT(dst == dstStart + n);
|
---|
661 |
|
---|
662 | b.resize(origSize + n + (b.words()[n1 - 1] != 0));
|
---|
663 | }
|
---|
664 | #else
|
---|
665 | if (k &= 0xf) {
|
---|
666 | uint32_t hiSubword = 0;
|
---|
667 | int s = 16 - k;
|
---|
668 | for (; src >= srcStart; --src) {
|
---|
669 | *dst-- = hiSubword | *src >> s;
|
---|
670 | hiSubword = (*src << k) & 0xffff;
|
---|
671 | }
|
---|
672 | *dst = hiSubword;
|
---|
673 | ASSERT(dst == dstStart + n);
|
---|
674 | result->wds = b->wds + n + (result->x[n1 - 1] != 0);
|
---|
675 | }
|
---|
676 | #endif
|
---|
677 | else {
|
---|
678 | do {
|
---|
679 | *--dst = *src--;
|
---|
680 | } while (src >= srcStart);
|
---|
681 | }
|
---|
682 | for (dst = dstStart + n; dst != dstStart; )
|
---|
683 | *--dst = 0;
|
---|
684 |
|
---|
685 | ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
|
---|
686 | }
|
---|
687 |
|
---|
688 | static int cmp(const BigInt& a, const BigInt& b)
|
---|
689 | {
|
---|
690 | const uint32_t *xa, *xa0, *xb, *xb0;
|
---|
691 | int i, j;
|
---|
692 |
|
---|
693 | i = a.size();
|
---|
694 | j = b.size();
|
---|
695 | ASSERT(i <= 1 || a.words()[i - 1]);
|
---|
696 | ASSERT(j <= 1 || b.words()[j - 1]);
|
---|
697 | if (i -= j)
|
---|
698 | return i;
|
---|
699 | xa0 = a.words();
|
---|
700 | xa = xa0 + j;
|
---|
701 | xb0 = b.words();
|
---|
702 | xb = xb0 + j;
|
---|
703 | for (;;) {
|
---|
704 | if (*--xa != *--xb)
|
---|
705 | return *xa < *xb ? -1 : 1;
|
---|
706 | if (xa <= xa0)
|
---|
707 | break;
|
---|
708 | }
|
---|
709 | return 0;
|
---|
710 | }
|
---|
711 |
|
---|
712 | static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
|
---|
713 | {
|
---|
714 | const BigInt* a = &aRef;
|
---|
715 | const BigInt* b = &bRef;
|
---|
716 | int i, wa, wb;
|
---|
717 | uint32_t *xc;
|
---|
718 |
|
---|
719 | i = cmp(*a, *b);
|
---|
720 | if (!i) {
|
---|
721 | c.sign = 0;
|
---|
722 | c.resize(1);
|
---|
723 | c.words()[0] = 0;
|
---|
724 | return;
|
---|
725 | }
|
---|
726 | if (i < 0) {
|
---|
727 | const BigInt* tmp = a;
|
---|
728 | a = b;
|
---|
729 | b = tmp;
|
---|
730 | i = 1;
|
---|
731 | } else
|
---|
732 | i = 0;
|
---|
733 |
|
---|
734 | wa = a->size();
|
---|
735 | const uint32_t* xa = a->words();
|
---|
736 | const uint32_t* xae = xa + wa;
|
---|
737 | wb = b->size();
|
---|
738 | const uint32_t* xb = b->words();
|
---|
739 | const uint32_t* xbe = xb + wb;
|
---|
740 |
|
---|
741 | c.resize(wa);
|
---|
742 | c.sign = i;
|
---|
743 | xc = c.words();
|
---|
744 | #ifdef USE_LONG_LONG
|
---|
745 | unsigned long long borrow = 0;
|
---|
746 | do {
|
---|
747 | unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
|
---|
748 | borrow = y >> 32 & (uint32_t)1;
|
---|
749 | *xc++ = (uint32_t)y & 0xffffffffUL;
|
---|
750 | } while (xb < xbe);
|
---|
751 | while (xa < xae) {
|
---|
752 | unsigned long long y = *xa++ - borrow;
|
---|
753 | borrow = y >> 32 & (uint32_t)1;
|
---|
754 | *xc++ = (uint32_t)y & 0xffffffffUL;
|
---|
755 | }
|
---|
756 | #else
|
---|
757 | uint32_t borrow = 0;
|
---|
758 | #ifdef Pack_32
|
---|
759 | do {
|
---|
760 | uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
|
---|
761 | borrow = (y & 0x10000) >> 16;
|
---|
762 | uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
|
---|
763 | borrow = (z & 0x10000) >> 16;
|
---|
764 | Storeinc(xc, z, y);
|
---|
765 | } while (xb < xbe);
|
---|
766 | while (xa < xae) {
|
---|
767 | uint32_t y = (*xa & 0xffff) - borrow;
|
---|
768 | borrow = (y & 0x10000) >> 16;
|
---|
769 | uint32_t z = (*xa++ >> 16) - borrow;
|
---|
770 | borrow = (z & 0x10000) >> 16;
|
---|
771 | Storeinc(xc, z, y);
|
---|
772 | }
|
---|
773 | #else
|
---|
774 | do {
|
---|
775 | uint32_t y = *xa++ - *xb++ - borrow;
|
---|
776 | borrow = (y & 0x10000) >> 16;
|
---|
777 | *xc++ = y & 0xffff;
|
---|
778 | } while (xb < xbe);
|
---|
779 | while (xa < xae) {
|
---|
780 | uint32_t y = *xa++ - borrow;
|
---|
781 | borrow = (y & 0x10000) >> 16;
|
---|
782 | *xc++ = y & 0xffff;
|
---|
783 | }
|
---|
784 | #endif
|
---|
785 | #endif
|
---|
786 | while (!*--xc)
|
---|
787 | wa--;
|
---|
788 | c.resize(wa);
|
---|
789 | }
|
---|
790 |
|
---|
791 | static double ulp(U *x)
|
---|
792 | {
|
---|
793 | register int32_t L;
|
---|
794 | U u;
|
---|
795 |
|
---|
796 | L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;
|
---|
797 | #ifndef Avoid_Underflow
|
---|
798 | #ifndef Sudden_Underflow
|
---|
799 | if (L > 0) {
|
---|
800 | #endif
|
---|
801 | #endif
|
---|
802 | word0(&u) = L;
|
---|
803 | word1(&u) = 0;
|
---|
804 | #ifndef Avoid_Underflow
|
---|
805 | #ifndef Sudden_Underflow
|
---|
806 | } else {
|
---|
807 | L = -L >> Exp_shift;
|
---|
808 | if (L < Exp_shift) {
|
---|
809 | word0(&u) = 0x80000 >> L;
|
---|
810 | word1(&u) = 0;
|
---|
811 | } else {
|
---|
812 | word0(&u) = 0;
|
---|
813 | L -= Exp_shift;
|
---|
814 | word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
|
---|
815 | }
|
---|
816 | }
|
---|
817 | #endif
|
---|
818 | #endif
|
---|
819 | return dval(&u);
|
---|
820 | }
|
---|
821 |
|
---|
822 | static double b2d(const BigInt& a, int* e)
|
---|
823 | {
|
---|
824 | const uint32_t* xa;
|
---|
825 | const uint32_t* xa0;
|
---|
826 | uint32_t w;
|
---|
827 | uint32_t y;
|
---|
828 | uint32_t z;
|
---|
829 | int k;
|
---|
830 | U d;
|
---|
831 |
|
---|
832 | #define d0 word0(&d)
|
---|
833 | #define d1 word1(&d)
|
---|
834 |
|
---|
835 | xa0 = a.words();
|
---|
836 | xa = xa0 + a.size();
|
---|
837 | y = *--xa;
|
---|
838 | ASSERT(y);
|
---|
839 | k = hi0bits(y);
|
---|
840 | *e = 32 - k;
|
---|
841 | #ifdef Pack_32
|
---|
842 | if (k < Ebits) {
|
---|
843 | d0 = Exp_1 | (y >> (Ebits - k));
|
---|
844 | w = xa > xa0 ? *--xa : 0;
|
---|
845 | d1 = (y << (32 - Ebits + k)) | (w >> (Ebits - k));
|
---|
846 | goto ret_d;
|
---|
847 | }
|
---|
848 | z = xa > xa0 ? *--xa : 0;
|
---|
849 | if (k -= Ebits) {
|
---|
850 | d0 = Exp_1 | (y << k) | (z >> (32 - k));
|
---|
851 | y = xa > xa0 ? *--xa : 0;
|
---|
852 | d1 = (z << k) | (y >> (32 - k));
|
---|
853 | } else {
|
---|
854 | d0 = Exp_1 | y;
|
---|
855 | d1 = z;
|
---|
856 | }
|
---|
857 | #else
|
---|
858 | if (k < Ebits + 16) {
|
---|
859 | z = xa > xa0 ? *--xa : 0;
|
---|
860 | d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
|
---|
861 | w = xa > xa0 ? *--xa : 0;
|
---|
862 | y = xa > xa0 ? *--xa : 0;
|
---|
863 | d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
|
---|
864 | goto ret_d;
|
---|
865 | }
|
---|
866 | z = xa > xa0 ? *--xa : 0;
|
---|
867 | w = xa > xa0 ? *--xa : 0;
|
---|
868 | k -= Ebits + 16;
|
---|
869 | d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
|
---|
870 | y = xa > xa0 ? *--xa : 0;
|
---|
871 | d1 = w << k + 16 | y << k;
|
---|
872 | #endif
|
---|
873 | ret_d:
|
---|
874 | #undef d0
|
---|
875 | #undef d1
|
---|
876 | return dval(&d);
|
---|
877 | }
|
---|
878 |
|
---|
879 | static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
|
---|
880 | {
|
---|
881 | int de, k;
|
---|
882 | uint32_t *x, y, z;
|
---|
883 | #ifndef Sudden_Underflow
|
---|
884 | int i;
|
---|
885 | #endif
|
---|
886 | #define d0 word0(d)
|
---|
887 | #define d1 word1(d)
|
---|
888 |
|
---|
889 | b.sign = 0;
|
---|
890 | #ifdef Pack_32
|
---|
891 | b.resize(1);
|
---|
892 | #else
|
---|
893 | b.resize(2);
|
---|
894 | #endif
|
---|
895 | x = b.words();
|
---|
896 |
|
---|
897 | z = d0 & Frac_mask;
|
---|
898 | d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
|
---|
899 | #ifdef Sudden_Underflow
|
---|
900 | de = (int)(d0 >> Exp_shift);
|
---|
901 | #else
|
---|
902 | if ((de = (int)(d0 >> Exp_shift)))
|
---|
903 | z |= Exp_msk1;
|
---|
904 | #endif
|
---|
905 | #ifdef Pack_32
|
---|
906 | if ((y = d1)) {
|
---|
907 | if ((k = lo0bits(&y))) {
|
---|
908 | x[0] = y | (z << (32 - k));
|
---|
909 | z >>= k;
|
---|
910 | } else
|
---|
911 | x[0] = y;
|
---|
912 | if (z) {
|
---|
913 | b.resize(2);
|
---|
914 | x[1] = z;
|
---|
915 | }
|
---|
916 |
|
---|
917 | #ifndef Sudden_Underflow
|
---|
918 | i = b.size();
|
---|
919 | #endif
|
---|
920 | } else {
|
---|
921 | k = lo0bits(&z);
|
---|
922 | x[0] = z;
|
---|
923 | #ifndef Sudden_Underflow
|
---|
924 | i = 1;
|
---|
925 | #endif
|
---|
926 | b.resize(1);
|
---|
927 | k += 32;
|
---|
928 | }
|
---|
929 | #else
|
---|
930 | if ((y = d1)) {
|
---|
931 | if ((k = lo0bits(&y))) {
|
---|
932 | if (k >= 16) {
|
---|
933 | x[0] = y | z << 32 - k & 0xffff;
|
---|
934 | x[1] = z >> k - 16 & 0xffff;
|
---|
935 | x[2] = z >> k;
|
---|
936 | i = 2;
|
---|
937 | } else {
|
---|
938 | x[0] = y & 0xffff;
|
---|
939 | x[1] = y >> 16 | z << 16 - k & 0xffff;
|
---|
940 | x[2] = z >> k & 0xffff;
|
---|
941 | x[3] = z >> k + 16;
|
---|
942 | i = 3;
|
---|
943 | }
|
---|
944 | } else {
|
---|
945 | x[0] = y & 0xffff;
|
---|
946 | x[1] = y >> 16;
|
---|
947 | x[2] = z & 0xffff;
|
---|
948 | x[3] = z >> 16;
|
---|
949 | i = 3;
|
---|
950 | }
|
---|
951 | } else {
|
---|
952 | k = lo0bits(&z);
|
---|
953 | if (k >= 16) {
|
---|
954 | x[0] = z;
|
---|
955 | i = 0;
|
---|
956 | } else {
|
---|
957 | x[0] = z & 0xffff;
|
---|
958 | x[1] = z >> 16;
|
---|
959 | i = 1;
|
---|
960 | }
|
---|
961 | k += 32;
|
---|
962 | } while (!x[i])
|
---|
963 | --i;
|
---|
964 | b->resize(i + 1);
|
---|
965 | #endif
|
---|
966 | #ifndef Sudden_Underflow
|
---|
967 | if (de) {
|
---|
968 | #endif
|
---|
969 | *e = de - Bias - (P - 1) + k;
|
---|
970 | *bits = P - k;
|
---|
971 | #ifndef Sudden_Underflow
|
---|
972 | } else {
|
---|
973 | *e = de - Bias - (P - 1) + 1 + k;
|
---|
974 | #ifdef Pack_32
|
---|
975 | *bits = (32 * i) - hi0bits(x[i - 1]);
|
---|
976 | #else
|
---|
977 | *bits = (i + 2) * 16 - hi0bits(x[i]);
|
---|
978 | #endif
|
---|
979 | }
|
---|
980 | #endif
|
---|
981 | }
|
---|
982 | #undef d0
|
---|
983 | #undef d1
|
---|
984 |
|
---|
985 | static double ratio(const BigInt& a, const BigInt& b)
|
---|
986 | {
|
---|
987 | U da, db;
|
---|
988 | int k, ka, kb;
|
---|
989 |
|
---|
990 | dval(&da) = b2d(a, &ka);
|
---|
991 | dval(&db) = b2d(b, &kb);
|
---|
992 | #ifdef Pack_32
|
---|
993 | k = ka - kb + 32 * (a.size() - b.size());
|
---|
994 | #else
|
---|
995 | k = ka - kb + 16 * (a.size() - b.size());
|
---|
996 | #endif
|
---|
997 | if (k > 0)
|
---|
998 | word0(&da) += k * Exp_msk1;
|
---|
999 | else {
|
---|
1000 | k = -k;
|
---|
1001 | word0(&db) += k * Exp_msk1;
|
---|
1002 | }
|
---|
1003 | return dval(&da) / dval(&db);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | static const double tens[] = {
|
---|
1007 | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
|
---|
1008 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
|
---|
1009 | 1e20, 1e21, 1e22
|
---|
1010 | };
|
---|
1011 |
|
---|
1012 | static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
|
---|
1013 | static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
|
---|
1014 | #ifdef Avoid_Underflow
|
---|
1015 | 9007199254740992. * 9007199254740992.e-256
|
---|
1016 | /* = 2^106 * 1e-53 */
|
---|
1017 | #else
|
---|
1018 | 1e-256
|
---|
1019 | #endif
|
---|
1020 | };
|
---|
1021 |
|
---|
1022 | /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
|
---|
1023 | /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
|
---|
1024 | #define Scale_Bit 0x10
|
---|
1025 | #define n_bigtens 5
|
---|
1026 |
|
---|
1027 | #if defined(INFNAN_CHECK)
|
---|
1028 |
|
---|
1029 | #ifndef NAN_WORD0
|
---|
1030 | #define NAN_WORD0 0x7ff80000
|
---|
1031 | #endif
|
---|
1032 |
|
---|
1033 | #ifndef NAN_WORD1
|
---|
1034 | #define NAN_WORD1 0
|
---|
1035 | #endif
|
---|
1036 |
|
---|
1037 | static int match(const char** sp, const char* t)
|
---|
1038 | {
|
---|
1039 | int c, d;
|
---|
1040 | const char* s = *sp;
|
---|
1041 |
|
---|
1042 | while ((d = *t++)) {
|
---|
1043 | if ((c = *++s) >= 'A' && c <= 'Z')
|
---|
1044 | c += 'a' - 'A';
|
---|
1045 | if (c != d)
|
---|
1046 | return 0;
|
---|
1047 | }
|
---|
1048 | *sp = s + 1;
|
---|
1049 | return 1;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | #ifndef No_Hex_NaN
|
---|
1053 | static void hexnan(U* rvp, const char** sp)
|
---|
1054 | {
|
---|
1055 | uint32_t c, x[2];
|
---|
1056 | const char* s;
|
---|
1057 | int havedig, udx0, xshift;
|
---|
1058 |
|
---|
1059 | x[0] = x[1] = 0;
|
---|
1060 | havedig = xshift = 0;
|
---|
1061 | udx0 = 1;
|
---|
1062 | s = *sp;
|
---|
1063 | while ((c = *(const unsigned char*)++s)) {
|
---|
1064 | if (c >= '0' && c <= '9')
|
---|
1065 | c -= '0';
|
---|
1066 | else if (c >= 'a' && c <= 'f')
|
---|
1067 | c += 10 - 'a';
|
---|
1068 | else if (c >= 'A' && c <= 'F')
|
---|
1069 | c += 10 - 'A';
|
---|
1070 | else if (c <= ' ') {
|
---|
1071 | if (udx0 && havedig) {
|
---|
1072 | udx0 = 0;
|
---|
1073 | xshift = 1;
|
---|
1074 | }
|
---|
1075 | continue;
|
---|
1076 | } else if (/*(*/ c == ')' && havedig) {
|
---|
1077 | *sp = s + 1;
|
---|
1078 | break;
|
---|
1079 | } else
|
---|
1080 | return; /* invalid form: don't change *sp */
|
---|
1081 | havedig = 1;
|
---|
1082 | if (xshift) {
|
---|
1083 | xshift = 0;
|
---|
1084 | x[0] = x[1];
|
---|
1085 | x[1] = 0;
|
---|
1086 | }
|
---|
1087 | if (udx0)
|
---|
1088 | x[0] = (x[0] << 4) | (x[1] >> 28);
|
---|
1089 | x[1] = (x[1] << 4) | c;
|
---|
1090 | }
|
---|
1091 | if ((x[0] &= 0xfffff) || x[1]) {
|
---|
1092 | word0(rvp) = Exp_mask | x[0];
|
---|
1093 | word1(rvp) = x[1];
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 | #endif /*No_Hex_NaN*/
|
---|
1097 | #endif /* INFNAN_CHECK */
|
---|
1098 |
|
---|
1099 | double strtod(const char* s00, char** se)
|
---|
1100 | {
|
---|
1101 | #ifdef Avoid_Underflow
|
---|
1102 | int scale;
|
---|
1103 | #endif
|
---|
1104 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
|
---|
1105 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
|
---|
1106 | const char *s, *s0, *s1;
|
---|
1107 | double aadj, aadj1;
|
---|
1108 | U aadj2, adj, rv, rv0;
|
---|
1109 | int32_t L;
|
---|
1110 | uint32_t y, z;
|
---|
1111 | BigInt bb, bb1, bd, bd0, bs, delta;
|
---|
1112 | #ifdef SET_INEXACT
|
---|
1113 | int inexact, oldinexact;
|
---|
1114 | #endif
|
---|
1115 |
|
---|
1116 | sign = nz0 = nz = 0;
|
---|
1117 | dval(&rv) = 0;
|
---|
1118 | for (s = s00; ; s++)
|
---|
1119 | switch (*s) {
|
---|
1120 | case '-':
|
---|
1121 | sign = 1;
|
---|
1122 | /* no break */
|
---|
1123 | case '+':
|
---|
1124 | if (*++s)
|
---|
1125 | goto break2;
|
---|
1126 | /* no break */
|
---|
1127 | case 0:
|
---|
1128 | goto ret0;
|
---|
1129 | case '\t':
|
---|
1130 | case '\n':
|
---|
1131 | case '\v':
|
---|
1132 | case '\f':
|
---|
1133 | case '\r':
|
---|
1134 | case ' ':
|
---|
1135 | continue;
|
---|
1136 | default:
|
---|
1137 | goto break2;
|
---|
1138 | }
|
---|
1139 | break2:
|
---|
1140 | if (*s == '0') {
|
---|
1141 | nz0 = 1;
|
---|
1142 | while (*++s == '0') { }
|
---|
1143 | if (!*s)
|
---|
1144 | goto ret;
|
---|
1145 | }
|
---|
1146 | s0 = s;
|
---|
1147 | y = z = 0;
|
---|
1148 | for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
|
---|
1149 | if (nd < 9)
|
---|
1150 | y = (10 * y) + c - '0';
|
---|
1151 | else if (nd < 16)
|
---|
1152 | z = (10 * z) + c - '0';
|
---|
1153 | nd0 = nd;
|
---|
1154 | if (c == '.') {
|
---|
1155 | c = *++s;
|
---|
1156 | if (!nd) {
|
---|
1157 | for (; c == '0'; c = *++s)
|
---|
1158 | nz++;
|
---|
1159 | if (c > '0' && c <= '9') {
|
---|
1160 | s0 = s;
|
---|
1161 | nf += nz;
|
---|
1162 | nz = 0;
|
---|
1163 | goto have_dig;
|
---|
1164 | }
|
---|
1165 | goto dig_done;
|
---|
1166 | }
|
---|
1167 | for (; c >= '0' && c <= '9'; c = *++s) {
|
---|
1168 | have_dig:
|
---|
1169 | nz++;
|
---|
1170 | if (c -= '0') {
|
---|
1171 | nf += nz;
|
---|
1172 | for (i = 1; i < nz; i++)
|
---|
1173 | if (nd++ < 9)
|
---|
1174 | y *= 10;
|
---|
1175 | else if (nd <= DBL_DIG + 1)
|
---|
1176 | z *= 10;
|
---|
1177 | if (nd++ < 9)
|
---|
1178 | y = (10 * y) + c;
|
---|
1179 | else if (nd <= DBL_DIG + 1)
|
---|
1180 | z = (10 * z) + c;
|
---|
1181 | nz = 0;
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | dig_done:
|
---|
1186 | e = 0;
|
---|
1187 | if (c == 'e' || c == 'E') {
|
---|
1188 | if (!nd && !nz && !nz0) {
|
---|
1189 | goto ret0;
|
---|
1190 | }
|
---|
1191 | s00 = s;
|
---|
1192 | esign = 0;
|
---|
1193 | switch (c = *++s) {
|
---|
1194 | case '-':
|
---|
1195 | esign = 1;
|
---|
1196 | case '+':
|
---|
1197 | c = *++s;
|
---|
1198 | }
|
---|
1199 | if (c >= '0' && c <= '9') {
|
---|
1200 | while (c == '0')
|
---|
1201 | c = *++s;
|
---|
1202 | if (c > '0' && c <= '9') {
|
---|
1203 | L = c - '0';
|
---|
1204 | s1 = s;
|
---|
1205 | while ((c = *++s) >= '0' && c <= '9')
|
---|
1206 | L = (10 * L) + c - '0';
|
---|
1207 | if (s - s1 > 8 || L > 19999)
|
---|
1208 | /* Avoid confusion from exponents
|
---|
1209 | * so large that e might overflow.
|
---|
1210 | */
|
---|
1211 | e = 19999; /* safe for 16 bit ints */
|
---|
1212 | else
|
---|
1213 | e = (int)L;
|
---|
1214 | if (esign)
|
---|
1215 | e = -e;
|
---|
1216 | } else
|
---|
1217 | e = 0;
|
---|
1218 | } else
|
---|
1219 | s = s00;
|
---|
1220 | }
|
---|
1221 | if (!nd) {
|
---|
1222 | if (!nz && !nz0) {
|
---|
1223 | #ifdef INFNAN_CHECK
|
---|
1224 | /* Check for Nan and Infinity */
|
---|
1225 | switch(c) {
|
---|
1226 | case 'i':
|
---|
1227 | case 'I':
|
---|
1228 | if (match(&s,"nf")) {
|
---|
1229 | --s;
|
---|
1230 | if (!match(&s,"inity"))
|
---|
1231 | ++s;
|
---|
1232 | word0(&rv) = 0x7ff00000;
|
---|
1233 | word1(&rv) = 0;
|
---|
1234 | goto ret;
|
---|
1235 | }
|
---|
1236 | break;
|
---|
1237 | case 'n':
|
---|
1238 | case 'N':
|
---|
1239 | if (match(&s, "an")) {
|
---|
1240 | word0(&rv) = NAN_WORD0;
|
---|
1241 | word1(&rv) = NAN_WORD1;
|
---|
1242 | #ifndef No_Hex_NaN
|
---|
1243 | if (*s == '(') /*)*/
|
---|
1244 | hexnan(&rv, &s);
|
---|
1245 | #endif
|
---|
1246 | goto ret;
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 | #endif /* INFNAN_CHECK */
|
---|
1250 | ret0:
|
---|
1251 | s = s00;
|
---|
1252 | sign = 0;
|
---|
1253 | }
|
---|
1254 | goto ret;
|
---|
1255 | }
|
---|
1256 | e1 = e -= nf;
|
---|
1257 |
|
---|
1258 | /* Now we have nd0 digits, starting at s0, followed by a
|
---|
1259 | * decimal point, followed by nd-nd0 digits. The number we're
|
---|
1260 | * after is the integer represented by those digits times
|
---|
1261 | * 10**e */
|
---|
1262 |
|
---|
1263 | if (!nd0)
|
---|
1264 | nd0 = nd;
|
---|
1265 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
|
---|
1266 | dval(&rv) = y;
|
---|
1267 | if (k > 9) {
|
---|
1268 | #ifdef SET_INEXACT
|
---|
1269 | if (k > DBL_DIG)
|
---|
1270 | oldinexact = get_inexact();
|
---|
1271 | #endif
|
---|
1272 | dval(&rv) = tens[k - 9] * dval(&rv) + z;
|
---|
1273 | }
|
---|
1274 | if (nd <= DBL_DIG && Flt_Rounds == 1) {
|
---|
1275 | if (!e)
|
---|
1276 | goto ret;
|
---|
1277 | if (e > 0) {
|
---|
1278 | if (e <= Ten_pmax) {
|
---|
1279 | /* rv = */ rounded_product(dval(&rv), tens[e]);
|
---|
1280 | goto ret;
|
---|
1281 | }
|
---|
1282 | i = DBL_DIG - nd;
|
---|
1283 | if (e <= Ten_pmax + i) {
|
---|
1284 | /* A fancier test would sometimes let us do
|
---|
1285 | * this for larger i values.
|
---|
1286 | */
|
---|
1287 | e -= i;
|
---|
1288 | dval(&rv) *= tens[i];
|
---|
1289 | /* rv = */ rounded_product(dval(&rv), tens[e]);
|
---|
1290 | goto ret;
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 | #ifndef Inaccurate_Divide
|
---|
1294 | else if (e >= -Ten_pmax) {
|
---|
1295 | /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
|
---|
1296 | goto ret;
|
---|
1297 | }
|
---|
1298 | #endif
|
---|
1299 | }
|
---|
1300 | e1 += nd - k;
|
---|
1301 |
|
---|
1302 | #ifdef SET_INEXACT
|
---|
1303 | inexact = 1;
|
---|
1304 | if (k <= DBL_DIG)
|
---|
1305 | oldinexact = get_inexact();
|
---|
1306 | #endif
|
---|
1307 | #ifdef Avoid_Underflow
|
---|
1308 | scale = 0;
|
---|
1309 | #endif
|
---|
1310 |
|
---|
1311 | /* Get starting approximation = rv * 10**e1 */
|
---|
1312 |
|
---|
1313 | if (e1 > 0) {
|
---|
1314 | if ((i = e1 & 15))
|
---|
1315 | dval(&rv) *= tens[i];
|
---|
1316 | if (e1 &= ~15) {
|
---|
1317 | if (e1 > DBL_MAX_10_EXP) {
|
---|
1318 | ovfl:
|
---|
1319 | #ifndef NO_ERRNO
|
---|
1320 | errno = ERANGE;
|
---|
1321 | #endif
|
---|
1322 | /* Can't trust HUGE_VAL */
|
---|
1323 | word0(&rv) = Exp_mask;
|
---|
1324 | word1(&rv) = 0;
|
---|
1325 | #ifdef SET_INEXACT
|
---|
1326 | /* set overflow bit */
|
---|
1327 | dval(&rv0) = 1e300;
|
---|
1328 | dval(&rv0) *= dval(&rv0);
|
---|
1329 | #endif
|
---|
1330 | goto ret;
|
---|
1331 | }
|
---|
1332 | e1 >>= 4;
|
---|
1333 | for (j = 0; e1 > 1; j++, e1 >>= 1)
|
---|
1334 | if (e1 & 1)
|
---|
1335 | dval(&rv) *= bigtens[j];
|
---|
1336 | /* The last multiplication could overflow. */
|
---|
1337 | word0(&rv) -= P * Exp_msk1;
|
---|
1338 | dval(&rv) *= bigtens[j];
|
---|
1339 | if ((z = word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
|
---|
1340 | goto ovfl;
|
---|
1341 | if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P)) {
|
---|
1342 | /* set to largest number */
|
---|
1343 | /* (Can't trust DBL_MAX) */
|
---|
1344 | word0(&rv) = Big0;
|
---|
1345 | word1(&rv) = Big1;
|
---|
1346 | } else
|
---|
1347 | word0(&rv) += P * Exp_msk1;
|
---|
1348 | }
|
---|
1349 | } else if (e1 < 0) {
|
---|
1350 | e1 = -e1;
|
---|
1351 | if ((i = e1 & 15))
|
---|
1352 | dval(&rv) /= tens[i];
|
---|
1353 | if (e1 >>= 4) {
|
---|
1354 | if (e1 >= 1 << n_bigtens)
|
---|
1355 | goto undfl;
|
---|
1356 | #ifdef Avoid_Underflow
|
---|
1357 | if (e1 & Scale_Bit)
|
---|
1358 | scale = 2 * P;
|
---|
1359 | for (j = 0; e1 > 0; j++, e1 >>= 1)
|
---|
1360 | if (e1 & 1)
|
---|
1361 | dval(&rv) *= tinytens[j];
|
---|
1362 | if (scale && (j = (2 * P) + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0) {
|
---|
1363 | /* scaled rv is denormal; zap j low bits */
|
---|
1364 | if (j >= 32) {
|
---|
1365 | word1(&rv) = 0;
|
---|
1366 | if (j >= 53)
|
---|
1367 | word0(&rv) = (P + 2) * Exp_msk1;
|
---|
1368 | else
|
---|
1369 | word0(&rv) &= 0xffffffff << (j - 32);
|
---|
1370 | } else
|
---|
1371 | word1(&rv) &= 0xffffffff << j;
|
---|
1372 | }
|
---|
1373 | #else
|
---|
1374 | for (j = 0; e1 > 1; j++, e1 >>= 1)
|
---|
1375 | if (e1 & 1)
|
---|
1376 | dval(&rv) *= tinytens[j];
|
---|
1377 | /* The last multiplication could underflow. */
|
---|
1378 | dval(&rv0) = dval(&rv);
|
---|
1379 | dval(&rv) *= tinytens[j];
|
---|
1380 | if (!dval(&rv)) {
|
---|
1381 | dval(&rv) = 2. * dval(&rv0);
|
---|
1382 | dval(&rv) *= tinytens[j];
|
---|
1383 | #endif
|
---|
1384 | if (!dval(&rv)) {
|
---|
1385 | undfl:
|
---|
1386 | dval(&rv) = 0.;
|
---|
1387 | #ifndef NO_ERRNO
|
---|
1388 | errno = ERANGE;
|
---|
1389 | #endif
|
---|
1390 | goto ret;
|
---|
1391 | }
|
---|
1392 | #ifndef Avoid_Underflow
|
---|
1393 | word0(&rv) = Tiny0;
|
---|
1394 | word1(&rv) = Tiny1;
|
---|
1395 | /* The refinement below will clean
|
---|
1396 | * this approximation up.
|
---|
1397 | */
|
---|
1398 | }
|
---|
1399 | #endif
|
---|
1400 | }
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | /* Now the hard part -- adjusting rv to the correct value.*/
|
---|
1404 |
|
---|
1405 | /* Put digits into bd: true value = bd * 10^e */
|
---|
1406 |
|
---|
1407 | s2b(bd0, s0, nd0, nd, y);
|
---|
1408 |
|
---|
1409 | for (;;) {
|
---|
1410 | bd = bd0;
|
---|
1411 | d2b(bb, &rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
|
---|
1412 | i2b(bs, 1);
|
---|
1413 |
|
---|
1414 | if (e >= 0) {
|
---|
1415 | bb2 = bb5 = 0;
|
---|
1416 | bd2 = bd5 = e;
|
---|
1417 | } else {
|
---|
1418 | bb2 = bb5 = -e;
|
---|
1419 | bd2 = bd5 = 0;
|
---|
1420 | }
|
---|
1421 | if (bbe >= 0)
|
---|
1422 | bb2 += bbe;
|
---|
1423 | else
|
---|
1424 | bd2 -= bbe;
|
---|
1425 | bs2 = bb2;
|
---|
1426 | #ifdef Avoid_Underflow
|
---|
1427 | j = bbe - scale;
|
---|
1428 | i = j + bbbits - 1; /* logb(rv) */
|
---|
1429 | if (i < Emin) /* denormal */
|
---|
1430 | j += P - Emin;
|
---|
1431 | else
|
---|
1432 | j = P + 1 - bbbits;
|
---|
1433 | #else /*Avoid_Underflow*/
|
---|
1434 | #ifdef Sudden_Underflow
|
---|
1435 | j = P + 1 - bbbits;
|
---|
1436 | #else /*Sudden_Underflow*/
|
---|
1437 | j = bbe;
|
---|
1438 | i = j + bbbits - 1; /* logb(rv) */
|
---|
1439 | if (i < Emin) /* denormal */
|
---|
1440 | j += P - Emin;
|
---|
1441 | else
|
---|
1442 | j = P + 1 - bbbits;
|
---|
1443 | #endif /*Sudden_Underflow*/
|
---|
1444 | #endif /*Avoid_Underflow*/
|
---|
1445 | bb2 += j;
|
---|
1446 | bd2 += j;
|
---|
1447 | #ifdef Avoid_Underflow
|
---|
1448 | bd2 += scale;
|
---|
1449 | #endif
|
---|
1450 | i = bb2 < bd2 ? bb2 : bd2;
|
---|
1451 | if (i > bs2)
|
---|
1452 | i = bs2;
|
---|
1453 | if (i > 0) {
|
---|
1454 | bb2 -= i;
|
---|
1455 | bd2 -= i;
|
---|
1456 | bs2 -= i;
|
---|
1457 | }
|
---|
1458 | if (bb5 > 0) {
|
---|
1459 | pow5mult(bs, bb5);
|
---|
1460 | mult(bb, bs);
|
---|
1461 | }
|
---|
1462 | if (bb2 > 0)
|
---|
1463 | lshift(bb, bb2);
|
---|
1464 | if (bd5 > 0)
|
---|
1465 | pow5mult(bd, bd5);
|
---|
1466 | if (bd2 > 0)
|
---|
1467 | lshift(bd, bd2);
|
---|
1468 | if (bs2 > 0)
|
---|
1469 | lshift(bs, bs2);
|
---|
1470 | diff(delta, bb, bd);
|
---|
1471 | dsign = delta.sign;
|
---|
1472 | delta.sign = 0;
|
---|
1473 | i = cmp(delta, bs);
|
---|
1474 |
|
---|
1475 | if (i < 0) {
|
---|
1476 | /* Error is less than half an ulp -- check for
|
---|
1477 | * special case of mantissa a power of two.
|
---|
1478 | */
|
---|
1479 | if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
|
---|
1480 | #ifdef Avoid_Underflow
|
---|
1481 | || (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1
|
---|
1482 | #else
|
---|
1483 | || (word0(&rv) & Exp_mask) <= Exp_msk1
|
---|
1484 | #endif
|
---|
1485 | ) {
|
---|
1486 | #ifdef SET_INEXACT
|
---|
1487 | if (!delta->words()[0] && delta->size() <= 1)
|
---|
1488 | inexact = 0;
|
---|
1489 | #endif
|
---|
1490 | break;
|
---|
1491 | }
|
---|
1492 | if (!delta.words()[0] && delta.size() <= 1) {
|
---|
1493 | /* exact result */
|
---|
1494 | #ifdef SET_INEXACT
|
---|
1495 | inexact = 0;
|
---|
1496 | #endif
|
---|
1497 | break;
|
---|
1498 | }
|
---|
1499 | lshift(delta, Log2P);
|
---|
1500 | if (cmp(delta, bs) > 0)
|
---|
1501 | goto drop_down;
|
---|
1502 | break;
|
---|
1503 | }
|
---|
1504 | if (i == 0) {
|
---|
1505 | /* exactly half-way between */
|
---|
1506 | if (dsign) {
|
---|
1507 | if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
|
---|
1508 | && word1(&rv) == (
|
---|
1509 | #ifdef Avoid_Underflow
|
---|
1510 | (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
|
---|
1511 | ? (0xffffffff & (0xffffffff << (2 * P + 1 - (y >> Exp_shift)))) :
|
---|
1512 | #endif
|
---|
1513 | 0xffffffff)) {
|
---|
1514 | /*boundary case -- increment exponent*/
|
---|
1515 | word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1;
|
---|
1516 | word1(&rv) = 0;
|
---|
1517 | #ifdef Avoid_Underflow
|
---|
1518 | dsign = 0;
|
---|
1519 | #endif
|
---|
1520 | break;
|
---|
1521 | }
|
---|
1522 | } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
|
---|
1523 | drop_down:
|
---|
1524 | /* boundary case -- decrement exponent */
|
---|
1525 | #ifdef Sudden_Underflow /*{{*/
|
---|
1526 | L = word0(&rv) & Exp_mask;
|
---|
1527 | #ifdef Avoid_Underflow
|
---|
1528 | if (L <= (scale ? (2 * P + 1) * Exp_msk1 : Exp_msk1))
|
---|
1529 | #else
|
---|
1530 | if (L <= Exp_msk1)
|
---|
1531 | #endif /*Avoid_Underflow*/
|
---|
1532 | goto undfl;
|
---|
1533 | L -= Exp_msk1;
|
---|
1534 | #else /*Sudden_Underflow}{*/
|
---|
1535 | #ifdef Avoid_Underflow
|
---|
1536 | if (scale) {
|
---|
1537 | L = word0(&rv) & Exp_mask;
|
---|
1538 | if (L <= (2 * P + 1) * Exp_msk1) {
|
---|
1539 | if (L > (P + 2) * Exp_msk1)
|
---|
1540 | /* round even ==> */
|
---|
1541 | /* accept rv */
|
---|
1542 | break;
|
---|
1543 | /* rv = smallest denormal */
|
---|
1544 | goto undfl;
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 | #endif /*Avoid_Underflow*/
|
---|
1548 | L = (word0(&rv) & Exp_mask) - Exp_msk1;
|
---|
1549 | #endif /*Sudden_Underflow}}*/
|
---|
1550 | word0(&rv) = L | Bndry_mask1;
|
---|
1551 | word1(&rv) = 0xffffffff;
|
---|
1552 | break;
|
---|
1553 | }
|
---|
1554 | if (!(word1(&rv) & LSB))
|
---|
1555 | break;
|
---|
1556 | if (dsign)
|
---|
1557 | dval(&rv) += ulp(&rv);
|
---|
1558 | else {
|
---|
1559 | dval(&rv) -= ulp(&rv);
|
---|
1560 | #ifndef Sudden_Underflow
|
---|
1561 | if (!dval(&rv))
|
---|
1562 | goto undfl;
|
---|
1563 | #endif
|
---|
1564 | }
|
---|
1565 | #ifdef Avoid_Underflow
|
---|
1566 | dsign = 1 - dsign;
|
---|
1567 | #endif
|
---|
1568 | break;
|
---|
1569 | }
|
---|
1570 | if ((aadj = ratio(delta, bs)) <= 2.) {
|
---|
1571 | if (dsign)
|
---|
1572 | aadj = aadj1 = 1.;
|
---|
1573 | else if (word1(&rv) || word0(&rv) & Bndry_mask) {
|
---|
1574 | #ifndef Sudden_Underflow
|
---|
1575 | if (word1(&rv) == Tiny1 && !word0(&rv))
|
---|
1576 | goto undfl;
|
---|
1577 | #endif
|
---|
1578 | aadj = 1.;
|
---|
1579 | aadj1 = -1.;
|
---|
1580 | } else {
|
---|
1581 | /* special case -- power of FLT_RADIX to be */
|
---|
1582 | /* rounded down... */
|
---|
1583 |
|
---|
1584 | if (aadj < 2. / FLT_RADIX)
|
---|
1585 | aadj = 1. / FLT_RADIX;
|
---|
1586 | else
|
---|
1587 | aadj *= 0.5;
|
---|
1588 | aadj1 = -aadj;
|
---|
1589 | }
|
---|
1590 | } else {
|
---|
1591 | aadj *= 0.5;
|
---|
1592 | aadj1 = dsign ? aadj : -aadj;
|
---|
1593 | #ifdef Check_FLT_ROUNDS
|
---|
1594 | switch (Rounding) {
|
---|
1595 | case 2: /* towards +infinity */
|
---|
1596 | aadj1 -= 0.5;
|
---|
1597 | break;
|
---|
1598 | case 0: /* towards 0 */
|
---|
1599 | case 3: /* towards -infinity */
|
---|
1600 | aadj1 += 0.5;
|
---|
1601 | }
|
---|
1602 | #else
|
---|
1603 | if (Flt_Rounds == 0)
|
---|
1604 | aadj1 += 0.5;
|
---|
1605 | #endif /*Check_FLT_ROUNDS*/
|
---|
1606 | }
|
---|
1607 | y = word0(&rv) & Exp_mask;
|
---|
1608 |
|
---|
1609 | /* Check for overflow */
|
---|
1610 |
|
---|
1611 | if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) {
|
---|
1612 | dval(&rv0) = dval(&rv);
|
---|
1613 | word0(&rv) -= P * Exp_msk1;
|
---|
1614 | adj.d = aadj1 * ulp(&rv);
|
---|
1615 | dval(&rv) += adj.d;
|
---|
1616 | if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P)) {
|
---|
1617 | if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
|
---|
1618 | goto ovfl;
|
---|
1619 | word0(&rv) = Big0;
|
---|
1620 | word1(&rv) = Big1;
|
---|
1621 | goto cont;
|
---|
1622 | } else
|
---|
1623 | word0(&rv) += P * Exp_msk1;
|
---|
1624 | } else {
|
---|
1625 | #ifdef Avoid_Underflow
|
---|
1626 | if (scale && y <= 2 * P * Exp_msk1) {
|
---|
1627 | if (aadj <= 0x7fffffff) {
|
---|
1628 | if ((z = (uint32_t)aadj) <= 0)
|
---|
1629 | z = 1;
|
---|
1630 | aadj = z;
|
---|
1631 | aadj1 = dsign ? aadj : -aadj;
|
---|
1632 | }
|
---|
1633 | dval(&aadj2) = aadj1;
|
---|
1634 | word0(&aadj2) += (2 * P + 1) * Exp_msk1 - y;
|
---|
1635 | aadj1 = dval(&aadj2);
|
---|
1636 | }
|
---|
1637 | adj.d = aadj1 * ulp(&rv);
|
---|
1638 | dval(&rv) += adj.d;
|
---|
1639 | #else
|
---|
1640 | #ifdef Sudden_Underflow
|
---|
1641 | if ((word0(&rv) & Exp_mask) <= P * Exp_msk1) {
|
---|
1642 | dval(&rv0) = dval(&rv);
|
---|
1643 | word0(&rv) += P * Exp_msk1;
|
---|
1644 | adj.d = aadj1 * ulp(&rv);
|
---|
1645 | dval(&rv) += adj.d;
|
---|
1646 | if ((word0(&rv) & Exp_mask) <= P * Exp_msk1)
|
---|
1647 | {
|
---|
1648 | if (word0(&rv0) == Tiny0 && word1(&rv0) == Tiny1)
|
---|
1649 | goto undfl;
|
---|
1650 | word0(&rv) = Tiny0;
|
---|
1651 | word1(&rv) = Tiny1;
|
---|
1652 | goto cont;
|
---|
1653 | }
|
---|
1654 | else
|
---|
1655 | word0(&rv) -= P * Exp_msk1;
|
---|
1656 | } else {
|
---|
1657 | adj.d = aadj1 * ulp(&rv);
|
---|
1658 | dval(&rv) += adj.d;
|
---|
1659 | }
|
---|
1660 | #else /*Sudden_Underflow*/
|
---|
1661 | /* Compute adj so that the IEEE rounding rules will
|
---|
1662 | * correctly round rv + adj in some half-way cases.
|
---|
1663 | * If rv * ulp(rv) is denormalized (i.e.,
|
---|
1664 | * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
|
---|
1665 | * trouble from bits lost to denormalization;
|
---|
1666 | * example: 1.2e-307 .
|
---|
1667 | */
|
---|
1668 | if (y <= (P - 1) * Exp_msk1 && aadj > 1.) {
|
---|
1669 | aadj1 = (double)(int)(aadj + 0.5);
|
---|
1670 | if (!dsign)
|
---|
1671 | aadj1 = -aadj1;
|
---|
1672 | }
|
---|
1673 | adj.d = aadj1 * ulp(&rv);
|
---|
1674 | dval(&rv) += adj.d;
|
---|
1675 | #endif /*Sudden_Underflow*/
|
---|
1676 | #endif /*Avoid_Underflow*/
|
---|
1677 | }
|
---|
1678 | z = word0(&rv) & Exp_mask;
|
---|
1679 | #ifndef SET_INEXACT
|
---|
1680 | #ifdef Avoid_Underflow
|
---|
1681 | if (!scale)
|
---|
1682 | #endif
|
---|
1683 | if (y == z) {
|
---|
1684 | /* Can we stop now? */
|
---|
1685 | L = (int32_t)aadj;
|
---|
1686 | aadj -= L;
|
---|
1687 | /* The tolerances below are conservative. */
|
---|
1688 | if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
|
---|
1689 | if (aadj < .4999999 || aadj > .5000001)
|
---|
1690 | break;
|
---|
1691 | } else if (aadj < .4999999 / FLT_RADIX)
|
---|
1692 | break;
|
---|
1693 | }
|
---|
1694 | #endif
|
---|
1695 | cont:
|
---|
1696 | ;
|
---|
1697 | }
|
---|
1698 | #ifdef SET_INEXACT
|
---|
1699 | if (inexact) {
|
---|
1700 | if (!oldinexact) {
|
---|
1701 | word0(&rv0) = Exp_1 + (70 << Exp_shift);
|
---|
1702 | word1(&rv0) = 0;
|
---|
1703 | dval(&rv0) += 1.;
|
---|
1704 | }
|
---|
1705 | } else if (!oldinexact)
|
---|
1706 | clear_inexact();
|
---|
1707 | #endif
|
---|
1708 | #ifdef Avoid_Underflow
|
---|
1709 | if (scale) {
|
---|
1710 | word0(&rv0) = Exp_1 - 2 * P * Exp_msk1;
|
---|
1711 | word1(&rv0) = 0;
|
---|
1712 | dval(&rv) *= dval(&rv0);
|
---|
1713 | #ifndef NO_ERRNO
|
---|
1714 | /* try to avoid the bug of testing an 8087 register value */
|
---|
1715 | if (word0(&rv) == 0 && word1(&rv) == 0)
|
---|
1716 | errno = ERANGE;
|
---|
1717 | #endif
|
---|
1718 | }
|
---|
1719 | #endif /* Avoid_Underflow */
|
---|
1720 | #ifdef SET_INEXACT
|
---|
1721 | if (inexact && !(word0(&rv) & Exp_mask)) {
|
---|
1722 | /* set underflow bit */
|
---|
1723 | dval(&rv0) = 1e-300;
|
---|
1724 | dval(&rv0) *= dval(&rv0);
|
---|
1725 | }
|
---|
1726 | #endif
|
---|
1727 | ret:
|
---|
1728 | if (se)
|
---|
1729 | *se = const_cast<char*>(s);
|
---|
1730 | return sign ? -dval(&rv) : dval(&rv);
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
|
---|
1734 | {
|
---|
1735 | size_t n;
|
---|
1736 | uint32_t *bx, *bxe, q, *sx, *sxe;
|
---|
1737 | #ifdef USE_LONG_LONG
|
---|
1738 | unsigned long long borrow, carry, y, ys;
|
---|
1739 | #else
|
---|
1740 | uint32_t borrow, carry, y, ys;
|
---|
1741 | #ifdef Pack_32
|
---|
1742 | uint32_t si, z, zs;
|
---|
1743 | #endif
|
---|
1744 | #endif
|
---|
1745 | ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
|
---|
1746 | ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
|
---|
1747 |
|
---|
1748 | n = S.size();
|
---|
1749 | ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
|
---|
1750 | if (b.size() < n)
|
---|
1751 | return 0;
|
---|
1752 | sx = S.words();
|
---|
1753 | sxe = sx + --n;
|
---|
1754 | bx = b.words();
|
---|
1755 | bxe = bx + n;
|
---|
1756 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
|
---|
1757 | ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
|
---|
1758 | if (q) {
|
---|
1759 | borrow = 0;
|
---|
1760 | carry = 0;
|
---|
1761 | do {
|
---|
1762 | #ifdef USE_LONG_LONG
|
---|
1763 | ys = *sx++ * (unsigned long long)q + carry;
|
---|
1764 | carry = ys >> 32;
|
---|
1765 | y = *bx - (ys & 0xffffffffUL) - borrow;
|
---|
1766 | borrow = y >> 32 & (uint32_t)1;
|
---|
1767 | *bx++ = (uint32_t)y & 0xffffffffUL;
|
---|
1768 | #else
|
---|
1769 | #ifdef Pack_32
|
---|
1770 | si = *sx++;
|
---|
1771 | ys = (si & 0xffff) * q + carry;
|
---|
1772 | zs = (si >> 16) * q + (ys >> 16);
|
---|
1773 | carry = zs >> 16;
|
---|
1774 | y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
|
---|
1775 | borrow = (y & 0x10000) >> 16;
|
---|
1776 | z = (*bx >> 16) - (zs & 0xffff) - borrow;
|
---|
1777 | borrow = (z & 0x10000) >> 16;
|
---|
1778 | Storeinc(bx, z, y);
|
---|
1779 | #else
|
---|
1780 | ys = *sx++ * q + carry;
|
---|
1781 | carry = ys >> 16;
|
---|
1782 | y = *bx - (ys & 0xffff) - borrow;
|
---|
1783 | borrow = (y & 0x10000) >> 16;
|
---|
1784 | *bx++ = y & 0xffff;
|
---|
1785 | #endif
|
---|
1786 | #endif
|
---|
1787 | } while (sx <= sxe);
|
---|
1788 | if (!*bxe) {
|
---|
1789 | bx = b.words();
|
---|
1790 | while (--bxe > bx && !*bxe)
|
---|
1791 | --n;
|
---|
1792 | b.resize(n);
|
---|
1793 | }
|
---|
1794 | }
|
---|
1795 | if (cmp(b, S) >= 0) {
|
---|
1796 | q++;
|
---|
1797 | borrow = 0;
|
---|
1798 | carry = 0;
|
---|
1799 | bx = b.words();
|
---|
1800 | sx = S.words();
|
---|
1801 | do {
|
---|
1802 | #ifdef USE_LONG_LONG
|
---|
1803 | ys = *sx++ + carry;
|
---|
1804 | carry = ys >> 32;
|
---|
1805 | y = *bx - (ys & 0xffffffffUL) - borrow;
|
---|
1806 | borrow = y >> 32 & (uint32_t)1;
|
---|
1807 | *bx++ = (uint32_t)y & 0xffffffffUL;
|
---|
1808 | #else
|
---|
1809 | #ifdef Pack_32
|
---|
1810 | si = *sx++;
|
---|
1811 | ys = (si & 0xffff) + carry;
|
---|
1812 | zs = (si >> 16) + (ys >> 16);
|
---|
1813 | carry = zs >> 16;
|
---|
1814 | y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
|
---|
1815 | borrow = (y & 0x10000) >> 16;
|
---|
1816 | z = (*bx >> 16) - (zs & 0xffff) - borrow;
|
---|
1817 | borrow = (z & 0x10000) >> 16;
|
---|
1818 | Storeinc(bx, z, y);
|
---|
1819 | #else
|
---|
1820 | ys = *sx++ + carry;
|
---|
1821 | carry = ys >> 16;
|
---|
1822 | y = *bx - (ys & 0xffff) - borrow;
|
---|
1823 | borrow = (y & 0x10000) >> 16;
|
---|
1824 | *bx++ = y & 0xffff;
|
---|
1825 | #endif
|
---|
1826 | #endif
|
---|
1827 | } while (sx <= sxe);
|
---|
1828 | bx = b.words();
|
---|
1829 | bxe = bx + n;
|
---|
1830 | if (!*bxe) {
|
---|
1831 | while (--bxe > bx && !*bxe)
|
---|
1832 | --n;
|
---|
1833 | b.resize(n);
|
---|
1834 | }
|
---|
1835 | }
|
---|
1836 | return q;
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 | /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
|
---|
1840 | *
|
---|
1841 | * Inspired by "How to Print Floating-Point Numbers Accurately" by
|
---|
1842 | * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
|
---|
1843 | *
|
---|
1844 | * Modifications:
|
---|
1845 | * 1. Rather than iterating, we use a simple numeric overestimate
|
---|
1846 | * to determine k = floor(log10(d)). We scale relevant
|
---|
1847 | * quantities using O(log2(k)) rather than O(k) multiplications.
|
---|
1848 | * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
|
---|
1849 | * try to generate digits strictly left to right. Instead, we
|
---|
1850 | * compute with fewer bits and propagate the carry if necessary
|
---|
1851 | * when rounding the final digit up. This is often faster.
|
---|
1852 | * 3. Under the assumption that input will be rounded nearest,
|
---|
1853 | * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
|
---|
1854 | * That is, we allow equality in stopping tests when the
|
---|
1855 | * round-nearest rule will give the same floating-point value
|
---|
1856 | * as would satisfaction of the stopping test with strict
|
---|
1857 | * inequality.
|
---|
1858 | * 4. We remove common factors of powers of 2 from relevant
|
---|
1859 | * quantities.
|
---|
1860 | * 5. When converting floating-point integers less than 1e16,
|
---|
1861 | * we use floating-point arithmetic rather than resorting
|
---|
1862 | * to multiple-precision integers.
|
---|
1863 | * 6. When asked to produce fewer than 15 digits, we first try
|
---|
1864 | * to get by with floating-point arithmetic; we resort to
|
---|
1865 | * multiple-precision integer arithmetic only if we cannot
|
---|
1866 | * guarantee that the floating-point calculation has given
|
---|
1867 | * the correctly rounded result. For k requested digits and
|
---|
1868 | * "uniformly" distributed input, the probability is
|
---|
1869 | * something like 10^(k-15) that we must resort to the int32_t
|
---|
1870 | * calculation.
|
---|
1871 | */
|
---|
1872 |
|
---|
1873 | void dtoa(DtoaBuffer result, double dd, int ndigits, int* decpt, int* sign, char** rve)
|
---|
1874 | {
|
---|
1875 | /*
|
---|
1876 | Arguments ndigits, decpt, sign are similar to those
|
---|
1877 | of ecvt and fcvt; trailing zeros are suppressed from
|
---|
1878 | the returned string. If not null, *rve is set to point
|
---|
1879 | to the end of the return value. If d is +-Infinity or NaN,
|
---|
1880 | then *decpt is set to 9999.
|
---|
1881 |
|
---|
1882 | */
|
---|
1883 |
|
---|
1884 | int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
|
---|
1885 | j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
|
---|
1886 | spec_case, try_quick;
|
---|
1887 | int32_t L;
|
---|
1888 | #ifndef Sudden_Underflow
|
---|
1889 | int denorm;
|
---|
1890 | uint32_t x;
|
---|
1891 | #endif
|
---|
1892 | BigInt b, b1, delta, mlo, mhi, S;
|
---|
1893 | U d2, eps, u;
|
---|
1894 | double ds;
|
---|
1895 | char *s, *s0;
|
---|
1896 | #ifdef SET_INEXACT
|
---|
1897 | int inexact, oldinexact;
|
---|
1898 | #endif
|
---|
1899 |
|
---|
1900 | u.d = dd;
|
---|
1901 | if (word0(&u) & Sign_bit) {
|
---|
1902 | /* set sign for everything, including 0's and NaNs */
|
---|
1903 | *sign = 1;
|
---|
1904 | word0(&u) &= ~Sign_bit; /* clear sign bit */
|
---|
1905 | } else
|
---|
1906 | *sign = 0;
|
---|
1907 |
|
---|
1908 | if ((word0(&u) & Exp_mask) == Exp_mask)
|
---|
1909 | {
|
---|
1910 | /* Infinity or NaN */
|
---|
1911 | *decpt = 9999;
|
---|
1912 | if (!word1(&u) && !(word0(&u) & 0xfffff)) {
|
---|
1913 | strcpy(result, "Infinity");
|
---|
1914 | if (rve)
|
---|
1915 | *rve = result + 8;
|
---|
1916 | } else {
|
---|
1917 | strcpy(result, "NaN");
|
---|
1918 | if (rve)
|
---|
1919 | *rve = result + 3;
|
---|
1920 | }
|
---|
1921 | return;
|
---|
1922 | }
|
---|
1923 | if (!dval(&u)) {
|
---|
1924 | *decpt = 1;
|
---|
1925 | result[0] = '0';
|
---|
1926 | result[1] = '\0';
|
---|
1927 | if (rve)
|
---|
1928 | *rve = result + 1;
|
---|
1929 | return;
|
---|
1930 | }
|
---|
1931 |
|
---|
1932 | #ifdef SET_INEXACT
|
---|
1933 | try_quick = oldinexact = get_inexact();
|
---|
1934 | inexact = 1;
|
---|
1935 | #endif
|
---|
1936 |
|
---|
1937 | d2b(b, &u, &be, &bbits);
|
---|
1938 | #ifdef Sudden_Underflow
|
---|
1939 | i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
|
---|
1940 | #else
|
---|
1941 | if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
|
---|
1942 | #endif
|
---|
1943 | dval(&d2) = dval(&u);
|
---|
1944 | word0(&d2) &= Frac_mask1;
|
---|
1945 | word0(&d2) |= Exp_11;
|
---|
1946 |
|
---|
1947 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
|
---|
1948 | * log10(x) = log(x) / log(10)
|
---|
1949 | * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
|
---|
1950 | * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
|
---|
1951 | *
|
---|
1952 | * This suggests computing an approximation k to log10(d) by
|
---|
1953 | *
|
---|
1954 | * k = (i - Bias)*0.301029995663981
|
---|
1955 | * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
|
---|
1956 | *
|
---|
1957 | * We want k to be too large rather than too small.
|
---|
1958 | * The error in the first-order Taylor series approximation
|
---|
1959 | * is in our favor, so we just round up the constant enough
|
---|
1960 | * to compensate for any error in the multiplication of
|
---|
1961 | * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
|
---|
1962 | * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
|
---|
1963 | * adding 1e-13 to the constant term more than suffices.
|
---|
1964 | * Hence we adjust the constant term to 0.1760912590558.
|
---|
1965 | * (We could get a more accurate k by invoking log10,
|
---|
1966 | * but this is probably not worthwhile.)
|
---|
1967 | */
|
---|
1968 |
|
---|
1969 | i -= Bias;
|
---|
1970 | #ifndef Sudden_Underflow
|
---|
1971 | denorm = 0;
|
---|
1972 | } else {
|
---|
1973 | /* d is denormalized */
|
---|
1974 |
|
---|
1975 | i = bbits + be + (Bias + (P - 1) - 1);
|
---|
1976 | x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
|
---|
1977 | : word1(&u) << (32 - i);
|
---|
1978 | dval(&d2) = x;
|
---|
1979 | word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
|
---|
1980 | i -= (Bias + (P - 1) - 1) + 1;
|
---|
1981 | denorm = 1;
|
---|
1982 | }
|
---|
1983 | #endif
|
---|
1984 | ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
|
---|
1985 | k = (int)ds;
|
---|
1986 | if (ds < 0. && ds != k)
|
---|
1987 | k--; /* want k = floor(ds) */
|
---|
1988 | k_check = 1;
|
---|
1989 | if (k >= 0 && k <= Ten_pmax) {
|
---|
1990 | if (dval(&u) < tens[k])
|
---|
1991 | k--;
|
---|
1992 | k_check = 0;
|
---|
1993 | }
|
---|
1994 | j = bbits - i - 1;
|
---|
1995 | if (j >= 0) {
|
---|
1996 | b2 = 0;
|
---|
1997 | s2 = j;
|
---|
1998 | } else {
|
---|
1999 | b2 = -j;
|
---|
2000 | s2 = 0;
|
---|
2001 | }
|
---|
2002 | if (k >= 0) {
|
---|
2003 | b5 = 0;
|
---|
2004 | s5 = k;
|
---|
2005 | s2 += k;
|
---|
2006 | } else {
|
---|
2007 | b2 -= k;
|
---|
2008 | b5 = -k;
|
---|
2009 | s5 = 0;
|
---|
2010 | }
|
---|
2011 |
|
---|
2012 | #ifndef SET_INEXACT
|
---|
2013 | #ifdef Check_FLT_ROUNDS
|
---|
2014 | try_quick = Rounding == 1;
|
---|
2015 | #else
|
---|
2016 | try_quick = 1;
|
---|
2017 | #endif
|
---|
2018 | #endif /*SET_INEXACT*/
|
---|
2019 |
|
---|
2020 | leftright = 1;
|
---|
2021 | ilim = ilim1 = -1;
|
---|
2022 | i = 18;
|
---|
2023 | ndigits = 0;
|
---|
2024 | s = s0 = result;
|
---|
2025 |
|
---|
2026 | if (ilim >= 0 && ilim <= Quick_max && try_quick) {
|
---|
2027 |
|
---|
2028 | /* Try to get by with floating-point arithmetic. */
|
---|
2029 |
|
---|
2030 | i = 0;
|
---|
2031 | dval(&d2) = dval(&u);
|
---|
2032 | k0 = k;
|
---|
2033 | ilim0 = ilim;
|
---|
2034 | ieps = 2; /* conservative */
|
---|
2035 | if (k > 0) {
|
---|
2036 | ds = tens[k & 0xf];
|
---|
2037 | j = k >> 4;
|
---|
2038 | if (j & Bletch) {
|
---|
2039 | /* prevent overflows */
|
---|
2040 | j &= Bletch - 1;
|
---|
2041 | dval(&u) /= bigtens[n_bigtens - 1];
|
---|
2042 | ieps++;
|
---|
2043 | }
|
---|
2044 | for (; j; j >>= 1, i++) {
|
---|
2045 | if (j & 1) {
|
---|
2046 | ieps++;
|
---|
2047 | ds *= bigtens[i];
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 | dval(&u) /= ds;
|
---|
2051 | } else if ((j1 = -k)) {
|
---|
2052 | dval(&u) *= tens[j1 & 0xf];
|
---|
2053 | for (j = j1 >> 4; j; j >>= 1, i++) {
|
---|
2054 | if (j & 1) {
|
---|
2055 | ieps++;
|
---|
2056 | dval(&u) *= bigtens[i];
|
---|
2057 | }
|
---|
2058 | }
|
---|
2059 | }
|
---|
2060 | if (k_check && dval(&u) < 1. && ilim > 0) {
|
---|
2061 | if (ilim1 <= 0)
|
---|
2062 | goto fast_failed;
|
---|
2063 | ilim = ilim1;
|
---|
2064 | k--;
|
---|
2065 | dval(&u) *= 10.;
|
---|
2066 | ieps++;
|
---|
2067 | }
|
---|
2068 | dval(&eps) = (ieps * dval(&u)) + 7.;
|
---|
2069 | word0(&eps) -= (P - 1) * Exp_msk1;
|
---|
2070 | if (ilim == 0) {
|
---|
2071 | S.clear();
|
---|
2072 | mhi.clear();
|
---|
2073 | dval(&u) -= 5.;
|
---|
2074 | if (dval(&u) > dval(&eps))
|
---|
2075 | goto one_digit;
|
---|
2076 | if (dval(&u) < -dval(&eps))
|
---|
2077 | goto no_digits;
|
---|
2078 | goto fast_failed;
|
---|
2079 | }
|
---|
2080 | #ifndef No_leftright
|
---|
2081 | if (leftright) {
|
---|
2082 | /* Use Steele & White method of only
|
---|
2083 | * generating digits needed.
|
---|
2084 | */
|
---|
2085 | dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
|
---|
2086 | for (i = 0;;) {
|
---|
2087 | L = (long int)dval(&u);
|
---|
2088 | dval(&u) -= L;
|
---|
2089 | *s++ = '0' + (int)L;
|
---|
2090 | if (dval(&u) < dval(&eps))
|
---|
2091 | goto ret;
|
---|
2092 | if (1. - dval(&u) < dval(&eps))
|
---|
2093 | goto bump_up;
|
---|
2094 | if (++i >= ilim)
|
---|
2095 | break;
|
---|
2096 | dval(&eps) *= 10.;
|
---|
2097 | dval(&u) *= 10.;
|
---|
2098 | }
|
---|
2099 | } else {
|
---|
2100 | #endif
|
---|
2101 | /* Generate ilim digits, then fix them up. */
|
---|
2102 | dval(&eps) *= tens[ilim - 1];
|
---|
2103 | for (i = 1;; i++, dval(&u) *= 10.) {
|
---|
2104 | L = (int32_t)(dval(&u));
|
---|
2105 | if (!(dval(&u) -= L))
|
---|
2106 | ilim = i;
|
---|
2107 | *s++ = '0' + (int)L;
|
---|
2108 | if (i == ilim) {
|
---|
2109 | if (dval(&u) > 0.5 + dval(&eps))
|
---|
2110 | goto bump_up;
|
---|
2111 | else if (dval(&u) < 0.5 - dval(&eps)) {
|
---|
2112 | while (*--s == '0') { }
|
---|
2113 | s++;
|
---|
2114 | goto ret;
|
---|
2115 | }
|
---|
2116 | break;
|
---|
2117 | }
|
---|
2118 | }
|
---|
2119 | #ifndef No_leftright
|
---|
2120 | }
|
---|
2121 | #endif
|
---|
2122 | fast_failed:
|
---|
2123 | s = s0;
|
---|
2124 | dval(&u) = dval(&d2);
|
---|
2125 | k = k0;
|
---|
2126 | ilim = ilim0;
|
---|
2127 | }
|
---|
2128 |
|
---|
2129 | /* Do we have a "small" integer? */
|
---|
2130 |
|
---|
2131 | if (be >= 0 && k <= Int_max) {
|
---|
2132 | /* Yes. */
|
---|
2133 | ds = tens[k];
|
---|
2134 | if (ndigits < 0 && ilim <= 0) {
|
---|
2135 | S.clear();
|
---|
2136 | mhi.clear();
|
---|
2137 | if (ilim < 0 || dval(&u) <= 5 * ds)
|
---|
2138 | goto no_digits;
|
---|
2139 | goto one_digit;
|
---|
2140 | }
|
---|
2141 | for (i = 1;; i++, dval(&u) *= 10.) {
|
---|
2142 | L = (int32_t)(dval(&u) / ds);
|
---|
2143 | dval(&u) -= L * ds;
|
---|
2144 | #ifdef Check_FLT_ROUNDS
|
---|
2145 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */
|
---|
2146 | if (dval(&u) < 0) {
|
---|
2147 | L--;
|
---|
2148 | dval(&u) += ds;
|
---|
2149 | }
|
---|
2150 | #endif
|
---|
2151 | *s++ = '0' + (int)L;
|
---|
2152 | if (!dval(&u)) {
|
---|
2153 | #ifdef SET_INEXACT
|
---|
2154 | inexact = 0;
|
---|
2155 | #endif
|
---|
2156 | break;
|
---|
2157 | }
|
---|
2158 | if (i == ilim) {
|
---|
2159 | dval(&u) += dval(&u);
|
---|
2160 | if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
|
---|
2161 | bump_up:
|
---|
2162 | while (*--s == '9')
|
---|
2163 | if (s == s0) {
|
---|
2164 | k++;
|
---|
2165 | *s = '0';
|
---|
2166 | break;
|
---|
2167 | }
|
---|
2168 | ++*s++;
|
---|
2169 | }
|
---|
2170 | break;
|
---|
2171 | }
|
---|
2172 | }
|
---|
2173 | goto ret;
|
---|
2174 | }
|
---|
2175 |
|
---|
2176 | m2 = b2;
|
---|
2177 | m5 = b5;
|
---|
2178 | mhi.clear();
|
---|
2179 | mlo.clear();
|
---|
2180 | if (leftright) {
|
---|
2181 | i =
|
---|
2182 | #ifndef Sudden_Underflow
|
---|
2183 | denorm ? be + (Bias + (P - 1) - 1 + 1) :
|
---|
2184 | #endif
|
---|
2185 | 1 + P - bbits;
|
---|
2186 | b2 += i;
|
---|
2187 | s2 += i;
|
---|
2188 | i2b(mhi, 1);
|
---|
2189 | }
|
---|
2190 | if (m2 > 0 && s2 > 0) {
|
---|
2191 | i = m2 < s2 ? m2 : s2;
|
---|
2192 | b2 -= i;
|
---|
2193 | m2 -= i;
|
---|
2194 | s2 -= i;
|
---|
2195 | }
|
---|
2196 | if (b5 > 0) {
|
---|
2197 | if (leftright) {
|
---|
2198 | if (m5 > 0) {
|
---|
2199 | pow5mult(mhi, m5);
|
---|
2200 | mult(b, mhi);
|
---|
2201 | }
|
---|
2202 | if ((j = b5 - m5))
|
---|
2203 | pow5mult(b, j);
|
---|
2204 | } else
|
---|
2205 | pow5mult(b, b5);
|
---|
2206 | }
|
---|
2207 | i2b(S, 1);
|
---|
2208 | if (s5 > 0)
|
---|
2209 | pow5mult(S, s5);
|
---|
2210 |
|
---|
2211 | /* Check for special case that d is a normalized power of 2. */
|
---|
2212 |
|
---|
2213 | spec_case = 0;
|
---|
2214 | if (!word1(&u) && !(word0(&u) & Bndry_mask)
|
---|
2215 | #ifndef Sudden_Underflow
|
---|
2216 | && word0(&u) & (Exp_mask & ~Exp_msk1)
|
---|
2217 | #endif
|
---|
2218 | ) {
|
---|
2219 | /* The special case */
|
---|
2220 | b2 += Log2P;
|
---|
2221 | s2 += Log2P;
|
---|
2222 | spec_case = 1;
|
---|
2223 | }
|
---|
2224 |
|
---|
2225 | /* Arrange for convenient computation of quotients:
|
---|
2226 | * shift left if necessary so divisor has 4 leading 0 bits.
|
---|
2227 | *
|
---|
2228 | * Perhaps we should just compute leading 28 bits of S once
|
---|
2229 | * and for all and pass them and a shift to quorem, so it
|
---|
2230 | * can do shifts and ors to compute the numerator for q.
|
---|
2231 | */
|
---|
2232 | #ifdef Pack_32
|
---|
2233 | if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
|
---|
2234 | i = 32 - i;
|
---|
2235 | #else
|
---|
2236 | if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0xf))
|
---|
2237 | i = 16 - i;
|
---|
2238 | #endif
|
---|
2239 | if (i > 4) {
|
---|
2240 | i -= 4;
|
---|
2241 | b2 += i;
|
---|
2242 | m2 += i;
|
---|
2243 | s2 += i;
|
---|
2244 | } else if (i < 4) {
|
---|
2245 | i += 28;
|
---|
2246 | b2 += i;
|
---|
2247 | m2 += i;
|
---|
2248 | s2 += i;
|
---|
2249 | }
|
---|
2250 | if (b2 > 0)
|
---|
2251 | lshift(b, b2);
|
---|
2252 | if (s2 > 0)
|
---|
2253 | lshift(S, s2);
|
---|
2254 | if (k_check) {
|
---|
2255 | if (cmp(b,S) < 0) {
|
---|
2256 | k--;
|
---|
2257 | multadd(b, 10, 0); /* we botched the k estimate */
|
---|
2258 | if (leftright)
|
---|
2259 | multadd(mhi, 10, 0);
|
---|
2260 | ilim = ilim1;
|
---|
2261 | }
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | if (leftright) {
|
---|
2265 | if (m2 > 0)
|
---|
2266 | lshift(mhi, m2);
|
---|
2267 |
|
---|
2268 | /* Compute mlo -- check for special case
|
---|
2269 | * that d is a normalized power of 2.
|
---|
2270 | */
|
---|
2271 |
|
---|
2272 | mlo = mhi;
|
---|
2273 | if (spec_case) {
|
---|
2274 | mhi = mlo;
|
---|
2275 | lshift(mhi, Log2P);
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 | for (i = 1;;i++) {
|
---|
2279 | dig = quorem(b,S) + '0';
|
---|
2280 | /* Do we yet have the shortest decimal string
|
---|
2281 | * that will round to d?
|
---|
2282 | */
|
---|
2283 | j = cmp(b, mlo);
|
---|
2284 | diff(delta, S, mhi);
|
---|
2285 | j1 = delta.sign ? 1 : cmp(b, delta);
|
---|
2286 | if (j1 == 0 && !(word1(&u) & 1)) {
|
---|
2287 | if (dig == '9')
|
---|
2288 | goto round_9_up;
|
---|
2289 | if (j > 0)
|
---|
2290 | dig++;
|
---|
2291 | #ifdef SET_INEXACT
|
---|
2292 | else if (!b->x[0] && b->wds <= 1)
|
---|
2293 | inexact = 0;
|
---|
2294 | #endif
|
---|
2295 | *s++ = dig;
|
---|
2296 | goto ret;
|
---|
2297 | }
|
---|
2298 | if (j < 0 || (j == 0 && !(word1(&u) & 1))) {
|
---|
2299 | if (!b.words()[0] && b.size() <= 1) {
|
---|
2300 | #ifdef SET_INEXACT
|
---|
2301 | inexact = 0;
|
---|
2302 | #endif
|
---|
2303 | goto accept_dig;
|
---|
2304 | }
|
---|
2305 | if (j1 > 0) {
|
---|
2306 | lshift(b, 1);
|
---|
2307 | j1 = cmp(b, S);
|
---|
2308 | if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9')
|
---|
2309 | goto round_9_up;
|
---|
2310 | }
|
---|
2311 | accept_dig:
|
---|
2312 | *s++ = dig;
|
---|
2313 | goto ret;
|
---|
2314 | }
|
---|
2315 | if (j1 > 0) {
|
---|
2316 | if (dig == '9') { /* possible if i == 1 */
|
---|
2317 | round_9_up:
|
---|
2318 | *s++ = '9';
|
---|
2319 | goto roundoff;
|
---|
2320 | }
|
---|
2321 | *s++ = dig + 1;
|
---|
2322 | goto ret;
|
---|
2323 | }
|
---|
2324 | *s++ = dig;
|
---|
2325 | if (i == ilim)
|
---|
2326 | break;
|
---|
2327 | multadd(b, 10, 0);
|
---|
2328 | multadd(mlo, 10, 0);
|
---|
2329 | multadd(mhi, 10, 0);
|
---|
2330 | }
|
---|
2331 | } else
|
---|
2332 | for (i = 1;; i++) {
|
---|
2333 | *s++ = dig = quorem(b,S) + '0';
|
---|
2334 | if (!b.words()[0] && b.size() <= 1) {
|
---|
2335 | #ifdef SET_INEXACT
|
---|
2336 | inexact = 0;
|
---|
2337 | #endif
|
---|
2338 | goto ret;
|
---|
2339 | }
|
---|
2340 | if (i >= ilim)
|
---|
2341 | break;
|
---|
2342 | multadd(b, 10, 0);
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 | /* Round off last digit */
|
---|
2346 |
|
---|
2347 | lshift(b, 1);
|
---|
2348 | j = cmp(b, S);
|
---|
2349 | if (j > 0 || (j == 0 && (dig & 1))) {
|
---|
2350 | roundoff:
|
---|
2351 | while (*--s == '9')
|
---|
2352 | if (s == s0) {
|
---|
2353 | k++;
|
---|
2354 | *s++ = '1';
|
---|
2355 | goto ret;
|
---|
2356 | }
|
---|
2357 | ++*s++;
|
---|
2358 | } else {
|
---|
2359 | while (*--s == '0') { }
|
---|
2360 | s++;
|
---|
2361 | }
|
---|
2362 | goto ret;
|
---|
2363 | no_digits:
|
---|
2364 | k = -1 - ndigits;
|
---|
2365 | goto ret;
|
---|
2366 | one_digit:
|
---|
2367 | *s++ = '1';
|
---|
2368 | k++;
|
---|
2369 | goto ret;
|
---|
2370 | ret:
|
---|
2371 | #ifdef SET_INEXACT
|
---|
2372 | if (inexact) {
|
---|
2373 | if (!oldinexact) {
|
---|
2374 | word0(&u) = Exp_1 + (70 << Exp_shift);
|
---|
2375 | word1(&u) = 0;
|
---|
2376 | dval(&u) += 1.;
|
---|
2377 | }
|
---|
2378 | } else if (!oldinexact)
|
---|
2379 | clear_inexact();
|
---|
2380 | #endif
|
---|
2381 | *s = 0;
|
---|
2382 | *decpt = k + 1;
|
---|
2383 | if (rve)
|
---|
2384 | *rve = s;
|
---|
2385 | }
|
---|
2386 |
|
---|
2387 | static ALWAYS_INLINE void append(char*& next, const char* src, unsigned size)
|
---|
2388 | {
|
---|
2389 | for (unsigned i = 0; i < size; ++i)
|
---|
2390 | *next++ = *src++;
|
---|
2391 | }
|
---|
2392 |
|
---|
2393 | void doubleToStringInJavaScriptFormat(double d, DtoaBuffer buffer, unsigned* resultLength)
|
---|
2394 | {
|
---|
2395 | ASSERT(buffer);
|
---|
2396 |
|
---|
2397 | // avoid ever printing -NaN, in JS conceptually there is only one NaN value
|
---|
2398 | if (isnan(d)) {
|
---|
2399 | append(buffer, "NaN", 3);
|
---|
2400 | if (resultLength)
|
---|
2401 | *resultLength = 3;
|
---|
2402 | return;
|
---|
2403 | }
|
---|
2404 | // -0 -> "0"
|
---|
2405 | if (!d) {
|
---|
2406 | buffer[0] = '0';
|
---|
2407 | if (resultLength)
|
---|
2408 | *resultLength = 1;
|
---|
2409 | return;
|
---|
2410 | }
|
---|
2411 |
|
---|
2412 | int decimalPoint;
|
---|
2413 | int sign;
|
---|
2414 |
|
---|
2415 | DtoaBuffer result;
|
---|
2416 | char* resultEnd = 0;
|
---|
2417 | WTF::dtoa(result, d, 0, &decimalPoint, &sign, &resultEnd);
|
---|
2418 | int length = resultEnd - result;
|
---|
2419 |
|
---|
2420 | char* next = buffer;
|
---|
2421 | if (sign)
|
---|
2422 | *next++ = '-';
|
---|
2423 |
|
---|
2424 | if (decimalPoint <= 0 && decimalPoint > -6) {
|
---|
2425 | *next++ = '0';
|
---|
2426 | *next++ = '.';
|
---|
2427 | for (int j = decimalPoint; j < 0; j++)
|
---|
2428 | *next++ = '0';
|
---|
2429 | append(next, result, length);
|
---|
2430 | } else if (decimalPoint <= 21 && decimalPoint > 0) {
|
---|
2431 | if (length <= decimalPoint) {
|
---|
2432 | append(next, result, length);
|
---|
2433 | for (int j = 0; j < decimalPoint - length; j++)
|
---|
2434 | *next++ = '0';
|
---|
2435 | } else {
|
---|
2436 | append(next, result, decimalPoint);
|
---|
2437 | *next++ = '.';
|
---|
2438 | append(next, result + decimalPoint, length - decimalPoint);
|
---|
2439 | }
|
---|
2440 | } else if (result[0] < '0' || result[0] > '9')
|
---|
2441 | append(next, result, length);
|
---|
2442 | else {
|
---|
2443 | *next++ = result[0];
|
---|
2444 | if (length > 1) {
|
---|
2445 | *next++ = '.';
|
---|
2446 | append(next, result + 1, length - 1);
|
---|
2447 | }
|
---|
2448 |
|
---|
2449 | *next++ = 'e';
|
---|
2450 | *next++ = (decimalPoint >= 0) ? '+' : '-';
|
---|
2451 | // decimalPoint can't be more than 3 digits decimal given the
|
---|
2452 | // nature of float representation
|
---|
2453 | int exponential = decimalPoint - 1;
|
---|
2454 | if (exponential < 0)
|
---|
2455 | exponential = -exponential;
|
---|
2456 | if (exponential >= 100)
|
---|
2457 | *next++ = static_cast<char>('0' + exponential / 100);
|
---|
2458 | if (exponential >= 10)
|
---|
2459 | *next++ = static_cast<char>('0' + (exponential % 100) / 10);
|
---|
2460 | *next++ = static_cast<char>('0' + exponential % 10);
|
---|
2461 | }
|
---|
2462 | if (resultLength)
|
---|
2463 | *resultLength = next - buffer;
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | } // namespace WTF
|
---|