1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991 by AT&T.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* Please send bug reports to
22 AT&T Bell Laboratories, Room 2C-463
24 Murray Hill, NJ 07974-2070
26 dmg@research.att.com or research!dmg
29 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
31 * This strtod returns a nearest machine number to the input decimal
32 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
33 * broken by the IEEE round-even rule. Otherwise ties are broken by
34 * biased rounding (add half and chop).
36 * Inspired loosely by William D. Clinger's paper "How to Read Floating
37 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
41 * 1. We only require IEEE, IBM, or VAX double-precision
42 * arithmetic (not IEEE double-extended).
43 * 2. We get by with floating-point arithmetic in a case that
44 * Clinger missed -- when we're computing d * 10^n
45 * for a small integer d and the integer n is not too
46 * much larger than 22 (the maximum integer k for which
47 * we can represent 10^k exactly), we may be able to
48 * compute (d*10^k) * 10^(e-k) with just one roundoff.
49 * 3. Rather than a bit-at-a-time adjustment of the binary
50 * result in the hard case, we use floating-point
51 * arithmetic to determine the adjustment to within
52 * one bit; only in really hard cases do we need to
53 * compute a second residual.
54 * 4. Because of 3., we don't need a large table of powers of 10
55 * for ten-to-e (just some small tables, e.g. of 10^k
60 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
61 * significant byte has the lowest address.
62 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
63 * significant byte has the lowest address.
64 * #define Long int on machines with 32-bit ints and 64-bit longs.
65 * #define Sudden_Underflow for IEEE-format machines without gradual
66 * underflow (i.e., that flush to zero on underflow).
67 * #define IBM for IBM mainframe-style floating-point arithmetic.
68 * #define VAX for VAX-style floating-point arithmetic.
69 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
70 * #define No_leftright to omit left-right logic in fast floating-point
71 * computation of dtoa.
72 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
73 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
74 * that use extended-precision instructions to compute rounded
75 * products and quotients) with IBM.
76 * #define ROUND_BIASED for IEEE-format with biased rounding.
77 * #define Inaccurate_Divide for IEEE-format with correctly rounded
78 * products but inaccurate quotients, e.g., for Intel i860.
79 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
80 * integer arithmetic. Whether this speeds things up or slows things
81 * down depends on the machine and the number being converted.
82 * #define KR_headers for old-style C function headers.
83 * #define Bad_float_h if your system lacks a float.h or if it does not
84 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
85 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
86 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
87 * if memory is available and otherwise does something you deem
88 * appropriate. If MALLOC is undefined, malloc will be invoked
89 * directly -- and assumed always to succeed.
92 #include "hphp/runtime/base/zend/zend_strtod.h"
93 #include "hphp/runtime/base/util/exceptions.h"
94 #include "hphp/util/thread_local.h"
97 ///////////////////////////////////////////////////////////////////////////////
99 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
100 # if defined(__LITTLE_ENDIAN__)
101 # undef WORDS_BIGENDIAN
103 # if defined(__BIG_ENDIAN__)
104 # define WORDS_BIGENDIAN
109 #ifdef WORDS_BIGENDIAN
110 #define IEEE_BIG_ENDIAN
112 #define IEEE_LITTLE_ENDIAN
115 #if defined(__arm__) && !defined(__VFP_FP__)
117 * * Although the CPU is little endian the FP has different
118 * * byte and word endianness. The byte order is still little endian
119 * * but the word order is big endian.
121 #define IEEE_BIG_ENDIAN
122 #undef IEEE_LITTLE_ENDIAN
129 #if defined(_MSC_VER)
130 #define int32_t __int32
131 #define uint32_t unsigned __int32
132 #define IEEE_LITTLE_ENDIAN
136 #define ULong uint32_t
140 extern char *MALLOC();
142 extern void *MALLOC(size_t);
145 #define MALLOC malloc
152 #ifdef IEEE_BIG_ENDIAN
153 #define IEEE_ARITHMETIC
155 #ifdef IEEE_LITTLE_ENDIAN
156 #define IEEE_ARITHMETIC
159 #ifdef IEEE_ARITHMETIC
161 #define DBL_MAX_10_EXP 308
162 #define DBL_MAX_EXP 1024
165 #define DBL_MAX 1.7976931348623157e+308
170 #define DBL_MAX_10_EXP 75
171 #define DBL_MAX_EXP 63
174 #define DBL_MAX 7.2370055773322621e+75
179 #define DBL_MAX_10_EXP 38
180 #define DBL_MAX_EXP 127
183 #define DBL_MAX 1.7014118346046923e+38
188 #define LONG_MAX 2147483647
199 #define CONST /* blank */
205 #ifdef Unsigned_Shifts
206 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
208 #define Sign_Extend(a,b) /*no-op*/
211 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
213 Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN
, VAX
, or
214 IBM should be defined
.
221 #define value(x) ((x).d)
222 #ifdef IEEE_LITTLE_ENDIAN
223 #define word0(x) ((x).ul[1])
224 #define word1(x) ((x).ul[0])
226 #define word0(x) ((x).ul[0])
227 #define word1(x) ((x).ul[1])
230 /* The following definition of Storeinc is appropriate for MIPS processors.
231 * An alternative that might be better on some machines is
232 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
234 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
235 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
236 ((unsigned short *)a)[0] = (unsigned short)c, a++)
238 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
239 ((unsigned short *)a)[1] = (unsigned short)c, a++)
242 /* #define P DBL_MANT_DIG */
243 /* Ten_pmax = floor(P*log(2)/log(5)) */
244 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
245 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
246 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
248 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
250 #define Exp_shift1 20
251 #define Exp_msk1 0x100000
252 #define Exp_msk11 0x100000
253 #define Exp_mask 0x7ff00000
258 #define Exp_1 0x3ff00000
259 #define Exp_11 0x3ff00000
261 #define Frac_mask 0xfffff
262 #define Frac_mask1 0xfffff
265 #define Bndry_mask 0xfffff
266 #define Bndry_mask1 0xfffff
268 #define Sign_bit 0x80000000
274 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
276 #undef Sudden_Underflow
277 #define Sudden_Underflow
280 #define Exp_shift1 24
281 #define Exp_msk1 0x1000000
282 #define Exp_msk11 0x1000000
283 #define Exp_mask 0x7f000000
286 #define Exp_1 0x41000000
287 #define Exp_11 0x41000000
288 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
289 #define Frac_mask 0xffffff
290 #define Frac_mask1 0xffffff
293 #define Bndry_mask 0xefffff
294 #define Bndry_mask1 0xffffff
296 #define Sign_bit 0x80000000
298 #define Tiny0 0x100000
305 #define Exp_msk1 0x80
306 #define Exp_msk11 0x800000
307 #define Exp_mask 0x7f80
310 #define Exp_1 0x40800000
311 #define Exp_11 0x4080
313 #define Frac_mask 0x7fffff
314 #define Frac_mask1 0xffff007f
317 #define Bndry_mask 0xffff007f
318 #define Bndry_mask1 0xffff007f
320 #define Sign_bit 0x8000
334 #define rounded_product(a,b) a = rnd_prod(a, b)
335 #define rounded_quotient(a,b) a = rnd_quot(a, b)
337 extern double rnd_prod(), rnd_quot();
339 extern double rnd_prod(double, double), rnd_quot(double, double);
342 #define rounded_product(a,b) a *= b
343 #define rounded_quotient(a,b) a /= b
346 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
347 #define Big1 0xffffffff
350 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
351 * * This makes some inner loops simpler and sometimes saves work
352 * * during multiplications, but it often seems to make things slightly
353 * * slower. Hence the default is now to store 32 bits per Long.
364 int k
, maxwds
, sign
, wds
;
368 typedef struct Bigint Bigint
;
370 void destroy_freelist(void);
374 BigintData() : p5s(nullptr) {
375 freelist
= (Bigint
**)calloc(Kmax
+ 1, sizeof(Bigint
*));
385 static IMPLEMENT_THREAD_LOCAL_NO_CHECK(BigintData
, s_bigint_data
);
387 void zend_get_bigint_data() {
388 s_bigint_data
.getCheck();
391 static Bigint
* Balloc(int k
)
397 throw FatalErrorException("Balloc() allocation exceeds list boundary");
400 Bigint
**&freelist
= s_bigint_data
->freelist
;
401 if ((rv
= freelist
[k
])) {
402 freelist
[k
] = rv
->next
;
405 rv
= (Bigint
*)MALLOC(sizeof(Bigint
) + (x
-1)*sizeof(Long
));
407 throw FatalErrorException("Balloc() failed to allocate memory");
412 rv
->sign
= rv
->wds
= 0;
416 static void Bfree(Bigint
*v
)
419 Bigint
**&freelist
= s_bigint_data
->freelist
;
420 v
->next
= freelist
[v
->k
];
425 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
426 y->wds*sizeof(Long) + 2*sizeof(int))
428 /* return value is only used as a simple string, so mis-aligned parts
429 * inside the Bigint are not at risk on strict align architectures
431 static char * rv_alloc(int i
) {
436 (int)(sizeof(Bigint
) - sizeof(ULong
) - sizeof(int) + j
) <= i
;
442 return (char *)(r
+1);
446 static char * nrv_alloc(const char *s
, char **rve
, int n
)
450 t
= rv
= rv_alloc(n
);
451 while((*t
= *s
++) !=0) {
460 static Bigint
* multadd(Bigint
*b
, int m
, int a
) /* multiply by m and add a */
475 y
= (xi
& 0xffff) * m
+ a
;
476 z
= (xi
>> 16) * m
+ (y
>> 16);
478 *x
++ = (z
<< 16) + (y
& 0xffff);
487 if (wds
>= b
->maxwds
) {
499 static int hi0bits(ULong x
)
503 if (!(x
& 0xffff0000)) {
507 if (!(x
& 0xff000000)) {
511 if (!(x
& 0xf0000000)) {
515 if (!(x
& 0xc0000000)) {
519 if (!(x
& 0x80000000)) {
521 if (!(x
& 0x40000000)) {
528 static int lo0bits(ULong
*y
)
572 static Bigint
* i2b(int i
)
582 static Bigint
* mult(Bigint
*a
, Bigint
*b
)
587 ULong
*x
, *xa
, *xae
, *xb
, *xbe
, *xc
, *xc0
;
592 if (a
->wds
< b
->wds
) {
601 if (wc
> a
->maxwds
) {
605 for(x
= c
->x
, xa
= x
+ wc
; x
< xa
; x
++) {
614 for(; xb
< xbe
; xb
++, xc0
++) {
615 if ((y
= *xb
& 0xffff)) {
620 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
622 z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
629 if ((y
= *xb
>> 16)) {
635 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
638 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
646 for(; xb
< xbe
; xc0
++) {
652 z
= *x
++ * y
+ *xc
+ carry
;
661 for(xc0
= c
->x
, xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) ;
666 static Bigint
* s2b (CONST
char *s
, int nd0
, int nd
, ULong y9
)
673 for(k
= 0, y
= 1; x
> y
; y
<<= 1, k
++) ;
680 b
->x
[0] = y9
& 0xffff;
681 b
->wds
= (b
->x
[1] = y9
>> 16) ? 2 : 1;
687 do b
= multadd(b
, 10, *s
++ - '0');
694 b
= multadd(b
, 10, *s
++ - '0');
699 static Bigint
* pow5mult(Bigint
*b
, int k
)
701 Bigint
*b1
, *p5
, *p51
;
703 static int p05
[3] = { 5, 25, 125 };
705 Bigint
*&p5s
= s_bigint_data
->p5s
;
707 b
= multadd(b
, p05
[i
-1], 0);
727 if (!(p51
= p5
->next
)) {
728 if (!(p51
= p5
->next
)) {
729 p51
= p5
->next
= mult(p5
,p5
);
739 static Bigint
*lshift(Bigint
*b
, int k
)
743 ULong
*x
, *x1
, *xe
, z
;
752 for(i
= b
->maxwds
; n1
> i
; i
<<= 1) {
757 for(i
= 0; i
< n
; i
++) {
780 *x1
++ = *x
<< k
& 0xffff | z
;
797 static int cmp(Bigint
*a
, Bigint
*b
)
799 ULong
*xa
, *xa0
, *xb
, *xb0
;
812 return *xa
< *xb
? -1 : 1;
820 static Bigint
* diff(Bigint
*a
, Bigint
*b
)
824 Long borrow
, y
; /* We need signed shifts here. */
825 ULong
*xa
, *xae
, *xb
, *xbe
, *xc
;
857 y
= (*xa
& 0xffff) - (*xb
& 0xffff) + borrow
;
859 Sign_Extend(borrow
, y
);
860 z
= (*xa
++ >> 16) - (*xb
++ >> 16) + borrow
;
862 Sign_Extend(borrow
, z
);
866 y
= (*xa
& 0xffff) + borrow
;
868 Sign_Extend(borrow
, y
);
869 z
= (*xa
++ >> 16) + borrow
;
871 Sign_Extend(borrow
, z
);
876 y
= *xa
++ - *xb
++ + borrow
;
878 Sign_Extend(borrow
, y
);
884 Sign_Extend(borrow
, y
);
895 static double ulp (double _x
)
902 L
= (word0(x
) & Exp_mask
) - (P
-1)*Exp_msk1
;
903 #ifndef Sudden_Underflow
911 #ifndef Sudden_Underflow
916 word0(a
) = 0x80000 >> L
;
922 word1(a
) = L
>= 31 ? 1 : 1 << (31 - L
);
932 (a
, e
) Bigint
*a
; int *e
;
937 ULong
*xa
, *xa0
, w
, y
, z
;
954 d0
= Exp_1
| y
>> (Ebits
- k
);
955 w
= xa
> xa0
? *--xa
: 0;
956 d1
= y
<< ((32-Ebits
) + k
) | w
>> (Ebits
- k
);
959 z
= xa
> xa0
? *--xa
: 0;
961 d0
= Exp_1
| y
<< k
| z
>> (32 - k
);
962 y
= xa
> xa0
? *--xa
: 0;
963 d1
= z
<< k
| y
>> (32 - k
);
970 if (k
< Ebits
+ 16) {
971 z
= xa
> xa0
? *--xa
: 0;
972 d0
= Exp_1
| y
<< k
- Ebits
| z
>> Ebits
+ 16 - k
;
973 w
= xa
> xa0
? *--xa
: 0;
974 y
= xa
> xa0
? *--xa
: 0;
975 d1
= z
<< k
+ 16 - Ebits
| w
<< k
- Ebits
| y
>> 16 + Ebits
- k
;
978 z
= xa
> xa0
? *--xa
: 0;
979 w
= xa
> xa0
? *--xa
: 0;
981 d0
= Exp_1
| y
<< k
+ 16 | z
<< k
| w
>> 16 - k
;
982 y
= xa
> xa0
? *--xa
: 0;
983 d1
= w
<< k
+ 16 | y
<< k
;
987 word0(d
) = d0
>> 16 | d0
<< 16;
988 word1(d
) = d1
>> 16 | d1
<< 16;
997 static Bigint
* d2b(double _d
, int *e
, int *bits
)
1009 d0
= word0(d
) >> 16 | word0(d
) << 16;
1010 d1
= word1(d
) >> 16 | word1(d
) << 16;
1024 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
1025 #ifdef Sudden_Underflow
1026 de
= (int)(d0
>> Exp_shift
);
1031 if ((de
= (int)(d0
>> Exp_shift
)))
1036 if ((k
= lo0bits(&y
))) {
1037 x
[0] = y
| (z
<< (32 - k
));
1042 i
= b
->wds
= (x
[1] = z
) ? 2 : 1;
1051 if (k
= lo0bits(&y
)) {
1053 x
[0] = y
| z
<< 32 - k
& 0xffff;
1054 x
[1] = z
>> k
- 16 & 0xffff;
1059 x
[1] = y
>> 16 | z
<< 16 - k
& 0xffff;
1060 x
[2] = z
>> k
& 0xffff;
1087 #ifndef Sudden_Underflow
1091 *e
= (de
- Bias
- (P
-1) << 2) + k
;
1092 *bits
= 4*P
+ 8 - k
- hi0bits(word0(d
) & Frac_mask
);
1094 *e
= de
- Bias
- (P
-1) + k
;
1097 #ifndef Sudden_Underflow
1099 *e
= de
- Bias
- (P
-1) + 1 + k
;
1101 *bits
= 32*i
- hi0bits(x
[i
-1]);
1103 *bits
= (i
+2)*16 - hi0bits(x
[i
]);
1113 static double ratio (Bigint
*a
, Bigint
*b
)
1118 value(da
) = b2d(a
, &ka
);
1119 value(db
) = b2d(b
, &kb
);
1121 k
= ka
- kb
+ 32*(a
->wds
- b
->wds
);
1123 k
= ka
- kb
+ 16*(a
->wds
- b
->wds
);
1127 word0(da
) += (k
>> 2)*Exp_msk1
;
1133 word0(db
) += (k
>> 2)*Exp_msk1
;
1139 word0(da
) += k
*Exp_msk1
;
1142 word0(db
) += k
*Exp_msk1
;
1145 return value(da
) / value(db
);
1150 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
1151 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
1159 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
1160 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1164 static CONST
double bigtens
[] = { 1e16
, 1e32
, 1e64
};
1165 static CONST
double tinytens
[] = { 1e-16, 1e-32, 1e-64 };
1168 static CONST
double bigtens
[] = { 1e16
, 1e32
};
1169 static CONST
double tinytens
[] = { 1e-16, 1e-32 };
1175 static int quorem(Bigint
*b
, Bigint
*S
)
1180 ULong
*bx
, *bxe
, *sx
, *sxe
;
1193 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
1200 ys
= (si
& 0xffff) * q
+ carry
;
1201 zs
= (si
>> 16) * q
+ (ys
>> 16);
1203 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1205 Sign_Extend(borrow
, y
);
1206 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1208 Sign_Extend(borrow
, z
);
1211 ys
= *sx
++ * q
+ carry
;
1213 y
= *bx
- (ys
& 0xffff) + borrow
;
1215 Sign_Extend(borrow
, y
);
1222 while(--bxe
> bx
&& !*bxe
)
1227 if (cmp(b
, S
) >= 0) {
1236 ys
= (si
& 0xffff) + carry
;
1237 zs
= (si
>> 16) + (ys
>> 16);
1239 y
= (*bx
& 0xffff) - (ys
& 0xffff) + borrow
;
1241 Sign_Extend(borrow
, y
);
1242 z
= (*bx
>> 16) - (zs
& 0xffff) + borrow
;
1244 Sign_Extend(borrow
, z
);
1249 y
= *bx
- (ys
& 0xffff) + borrow
;
1251 Sign_Extend(borrow
, y
);
1259 while(--bxe
> bx
&& !*bxe
)
1267 void destroy_freelist(void)
1272 Bigint
**&freelist
= s_bigint_data
->freelist
;
1273 for (i
= 0; i
<= Kmax
; i
++) {
1274 Bigint
**listp
= &freelist
[i
];
1275 while ((tmp
= *listp
) != nullptr) {
1279 freelist
[i
] = nullptr;
1285 void zend_freedtoa(char *s
)
1287 Bigint
*b
= (Bigint
*)((int *)s
- 1);
1288 b
->maxwds
= 1 << (b
->k
= *(int*)b
);
1292 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1294 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1295 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1298 * 1. Rather than iterating, we use a simple numeric overestimate
1299 * to determine k = floor(log10(d)). We scale relevant
1300 * quantities using O(log2(k)) rather than O(k) multiplications.
1301 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1302 * try to generate digits strictly left to right. Instead, we
1303 * compute with fewer bits and propagate the carry if necessary
1304 * when rounding the final digit up. This is often faster.
1305 * 3. Under the assumption that input will be rounded nearest,
1306 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1307 * That is, we allow equality in stopping tests when the
1308 * round-nearest rule will give the same floating-point value
1309 * as would satisfaction of the stopping test with strict
1311 * 4. We remove common factors of powers of 2 from relevant
1313 * 5. When converting floating-point integers less than 1e16,
1314 * we use floating-point arithmetic rather than resorting
1315 * to multiple-precision integers.
1316 * 6. When asked to produce fewer than 15 digits, we first try
1317 * to get by with floating-point arithmetic; we resort to
1318 * multiple-precision integer arithmetic only if we cannot
1319 * guarantee that the floating-point calculation has given
1320 * the correctly rounded result. For k requested digits and
1321 * "uniformly" distributed input, the probability is
1322 * something like 10^(k-15) that we must resort to the Long
1326 char * zend_dtoa(double _d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
)
1328 /* Arguments ndigits, decpt, sign are similar to those
1329 of ecvt and fcvt; trailing zeros are suppressed from
1330 the returned string. If not null, *rve is set to point
1331 to the end of the return value. If d is +-Infinity or NaN,
1332 then *decpt is set to 9999.
1335 0 ==> shortest string that yields d when read in
1336 and rounded to nearest.
1337 1 ==> like 0, but with Steele & White stopping rule;
1338 e.g. with IEEE P754 arithmetic , mode 0 gives
1339 1e23 whereas mode 1 gives 9.999999999999999e22.
1340 2 ==> max(1,ndigits) significant digits. This gives a
1341 return value similar to that of ecvt, except
1342 that trailing zeros are suppressed.
1343 3 ==> through ndigits past the decimal point. This
1344 gives a return value similar to that from fcvt,
1345 except that trailing zeros are suppressed, and
1346 ndigits can be negative.
1347 4-9 should give the same return values as 2-3, i.e.,
1348 4 <= mode <= 9 ==> same return as mode
1349 2 + (mode & 1). These modes are mainly for
1350 debugging; often they run slower but sometimes
1351 faster than modes 2-3.
1352 4,5,8,9 ==> left-to-right digit generation.
1353 6-9 ==> don't try fast floating-point estimate
1356 Values of mode other than 0-9 are treated as mode 0.
1358 Sufficient space is allocated to the return value
1359 to hold the suppressed trailing zeros.
1362 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
1363 j
, j1
, k
, k0
, k_check
, leftright
, m2
, m5
, s2
, s5
,
1364 spec_case
= 0, try_quick
;
1366 #ifndef Sudden_Underflow
1370 Bigint
*b
, *b1
, *delta
, *mlo
= 0, *mhi
, *S
, *tmp
;
1377 if (word0(d
) & Sign_bit
) {
1378 /* set sign for everything, including 0's and NaNs */
1380 word0(d
) &= ~Sign_bit
; /* clear sign bit */
1385 #if defined(IEEE_Arith) + defined(VAX)
1387 if ((word0(d
) & Exp_mask
) == Exp_mask
)
1389 if (word0(d
) == 0x8000)
1392 /* Infinity or NaN */
1395 if (!word1(d
) && !(word0(d
) & 0xfffff))
1396 return nrv_alloc("Infinity", rve
, 8);
1398 return nrv_alloc("NaN", rve
, 3);
1402 value(d
) += 0; /* normalize */
1406 return nrv_alloc("0", rve
, 1);
1409 b
= d2b(value(d
), &be
, &bbits
);
1410 #ifdef Sudden_Underflow
1411 i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
));
1413 if ((i
= (int)(word0(d
) >> Exp_shift1
& (Exp_mask
>>Exp_shift1
)))) {
1415 value(d2
) = value(d
);
1416 word0(d2
) &= Frac_mask1
;
1417 word0(d2
) |= Exp_11
;
1419 if (j
= 11 - hi0bits(word0(d2
) & Frac_mask
))
1420 value(d2
) /= 1 << j
;
1423 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1424 * log10(x) = log(x) / log(10)
1425 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1426 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1428 * This suggests computing an approximation k to log10(d) by
1430 * k = (i - Bias)*0.301029995663981
1431 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1433 * We want k to be too large rather than too small.
1434 * The error in the first-order Taylor series approximation
1435 * is in our favor, so we just round up the constant enough
1436 * to compensate for any error in the multiplication of
1437 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1438 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1439 * adding 1e-13 to the constant term more than suffices.
1440 * Hence we adjust the constant term to 0.1760912590558.
1441 * (We could get a more accurate k by invoking log10,
1442 * but this is probably not worthwhile.)
1450 #ifndef Sudden_Underflow
1454 /* d is denormalized */
1456 i
= bbits
+ be
+ (Bias
+ (P
-1) - 1);
1457 x
= i
> 32 ? (word0(d
) << (64 - i
)) | (word1(d
) >> (i
- 32))
1458 : (word1(d
) << (32 - i
));
1460 word0(d2
) -= 31*Exp_msk1
; /* adjust exponent */
1461 i
-= (Bias
+ (P
-1) - 1) + 1;
1465 ds
= (value(d2
)-1.5)*0.289529654602168 + 0.1760912590558 + i
*0.301029995663981;
1467 if (ds
< 0. && ds
!= k
)
1468 k
--; /* want k = floor(ds) */
1470 if (k
>= 0 && k
<= Ten_pmax
) {
1471 if (value(d
) < tens
[k
])
1494 if (mode
< 0 || mode
> 9)
1515 ilim
= ilim1
= i
= ndigits
;
1521 i
= ndigits
+ k
+ 1;
1527 s
= s0
= rv_alloc(i
);
1529 if (ilim
>= 0 && ilim
<= Quick_max
&& try_quick
) {
1531 /* Try to get by with floating-point arithmetic. */
1534 value(d2
) = value(d
);
1537 ieps
= 2; /* conservative */
1542 /* prevent overflows */
1544 value(d
) /= bigtens
[n_bigtens
-1];
1547 for(; j
; j
>>= 1, i
++)
1554 else if ((j1
= -k
)) {
1555 value(d
) *= tens
[j1
& 0xf];
1556 for(j
= j1
>> 4; j
; j
>>= 1, i
++)
1559 value(d
) *= bigtens
[i
];
1562 if (k_check
&& value(d
) < 1. && ilim
> 0) {
1570 value(eps
) = ieps
*value(d
) + 7.;
1571 word0(eps
) -= (P
-1)*Exp_msk1
;
1575 if (value(d
) > value(eps
))
1577 if (value(d
) < -value(eps
))
1581 #ifndef No_leftright
1583 /* Use Steele & White method of only
1584 * generating digits needed.
1586 value(eps
) = 0.5/tens
[ilim
-1] - value(eps
);
1588 L
= (int32_t)value(d
);
1590 *s
++ = '0' + (int)L
;
1591 if (value(d
) < value(eps
))
1593 if (1. - value(d
) < value(eps
))
1603 /* Generate ilim digits, then fix them up. */
1604 value(eps
) *= tens
[ilim
-1];
1605 for(i
= 1;; i
++, value(d
) *= 10.) {
1606 L
= (int32_t)value(d
);
1608 *s
++ = '0' + (int)L
;
1610 if (value(d
) > 0.5 + value(eps
))
1612 else if (value(d
) < 0.5 - value(eps
)) {
1613 /* cut ALL traling zeros only if the number of chars is greater than precision
1614 * otherwise cut only extra zeros
1617 while(*--s
== '0' && (s
- s0
) > k
);
1627 #ifndef No_leftright
1632 value(d
) = value(d2
);
1637 /* Do we have a "small" integer? */
1639 if (be
>= 0 && k
<= Int_max
) {
1642 if (ndigits
< 0 && ilim
<= 0) {
1644 if (ilim
< 0 || value(d
) <= 5*ds
)
1649 L
= (int32_t)(value(d
) / ds
);
1651 #ifdef Check_FLT_ROUNDS
1652 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
1658 *s
++ = '0' + (int)L
;
1660 value(d
) += value(d
);
1661 if (value(d
) > ds
|| (value(d
) == ds
&& (L
& 1))) {
1673 if (!(value(d
) *= 10.))
1685 #ifndef Sudden_Underflow
1686 denorm
? be
+ (Bias
+ (P
-1) - 1 + 1) :
1689 1 + 4*P
- 3 - bbits
+ ((bbits
+ be
- 1) & 3);
1703 if ((i
= ilim
) < 0) {
1712 if (m2
> 0 && s2
> 0) {
1713 i
= m2
< s2
? m2
: s2
;
1721 mhi
= pow5mult(mhi
, m5
);
1726 if ((j
= b5
- m5
)) {
1730 b
= pow5mult(b
, b5
);
1735 S
= pow5mult(S
, s5
);
1736 /* Check for special case that d is a normalized power of 2. */
1739 if (!word1(d
) && !(word0(d
) & Bndry_mask
)
1740 #ifndef Sudden_Underflow
1741 && word0(d
) & Exp_mask
1744 /* The special case */
1753 /* Arrange for convenient computation of quotients:
1754 * shift left if necessary so divisor has 4 leading 0 bits.
1756 * Perhaps we should just compute leading 28 bits of S once
1757 * and for all and pass them and a shift to quorem, so it
1758 * can do shifts and ors to compute the numerator for q.
1761 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0x1f))
1764 if ((i
= ((s5
? 32 - hi0bits(S
->x
[S
->wds
-1]) : 1) + s2
) & 0xf))
1786 b
= multadd(b
, 10, 0); /* we botched the k estimate */
1788 mhi
= multadd(mhi
, 10, 0);
1792 if (ilim
<= 0 && mode
> 2) {
1793 if (ilim
< 0 || cmp(b
,S
= multadd(S
,5,0)) <= 0) {
1794 /* no digits, fcvt style */
1806 mhi
= lshift(mhi
, m2
);
1808 /* Compute mlo -- check for special case
1809 * that d is a normalized power of 2.
1814 mhi
= Balloc(mhi
->k
);
1816 mhi
= lshift(mhi
, Log2P
);
1820 dig
= quorem(b
,S
) + '0';
1821 /* Do we yet have the shortest decimal string
1822 * that will round to d?
1825 delta
= diff(S
, mhi
);
1826 j1
= delta
->sign
? 1 : cmp(b
, delta
);
1828 #ifndef ROUND_BIASED
1829 if (j1
== 0 && !mode
&& !(word1(d
) & 1)) {
1838 if (j
< 0 || (j
== 0 && !mode
1839 #ifndef ROUND_BIASED
1846 if ((j1
> 0 || (j1
== 0 && (dig
& 1)))
1854 if (dig
== '9') { /* possible if i == 1 */
1865 b
= multadd(b
, 10, 0);
1867 mlo
= mhi
= multadd(mhi
, 10, 0);
1869 mlo
= multadd(mlo
, 10, 0);
1870 mhi
= multadd(mhi
, 10, 0);
1876 *s
++ = dig
= quorem(b
,S
) + '0';
1879 b
= multadd(b
, 10, 0);
1882 /* Round off last digit */
1886 if (j
> 0 || (j
== 0 && (dig
& 1))) {
1903 if (mlo
&& mlo
!= mhi
)
1910 Bigint
*&p5s
= s_bigint_data
->p5s
;
1920 if (s
== s0
) { /* don't return empty string */
1931 double zend_strtod (CONST
char *s00
, char **se
)
1933 int bb2
, bb5
, bbe
, bd2
, bd5
, bbbits
, bs2
, c
, dsign
,
1934 e
, e1
, esign
, i
, j
, k
, nd
, nd0
, nf
, nz
, nz0
, sign
;
1935 CONST
char *s
, *s0
, *s1
;
1936 double aadj
, aadj1
, adj
;
1940 Bigint
*bb
= 0, *bb1
, *bd
= 0, *bd0
, *bs
= 0, *delta
= 0, *tmp
;
1943 CONST
char decimal_point
= '.';
1945 sign
= nz0
= nz
= 0;
1949 for(s
= s00
; isspace((unsigned char) *s
); s
++)
1955 } else if (*s
== '+') {
1966 while(*++s
== '0') ;
1972 for(nd
= nf
= 0; (c
= *s
) >= '0' && c
<= '9'; nd
++, s
++)
1978 if (c
== decimal_point
) {
1981 for(; c
== '0'; c
= *++s
)
1983 if (c
> '0' && c
<= '9') {
1991 for(; c
>= '0' && c
<= '9'; c
= *++s
) {
1996 for(i
= 1; i
< nz
; i
++)
1999 else if (nd
<= DBL_DIG
+ 1)
2003 else if (nd
<= DBL_DIG
+ 1)
2011 if (c
== 'e' || c
== 'E') {
2012 if (!nd
&& !nz
&& !nz0
) {
2024 if (c
>= '0' && c
<= '9') {
2027 if (c
> '0' && c
<= '9') {
2030 while((c
= *++s
) >= '0' && c
<= '9')
2032 if (s
- s1
> 8 || L
> 19999)
2033 /* Avoid confusion from exponents
2034 * so large that e might overflow.
2036 e
= 19999; /* safe for 16 bit ints */
2055 /* Now we have nd0 digits, starting at s0, followed by a
2056 * decimal point, followed by nd-nd0 digits. The number we're
2057 * after is the integer represented by those digits times
2062 k
= nd
< DBL_DIG
+ 1 ? nd
: DBL_DIG
+ 1;
2065 value(rv
) = tens
[k
- 9] * value(rv
) + z
;
2068 #ifndef RND_PRODQUOT
2075 if (e
<= Ten_pmax
) {
2077 goto vax_ovfl_check
;
2079 /* value(rv) = */ rounded_product(value(rv
),
2085 if (e
<= Ten_pmax
+ i
) {
2086 /* A fancier test would sometimes let us do
2087 * this for larger i values.
2090 value(rv
) *= tens
[i
];
2092 /* VAX exponent range is so narrow we must
2093 * worry about overflow here...
2096 word0(rv
) -= P
*Exp_msk1
;
2097 /* value(rv) = */ rounded_product(value(rv
),
2099 if ((word0(rv
) & Exp_mask
)
2100 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
))
2102 word0(rv
) += P
*Exp_msk1
;
2104 /* value(rv) = */ rounded_product(value(rv
),
2110 #ifndef Inaccurate_Divide
2111 else if (e
>= -Ten_pmax
) {
2112 /* value(rv) = */ rounded_quotient(value(rv
),
2120 /* Get starting approximation = rv * 10**e1 */
2124 value(rv
) *= tens
[i
];
2126 if (e1
> DBL_MAX_10_EXP
) {
2130 value(rv
) = HUGE_VAL
;
2132 /* Can't trust HUGE_VAL */
2134 word0(rv
) = Exp_mask
;
2146 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2148 value(rv
) *= bigtens
[j
];
2149 /* The last multiplication could overflow. */
2150 word0(rv
) -= P
*Exp_msk1
;
2151 value(rv
) *= bigtens
[j
];
2152 if ((z
= word0(rv
) & Exp_mask
)
2153 > Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
))
2155 if (z
> Exp_msk1
*(DBL_MAX_EXP
+Bias
-1-P
)) {
2156 /* set to largest number */
2157 /* (Can't trust DBL_MAX) */
2162 word0(rv
) += P
*Exp_msk1
;
2170 value(rv
) /= tens
[i
];
2173 if (e1
>= 1 << n_bigtens
)
2175 for(j
= 0; e1
> 1; j
++, e1
>>= 1)
2177 value(rv
) *= tinytens
[j
];
2178 /* The last multiplication could underflow. */
2179 value(rv0
) = value(rv
);
2180 value(rv
) *= tinytens
[j
];
2182 value(rv
) = 2.*value(rv0
);
2183 value(rv
) *= tinytens
[j
];
2194 /* The refinement below will clean
2195 * this approximation up.
2201 /* Now the hard part -- adjusting rv to the correct value.*/
2203 /* Put digits into bd: true value = bd * 10^e */
2205 bd0
= s2b(s0
, nd0
, nd
, y
);
2208 bd
= Balloc(bd0
->k
);
2210 bb
= d2b(value(rv
), &bbe
, &bbbits
); /* rv = bb * 2^bbe */
2226 #ifdef Sudden_Underflow
2228 j
= 1 + 4*P
- 3 - bbbits
+ ((bbe
+ bbbits
- 1) & 3);
2233 i
= bbe
+ bbbits
- 1; /* logb(rv) */
2234 if (i
< Emin
) /* denormal */
2241 i
= bb2
< bd2
? bb2
: bd2
;
2250 bs
= pow5mult(bs
, bb5
);
2256 bb
= lshift(bb
, bb2
);
2258 bd
= pow5mult(bd
, bd5
);
2260 bd
= lshift(bd
, bd2
);
2262 bs
= lshift(bs
, bs2
);
2263 delta
= diff(bb
, bd
);
2264 dsign
= delta
->sign
;
2268 /* Error is less than half an ulp -- check for
2269 * special case of mantissa a power of two.
2271 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
)
2273 delta
= lshift(delta
,Log2P
);
2274 if (cmp(delta
, bs
) > 0)
2279 /* exactly half-way between */
2281 if ((word0(rv
) & Bndry_mask1
) == Bndry_mask1
2282 && word1(rv
) == 0xffffffff) {
2283 /*boundary case -- increment exponent*/
2284 word0(rv
) = (word0(rv
) & Exp_mask
)
2294 else if (!(word0(rv
) & Bndry_mask
) && !word1(rv
)) {
2296 /* boundary case -- decrement exponent */
2297 #ifdef Sudden_Underflow
2298 L
= word0(rv
) & Exp_mask
;
2307 L
= (word0(rv
) & Exp_mask
) - Exp_msk1
;
2309 word0(rv
) = L
| Bndry_mask1
;
2310 word1(rv
) = 0xffffffff;
2317 #ifndef ROUND_BIASED
2318 if (!(word1(rv
) & LSB
))
2322 value(rv
) += ulp(value(rv
));
2323 #ifndef ROUND_BIASED
2325 value(rv
) -= ulp(value(rv
));
2326 #ifndef Sudden_Underflow
2334 if ((aadj
= ratio(delta
, bs
)) <= 2.) {
2337 else if (word1(rv
) || word0(rv
) & Bndry_mask
) {
2338 #ifndef Sudden_Underflow
2339 if (word1(rv
) == Tiny1
&& !word0(rv
))
2346 /* special case -- power of FLT_RADIX to be */
2347 /* rounded down... */
2349 if (aadj
< 2./FLT_RADIX
)
2350 aadj
= 1./FLT_RADIX
;
2358 aadj1
= dsign
? aadj
: -aadj
;
2359 #ifdef Check_FLT_ROUNDS
2360 switch(FLT_ROUNDS
) {
2361 case 2: /* towards +infinity */
2364 case 0: /* towards 0 */
2365 case 3: /* towards -infinity */
2369 if (FLT_ROUNDS
== 0)
2373 y
= word0(rv
) & Exp_mask
;
2375 /* Check for overflow */
2377 if (y
== Exp_msk1
*(DBL_MAX_EXP
+Bias
-1)) {
2378 value(rv0
) = value(rv
);
2379 word0(rv
) -= P
*Exp_msk1
;
2380 adj
= aadj1
* ulp(value(rv
));
2382 if ((word0(rv
) & Exp_mask
) >=
2383 Exp_msk1
*(DBL_MAX_EXP
+Bias
-P
)) {
2384 if (word0(rv0
) == Big0
&& word1(rv0
) == Big1
)
2391 word0(rv
) += P
*Exp_msk1
;
2394 #ifdef Sudden_Underflow
2395 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
) {
2396 value(rv0
) = value(rv
);
2397 word0(rv
) += P
*Exp_msk1
;
2398 adj
= aadj1
* ulp(value(rv
));
2401 if ((word0(rv
) & Exp_mask
) < P
*Exp_msk1
)
2403 if ((word0(rv
) & Exp_mask
) <= P
*Exp_msk1
)
2406 if (word0(rv0
) == Tiny0
2407 && word1(rv0
) == Tiny1
)
2414 word0(rv
) -= P
*Exp_msk1
;
2417 adj
= aadj1
* ulp(value(rv
));
2421 /* Compute adj so that the IEEE rounding rules will
2422 * correctly round rv + adj in some half-way cases.
2423 * If rv * ulp(rv) is denormalized (i.e.,
2424 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2425 * trouble from bits lost to denormalization;
2426 * example: 1.2e-307 .
2428 if (y
<= (P
-1)*Exp_msk1
&& aadj
>= 1.) {
2429 aadj1
= (double)(int)(aadj
+ 0.5);
2433 adj
= aadj1
* ulp(value(rv
));
2437 z
= word0(rv
) & Exp_mask
;
2439 /* Can we stop now? */
2442 /* The tolerances below are conservative. */
2443 if (dsign
|| word1(rv
) || word0(rv
) & Bndry_mask
) {
2444 if (aadj
< .4999999 || aadj
> .5000001)
2447 else if (aadj
< .4999999/FLT_RADIX
)
2465 result
= sign
? -value(rv
) : value(rv
);
2467 Bigint
*&p5s
= s_bigint_data
->p5s
;
2477 double zend_hex_strtod(const char *str
, char **endptr
)
2479 const char *s
= str
;
2484 if (*s
== '0' && (s
[1] == 'x' || s
[1] == 'X')) {
2488 while ((c
= *s
++)) {
2489 if (c
>= '0' && c
<= '9') {
2491 } else if (c
>= 'A' && c
<= 'F') {
2493 } else if (c
>= 'a' && c
<= 'f') {
2500 value
= value
* 16 + c
;
2503 if (endptr
!= nullptr) {
2504 *endptr
= (char *)(any
? s
- 1 : str
);
2510 double zend_oct_strtod(const char *str
, char **endptr
)
2512 const char *s
= str
;
2517 /* skip leading zero */
2520 while ((c
= *s
++)) {
2522 /* break and return the current value if the number is not well-formed
2523 * that's what Linux strtol() does
2527 value
= value
* 8 + c
- '0';
2531 if (endptr
!= nullptr) {
2532 *endptr
= (char *)(any
? s
- 1 : str
);
2538 ///////////////////////////////////////////////////////////////////////////////