1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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.
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.
19 ***************************************************************/
21 /* Please send bug reports to
23 Bell Laboratories, Room 2C-463
25 Murray Hill, NJ 07974-0636
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
41 /* strtod for IEEE-arithmetic machines.
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).
48 * Inspired loosely by William D. Clinger's paper "How to Read Floating
49 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
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
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
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.
148 #include <wtf/AlwaysInline.h>
149 #include <wtf/Assertions.h>
150 #include <wtf/FastMalloc.h>
151 #include <wtf/Vector.h>
152 #include <wtf/Threading.h>
157 #pragma warning(disable: 4244)
158 #pragma warning(disable: 4245)
159 #pragma warning(disable: 4554)
162 #if PLATFORM(BIG_ENDIAN)
164 #elif PLATFORM(MIDDLE_ENDIAN)
172 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) != 1
173 Exactly one of IEEE_8087
, IEEE_ARM
or IEEE_MC68k should be defined
.
178 #if ENABLE(JSC_MULTIPLE_THREADS)
179 Mutex
* s_dtoaP5Mutex
;
182 typedef union { double d
; uint32_t L
[2]; } U
;
187 #define word0(x) ((uint32_t*)&x)[1]
188 #define word1(x) ((uint32_t*)&x)[0]
190 #define word0(x) ((uint32_t*)&x)[0]
191 #define word1(x) ((uint32_t*)&x)[1]
195 #define word0(x) (x)->L[1]
196 #define word1(x) (x)->L[0]
198 #define word0(x) (x)->L[0]
199 #define word1(x) (x)->L[1]
201 #define dval(x) (x)->d
204 /* The following definition of Storeinc is appropriate for MIPS processors.
205 * An alternative that might be better on some machines is
206 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
208 #if defined(IEEE_8087) || defined(IEEE_ARM)
209 #define Storeinc(a,b,c) (((unsigned short*)a)[1] = (unsigned short)b, ((unsigned short*)a)[0] = (unsigned short)c, a++)
211 #define Storeinc(a,b,c) (((unsigned short*)a)[0] = (unsigned short)b, ((unsigned short*)a)[1] = (unsigned short)c, a++)
215 #define Exp_shift1 20
216 #define Exp_msk1 0x100000
217 #define Exp_msk11 0x100000
218 #define Exp_mask 0x7ff00000
222 #define Exp_1 0x3ff00000
223 #define Exp_11 0x3ff00000
225 #define Frac_mask 0xfffff
226 #define Frac_mask1 0xfffff
229 #define Bndry_mask 0xfffff
230 #define Bndry_mask1 0xfffff
232 #define Sign_bit 0x80000000
239 #if !defined(NO_IEEE_Scale)
240 #undef Avoid_Underflow
241 #define Avoid_Underflow
244 #if !defined(Flt_Rounds)
245 #if defined(FLT_ROUNDS)
246 #define Flt_Rounds FLT_ROUNDS
250 #endif /*Flt_Rounds*/
253 #define rounded_product(a,b) a *= b
254 #define rounded_quotient(a,b) a /= b
256 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
257 #define Big1 0xffffffff
260 // FIXME: we should remove non-Pack_32 mode since it is unused and unmaintained
265 #if PLATFORM(PPC64) || PLATFORM(X86_64)
266 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
267 #define USE_LONG_LONG
270 #ifndef USE_LONG_LONG
273 /* When Pack_32 is not defined, we store 16 bits per 32-bit int32_t.
274 * This makes some inner loops simpler and sometimes saves work
275 * during multiplications, but it often seems to make things slightly
276 * slower. Hence the default is now to store 32 bits per int32_t.
284 BigInt() : sign(0) { }
295 return m_words
.size();
298 void resize(size_t s
)
305 return m_words
.data();
308 const uint32_t* words() const
310 return m_words
.data();
313 void append(uint32_t w
)
318 Vector
<uint32_t, 16> m_words
;
321 static void multadd(BigInt
& b
, int m
, int a
) /* multiply by m and add a */
324 unsigned long long carry
;
330 uint32_t* x
= b
.words();
335 unsigned long long y
= *x
* (unsigned long long)m
+ carry
;
337 *x
++ = (uint32_t)y
& 0xffffffffUL
;
341 uint32_t y
= (xi
& 0xffff) * m
+ carry
;
342 uint32_t z
= (xi
>> 16) * m
+ (y
>> 16);
344 *x
++ = (z
<< 16) + (y
& 0xffff);
346 uint32_t y
= *x
* m
+ carry
;
354 b
.append((uint32_t)carry
);
357 static void s2b(BigInt
& b
, const char* s
, int nd0
, int nd
, uint32_t y9
)
361 int32_t x
= (nd
+ 8) / 9;
363 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) { }
370 b
.resize((b
->x
[1] = y9
>> 16) ? 2 : 1);
371 b
.words()[0] = y9
& 0xffff;
378 multadd(b
, 10, *s
++ - '0');
384 multadd(b
, 10, *s
++ - '0');
387 static int hi0bits(uint32_t x
)
391 if (!(x
& 0xffff0000)) {
395 if (!(x
& 0xff000000)) {
399 if (!(x
& 0xf0000000)) {
403 if (!(x
& 0xc0000000)) {
407 if (!(x
& 0x80000000)) {
409 if (!(x
& 0x40000000))
415 static int lo0bits (uint32_t* y
)
457 static void i2b(BigInt
& b
, int i
)
464 static void mult(BigInt
& aRef
, const BigInt
& bRef
)
466 const BigInt
* a
= &aRef
;
467 const BigInt
* b
= &bRef
;
470 const uint32_t *x
= 0, *xa
, *xb
, *xae
, *xbe
;
474 unsigned long long carry
, z
;
479 if (a
->size() < b
->size()) {
480 const BigInt
* tmp
= a
;
490 for (xc
= c
.words(), xa
= xc
+ wc
; xc
< xa
; xc
++)
498 for (; xb
< xbe
; xc0
++) {
504 z
= *x
++ * (unsigned long long)y
+ *xc
+ carry
;
506 *xc
++ = (uint32_t)z
& 0xffffffffUL
;
508 *xc
= (uint32_t)carry
;
513 for (; xb
< xbe
; xb
++, xc0
++) {
514 if ((y
= *xb
& 0xffff)) {
519 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
521 uint32_t z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
527 if ((y
= *xb
>> 16)) {
533 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
536 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
543 for(; xb
< xbe
; xc0
++) {
549 z
= *x
++ * y
+ *xc
+ carry
;
558 for (xc0
= c
.words(), xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) { }
569 static int p5s_count
;
571 static ALWAYS_INLINE
void pow5mult(BigInt
& b
, int k
)
573 static int p05
[3] = { 5, 25, 125 };
576 multadd(b
, p05
[i
- 1], 0);
581 #if ENABLE(JSC_MULTIPLE_THREADS)
582 s_dtoaP5Mutex
->lock();
595 int p5s_count_local
= p5s_count
;
596 #if ENABLE(JSC_MULTIPLE_THREADS)
597 s_dtoaP5Mutex
->unlock();
608 if (++p5s_used
== p5s_count_local
) {
609 #if ENABLE(JSC_MULTIPLE_THREADS)
610 s_dtoaP5Mutex
->lock();
612 if (p5s_used
== p5s_count
) {
614 p5
->next
= new P5Node
;
616 p5
->next
->val
= p5
->val
;
617 mult(p5
->next
->val
, p5
->next
->val
);
621 p5s_count_local
= p5s_count
;
622 #if ENABLE(JSC_MULTIPLE_THREADS)
623 s_dtoaP5Mutex
->unlock();
630 static ALWAYS_INLINE
void lshift(BigInt
& b
, int k
)
638 int origSize
= b
.size();
639 int n1
= n
+ origSize
+ 1;
642 b
.resize(b
.size() + n
+ 1);
644 b
.resize(b
.size() + n
);
646 const uint32_t* srcStart
= b
.words();
647 uint32_t* dstStart
= b
.words();
648 const uint32_t* src
= srcStart
+ origSize
- 1;
649 uint32_t* dst
= dstStart
+ n1
- 1;
652 uint32_t hiSubword
= 0;
654 for (; src
>= srcStart
; --src
) {
655 *dst
-- = hiSubword
| *src
>> s
;
656 hiSubword
= *src
<< k
;
659 ASSERT(dst
== dstStart
+ n
);
661 b
.resize(origSize
+ n
+ (b
.words()[n1
- 1] != 0));
665 uint32_t hiSubword
= 0;
667 for (; src
>= srcStart
; --src
) {
668 *dst
-- = hiSubword
| *src
>> s
;
669 hiSubword
= (*src
<< k
) & 0xffff;
672 ASSERT(dst
== dstStart
+ n
);
673 result
->wds
= b
->wds
+ n
+ (result
->x
[n1
- 1] != 0);
679 } while (src
>= srcStart
);
681 for (dst
= dstStart
+ n
; dst
!= dstStart
; )
684 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
687 static int cmp(const BigInt
& a
, const BigInt
& b
)
689 const uint32_t *xa
, *xa0
, *xb
, *xb0
;
694 ASSERT(i
<= 1 || a
.words()[i
- 1]);
695 ASSERT(j
<= 1 || b
.words()[j
- 1]);
704 return *xa
< *xb
? -1 : 1;
711 static ALWAYS_INLINE
void diff(BigInt
& c
, const BigInt
& aRef
, const BigInt
& bRef
)
713 const BigInt
* a
= &aRef
;
714 const BigInt
* b
= &bRef
;
726 const BigInt
* tmp
= a
;
734 const uint32_t* xa
= a
->words();
735 const uint32_t* xae
= xa
+ wa
;
737 const uint32_t* xb
= b
->words();
738 const uint32_t* xbe
= xb
+ wb
;
744 unsigned long long borrow
= 0;
746 unsigned long long y
= (unsigned long long)*xa
++ - *xb
++ - borrow
;
747 borrow
= y
>> 32 & (uint32_t)1;
748 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
751 unsigned long long y
= *xa
++ - borrow
;
752 borrow
= y
>> 32 & (uint32_t)1;
753 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
759 uint32_t y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
760 borrow
= (y
& 0x10000) >> 16;
761 uint32_t z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
762 borrow
= (z
& 0x10000) >> 16;
766 uint32_t y
= (*xa
& 0xffff) - borrow
;
767 borrow
= (y
& 0x10000) >> 16;
768 uint32_t z
= (*xa
++ >> 16) - borrow
;
769 borrow
= (z
& 0x10000) >> 16;
774 uint32_t y
= *xa
++ - *xb
++ - borrow
;
775 borrow
= (y
& 0x10000) >> 16;
779 uint32_t y
= *xa
++ - borrow
;
780 borrow
= (y
& 0x10000) >> 16;
790 static double ulp(U
*x
)
795 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
796 #ifndef Avoid_Underflow
797 #ifndef Sudden_Underflow
803 #ifndef Avoid_Underflow
804 #ifndef Sudden_Underflow
808 word0(&u
) = 0x80000 >> L
;
813 word1(&u
) = L
>= 31 ? 1 : 1 << 31 - L
;
821 static double b2d(const BigInt
& a
, int* e
)
842 d0
= Exp_1
| (y
>> (Ebits
- k
));
843 w
= xa
> xa0
? *--xa
: 0;
844 d1
= (y
<< (32 - Ebits
+ k
)) | (w
>> (Ebits
- k
));
847 z
= xa
> xa0
? *--xa
: 0;
849 d0
= Exp_1
| (y
<< k
) | (z
>> (32 - k
));
850 y
= xa
> xa0
? *--xa
: 0;
851 d1
= (z
<< k
) | (y
>> (32 - k
));
857 if (k
< Ebits
+ 16) {
858 z
= xa
> xa0
? *--xa
: 0;
859 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
860 w
= xa
> xa0
? *--xa
: 0;
861 y
= xa
> xa0
? *--xa
: 0;
862 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
865 z
= xa
> xa0
? *--xa
: 0;
866 w
= xa
> xa0
? *--xa
: 0;
868 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
869 y
= xa
> xa0
? *--xa
: 0;
870 d1
= w
<< k
+ 16 | y
<< k
;
878 static ALWAYS_INLINE
void d2b(BigInt
& b
, U
* d
, int* e
, int* bits
)
882 #ifndef Sudden_Underflow
897 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
898 #ifdef Sudden_Underflow
899 de
= (int)(d0
>> Exp_shift
);
901 if ((de
= (int)(d0
>> Exp_shift
)))
906 if ((k
= lo0bits(&y
))) {
907 x
[0] = y
| (z
<< (32 - k
));
916 #ifndef Sudden_Underflow
922 #ifndef Sudden_Underflow
930 if ((k
= lo0bits(&y
))) {
932 x
[0] = y
| z
<< 32 - k
& 0xffff;
933 x
[1] = z
>> k
- 16 & 0xffff;
938 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
939 x
[2] = z
>> k
& 0xffff;
965 #ifndef Sudden_Underflow
968 *e
= de
- Bias
- (P
- 1) + k
;
970 #ifndef Sudden_Underflow
972 *e
= de
- Bias
- (P
- 1) + 1 + k
;
974 *bits
= (32 * i
) - hi0bits(x
[i
- 1]);
976 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
984 static double ratio(const BigInt
& a
, const BigInt
& b
)
989 dval(&da
) = b2d(a
, &ka
);
990 dval(&db
) = b2d(b
, &kb
);
992 k
= ka
- kb
+ 32 * (a
.size() - b
.size());
994 k
= ka
- kb
+ 16 * (a
.size() - b
.size());
997 word0(&da
) += k
* Exp_msk1
;
1000 word0(&db
) += k
* Exp_msk1
;
1002 return dval(&da
) / dval(&db
);
1005 static const double tens
[] = {
1006 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1007 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1011 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1012 static const double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1013 #ifdef Avoid_Underflow
1014 9007199254740992. * 9007199254740992.e
-256
1015 /* = 2^106 * 1e-53 */
1021 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1022 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1023 #define Scale_Bit 0x10
1026 #if defined(INFNAN_CHECK)
1029 #define NAN_WORD0 0x7ff80000
1036 static int match(const char** sp
, const char* t
)
1039 const char* s
= *sp
;
1041 while ((d
= *t
++)) {
1042 if ((c
= *++s
) >= 'A' && c
<= 'Z')
1052 static void hexnan(U
* rvp
, const char** sp
)
1056 int havedig
, udx0
, xshift
;
1059 havedig
= xshift
= 0;
1062 while ((c
= *(const unsigned char*)++s
)) {
1063 if (c
>= '0' && c
<= '9')
1065 else if (c
>= 'a' && c
<= 'f')
1067 else if (c
>= 'A' && c
<= 'F')
1069 else if (c
<= ' ') {
1070 if (udx0
&& havedig
) {
1075 } else if (/*(*/ c
== ')' && havedig
) {
1079 return; /* invalid form: don't change *sp */
1087 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1088 x
[1] = (x
[1] << 4) | c
;
1090 if ((x
[0] &= 0xfffff) || x
[1]) {
1091 word0(rvp
) = Exp_mask
| x
[0];
1095 #endif /*No_Hex_NaN*/
1096 #endif /* INFNAN_CHECK */
1098 double strtod(const char* s00
, char** se
)
1100 #ifdef Avoid_Underflow
1103 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1104 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1105 const char *s
, *s0
, *s1
;
1107 U aadj2
, adj
, rv
, rv0
;
1110 BigInt bb
, bb1
, bd
, bd0
, bs
, delta
;
1112 int inexact
, oldinexact
;
1115 sign
= nz0
= nz
= 0;
1117 for (s
= s00
; ; s
++)
1141 while (*++s
== '0') { }
1147 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1149 y
= (10 * y
) + c
- '0';
1151 z
= (10 * z
) + c
- '0';
1156 for (; c
== '0'; c
= *++s
)
1158 if (c
> '0' && c
<= '9') {
1166 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
1171 for (i
= 1; i
< nz
; i
++)
1174 else if (nd
<= DBL_DIG
+ 1)
1178 else if (nd
<= DBL_DIG
+ 1)
1186 if (c
== 'e' || c
== 'E') {
1187 if (!nd
&& !nz
&& !nz0
) {
1198 if (c
>= '0' && c
<= '9') {
1201 if (c
> '0' && c
<= '9') {
1204 while ((c
= *++s
) >= '0' && c
<= '9')
1205 L
= (10 * L
) + c
- '0';
1206 if (s
- s1
> 8 || L
> 19999)
1207 /* Avoid confusion from exponents
1208 * so large that e might overflow.
1210 e
= 19999; /* safe for 16 bit ints */
1223 /* Check for Nan and Infinity */
1227 if (match(&s
,"nf")) {
1229 if (!match(&s
,"inity"))
1231 word0(&rv
) = 0x7ff00000;
1238 if (match(&s
, "an")) {
1239 word0(&rv
) = NAN_WORD0
;
1240 word1(&rv
) = NAN_WORD1
;
1242 if (*s
== '(') /*)*/
1248 #endif /* INFNAN_CHECK */
1257 /* Now we have nd0 digits, starting at s0, followed by a
1258 * decimal point, followed by nd-nd0 digits. The number we're
1259 * after is the integer represented by those digits times
1264 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1269 oldinexact
= get_inexact();
1271 dval(&rv
) = tens
[k
- 9] * dval(&rv
) + z
;
1273 if (nd
<= DBL_DIG
&& Flt_Rounds
== 1) {
1277 if (e
<= Ten_pmax
) {
1278 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1282 if (e
<= Ten_pmax
+ i
) {
1283 /* A fancier test would sometimes let us do
1284 * this for larger i values.
1287 dval(&rv
) *= tens
[i
];
1288 /* rv = */ rounded_product(dval(&rv
), tens
[e
]);
1292 #ifndef Inaccurate_Divide
1293 else if (e
>= -Ten_pmax
) {
1294 /* rv = */ rounded_quotient(dval(&rv
), tens
[-e
]);
1304 oldinexact
= get_inexact();
1306 #ifdef Avoid_Underflow
1310 /* Get starting approximation = rv * 10**e1 */
1314 dval(&rv
) *= tens
[i
];
1316 if (e1
> DBL_MAX_10_EXP
) {
1321 /* Can't trust HUGE_VAL */
1322 word0(&rv
) = Exp_mask
;
1325 /* set overflow bit */
1327 dval(&rv0
) *= dval(&rv0
);
1332 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1334 dval(&rv
) *= bigtens
[j
];
1335 /* The last multiplication could overflow. */
1336 word0(&rv
) -= P
* Exp_msk1
;
1337 dval(&rv
) *= bigtens
[j
];
1338 if ((z
= word0(&rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
))
1340 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
1341 /* set to largest number */
1342 /* (Can't trust DBL_MAX) */
1346 word0(&rv
) += P
* Exp_msk1
;
1348 } else if (e1
< 0) {
1351 dval(&rv
) /= tens
[i
];
1353 if (e1
>= 1 << n_bigtens
)
1355 #ifdef Avoid_Underflow
1358 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
1360 dval(&rv
) *= tinytens
[j
];
1361 if (scale
&& (j
= (2 * P
) + 1 - ((word0(&rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
1362 /* scaled rv is denormal; zap j low bits */
1366 word0(&rv
) = (P
+ 2) * Exp_msk1
;
1368 word0(&rv
) &= 0xffffffff << (j
- 32);
1370 word1(&rv
) &= 0xffffffff << j
;
1373 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1375 dval(&rv
) *= tinytens
[j
];
1376 /* The last multiplication could underflow. */
1377 dval(&rv0
) = dval(&rv
);
1378 dval(&rv
) *= tinytens
[j
];
1380 dval(&rv
) = 2. * dval(&rv0
);
1381 dval(&rv
) *= tinytens
[j
];
1391 #ifndef Avoid_Underflow
1394 /* The refinement below will clean
1395 * this approximation up.
1402 /* Now the hard part -- adjusting rv to the correct value.*/
1404 /* Put digits into bd: true value = bd * 10^e */
1406 s2b(bd0
, s0
, nd0
, nd
, y
);
1410 d2b(bb
, &rv
, &bbe
, &bbbits
); /* rv = bb * 2^bbe */
1425 #ifdef Avoid_Underflow
1427 i
= j
+ bbbits
- 1; /* logb(rv) */
1428 if (i
< Emin
) /* denormal */
1432 #else /*Avoid_Underflow*/
1433 #ifdef Sudden_Underflow
1435 #else /*Sudden_Underflow*/
1437 i
= j
+ bbbits
- 1; /* logb(rv) */
1438 if (i
< Emin
) /* denormal */
1442 #endif /*Sudden_Underflow*/
1443 #endif /*Avoid_Underflow*/
1446 #ifdef Avoid_Underflow
1449 i
= bb2
< bd2
? bb2
: bd2
;
1469 diff(delta
, bb
, bd
);
1475 /* Error is less than half an ulp -- check for
1476 * special case of mantissa a power of two.
1478 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
1479 #ifdef Avoid_Underflow
1480 || (word0(&rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
1482 || (word0(&rv
) & Exp_mask
) <= Exp_msk1
1486 if (!delta
->words()[0] && delta
->size() <= 1)
1491 if (!delta
.words()[0] && delta
.size() <= 1) {
1498 lshift(delta
, Log2P
);
1499 if (cmp(delta
, bs
) > 0)
1504 /* exactly half-way between */
1506 if ((word0(&rv
) & Bndry_mask1
) == Bndry_mask1
1508 #ifdef Avoid_Underflow
1509 (scale
&& (y
= word0(&rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
1510 ? (0xffffffff & (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
)))) :
1513 /*boundary case -- increment exponent*/
1514 word0(&rv
) = (word0(&rv
) & Exp_mask
) + Exp_msk1
;
1516 #ifdef Avoid_Underflow
1521 } else if (!(word0(&rv
) & Bndry_mask
) && !word1(&rv
)) {
1523 /* boundary case -- decrement exponent */
1524 #ifdef Sudden_Underflow /*{{*/
1525 L
= word0(&rv
) & Exp_mask
;
1526 #ifdef Avoid_Underflow
1527 if (L
<= (scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
1530 #endif /*Avoid_Underflow*/
1533 #else /*Sudden_Underflow}{*/
1534 #ifdef Avoid_Underflow
1536 L
= word0(&rv
) & Exp_mask
;
1537 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
1538 if (L
> (P
+ 2) * Exp_msk1
)
1539 /* round even ==> */
1542 /* rv = smallest denormal */
1546 #endif /*Avoid_Underflow*/
1547 L
= (word0(&rv
) & Exp_mask
) - Exp_msk1
;
1548 #endif /*Sudden_Underflow}}*/
1549 word0(&rv
) = L
| Bndry_mask1
;
1550 word1(&rv
) = 0xffffffff;
1553 if (!(word1(&rv
) & LSB
))
1556 dval(&rv
) += ulp(&rv
);
1558 dval(&rv
) -= ulp(&rv
);
1559 #ifndef Sudden_Underflow
1564 #ifdef Avoid_Underflow
1569 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
1572 else if (word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1573 #ifndef Sudden_Underflow
1574 if (word1(&rv
) == Tiny1
&& !word0(&rv
))
1580 /* special case -- power of FLT_RADIX to be */
1581 /* rounded down... */
1583 if (aadj
< 2. / FLT_RADIX
)
1584 aadj
= 1. / FLT_RADIX
;
1591 aadj1
= dsign
? aadj
: -aadj
;
1592 #ifdef Check_FLT_ROUNDS
1594 case 2: /* towards +infinity */
1597 case 0: /* towards 0 */
1598 case 3: /* towards -infinity */
1602 if (Flt_Rounds
== 0)
1604 #endif /*Check_FLT_ROUNDS*/
1606 y
= word0(&rv
) & Exp_mask
;
1608 /* Check for overflow */
1610 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
1611 dval(&rv0
) = dval(&rv
);
1612 word0(&rv
) -= P
* Exp_msk1
;
1613 adj
.d
= aadj1
* ulp(&rv
);
1615 if ((word0(&rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
1616 if (word0(&rv0
) == Big0
&& word1(&rv0
) == Big1
)
1622 word0(&rv
) += P
* Exp_msk1
;
1624 #ifdef Avoid_Underflow
1625 if (scale
&& y
<= 2 * P
* Exp_msk1
) {
1626 if (aadj
<= 0x7fffffff) {
1627 if ((z
= (uint32_t)aadj
) <= 0)
1630 aadj1
= dsign
? aadj
: -aadj
;
1632 dval(&aadj2
) = aadj1
;
1633 word0(&aadj2
) += (2 * P
+ 1) * Exp_msk1
- y
;
1634 aadj1
= dval(&aadj2
);
1636 adj
.d
= aadj1
* ulp(&rv
);
1639 #ifdef Sudden_Underflow
1640 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
) {
1641 dval(&rv0
) = dval(&rv
);
1642 word0(&rv
) += P
* Exp_msk1
;
1643 adj
.d
= aadj1
* ulp(&rv
);
1645 if ((word0(&rv
) & Exp_mask
) <= P
* Exp_msk1
)
1647 if (word0(&rv0
) == Tiny0
&& word1(&rv0
) == Tiny1
)
1654 word0(&rv
) -= P
* Exp_msk1
;
1656 adj
.d
= aadj1
* ulp(&rv
);
1659 #else /*Sudden_Underflow*/
1660 /* Compute adj so that the IEEE rounding rules will
1661 * correctly round rv + adj in some half-way cases.
1662 * If rv * ulp(rv) is denormalized (i.e.,
1663 * y <= (P - 1) * Exp_msk1), we must adjust aadj to avoid
1664 * trouble from bits lost to denormalization;
1665 * example: 1.2e-307 .
1667 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
1668 aadj1
= (double)(int)(aadj
+ 0.5);
1672 adj
.d
= aadj1
* ulp(&rv
);
1674 #endif /*Sudden_Underflow*/
1675 #endif /*Avoid_Underflow*/
1677 z
= word0(&rv
) & Exp_mask
;
1679 #ifdef Avoid_Underflow
1683 /* Can we stop now? */
1686 /* The tolerances below are conservative. */
1687 if (dsign
|| word1(&rv
) || word0(&rv
) & Bndry_mask
) {
1688 if (aadj
< .4999999 || aadj
> .5000001)
1690 } else if (aadj
< .4999999 / FLT_RADIX
)
1700 word0(&rv0
) = Exp_1
+ (70 << Exp_shift
);
1704 } else if (!oldinexact
)
1707 #ifdef Avoid_Underflow
1709 word0(&rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
1711 dval(&rv
) *= dval(&rv0
);
1713 /* try to avoid the bug of testing an 8087 register value */
1714 if (word0(&rv
) == 0 && word1(&rv
) == 0)
1718 #endif /* Avoid_Underflow */
1720 if (inexact
&& !(word0(&rv
) & Exp_mask
)) {
1721 /* set underflow bit */
1722 dval(&rv0
) = 1e-300;
1723 dval(&rv0
) *= dval(&rv0
);
1728 *se
= const_cast<char*>(s
);
1729 return sign
? -dval(&rv
) : dval(&rv
);
1732 static ALWAYS_INLINE
int quorem(BigInt
& b
, BigInt
& S
)
1735 uint32_t *bx
, *bxe
, q
, *sx
, *sxe
;
1736 #ifdef USE_LONG_LONG
1737 unsigned long long borrow
, carry
, y
, ys
;
1739 uint32_t borrow
, carry
, y
, ys
;
1744 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
1745 ASSERT(S
.size() <= 1 || S
.words()[S
.size() - 1]);
1748 ASSERT_WITH_MESSAGE(b
.size() <= n
, "oversize b in quorem");
1755 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1756 ASSERT_WITH_MESSAGE(q
<= 9, "oversized quotient in quorem");
1761 #ifdef USE_LONG_LONG
1762 ys
= *sx
++ * (unsigned long long)q
+ carry
;
1764 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1765 borrow
= y
>> 32 & (uint32_t)1;
1766 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1770 ys
= (si
& 0xffff) * q
+ carry
;
1771 zs
= (si
>> 16) * q
+ (ys
>> 16);
1773 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1774 borrow
= (y
& 0x10000) >> 16;
1775 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1776 borrow
= (z
& 0x10000) >> 16;
1779 ys
= *sx
++ * q
+ carry
;
1781 y
= *bx
- (ys
& 0xffff) - borrow
;
1782 borrow
= (y
& 0x10000) >> 16;
1786 } while (sx
<= sxe
);
1789 while (--bxe
> bx
&& !*bxe
)
1794 if (cmp(b
, S
) >= 0) {
1801 #ifdef USE_LONG_LONG
1804 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
1805 borrow
= y
>> 32 & (uint32_t)1;
1806 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
1810 ys
= (si
& 0xffff) + carry
;
1811 zs
= (si
>> 16) + (ys
>> 16);
1813 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
1814 borrow
= (y
& 0x10000) >> 16;
1815 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
1816 borrow
= (z
& 0x10000) >> 16;
1821 y
= *bx
- (ys
& 0xffff) - borrow
;
1822 borrow
= (y
& 0x10000) >> 16;
1826 } while (sx
<= sxe
);
1830 while (--bxe
> bx
&& !*bxe
)
1838 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1840 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1841 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1844 * 1. Rather than iterating, we use a simple numeric overestimate
1845 * to determine k = floor(log10(d)). We scale relevant
1846 * quantities using O(log2(k)) rather than O(k) multiplications.
1847 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1848 * try to generate digits strictly left to right. Instead, we
1849 * compute with fewer bits and propagate the carry if necessary
1850 * when rounding the final digit up. This is often faster.
1851 * 3. Under the assumption that input will be rounded nearest,
1852 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1853 * That is, we allow equality in stopping tests when the
1854 * round-nearest rule will give the same floating-point value
1855 * as would satisfaction of the stopping test with strict
1857 * 4. We remove common factors of powers of 2 from relevant
1859 * 5. When converting floating-point integers less than 1e16,
1860 * we use floating-point arithmetic rather than resorting
1861 * to multiple-precision integers.
1862 * 6. When asked to produce fewer than 15 digits, we first try
1863 * to get by with floating-point arithmetic; we resort to
1864 * multiple-precision integer arithmetic only if we cannot
1865 * guarantee that the floating-point calculation has given
1866 * the correctly rounded result. For k requested digits and
1867 * "uniformly" distributed input, the probability is
1868 * something like 10^(k-15) that we must resort to the int32_t
1872 void dtoa(char* result
, double dd
, int ndigits
, int* decpt
, int* sign
, char** rve
)
1875 Arguments ndigits, decpt, sign are similar to those
1876 of ecvt and fcvt; trailing zeros are suppressed from
1877 the returned string. If not null, *rve is set to point
1878 to the end of the return value. If d is +-Infinity or NaN,
1879 then *decpt is set to 9999.
1883 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
1884 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1885 spec_case
, try_quick
;
1887 #ifndef Sudden_Underflow
1891 BigInt b
, b1
, delta
, mlo
, mhi
, S
;
1896 int inexact
, oldinexact
;
1900 if (word0(&u
) & Sign_bit
) {
1901 /* set sign for everything, including 0's and NaNs */
1903 word0(&u
) &= ~Sign_bit
; /* clear sign bit */
1907 if ((word0(&u
) & Exp_mask
) == Exp_mask
)
1909 /* Infinity or NaN */
1911 if (!word1(&u
) && !(word0(&u
) & 0xfffff))
1912 strcpy(result
, "Infinity");
1914 strcpy(result
, "NaN");
1925 try_quick
= oldinexact
= get_inexact();
1929 d2b(b
, &u
, &be
, &bbits
);
1930 #ifdef Sudden_Underflow
1931 i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
1933 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
1935 dval(&d2
) = dval(&u
);
1936 word0(&d2
) &= Frac_mask1
;
1937 word0(&d2
) |= Exp_11
;
1939 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1940 * log10(x) = log(x) / log(10)
1941 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1942 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1944 * This suggests computing an approximation k to log10(d) by
1946 * k = (i - Bias)*0.301029995663981
1947 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1949 * We want k to be too large rather than too small.
1950 * The error in the first-order Taylor series approximation
1951 * is in our favor, so we just round up the constant enough
1952 * to compensate for any error in the multiplication of
1953 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1954 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1955 * adding 1e-13 to the constant term more than suffices.
1956 * Hence we adjust the constant term to 0.1760912590558.
1957 * (We could get a more accurate k by invoking log10,
1958 * but this is probably not worthwhile.)
1962 #ifndef Sudden_Underflow
1965 /* d is denormalized */
1967 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
1968 x
= (i
> 32) ? (word0(&u
) << (64 - i
)) | (word1(&u
) >> (i
- 32))
1969 : word1(&u
) << (32 - i
);
1971 word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */
1972 i
-= (Bias
+ (P
- 1) - 1) + 1;
1976 ds
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i
* 0.301029995663981);
1978 if (ds
< 0. && ds
!= k
)
1979 k
--; /* want k = floor(ds) */
1981 if (k
>= 0 && k
<= Ten_pmax
) {
1982 if (dval(&u
) < tens
[k
])
2005 #ifdef Check_FLT_ROUNDS
2006 try_quick
= Rounding
== 1;
2010 #endif /*SET_INEXACT*/
2018 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
2020 /* Try to get by with floating-point arithmetic. */
2023 dval(&d2
) = dval(&u
);
2026 ieps
= 2; /* conservative */
2031 /* prevent overflows */
2033 dval(&u
) /= bigtens
[n_bigtens
- 1];
2036 for (; j
; j
>>= 1, i
++) {
2043 } else if ((j1
= -k
)) {
2044 dval(&u
) *= tens
[j1
& 0xf];
2045 for (j
= j1
>> 4; j
; j
>>= 1, i
++) {
2048 dval(&u
) *= bigtens
[i
];
2052 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
2060 dval(&eps
) = (ieps
* dval(&u
)) + 7.;
2061 word0(&eps
) -= (P
- 1) * Exp_msk1
;
2066 if (dval(&u
) > dval(&eps
))
2068 if (dval(&u
) < -dval(&eps
))
2072 #ifndef No_leftright
2074 /* Use Steele & White method of only
2075 * generating digits needed.
2077 dval(&eps
) = (0.5 / tens
[ilim
- 1]) - dval(&eps
);
2079 L
= (long int)dval(&u
);
2081 *s
++ = '0' + (int)L
;
2082 if (dval(&u
) < dval(&eps
))
2084 if (1. - dval(&u
) < dval(&eps
))
2093 /* Generate ilim digits, then fix them up. */
2094 dval(&eps
) *= tens
[ilim
- 1];
2095 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2096 L
= (int32_t)(dval(&u
));
2097 if (!(dval(&u
) -= L
))
2099 *s
++ = '0' + (int)L
;
2101 if (dval(&u
) > 0.5 + dval(&eps
))
2103 else if (dval(&u
) < 0.5 - dval(&eps
)) {
2104 while (*--s
== '0') { }
2111 #ifndef No_leftright
2116 dval(&u
) = dval(&d2
);
2121 /* Do we have a "small" integer? */
2123 if (be
>= 0 && k
<= Int_max
) {
2126 if (ndigits
< 0 && ilim
<= 0) {
2129 if (ilim
< 0 || dval(&u
) <= 5 * ds
)
2133 for (i
= 1;; i
++, dval(&u
) *= 10.) {
2134 L
= (int32_t)(dval(&u
) / ds
);
2136 #ifdef Check_FLT_ROUNDS
2137 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2143 *s
++ = '0' + (int)L
;
2151 dval(&u
) += dval(&u
);
2152 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& (L
& 1))) {
2174 #ifndef Sudden_Underflow
2175 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
2182 if (m2
> 0 && s2
> 0) {
2183 i
= m2
< s2
? m2
: s2
;
2203 /* Check for special case that d is a normalized power of 2. */
2206 if (!word1(&u
) && !(word0(&u
) & Bndry_mask
)
2207 #ifndef Sudden_Underflow
2208 && word0(&u
) & (Exp_mask
& ~Exp_msk1
)
2211 /* The special case */
2217 /* Arrange for convenient computation of quotients:
2218 * shift left if necessary so divisor has 4 leading 0 bits.
2220 * Perhaps we should just compute leading 28 bits of S once
2221 * and for all and pass them and a shift to quorem, so it
2222 * can do shifts and ors to compute the numerator for q.
2225 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0x1f))
2228 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0xf))
2249 multadd(b
, 10, 0); /* we botched the k estimate */
2251 multadd(mhi
, 10, 0);
2260 /* Compute mlo -- check for special case
2261 * that d is a normalized power of 2.
2271 dig
= quorem(b
,S
) + '0';
2272 /* Do we yet have the shortest decimal string
2273 * that will round to d?
2276 diff(delta
, S
, mhi
);
2277 j1
= delta
.sign
? 1 : cmp(b
, delta
);
2278 if (j1
== 0 && !(word1(&u
) & 1)) {
2284 else if (!b
->x
[0] && b
->wds
<= 1)
2290 if (j
< 0 || (j
== 0 && !(word1(&u
) & 1))) {
2291 if (!b
.words()[0] && b
.size() <= 1) {
2300 if ((j1
> 0 || (j1
== 0 && (dig
& 1))) && dig
++ == '9')
2308 if (dig
== '9') { /* possible if i == 1 */
2320 multadd(mlo
, 10, 0);
2321 multadd(mhi
, 10, 0);
2325 *s
++ = dig
= quorem(b
,S
) + '0';
2326 if (!b
.words()[0] && b
.size() <= 1) {
2337 /* Round off last digit */
2341 if (j
> 0 || (j
== 0 && (dig
& 1))) {
2351 while (*--s
== '0') { }
2366 word0(&u
) = Exp_1
+ (70 << Exp_shift
);
2370 } else if (!oldinexact
)