make #includes consistent
[hiphop-php.git] / hphp / runtime / base / zend / zend_strtod.cpp
blob922bb1d993738e26f88a719d196eec1cd76a0abd
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
21 David M. Gay
22 AT&T Bell Laboratories, Room 2C-463
23 600 Mountain Avenue
24 Murray Hill, NJ 07974-2070
25 U.S.A.
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].
39 * Modifications:
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
56 * for 0 <= k <= 22).
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"
96 namespace HPHP {
97 ///////////////////////////////////////////////////////////////////////////////
99 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
100 # if defined(__LITTLE_ENDIAN__)
101 # undef WORDS_BIGENDIAN
102 # else
103 # if defined(__BIG_ENDIAN__)
104 # define WORDS_BIGENDIAN
105 # endif
106 # endif
107 #endif
109 #ifdef WORDS_BIGENDIAN
110 #define IEEE_BIG_ENDIAN
111 #else
112 #define IEEE_LITTLE_ENDIAN
113 #endif
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.
120 * */
121 #define IEEE_BIG_ENDIAN
122 #undef IEEE_LITTLE_ENDIAN
123 #endif
125 #ifdef __vax__
126 #define VAX
127 #endif
129 #if defined(_MSC_VER)
130 #define int32_t __int32
131 #define uint32_t unsigned __int32
132 #define IEEE_LITTLE_ENDIAN
133 #endif
135 #define Long int32_t
136 #define ULong uint32_t
138 #ifdef MALLOC
139 #ifdef KR_headers
140 extern char *MALLOC();
141 #else
142 extern void *MALLOC(size_t);
143 #endif
144 #else
145 #define MALLOC malloc
146 #endif
148 #include <ctype.h>
149 #include <errno.h>
151 #ifdef Bad_float_h
152 #ifdef IEEE_BIG_ENDIAN
153 #define IEEE_ARITHMETIC
154 #endif
155 #ifdef IEEE_LITTLE_ENDIAN
156 #define IEEE_ARITHMETIC
157 #endif
159 #ifdef IEEE_ARITHMETIC
160 #define DBL_DIG 15
161 #define DBL_MAX_10_EXP 308
162 #define DBL_MAX_EXP 1024
163 #define FLT_RADIX 2
164 #define FLT_ROUNDS 1
165 #define DBL_MAX 1.7976931348623157e+308
166 #endif
168 #ifdef IBM
169 #define DBL_DIG 16
170 #define DBL_MAX_10_EXP 75
171 #define DBL_MAX_EXP 63
172 #define FLT_RADIX 16
173 #define FLT_ROUNDS 0
174 #define DBL_MAX 7.2370055773322621e+75
175 #endif
177 #ifdef VAX
178 #define DBL_DIG 16
179 #define DBL_MAX_10_EXP 38
180 #define DBL_MAX_EXP 127
181 #define FLT_RADIX 2
182 #define FLT_ROUNDS 1
183 #define DBL_MAX 1.7014118346046923e+38
184 #endif
187 #ifndef LONG_MAX
188 #define LONG_MAX 2147483647
189 #endif
190 #else
191 #include <float.h>
192 #endif
193 #ifndef __MATH_H__
194 #include <math.h>
195 #endif
197 #ifndef CONST
198 #ifdef KR_headers
199 #define CONST /* blank */
200 #else
201 #define CONST const
202 #endif
203 #endif
205 #ifdef Unsigned_Shifts
206 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
207 #else
208 #define Sign_Extend(a,b) /*no-op*/
209 #endif
211 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
212 defined(IBM) != 1
213 Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
214 IBM should be defined.
215 #endif
217 typedef union {
218 double d;
219 ULong ul[2];
220 } _double;
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])
225 #else
226 #define word0(x) ((x).ul[0])
227 #define word1(x) ((x).ul[1])
228 #endif
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++)
237 #else
238 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
239 ((unsigned short *)a)[1] = (unsigned short)c, a++)
240 #endif
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)
249 #define Exp_shift 20
250 #define Exp_shift1 20
251 #define Exp_msk1 0x100000
252 #define Exp_msk11 0x100000
253 #define Exp_mask 0x7ff00000
254 #define P 53
255 #define Bias 1023
256 #define IEEE_Arith
257 #define Emin (-1022)
258 #define Exp_1 0x3ff00000
259 #define Exp_11 0x3ff00000
260 #define Ebits 11
261 #define Frac_mask 0xfffff
262 #define Frac_mask1 0xfffff
263 #define Ten_pmax 22
264 #define Bletch 0x10
265 #define Bndry_mask 0xfffff
266 #define Bndry_mask1 0xfffff
267 #define LSB 1
268 #define Sign_bit 0x80000000
269 #define Log2P 1
270 #define Tiny0 0
271 #define Tiny1 1
272 #define Quick_max 14
273 #define Int_max 14
274 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
275 #else
276 #undef Sudden_Underflow
277 #define Sudden_Underflow
278 #ifdef IBM
279 #define Exp_shift 24
280 #define Exp_shift1 24
281 #define Exp_msk1 0x1000000
282 #define Exp_msk11 0x1000000
283 #define Exp_mask 0x7f000000
284 #define P 14
285 #define Bias 65
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
291 #define Bletch 4
292 #define Ten_pmax 22
293 #define Bndry_mask 0xefffff
294 #define Bndry_mask1 0xffffff
295 #define LSB 1
296 #define Sign_bit 0x80000000
297 #define Log2P 4
298 #define Tiny0 0x100000
299 #define Tiny1 0
300 #define Quick_max 14
301 #define Int_max 15
302 #else /* VAX */
303 #define Exp_shift 23
304 #define Exp_shift1 7
305 #define Exp_msk1 0x80
306 #define Exp_msk11 0x800000
307 #define Exp_mask 0x7f80
308 #define P 56
309 #define Bias 129
310 #define Exp_1 0x40800000
311 #define Exp_11 0x4080
312 #define Ebits 8
313 #define Frac_mask 0x7fffff
314 #define Frac_mask1 0xffff007f
315 #define Ten_pmax 24
316 #define Bletch 2
317 #define Bndry_mask 0xffff007f
318 #define Bndry_mask1 0xffff007f
319 #define LSB 0x10000
320 #define Sign_bit 0x8000
321 #define Log2P 1
322 #define Tiny0 0x80
323 #define Tiny1 0
324 #define Quick_max 15
325 #define Int_max 15
326 #endif
327 #endif
329 #ifndef IEEE_Arith
330 #define ROUND_BIASED
331 #endif
333 #ifdef RND_PRODQUOT
334 #define rounded_product(a,b) a = rnd_prod(a, b)
335 #define rounded_quotient(a,b) a = rnd_quot(a, b)
336 #ifdef KR_headers
337 extern double rnd_prod(), rnd_quot();
338 #else
339 extern double rnd_prod(double, double), rnd_quot(double, double);
340 #endif
341 #else
342 #define rounded_product(a,b) a *= b
343 #define rounded_quotient(a,b) a /= b
344 #endif
346 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
347 #define Big1 0xffffffff
349 #ifndef Just_16
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.
354 * */
355 #ifndef Pack_32
356 #define Pack_32
357 #endif
358 #endif
360 #define Kmax 15
362 struct Bigint {
363 struct Bigint *next;
364 int k, maxwds, sign, wds;
365 ULong x[1];
368 typedef struct Bigint Bigint;
370 void destroy_freelist(void);
372 class BigintData {
373 public:
374 BigintData() : p5s(nullptr) {
375 freelist = (Bigint **)calloc(Kmax + 1, sizeof(Bigint *));
377 ~BigintData() {
378 destroy_freelist();
379 free(freelist);
382 Bigint **freelist;
383 Bigint *p5s;
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)
393 int x;
394 Bigint *rv;
396 if (k > Kmax) {
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;
403 } else {
404 x = 1 << k;
405 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
406 if (!rv) {
407 throw FatalErrorException("Balloc() failed to allocate memory");
409 rv->k = k;
410 rv->maxwds = x;
412 rv->sign = rv->wds = 0;
413 return rv;
416 static void Bfree(Bigint *v)
418 if (v) {
419 Bigint **&freelist = s_bigint_data->freelist;
420 v->next = freelist[v->k];
421 freelist[v->k] = v;
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) {
432 int j, k, *r;
434 j = sizeof(ULong);
435 for(k = 0;
436 (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j) <= i;
437 j <<= 1) {
438 k++;
440 r = (int*)Balloc(k);
441 *r = k;
442 return (char *)(r+1);
446 static char * nrv_alloc(const char *s, char **rve, int n)
448 char *rv, *t;
450 t = rv = rv_alloc(n);
451 while((*t = *s++) !=0) {
452 t++;
454 if (rve) {
455 *rve = t;
457 return rv;
460 static Bigint * multadd(Bigint *b, int m, int a) /* multiply by m and add a */
462 int i, wds;
463 ULong *x, y;
464 #ifdef Pack_32
465 ULong xi, z;
466 #endif
467 Bigint *b1;
469 wds = b->wds;
470 x = b->x;
471 i = 0;
472 do {
473 #ifdef Pack_32
474 xi = *x;
475 y = (xi & 0xffff) * m + a;
476 z = (xi >> 16) * m + (y >> 16);
477 a = (int)(z >> 16);
478 *x++ = (z << 16) + (y & 0xffff);
479 #else
480 y = *x * m + a;
481 a = (int)(y >> 16);
482 *x++ = y & 0xffff;
483 #endif
485 while(++i < wds);
486 if (a) {
487 if (wds >= b->maxwds) {
488 b1 = Balloc(b->k+1);
489 Bcopy(b1, b);
490 Bfree(b);
491 b = b1;
493 b->x[wds++] = a;
494 b->wds = wds;
496 return b;
499 static int hi0bits(ULong x)
501 int k = 0;
503 if (!(x & 0xffff0000)) {
504 k = 16;
505 x <<= 16;
507 if (!(x & 0xff000000)) {
508 k += 8;
509 x <<= 8;
511 if (!(x & 0xf0000000)) {
512 k += 4;
513 x <<= 4;
515 if (!(x & 0xc0000000)) {
516 k += 2;
517 x <<= 2;
519 if (!(x & 0x80000000)) {
520 k++;
521 if (!(x & 0x40000000)) {
522 return 32;
525 return k;
528 static int lo0bits(ULong *y)
530 int k;
531 ULong x = *y;
533 if (x & 7) {
534 if (x & 1) {
535 return 0;
537 if (x & 2) {
538 *y = x >> 1;
539 return 1;
541 *y = x >> 2;
542 return 2;
544 k = 0;
545 if (!(x & 0xffff)) {
546 k = 16;
547 x >>= 16;
549 if (!(x & 0xff)) {
550 k += 8;
551 x >>= 8;
553 if (!(x & 0xf)) {
554 k += 4;
555 x >>= 4;
557 if (!(x & 0x3)) {
558 k += 2;
559 x >>= 2;
561 if (!(x & 1)) {
562 k++;
563 x >>= 1;
564 if (!(x & 1)) {
565 return 32;
568 *y = x;
569 return k;
572 static Bigint * i2b(int i)
574 Bigint *b;
576 b = Balloc(1);
577 b->x[0] = i;
578 b->wds = 1;
579 return b;
582 static Bigint * mult(Bigint *a, Bigint *b)
584 Bigint *c;
585 int k, wa, wb, wc;
586 ULong carry, y, z;
587 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
588 #ifdef Pack_32
589 ULong z2;
590 #endif
592 if (a->wds < b->wds) {
593 c = a;
594 a = b;
595 b = c;
597 k = a->k;
598 wa = a->wds;
599 wb = b->wds;
600 wc = wa + wb;
601 if (wc > a->maxwds) {
602 k++;
604 c = Balloc(k);
605 for(x = c->x, xa = x + wc; x < xa; x++) {
606 *x = 0;
608 xa = a->x;
609 xae = xa + wa;
610 xb = b->x;
611 xbe = xb + wb;
612 xc0 = c->x;
613 #ifdef Pack_32
614 for(; xb < xbe; xb++, xc0++) {
615 if ((y = *xb & 0xffff)) {
616 x = xa;
617 xc = xc0;
618 carry = 0;
619 do {
620 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
621 carry = z >> 16;
622 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
623 carry = z2 >> 16;
624 Storeinc(xc, z2, z);
626 while(x < xae);
627 *xc = carry;
629 if ((y = *xb >> 16)) {
630 x = xa;
631 xc = xc0;
632 carry = 0;
633 z2 = *xc;
634 do {
635 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
636 carry = z >> 16;
637 Storeinc(xc, z, z2);
638 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
639 carry = z2 >> 16;
641 while(x < xae);
642 *xc = z2;
645 #else
646 for(; xb < xbe; xc0++) {
647 if (y = *xb++) {
648 x = xa;
649 xc = xc0;
650 carry = 0;
651 do {
652 z = *x++ * y + *xc + carry;
653 carry = z >> 16;
654 *xc++ = z & 0xffff;
656 while(x < xae);
657 *xc = carry;
660 #endif
661 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
662 c->wds = wc;
663 return c;
666 static Bigint * s2b (CONST char *s, int nd0, int nd, ULong y9)
668 Bigint *b;
669 int i, k;
670 Long x, y;
672 x = (nd + 8) / 9;
673 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
674 #ifdef Pack_32
675 b = Balloc(k);
676 b->x[0] = y9;
677 b->wds = 1;
678 #else
679 b = Balloc(k+1);
680 b->x[0] = y9 & 0xffff;
681 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
682 #endif
684 i = 9;
685 if (9 < nd0) {
686 s += 9;
687 do b = multadd(b, 10, *s++ - '0');
688 while(++i < nd0);
689 s++;
690 } else {
691 s += 10;
693 for(; i < nd; i++) {
694 b = multadd(b, 10, *s++ - '0');
696 return b;
699 static Bigint * pow5mult(Bigint *b, int k)
701 Bigint *b1, *p5, *p51;
702 int i;
703 static int p05[3] = { 5, 25, 125 };
705 Bigint *&p5s = s_bigint_data->p5s;
706 if ((i = k & 3)) {
707 b = multadd(b, p05[i-1], 0);
710 if (!(k >>= 2)) {
711 return b;
713 if (!(p5 = p5s)) {
714 /* first time */
715 p5 = p5s = i2b(625);
716 p5->next = 0;
718 for(;;) {
719 if (k & 1) {
720 b1 = mult(b, p5);
721 Bfree(b);
722 b = b1;
724 if (!(k >>= 1)) {
725 break;
727 if (!(p51 = p5->next)) {
728 if (!(p51 = p5->next)) {
729 p51 = p5->next = mult(p5,p5);
730 p51->next = 0;
733 p5 = p51;
735 return b;
739 static Bigint *lshift(Bigint *b, int k)
741 int i, k1, n, n1;
742 Bigint *b1;
743 ULong *x, *x1, *xe, z;
745 #ifdef Pack_32
746 n = k >> 5;
747 #else
748 n = k >> 4;
749 #endif
750 k1 = b->k;
751 n1 = n + b->wds + 1;
752 for(i = b->maxwds; n1 > i; i <<= 1) {
753 k1++;
755 b1 = Balloc(k1);
756 x1 = b1->x;
757 for(i = 0; i < n; i++) {
758 *x1++ = 0;
760 x = b->x;
761 xe = x + b->wds;
762 #ifdef Pack_32
763 if (k &= 0x1f) {
764 k1 = 32 - k;
765 z = 0;
766 do {
767 *x1++ = *x << k | z;
768 z = *x++ >> k1;
770 while(x < xe);
771 if ((*x1 = z)) {
772 ++n1;
775 #else
776 if (k &= 0xf) {
777 k1 = 16 - k;
778 z = 0;
779 do {
780 *x1++ = *x << k & 0xffff | z;
781 z = *x++ >> k1;
783 while(x < xe);
784 if (*x1 = z) {
785 ++n1;
788 #endif
789 else do
790 *x1++ = *x++;
791 while(x < xe);
792 b1->wds = n1 - 1;
793 Bfree(b);
794 return b1;
797 static int cmp(Bigint *a, Bigint *b)
799 ULong *xa, *xa0, *xb, *xb0;
800 int i, j;
802 i = a->wds;
803 j = b->wds;
804 if (i -= j)
805 return i;
806 xa0 = a->x;
807 xa = xa0 + j;
808 xb0 = b->x;
809 xb = xb0 + j;
810 for(;;) {
811 if (*--xa != *--xb)
812 return *xa < *xb ? -1 : 1;
813 if (xa <= xa0)
814 break;
816 return 0;
820 static Bigint * diff(Bigint *a, Bigint *b)
822 Bigint *c;
823 int i, wa, wb;
824 Long borrow, y; /* We need signed shifts here. */
825 ULong *xa, *xae, *xb, *xbe, *xc;
826 #ifdef Pack_32
827 Long z;
828 #endif
830 i = cmp(a,b);
831 if (!i) {
832 c = Balloc(0);
833 c->wds = 1;
834 c->x[0] = 0;
835 return c;
837 if (i < 0) {
838 c = a;
839 a = b;
840 b = c;
841 i = 1;
842 } else {
843 i = 0;
845 c = Balloc(a->k);
846 c->sign = i;
847 wa = a->wds;
848 xa = a->x;
849 xae = xa + wa;
850 wb = b->wds;
851 xb = b->x;
852 xbe = xb + wb;
853 xc = c->x;
854 borrow = 0;
855 #ifdef Pack_32
856 do {
857 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
858 borrow = y >> 16;
859 Sign_Extend(borrow, y);
860 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
861 borrow = z >> 16;
862 Sign_Extend(borrow, z);
863 Storeinc(xc, z, y);
864 } while(xb < xbe);
865 while(xa < xae) {
866 y = (*xa & 0xffff) + borrow;
867 borrow = y >> 16;
868 Sign_Extend(borrow, y);
869 z = (*xa++ >> 16) + borrow;
870 borrow = z >> 16;
871 Sign_Extend(borrow, z);
872 Storeinc(xc, z, y);
874 #else
875 do {
876 y = *xa++ - *xb++ + borrow;
877 borrow = y >> 16;
878 Sign_Extend(borrow, y);
879 *xc++ = y & 0xffff;
880 } while(xb < xbe);
881 while(xa < xae) {
882 y = *xa++ + borrow;
883 borrow = y >> 16;
884 Sign_Extend(borrow, y);
885 *xc++ = y & 0xffff;
887 #endif
888 while(!*--xc) {
889 wa--;
891 c->wds = wa;
892 return c;
895 static double ulp (double _x)
897 _double x;
898 register Long L;
899 _double a;
901 value(x) = _x;
902 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
903 #ifndef Sudden_Underflow
904 if (L > 0) {
905 #endif
906 #ifdef IBM
907 L |= Exp_msk1 >> 4;
908 #endif
909 word0(a) = L;
910 word1(a) = 0;
911 #ifndef Sudden_Underflow
913 else {
914 L = -L >> Exp_shift;
915 if (L < Exp_shift) {
916 word0(a) = 0x80000 >> L;
917 word1(a) = 0;
919 else {
920 word0(a) = 0;
921 L -= Exp_shift;
922 word1(a) = L >= 31 ? 1 : 1 << (31 - L);
925 #endif
926 return value(a);
929 static double
931 #ifdef KR_headers
932 (a, e) Bigint *a; int *e;
933 #else
934 (Bigint *a, int *e)
935 #endif
937 ULong *xa, *xa0, w, y, z;
938 int k;
939 _double d;
940 #ifdef VAX
941 ULong d0, d1;
942 #else
943 #define d0 word0(d)
944 #define d1 word1(d)
945 #endif
947 xa0 = a->x;
948 xa = xa0 + a->wds;
949 y = *--xa;
950 k = hi0bits(y);
951 *e = 32 - k;
952 #ifdef Pack_32
953 if (k < Ebits) {
954 d0 = Exp_1 | y >> (Ebits - k);
955 w = xa > xa0 ? *--xa : 0;
956 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
957 goto ret_d;
959 z = xa > xa0 ? *--xa : 0;
960 if (k -= Ebits) {
961 d0 = Exp_1 | y << k | z >> (32 - k);
962 y = xa > xa0 ? *--xa : 0;
963 d1 = z << k | y >> (32 - k);
965 else {
966 d0 = Exp_1 | y;
967 d1 = z;
969 #else
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;
976 goto ret_d;
978 z = xa > xa0 ? *--xa : 0;
979 w = xa > xa0 ? *--xa : 0;
980 k -= Ebits + 16;
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;
984 #endif
985 ret_d:
986 #ifdef VAX
987 word0(d) = d0 >> 16 | d0 << 16;
988 word1(d) = d1 >> 16 | d1 << 16;
989 #else
990 #undef d0
991 #undef d1
992 #endif
993 return value(d);
997 static Bigint * d2b(double _d, int *e, int *bits)
999 Bigint *b;
1000 int de, i, k;
1001 ULong *x, y, z;
1002 _double d;
1003 #ifdef VAX
1004 ULong d0, d1;
1005 #endif
1007 value(d) = _d;
1008 #ifdef VAX
1009 d0 = word0(d) >> 16 | word0(d) << 16;
1010 d1 = word1(d) >> 16 | word1(d) << 16;
1011 #else
1012 #define d0 word0(d)
1013 #define d1 word1(d)
1014 #endif
1016 #ifdef Pack_32
1017 b = Balloc(1);
1018 #else
1019 b = Balloc(2);
1020 #endif
1021 x = b->x;
1023 z = d0 & Frac_mask;
1024 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1025 #ifdef Sudden_Underflow
1026 de = (int)(d0 >> Exp_shift);
1027 #ifndef IBM
1028 z |= Exp_msk11;
1029 #endif
1030 #else
1031 if ((de = (int)(d0 >> Exp_shift)))
1032 z |= Exp_msk1;
1033 #endif
1034 #ifdef Pack_32
1035 if ((y = d1)) {
1036 if ((k = lo0bits(&y))) {
1037 x[0] = y | (z << (32 - k));
1038 z >>= k;
1039 } else {
1040 x[0] = y;
1042 i = b->wds = (x[1] = z) ? 2 : 1;
1043 } else {
1044 k = lo0bits(&z);
1045 x[0] = z;
1046 i = b->wds = 1;
1047 k += 32;
1049 #else
1050 if (y = d1) {
1051 if (k = lo0bits(&y)) {
1052 if (k >= 16) {
1053 x[0] = y | z << 32 - k & 0xffff;
1054 x[1] = z >> k - 16 & 0xffff;
1055 x[2] = z >> k;
1056 i = 2;
1057 } else {
1058 x[0] = y & 0xffff;
1059 x[1] = y >> 16 | z << 16 - k & 0xffff;
1060 x[2] = z >> k & 0xffff;
1061 x[3] = z >> k+16;
1062 i = 3;
1064 } else {
1065 x[0] = y & 0xffff;
1066 x[1] = y >> 16;
1067 x[2] = z & 0xffff;
1068 x[3] = z >> 16;
1069 i = 3;
1071 } else {
1072 k = lo0bits(&z);
1073 if (k >= 16) {
1074 x[0] = z;
1075 i = 0;
1076 } else {
1077 x[0] = z & 0xffff;
1078 x[1] = z >> 16;
1079 i = 1;
1081 k += 32;
1083 while(!x[i])
1084 --i;
1085 b->wds = i + 1;
1086 #endif
1087 #ifndef Sudden_Underflow
1088 if (de) {
1089 #endif
1090 #ifdef IBM
1091 *e = (de - Bias - (P-1) << 2) + k;
1092 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1093 #else
1094 *e = de - Bias - (P-1) + k;
1095 *bits = P - k;
1096 #endif
1097 #ifndef Sudden_Underflow
1098 } else {
1099 *e = de - Bias - (P-1) + 1 + k;
1100 #ifdef Pack_32
1101 *bits = 32*i - hi0bits(x[i-1]);
1102 #else
1103 *bits = (i+2)*16 - hi0bits(x[i]);
1104 #endif
1106 #endif
1107 return b;
1109 #undef d0
1110 #undef d1
1113 static double ratio (Bigint *a, Bigint *b)
1115 _double da, db;
1116 int k, ka, kb;
1118 value(da) = b2d(a, &ka);
1119 value(db) = b2d(b, &kb);
1120 #ifdef Pack_32
1121 k = ka - kb + 32*(a->wds - b->wds);
1122 #else
1123 k = ka - kb + 16*(a->wds - b->wds);
1124 #endif
1125 #ifdef IBM
1126 if (k > 0) {
1127 word0(da) += (k >> 2)*Exp_msk1;
1128 if (k &= 3) {
1129 da *= 1 << k;
1131 } else {
1132 k = -k;
1133 word0(db) += (k >> 2)*Exp_msk1;
1134 if (k &= 3)
1135 db *= 1 << k;
1137 #else
1138 if (k > 0) {
1139 word0(da) += k*Exp_msk1;
1140 } else {
1141 k = -k;
1142 word0(db) += k*Exp_msk1;
1144 #endif
1145 return value(da) / value(db);
1148 static CONST double
1149 tens[] = {
1150 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1151 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1152 1e20, 1e21, 1e22
1153 #ifdef VAX
1154 , 1e23, 1e24
1155 #endif
1158 #ifdef IEEE_Arith
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 };
1161 #define n_bigtens 5
1162 #else
1163 #ifdef IBM
1164 static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1165 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1166 #define n_bigtens 3
1167 #else
1168 static CONST double bigtens[] = { 1e16, 1e32 };
1169 static CONST double tinytens[] = { 1e-16, 1e-32 };
1170 #define n_bigtens 2
1171 #endif
1172 #endif
1175 static int quorem(Bigint *b, Bigint *S)
1177 int n;
1178 Long borrow, y;
1179 ULong carry, q, ys;
1180 ULong *bx, *bxe, *sx, *sxe;
1181 #ifdef Pack_32
1182 Long z;
1183 ULong si, zs;
1184 #endif
1186 n = S->wds;
1187 if (b->wds < n)
1188 return 0;
1189 sx = S->x;
1190 sxe = sx + --n;
1191 bx = b->x;
1192 bxe = bx + n;
1193 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1194 if (q) {
1195 borrow = 0;
1196 carry = 0;
1197 do {
1198 #ifdef Pack_32
1199 si = *sx++;
1200 ys = (si & 0xffff) * q + carry;
1201 zs = (si >> 16) * q + (ys >> 16);
1202 carry = zs >> 16;
1203 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1204 borrow = y >> 16;
1205 Sign_Extend(borrow, y);
1206 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1207 borrow = z >> 16;
1208 Sign_Extend(borrow, z);
1209 Storeinc(bx, z, y);
1210 #else
1211 ys = *sx++ * q + carry;
1212 carry = ys >> 16;
1213 y = *bx - (ys & 0xffff) + borrow;
1214 borrow = y >> 16;
1215 Sign_Extend(borrow, y);
1216 *bx++ = y & 0xffff;
1217 #endif
1219 while(sx <= sxe);
1220 if (!*bxe) {
1221 bx = b->x;
1222 while(--bxe > bx && !*bxe)
1223 --n;
1224 b->wds = n;
1227 if (cmp(b, S) >= 0) {
1228 q++;
1229 borrow = 0;
1230 carry = 0;
1231 bx = b->x;
1232 sx = S->x;
1233 do {
1234 #ifdef Pack_32
1235 si = *sx++;
1236 ys = (si & 0xffff) + carry;
1237 zs = (si >> 16) + (ys >> 16);
1238 carry = zs >> 16;
1239 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1240 borrow = y >> 16;
1241 Sign_Extend(borrow, y);
1242 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1243 borrow = z >> 16;
1244 Sign_Extend(borrow, z);
1245 Storeinc(bx, z, y);
1246 #else
1247 ys = *sx++ + carry;
1248 carry = ys >> 16;
1249 y = *bx - (ys & 0xffff) + borrow;
1250 borrow = y >> 16;
1251 Sign_Extend(borrow, y);
1252 *bx++ = y & 0xffff;
1253 #endif
1255 while(sx <= sxe);
1256 bx = b->x;
1257 bxe = bx + n;
1258 if (!*bxe) {
1259 while(--bxe > bx && !*bxe)
1260 --n;
1261 b->wds = n;
1264 return q;
1267 void destroy_freelist(void)
1269 int i;
1270 Bigint *tmp;
1272 Bigint **&freelist = s_bigint_data->freelist;
1273 for (i = 0; i <= Kmax; i++) {
1274 Bigint **listp = &freelist[i];
1275 while ((tmp = *listp) != nullptr) {
1276 *listp = tmp->next;
1277 free(tmp);
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);
1289 Bfree(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].
1297 * Modifications:
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
1310 * inequality.
1311 * 4. We remove common factors of powers of 2 from relevant
1312 * quantities.
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
1323 * calculation.
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.
1334 mode:
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
1354 (if applicable).
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;
1365 Long L;
1366 #ifndef Sudden_Underflow
1367 int denorm;
1368 ULong x;
1369 #endif
1370 Bigint *b, *b1, *delta, *mlo = 0, *mhi, *S, *tmp;
1371 double ds;
1372 char *s, *s0;
1373 _double d, d2, eps;
1375 value(d) = _d;
1377 if (word0(d) & Sign_bit) {
1378 /* set sign for everything, including 0's and NaNs */
1379 *sign = 1;
1380 word0(d) &= ~Sign_bit; /* clear sign bit */
1382 else
1383 *sign = 0;
1385 #if defined(IEEE_Arith) + defined(VAX)
1386 #ifdef IEEE_Arith
1387 if ((word0(d) & Exp_mask) == Exp_mask)
1388 #else
1389 if (word0(d) == 0x8000)
1390 #endif
1392 /* Infinity or NaN */
1393 *decpt = 9999;
1394 #ifdef IEEE_Arith
1395 if (!word1(d) && !(word0(d) & 0xfffff))
1396 return nrv_alloc("Infinity", rve, 8);
1397 #endif
1398 return nrv_alloc("NaN", rve, 3);
1400 #endif
1401 #ifdef IBM
1402 value(d) += 0; /* normalize */
1403 #endif
1404 if (!value(d)) {
1405 *decpt = 1;
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));
1412 #else
1413 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
1414 #endif
1415 value(d2) = value(d);
1416 word0(d2) &= Frac_mask1;
1417 word0(d2) |= Exp_11;
1418 #ifdef IBM
1419 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
1420 value(d2) /= 1 << j;
1421 #endif
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.)
1445 i -= Bias;
1446 #ifdef IBM
1447 i <<= 2;
1448 i += j;
1449 #endif
1450 #ifndef Sudden_Underflow
1451 denorm = 0;
1453 else {
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));
1459 value(d2) = x;
1460 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
1461 i -= (Bias + (P-1) - 1) + 1;
1462 denorm = 1;
1464 #endif
1465 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
1466 k = (int)ds;
1467 if (ds < 0. && ds != k)
1468 k--; /* want k = floor(ds) */
1469 k_check = 1;
1470 if (k >= 0 && k <= Ten_pmax) {
1471 if (value(d) < tens[k])
1472 k--;
1473 k_check = 0;
1475 j = bbits - i - 1;
1476 if (j >= 0) {
1477 b2 = 0;
1478 s2 = j;
1480 else {
1481 b2 = -j;
1482 s2 = 0;
1484 if (k >= 0) {
1485 b5 = 0;
1486 s5 = k;
1487 s2 += k;
1489 else {
1490 b2 -= k;
1491 b5 = -k;
1492 s5 = 0;
1494 if (mode < 0 || mode > 9)
1495 mode = 0;
1496 try_quick = 1;
1497 if (mode > 5) {
1498 mode -= 4;
1499 try_quick = 0;
1501 leftright = 1;
1502 switch(mode) {
1503 case 0:
1504 case 1:
1505 ilim = ilim1 = -1;
1506 i = 18;
1507 ndigits = 0;
1508 break;
1509 case 2:
1510 leftright = 0;
1511 /* no break */
1512 case 4:
1513 if (ndigits <= 0)
1514 ndigits = 1;
1515 ilim = ilim1 = i = ndigits;
1516 break;
1517 case 3:
1518 leftright = 0;
1519 /* no break */
1520 case 5:
1521 i = ndigits + k + 1;
1522 ilim = i;
1523 ilim1 = i - 1;
1524 if (i <= 0)
1525 i = 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. */
1533 i = 0;
1534 value(d2) = value(d);
1535 k0 = k;
1536 ilim0 = ilim;
1537 ieps = 2; /* conservative */
1538 if (k > 0) {
1539 ds = tens[k&0xf];
1540 j = k >> 4;
1541 if (j & Bletch) {
1542 /* prevent overflows */
1543 j &= Bletch - 1;
1544 value(d) /= bigtens[n_bigtens-1];
1545 ieps++;
1547 for(; j; j >>= 1, i++)
1548 if (j & 1) {
1549 ieps++;
1550 ds *= bigtens[i];
1552 value(d) /= ds;
1554 else if ((j1 = -k)) {
1555 value(d) *= tens[j1 & 0xf];
1556 for(j = j1 >> 4; j; j >>= 1, i++)
1557 if (j & 1) {
1558 ieps++;
1559 value(d) *= bigtens[i];
1562 if (k_check && value(d) < 1. && ilim > 0) {
1563 if (ilim1 <= 0)
1564 goto fast_failed;
1565 ilim = ilim1;
1566 k--;
1567 value(d) *= 10.;
1568 ieps++;
1570 value(eps) = ieps*value(d) + 7.;
1571 word0(eps) -= (P-1)*Exp_msk1;
1572 if (ilim == 0) {
1573 S = mhi = 0;
1574 value(d) -= 5.;
1575 if (value(d) > value(eps))
1576 goto one_digit;
1577 if (value(d) < -value(eps))
1578 goto no_digits;
1579 goto fast_failed;
1581 #ifndef No_leftright
1582 if (leftright) {
1583 /* Use Steele & White method of only
1584 * generating digits needed.
1586 value(eps) = 0.5/tens[ilim-1] - value(eps);
1587 for(i = 0;;) {
1588 L = (int32_t)value(d);
1589 value(d) -= L;
1590 *s++ = '0' + (int)L;
1591 if (value(d) < value(eps))
1592 goto ret1;
1593 if (1. - value(d) < value(eps))
1594 goto bump_up;
1595 if (++i >= ilim)
1596 break;
1597 value(eps) *= 10.;
1598 value(d) *= 10.;
1601 else {
1602 #endif
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);
1607 value(d) -= L;
1608 *s++ = '0' + (int)L;
1609 if (i == ilim) {
1610 if (value(d) > 0.5 + value(eps))
1611 goto bump_up;
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
1616 if (k < ndigits) {
1617 while(*--s == '0' && (s - s0) > k);
1618 } else {
1619 while(*--s == '0');
1621 s++;
1622 goto ret1;
1624 break;
1627 #ifndef No_leftright
1629 #endif
1630 fast_failed:
1631 s = s0;
1632 value(d) = value(d2);
1633 k = k0;
1634 ilim = ilim0;
1637 /* Do we have a "small" integer? */
1639 if (be >= 0 && k <= Int_max) {
1640 /* Yes. */
1641 ds = tens[k];
1642 if (ndigits < 0 && ilim <= 0) {
1643 S = mhi = 0;
1644 if (ilim < 0 || value(d) <= 5*ds)
1645 goto no_digits;
1646 goto one_digit;
1648 for(i = 1;; i++) {
1649 L = (int32_t)(value(d) / ds);
1650 value(d) -= L*ds;
1651 #ifdef Check_FLT_ROUNDS
1652 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
1653 if (value(d) < 0) {
1654 L--;
1655 value(d) += ds;
1657 #endif
1658 *s++ = '0' + (int)L;
1659 if (i == ilim) {
1660 value(d) += value(d);
1661 if (value(d) > ds || (value(d) == ds && (L & 1))) {
1662 bump_up:
1663 while(*--s == '9')
1664 if (s == s0) {
1665 k++;
1666 *s = '0';
1667 break;
1669 ++*s++;
1671 break;
1673 if (!(value(d) *= 10.))
1674 break;
1676 goto ret1;
1679 m2 = b2;
1680 m5 = b5;
1681 mhi = mlo = 0;
1682 if (leftright) {
1683 if (mode < 2) {
1685 #ifndef Sudden_Underflow
1686 denorm ? be + (Bias + (P-1) - 1 + 1) :
1687 #endif
1688 #ifdef IBM
1689 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
1690 #else
1691 1 + P - bbits;
1692 #endif
1694 else {
1695 j = ilim - 1;
1696 if (m5 >= j)
1697 m5 -= j;
1698 else {
1699 s5 += j -= m5;
1700 b5 += j;
1701 m5 = 0;
1703 if ((i = ilim) < 0) {
1704 m2 -= i;
1705 i = 0;
1708 b2 += i;
1709 s2 += i;
1710 mhi = i2b(1);
1712 if (m2 > 0 && s2 > 0) {
1713 i = m2 < s2 ? m2 : s2;
1714 b2 -= i;
1715 m2 -= i;
1716 s2 -= i;
1718 if (b5 > 0) {
1719 if (leftright) {
1720 if (m5 > 0) {
1721 mhi = pow5mult(mhi, m5);
1722 b1 = mult(mhi, b);
1723 Bfree(b);
1724 b = b1;
1726 if ((j = b5 - m5)) {
1727 b = pow5mult(b, j);
1729 } else {
1730 b = pow5mult(b, b5);
1733 S = i2b(1);
1734 if (s5 > 0)
1735 S = pow5mult(S, s5);
1736 /* Check for special case that d is a normalized power of 2. */
1738 if (mode < 2) {
1739 if (!word1(d) && !(word0(d) & Bndry_mask)
1740 #ifndef Sudden_Underflow
1741 && word0(d) & Exp_mask
1742 #endif
1744 /* The special case */
1745 b2 += Log2P;
1746 s2 += Log2P;
1747 spec_case = 1;
1748 } else {
1749 spec_case = 0;
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.
1760 #ifdef Pack_32
1761 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
1762 i = 32 - i;
1763 #else
1764 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
1765 i = 16 - i;
1766 #endif
1767 if (i > 4) {
1768 i -= 4;
1769 b2 += i;
1770 m2 += i;
1771 s2 += i;
1773 else if (i < 4) {
1774 i += 28;
1775 b2 += i;
1776 m2 += i;
1777 s2 += i;
1779 if (b2 > 0)
1780 b = lshift(b, b2);
1781 if (s2 > 0)
1782 S = lshift(S, s2);
1783 if (k_check) {
1784 if (cmp(b,S) < 0) {
1785 k--;
1786 b = multadd(b, 10, 0); /* we botched the k estimate */
1787 if (leftright)
1788 mhi = multadd(mhi, 10, 0);
1789 ilim = ilim1;
1792 if (ilim <= 0 && mode > 2) {
1793 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
1794 /* no digits, fcvt style */
1795 no_digits:
1796 k = -1 - ndigits;
1797 goto ret;
1799 one_digit:
1800 *s++ = '1';
1801 k++;
1802 goto ret;
1804 if (leftright) {
1805 if (m2 > 0)
1806 mhi = lshift(mhi, m2);
1808 /* Compute mlo -- check for special case
1809 * that d is a normalized power of 2.
1812 mlo = mhi;
1813 if (spec_case) {
1814 mhi = Balloc(mhi->k);
1815 Bcopy(mhi, mlo);
1816 mhi = lshift(mhi, Log2P);
1819 for(i = 1;;i++) {
1820 dig = quorem(b,S) + '0';
1821 /* Do we yet have the shortest decimal string
1822 * that will round to d?
1824 j = cmp(b, mlo);
1825 delta = diff(S, mhi);
1826 j1 = delta->sign ? 1 : cmp(b, delta);
1827 Bfree(delta);
1828 #ifndef ROUND_BIASED
1829 if (j1 == 0 && !mode && !(word1(d) & 1)) {
1830 if (dig == '9')
1831 goto round_9_up;
1832 if (j > 0)
1833 dig++;
1834 *s++ = dig;
1835 goto ret;
1837 #endif
1838 if (j < 0 || (j == 0 && !mode
1839 #ifndef ROUND_BIASED
1840 && !(word1(d) & 1)
1841 #endif
1842 )) {
1843 if (j1 > 0) {
1844 b = lshift(b, 1);
1845 j1 = cmp(b, S);
1846 if ((j1 > 0 || (j1 == 0 && (dig & 1)))
1847 && dig++ == '9')
1848 goto round_9_up;
1850 *s++ = dig;
1851 goto ret;
1853 if (j1 > 0) {
1854 if (dig == '9') { /* possible if i == 1 */
1855 round_9_up:
1856 *s++ = '9';
1857 goto roundoff;
1859 *s++ = dig + 1;
1860 goto ret;
1862 *s++ = dig;
1863 if (i == ilim)
1864 break;
1865 b = multadd(b, 10, 0);
1866 if (mlo == mhi)
1867 mlo = mhi = multadd(mhi, 10, 0);
1868 else {
1869 mlo = multadd(mlo, 10, 0);
1870 mhi = multadd(mhi, 10, 0);
1874 else
1875 for(i = 1;; i++) {
1876 *s++ = dig = quorem(b,S) + '0';
1877 if (i >= ilim)
1878 break;
1879 b = multadd(b, 10, 0);
1882 /* Round off last digit */
1884 b = lshift(b, 1);
1885 j = cmp(b, S);
1886 if (j > 0 || (j == 0 && (dig & 1))) {
1887 roundoff:
1888 while(*--s == '9')
1889 if (s == s0) {
1890 k++;
1891 *s++ = '1';
1892 goto ret;
1894 ++*s++;
1896 else {
1897 while(*--s == '0');
1898 s++;
1900 ret:
1901 Bfree(S);
1902 if (mhi) {
1903 if (mlo && mlo != mhi)
1904 Bfree(mlo);
1905 Bfree(mhi);
1907 ret1:
1910 Bigint *&p5s = s_bigint_data->p5s;
1911 while (p5s) {
1912 tmp = p5s;
1913 p5s = p5s->next;
1914 free(tmp);
1918 Bfree(b);
1920 if (s == s0) { /* don't return empty string */
1921 *s++ = '0';
1922 k = 0;
1924 *s = 0;
1925 *decpt = k + 1;
1926 if (rve)
1927 *rve = s;
1928 return s0;
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;
1937 _double rv, rv0;
1938 Long L;
1939 ULong y, z;
1940 Bigint *bb = 0, *bb1, *bd = 0, *bd0, *bs = 0, *delta = 0, *tmp;
1941 double result;
1943 CONST char decimal_point = '.';
1945 sign = nz0 = nz = 0;
1946 value(rv) = 0.;
1949 for(s = s00; isspace((unsigned char) *s); s++)
1952 if (*s == '-') {
1953 sign = 1;
1954 s++;
1955 } else if (*s == '+') {
1956 s++;
1959 if (*s == '\0') {
1960 s = s00;
1961 goto ret;
1964 if (*s == '0') {
1965 nz0 = 1;
1966 while(*++s == '0') ;
1967 if (!*s)
1968 goto ret;
1970 s0 = s;
1971 y = z = 0;
1972 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1973 if (nd < 9)
1974 y = 10*y + c - '0';
1975 else if (nd < 16)
1976 z = 10*z + c - '0';
1977 nd0 = nd;
1978 if (c == decimal_point) {
1979 c = *++s;
1980 if (!nd) {
1981 for(; c == '0'; c = *++s)
1982 nz++;
1983 if (c > '0' && c <= '9') {
1984 s0 = s;
1985 nf += nz;
1986 nz = 0;
1987 goto have_dig;
1989 goto dig_done;
1991 for(; c >= '0' && c <= '9'; c = *++s) {
1992 have_dig:
1993 nz++;
1994 if (c -= '0') {
1995 nf += nz;
1996 for(i = 1; i < nz; i++)
1997 if (nd++ < 9)
1998 y *= 10;
1999 else if (nd <= DBL_DIG + 1)
2000 z *= 10;
2001 if (nd++ < 9)
2002 y = 10*y + c;
2003 else if (nd <= DBL_DIG + 1)
2004 z = 10*z + c;
2005 nz = 0;
2009 dig_done:
2010 e = 0;
2011 if (c == 'e' || c == 'E') {
2012 if (!nd && !nz && !nz0) {
2013 s = s00;
2014 goto ret;
2016 s00 = s;
2017 esign = 0;
2018 switch(c = *++s) {
2019 case '-':
2020 esign = 1;
2021 case '+':
2022 c = *++s;
2024 if (c >= '0' && c <= '9') {
2025 while(c == '0')
2026 c = *++s;
2027 if (c > '0' && c <= '9') {
2028 L = c - '0';
2029 s1 = s;
2030 while((c = *++s) >= '0' && c <= '9')
2031 L = 10*L + c - '0';
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 */
2037 else
2038 e = (int)L;
2039 if (esign)
2040 e = -e;
2042 else
2043 e = 0;
2045 else
2046 s = s00;
2048 if (!nd) {
2049 if (!nz && !nz0)
2050 s = s00;
2051 goto ret;
2053 e1 = e -= nf;
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
2058 * 10**e */
2060 if (!nd0)
2061 nd0 = nd;
2062 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
2063 value(rv) = y;
2064 if (k > 9)
2065 value(rv) = tens[k - 9] * value(rv) + z;
2066 bd0 = 0;
2067 if (nd <= DBL_DIG
2068 #ifndef RND_PRODQUOT
2069 && FLT_ROUNDS == 1
2070 #endif
2072 if (!e)
2073 goto ret;
2074 if (e > 0) {
2075 if (e <= Ten_pmax) {
2076 #ifdef VAX
2077 goto vax_ovfl_check;
2078 #else
2079 /* value(rv) = */ rounded_product(value(rv),
2080 tens[e]);
2081 goto ret;
2082 #endif
2084 i = DBL_DIG - nd;
2085 if (e <= Ten_pmax + i) {
2086 /* A fancier test would sometimes let us do
2087 * this for larger i values.
2089 e -= i;
2090 value(rv) *= tens[i];
2091 #ifdef VAX
2092 /* VAX exponent range is so narrow we must
2093 * worry about overflow here...
2095 vax_ovfl_check:
2096 word0(rv) -= P*Exp_msk1;
2097 /* value(rv) = */ rounded_product(value(rv),
2098 tens[e]);
2099 if ((word0(rv) & Exp_mask)
2100 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2101 goto ovfl;
2102 word0(rv) += P*Exp_msk1;
2103 #else
2104 /* value(rv) = */ rounded_product(value(rv),
2105 tens[e]);
2106 #endif
2107 goto ret;
2110 #ifndef Inaccurate_Divide
2111 else if (e >= -Ten_pmax) {
2112 /* value(rv) = */ rounded_quotient(value(rv),
2113 tens[-e]);
2114 goto ret;
2116 #endif
2118 e1 += nd - k;
2120 /* Get starting approximation = rv * 10**e1 */
2122 if (e1 > 0) {
2123 if ((i = e1 & 15))
2124 value(rv) *= tens[i];
2125 if (e1 &= ~15) {
2126 if (e1 > DBL_MAX_10_EXP) {
2127 ovfl:
2128 errno = ERANGE;
2129 #ifndef Bad_float_h
2130 value(rv) = HUGE_VAL;
2131 #else
2132 /* Can't trust HUGE_VAL */
2133 #ifdef IEEE_Arith
2134 word0(rv) = Exp_mask;
2135 word1(rv) = 0;
2136 #else
2137 word0(rv) = Big0;
2138 word1(rv) = Big1;
2139 #endif
2140 #endif
2141 if (bd0)
2142 goto retfree;
2143 goto ret;
2145 if (e1 >>= 4) {
2146 for(j = 0; e1 > 1; j++, e1 >>= 1)
2147 if (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))
2154 goto ovfl;
2155 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2156 /* set to largest number */
2157 /* (Can't trust DBL_MAX) */
2158 word0(rv) = Big0;
2159 word1(rv) = Big1;
2161 else
2162 word0(rv) += P*Exp_msk1;
2167 else if (e1 < 0) {
2168 e1 = -e1;
2169 if ((i = e1 & 15))
2170 value(rv) /= tens[i];
2171 if (e1 &= ~15) {
2172 e1 >>= 4;
2173 if (e1 >= 1 << n_bigtens)
2174 goto undfl;
2175 for(j = 0; e1 > 1; j++, e1 >>= 1)
2176 if (e1 & 1)
2177 value(rv) *= tinytens[j];
2178 /* The last multiplication could underflow. */
2179 value(rv0) = value(rv);
2180 value(rv) *= tinytens[j];
2181 if (!value(rv)) {
2182 value(rv) = 2.*value(rv0);
2183 value(rv) *= tinytens[j];
2184 if (!value(rv)) {
2185 undfl:
2186 value(rv) = 0.;
2187 errno = ERANGE;
2188 if (bd0)
2189 goto retfree;
2190 goto ret;
2192 word0(rv) = Tiny0;
2193 word1(rv) = Tiny1;
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);
2207 for(;;) {
2208 bd = Balloc(bd0->k);
2209 Bcopy(bd, bd0);
2210 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
2211 bs = i2b(1);
2213 if (e >= 0) {
2214 bb2 = bb5 = 0;
2215 bd2 = bd5 = e;
2217 else {
2218 bb2 = bb5 = -e;
2219 bd2 = bd5 = 0;
2221 if (bbe >= 0)
2222 bb2 += bbe;
2223 else
2224 bd2 -= bbe;
2225 bs2 = bb2;
2226 #ifdef Sudden_Underflow
2227 #ifdef IBM
2228 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2229 #else
2230 j = P + 1 - bbbits;
2231 #endif
2232 #else
2233 i = bbe + bbbits - 1; /* logb(rv) */
2234 if (i < Emin) /* denormal */
2235 j = bbe + (P-Emin);
2236 else
2237 j = P + 1 - bbbits;
2238 #endif
2239 bb2 += j;
2240 bd2 += j;
2241 i = bb2 < bd2 ? bb2 : bd2;
2242 if (i > bs2)
2243 i = bs2;
2244 if (i > 0) {
2245 bb2 -= i;
2246 bd2 -= i;
2247 bs2 -= i;
2249 if (bb5 > 0) {
2250 bs = pow5mult(bs, bb5);
2251 bb1 = mult(bs, bb);
2252 Bfree(bb);
2253 bb = bb1;
2255 if (bb2 > 0)
2256 bb = lshift(bb, bb2);
2257 if (bd5 > 0)
2258 bd = pow5mult(bd, bd5);
2259 if (bd2 > 0)
2260 bd = lshift(bd, bd2);
2261 if (bs2 > 0)
2262 bs = lshift(bs, bs2);
2263 delta = diff(bb, bd);
2264 dsign = delta->sign;
2265 delta->sign = 0;
2266 i = cmp(delta, bs);
2267 if (i < 0) {
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)
2272 break;
2273 delta = lshift(delta,Log2P);
2274 if (cmp(delta, bs) > 0)
2275 goto drop_down;
2276 break;
2278 if (i == 0) {
2279 /* exactly half-way between */
2280 if (dsign) {
2281 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2282 && word1(rv) == 0xffffffff) {
2283 /*boundary case -- increment exponent*/
2284 word0(rv) = (word0(rv) & Exp_mask)
2285 + Exp_msk1
2286 #ifdef IBM
2287 | Exp_msk1 >> 4
2288 #endif
2290 word1(rv) = 0;
2291 break;
2294 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2295 drop_down:
2296 /* boundary case -- decrement exponent */
2297 #ifdef Sudden_Underflow
2298 L = word0(rv) & Exp_mask;
2299 #ifdef IBM
2300 if (L < Exp_msk1)
2301 #else
2302 if (L <= Exp_msk1)
2303 #endif
2304 goto undfl;
2305 L -= Exp_msk1;
2306 #else
2307 L = (word0(rv) & Exp_mask) - Exp_msk1;
2308 #endif
2309 word0(rv) = L | Bndry_mask1;
2310 word1(rv) = 0xffffffff;
2311 #ifdef IBM
2312 goto cont;
2313 #else
2314 break;
2315 #endif
2317 #ifndef ROUND_BIASED
2318 if (!(word1(rv) & LSB))
2319 break;
2320 #endif
2321 if (dsign)
2322 value(rv) += ulp(value(rv));
2323 #ifndef ROUND_BIASED
2324 else {
2325 value(rv) -= ulp(value(rv));
2326 #ifndef Sudden_Underflow
2327 if (!value(rv))
2328 goto undfl;
2329 #endif
2331 #endif
2332 break;
2334 if ((aadj = ratio(delta, bs)) <= 2.) {
2335 if (dsign)
2336 aadj = aadj1 = 1.;
2337 else if (word1(rv) || word0(rv) & Bndry_mask) {
2338 #ifndef Sudden_Underflow
2339 if (word1(rv) == Tiny1 && !word0(rv))
2340 goto undfl;
2341 #endif
2342 aadj = 1.;
2343 aadj1 = -1.;
2345 else {
2346 /* special case -- power of FLT_RADIX to be */
2347 /* rounded down... */
2349 if (aadj < 2./FLT_RADIX)
2350 aadj = 1./FLT_RADIX;
2351 else
2352 aadj *= 0.5;
2353 aadj1 = -aadj;
2356 else {
2357 aadj *= 0.5;
2358 aadj1 = dsign ? aadj : -aadj;
2359 #ifdef Check_FLT_ROUNDS
2360 switch(FLT_ROUNDS) {
2361 case 2: /* towards +infinity */
2362 aadj1 -= 0.5;
2363 break;
2364 case 0: /* towards 0 */
2365 case 3: /* towards -infinity */
2366 aadj1 += 0.5;
2368 #else
2369 if (FLT_ROUNDS == 0)
2370 aadj1 += 0.5;
2371 #endif
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));
2381 value(rv) += adj;
2382 if ((word0(rv) & Exp_mask) >=
2383 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2384 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2385 goto ovfl;
2386 word0(rv) = Big0;
2387 word1(rv) = Big1;
2388 goto cont;
2390 else
2391 word0(rv) += P*Exp_msk1;
2393 else {
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));
2399 value(rv) += adj;
2400 #ifdef IBM
2401 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2402 #else
2403 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2404 #endif
2406 if (word0(rv0) == Tiny0
2407 && word1(rv0) == Tiny1)
2408 goto undfl;
2409 word0(rv) = Tiny0;
2410 word1(rv) = Tiny1;
2411 goto cont;
2413 else
2414 word0(rv) -= P*Exp_msk1;
2416 else {
2417 adj = aadj1 * ulp(value(rv));
2418 value(rv) += adj;
2420 #else
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);
2430 if (!dsign)
2431 aadj1 = -aadj1;
2433 adj = aadj1 * ulp(value(rv));
2434 value(rv) += adj;
2435 #endif
2437 z = word0(rv) & Exp_mask;
2438 if (y == z) {
2439 /* Can we stop now? */
2440 L = (int32_t)aadj;
2441 aadj -= L;
2442 /* The tolerances below are conservative. */
2443 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2444 if (aadj < .4999999 || aadj > .5000001)
2445 break;
2447 else if (aadj < .4999999/FLT_RADIX)
2448 break;
2450 cont:
2451 Bfree(bb);
2452 Bfree(bd);
2453 Bfree(bs);
2454 Bfree(delta);
2456 retfree:
2457 Bfree(bb);
2458 Bfree(bd);
2459 Bfree(bs);
2460 Bfree(bd0);
2461 Bfree(delta);
2462 ret:
2463 if (se)
2464 *se = (char *)s;
2465 result = sign ? -value(rv) : value(rv);
2467 Bigint *&p5s = s_bigint_data->p5s;
2468 while (p5s) {
2469 tmp = p5s;
2470 p5s = p5s->next;
2471 free(tmp);
2474 return result;
2477 double zend_hex_strtod(const char *str, char **endptr)
2479 const char *s = str;
2480 char c;
2481 int any = 0;
2482 double value = 0;
2484 if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
2485 s += 2;
2488 while ((c = *s++)) {
2489 if (c >= '0' && c <= '9') {
2490 c -= '0';
2491 } else if (c >= 'A' && c <= 'F') {
2492 c -= 'A' - 10;
2493 } else if (c >= 'a' && c <= 'f') {
2494 c -= 'a' - 10;
2495 } else {
2496 break;
2499 any = 1;
2500 value = value * 16 + c;
2503 if (endptr != nullptr) {
2504 *endptr = (char *)(any ? s - 1 : str);
2507 return value;
2510 double zend_oct_strtod(const char *str, char **endptr)
2512 const char *s = str;
2513 char c;
2514 double value = 0;
2515 int any = 0;
2517 /* skip leading zero */
2518 s++;
2520 while ((c = *s++)) {
2521 if (c > '7') {
2522 /* break and return the current value if the number is not well-formed
2523 * that's what Linux strtol() does
2525 break;
2527 value = value * 8 + c - '0';
2528 any = 1;
2531 if (endptr != nullptr) {
2532 *endptr = (char *)(any ? s - 1 : str);
2535 return value;
2538 ///////////////////////////////////////////////////////////////////////////////