]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/dtoa.cpp
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / wtf / dtoa.cpp
CommitLineData
9dae56ea
A
1/****************************************************************
2 *
3 * The author of this software is David M. Gay.
4 *
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
14957cd0 6 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
9dae56ea
A
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
14957cd0
A
21/* Please send bug reports to David M. Gay (dmg at acm dot org,
22 * with " at " changed at "@" and " dot " changed to "."). */
9dae56ea
A
23
24/* On a machine with IEEE extended-precision registers, it is
25 * necessary to specify double-precision (53-bit) rounding precision
26 * before invoking strtod or dtoa. If the machine uses (the equivalent
27 * of) Intel 80x87 arithmetic, the call
28 * _control87(PC_53, MCW_PC);
29 * does this with many compilers. Whether this or another call is
30 * appropriate depends on the compiler; for this to work, it may be
31 * necessary to #include "float.h" or another system-dependent header
32 * file.
33 */
34
35/* strtod for IEEE-arithmetic machines.
36 *
37 * This strtod returns a nearest machine number to the input decimal
38 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
39 * broken by the IEEE round-even rule. Otherwise ties are broken by
40 * biased rounding (add half and chop).
41 *
42 * Inspired loosely by William D. Clinger's paper "How to Read Floating
43 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
44 *
45 * Modifications:
46 *
14957cd0 47 * 1. We only require IEEE double-precision arithmetic (not IEEE double-extended).
9dae56ea
A
48 * 2. We get by with floating-point arithmetic in a case that
49 * Clinger missed -- when we're computing d * 10^n
50 * for a small integer d and the integer n is not too
51 * much larger than 22 (the maximum integer k for which
52 * we can represent 10^k exactly), we may be able to
53 * compute (d*10^k) * 10^(e-k) with just one roundoff.
54 * 3. Rather than a bit-at-a-time adjustment of the binary
55 * result in the hard case, we use floating-point
56 * arithmetic to determine the adjustment to within
57 * one bit; only in really hard cases do we need to
58 * compute a second residual.
59 * 4. Because of 3., we don't need a large table of powers of 10
60 * for ten-to-e (just some small tables, e.g. of 10^k
61 * for 0 <= k <= 22).
62 */
63
9dae56ea
A
64#include "config.h"
65#include "dtoa.h"
66
67#if HAVE(ERRNO_H)
68#include <errno.h>
9dae56ea 69#endif
14957cd0 70#include <float.h>
9dae56ea
A
71#include <math.h>
72#include <stdint.h>
14957cd0 73#include <stdio.h>
9dae56ea
A
74#include <stdlib.h>
75#include <string.h>
76#include <wtf/AlwaysInline.h>
77#include <wtf/Assertions.h>
14957cd0 78#include <wtf/DecimalNumber.h>
9dae56ea 79#include <wtf/FastMalloc.h>
f9bf01c6 80#include <wtf/MathExtras.h>
9dae56ea 81#include <wtf/Threading.h>
14957cd0
A
82#include <wtf/UnusedParam.h>
83#include <wtf/Vector.h>
ba379fdc 84
9dae56ea
A
85#if COMPILER(MSVC)
86#pragma warning(disable: 4244)
87#pragma warning(disable: 4245)
88#pragma warning(disable: 4554)
89#endif
90
9dae56ea
A
91namespace WTF {
92
14957cd0 93#if ENABLE(WTF_MULTIPLE_THREADS)
9dae56ea
A
94Mutex* s_dtoaP5Mutex;
95#endif
96
14957cd0
A
97typedef union {
98 double d;
99 uint32_t L[2];
100} U;
9dae56ea 101
14957cd0
A
102#if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
103#define word0(x) (x)->L[0]
104#define word1(x) (x)->L[1]
9dae56ea 105#else
ba379fdc
A
106#define word0(x) (x)->L[1]
107#define word1(x) (x)->L[0]
9dae56ea 108#endif
ba379fdc 109#define dval(x) (x)->d
9dae56ea
A
110
111/* The following definition of Storeinc is appropriate for MIPS processors.
112 * An alternative that might be better on some machines is
14957cd0 113 * *p++ = high << 16 | low & 0xffff;
9dae56ea 114 */
14957cd0
A
115static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low)
116{
117 uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
118#if CPU(BIG_ENDIAN)
119 p16[0] = high;
120 p16[1] = low;
9dae56ea 121#else
14957cd0
A
122 p16[1] = high;
123 p16[0] = low;
9dae56ea 124#endif
14957cd0
A
125 return p + 1;
126}
9dae56ea
A
127
128#define Exp_shift 20
129#define Exp_shift1 20
130#define Exp_msk1 0x100000
131#define Exp_msk11 0x100000
132#define Exp_mask 0x7ff00000
133#define P 53
134#define Bias 1023
135#define Emin (-1022)
136#define Exp_1 0x3ff00000
137#define Exp_11 0x3ff00000
138#define Ebits 11
139#define Frac_mask 0xfffff
140#define Frac_mask1 0xfffff
141#define Ten_pmax 22
142#define Bletch 0x10
143#define Bndry_mask 0xfffff
144#define Bndry_mask1 0xfffff
145#define LSB 1
146#define Sign_bit 0x80000000
147#define Log2P 1
148#define Tiny0 0
149#define Tiny1 1
150#define Quick_max 14
151#define Int_max 14
152
14957cd0
A
153#define rounded_product(a, b) a *= b
154#define rounded_quotient(a, b) a /= b
9dae56ea
A
155
156#define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
157#define Big1 0xffffffff
158
f9bf01c6
A
159#if CPU(PPC64) || CPU(X86_64)
160// FIXME: should we enable this on all 64-bit CPUs?
9dae56ea
A
161// 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
162#define USE_LONG_LONG
163#endif
164
ba379fdc
A
165struct BigInt {
166 BigInt() : sign(0) { }
167 int sign;
9dae56ea 168
ba379fdc
A
169 void clear()
170 {
171 sign = 0;
172 m_words.clear();
173 }
14957cd0 174
ba379fdc
A
175 size_t size() const
176 {
177 return m_words.size();
178 }
9dae56ea 179
ba379fdc
A
180 void resize(size_t s)
181 {
182 m_words.resize(s);
183 }
14957cd0 184
ba379fdc
A
185 uint32_t* words()
186 {
187 return m_words.data();
188 }
9dae56ea 189
ba379fdc
A
190 const uint32_t* words() const
191 {
192 return m_words.data();
193 }
14957cd0 194
ba379fdc
A
195 void append(uint32_t w)
196 {
197 m_words.append(w);
198 }
14957cd0 199
ba379fdc
A
200 Vector<uint32_t, 16> m_words;
201};
9dae56ea 202
ba379fdc 203static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */
9dae56ea
A
204{
205#ifdef USE_LONG_LONG
206 unsigned long long carry;
207#else
208 uint32_t carry;
209#endif
210
ba379fdc
A
211 int wds = b.size();
212 uint32_t* x = b.words();
9dae56ea
A
213 int i = 0;
214 carry = a;
215 do {
216#ifdef USE_LONG_LONG
217 unsigned long long y = *x * (unsigned long long)m + carry;
218 carry = y >> 32;
219 *x++ = (uint32_t)y & 0xffffffffUL;
220#else
9dae56ea
A
221 uint32_t xi = *x;
222 uint32_t y = (xi & 0xffff) * m + carry;
223 uint32_t z = (xi >> 16) * m + (y >> 16);
224 carry = z >> 16;
225 *x++ = (z << 16) + (y & 0xffff);
9dae56ea
A
226#endif
227 } while (++i < wds);
228
ba379fdc
A
229 if (carry)
230 b.append((uint32_t)carry);
9dae56ea
A
231}
232
ba379fdc 233static void s2b(BigInt& b, const char* s, int nd0, int nd, uint32_t y9)
9dae56ea 234{
ba379fdc
A
235 b.sign = 0;
236 b.resize(1);
237 b.words()[0] = y9;
9dae56ea
A
238
239 int i = 9;
240 if (9 < nd0) {
241 s += 9;
242 do {
ba379fdc 243 multadd(b, 10, *s++ - '0');
9dae56ea
A
244 } while (++i < nd0);
245 s++;
246 } else
247 s += 10;
248 for (; i < nd; i++)
ba379fdc 249 multadd(b, 10, *s++ - '0');
9dae56ea
A
250}
251
252static int hi0bits(uint32_t x)
253{
254 int k = 0;
255
256 if (!(x & 0xffff0000)) {
257 k = 16;
258 x <<= 16;
259 }
260 if (!(x & 0xff000000)) {
261 k += 8;
262 x <<= 8;
263 }
264 if (!(x & 0xf0000000)) {
265 k += 4;
266 x <<= 4;
267 }
268 if (!(x & 0xc0000000)) {
269 k += 2;
270 x <<= 2;
271 }
272 if (!(x & 0x80000000)) {
273 k++;
274 if (!(x & 0x40000000))
275 return 32;
276 }
277 return k;
278}
279
14957cd0 280static int lo0bits(uint32_t* y)
9dae56ea
A
281{
282 int k;
283 uint32_t x = *y;
284
285 if (x & 7) {
286 if (x & 1)
287 return 0;
288 if (x & 2) {
289 *y = x >> 1;
290 return 1;
291 }
292 *y = x >> 2;
293 return 2;
294 }
295 k = 0;
296 if (!(x & 0xffff)) {
297 k = 16;
298 x >>= 16;
299 }
300 if (!(x & 0xff)) {
301 k += 8;
302 x >>= 8;
303 }
304 if (!(x & 0xf)) {
305 k += 4;
306 x >>= 4;
307 }
308 if (!(x & 0x3)) {
309 k += 2;
310 x >>= 2;
311 }
312 if (!(x & 1)) {
313 k++;
314 x >>= 1;
14957cd0 315 if (!x)
9dae56ea
A
316 return 32;
317 }
318 *y = x;
319 return k;
320}
321
ba379fdc 322static void i2b(BigInt& b, int i)
9dae56ea 323{
ba379fdc
A
324 b.sign = 0;
325 b.resize(1);
326 b.words()[0] = i;
9dae56ea
A
327}
328
ba379fdc 329static void mult(BigInt& aRef, const BigInt& bRef)
9dae56ea 330{
ba379fdc
A
331 const BigInt* a = &aRef;
332 const BigInt* b = &bRef;
333 BigInt c;
334 int wa, wb, wc;
14957cd0
A
335 const uint32_t* x = 0;
336 const uint32_t* xa;
337 const uint32_t* xb;
338 const uint32_t* xae;
339 const uint32_t* xbe;
340 uint32_t* xc;
341 uint32_t* xc0;
9dae56ea
A
342 uint32_t y;
343#ifdef USE_LONG_LONG
344 unsigned long long carry, z;
345#else
346 uint32_t carry, z;
347#endif
348
ba379fdc
A
349 if (a->size() < b->size()) {
350 const BigInt* tmp = a;
9dae56ea 351 a = b;
ba379fdc 352 b = tmp;
9dae56ea 353 }
14957cd0 354
ba379fdc
A
355 wa = a->size();
356 wb = b->size();
9dae56ea 357 wc = wa + wb;
ba379fdc
A
358 c.resize(wc);
359
360 for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
361 *xc = 0;
362 xa = a->words();
9dae56ea 363 xae = xa + wa;
ba379fdc 364 xb = b->words();
9dae56ea 365 xbe = xb + wb;
ba379fdc 366 xc0 = c.words();
9dae56ea
A
367#ifdef USE_LONG_LONG
368 for (; xb < xbe; xc0++) {
369 if ((y = *xb++)) {
370 x = xa;
371 xc = xc0;
372 carry = 0;
373 do {
374 z = *x++ * (unsigned long long)y + *xc + carry;
375 carry = z >> 32;
376 *xc++ = (uint32_t)z & 0xffffffffUL;
377 } while (x < xae);
378 *xc = (uint32_t)carry;
379 }
380 }
381#else
9dae56ea
A
382 for (; xb < xbe; xb++, xc0++) {
383 if ((y = *xb & 0xffff)) {
384 x = xa;
385 xc = xc0;
386 carry = 0;
387 do {
388 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
389 carry = z >> 16;
390 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
391 carry = z2 >> 16;
14957cd0 392 xc = storeInc(xc, z2, z);
9dae56ea
A
393 } while (x < xae);
394 *xc = carry;
395 }
396 if ((y = *xb >> 16)) {
397 x = xa;
398 xc = xc0;
399 carry = 0;
400 uint32_t z2 = *xc;
401 do {
402 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
403 carry = z >> 16;
14957cd0 404 xc = storeInc(xc, z, z2);
9dae56ea
A
405 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
406 carry = z2 >> 16;
407 } while (x < xae);
408 *xc = z2;
409 }
410 }
9dae56ea 411#endif
ba379fdc
A
412 for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
413 c.resize(wc);
414 aRef = c;
9dae56ea
A
415}
416
14957cd0
A
417struct P5Node {
418 WTF_MAKE_NONCOPYABLE(P5Node); WTF_MAKE_FAST_ALLOCATED;
419public:
420 P5Node() { }
ba379fdc
A
421 BigInt val;
422 P5Node* next;
423};
14957cd0 424
ba379fdc 425static P5Node* p5s;
14957cd0 426static int p5sCount;
9dae56ea 427
ba379fdc 428static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
9dae56ea
A
429{
430 static int p05[3] = { 5, 25, 125 };
431
432 if (int i = k & 3)
ba379fdc 433 multadd(b, p05[i - 1], 0);
9dae56ea
A
434
435 if (!(k >>= 2))
ba379fdc 436 return;
9dae56ea 437
14957cd0 438#if ENABLE(WTF_MULTIPLE_THREADS)
9dae56ea
A
439 s_dtoaP5Mutex->lock();
440#endif
ba379fdc
A
441 P5Node* p5 = p5s;
442
9dae56ea
A
443 if (!p5) {
444 /* first time */
ba379fdc
A
445 p5 = new P5Node;
446 i2b(p5->val, 625);
447 p5->next = 0;
448 p5s = p5;
14957cd0 449 p5sCount = 1;
9dae56ea 450 }
ba379fdc 451
14957cd0
A
452 int p5sCountLocal = p5sCount;
453#if ENABLE(WTF_MULTIPLE_THREADS)
9dae56ea
A
454 s_dtoaP5Mutex->unlock();
455#endif
14957cd0 456 int p5sUsed = 0;
9dae56ea
A
457
458 for (;;) {
ba379fdc
A
459 if (k & 1)
460 mult(b, p5->val);
461
9dae56ea
A
462 if (!(k >>= 1))
463 break;
464
14957cd0
A
465 if (++p5sUsed == p5sCountLocal) {
466#if ENABLE(WTF_MULTIPLE_THREADS)
9dae56ea
A
467 s_dtoaP5Mutex->lock();
468#endif
14957cd0 469 if (p5sUsed == p5sCount) {
9dae56ea 470 ASSERT(!p5->next);
ba379fdc
A
471 p5->next = new P5Node;
472 p5->next->next = 0;
473 p5->next->val = p5->val;
474 mult(p5->next->val, p5->next->val);
14957cd0 475 ++p5sCount;
9dae56ea 476 }
14957cd0
A
477
478 p5sCountLocal = p5sCount;
479#if ENABLE(WTF_MULTIPLE_THREADS)
9dae56ea
A
480 s_dtoaP5Mutex->unlock();
481#endif
482 }
483 p5 = p5->next;
484 }
9dae56ea
A
485}
486
ba379fdc 487static ALWAYS_INLINE void lshift(BigInt& b, int k)
9dae56ea 488{
9dae56ea 489 int n = k >> 5;
9dae56ea 490
ba379fdc
A
491 int origSize = b.size();
492 int n1 = n + origSize + 1;
9dae56ea 493
ba379fdc
A
494 if (k &= 0x1f)
495 b.resize(b.size() + n + 1);
496 else
497 b.resize(b.size() + n);
498
499 const uint32_t* srcStart = b.words();
500 uint32_t* dstStart = b.words();
501 const uint32_t* src = srcStart + origSize - 1;
9dae56ea 502 uint32_t* dst = dstStart + n1 - 1;
ba379fdc 503 if (k) {
9dae56ea
A
504 uint32_t hiSubword = 0;
505 int s = 32 - k;
506 for (; src >= srcStart; --src) {
507 *dst-- = hiSubword | *src >> s;
508 hiSubword = *src << k;
509 }
510 *dst = hiSubword;
511 ASSERT(dst == dstStart + n);
ba379fdc 512
14957cd0 513 b.resize(origSize + n + !!b.words()[n1 - 1]);
9dae56ea 514 }
9dae56ea
A
515 else {
516 do {
517 *--dst = *src--;
518 } while (src >= srcStart);
9dae56ea
A
519 }
520 for (dst = dstStart + n; dst != dstStart; )
521 *--dst = 0;
522
ba379fdc 523 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
9dae56ea
A
524}
525
ba379fdc 526static int cmp(const BigInt& a, const BigInt& b)
9dae56ea 527{
ba379fdc 528 const uint32_t *xa, *xa0, *xb, *xb0;
9dae56ea
A
529 int i, j;
530
ba379fdc
A
531 i = a.size();
532 j = b.size();
533 ASSERT(i <= 1 || a.words()[i - 1]);
534 ASSERT(j <= 1 || b.words()[j - 1]);
9dae56ea
A
535 if (i -= j)
536 return i;
ba379fdc 537 xa0 = a.words();
9dae56ea 538 xa = xa0 + j;
ba379fdc 539 xb0 = b.words();
9dae56ea
A
540 xb = xb0 + j;
541 for (;;) {
542 if (*--xa != *--xb)
543 return *xa < *xb ? -1 : 1;
544 if (xa <= xa0)
545 break;
546 }
547 return 0;
548}
549
ba379fdc 550static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
9dae56ea 551{
ba379fdc
A
552 const BigInt* a = &aRef;
553 const BigInt* b = &bRef;
9dae56ea 554 int i, wa, wb;
14957cd0 555 uint32_t* xc;
9dae56ea 556
ba379fdc 557 i = cmp(*a, *b);
9dae56ea 558 if (!i) {
ba379fdc
A
559 c.sign = 0;
560 c.resize(1);
561 c.words()[0] = 0;
562 return;
9dae56ea
A
563 }
564 if (i < 0) {
ba379fdc 565 const BigInt* tmp = a;
9dae56ea 566 a = b;
ba379fdc 567 b = tmp;
9dae56ea
A
568 i = 1;
569 } else
570 i = 0;
ba379fdc
A
571
572 wa = a->size();
573 const uint32_t* xa = a->words();
574 const uint32_t* xae = xa + wa;
575 wb = b->size();
576 const uint32_t* xb = b->words();
577 const uint32_t* xbe = xb + wb;
578
579 c.resize(wa);
580 c.sign = i;
581 xc = c.words();
9dae56ea
A
582#ifdef USE_LONG_LONG
583 unsigned long long borrow = 0;
584 do {
585 unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
586 borrow = y >> 32 & (uint32_t)1;
587 *xc++ = (uint32_t)y & 0xffffffffUL;
588 } while (xb < xbe);
589 while (xa < xae) {
590 unsigned long long y = *xa++ - borrow;
591 borrow = y >> 32 & (uint32_t)1;
592 *xc++ = (uint32_t)y & 0xffffffffUL;
593 }
594#else
595 uint32_t borrow = 0;
9dae56ea
A
596 do {
597 uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
598 borrow = (y & 0x10000) >> 16;
599 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
600 borrow = (z & 0x10000) >> 16;
14957cd0 601 xc = storeInc(xc, z, y);
9dae56ea
A
602 } while (xb < xbe);
603 while (xa < xae) {
604 uint32_t y = (*xa & 0xffff) - borrow;
605 borrow = (y & 0x10000) >> 16;
606 uint32_t z = (*xa++ >> 16) - borrow;
607 borrow = (z & 0x10000) >> 16;
14957cd0 608 xc = storeInc(xc, z, y);
9dae56ea 609 }
9dae56ea
A
610#endif
611 while (!*--xc)
612 wa--;
ba379fdc 613 c.resize(wa);
9dae56ea
A
614}
615
ba379fdc 616static double ulp(U *x)
9dae56ea
A
617{
618 register int32_t L;
ba379fdc 619 U u;
9dae56ea
A
620
621 L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;
ba379fdc
A
622 word0(&u) = L;
623 word1(&u) = 0;
ba379fdc 624 return dval(&u);
9dae56ea
A
625}
626
ba379fdc 627static double b2d(const BigInt& a, int* e)
9dae56ea 628{
ba379fdc
A
629 const uint32_t* xa;
630 const uint32_t* xa0;
9dae56ea
A
631 uint32_t w;
632 uint32_t y;
633 uint32_t z;
634 int k;
ba379fdc 635 U d;
9dae56ea 636
ba379fdc
A
637#define d0 word0(&d)
638#define d1 word1(&d)
9dae56ea 639
ba379fdc
A
640 xa0 = a.words();
641 xa = xa0 + a.size();
9dae56ea
A
642 y = *--xa;
643 ASSERT(y);
644 k = hi0bits(y);
645 *e = 32 - k;
9dae56ea 646 if (k < Ebits) {
ba379fdc 647 d0 = Exp_1 | (y >> (Ebits - k));
9dae56ea 648 w = xa > xa0 ? *--xa : 0;
ba379fdc 649 d1 = (y << (32 - Ebits + k)) | (w >> (Ebits - k));
14957cd0 650 goto returnD;
9dae56ea
A
651 }
652 z = xa > xa0 ? *--xa : 0;
653 if (k -= Ebits) {
ba379fdc 654 d0 = Exp_1 | (y << k) | (z >> (32 - k));
9dae56ea 655 y = xa > xa0 ? *--xa : 0;
ba379fdc 656 d1 = (z << k) | (y >> (32 - k));
9dae56ea
A
657 } else {
658 d0 = Exp_1 | y;
659 d1 = z;
660 }
14957cd0 661returnD:
9dae56ea
A
662#undef d0
663#undef d1
ba379fdc 664 return dval(&d);
9dae56ea
A
665}
666
ba379fdc 667static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
9dae56ea 668{
9dae56ea 669 int de, k;
14957cd0
A
670 uint32_t* x;
671 uint32_t y, z;
9dae56ea 672 int i;
9dae56ea
A
673#define d0 word0(d)
674#define d1 word1(d)
675
ba379fdc 676 b.sign = 0;
ba379fdc 677 b.resize(1);
ba379fdc 678 x = b.words();
9dae56ea
A
679
680 z = d0 & Frac_mask;
681 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
9dae56ea
A
682 if ((de = (int)(d0 >> Exp_shift)))
683 z |= Exp_msk1;
9dae56ea
A
684 if ((y = d1)) {
685 if ((k = lo0bits(&y))) {
ba379fdc 686 x[0] = y | (z << (32 - k));
9dae56ea
A
687 z >>= k;
688 } else
689 x[0] = y;
14957cd0
A
690 if (z) {
691 b.resize(2);
692 x[1] = z;
693 }
ba379fdc 694
ba379fdc 695 i = b.size();
9dae56ea
A
696 } else {
697 k = lo0bits(&z);
698 x[0] = z;
ba379fdc 699 i = 1;
ba379fdc 700 b.resize(1);
9dae56ea
A
701 k += 32;
702 }
9dae56ea 703 if (de) {
9dae56ea
A
704 *e = de - Bias - (P - 1) + k;
705 *bits = P - k;
9dae56ea
A
706 } else {
707 *e = de - Bias - (P - 1) + 1 + k;
9dae56ea 708 *bits = (32 * i) - hi0bits(x[i - 1]);
9dae56ea 709 }
9dae56ea
A
710}
711#undef d0
712#undef d1
713
ba379fdc 714static double ratio(const BigInt& a, const BigInt& b)
9dae56ea 715{
ba379fdc 716 U da, db;
9dae56ea
A
717 int k, ka, kb;
718
ba379fdc
A
719 dval(&da) = b2d(a, &ka);
720 dval(&db) = b2d(b, &kb);
ba379fdc 721 k = ka - kb + 32 * (a.size() - b.size());
9dae56ea 722 if (k > 0)
ba379fdc 723 word0(&da) += k * Exp_msk1;
9dae56ea
A
724 else {
725 k = -k;
ba379fdc 726 word0(&db) += k * Exp_msk1;
9dae56ea 727 }
ba379fdc 728 return dval(&da) / dval(&db);
9dae56ea
A
729}
730
731static const double tens[] = {
14957cd0
A
732 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
733 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
734 1e20, 1e21, 1e22
9dae56ea
A
735};
736
737static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
738static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
14957cd0
A
739 9007199254740992. * 9007199254740992.e-256
740 /* = 2^106 * 1e-256 */
9dae56ea
A
741};
742
743/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
744/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
745#define Scale_Bit 0x10
746#define n_bigtens 5
747
9dae56ea
A
748double strtod(const char* s00, char** se)
749{
9dae56ea 750 int scale;
9dae56ea 751 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
14957cd0 752 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
9dae56ea 753 const char *s, *s0, *s1;
ba379fdc
A
754 double aadj, aadj1;
755 U aadj2, adj, rv, rv0;
9dae56ea
A
756 int32_t L;
757 uint32_t y, z;
ba379fdc 758 BigInt bb, bb1, bd, bd0, bs, delta;
9dae56ea
A
759
760 sign = nz0 = nz = 0;
ba379fdc 761 dval(&rv) = 0;
14957cd0 762 for (s = s00; ; s++) {
9dae56ea 763 switch (*s) {
14957cd0
A
764 case '-':
765 sign = 1;
766 /* no break */
767 case '+':
768 if (*++s)
9dae56ea 769 goto break2;
14957cd0
A
770 /* no break */
771 case 0:
772 goto ret0;
773 case '\t':
774 case '\n':
775 case '\v':
776 case '\f':
777 case '\r':
778 case ' ':
779 continue;
780 default:
781 goto break2;
9dae56ea 782 }
14957cd0 783 }
9dae56ea
A
784break2:
785 if (*s == '0') {
786 nz0 = 1;
787 while (*++s == '0') { }
788 if (!*s)
789 goto ret;
790 }
791 s0 = s;
792 y = z = 0;
793 for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
794 if (nd < 9)
795 y = (10 * y) + c - '0';
796 else if (nd < 16)
797 z = (10 * z) + c - '0';
798 nd0 = nd;
799 if (c == '.') {
800 c = *++s;
801 if (!nd) {
802 for (; c == '0'; c = *++s)
803 nz++;
804 if (c > '0' && c <= '9') {
805 s0 = s;
806 nf += nz;
807 nz = 0;
14957cd0 808 goto haveDig;
9dae56ea 809 }
14957cd0 810 goto digDone;
9dae56ea
A
811 }
812 for (; c >= '0' && c <= '9'; c = *++s) {
14957cd0 813haveDig:
9dae56ea
A
814 nz++;
815 if (c -= '0') {
816 nf += nz;
817 for (i = 1; i < nz; i++)
818 if (nd++ < 9)
819 y *= 10;
820 else if (nd <= DBL_DIG + 1)
821 z *= 10;
822 if (nd++ < 9)
823 y = (10 * y) + c;
824 else if (nd <= DBL_DIG + 1)
825 z = (10 * z) + c;
826 nz = 0;
827 }
828 }
829 }
14957cd0 830digDone:
9dae56ea
A
831 e = 0;
832 if (c == 'e' || c == 'E') {
14957cd0 833 if (!nd && !nz && !nz0)
9dae56ea 834 goto ret0;
9dae56ea
A
835 s00 = s;
836 esign = 0;
837 switch (c = *++s) {
14957cd0
A
838 case '-':
839 esign = 1;
840 case '+':
841 c = *++s;
9dae56ea
A
842 }
843 if (c >= '0' && c <= '9') {
844 while (c == '0')
845 c = *++s;
846 if (c > '0' && c <= '9') {
847 L = c - '0';
848 s1 = s;
849 while ((c = *++s) >= '0' && c <= '9')
850 L = (10 * L) + c - '0';
851 if (s - s1 > 8 || L > 19999)
852 /* Avoid confusion from exponents
853 * so large that e might overflow.
854 */
855 e = 19999; /* safe for 16 bit ints */
856 else
857 e = (int)L;
858 if (esign)
859 e = -e;
860 } else
861 e = 0;
862 } else
863 s = s00;
864 }
865 if (!nd) {
866 if (!nz && !nz0) {
9dae56ea
A
867ret0:
868 s = s00;
869 sign = 0;
870 }
871 goto ret;
872 }
873 e1 = e -= nf;
874
875 /* Now we have nd0 digits, starting at s0, followed by a
876 * decimal point, followed by nd-nd0 digits. The number we're
877 * after is the integer represented by those digits times
878 * 10**e */
879
880 if (!nd0)
881 nd0 = nd;
882 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
ba379fdc 883 dval(&rv) = y;
14957cd0 884 if (k > 9)
ba379fdc 885 dval(&rv) = tens[k - 9] * dval(&rv) + z;
14957cd0 886 if (nd <= DBL_DIG) {
9dae56ea
A
887 if (!e)
888 goto ret;
889 if (e > 0) {
890 if (e <= Ten_pmax) {
ba379fdc 891 /* rv = */ rounded_product(dval(&rv), tens[e]);
9dae56ea
A
892 goto ret;
893 }
894 i = DBL_DIG - nd;
895 if (e <= Ten_pmax + i) {
896 /* A fancier test would sometimes let us do
897 * this for larger i values.
898 */
899 e -= i;
ba379fdc
A
900 dval(&rv) *= tens[i];
901 /* rv = */ rounded_product(dval(&rv), tens[e]);
9dae56ea
A
902 goto ret;
903 }
14957cd0 904 } else if (e >= -Ten_pmax) {
ba379fdc 905 /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
9dae56ea
A
906 goto ret;
907 }
9dae56ea
A
908 }
909 e1 += nd - k;
910
9dae56ea 911 scale = 0;
9dae56ea
A
912
913 /* Get starting approximation = rv * 10**e1 */
914
915 if (e1 > 0) {
916 if ((i = e1 & 15))
ba379fdc 917 dval(&rv) *= tens[i];
9dae56ea
A
918 if (e1 &= ~15) {
919 if (e1 > DBL_MAX_10_EXP) {
920ovfl:
14957cd0 921#if HAVE(ERRNO_H)
9dae56ea
A
922 errno = ERANGE;
923#endif
924 /* Can't trust HUGE_VAL */
ba379fdc
A
925 word0(&rv) = Exp_mask;
926 word1(&rv) = 0;
9dae56ea
A
927 goto ret;
928 }
929 e1 >>= 4;
930 for (j = 0; e1 > 1; j++, e1 >>= 1)
931 if (e1 & 1)
ba379fdc 932 dval(&rv) *= bigtens[j];
9dae56ea 933 /* The last multiplication could overflow. */
ba379fdc
A
934 word0(&rv) -= P * Exp_msk1;
935 dval(&rv) *= bigtens[j];
936 if ((z = word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
9dae56ea
A
937 goto ovfl;
938 if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P)) {
939 /* set to largest number */
940 /* (Can't trust DBL_MAX) */
ba379fdc
A
941 word0(&rv) = Big0;
942 word1(&rv) = Big1;
9dae56ea 943 } else
ba379fdc 944 word0(&rv) += P * Exp_msk1;
9dae56ea
A
945 }
946 } else if (e1 < 0) {
947 e1 = -e1;
948 if ((i = e1 & 15))
ba379fdc 949 dval(&rv) /= tens[i];
9dae56ea
A
950 if (e1 >>= 4) {
951 if (e1 >= 1 << n_bigtens)
952 goto undfl;
9dae56ea
A
953 if (e1 & Scale_Bit)
954 scale = 2 * P;
955 for (j = 0; e1 > 0; j++, e1 >>= 1)
956 if (e1 & 1)
ba379fdc
A
957 dval(&rv) *= tinytens[j];
958 if (scale && (j = (2 * P) + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0) {
14957cd0 959 /* scaled rv is denormal; clear j low bits */
9dae56ea 960 if (j >= 32) {
ba379fdc 961 word1(&rv) = 0;
9dae56ea 962 if (j >= 53)
14957cd0 963 word0(&rv) = (P + 2) * Exp_msk1;
9dae56ea 964 else
14957cd0 965 word0(&rv) &= 0xffffffff << (j - 32);
9dae56ea 966 } else
ba379fdc 967 word1(&rv) &= 0xffffffff << j;
9dae56ea 968 }
ba379fdc 969 if (!dval(&rv)) {
9dae56ea 970undfl:
ba379fdc 971 dval(&rv) = 0.;
14957cd0 972#if HAVE(ERRNO_H)
9dae56ea
A
973 errno = ERANGE;
974#endif
9dae56ea
A
975 goto ret;
976 }
9dae56ea
A
977 }
978 }
979
980 /* Now the hard part -- adjusting rv to the correct value.*/
981
982 /* Put digits into bd: true value = bd * 10^e */
983
ba379fdc 984 s2b(bd0, s0, nd0, nd, y);
9dae56ea
A
985
986 for (;;) {
ba379fdc
A
987 bd = bd0;
988 d2b(bb, &rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
989 i2b(bs, 1);
9dae56ea
A
990
991 if (e >= 0) {
992 bb2 = bb5 = 0;
993 bd2 = bd5 = e;
994 } else {
995 bb2 = bb5 = -e;
996 bd2 = bd5 = 0;
997 }
998 if (bbe >= 0)
999 bb2 += bbe;
1000 else
1001 bd2 -= bbe;
1002 bs2 = bb2;
9dae56ea
A
1003 j = bbe - scale;
1004 i = j + bbbits - 1; /* logb(rv) */
1005 if (i < Emin) /* denormal */
1006 j += P - Emin;
1007 else
1008 j = P + 1 - bbbits;
9dae56ea
A
1009 bb2 += j;
1010 bd2 += j;
9dae56ea 1011 bd2 += scale;
9dae56ea
A
1012 i = bb2 < bd2 ? bb2 : bd2;
1013 if (i > bs2)
1014 i = bs2;
1015 if (i > 0) {
1016 bb2 -= i;
1017 bd2 -= i;
1018 bs2 -= i;
1019 }
1020 if (bb5 > 0) {
ba379fdc
A
1021 pow5mult(bs, bb5);
1022 mult(bb, bs);
9dae56ea
A
1023 }
1024 if (bb2 > 0)
ba379fdc 1025 lshift(bb, bb2);
9dae56ea 1026 if (bd5 > 0)
ba379fdc 1027 pow5mult(bd, bd5);
9dae56ea 1028 if (bd2 > 0)
ba379fdc 1029 lshift(bd, bd2);
9dae56ea 1030 if (bs2 > 0)
ba379fdc
A
1031 lshift(bs, bs2);
1032 diff(delta, bb, bd);
1033 dsign = delta.sign;
1034 delta.sign = 0;
9dae56ea
A
1035 i = cmp(delta, bs);
1036
1037 if (i < 0) {
1038 /* Error is less than half an ulp -- check for
1039 * special case of mantissa a power of two.
1040 */
ba379fdc 1041 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
ba379fdc 1042 || (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1
9dae56ea 1043 ) {
9dae56ea
A
1044 break;
1045 }
ba379fdc 1046 if (!delta.words()[0] && delta.size() <= 1) {
9dae56ea 1047 /* exact result */
9dae56ea
A
1048 break;
1049 }
ba379fdc 1050 lshift(delta, Log2P);
9dae56ea 1051 if (cmp(delta, bs) > 0)
14957cd0 1052 goto dropDown;
9dae56ea
A
1053 break;
1054 }
14957cd0 1055 if (!i) {
9dae56ea
A
1056 /* exactly half-way between */
1057 if (dsign) {
ba379fdc
A
1058 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1059 && word1(&rv) == (
ba379fdc 1060 (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
9dae56ea 1061 ? (0xffffffff & (0xffffffff << (2 * P + 1 - (y >> Exp_shift)))) :
9dae56ea
A
1062 0xffffffff)) {
1063 /*boundary case -- increment exponent*/
ba379fdc
A
1064 word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1;
1065 word1(&rv) = 0;
9dae56ea 1066 dsign = 0;
9dae56ea
A
1067 break;
1068 }
ba379fdc 1069 } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
14957cd0 1070dropDown:
9dae56ea 1071 /* boundary case -- decrement exponent */
9dae56ea 1072 if (scale) {
ba379fdc 1073 L = word0(&rv) & Exp_mask;
9dae56ea
A
1074 if (L <= (2 * P + 1) * Exp_msk1) {
1075 if (L > (P + 2) * Exp_msk1)
1076 /* round even ==> */
1077 /* accept rv */
1078 break;
1079 /* rv = smallest denormal */
1080 goto undfl;
1081 }
1082 }
ba379fdc 1083 L = (word0(&rv) & Exp_mask) - Exp_msk1;
ba379fdc
A
1084 word0(&rv) = L | Bndry_mask1;
1085 word1(&rv) = 0xffffffff;
9dae56ea
A
1086 break;
1087 }
ba379fdc 1088 if (!(word1(&rv) & LSB))
9dae56ea
A
1089 break;
1090 if (dsign)
ba379fdc 1091 dval(&rv) += ulp(&rv);
9dae56ea 1092 else {
ba379fdc 1093 dval(&rv) -= ulp(&rv);
ba379fdc 1094 if (!dval(&rv))
9dae56ea 1095 goto undfl;
9dae56ea 1096 }
9dae56ea 1097 dsign = 1 - dsign;
9dae56ea
A
1098 break;
1099 }
1100 if ((aadj = ratio(delta, bs)) <= 2.) {
1101 if (dsign)
1102 aadj = aadj1 = 1.;
ba379fdc 1103 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
ba379fdc 1104 if (word1(&rv) == Tiny1 && !word0(&rv))
9dae56ea 1105 goto undfl;
9dae56ea
A
1106 aadj = 1.;
1107 aadj1 = -1.;
1108 } else {
1109 /* special case -- power of FLT_RADIX to be */
1110 /* rounded down... */
1111
1112 if (aadj < 2. / FLT_RADIX)
1113 aadj = 1. / FLT_RADIX;
1114 else
1115 aadj *= 0.5;
1116 aadj1 = -aadj;
1117 }
1118 } else {
1119 aadj *= 0.5;
1120 aadj1 = dsign ? aadj : -aadj;
9dae56ea 1121 }
ba379fdc 1122 y = word0(&rv) & Exp_mask;
9dae56ea
A
1123
1124 /* Check for overflow */
1125
1126 if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) {
ba379fdc
A
1127 dval(&rv0) = dval(&rv);
1128 word0(&rv) -= P * Exp_msk1;
1129 adj.d = aadj1 * ulp(&rv);
1130 dval(&rv) += adj.d;
1131 if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P)) {
1132 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
9dae56ea 1133 goto ovfl;
ba379fdc
A
1134 word0(&rv) = Big0;
1135 word1(&rv) = Big1;
9dae56ea 1136 goto cont;
14957cd0
A
1137 }
1138 word0(&rv) += P * Exp_msk1;
9dae56ea 1139 } else {
9dae56ea
A
1140 if (scale && y <= 2 * P * Exp_msk1) {
1141 if (aadj <= 0x7fffffff) {
1142 if ((z = (uint32_t)aadj) <= 0)
1143 z = 1;
1144 aadj = z;
1145 aadj1 = dsign ? aadj : -aadj;
1146 }
ba379fdc
A
1147 dval(&aadj2) = aadj1;
1148 word0(&aadj2) += (2 * P + 1) * Exp_msk1 - y;
1149 aadj1 = dval(&aadj2);
9dae56ea 1150 }
ba379fdc
A
1151 adj.d = aadj1 * ulp(&rv);
1152 dval(&rv) += adj.d;
9dae56ea 1153 }
ba379fdc 1154 z = word0(&rv) & Exp_mask;
14957cd0 1155 if (!scale && y == z) {
9dae56ea
A
1156 /* Can we stop now? */
1157 L = (int32_t)aadj;
1158 aadj -= L;
1159 /* The tolerances below are conservative. */
ba379fdc 1160 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
9dae56ea
A
1161 if (aadj < .4999999 || aadj > .5000001)
1162 break;
1163 } else if (aadj < .4999999 / FLT_RADIX)
1164 break;
1165 }
9dae56ea 1166cont:
14957cd0
A
1167 {}
1168 }
9dae56ea 1169 if (scale) {
ba379fdc
A
1170 word0(&rv0) = Exp_1 - 2 * P * Exp_msk1;
1171 word1(&rv0) = 0;
1172 dval(&rv) *= dval(&rv0);
14957cd0 1173#if HAVE(ERRNO_H)
9dae56ea 1174 /* try to avoid the bug of testing an 8087 register value */
14957cd0 1175 if (!word0(&rv) && !word1(&rv))
9dae56ea
A
1176 errno = ERANGE;
1177#endif
1178 }
9dae56ea
A
1179ret:
1180 if (se)
1181 *se = const_cast<char*>(s);
ba379fdc 1182 return sign ? -dval(&rv) : dval(&rv);
9dae56ea
A
1183}
1184
ba379fdc 1185static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
9dae56ea 1186{
ba379fdc 1187 size_t n;
14957cd0
A
1188 uint32_t* bx;
1189 uint32_t* bxe;
1190 uint32_t q;
1191 uint32_t* sx;
1192 uint32_t* sxe;
9dae56ea
A
1193#ifdef USE_LONG_LONG
1194 unsigned long long borrow, carry, y, ys;
1195#else
1196 uint32_t borrow, carry, y, ys;
9dae56ea 1197 uint32_t si, z, zs;
9dae56ea 1198#endif
ba379fdc
A
1199 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
1200 ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
9dae56ea 1201
ba379fdc
A
1202 n = S.size();
1203 ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
1204 if (b.size() < n)
9dae56ea 1205 return 0;
ba379fdc 1206 sx = S.words();
9dae56ea 1207 sxe = sx + --n;
ba379fdc 1208 bx = b.words();
9dae56ea
A
1209 bxe = bx + n;
1210 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1211 ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
1212 if (q) {
1213 borrow = 0;
1214 carry = 0;
1215 do {
1216#ifdef USE_LONG_LONG
1217 ys = *sx++ * (unsigned long long)q + carry;
1218 carry = ys >> 32;
1219 y = *bx - (ys & 0xffffffffUL) - borrow;
1220 borrow = y >> 32 & (uint32_t)1;
1221 *bx++ = (uint32_t)y & 0xffffffffUL;
1222#else
9dae56ea
A
1223 si = *sx++;
1224 ys = (si & 0xffff) * q + carry;
1225 zs = (si >> 16) * q + (ys >> 16);
1226 carry = zs >> 16;
1227 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1228 borrow = (y & 0x10000) >> 16;
1229 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1230 borrow = (z & 0x10000) >> 16;
14957cd0 1231 bx = storeInc(bx, z, y);
9dae56ea
A
1232#endif
1233 } while (sx <= sxe);
1234 if (!*bxe) {
ba379fdc 1235 bx = b.words();
9dae56ea
A
1236 while (--bxe > bx && !*bxe)
1237 --n;
ba379fdc 1238 b.resize(n);
9dae56ea
A
1239 }
1240 }
1241 if (cmp(b, S) >= 0) {
1242 q++;
1243 borrow = 0;
1244 carry = 0;
ba379fdc
A
1245 bx = b.words();
1246 sx = S.words();
9dae56ea
A
1247 do {
1248#ifdef USE_LONG_LONG
1249 ys = *sx++ + carry;
1250 carry = ys >> 32;
1251 y = *bx - (ys & 0xffffffffUL) - borrow;
1252 borrow = y >> 32 & (uint32_t)1;
1253 *bx++ = (uint32_t)y & 0xffffffffUL;
1254#else
9dae56ea
A
1255 si = *sx++;
1256 ys = (si & 0xffff) + carry;
1257 zs = (si >> 16) + (ys >> 16);
1258 carry = zs >> 16;
1259 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
1260 borrow = (y & 0x10000) >> 16;
1261 z = (*bx >> 16) - (zs & 0xffff) - borrow;
1262 borrow = (z & 0x10000) >> 16;
14957cd0 1263 bx = storeInc(bx, z, y);
9dae56ea
A
1264#endif
1265 } while (sx <= sxe);
ba379fdc 1266 bx = b.words();
9dae56ea
A
1267 bxe = bx + n;
1268 if (!*bxe) {
1269 while (--bxe > bx && !*bxe)
1270 --n;
ba379fdc 1271 b.resize(n);
9dae56ea
A
1272 }
1273 }
1274 return q;
1275}
1276
9dae56ea
A
1277/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1278 *
1279 * Inspired by "How to Print Floating-Point Numbers Accurately" by
14957cd0 1280 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
9dae56ea
A
1281 *
1282 * Modifications:
1283 * 1. Rather than iterating, we use a simple numeric overestimate
1284 * to determine k = floor(log10(d)). We scale relevant
1285 * quantities using O(log2(k)) rather than O(k) multiplications.
1286 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1287 * try to generate digits strictly left to right. Instead, we
1288 * compute with fewer bits and propagate the carry if necessary
1289 * when rounding the final digit up. This is often faster.
1290 * 3. Under the assumption that input will be rounded nearest,
1291 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1292 * That is, we allow equality in stopping tests when the
1293 * round-nearest rule will give the same floating-point value
1294 * as would satisfaction of the stopping test with strict
1295 * inequality.
1296 * 4. We remove common factors of powers of 2 from relevant
1297 * quantities.
1298 * 5. When converting floating-point integers less than 1e16,
1299 * we use floating-point arithmetic rather than resorting
1300 * to multiple-precision integers.
1301 * 6. When asked to produce fewer than 15 digits, we first try
1302 * to get by with floating-point arithmetic; we resort to
1303 * multiple-precision integer arithmetic only if we cannot
1304 * guarantee that the floating-point calculation has given
1305 * the correctly rounded result. For k requested digits and
1306 * "uniformly" distributed input, the probability is
1307 * something like 10^(k-15) that we must resort to the int32_t
1308 * calculation.
14957cd0
A
1309 *
1310 * Note: 'leftright' translates to 'generate shortest possible string'.
9dae56ea 1311 */
14957cd0
A
1312template<bool roundingNone, bool roundingSignificantFigures, bool roundingDecimalPlaces, bool leftright>
1313void dtoa(DtoaBuffer result, double dd, int ndigits, bool& signOut, int& exponentOut, unsigned& precisionOut)
9dae56ea 1314{
14957cd0
A
1315 // Exactly one rounding mode must be specified.
1316 ASSERT(roundingNone + roundingSignificantFigures + roundingDecimalPlaces == 1);
1317 // roundingNone only allowed (only sensible?) with leftright set.
1318 ASSERT(!roundingNone || leftright);
9dae56ea 1319
14957cd0 1320 ASSERT(!isnan(dd) && !isinf(dd));
9dae56ea
A
1321
1322 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
14957cd0
A
1323 j, j1, k, k0, k_check, m2, m5, s2, s5,
1324 spec_case;
9dae56ea 1325 int32_t L;
9dae56ea
A
1326 int denorm;
1327 uint32_t x;
14957cd0 1328 BigInt b, delta, mlo, mhi, S;
ba379fdc
A
1329 U d2, eps, u;
1330 double ds;
14957cd0
A
1331 char* s;
1332 char* s0;
9dae56ea 1333
ba379fdc 1334 u.d = dd;
9dae56ea 1335
14957cd0
A
1336 /* Infinity or NaN */
1337 ASSERT((word0(&u) & Exp_mask) != Exp_mask);
1338
1339 // JavaScript toString conversion treats -0 as 0.
ba379fdc 1340 if (!dval(&u)) {
14957cd0
A
1341 signOut = false;
1342 exponentOut = 0;
1343 precisionOut = 1;
ba379fdc
A
1344 result[0] = '0';
1345 result[1] = '\0';
1346 return;
9dae56ea
A
1347 }
1348
14957cd0
A
1349 if (word0(&u) & Sign_bit) {
1350 signOut = true;
1351 word0(&u) &= ~Sign_bit; // clear sign bit
1352 } else
1353 signOut = false;
9dae56ea 1354
ba379fdc 1355 d2b(b, &u, &be, &bbits);
ba379fdc 1356 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
ba379fdc
A
1357 dval(&d2) = dval(&u);
1358 word0(&d2) &= Frac_mask1;
1359 word0(&d2) |= Exp_11;
9dae56ea
A
1360
1361 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1362 * log10(x) = log(x) / log(10)
1363 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1364 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1365 *
1366 * This suggests computing an approximation k to log10(d) by
1367 *
1368 * k = (i - Bias)*0.301029995663981
1369 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1370 *
1371 * We want k to be too large rather than too small.
1372 * The error in the first-order Taylor series approximation
1373 * is in our favor, so we just round up the constant enough
1374 * to compensate for any error in the multiplication of
1375 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1376 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1377 * adding 1e-13 to the constant term more than suffices.
1378 * Hence we adjust the constant term to 0.1760912590558.
1379 * (We could get a more accurate k by invoking log10,
1380 * but this is probably not worthwhile.)
1381 */
1382
1383 i -= Bias;
9dae56ea
A
1384 denorm = 0;
1385 } else {
1386 /* d is denormalized */
1387
1388 i = bbits + be + (Bias + (P - 1) - 1);
ba379fdc
A
1389 x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
1390 : word1(&u) << (32 - i);
1391 dval(&d2) = x;
1392 word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
9dae56ea
A
1393 i -= (Bias + (P - 1) - 1) + 1;
1394 denorm = 1;
1395 }
ba379fdc 1396 ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
9dae56ea
A
1397 k = (int)ds;
1398 if (ds < 0. && ds != k)
1399 k--; /* want k = floor(ds) */
1400 k_check = 1;
1401 if (k >= 0 && k <= Ten_pmax) {
ba379fdc 1402 if (dval(&u) < tens[k])
9dae56ea
A
1403 k--;
1404 k_check = 0;
1405 }
1406 j = bbits - i - 1;
1407 if (j >= 0) {
1408 b2 = 0;
1409 s2 = j;
1410 } else {
1411 b2 = -j;
1412 s2 = 0;
1413 }
1414 if (k >= 0) {
1415 b5 = 0;
1416 s5 = k;
1417 s2 += k;
1418 } else {
1419 b2 -= k;
1420 b5 = -k;
1421 s5 = 0;
1422 }
1423
14957cd0
A
1424 if (roundingNone) {
1425 ilim = ilim1 = -1;
1426 i = 18;
1427 ndigits = 0;
1428 }
1429 if (roundingSignificantFigures) {
1430 if (ndigits <= 0)
1431 ndigits = 1;
1432 ilim = ilim1 = i = ndigits;
1433 }
1434 if (roundingDecimalPlaces) {
1435 i = ndigits + k + 1;
1436 ilim = i;
1437 ilim1 = i - 1;
1438 if (i <= 0)
1439 i = 1;
1440 }
9dae56ea 1441
ba379fdc 1442 s = s0 = result;
9dae56ea 1443
14957cd0 1444 if (ilim >= 0 && ilim <= Quick_max) {
9dae56ea
A
1445 /* Try to get by with floating-point arithmetic. */
1446
1447 i = 0;
ba379fdc 1448 dval(&d2) = dval(&u);
9dae56ea
A
1449 k0 = k;
1450 ilim0 = ilim;
1451 ieps = 2; /* conservative */
1452 if (k > 0) {
1453 ds = tens[k & 0xf];
1454 j = k >> 4;
1455 if (j & Bletch) {
1456 /* prevent overflows */
1457 j &= Bletch - 1;
ba379fdc 1458 dval(&u) /= bigtens[n_bigtens - 1];
9dae56ea
A
1459 ieps++;
1460 }
1461 for (; j; j >>= 1, i++) {
1462 if (j & 1) {
1463 ieps++;
1464 ds *= bigtens[i];
1465 }
1466 }
ba379fdc 1467 dval(&u) /= ds;
9dae56ea 1468 } else if ((j1 = -k)) {
ba379fdc 1469 dval(&u) *= tens[j1 & 0xf];
9dae56ea
A
1470 for (j = j1 >> 4; j; j >>= 1, i++) {
1471 if (j & 1) {
1472 ieps++;
ba379fdc 1473 dval(&u) *= bigtens[i];
9dae56ea
A
1474 }
1475 }
1476 }
ba379fdc 1477 if (k_check && dval(&u) < 1. && ilim > 0) {
9dae56ea 1478 if (ilim1 <= 0)
14957cd0 1479 goto fastFailed;
9dae56ea
A
1480 ilim = ilim1;
1481 k--;
ba379fdc 1482 dval(&u) *= 10.;
9dae56ea
A
1483 ieps++;
1484 }
ba379fdc
A
1485 dval(&eps) = (ieps * dval(&u)) + 7.;
1486 word0(&eps) -= (P - 1) * Exp_msk1;
14957cd0 1487 if (!ilim) {
ba379fdc
A
1488 S.clear();
1489 mhi.clear();
1490 dval(&u) -= 5.;
1491 if (dval(&u) > dval(&eps))
14957cd0 1492 goto oneDigit;
ba379fdc 1493 if (dval(&u) < -dval(&eps))
14957cd0
A
1494 goto noDigits;
1495 goto fastFailed;
9dae56ea 1496 }
9dae56ea
A
1497 if (leftright) {
1498 /* Use Steele & White method of only
1499 * generating digits needed.
1500 */
ba379fdc 1501 dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
9dae56ea 1502 for (i = 0;;) {
ba379fdc
A
1503 L = (long int)dval(&u);
1504 dval(&u) -= L;
9dae56ea 1505 *s++ = '0' + (int)L;
ba379fdc
A
1506 if (dval(&u) < dval(&eps))
1507 goto ret;
1508 if (1. - dval(&u) < dval(&eps))
14957cd0 1509 goto bumpUp;
9dae56ea
A
1510 if (++i >= ilim)
1511 break;
ba379fdc
A
1512 dval(&eps) *= 10.;
1513 dval(&u) *= 10.;
9dae56ea
A
1514 }
1515 } else {
9dae56ea 1516 /* Generate ilim digits, then fix them up. */
ba379fdc
A
1517 dval(&eps) *= tens[ilim - 1];
1518 for (i = 1;; i++, dval(&u) *= 10.) {
1519 L = (int32_t)(dval(&u));
1520 if (!(dval(&u) -= L))
9dae56ea
A
1521 ilim = i;
1522 *s++ = '0' + (int)L;
1523 if (i == ilim) {
ba379fdc 1524 if (dval(&u) > 0.5 + dval(&eps))
14957cd0
A
1525 goto bumpUp;
1526 if (dval(&u) < 0.5 - dval(&eps)) {
9dae56ea
A
1527 while (*--s == '0') { }
1528 s++;
ba379fdc 1529 goto ret;
9dae56ea
A
1530 }
1531 break;
1532 }
1533 }
9dae56ea 1534 }
14957cd0 1535fastFailed:
9dae56ea 1536 s = s0;
ba379fdc 1537 dval(&u) = dval(&d2);
9dae56ea
A
1538 k = k0;
1539 ilim = ilim0;
1540 }
1541
1542 /* Do we have a "small" integer? */
1543
1544 if (be >= 0 && k <= Int_max) {
1545 /* Yes. */
1546 ds = tens[k];
1547 if (ndigits < 0 && ilim <= 0) {
ba379fdc
A
1548 S.clear();
1549 mhi.clear();
1550 if (ilim < 0 || dval(&u) <= 5 * ds)
14957cd0
A
1551 goto noDigits;
1552 goto oneDigit;
9dae56ea 1553 }
ba379fdc
A
1554 for (i = 1;; i++, dval(&u) *= 10.) {
1555 L = (int32_t)(dval(&u) / ds);
1556 dval(&u) -= L * ds;
9dae56ea 1557 *s++ = '0' + (int)L;
ba379fdc 1558 if (!dval(&u)) {
9dae56ea
A
1559 break;
1560 }
1561 if (i == ilim) {
ba379fdc
A
1562 dval(&u) += dval(&u);
1563 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
14957cd0 1564bumpUp:
9dae56ea
A
1565 while (*--s == '9')
1566 if (s == s0) {
1567 k++;
1568 *s = '0';
1569 break;
1570 }
1571 ++*s++;
1572 }
1573 break;
1574 }
1575 }
ba379fdc 1576 goto ret;
9dae56ea
A
1577 }
1578
1579 m2 = b2;
1580 m5 = b5;
ba379fdc
A
1581 mhi.clear();
1582 mlo.clear();
9dae56ea 1583 if (leftright) {
14957cd0 1584 i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits;
9dae56ea
A
1585 b2 += i;
1586 s2 += i;
ba379fdc 1587 i2b(mhi, 1);
9dae56ea
A
1588 }
1589 if (m2 > 0 && s2 > 0) {
1590 i = m2 < s2 ? m2 : s2;
1591 b2 -= i;
1592 m2 -= i;
1593 s2 -= i;
1594 }
1595 if (b5 > 0) {
1596 if (leftright) {
1597 if (m5 > 0) {
ba379fdc
A
1598 pow5mult(mhi, m5);
1599 mult(b, mhi);
9dae56ea
A
1600 }
1601 if ((j = b5 - m5))
ba379fdc 1602 pow5mult(b, j);
9dae56ea 1603 } else
ba379fdc 1604 pow5mult(b, b5);
14957cd0 1605 }
ba379fdc 1606 i2b(S, 1);
9dae56ea 1607 if (s5 > 0)
ba379fdc 1608 pow5mult(S, s5);
9dae56ea
A
1609
1610 /* Check for special case that d is a normalized power of 2. */
1611
1612 spec_case = 0;
14957cd0 1613 if ((roundingNone || leftright) && (!word1(&u) && !(word0(&u) & Bndry_mask) && word0(&u) & (Exp_mask & ~Exp_msk1))) {
9dae56ea
A
1614 /* The special case */
1615 b2 += Log2P;
1616 s2 += Log2P;
1617 spec_case = 1;
1618 }
1619
1620 /* Arrange for convenient computation of quotients:
1621 * shift left if necessary so divisor has 4 leading 0 bits.
1622 *
1623 * Perhaps we should just compute leading 28 bits of S once
1624 * and for all and pass them and a shift to quorem, so it
1625 * can do shifts and ors to compute the numerator for q.
1626 */
ba379fdc 1627 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
9dae56ea 1628 i = 32 - i;
9dae56ea
A
1629 if (i > 4) {
1630 i -= 4;
1631 b2 += i;
1632 m2 += i;
1633 s2 += i;
1634 } else if (i < 4) {
1635 i += 28;
1636 b2 += i;
1637 m2 += i;
1638 s2 += i;
1639 }
1640 if (b2 > 0)
ba379fdc 1641 lshift(b, b2);
9dae56ea 1642 if (s2 > 0)
ba379fdc 1643 lshift(S, s2);
9dae56ea 1644 if (k_check) {
14957cd0 1645 if (cmp(b, S) < 0) {
9dae56ea 1646 k--;
ba379fdc 1647 multadd(b, 10, 0); /* we botched the k estimate */
9dae56ea 1648 if (leftright)
ba379fdc 1649 multadd(mhi, 10, 0);
9dae56ea
A
1650 ilim = ilim1;
1651 }
1652 }
14957cd0
A
1653 if (ilim <= 0 && roundingDecimalPlaces) {
1654 if (ilim < 0)
1655 goto noDigits;
1656 multadd(S, 5, 0);
1657 // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero.
1658 if (cmp(b, S) < 0)
1659 goto noDigits;
1660 goto oneDigit;
1661 }
9dae56ea
A
1662 if (leftright) {
1663 if (m2 > 0)
ba379fdc 1664 lshift(mhi, m2);
9dae56ea
A
1665
1666 /* Compute mlo -- check for special case
1667 * that d is a normalized power of 2.
1668 */
1669
1670 mlo = mhi;
14957cd0 1671 if (spec_case)
ba379fdc 1672 lshift(mhi, Log2P);
9dae56ea
A
1673
1674 for (i = 1;;i++) {
14957cd0 1675 dig = quorem(b, S) + '0';
9dae56ea
A
1676 /* Do we yet have the shortest decimal string
1677 * that will round to d?
1678 */
1679 j = cmp(b, mlo);
ba379fdc
A
1680 diff(delta, S, mhi);
1681 j1 = delta.sign ? 1 : cmp(b, delta);
14957cd0
A
1682#ifdef DTOA_ROUND_BIASED
1683 if (j < 0 || !j) {
1684#else
1685 // FIXME: ECMA-262 specifies that equidistant results round away from
1686 // zero, which probably means we shouldn't be on the unbiased code path
1687 // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't
1688 // yet understood this code well enough to make the call, but we should
1689 // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner
1690 // case to understand is probably "Math.pow(0.5, 24).toString()".
1691 // I believe this value is interesting because I think it is precisely
1692 // representable in binary floating point, and its decimal representation
1693 // has a single digit that Steele & White reduction can remove, with the
1694 // value 5 (thus equidistant from the next numbers above and below).
1695 // We produce the correct answer using either codepath, and I don't as
1696 // yet understand why. :-)
1697 if (!j1 && !(word1(&u) & 1)) {
9dae56ea 1698 if (dig == '9')
14957cd0 1699 goto round9up;
9dae56ea
A
1700 if (j > 0)
1701 dig++;
9dae56ea
A
1702 *s++ = dig;
1703 goto ret;
1704 }
14957cd0 1705 if (j < 0 || (!j && !(word1(&u) & 1))) {
9dae56ea 1706#endif
14957cd0 1707 if ((b.words()[0] || b.size() > 1) && (j1 > 0)) {
ba379fdc 1708 lshift(b, 1);
9dae56ea 1709 j1 = cmp(b, S);
14957cd0
A
1710 // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))),
1711 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1712 // be rounded away from zero.
1713 if (j1 >= 0) {
1714 if (dig == '9')
1715 goto round9up;
1716 dig++;
1717 }
9dae56ea 1718 }
9dae56ea
A
1719 *s++ = dig;
1720 goto ret;
1721 }
1722 if (j1 > 0) {
1723 if (dig == '9') { /* possible if i == 1 */
14957cd0 1724round9up:
9dae56ea
A
1725 *s++ = '9';
1726 goto roundoff;
1727 }
1728 *s++ = dig + 1;
1729 goto ret;
1730 }
1731 *s++ = dig;
1732 if (i == ilim)
1733 break;
ba379fdc
A
1734 multadd(b, 10, 0);
1735 multadd(mlo, 10, 0);
1736 multadd(mhi, 10, 0);
9dae56ea 1737 }
14957cd0 1738 } else {
9dae56ea 1739 for (i = 1;; i++) {
14957cd0
A
1740 *s++ = dig = quorem(b, S) + '0';
1741 if (!b.words()[0] && b.size() <= 1)
9dae56ea 1742 goto ret;
9dae56ea
A
1743 if (i >= ilim)
1744 break;
ba379fdc 1745 multadd(b, 10, 0);
9dae56ea 1746 }
14957cd0 1747 }
9dae56ea
A
1748
1749 /* Round off last digit */
1750
ba379fdc 1751 lshift(b, 1);
9dae56ea 1752 j = cmp(b, S);
14957cd0
A
1753 // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))),
1754 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1755 // be rounded away from zero.
1756 if (j >= 0) {
9dae56ea
A
1757roundoff:
1758 while (*--s == '9')
1759 if (s == s0) {
1760 k++;
1761 *s++ = '1';
1762 goto ret;
1763 }
1764 ++*s++;
1765 } else {
1766 while (*--s == '0') { }
1767 s++;
1768 }
1769 goto ret;
14957cd0
A
1770noDigits:
1771 exponentOut = 0;
1772 precisionOut = 1;
1773 result[0] = '0';
1774 result[1] = '\0';
1775 return;
1776oneDigit:
9dae56ea
A
1777 *s++ = '1';
1778 k++;
1779 goto ret;
1780ret:
14957cd0 1781 ASSERT(s > result);
9dae56ea 1782 *s = 0;
14957cd0
A
1783 exponentOut = k;
1784 precisionOut = s - result;
9dae56ea
A
1785}
1786
14957cd0 1787void dtoa(DtoaBuffer result, double dd, bool& sign, int& exponent, unsigned& precision)
f9bf01c6 1788{
14957cd0
A
1789 // flags are roundingNone, leftright.
1790 dtoa<true, false, false, true>(result, dd, 0, sign, exponent, precision);
f9bf01c6
A
1791}
1792
14957cd0 1793void dtoaRoundSF(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
f9bf01c6 1794{
14957cd0
A
1795 // flag is roundingSignificantFigures.
1796 dtoa<false, true, false, false>(result, dd, ndigits, sign, exponent, precision);
1797}
f9bf01c6 1798
14957cd0
A
1799void dtoaRoundDP(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
1800{
1801 // flag is roundingDecimalPlaces.
1802 dtoa<false, false, true, false>(result, dd, ndigits, sign, exponent, precision);
1803}
f9bf01c6 1804
14957cd0
A
1805static ALWAYS_INLINE void copyAsciiToUTF16(UChar* next, const char* src, unsigned size)
1806{
1807 for (unsigned i = 0; i < size; ++i)
1808 *next++ = *src++;
1809}
f9bf01c6 1810
14957cd0
A
1811unsigned numberToString(double d, NumberToStringBuffer buffer)
1812{
1813 // Handle NaN and Infinity.
1814 if (isnan(d) || isinf(d)) {
1815 if (isnan(d)) {
1816 copyAsciiToUTF16(buffer, "NaN", 3);
1817 return 3;
f9bf01c6 1818 }
14957cd0
A
1819 if (d > 0) {
1820 copyAsciiToUTF16(buffer, "Infinity", 8);
1821 return 8;
f9bf01c6 1822 }
14957cd0
A
1823 copyAsciiToUTF16(buffer, "-Infinity", 9);
1824 return 9;
1825 }
f9bf01c6 1826
14957cd0
A
1827 // Convert to decimal with rounding.
1828 DecimalNumber number(d);
1829 return number.exponent() >= -6 && number.exponent() < 21
1830 ? number.toStringDecimal(buffer, NumberToStringBufferLength)
1831 : number.toStringExponential(buffer, NumberToStringBufferLength);
f9bf01c6
A
1832}
1833
9dae56ea 1834} // namespace WTF