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

Last change on this file since 38247 was 38117, checked in by [email protected], 17 years ago

2008-11-04 Cameron Zwarich <[email protected]>

Rubber-stamped by Sam Weinig.

Move kjs/dtoa.h to the wtf subdirectory of JavaScriptCore, and remove
its unused forwarding header in WebKit/mac.

JavaScriptCore:

  • AllInOneFile.cpp:
  • GNUmakefile.am:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • kjs/dtoa.cpp: Removed.
  • kjs/dtoa.h: Removed.
  • wtf/dtoa.cpp: Copied from kjs/dtoa.cpp.
  • wtf/dtoa.h: Copied from kjs/dtoa.h.

WebCore:

  • ForwardingHeaders/kjs/dtoa.h: Removed.
  • ForwardingHeaders/wtf/dtoa.h: Copied from ForwardingHeaders/kjs/dtoa.h.
  • css/CSSParser.cpp:
  • platform/text/String.cpp:
  • platform/text/StringImpl.cpp:

WebKit/mac:

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