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