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