source: webkit/trunk/JavaScriptCore/wtf/dtoa.cpp@ 65104

Last change on this file since 65104 was 64706, checked in by [email protected], 15 years ago

JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=43461
Invalid NaN parsing

Reviewed by Oliver Hunt and Beth Dakin.

  • wtf/dtoa.cpp: Turn off the dtoa feature that allows you to specify a

non-standard NaN representation, since our NaN encoding assumes that all
true NaNs have the standard bit pattern.

  • API/JSValueRef.cpp:

(JSValueMakeNumber): Don't allow an API client to accidentally specify
a non-standard NaN either.

LayoutTests: https://bugs.webkit.org/show_bug.cgi?id=43461
Crash parsing certain values for NaN

Reviewed by Oliver Hunt and Beth Dakin.

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