1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * This file is based on the third-party code dtoa.c. We minimize our
8 * modifications to third-party code to make it easy to merge new versions.
9 * The author of dtoa.c was not willing to add the parentheses suggested by
10 * GCC, so we suppress these warnings.
12 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
13 # pragma GCC diagnostic ignored "-Wparentheses"
19 #define MULTIPLE_THREADS
20 #define ACQUIRE_DTOA_LOCK(n) PR_Lock(dtoa_lock[n])
21 #define FREE_DTOA_LOCK(n) PR_Unlock(dtoa_lock[n])
23 static PRLock
* dtoa_lock
[2];
25 void _PR_InitDtoa(void) {
26 dtoa_lock
[0] = PR_NewLock();
27 dtoa_lock
[1] = PR_NewLock();
30 void _PR_CleanupDtoa(void) {
31 PR_DestroyLock(dtoa_lock
[0]);
33 PR_DestroyLock(dtoa_lock
[1]);
36 /* FIXME: deal with freelist and p5s. */
39 #if !defined(__ARM_EABI__) && (defined(__arm) || defined(__arm__) || \
40 defined(__arm26__) || defined(__arm32__))
42 #elif defined(IS_LITTLE_ENDIAN)
49 #define ULong PRUint32
54 /****************************************************************
56 * The author of this software is David M. Gay.
58 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose without fee is hereby granted, provided that this entire notice
62 * is included in all copies of any software which is or includes a copy
63 * or modification of this software and in all copies of the supporting
64 * documentation for such software.
66 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
67 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
68 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
69 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
71 ***************************************************************/
73 /* Please send bug reports to David M. Gay (dmg at acm dot org,
74 * with " at " changed at "@" and " dot " changed to "."). */
76 /* On a machine with IEEE extended-precision registers, it is
77 * necessary to specify double-precision (53-bit) rounding precision
78 * before invoking strtod or dtoa. If the machine uses (the equivalent
79 * of) Intel 80x87 arithmetic, the call
80 * _control87(PC_53, MCW_PC);
81 * does this with many compilers. Whether this or another call is
82 * appropriate depends on the compiler; for this to work, it may be
83 * necessary to #include "float.h" or another system-dependent header
87 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
89 * This strtod returns a nearest machine number to the input decimal
90 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
91 * broken by the IEEE round-even rule. Otherwise ties are broken by
92 * biased rounding (add half and chop).
94 * Inspired loosely by William D. Clinger's paper "How to Read Floating
95 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
99 * 1. We only require IEEE, IBM, or VAX double-precision
100 * arithmetic (not IEEE double-extended).
101 * 2. We get by with floating-point arithmetic in a case that
102 * Clinger missed -- when we're computing d * 10^n
103 * for a small integer d and the integer n is not too
104 * much larger than 22 (the maximum integer k for which
105 * we can represent 10^k exactly), we may be able to
106 * compute (d*10^k) * 10^(e-k) with just one roundoff.
107 * 3. Rather than a bit-at-a-time adjustment of the binary
108 * result in the hard case, we use floating-point
109 * arithmetic to determine the adjustment to within
110 * one bit; only in really hard cases do we need to
111 * compute a second residual.
112 * 4. Because of 3., we don't need a large table of powers of 10
113 * for ten-to-e (just some small tables, e.g. of 10^k
118 * #define IEEE_8087 for IEEE-arithmetic machines where the least
119 * significant byte has the lowest address.
120 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
121 * significant byte has the lowest address.
122 * #define IEEE_ARM for IEEE-arithmetic machines where the two words
123 * in a double are stored in big endian order but the two shorts
124 * in a word are still stored in little endian order.
125 * #define Long int on machines with 32-bit ints and 64-bit longs.
126 * #define IBM for IBM mainframe-style floating-point arithmetic.
127 * #define VAX for VAX-style floating-point arithmetic (D_floating).
128 * #define No_leftright to omit left-right logic in fast floating-point
129 * computation of dtoa.
130 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
131 * and strtod and dtoa should round accordingly.
132 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
133 * and Honor_FLT_ROUNDS is not #defined.
134 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
135 * that use extended-precision instructions to compute rounded
136 * products and quotients) with IBM.
137 * #define ROUND_BIASED for IEEE-format with biased rounding.
138 * #define Inaccurate_Divide for IEEE-format with correctly rounded
139 * products but inaccurate quotients, e.g., for Intel i860.
140 * #define NO_LONG_LONG on machines that do not have a "long long"
141 * integer type (of >= 64 bits). On such machines, you can
142 * #define Just_16 to store 16 bits per 32-bit Long when doing
143 * high-precision integer arithmetic. Whether this speeds things
144 * up or slows things down depends on the machine and the number
145 * being converted. If long long is available and the name is
146 * something other than "long long", #define Llong to be the name,
147 * and if "unsigned Llong" does not work as an unsigned version of
148 * Llong, #define #ULLong to be the corresponding unsigned type.
149 * #define KR_headers for old-style C function headers.
150 * #define Bad_float_h if your system lacks a float.h or if it does not
151 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
152 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
153 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
154 * if memory is available and otherwise does something you deem
155 * appropriate. If MALLOC is undefined, malloc will be invoked
156 * directly -- and assumed always to succeed. Similarly, if you
157 * want something other than the system's free() to be called to
158 * recycle memory acquired from MALLOC, #define FREE to be the
159 * name of the alternate routine. (FREE or free is only called in
160 * pathological cases, e.g., in a dtoa call after a dtoa return in
161 * mode 3 with thousands of digits requested.)
162 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
163 * memory allocations from a private pool of memory when possible.
164 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
165 * unless #defined to be a different length. This default length
166 * suffices to get rid of MALLOC calls except for unusual cases,
167 * such as decimal-to-binary conversion of a very long string of
168 * digits. The longest string dtoa can return is about 751 bytes
169 * long. For conversions by strtod of strings of 800 digits and
170 * all dtoa conversions in single-threaded executions with 8-byte
171 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
172 * pointers, PRIVATE_MEM >= 7112 appears adequate.
173 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
174 * Infinity and NaN (case insensitively). On some systems (e.g.,
175 * some HP systems), it may be necessary to #define NAN_WORD0
176 * appropriately -- to the most significant word of a quiet NaN.
177 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
178 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
179 * strtod also accepts (case insensitively) strings of the form
180 * NaN(x), where x is a string of hexadecimal digits and spaces;
181 * if there is only one string of hexadecimal digits, it is taken
182 * for the 52 fraction bits of the resulting NaN; if there are two
183 * or more strings of hex digits, the first is for the high 20 bits,
184 * the second and subsequent for the low 32 bits, with intervening
185 * white space ignored; but if this results in none of the 52
186 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
187 * and NAN_WORD1 are used instead.
188 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
189 * multiple threads. In this case, you must provide (or suitably
190 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
191 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
192 * in pow5mult, ensures lazy evaluation of only one copy of high
193 * powers of 5; omitting this lock would introduce a small
194 * probability of wasting memory, but would otherwise be harmless.)
195 * You must also invoke freedtoa(s) to free the value s returned by
196 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
197 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
198 * avoids underflows on inputs whose result does not underflow.
199 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
200 * floating-point numbers and flushes underflows to zero rather
201 * than implementing gradual underflow, then you must also #define
203 * #define USE_LOCALE to use the current locale's decimal_point value.
204 * #define SET_INEXACT if IEEE arithmetic is being used and extra
205 * computation should be done to set the inexact flag when the
206 * result is inexact and avoid setting inexact when the result
207 * is exact. In this case, dtoa.c must be compiled in
208 * an environment, perhaps provided by #include "dtoa.c" in a
209 * suitable wrapper, that defines two functions,
210 * int get_inexact(void);
211 * void clear_inexact(void);
212 * such that get_inexact() returns a nonzero value if the
213 * inexact bit is already set, and clear_inexact() sets the
214 * inexact bit to 0. When SET_INEXACT is #defined, strtod
215 * also does extra computations to set the underflow and overflow
216 * flags when appropriate (i.e., when the result is tiny and
217 * inexact or when it is a numeric value rounded to +-infinity).
218 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
219 * the result overflows to +-Infinity or underflows to 0.
226 typedef unsigned Long ULong
;
233 fprintf(stderr, "%s\n", x); \
247 extern char* MALLOC();
249 extern void* MALLOC(size_t);
252 # define MALLOC malloc
255 #ifndef Omit_Private_Memory
257 # define PRIVATE_MEM 2304
259 # define PRIVATE_mem ((PRIVATE_MEM + sizeof(double) - 1) / sizeof(double))
260 static double private_mem
[PRIVATE_mem
], *pmem_next
= private_mem
;
264 #undef Avoid_Underflow
281 # define DBL_MAX_10_EXP 308
282 # define DBL_MAX_EXP 1024
284 # endif /*IEEE_Arith*/
288 # define DBL_MAX_10_EXP 75
289 # define DBL_MAX_EXP 63
290 # define FLT_RADIX 16
291 # define DBL_MAX 7.2370055773322621e+75
296 # define DBL_MAX_10_EXP 38
297 # define DBL_MAX_EXP 127
299 # define DBL_MAX 1.7014118346046923e+38
303 # define LONG_MAX 2147483647
306 #else /* ifndef Bad_float_h */
308 #endif /* Bad_float_h */
320 # define CONST /* blank */
326 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(IEEE_ARM) + \
327 defined(VAX) + defined(IBM) != \
329 Exactly one of IEEE_8087
, IEEE_MC68k
, IEEE_ARM
, VAX
, or IBM should be defined
.
337 #define dval(x) (x).d
339 # define word0(x) (x).L[1]
340 # define word1(x) (x).L[0]
342 # define word0(x) (x).L[0]
343 # define word1(x) (x).L[1]
346 /* The following definition of Storeinc is appropriate for MIPS processors.
347 * An alternative that might be better on some machines is
348 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
350 #if defined(IEEE_8087) + defined(IEEE_ARM) + defined(VAX)
351 # define Storeinc(a, b, c) \
352 (((unsigned short*)a)[1] = (unsigned short)b, \
353 ((unsigned short*)a)[0] = (unsigned short)c, a++)
355 # define Storeinc(a, b, c) \
356 (((unsigned short*)a)[0] = (unsigned short)b, \
357 ((unsigned short*)a)[1] = (unsigned short)c, a++)
360 /* #define P DBL_MANT_DIG */
361 /* Ten_pmax = floor(P*log(2)/log(5)) */
362 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
363 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
364 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
367 # define Exp_shift 20
368 # define Exp_shift1 20
369 # define Exp_msk1 0x100000
370 # define Exp_msk11 0x100000
371 # define Exp_mask 0x7ff00000
374 # define Emin (-1022)
375 # define Exp_1 0x3ff00000
376 # define Exp_11 0x3ff00000
378 # define Frac_mask 0xfffff
379 # define Frac_mask1 0xfffff
382 # define Bndry_mask 0xfffff
383 # define Bndry_mask1 0xfffff
385 # define Sign_bit 0x80000000
389 # define Quick_max 14
391 # ifndef NO_IEEE_Scale
392 # define Avoid_Underflow
393 # ifdef Flush_Denorm /* debugging option */
394 # undef Sudden_Underflow
400 # define Flt_Rounds FLT_ROUNDS
402 # define Flt_Rounds 1
404 # endif /*Flt_Rounds*/
406 # ifdef Honor_FLT_ROUNDS
407 # define Rounding rounding
408 # undef Check_FLT_ROUNDS
409 # define Check_FLT_ROUNDS
411 # define Rounding Flt_Rounds
414 #else /* ifndef IEEE_Arith */
415 # undef Check_FLT_ROUNDS
416 # undef Honor_FLT_ROUNDS
418 # undef Sudden_Underflow
419 # define Sudden_Underflow
422 # define Flt_Rounds 0
423 # define Exp_shift 24
424 # define Exp_shift1 24
425 # define Exp_msk1 0x1000000
426 # define Exp_msk11 0x1000000
427 # define Exp_mask 0x7f000000
430 # define Exp_1 0x41000000
431 # define Exp_11 0x41000000
432 # define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
433 # define Frac_mask 0xffffff
434 # define Frac_mask1 0xffffff
437 # define Bndry_mask 0xefffff
438 # define Bndry_mask1 0xffffff
440 # define Sign_bit 0x80000000
442 # define Tiny0 0x100000
444 # define Quick_max 14
448 # define Flt_Rounds 1
449 # define Exp_shift 23
450 # define Exp_shift1 7
451 # define Exp_msk1 0x80
452 # define Exp_msk11 0x800000
453 # define Exp_mask 0x7f80
456 # define Exp_1 0x40800000
457 # define Exp_11 0x4080
459 # define Frac_mask 0x7fffff
460 # define Frac_mask1 0xffff007f
463 # define Bndry_mask 0xffff007f
464 # define Bndry_mask1 0xffff007f
466 # define Sign_bit 0x8000
470 # define Quick_max 15
472 # endif /* IBM, VAX */
473 #endif /* IEEE_Arith */
476 # define ROUND_BIASED
480 # define rounded_product(a, b) a = rnd_prod(a, b)
481 # define rounded_quotient(a, b) a = rnd_quot(a, b)
483 extern double rnd_prod(), rnd_quot();
485 extern double rnd_prod(double, double), rnd_quot(double, double);
488 # define rounded_product(a, b) a *= b
489 # define rounded_quotient(a, b) a /= b
492 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
493 #define Big1 0xffffffff
500 # define FFFFFFFF ((((unsigned long)0xffff) << 16) | (unsigned long)0xffff)
502 # define FFFFFFFF 0xffffffffUL
509 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
510 * This makes some inner loops simpler and sometimes saves work
511 * during multiplications, but it often seems to make things slightly
512 * slower. Hence the default is now to store 32 bits per Long.
515 #else /* long long available */
517 # define Llong long long
520 # define ULLong unsigned Llong
522 #endif /* NO_LONG_LONG */
524 #ifndef MULTIPLE_THREADS
525 # define ACQUIRE_DTOA_LOCK(n) /*nothing*/
526 # define FREE_DTOA_LOCK(n) /*nothing*/
533 int k
, maxwds
, sign
, wds
;
537 typedef struct Bigint Bigint
;
539 static Bigint
* freelist
[Kmax
+ 1];
541 static Bigint
* Balloc
550 #ifndef Omit_Private_Memory
554 ACQUIRE_DTOA_LOCK(0);
555 /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
556 /* but this case seems very unlikely. */
557 if (k
<= Kmax
&& (rv
= freelist
[k
])) {
558 freelist
[k
] = rv
->next
;
561 #ifdef Omit_Private_Memory
562 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
- 1) * sizeof(ULong
));
564 len
= (sizeof(Bigint
) + (x
- 1) * sizeof(ULong
) + sizeof(double) - 1) /
566 if (k
<= Kmax
&& pmem_next
- private_mem
+ len
<= PRIVATE_mem
) {
567 rv
= (Bigint
*)pmem_next
;
570 rv
= (Bigint
*)MALLOC(len
* sizeof(double));
577 rv
->sign
= rv
->wds
= 0;
596 ACQUIRE_DTOA_LOCK(0);
597 v
->next
= freelist
[v
->k
];
604 #define Bcopy(x, y) \
605 memcpy((char*)&x->sign, (char*)&y->sign, \
606 y->wds * sizeof(Long) + 2 * sizeof(int))
608 static Bigint
* multadd
613 (Bigint
* b
, int m
, int a
) /* multiply by m and add a */
634 y
= *x
* (ULLong
)m
+ carry
;
640 y
= (xi
& 0xffff) * m
+ carry
;
641 z
= (xi
>> 16) * m
+ (y
>> 16);
643 *x
++ = (z
<< 16) + (y
& 0xffff);
652 if (wds
>= b
->maxwds
) {
653 b1
= Balloc(b
->k
+ 1);
666 (s
, nd0
, nd
, y9
) CONST
char* s
;
670 (CONST
char* s
, int nd0
, int nd
, ULong y9
)
678 for (k
= 0, y
= 1; x
> y
; y
<<= 1, k
++);
685 b
->x
[0] = y9
& 0xffff;
686 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
693 b
= multadd(b
, 10, *s
++ - '0');
699 for (; i
< nd
; i
++) {
700 b
= multadd(b
, 10, *s
++ - '0');
707 (x
) register ULong x
;
712 #ifdef PR_HAVE_BUILTIN_BITSCAN32
713 return ((!x
) ? 32 : pr_bitscan_clz32(x
));
717 if (!(x
& 0xffff0000)) {
721 if (!(x
& 0xff000000)) {
725 if (!(x
& 0xf0000000)) {
729 if (!(x
& 0xc0000000)) {
733 if (!(x
& 0x80000000)) {
735 if (!(x
& 0x40000000)) {
740 #endif /* PR_HAVE_BUILTIN_BITSCAN32 */
750 #ifdef PR_HAVE_BUILTIN_BITSCAN32
755 *y
= (x
>> (k
= pr_bitscan_ctz32(x
)));
761 register ULong x
= *y
;
799 #endif /* PR_HAVE_BUILTIN_BITSCAN32 */
823 (Bigint
* a
, Bigint
* b
)
828 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
839 if (a
->wds
< b
->wds
) {
848 if (wc
> a
->maxwds
) {
852 for (x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++) {
861 for (; xb
< xbe
; xc0
++) {
867 z
= *x
++ * (ULLong
)y
+ *xc
+ carry
;
869 *xc
++ = z
& FFFFFFFF
;
876 for (; xb
< xbe
; xb
++, xc0
++) {
877 if (y
= *xb
& 0xffff) {
882 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
884 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
896 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
899 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
906 for (; xb
< xbe
; xc0
++) {
912 z
= *x
++ * y
+ *xc
+ carry
;
921 for (xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
);
928 static Bigint
* pow5mult
936 Bigint
*b1
, *p5
, *p51
;
938 static int p05
[3] = {5, 25, 125};
941 b
= multadd(b
, p05
[i
- 1], 0);
949 #ifdef MULTIPLE_THREADS
950 ACQUIRE_DTOA_LOCK(1);
970 if (!(p51
= p5
->next
)) {
971 #ifdef MULTIPLE_THREADS
972 ACQUIRE_DTOA_LOCK(1);
973 if (!(p51
= p5
->next
)) {
974 p51
= p5
->next
= mult(p5
, p5
);
979 p51
= p5
->next
= mult(p5
, p5
);
988 static Bigint
* lshift
998 ULong
*x
, *x1
, *xe
, z
;
1006 n1
= n
+ b
->wds
+ 1;
1007 for (i
= b
->maxwds
; n1
> i
; i
<<= 1) {
1012 for (i
= 0; i
< n
; i
++) {
1022 *x1
++ = *x
<< k
| z
;
1034 *x1
++ = *x
<< k
& 0xffff | z
;
1056 (Bigint
* a
, Bigint
* b
)
1059 ULong
*xa
, *xa0
, *xb
, *xb0
;
1065 if (i
> 1 && !a
->x
[i
- 1]) {
1066 Bug("cmp called with a->x[a->wds-1] == 0");
1068 if (j
> 1 && !b
->x
[j
- 1]) {
1069 Bug("cmp called with b->x[b->wds-1] == 0");
1080 if (*--xa
!= *--xb
) {
1081 return *xa
< *xb
? -1 : 1;
1095 (Bigint
* a
, Bigint
* b
)
1100 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
1137 y
= (ULLong
)*xa
++ - *xb
++ - borrow
;
1138 borrow
= y
>> 32 & (ULong
)1;
1139 *xc
++ = y
& FFFFFFFF
;
1143 borrow
= y
>> 32 & (ULong
)1;
1144 *xc
++ = y
& FFFFFFFF
;
1149 y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
1150 borrow
= (y
& 0x10000) >> 16;
1151 z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
1152 borrow
= (z
& 0x10000) >> 16;
1156 y
= (*xa
& 0xffff) - borrow
;
1157 borrow
= (y
& 0x10000) >> 16;
1158 z
= (*xa
++ >> 16) - borrow
;
1159 borrow
= (z
& 0x10000) >> 16;
1164 y
= *xa
++ - *xb
++ - borrow
;
1165 borrow
= (y
& 0x10000) >> 16;
1170 borrow
= (y
& 0x10000) >> 16;
1193 L
= (word0(x
) & Exp_mask
) - (P
- 1) * Exp_msk1
;
1194 #ifndef Avoid_Underflow
1195 # ifndef Sudden_Underflow
1204 #ifndef Avoid_Underflow
1205 # ifndef Sudden_Underflow
1207 L
= -L
>> Exp_shift
;
1208 if (L
< Exp_shift
) {
1209 word0(a
) = 0x80000 >> L
;
1214 word1(a
) = L
>= 31 ? 1 : 1 << 31 - L
;
1230 ULong
*xa
, *xa0
, w
, y
, z
;
1236 # define d0 word0(d)
1237 # define d1 word1(d)
1245 Bug("zero y in b2d");
1252 d0
= Exp_1
| y
>> Ebits
- k
;
1253 w
= xa
> xa0
? *--xa
: 0;
1254 d1
= y
<< (32 - Ebits
) + k
| w
>> Ebits
- k
;
1257 z
= xa
> xa0
? *--xa
: 0;
1259 d0
= Exp_1
| y
<< k
| z
>> 32 - k
;
1260 y
= xa
> xa0
? *--xa
: 0;
1261 d1
= z
<< k
| y
>> 32 - k
;
1267 if (k
< Ebits
+ 16) {
1268 z
= xa
> xa0
? *--xa
: 0;
1269 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
1270 w
= xa
> xa0
? *--xa
: 0;
1271 y
= xa
> xa0
? *--xa
: 0;
1272 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
1275 z
= xa
> xa0
? *--xa
: 0;
1276 w
= xa
> xa0
? *--xa
: 0;
1278 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
1279 y
= xa
> xa0
? *--xa
: 0;
1280 d1
= w
<< k
+ 16 | y
<< k
;
1284 word0(d
) = d0
>> 16 | d0
<< 16;
1285 word1(d
) = d1
>> 16 | d1
<< 16;
1295 (dd
, e
, bits
) double dd
;
1298 (double dd
, int* e
, int* bits
)
1305 #ifndef Sudden_Underflow
1314 d0
= word0(d
) >> 16 | word0(d
) << 16;
1315 d1
= word1(d
) >> 16 | word1(d
) << 16;
1317 # define d0 word0(d)
1318 # define d1 word1(d)
1329 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1330 #ifdef Sudden_Underflow
1331 de
= (int)(d0
>> Exp_shift
);
1336 if (de
= (int)(d0
>> Exp_shift
)) {
1342 if (k
= lo0bits(&y
)) {
1343 x
[0] = y
| z
<< 32 - k
;
1348 # ifndef Sudden_Underflow
1351 b
->wds
= (x
[1] = z
) ? 2 : 1;
1355 # ifndef Sudden_Underflow
1363 if (k
= lo0bits(&y
))
1365 x
[0] = y
| z
<< 32 - k
& 0xffff;
1366 x
[1] = z
>> k
- 16 & 0xffff;
1371 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1372 x
[2] = z
>> k
& 0xffff;
1386 Bug("Zero passed to d2b");
1405 #ifndef Sudden_Underflow
1409 *e
= (de
- Bias
- (P
- 1) << 2) + k
;
1410 *bits
= 4 * P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1412 *e
= de
- Bias
- (P
- 1) + k
;
1415 #ifndef Sudden_Underflow
1417 *e
= de
- Bias
- (P
- 1) + 1 + k
;
1419 *bits
= 32 * i
- hi0bits(x
[i
- 1]);
1421 *bits
= (i
+ 2) * 16 - hi0bits(x
[i
]);
1435 (Bigint
* a
, Bigint
* b
)
1441 dval(da
) = b2d(a
, &ka
);
1442 dval(db
) = b2d(b
, &kb
);
1444 k
= ka
- kb
+ 32 * (a
->wds
- b
->wds
);
1446 k
= ka
- kb
+ 16 * (a
->wds
- b
->wds
);
1450 word0(da
) += (k
>> 2) * Exp_msk1
;
1456 word0(db
) += (k
>> 2) * Exp_msk1
;
1463 word0(da
) += k
* Exp_msk1
;
1466 word0(db
) += k
* Exp_msk1
;
1469 return dval(da
) / dval(db
);
1472 static CONST
double tens
[] = {1e0
,
1504 bigtens
[] = {1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1505 static CONST
double tinytens
[] = {1e-16, 1e-32, 1e-64, 1e-128,
1506 # ifdef Avoid_Underflow
1507 9007199254740992. * 9007199254740992.e
-256
1508 /* = 2^106 * 1e-53 */
1513 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1514 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1515 # define Scale_Bit 0x10
1516 # define n_bigtens 5
1519 bigtens
[] = {1e16
, 1e32
, 1e64
};
1520 static CONST
double tinytens
[] = {1e-16, 1e-32, 1e-64};
1521 # define n_bigtens 3
1523 bigtens
[] = {1e16
, 1e32
};
1524 static CONST
double tinytens
[] = {1e-16, 1e-32};
1525 # define n_bigtens 2
1530 # undef INFNAN_CHECK
1536 # define NAN_WORD0 0x7ff80000
1540 # define NAN_WORD1 0
1548 (CONST
char** sp
, char* t
)
1552 CONST
char* s
= *sp
;
1555 if ((c
= *++s
) >= 'A' && c
<= 'Z') {
1569 (rvp
, sp
) double* rvp
;
1572 (double* rvp
, CONST
char** sp
)
1577 int havedig
, udx0
, xshift
;
1580 havedig
= xshift
= 0;
1583 while (c
= *(CONST
unsigned char*)++s
) {
1584 if (c
>= '0' && c
<= '9') {
1586 } else if (c
>= 'a' && c
<= 'f') {
1588 } else if (c
>= 'A' && c
<= 'F') {
1590 } else if (c
<= ' ') {
1591 if (udx0
&& havedig
) {
1596 } else if (/*(*/ c
== ')' && havedig
) {
1600 return; /* invalid form: don't change *sp */
1609 x
[0] = (x
[0] << 4) | (x
[1] >> 28);
1611 x
[1] = (x
[1] << 4) | c
;
1613 if ((x
[0] &= 0xfffff) || x
[1]) {
1614 word0(*rvp
) = Exp_mask
| x
[0];
1618 # endif /*No_Hex_NaN*/
1619 #endif /* INFNAN_CHECK */
1621 PR_IMPLEMENT(double)
1624 (s00
, se
) CONST
char* s00
;
1627 (CONST
char* s00
, char** se
)
1630 #ifdef Avoid_Underflow
1633 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
, e
, e1
, esign
, i
, j
, k
, nd
,
1634 nd0
, nf
, nz
, nz0
, sign
;
1635 CONST
char *s
, *s0
, *s1
;
1636 double aadj
, aadj1
, adj
;
1640 Bigint
*bb
, *bb1
, *bd
, *bd0
, *bs
, *delta
;
1642 int inexact
, oldinexact
;
1644 #ifdef Honor_FLT_ROUNDS
1651 if (!_pr_initialized
) {
1652 _PR_ImplicitInitialization();
1655 sign
= nz0
= nz
= 0;
1657 for (s
= s00
;; s
++) switch (*s
) {
1681 while (*++s
== '0');
1688 for (nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1690 y
= 10 * y
+ c
- '0';
1691 } else if (nd
< 16) {
1692 z
= 10 * z
+ c
- '0';
1696 s1
= localeconv()->decimal_point
;
1717 for (; c
== '0'; c
= *++s
) {
1720 if (c
> '0' && c
<= '9') {
1728 for (; c
>= '0' && c
<= '9'; c
= *++s
) {
1733 for (i
= 1; i
< nz
; i
++)
1736 } else if (nd
<= DBL_DIG
+ 1) {
1741 } else if (nd
<= DBL_DIG
+ 1) {
1749 if (nd
> 64 * 1024) {
1753 if (c
== 'e' || c
== 'E') {
1754 if (!nd
&& !nz
&& !nz0
) {
1765 if (c
>= '0' && c
<= '9') {
1769 if (c
> '0' && c
<= '9') {
1772 while ((c
= *++s
) >= '0' && c
<= '9') {
1773 L
= 10 * L
+ c
- '0';
1775 if (s
- s1
> 8 || L
> 19999)
1776 /* Avoid confusion from exponents
1777 * so large that e might overflow.
1780 e
= 19999; /* safe for 16 bit ints */
1797 /* Check for Nan and Infinity */
1801 if (match(&s
, "nf")) {
1803 if (!match(&s
, "inity")) {
1806 word0(rv
) = 0x7ff00000;
1813 if (match(&s
, "an")) {
1814 word0(rv
) = NAN_WORD0
;
1815 word1(rv
) = NAN_WORD1
;
1817 if (*s
== '(') { /*)*/
1824 #endif /* INFNAN_CHECK */
1833 /* Now we have nd0 digits, starting at s0, followed by a
1834 * decimal point, followed by nd-nd0 digits. The number we're
1835 * after is the integer represented by those digits times
1841 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
1846 oldinexact
= get_inexact();
1849 dval(rv
) = tens
[k
- 9] * dval(rv
) + z
;
1853 #ifndef RND_PRODQUOT
1854 # ifndef Honor_FLT_ROUNDS
1863 if (e
<= Ten_pmax
) {
1865 goto vax_ovfl_check
;
1867 # ifdef Honor_FLT_ROUNDS
1868 /* round correctly FLT_ROUNDS = 2 or 3 */
1874 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1879 if (e
<= Ten_pmax
+ i
) {
1880 /* A fancier test would sometimes let us do
1881 * this for larger i values.
1883 #ifdef Honor_FLT_ROUNDS
1884 /* round correctly FLT_ROUNDS = 2 or 3 */
1891 dval(rv
) *= tens
[i
];
1893 /* VAX exponent range is so narrow we must
1894 * worry about overflow here...
1897 word0(rv
) -= P
* Exp_msk1
;
1898 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1899 if ((word0(rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
1902 word0(rv
) += P
* Exp_msk1
;
1904 /* rv = */ rounded_product(dval(rv
), tens
[e
]);
1909 #ifndef Inaccurate_Divide
1910 else if (e
>= -Ten_pmax
) {
1911 # ifdef Honor_FLT_ROUNDS
1912 /* round correctly FLT_ROUNDS = 2 or 3 */
1918 /* rv = */ rounded_quotient(dval(rv
), tens
[-e
]);
1929 oldinexact
= get_inexact();
1932 # ifdef Avoid_Underflow
1935 # ifdef Honor_FLT_ROUNDS
1936 if ((rounding
= Flt_Rounds
) >= 2) {
1938 rounding
= rounding
== 2 ? 0 : 2;
1939 } else if (rounding
!= 2) {
1944 #endif /*IEEE_Arith*/
1946 /* Get starting approximation = rv * 10**e1 */
1950 dval(rv
) *= tens
[i
];
1953 if (e1
> DBL_MAX_10_EXP
) {
1956 PR_SetError(PR_RANGE_ERROR
, 0);
1958 /* Can't trust HUGE_VAL */
1960 # ifdef Honor_FLT_ROUNDS
1962 case 0: /* toward 0 */
1963 case 3: /* toward -infinity */
1968 word0(rv
) = Exp_mask
;
1971 # else /*Honor_FLT_ROUNDS*/
1972 word0(rv
) = Exp_mask
;
1974 # endif /*Honor_FLT_ROUNDS*/
1976 /* set overflow bit */
1978 dval(rv0
) *= dval(rv0
);
1980 #else /*IEEE_Arith*/
1983 #endif /*IEEE_Arith*/
1990 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
1992 dval(rv
) *= bigtens
[j
];
1994 /* The last multiplication could overflow. */
1995 word0(rv
) -= P
* Exp_msk1
;
1996 dval(rv
) *= bigtens
[j
];
1997 if ((z
= word0(rv
) & Exp_mask
) > Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
2000 if (z
> Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1 - P
)) {
2001 /* set to largest number */
2002 /* (Can't trust DBL_MAX) */
2006 word0(rv
) += P
* Exp_msk1
;
2009 } else if (e1
< 0) {
2012 dval(rv
) /= tens
[i
];
2015 if (e1
>= 1 << n_bigtens
) {
2018 #ifdef Avoid_Underflow
2019 if (e1
& Scale_Bit
) {
2022 for (j
= 0; e1
> 0; j
++, e1
>>= 1)
2024 dval(rv
) *= tinytens
[j
];
2027 (j
= 2 * P
+ 1 - ((word0(rv
) & Exp_mask
) >> Exp_shift
)) > 0) {
2028 /* scaled rv is denormal; zap j low bits */
2032 word0(rv
) = (P
+ 2) * Exp_msk1
;
2034 word0(rv
) &= 0xffffffff << j
- 32;
2037 word1(rv
) &= 0xffffffff << j
;
2041 for (j
= 0; e1
> 1; j
++, e1
>>= 1)
2043 dval(rv
) *= tinytens
[j
];
2045 /* The last multiplication could underflow. */
2046 dval(rv0
) = dval(rv
);
2047 dval(rv
) *= tinytens
[j
];
2049 dval(rv
) = 2. * dval(rv0
);
2050 dval(rv
) *= tinytens
[j
];
2056 PR_SetError(PR_RANGE_ERROR
, 0);
2063 #ifndef Avoid_Underflow
2066 /* The refinement below will clean
2067 * this approximation up.
2074 /* Now the hard part -- adjusting rv to the correct value.*/
2076 /* Put digits into bd: true value = bd * 10^e */
2078 bd0
= s2b(s0
, nd0
, nd
, y
);
2081 bd
= Balloc(bd0
->k
);
2083 bb
= d2b(dval(rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
2099 #ifdef Honor_FLT_ROUNDS
2100 if (rounding
!= 1) {
2104 #ifdef Avoid_Underflow
2106 i
= j
+ bbbits
- 1; /* logb(rv) */
2107 if (i
< Emin
) { /* denormal */
2112 #else /*Avoid_Underflow*/
2113 # ifdef Sudden_Underflow
2115 j
= 1 + 4 * P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
2119 # else /*Sudden_Underflow*/
2121 i
= j
+ bbbits
- 1; /* logb(rv) */
2122 if (i
< Emin
) { /* denormal */
2127 # endif /*Sudden_Underflow*/
2128 #endif /*Avoid_Underflow*/
2131 #ifdef Avoid_Underflow
2134 i
= bb2
< bd2
? bb2
: bd2
;
2144 bs
= pow5mult(bs
, bb5
);
2150 bb
= lshift(bb
, bb2
);
2153 bd
= pow5mult(bd
, bd5
);
2156 bd
= lshift(bd
, bd2
);
2159 bs
= lshift(bs
, bs2
);
2161 delta
= diff(bb
, bd
);
2162 dsign
= delta
->sign
;
2165 #ifdef Honor_FLT_ROUNDS
2166 if (rounding
!= 1) {
2168 /* Error is less than an ulp */
2169 if (!delta
->x
[0] && delta
->wds
<= 1) {
2181 } else if (!dsign
) {
2183 if (!word1(rv
) && !(word0(rv
) & Frac_mask
)) {
2184 y
= word0(rv
) & Exp_mask
;
2185 # ifdef Avoid_Underflow
2186 if (!scale
|| y
> 2 * P
* Exp_msk1
)
2191 delta
= lshift(delta
, Log2P
);
2192 if (cmp(delta
, bs
) <= 0) {
2198 # ifdef Avoid_Underflow
2199 if (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
) {
2200 word0(adj
) += (2 * P
+ 1) * Exp_msk1
- y
;
2203 # ifdef Sudden_Underflow
2204 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
) {
2205 word0(rv
) += P
* Exp_msk1
;
2206 dval(rv
) += adj
* ulp(dval(rv
));
2207 word0(rv
) -= P
* Exp_msk1
;
2209 # endif /*Sudden_Underflow*/
2210 # endif /*Avoid_Underflow*/
2211 dval(rv
) += adj
* ulp(dval(rv
));
2215 adj
= ratio(delta
, bs
);
2219 if (adj
<= 0x7ffffffe) {
2220 /* adj = rounding ? ceil(adj) : floor(adj); */
2223 if (!((rounding
>> 1) ^ dsign
)) {
2229 # ifdef Avoid_Underflow
2230 if (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
) {
2231 word0(adj
) += (2 * P
+ 1) * Exp_msk1
- y
;
2234 # ifdef Sudden_Underflow
2235 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
) {
2236 word0(rv
) += P
* Exp_msk1
;
2237 adj
*= ulp(dval(rv
));
2243 word0(rv
) -= P
* Exp_msk1
;
2246 # endif /*Sudden_Underflow*/
2247 # endif /*Avoid_Underflow*/
2248 adj
*= ulp(dval(rv
));
2256 #endif /*Honor_FLT_ROUNDS*/
2259 /* Error is less than half an ulp -- check for
2260 * special case of mantissa a power of two.
2262 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
2264 # ifdef Avoid_Underflow
2265 || (word0(rv
) & Exp_mask
) <= (2 * P
+ 1) * Exp_msk1
2267 || (word0(rv
) & Exp_mask
) <= Exp_msk1
2272 if (!delta
->x
[0] && delta
->wds
<= 1) {
2278 if (!delta
->x
[0] && delta
->wds
<= 1) {
2285 delta
= lshift(delta
, Log2P
);
2286 if (cmp(delta
, bs
) > 0) {
2292 /* exactly half-way between */
2294 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
&&
2297 #ifdef Avoid_Underflow
2298 (scale
&& (y
= word0(rv
) & Exp_mask
) <= 2 * P
* Exp_msk1
)
2300 (0xffffffff << (2 * P
+ 1 - (y
>> Exp_shift
))))
2304 /*boundary case -- increment exponent*/
2305 word0(rv
) = (word0(rv
) & Exp_mask
) + Exp_msk1
2311 #ifdef Avoid_Underflow
2316 } else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
2318 /* boundary case -- decrement exponent */
2319 #ifdef Sudden_Underflow /*{{*/
2320 L
= word0(rv
) & Exp_mask
;
2324 # ifdef Avoid_Underflow
2325 if (L
<= (scale
? (2 * P
+ 1) * Exp_msk1
: Exp_msk1
))
2328 # endif /*Avoid_Underflow*/
2332 #else /*Sudden_Underflow}{*/
2333 # ifdef Avoid_Underflow
2335 L
= word0(rv
) & Exp_mask
;
2336 if (L
<= (2 * P
+ 1) * Exp_msk1
) {
2337 if (L
> (P
+ 2) * Exp_msk1
)
2338 /* round even ==> */
2343 /* rv = smallest denormal */
2347 # endif /*Avoid_Underflow*/
2348 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
2349 #endif /*Sudden_Underflow}}*/
2350 word0(rv
) = L
| Bndry_mask1
;
2351 word1(rv
) = 0xffffffff;
2358 #ifndef ROUND_BIASED
2359 if (!(word1(rv
) & LSB
)) {
2364 dval(rv
) += ulp(dval(rv
));
2366 #ifndef ROUND_BIASED
2368 dval(rv
) -= ulp(dval(rv
));
2369 # ifndef Sudden_Underflow
2375 # ifdef Avoid_Underflow
2381 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
2384 } else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
2385 #ifndef Sudden_Underflow
2386 if (word1(rv
) == Tiny1
&& !word0(rv
)) {
2393 /* special case -- power of FLT_RADIX to be */
2394 /* rounded down... */
2396 if (aadj
< 2. / FLT_RADIX
) {
2397 aadj
= 1. / FLT_RADIX
;
2405 aadj1
= dsign
? aadj
: -aadj
;
2406 #ifdef Check_FLT_ROUNDS
2408 case 2: /* towards +infinity */
2411 case 0: /* towards 0 */
2412 case 3: /* towards -infinity */
2416 if (Flt_Rounds
== 0) {
2419 #endif /*Check_FLT_ROUNDS*/
2421 y
= word0(rv
) & Exp_mask
;
2423 /* Check for overflow */
2425 if (y
== Exp_msk1
* (DBL_MAX_EXP
+ Bias
- 1)) {
2426 dval(rv0
) = dval(rv
);
2427 word0(rv
) -= P
* Exp_msk1
;
2428 adj
= aadj1
* ulp(dval(rv
));
2430 if ((word0(rv
) & Exp_mask
) >= Exp_msk1
* (DBL_MAX_EXP
+ Bias
- P
)) {
2431 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
) {
2438 word0(rv
) += P
* Exp_msk1
;
2441 #ifdef Avoid_Underflow
2442 if (scale
&& y
<= 2 * P
* Exp_msk1
) {
2443 if (aadj
<= 0x7fffffff) {
2444 if ((z
= aadj
) <= 0) {
2448 aadj1
= dsign
? aadj
: -aadj
;
2450 dval(aadj2
) = aadj1
;
2451 word0(aadj2
) += (2 * P
+ 1) * Exp_msk1
- y
;
2452 aadj1
= dval(aadj2
);
2454 adj
= aadj1
* ulp(dval(rv
));
2457 # ifdef Sudden_Underflow
2458 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
) {
2459 dval(rv0
) = dval(rv
);
2460 word0(rv
) += P
* Exp_msk1
;
2461 adj
= aadj1
* ulp(dval(rv
));
2464 if ((word0(rv
) & Exp_mask
) < P
* Exp_msk1
)
2466 if ((word0(rv
) & Exp_mask
) <= P
* Exp_msk1
)
2469 if (word0(rv0
) == Tiny0
&& word1(rv0
) == Tiny1
) {
2476 word0(rv
) -= P
* Exp_msk1
;
2479 adj
= aadj1
* ulp(dval(rv
));
2482 # else /*Sudden_Underflow*/
2483 /* Compute adj so that the IEEE rounding rules will
2484 * correctly round rv + adj in some half-way cases.
2485 * If rv * ulp(rv) is denormalized (i.e.,
2486 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2487 * trouble from bits lost to denormalization;
2488 * example: 1.2e-307 .
2490 if (y
<= (P
- 1) * Exp_msk1
&& aadj
> 1.) {
2491 aadj1
= (double)(int)(aadj
+ 0.5);
2496 adj
= aadj1
* ulp(dval(rv
));
2498 # endif /*Sudden_Underflow*/
2499 #endif /*Avoid_Underflow*/
2501 z
= word0(rv
) & Exp_mask
;
2503 # ifdef Avoid_Underflow
2507 /* Can we stop now? */
2510 /* The tolerances below are conservative. */
2511 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
2512 if (aadj
< .4999999 || aadj
> .5000001) {
2515 } else if (aadj
< .4999999 / FLT_RADIX
) {
2529 word0(rv0
) = Exp_1
+ (70 << Exp_shift
);
2533 } else if (!oldinexact
) {
2537 #ifdef Avoid_Underflow
2539 word0(rv0
) = Exp_1
- 2 * P
* Exp_msk1
;
2541 dval(rv
) *= dval(rv0
);
2543 /* try to avoid the bug of testing an 8087 register value */
2544 if (word0(rv
) == 0 && word1(rv
) == 0) {
2545 PR_SetError(PR_RANGE_ERROR
, 0);
2549 #endif /* Avoid_Underflow */
2551 if (inexact
&& !(word0(rv
) & Exp_mask
)) {
2552 /* set underflow bit */
2554 dval(rv0
) *= dval(rv0
);
2562 ret
: if (se
) { *se
= (char*)s
; }
2563 return sign
? -dval(rv
) : dval(rv
);
2571 (Bigint
* b
, Bigint
* S
)
2575 ULong
*bx
, *bxe
, q
, *sx
, *sxe
;
2577 ULLong borrow
, carry
, y
, ys
;
2579 ULong borrow
, carry
, y
, ys
;
2587 /*debug*/ if (b
->wds
> n
)
2589 Bug("oversize b in quorem");
2599 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
2601 /*debug*/ if (q
> 9)
2603 Bug("oversized quotient in quorem");
2611 ys
= *sx
++ * (ULLong
)q
+ carry
;
2613 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2614 borrow
= y
>> 32 & (ULong
)1;
2615 *bx
++ = y
& FFFFFFFF
;
2619 ys
= (si
& 0xffff) * q
+ carry
;
2620 zs
= (si
>> 16) * q
+ (ys
>> 16);
2622 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2623 borrow
= (y
& 0x10000) >> 16;
2624 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2625 borrow
= (z
& 0x10000) >> 16;
2628 ys
= *sx
++ * q
+ carry
;
2630 y
= *bx
- (ys
& 0xffff) - borrow
;
2631 borrow
= (y
& 0x10000) >> 16;
2635 } while (sx
<= sxe
);
2638 while (--bxe
> bx
&& !*bxe
) {
2644 if (cmp(b
, S
) >= 0) {
2654 y
= *bx
- (ys
& FFFFFFFF
) - borrow
;
2655 borrow
= y
>> 32 & (ULong
)1;
2656 *bx
++ = y
& FFFFFFFF
;
2660 ys
= (si
& 0xffff) + carry
;
2661 zs
= (si
>> 16) + (ys
>> 16);
2663 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
2664 borrow
= (y
& 0x10000) >> 16;
2665 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
2666 borrow
= (z
& 0x10000) >> 16;
2671 y
= *bx
- (ys
& 0xffff) - borrow
;
2672 borrow
= (y
& 0x10000) >> 16;
2676 } while (sx
<= sxe
);
2680 while (--bxe
> bx
&& !*bxe
) {
2689 #ifndef MULTIPLE_THREADS
2690 static char* dtoa_result
;
2704 for (k
= 0; sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
<= i
; j
<<= 1) {
2707 r
= (int*)Balloc(k
);
2710 #ifndef MULTIPLE_THREADS
2718 nrv_alloc(s
, rve
, n
)
2722 nrv_alloc(char* s
, char** rve
, int n
)
2727 t
= rv
= rv_alloc(n
);
2737 /* freedtoa(s) must be used to free values s returned by dtoa
2738 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2739 * but for consistency with earlier versions of dtoa, it is optional
2740 * when MULTIPLE_THREADS is not defined.
2745 freedtoa(s
) char* s
;
2750 Bigint
* b
= (Bigint
*)((int*)s
- 1);
2751 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
2753 #ifndef MULTIPLE_THREADS
2754 if (s
== dtoa_result
) {
2760 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2762 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2763 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2766 * 1. Rather than iterating, we use a simple numeric overestimate
2767 * to determine k = floor(log10(d)). We scale relevant
2768 * quantities using O(log2(k)) rather than O(k) multiplications.
2769 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2770 * try to generate digits strictly left to right. Instead, we
2771 * compute with fewer bits and propagate the carry if necessary
2772 * when rounding the final digit up. This is often faster.
2773 * 3. Under the assumption that input will be rounded nearest,
2774 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2775 * That is, we allow equality in stopping tests when the
2776 * round-nearest rule will give the same floating-point value
2777 * as would satisfaction of the stopping test with strict
2779 * 4. We remove common factors of powers of 2 from relevant
2781 * 5. When converting floating-point integers less than 1e16,
2782 * we use floating-point arithmetic rather than resorting
2783 * to multiple-precision integers.
2784 * 6. When asked to produce fewer than 15 digits, we first try
2785 * to get by with floating-point arithmetic; we resort to
2786 * multiple-precision integer arithmetic only if we cannot
2787 * guarantee that the floating-point calculation has given
2788 * the correctly rounded result. For k requested digits and
2789 * "uniformly" distributed input, the probability is
2790 * something like 10^(k-15) that we must resort to the Long
2796 (dd
, mode
, ndigits
, decpt
, sign
, rve
)
2798 int mode
, ndigits
, *decpt
, *sign
;
2801 (double dd
, int mode
, int ndigits
, int* decpt
, int* sign
, char** rve
)
2804 /* Arguments ndigits, decpt, sign are similar to those
2805 of ecvt and fcvt; trailing zeros are suppressed from
2806 the returned string. If not null, *rve is set to point
2807 to the end of the return value. If d is +-Infinity or NaN,
2808 then *decpt is set to 9999.
2811 0 ==> shortest string that yields d when read in
2812 and rounded to nearest.
2813 1 ==> like 0, but with Steele & White stopping rule;
2814 e.g. with IEEE P754 arithmetic , mode 0 gives
2815 1e23 whereas mode 1 gives 9.999999999999999e22.
2816 2 ==> max(1,ndigits) significant digits. This gives a
2817 return value similar to that of ecvt, except
2818 that trailing zeros are suppressed.
2819 3 ==> through ndigits past the decimal point. This
2820 gives a return value similar to that from fcvt,
2821 except that trailing zeros are suppressed, and
2822 ndigits can be negative.
2823 4,5 ==> similar to 2 and 3, respectively, but (in
2824 round-nearest mode) with the tests of mode 0 to
2825 possibly return a shorter string that rounds to d.
2826 With IEEE arithmetic and compilation with
2827 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2828 as modes 2 and 3 when FLT_ROUNDS != 1.
2829 6-9 ==> Debugging modes similar to mode - 4: don't try
2830 fast floating-point estimate (if applicable).
2832 Values of mode other than 0-9 are treated as mode 0.
2834 Sufficient space is allocated to the return value
2835 to hold the suppressed trailing zeros.
2838 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
, ilim0
, ilim1
, j
, j1
, k
, k0
,
2839 k_check
, leftright
, m2
, m5
, s2
, s5
, spec_case
, try_quick
;
2841 #ifndef Sudden_Underflow
2845 Bigint
*b
, *b1
, *delta
, *mlo
, *mhi
, *S
;
2849 #ifdef Honor_FLT_ROUNDS
2853 int inexact
, oldinexact
;
2856 #ifndef MULTIPLE_THREADS
2858 freedtoa(dtoa_result
);
2864 if (word0(d
) & Sign_bit
) {
2865 /* set sign for everything, including 0's and NaNs */
2867 word0(d
) &= ~Sign_bit
; /* clear sign bit */
2872 #if defined(IEEE_Arith) + defined(VAX)
2874 if ((word0(d
) & Exp_mask
) == Exp_mask
)
2876 if (word0(d
) == 0x8000)
2879 /* Infinity or NaN */
2882 if (!word1(d
) && !(word0(d
) & 0xfffff)) {
2883 return nrv_alloc("Infinity", rve
, 8);
2886 return nrv_alloc("NaN", rve
, 3);
2890 dval(d
) += 0; /* normalize */
2894 return nrv_alloc("0", rve
, 1);
2898 try_quick
= oldinexact
= get_inexact();
2901 #ifdef Honor_FLT_ROUNDS
2902 if ((rounding
= Flt_Rounds
) >= 2) {
2904 rounding
= rounding
== 2 ? 0 : 2;
2905 } else if (rounding
!= 2) {
2911 b
= d2b(dval(d
), &be
, &bbits
);
2912 #ifdef Sudden_Underflow
2913 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
));
2915 if (i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
))) {
2918 word0(d2
) &= Frac_mask1
;
2919 word0(d2
) |= Exp_11
;
2921 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
)) {
2926 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2927 * log10(x) = log(x) / log(10)
2928 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2929 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2931 * This suggests computing an approximation k to log10(d) by
2933 * k = (i - Bias)*0.301029995663981
2934 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2936 * We want k to be too large rather than too small.
2937 * The error in the first-order Taylor series approximation
2938 * is in our favor, so we just round up the constant enough
2939 * to compensate for any error in the multiplication of
2940 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2941 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2942 * adding 1e-13 to the constant term more than suffices.
2943 * Hence we adjust the constant term to 0.1760912590558.
2944 * (We could get a more accurate k by invoking log10,
2945 * but this is probably not worthwhile.)
2953 #ifndef Sudden_Underflow
2957 /* d is denormalized */
2959 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
2960 x
= i
> 32 ? word0(d
) << 64 - i
| word1(d
) >> i
- 32 : word1(d
) << 32 - i
;
2962 word0(d2
) -= 31 * Exp_msk1
; /* adjust exponent */
2963 i
-= (Bias
+ (P
- 1) - 1) + 1;
2967 ds
= (dval(d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 +
2968 i
* 0.301029995663981;
2970 if (ds
< 0. && ds
!= k
) {
2971 k
--; /* want k = floor(ds) */
2974 if (k
>= 0 && k
<= Ten_pmax
) {
2975 if (dval(d
) < tens
[k
]) {
2997 if (mode
< 0 || mode
> 9) {
3002 # ifdef Check_FLT_ROUNDS
3003 try_quick
= Rounding
== 1;
3007 #endif /*SET_INEXACT*/
3028 ilim
= ilim1
= i
= ndigits
;
3034 i
= ndigits
+ k
+ 1;
3041 s
= s0
= rv_alloc(i
);
3043 #ifdef Honor_FLT_ROUNDS
3044 if (mode
> 1 && rounding
!= 1) {
3049 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
3050 /* Try to get by with floating-point arithmetic. */
3056 ieps
= 2; /* conservative */
3061 /* prevent overflows */
3063 dval(d
) /= bigtens
[n_bigtens
- 1];
3066 for (; j
; j
>>= 1, i
++)
3072 } else if (j1
= -k
) {
3073 dval(d
) *= tens
[j1
& 0xf];
3074 for (j
= j1
>> 4; j
; j
>>= 1, i
++)
3077 dval(d
) *= bigtens
[i
];
3080 if (k_check
&& dval(d
) < 1. && ilim
> 0) {
3089 dval(eps
) = ieps
* dval(d
) + 7.;
3090 word0(eps
) -= (P
- 1) * Exp_msk1
;
3094 if (dval(d
) > dval(eps
)) {
3097 if (dval(d
) < -dval(eps
)) {
3102 #ifndef No_leftright
3104 /* Use Steele & White method of only
3105 * generating digits needed.
3107 dval(eps
) = 0.5 / tens
[ilim
- 1] - dval(eps
);
3111 *s
++ = '0' + (int)L
;
3112 if (dval(d
) < dval(eps
)) {
3115 if (1. - dval(d
) < dval(eps
)) {
3126 /* Generate ilim digits, then fix them up. */
3127 dval(eps
) *= tens
[ilim
- 1];
3128 for (i
= 1;; i
++, dval(d
) *= 10.) {
3129 L
= (Long
)(dval(d
));
3130 if (!(dval(d
) -= L
)) {
3133 *s
++ = '0' + (int)L
;
3135 if (dval(d
) > 0.5 + dval(eps
)) {
3137 } else if (dval(d
) < 0.5 - dval(eps
)) {
3138 while (*--s
== '0');
3145 #ifndef No_leftright
3155 /* Do we have a "small" integer? */
3157 if (be
>= 0 && k
<= Int_max
) {
3160 if (ndigits
< 0 && ilim
<= 0) {
3162 if (ilim
< 0 || dval(d
) <= 5 * ds
) {
3167 for (i
= 1; i
<= k
+ 1; i
++, dval(d
) *= 10.) {
3168 L
= (Long
)(dval(d
) / ds
);
3170 #ifdef Check_FLT_ROUNDS
3171 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3177 *s
++ = '0' + (int)L
;
3185 #ifdef Honor_FLT_ROUNDS
3186 if (mode
> 1) switch (rounding
) {
3194 if (dval(d
) > ds
|| dval(d
) == ds
&& L
& 1) {
3215 #ifndef Sudden_Underflow
3216 denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) :
3219 1 + 4 * P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
3227 if (m2
> 0 && s2
> 0) {
3228 i
= m2
< s2
? m2
: s2
;
3236 mhi
= pow5mult(mhi
, m5
);
3245 b
= pow5mult(b
, b5
);
3250 S
= pow5mult(S
, s5
);
3253 /* Check for special case that d is a normalized power of 2. */
3256 if ((mode
< 2 || leftright
)
3257 #ifdef Honor_FLT_ROUNDS
3261 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
3262 #ifndef Sudden_Underflow
3263 && word0(d
) & (Exp_mask
& ~Exp_msk1
)
3266 /* The special case */
3273 /* Arrange for convenient computation of quotients:
3274 * shift left if necessary so divisor has 4 leading 0 bits.
3276 * Perhaps we should just compute leading 28 bits of S once
3277 * and for all and pass them and a shift to quorem, so it
3278 * can do shifts and ors to compute the numerator for q.
3281 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
- 1]) : 1) + s2
) & 0x1f) {
3285 if (i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
- 1]) : 1) + s2
) & 0xf) {
3307 if (cmp(b
, S
) < 0) {
3309 b
= multadd(b
, 10, 0); /* we botched the k estimate */
3311 mhi
= multadd(mhi
, 10, 0);
3316 if (ilim
<= 0 && (mode
== 3 || mode
== 5)) {
3317 if (ilim
< 0 || cmp(b
, S
= multadd(S
, 5, 0)) <= 0) {
3318 /* no digits, fcvt style */
3330 mhi
= lshift(mhi
, m2
);
3333 /* Compute mlo -- check for special case
3334 * that d is a normalized power of 2.
3339 mhi
= Balloc(mhi
->k
);
3341 mhi
= lshift(mhi
, Log2P
);
3345 dig
= quorem(b
, S
) + '0';
3346 /* Do we yet have the shortest decimal string
3347 * that will round to d?
3350 delta
= diff(S
, mhi
);
3351 j1
= delta
->sign
? 1 : cmp(b
, delta
);
3353 #ifndef ROUND_BIASED
3354 if (j1
== 0 && mode
!= 1 && !(word1(d
) & 1)
3355 # ifdef Honor_FLT_ROUNDS
3366 else if (!b
->x
[0] && b
->wds
<= 1) {
3374 if (j
< 0 || j
== 0 && mode
!= 1
3375 #ifndef ROUND_BIASED
3379 if (!b
->x
[0] && b
->wds
<= 1) {
3385 #ifdef Honor_FLT_ROUNDS
3386 if (mode
> 1) switch (rounding
) {
3392 #endif /*Honor_FLT_ROUNDS*/
3396 if ((j1
> 0 || j1
== 0 && dig
& 1) && dig
++ == '9') {
3405 #ifdef Honor_FLT_ROUNDS
3410 if (dig
== '9') { /* possible if i == 1 */
3418 #ifdef Honor_FLT_ROUNDS
3425 b
= multadd(b
, 10, 0);
3427 mlo
= mhi
= multadd(mhi
, 10, 0);
3429 mlo
= multadd(mlo
, 10, 0);
3430 mhi
= multadd(mhi
, 10, 0);
3435 *s
++ = dig
= quorem(b
, S
) + '0';
3436 if (!b
->x
[0] && b
->wds
<= 1) {
3445 b
= multadd(b
, 10, 0);
3448 /* Round off last digit */
3450 #ifdef Honor_FLT_ROUNDS
3460 if (j
> 0 || j
== 0 && dig
& 1) {
3470 #ifdef Honor_FLT_ROUNDS
3473 while (*--s
== '0');
3478 if (mlo
&& mlo
!= mhi
) {
3487 word0(d
) = Exp_1
+ (70 << Exp_shift
);
3492 else if (!oldinexact
) {
3508 PR_IMPLEMENT(PRStatus
)
3509 PR_dtoa(PRFloat64 d
, PRIntn mode
, PRIntn ndigits
, PRIntn
* decpt
, PRIntn
* sign
,
3510 char** rve
, char* buf
, PRSize bufsize
) {
3513 PRStatus rv
= PR_FAILURE
;
3515 if (!_pr_initialized
) {
3516 _PR_ImplicitInitialization();
3519 if (mode
< 0 || mode
> 3) {
3520 PR_SetError(PR_INVALID_ARGUMENT_ERROR
, 0);
3523 result
= dtoa(d
, mode
, ndigits
, decpt
, sign
, rve
);
3525 PR_SetError(PR_OUT_OF_MEMORY_ERROR
, 0);
3528 resultlen
= strlen(result
) + 1;
3529 if (bufsize
< resultlen
) {
3530 PR_SetError(PR_BUFFER_OVERFLOW_ERROR
, 0);
3532 memcpy(buf
, result
, resultlen
);
3534 *rve
= buf
+ (*rve
- result
);
3543 ** conversion routines for floating point
3544 ** prcsn - number of digits of precision to generate floating
3546 ** This should be reparameterized so that you can send in a
3547 ** prcn for the positive and negative ranges. For now,
3548 ** conform to the ECMA JavaScript spec which says numbers
3549 ** less than 1e-6 are in scientific notation.
3550 ** Also, the ECMA spec says that there should always be a
3551 ** '+' or '-' after the 'e' in scientific notation
3554 PR_cnvtf(char* buf
, int bufsz
, int prcsn
, double dfval
) {
3555 PRIntn decpt
, sign
, numdigits
;
3562 /* If anything fails, we store an empty string in 'buf' */
3563 num
= (char*)PR_MALLOC(bufsz
);
3568 /* XXX Why use mode 1? */
3569 if (PR_dtoa(dval(fval
), 1, prcsn
, &decpt
, &sign
, &endnum
, num
, bufsz
) ==
3574 numdigits
= endnum
- num
;
3577 if (sign
&& !(word0(fval
) == Sign_bit
&& word1(fval
) == 0) &&
3578 !((word0(fval
) & Exp_mask
) == Exp_mask
&&
3579 (word1(fval
) || (word0(fval
) & 0xfffff)))) {
3583 if (decpt
== 9999) {
3584 while ((*bufp
++ = *nump
++) != 0) {
3585 } /* nothing to execute */
3589 if (decpt
> (prcsn
+ 1) || decpt
< -(prcsn
- 1) || decpt
< -5) {
3591 if (numdigits
!= 1) {
3595 while (*nump
!= '\0') {
3599 PR_snprintf(bufp
, bufsz
- (bufp
- buf
), "%+d", decpt
- 1);
3600 } else if (decpt
>= 0) {
3605 if (*nump
!= '\0') {
3612 if (*nump
!= '\0') {
3614 while (*nump
!= '\0') {
3619 } else if (decpt
< 0) {
3626 while (*nump
!= '\0') {