Move the job of converting provided coeffects to ambient coeffects from callee to...
[hiphop-php.git] / hphp / zend / zend-strtod.cpp
blob8f87d12c812ae4539eb39364cd9c5385ee4fbf88
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/zend/zend-strtod.h"
94 #include "hphp/util/assertions.h"
95 #include "hphp/util/rds-local.h"
97 #include <new>
98 #include <stdint.h>
99 #include <stdlib.h>
100 #include <string.h>
102 namespace HPHP {
103 ///////////////////////////////////////////////////////////////////////////////
105 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
106 # if defined(__LITTLE_ENDIAN__)
107 # undef WORDS_BIGENDIAN
108 # else
109 # if defined(__BIG_ENDIAN__)
110 # define WORDS_BIGENDIAN
111 # endif
112 # endif
113 #endif
115 #ifdef WORDS_BIGENDIAN
116 #define IEEE_BIG_ENDIAN
117 #else
118 #define IEEE_LITTLE_ENDIAN
119 #endif
121 #if defined(__arm__) && !defined(__VFP_FP__)
123 * * Although the CPU is little endian the FP has different
124 * * byte and word endianness. The byte order is still little endian
125 * * but the word order is big endian.
126 * */
127 #define IEEE_BIG_ENDIAN
128 #undef IEEE_LITTLE_ENDIAN
129 #endif
131 #ifdef __vax__
132 #define VAX
133 #endif
135 #if defined(_MSC_VER)
136 #define int32_t __int32
137 #define uint32_t unsigned __int32
138 #define IEEE_LITTLE_ENDIAN
139 #endif
141 #define Long int32_t
142 #define ULong uint32_t
144 #ifdef MALLOC
145 #ifdef KR_headers
146 extern char *MALLOC();
147 #else
148 extern void *MALLOC(size_t);
149 #endif
150 #else
151 #define MALLOC malloc
152 #endif
154 } // namespace HPHP
156 #include <ctype.h>
157 #include <errno.h>
159 namespace HPHP {
161 #ifdef Bad_float_h
162 #ifdef IEEE_BIG_ENDIAN
163 #define IEEE_ARITHMETIC
164 #endif
165 #ifdef IEEE_LITTLE_ENDIAN
166 #define IEEE_ARITHMETIC
167 #endif
169 #ifdef IEEE_ARITHMETIC
170 #define DBL_DIG 15
171 #define DBL_MAX_10_EXP 308
172 #define DBL_MAX_EXP 1024
173 #define FLT_RADIX 2
174 #define FLT_ROUNDS 1
175 #define DBL_MAX 1.7976931348623157e+308
176 #endif
178 #ifdef IBM
179 #define DBL_DIG 16
180 #define DBL_MAX_10_EXP 75
181 #define DBL_MAX_EXP 63
182 #define FLT_RADIX 16
183 #define FLT_ROUNDS 0
184 #define DBL_MAX 7.2370055773322621e+75
185 #endif
187 #ifdef VAX
188 #define DBL_DIG 16
189 #define DBL_MAX_10_EXP 38
190 #define DBL_MAX_EXP 127
191 #define FLT_RADIX 2
192 #define FLT_ROUNDS 1
193 #define DBL_MAX 1.7014118346046923e+38
194 #endif
197 #ifndef LONG_MAX
198 #define LONG_MAX 2147483647
199 #endif
200 #else
201 } // namespace HPHP
203 #include <float.h>
205 namespace HPHP {
206 #endif
207 #ifndef __MATH_H__
208 } // namespace HPHP
210 #include <math.h>
212 namespace HPHP {
213 #endif
215 #ifndef CONST
216 #ifdef KR_headers
217 #define CONST /* blank */
218 #else
219 #define CONST const
220 #endif
221 #endif
223 #ifdef Unsigned_Shifts
224 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
225 #else
226 #define Sign_Extend(a,b) /*no-op*/
227 #endif
229 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
230 defined(IBM) != 1
231 #error Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM \
232 should be defined.
233 #endif
235 typedef union {
236 double d;
237 ULong ul[2];
238 } _double;
239 #define value(x) ((x).d)
240 #ifdef IEEE_LITTLE_ENDIAN
241 #define word0(x) ((x).ul[1])
242 #define word1(x) ((x).ul[0])
243 #else
244 #define word0(x) ((x).ul[0])
245 #define word1(x) ((x).ul[1])
246 #endif
248 /* The following definition of Storeinc is appropriate for MIPS processors.
249 * An alternative that might be better on some machines is
250 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
252 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
253 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
254 ((unsigned short *)a)[0] = (unsigned short)c, a++)
255 #else
256 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
257 ((unsigned short *)a)[1] = (unsigned short)c, a++)
258 #endif
260 /* #define P DBL_MANT_DIG */
261 /* Ten_pmax = floor(P*log(2)/log(5)) */
262 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
263 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
264 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
266 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
267 #define Exp_shift 20
268 #define Exp_shift1 20
269 #define Exp_msk1 0x100000
270 #define Exp_msk11 0x100000
271 #define Exp_mask 0x7ff00000
272 #define P 53
273 #define Bias 1023
274 #define IEEE_Arith
275 #define Emin (-1022)
276 #define Exp_1 0x3ff00000
277 #define Exp_11 0x3ff00000
278 #define Ebits 11
279 #define Frac_mask 0xfffff
280 #define Frac_mask1 0xfffff
281 #define Ten_pmax 22
282 #define Bletch 0x10
283 #define Bndry_mask 0xfffff
284 #define Bndry_mask1 0xfffff
285 #define LSB 1
286 #define Sign_bit 0x80000000
287 #define Log2P 1
288 #define Tiny0 0
289 #define Tiny1 1
290 #define Quick_max 14
291 #define Int_max 14
292 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
293 #else
294 #undef Sudden_Underflow
295 #define Sudden_Underflow
296 #ifdef IBM
297 #define Exp_shift 24
298 #define Exp_shift1 24
299 #define Exp_msk1 0x1000000
300 #define Exp_msk11 0x1000000
301 #define Exp_mask 0x7f000000
302 #define P 14
303 #define Bias 65
304 #define Exp_1 0x41000000
305 #define Exp_11 0x41000000
306 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
307 #define Frac_mask 0xffffff
308 #define Frac_mask1 0xffffff
309 #define Bletch 4
310 #define Ten_pmax 22
311 #define Bndry_mask 0xefffff
312 #define Bndry_mask1 0xffffff
313 #define LSB 1
314 #define Sign_bit 0x80000000
315 #define Log2P 4
316 #define Tiny0 0x100000
317 #define Tiny1 0
318 #define Quick_max 14
319 #define Int_max 15
320 #else /* VAX */
321 #define Exp_shift 23
322 #define Exp_shift1 7
323 #define Exp_msk1 0x80
324 #define Exp_msk11 0x800000
325 #define Exp_mask 0x7f80
326 #define P 56
327 #define Bias 129
328 #define Exp_1 0x40800000
329 #define Exp_11 0x4080
330 #define Ebits 8
331 #define Frac_mask 0x7fffff
332 #define Frac_mask1 0xffff007f
333 #define Ten_pmax 24
334 #define Bletch 2
335 #define Bndry_mask 0xffff007f
336 #define Bndry_mask1 0xffff007f
337 #define LSB 0x10000
338 #define Sign_bit 0x8000
339 #define Log2P 1
340 #define Tiny0 0x80
341 #define Tiny1 0
342 #define Quick_max 15
343 #define Int_max 15
344 #endif
345 #endif
347 #ifndef IEEE_Arith
348 #define ROUND_BIASED
349 #endif
351 #ifdef RND_PRODQUOT
352 #define rounded_product(a,b) a = rnd_prod(a, b)
353 #define rounded_quotient(a,b) a = rnd_quot(a, b)
354 #ifdef KR_headers
355 extern double rnd_prod(), rnd_quot();
356 #else
357 extern double rnd_prod(double, double), rnd_quot(double, double);
358 #endif
359 #else
360 #define rounded_product(a,b) a *= b
361 #define rounded_quotient(a,b) a /= b
362 #endif
364 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
365 #define Big1 0xffffffff
367 #ifndef Just_16
368 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
369 * * This makes some inner loops simpler and sometimes saves work
370 * * during multiplications, but it often seems to make things slightly
371 * * slower. Hence the default is now to store 32 bits per Long.
372 * */
373 #ifndef Pack_32
374 #define Pack_32
375 #endif
376 #endif
378 #define Kmax 15
380 namespace {
382 struct Bigint {
383 struct Bigint *next;
384 int k, maxwds, sign, wds;
385 ULong x[1];
388 typedef struct Bigint Bigint;
390 } // namespace
392 void destroy_freelist(Bigint** freelist);
394 struct BigintData {
395 BigintData() : p5s(nullptr) {
396 freelist = (Bigint **)calloc(Kmax + 1, sizeof(Bigint *));
398 ~BigintData() {
399 destroy_freelist(freelist);
400 free(freelist);
403 Bigint **freelist;
404 Bigint *p5s;
406 static RDS_LOCAL_NO_CHECK(BigintData, s_bigint_data);
408 // NOTE: If this has not been called, various functions in this file,
409 // and in other files (e.g. "zend-printf.cpp"), will crash when called.
410 void zend_get_bigint_data() {
411 s_bigint_data.getCheck();
414 static Bigint * Balloc(int k)
416 int x;
417 Bigint *rv;
419 assertx(k <= Kmax);
421 Bigint **&freelist = s_bigint_data->freelist;
422 if ((rv = freelist[k])) {
423 freelist[k] = rv->next;
424 } else {
425 x = 1 << k;
426 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
427 assertx(rv != nullptr);
428 rv->k = k;
429 rv->maxwds = x;
431 rv->sign = rv->wds = 0;
432 return rv;
435 static void Bfree(Bigint *v)
437 if (v) {
438 Bigint **&freelist = s_bigint_data->freelist;
439 v->next = freelist[v->k];
440 freelist[v->k] = v;
444 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
445 y->wds*sizeof(Long) + 2*sizeof(int))
447 /* return value is only used as a simple string, so mis-aligned parts
448 * inside the Bigint are not at risk on strict align architectures
450 static char * rv_alloc(int i) {
451 int j, k, *r;
453 j = sizeof(ULong);
454 for(k = 0;
455 (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j) <= i;
456 j <<= 1) {
457 k++;
459 r = (int*)Balloc(k);
460 *r = k;
461 return (char *)(r+1);
465 static char * nrv_alloc(const char *s, char **rve, int n)
467 char *rv, *t;
469 t = rv = rv_alloc(n);
470 while((*t = *s++) !=0) {
471 t++;
473 if (rve) {
474 *rve = t;
476 return rv;
479 static Bigint * multadd(Bigint *b, int m, int a) /* multiply by m and add a */
481 int i, wds;
482 ULong *x, y;
483 #ifdef Pack_32
484 ULong xi, z;
485 #endif
486 Bigint *b1;
488 wds = b->wds;
489 x = b->x;
490 i = 0;
491 do {
492 #ifdef Pack_32
493 xi = *x;
494 y = (xi & 0xffff) * m + a;
495 z = (xi >> 16) * m + (y >> 16);
496 a = (int)(z >> 16);
497 *x++ = (z << 16) + (y & 0xffff);
498 #else
499 y = *x * m + a;
500 a = (int)(y >> 16);
501 *x++ = y & 0xffff;
502 #endif
504 while(++i < wds);
505 if (a) {
506 if (wds >= b->maxwds) {
507 b1 = Balloc(b->k+1);
508 Bcopy(b1, b);
509 Bfree(b);
510 b = b1;
512 b->x[wds++] = a;
513 b->wds = wds;
515 return b;
518 static int hi0bits(ULong x)
520 int k = 0;
522 if (!(x & 0xffff0000)) {
523 k = 16;
524 x <<= 16;
526 if (!(x & 0xff000000)) {
527 k += 8;
528 x <<= 8;
530 if (!(x & 0xf0000000)) {
531 k += 4;
532 x <<= 4;
534 if (!(x & 0xc0000000)) {
535 k += 2;
536 x <<= 2;
538 if (!(x & 0x80000000)) {
539 k++;
540 if (!(x & 0x40000000)) {
541 return 32;
544 return k;
547 static int lo0bits(ULong *y)
549 int k;
550 ULong x = *y;
552 if (x & 7) {
553 if (x & 1) {
554 return 0;
556 if (x & 2) {
557 *y = x >> 1;
558 return 1;
560 *y = x >> 2;
561 return 2;
563 k = 0;
564 if (!(x & 0xffff)) {
565 k = 16;
566 x >>= 16;
568 if (!(x & 0xff)) {
569 k += 8;
570 x >>= 8;
572 if (!(x & 0xf)) {
573 k += 4;
574 x >>= 4;
576 if (!(x & 0x3)) {
577 k += 2;
578 x >>= 2;
580 if (!(x & 1)) {
581 k++;
582 x >>= 1;
583 if (!(x & 1)) {
584 return 32;
587 *y = x;
588 return k;
591 static Bigint * i2b(int i)
593 Bigint *b;
595 b = Balloc(1);
596 b->x[0] = i;
597 b->wds = 1;
598 return b;
601 static Bigint * mult(Bigint *a, Bigint *b)
603 Bigint *c;
604 int k, wa, wb, wc;
605 ULong carry, y, z;
606 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
607 #ifdef Pack_32
608 ULong z2;
609 #endif
611 if (a->wds < b->wds) {
612 c = a;
613 a = b;
614 b = c;
616 k = a->k;
617 wa = a->wds;
618 wb = b->wds;
619 wc = wa + wb;
620 if (wc > a->maxwds) {
621 k++;
623 c = Balloc(k);
624 for(x = c->x, xa = x + wc; x < xa; x++) {
625 *x = 0;
627 xa = a->x;
628 xae = xa + wa;
629 xb = b->x;
630 xbe = xb + wb;
631 xc0 = c->x;
632 #ifdef Pack_32
633 for(; xb < xbe; xb++, xc0++) {
634 if ((y = *xb & 0xffff)) {
635 x = xa;
636 xc = xc0;
637 carry = 0;
638 do {
639 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
640 carry = z >> 16;
641 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
642 carry = z2 >> 16;
643 Storeinc(xc, z2, z);
645 while(x < xae);
646 *xc = carry;
648 if ((y = *xb >> 16)) {
649 x = xa;
650 xc = xc0;
651 carry = 0;
652 z2 = *xc;
653 do {
654 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
655 carry = z >> 16;
656 Storeinc(xc, z, z2);
657 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
658 carry = z2 >> 16;
660 while(x < xae);
661 *xc = z2;
664 #else
665 for(; xb < xbe; xc0++) {
666 if (y = *xb++) {
667 x = xa;
668 xc = xc0;
669 carry = 0;
670 do {
671 z = *x++ * y + *xc + carry;
672 carry = z >> 16;
673 *xc++ = z & 0xffff;
675 while(x < xae);
676 *xc = carry;
679 #endif
680 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
681 c->wds = wc;
682 return c;
685 static Bigint * s2b (CONST char *s, int nd0, int nd, ULong y9)
687 Bigint *b;
688 int i, k;
689 Long x, y;
691 x = (nd + 8) / 9;
692 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
693 #ifdef Pack_32
694 b = Balloc(k);
695 b->x[0] = y9;
696 b->wds = 1;
697 #else
698 b = Balloc(k+1);
699 b->x[0] = y9 & 0xffff;
700 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
701 #endif
703 i = 9;
704 if (9 < nd0) {
705 s += 9;
706 do b = multadd(b, 10, *s++ - '0');
707 while(++i < nd0);
708 s++;
709 } else {
710 s += 10;
712 for(; i < nd; i++) {
713 b = multadd(b, 10, *s++ - '0');
715 return b;
718 static Bigint * pow5mult(Bigint *b, int k)
720 Bigint *b1, *p5, *p51;
721 int i;
722 static int p05[3] = { 5, 25, 125 };
724 Bigint *&p5s = s_bigint_data->p5s;
725 if ((i = k & 3)) {
726 b = multadd(b, p05[i-1], 0);
729 if (!(k >>= 2)) {
730 return b;
732 if (!(p5 = p5s)) {
733 /* first time */
734 p5 = p5s = i2b(625);
735 p5->next = 0;
737 for(;;) {
738 if (k & 1) {
739 b1 = mult(b, p5);
740 Bfree(b);
741 b = b1;
743 if (!(k >>= 1)) {
744 break;
746 if (!(p51 = p5->next)) {
747 if (!(p51 = p5->next)) {
748 p51 = p5->next = mult(p5,p5);
749 p51->next = 0;
752 p5 = p51;
754 return b;
758 static Bigint *lshift(Bigint *b, int k)
760 int i, k1, n, n1;
761 Bigint *b1;
762 ULong *x, *x1, *xe, z;
764 #ifdef Pack_32
765 n = k >> 5;
766 #else
767 n = k >> 4;
768 #endif
769 k1 = b->k;
770 n1 = n + b->wds + 1;
771 for(i = b->maxwds; n1 > i; i <<= 1) {
772 k1++;
774 b1 = Balloc(k1);
775 x1 = b1->x;
776 for(i = 0; i < n; i++) {
777 *x1++ = 0;
779 x = b->x;
780 xe = x + b->wds;
781 #ifdef Pack_32
782 if (k &= 0x1f) {
783 k1 = 32 - k;
784 z = 0;
785 do {
786 *x1++ = *x << k | z;
787 z = *x++ >> k1;
789 while(x < xe);
790 if ((*x1 = z)) {
791 ++n1;
794 #else
795 if (k &= 0xf) {
796 k1 = 16 - k;
797 z = 0;
798 do {
799 *x1++ = *x << k & 0xffff | z;
800 z = *x++ >> k1;
802 while(x < xe);
803 if (*x1 = z) {
804 ++n1;
807 #endif
808 else do
809 *x1++ = *x++;
810 while(x < xe);
811 b1->wds = n1 - 1;
812 Bfree(b);
813 return b1;
816 static int cmp(Bigint *a, Bigint *b)
818 ULong *xa, *xa0, *xb, *xb0;
819 int i, j;
821 i = a->wds;
822 j = b->wds;
823 if (i -= j)
824 return i;
825 xa0 = a->x;
826 xa = xa0 + j;
827 xb0 = b->x;
828 xb = xb0 + j;
829 for(;;) {
830 if (*--xa != *--xb)
831 return *xa < *xb ? -1 : 1;
832 if (xa <= xa0)
833 break;
835 return 0;
839 static Bigint * diff(Bigint *a, Bigint *b)
841 Bigint *c;
842 int i, wa, wb;
843 Long borrow, y; /* We need signed shifts here. */
844 ULong *xa, *xae, *xb, *xbe, *xc;
845 #ifdef Pack_32
846 Long z;
847 #endif
849 i = cmp(a,b);
850 if (!i) {
851 c = Balloc(0);
852 c->wds = 1;
853 c->x[0] = 0;
854 return c;
856 if (i < 0) {
857 c = a;
858 a = b;
859 b = c;
860 i = 1;
861 } else {
862 i = 0;
864 c = Balloc(a->k);
865 c->sign = i;
866 wa = a->wds;
867 xa = a->x;
868 xae = xa + wa;
869 wb = b->wds;
870 xb = b->x;
871 xbe = xb + wb;
872 xc = c->x;
873 borrow = 0;
874 #ifdef Pack_32
875 do {
876 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
877 borrow = y >> 16;
878 Sign_Extend(borrow, y);
879 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
880 borrow = z >> 16;
881 Sign_Extend(borrow, z);
882 Storeinc(xc, z, y);
883 } while(xb < xbe);
884 while(xa < xae) {
885 y = (*xa & 0xffff) + borrow;
886 borrow = y >> 16;
887 Sign_Extend(borrow, y);
888 z = (*xa++ >> 16) + borrow;
889 borrow = z >> 16;
890 Sign_Extend(borrow, z);
891 Storeinc(xc, z, y);
893 #else
894 do {
895 y = *xa++ - *xb++ + borrow;
896 borrow = y >> 16;
897 Sign_Extend(borrow, y);
898 *xc++ = y & 0xffff;
899 } while(xb < xbe);
900 while(xa < xae) {
901 y = *xa++ + borrow;
902 borrow = y >> 16;
903 Sign_Extend(borrow, y);
904 *xc++ = y & 0xffff;
906 #endif
907 while(!*--xc) {
908 wa--;
910 c->wds = wa;
911 return c;
914 static double ulp (double _x)
916 _double x;
917 register Long L;
918 _double a;
920 value(x) = _x;
921 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
922 #ifndef Sudden_Underflow
923 if (L > 0) {
924 #endif
925 #ifdef IBM
926 L |= Exp_msk1 >> 4;
927 #endif
928 word0(a) = L;
929 word1(a) = 0;
930 #ifndef Sudden_Underflow
932 else {
933 L = -L >> Exp_shift;
934 if (L < Exp_shift) {
935 word0(a) = 0x80000 >> L;
936 word1(a) = 0;
938 else {
939 word0(a) = 0;
940 L -= Exp_shift;
941 word1(a) = L >= 31 ? 1 : 1 << (31 - L);
944 #endif
945 return value(a);
948 static double
950 #ifdef KR_headers
951 (a, e) Bigint *a; int *e;
952 #else
953 (Bigint *a, int *e)
954 #endif
956 ULong *xa, *xa0, w, y, z;
957 int k;
958 _double d;
959 #ifdef VAX
960 ULong d0, d1;
961 #else
962 #define d0 word0(d)
963 #define d1 word1(d)
964 #endif
966 xa0 = a->x;
967 xa = xa0 + a->wds;
968 y = *--xa;
969 k = hi0bits(y);
970 *e = 32 - k;
971 #ifdef Pack_32
972 if (k < Ebits) {
973 d0 = Exp_1 | y >> (Ebits - k);
974 w = xa > xa0 ? *--xa : 0;
975 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
976 goto ret_d;
978 z = xa > xa0 ? *--xa : 0;
979 if (k -= Ebits) {
980 d0 = Exp_1 | y << k | z >> (32 - k);
981 y = xa > xa0 ? *--xa : 0;
982 d1 = z << k | y >> (32 - k);
984 else {
985 d0 = Exp_1 | y;
986 d1 = z;
988 #else
989 if (k < Ebits + 16) {
990 z = xa > xa0 ? *--xa : 0;
991 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
992 w = xa > xa0 ? *--xa : 0;
993 y = xa > xa0 ? *--xa : 0;
994 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
995 goto ret_d;
997 z = xa > xa0 ? *--xa : 0;
998 w = xa > xa0 ? *--xa : 0;
999 k -= Ebits + 16;
1000 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1001 y = xa > xa0 ? *--xa : 0;
1002 d1 = w << k + 16 | y << k;
1003 #endif
1004 ret_d:
1005 #ifdef VAX
1006 word0(d) = d0 >> 16 | d0 << 16;
1007 word1(d) = d1 >> 16 | d1 << 16;
1008 #else
1009 #undef d0
1010 #undef d1
1011 #endif
1012 return value(d);
1016 static Bigint * d2b(double _d, int *e, int *bits)
1018 Bigint *b;
1019 int de, i, k;
1020 ULong *x, y, z;
1021 _double d;
1022 #ifdef VAX
1023 ULong d0, d1;
1024 #endif
1026 value(d) = _d;
1027 #ifdef VAX
1028 d0 = word0(d) >> 16 | word0(d) << 16;
1029 d1 = word1(d) >> 16 | word1(d) << 16;
1030 #else
1031 #define d0 word0(d)
1032 #define d1 word1(d)
1033 #endif
1035 #ifdef Pack_32
1036 b = Balloc(1);
1037 #else
1038 b = Balloc(2);
1039 #endif
1040 x = b->x;
1042 z = d0 & Frac_mask;
1043 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1044 #ifdef Sudden_Underflow
1045 de = (int)(d0 >> Exp_shift);
1046 #ifndef IBM
1047 z |= Exp_msk11;
1048 #endif
1049 #else
1050 if ((de = (int)(d0 >> Exp_shift)))
1051 z |= Exp_msk1;
1052 #endif
1053 #ifdef Pack_32
1054 if ((y = d1)) {
1055 if ((k = lo0bits(&y))) {
1056 x[0] = y | (z << (32 - k));
1057 z >>= k;
1058 } else {
1059 x[0] = y;
1061 i = b->wds = (x[1] = z) ? 2 : 1;
1062 } else {
1063 k = lo0bits(&z);
1064 x[0] = z;
1065 i = b->wds = 1;
1066 k += 32;
1068 #else
1069 if (y = d1) {
1070 if (k = lo0bits(&y)) {
1071 if (k >= 16) {
1072 x[0] = y | z << 32 - k & 0xffff;
1073 x[1] = z >> k - 16 & 0xffff;
1074 x[2] = z >> k;
1075 i = 2;
1076 } else {
1077 x[0] = y & 0xffff;
1078 x[1] = y >> 16 | z << 16 - k & 0xffff;
1079 x[2] = z >> k & 0xffff;
1080 x[3] = z >> k+16;
1081 i = 3;
1083 } else {
1084 x[0] = y & 0xffff;
1085 x[1] = y >> 16;
1086 x[2] = z & 0xffff;
1087 x[3] = z >> 16;
1088 i = 3;
1090 } else {
1091 k = lo0bits(&z);
1092 if (k >= 16) {
1093 x[0] = z;
1094 i = 0;
1095 } else {
1096 x[0] = z & 0xffff;
1097 x[1] = z >> 16;
1098 i = 1;
1100 k += 32;
1102 while(!x[i])
1103 --i;
1104 b->wds = i + 1;
1105 #endif
1106 #ifndef Sudden_Underflow
1107 if (de) {
1108 #endif
1109 #ifdef IBM
1110 *e = (de - Bias - (P-1) << 2) + k;
1111 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1112 #else
1113 *e = de - Bias - (P-1) + k;
1114 *bits = P - k;
1115 #endif
1116 #ifndef Sudden_Underflow
1117 } else {
1118 *e = de - Bias - (P-1) + 1 + k;
1119 #ifdef Pack_32
1120 *bits = 32*i - hi0bits(x[i-1]);
1121 #else
1122 *bits = (i+2)*16 - hi0bits(x[i]);
1123 #endif
1125 #endif
1126 return b;
1128 #undef d0
1129 #undef d1
1132 static double ratio (Bigint *a, Bigint *b)
1134 _double da, db;
1135 int k, ka, kb;
1137 value(da) = b2d(a, &ka);
1138 value(db) = b2d(b, &kb);
1139 #ifdef Pack_32
1140 k = ka - kb + 32*(a->wds - b->wds);
1141 #else
1142 k = ka - kb + 16*(a->wds - b->wds);
1143 #endif
1144 #ifdef IBM
1145 if (k > 0) {
1146 word0(da) += (k >> 2)*Exp_msk1;
1147 if (k &= 3) {
1148 da *= 1 << k;
1150 } else {
1151 k = -k;
1152 word0(db) += (k >> 2)*Exp_msk1;
1153 if (k &= 3)
1154 db *= 1 << k;
1156 #else
1157 if (k > 0) {
1158 word0(da) += k*Exp_msk1;
1159 } else {
1160 k = -k;
1161 word0(db) += k*Exp_msk1;
1163 #endif
1164 return value(da) / value(db);
1167 static CONST double
1168 tens[] = {
1169 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1170 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1171 1e20, 1e21, 1e22
1172 #ifdef VAX
1173 , 1e23, 1e24
1174 #endif
1177 #ifdef IEEE_Arith
1178 static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1179 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1180 #define n_bigtens 5
1181 #else
1182 #ifdef IBM
1183 static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1184 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1185 #define n_bigtens 3
1186 #else
1187 static CONST double bigtens[] = { 1e16, 1e32 };
1188 static CONST double tinytens[] = { 1e-16, 1e-32 };
1189 #define n_bigtens 2
1190 #endif
1191 #endif
1194 static int quorem(Bigint *b, Bigint *S)
1196 int n;
1197 Long borrow, y;
1198 ULong carry, q, ys;
1199 ULong *bx, *bxe, *sx, *sxe;
1200 #ifdef Pack_32
1201 Long z;
1202 ULong si, zs;
1203 #endif
1205 n = S->wds;
1206 if (b->wds < n)
1207 return 0;
1208 sx = S->x;
1209 sxe = sx + --n;
1210 bx = b->x;
1211 bxe = bx + n;
1212 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1213 if (q) {
1214 borrow = 0;
1215 carry = 0;
1216 do {
1217 #ifdef Pack_32
1218 si = *sx++;
1219 ys = (si & 0xffff) * q + carry;
1220 zs = (si >> 16) * q + (ys >> 16);
1221 carry = zs >> 16;
1222 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1223 borrow = y >> 16;
1224 Sign_Extend(borrow, y);
1225 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1226 borrow = z >> 16;
1227 Sign_Extend(borrow, z);
1228 Storeinc(bx, z, y);
1229 #else
1230 ys = *sx++ * q + carry;
1231 carry = ys >> 16;
1232 y = *bx - (ys & 0xffff) + borrow;
1233 borrow = y >> 16;
1234 Sign_Extend(borrow, y);
1235 *bx++ = y & 0xffff;
1236 #endif
1238 while(sx <= sxe);
1239 if (!*bxe) {
1240 bx = b->x;
1241 while(--bxe > bx && !*bxe)
1242 --n;
1243 b->wds = n;
1246 if (cmp(b, S) >= 0) {
1247 q++;
1248 borrow = 0;
1249 carry = 0;
1250 bx = b->x;
1251 sx = S->x;
1252 do {
1253 #ifdef Pack_32
1254 si = *sx++;
1255 ys = (si & 0xffff) + carry;
1256 zs = (si >> 16) + (ys >> 16);
1257 carry = zs >> 16;
1258 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1259 borrow = y >> 16;
1260 Sign_Extend(borrow, y);
1261 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1262 borrow = z >> 16;
1263 Sign_Extend(borrow, z);
1264 Storeinc(bx, z, y);
1265 #else
1266 ys = *sx++ + carry;
1267 carry = ys >> 16;
1268 y = *bx - (ys & 0xffff) + borrow;
1269 borrow = y >> 16;
1270 Sign_Extend(borrow, y);
1271 *bx++ = y & 0xffff;
1272 #endif
1274 while(sx <= sxe);
1275 bx = b->x;
1276 bxe = bx + n;
1277 if (!*bxe) {
1278 while(--bxe > bx && !*bxe)
1279 --n;
1280 b->wds = n;
1283 return q;
1286 void destroy_freelist(Bigint** freelist)
1288 int i;
1289 Bigint *tmp;
1291 for (i = 0; i <= Kmax; i++) {
1292 Bigint **listp = &freelist[i];
1293 while ((tmp = *listp) != nullptr) {
1294 *listp = tmp->next;
1295 free(tmp);
1297 freelist[i] = nullptr;
1303 void zend_freedtoa(char *s)
1305 Bigint *b = (Bigint *)((int *)s - 1);
1306 b->maxwds = 1 << (b->k = *(int*)b);
1307 Bfree(b);
1310 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1312 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1313 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1315 * Modifications:
1316 * 1. Rather than iterating, we use a simple numeric overestimate
1317 * to determine k = floor(log10(d)). We scale relevant
1318 * quantities using O(log2(k)) rather than O(k) multiplications.
1319 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1320 * try to generate digits strictly left to right. Instead, we
1321 * compute with fewer bits and propagate the carry if necessary
1322 * when rounding the final digit up. This is often faster.
1323 * 3. Under the assumption that input will be rounded nearest,
1324 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1325 * That is, we allow equality in stopping tests when the
1326 * round-nearest rule will give the same floating-point value
1327 * as would satisfaction of the stopping test with strict
1328 * inequality.
1329 * 4. We remove common factors of powers of 2 from relevant
1330 * quantities.
1331 * 5. When converting floating-point integers less than 1e16,
1332 * we use floating-point arithmetic rather than resorting
1333 * to multiple-precision integers.
1334 * 6. When asked to produce fewer than 15 digits, we first try
1335 * to get by with floating-point arithmetic; we resort to
1336 * multiple-precision integer arithmetic only if we cannot
1337 * guarantee that the floating-point calculation has given
1338 * the correctly rounded result. For k requested digits and
1339 * "uniformly" distributed input, the probability is
1340 * something like 10^(k-15) that we must resort to the Long
1341 * calculation.
1344 char * zend_dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1346 /* Arguments ndigits, decpt, sign are similar to those
1347 of ecvt and fcvt; trailing zeros are suppressed from
1348 the returned string. If not null, *rve is set to point
1349 to the end of the return value. If d is +-Infinity or NaN,
1350 then *decpt is set to 9999.
1352 mode:
1353 0 ==> shortest string that yields d when read in
1354 and rounded to nearest.
1355 1 ==> like 0, but with Steele & White stopping rule;
1356 e.g. with IEEE P754 arithmetic , mode 0 gives
1357 1e23 whereas mode 1 gives 9.999999999999999e22.
1358 2 ==> max(1,ndigits) significant digits. This gives a
1359 return value similar to that of ecvt, except
1360 that trailing zeros are suppressed.
1361 3 ==> through ndigits past the decimal point. This
1362 gives a return value similar to that from fcvt,
1363 except that trailing zeros are suppressed, and
1364 ndigits can be negative.
1365 4-9 should give the same return values as 2-3, i.e.,
1366 4 <= mode <= 9 ==> same return as mode
1367 2 + (mode & 1). These modes are mainly for
1368 debugging; often they run slower but sometimes
1369 faster than modes 2-3.
1370 4,5,8,9 ==> left-to-right digit generation.
1371 6-9 ==> don't try fast floating-point estimate
1372 (if applicable).
1374 Values of mode other than 0-9 are treated as mode 0.
1376 Sufficient space is allocated to the return value
1377 to hold the suppressed trailing zeros.
1380 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
1381 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1382 spec_case = 0, try_quick;
1383 Long L;
1384 #ifndef Sudden_Underflow
1385 int denorm;
1386 ULong x;
1387 #endif
1388 Bigint *b, *b1, *delta, *mlo = 0, *mhi, *S, *tmp;
1389 double ds;
1390 char *s, *s0;
1391 _double d, d2, eps;
1393 value(d) = _d;
1395 if (word0(d) & Sign_bit) {
1396 /* set sign for everything, including 0's and NaNs */
1397 *sign = 1;
1398 word0(d) &= ~Sign_bit; /* clear sign bit */
1400 else
1401 *sign = 0;
1403 #if defined(IEEE_Arith) + defined(VAX)
1404 #ifdef IEEE_Arith
1405 if ((word0(d) & Exp_mask) == Exp_mask)
1406 #else
1407 if (word0(d) == 0x8000)
1408 #endif
1410 /* Infinity or NaN */
1411 *decpt = 9999;
1412 #ifdef IEEE_Arith
1413 if (!word1(d) && !(word0(d) & 0xfffff))
1414 return nrv_alloc("Infinity", rve, 8);
1415 #endif
1416 return nrv_alloc("NaN", rve, 3);
1418 #endif
1419 #ifdef IBM
1420 value(d) += 0; /* normalize */
1421 #endif
1422 if (!value(d)) {
1423 *decpt = 1;
1424 return nrv_alloc("0", rve, 1);
1427 b = d2b(value(d), &be, &bbits);
1428 #ifdef Sudden_Underflow
1429 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1430 #else
1431 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
1432 #endif
1433 value(d2) = value(d);
1434 word0(d2) &= Frac_mask1;
1435 word0(d2) |= Exp_11;
1436 #ifdef IBM
1437 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
1438 value(d2) /= 1 << j;
1439 #endif
1441 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1442 * log10(x) = log(x) / log(10)
1443 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1444 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1446 * This suggests computing an approximation k to log10(d) by
1448 * k = (i - Bias)*0.301029995663981
1449 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1451 * We want k to be too large rather than too small.
1452 * The error in the first-order Taylor series approximation
1453 * is in our favor, so we just round up the constant enough
1454 * to compensate for any error in the multiplication of
1455 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1456 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1457 * adding 1e-13 to the constant term more than suffices.
1458 * Hence we adjust the constant term to 0.1760912590558.
1459 * (We could get a more accurate k by invoking log10,
1460 * but this is probably not worthwhile.)
1463 i -= Bias;
1464 #ifdef IBM
1465 i <<= 2;
1466 i += j;
1467 #endif
1468 #ifndef Sudden_Underflow
1469 denorm = 0;
1471 else {
1472 /* d is denormalized */
1474 i = bbits + be + (Bias + (P-1) - 1);
1475 x = i > 32 ? (word0(d) << (64 - i)) | (word1(d) >> (i - 32))
1476 : (word1(d) << (32 - i));
1477 value(d2) = x;
1478 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
1479 i -= (Bias + (P-1) - 1) + 1;
1480 denorm = 1;
1482 #endif
1483 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
1484 k = (int)ds;
1485 if (ds < 0. && ds != k)
1486 k--; /* want k = floor(ds) */
1487 k_check = 1;
1488 if (k >= 0 && k <= Ten_pmax) {
1489 if (value(d) < tens[k])
1490 k--;
1491 k_check = 0;
1493 j = bbits - i - 1;
1494 if (j >= 0) {
1495 b2 = 0;
1496 s2 = j;
1498 else {
1499 b2 = -j;
1500 s2 = 0;
1502 if (k >= 0) {
1503 b5 = 0;
1504 s5 = k;
1505 s2 += k;
1507 else {
1508 b2 -= k;
1509 b5 = -k;
1510 s5 = 0;
1512 if (mode < 0 || mode > 9)
1513 mode = 0;
1514 try_quick = 1;
1515 if (mode > 5) {
1516 mode -= 4;
1517 try_quick = 0;
1519 leftright = 1;
1520 switch(mode) {
1521 case 0:
1522 case 1:
1523 ilim = ilim1 = -1;
1524 i = 18;
1525 ndigits = 0;
1526 break;
1527 case 2:
1528 leftright = 0;
1529 /* no break */
1530 case 4:
1531 if (ndigits <= 0)
1532 ndigits = 1;
1533 ilim = ilim1 = i = ndigits;
1534 break;
1535 case 3:
1536 leftright = 0;
1537 /* no break */
1538 case 5:
1539 i = ndigits + k + 1;
1540 ilim = i;
1541 ilim1 = i - 1;
1542 if (i <= 0)
1543 i = 1;
1545 s = s0 = rv_alloc(i);
1547 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
1549 /* Try to get by with floating-point arithmetic. */
1551 i = 0;
1552 value(d2) = value(d);
1553 k0 = k;
1554 ilim0 = ilim;
1555 ieps = 2; /* conservative */
1556 if (k > 0) {
1557 ds = tens[k&0xf];
1558 j = k >> 4;
1559 if (j & Bletch) {
1560 /* prevent overflows */
1561 j &= Bletch - 1;
1562 value(d) /= bigtens[n_bigtens-1];
1563 ieps++;
1565 for(; j; j >>= 1, i++)
1566 if (j & 1) {
1567 ieps++;
1568 ds *= bigtens[i];
1570 value(d) /= ds;
1572 else if ((j1 = -k)) {
1573 value(d) *= tens[j1 & 0xf];
1574 for(j = j1 >> 4; j; j >>= 1, i++)
1575 if (j & 1) {
1576 ieps++;
1577 value(d) *= bigtens[i];
1580 if (k_check && value(d) < 1. && ilim > 0) {
1581 if (ilim1 <= 0)
1582 goto fast_failed;
1583 ilim = ilim1;
1584 k--;
1585 value(d) *= 10.;
1586 ieps++;
1588 value(eps) = ieps*value(d) + 7.;
1589 word0(eps) -= (P-1)*Exp_msk1;
1590 if (ilim == 0) {
1591 S = mhi = 0;
1592 value(d) -= 5.;
1593 if (value(d) > value(eps))
1594 goto one_digit;
1595 if (value(d) < -value(eps))
1596 goto no_digits;
1597 goto fast_failed;
1599 #ifndef No_leftright
1600 if (leftright) {
1601 /* Use Steele & White method of only
1602 * generating digits needed.
1604 value(eps) = 0.5/tens[ilim-1] - value(eps);
1605 for(i = 0;;) {
1606 L = (int32_t)value(d);
1607 value(d) -= L;
1608 *s++ = '0' + (int)L;
1609 if (value(d) < value(eps))
1610 goto ret1;
1611 if (1. - value(d) < value(eps))
1612 goto bump_up;
1613 if (++i >= ilim)
1614 break;
1615 value(eps) *= 10.;
1616 value(d) *= 10.;
1619 else {
1620 #endif
1621 /* Generate ilim digits, then fix them up. */
1622 value(eps) *= tens[ilim-1];
1623 for(i = 1;; i++, value(d) *= 10.) {
1624 L = (int32_t)value(d);
1625 value(d) -= L;
1626 *s++ = '0' + (int)L;
1627 if (i == ilim) {
1628 if (value(d) > 0.5 + value(eps))
1629 goto bump_up;
1630 else if (value(d) < 0.5 - value(eps)) {
1631 /* cut ALL traling zeros only if the number of chars is greater than precision
1632 * otherwise cut only extra zeros
1634 if (k < ndigits) {
1635 while(*--s == '0' && (s - s0) > k);
1636 } else {
1637 while(*--s == '0');
1639 s++;
1640 goto ret1;
1642 break;
1645 #ifndef No_leftright
1647 #endif
1648 fast_failed:
1649 s = s0;
1650 value(d) = value(d2);
1651 k = k0;
1652 ilim = ilim0;
1655 /* Do we have a "small" integer? */
1657 if (be >= 0 && k <= Int_max) {
1658 /* Yes. */
1659 ds = tens[k];
1660 if (ndigits < 0 && ilim <= 0) {
1661 S = mhi = 0;
1662 if (ilim < 0 || value(d) <= 5*ds)
1663 goto no_digits;
1664 goto one_digit;
1666 for(i = 1;; i++) {
1667 L = (int32_t)(value(d) / ds);
1668 value(d) -= L*ds;
1669 #ifdef Check_FLT_ROUNDS
1670 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
1671 if (value(d) < 0) {
1672 L--;
1673 value(d) += ds;
1675 #endif
1676 *s++ = '0' + (int)L;
1677 if (i == ilim) {
1678 value(d) += value(d);
1679 if (value(d) > ds || (value(d) == ds && (L & 1))) {
1680 bump_up:
1681 while(*--s == '9')
1682 if (s == s0) {
1683 k++;
1684 *s = '0';
1685 break;
1687 ++*s++;
1689 break;
1691 if (!(value(d) *= 10.))
1692 break;
1694 goto ret1;
1697 m2 = b2;
1698 m5 = b5;
1699 mhi = mlo = 0;
1700 if (leftright) {
1701 if (mode < 2) {
1703 #ifndef Sudden_Underflow
1704 denorm ? be + (Bias + (P-1) - 1 + 1) :
1705 #endif
1706 #ifdef IBM
1707 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
1708 #else
1709 1 + P - bbits;
1710 #endif
1712 else {
1713 j = ilim - 1;
1714 if (m5 >= j)
1715 m5 -= j;
1716 else {
1717 s5 += j -= m5;
1718 b5 += j;
1719 m5 = 0;
1721 if ((i = ilim) < 0) {
1722 m2 -= i;
1723 i = 0;
1726 b2 += i;
1727 s2 += i;
1728 mhi = i2b(1);
1730 if (m2 > 0 && s2 > 0) {
1731 i = m2 < s2 ? m2 : s2;
1732 b2 -= i;
1733 m2 -= i;
1734 s2 -= i;
1736 if (b5 > 0) {
1737 if (leftright) {
1738 if (m5 > 0) {
1739 mhi = pow5mult(mhi, m5);
1740 b1 = mult(mhi, b);
1741 Bfree(b);
1742 b = b1;
1744 if ((j = b5 - m5)) {
1745 b = pow5mult(b, j);
1747 } else {
1748 b = pow5mult(b, b5);
1751 S = i2b(1);
1752 if (s5 > 0)
1753 S = pow5mult(S, s5);
1754 /* Check for special case that d is a normalized power of 2. */
1756 if (mode < 2) {
1757 if (!word1(d) && !(word0(d) & Bndry_mask)
1758 #ifndef Sudden_Underflow
1759 && word0(d) & Exp_mask
1760 #endif
1762 /* The special case */
1763 b2 += Log2P;
1764 s2 += Log2P;
1765 spec_case = 1;
1766 } else {
1767 spec_case = 0;
1771 /* Arrange for convenient computation of quotients:
1772 * shift left if necessary so divisor has 4 leading 0 bits.
1774 * Perhaps we should just compute leading 28 bits of S once
1775 * and for all and pass them and a shift to quorem, so it
1776 * can do shifts and ors to compute the numerator for q.
1778 #ifdef Pack_32
1779 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
1780 i = 32 - i;
1781 #else
1782 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
1783 i = 16 - i;
1784 #endif
1785 if (i > 4) {
1786 i -= 4;
1787 b2 += i;
1788 m2 += i;
1789 s2 += i;
1791 else if (i < 4) {
1792 i += 28;
1793 b2 += i;
1794 m2 += i;
1795 s2 += i;
1797 if (b2 > 0)
1798 b = lshift(b, b2);
1799 if (s2 > 0)
1800 S = lshift(S, s2);
1801 if (k_check) {
1802 if (cmp(b,S) < 0) {
1803 k--;
1804 b = multadd(b, 10, 0); /* we botched the k estimate */
1805 if (leftright)
1806 mhi = multadd(mhi, 10, 0);
1807 ilim = ilim1;
1810 if (ilim <= 0 && mode > 2) {
1811 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
1812 /* no digits, fcvt style */
1813 no_digits:
1814 k = -1 - ndigits;
1815 goto ret;
1817 one_digit:
1818 *s++ = '1';
1819 k++;
1820 goto ret;
1822 if (leftright) {
1823 if (m2 > 0)
1824 mhi = lshift(mhi, m2);
1826 /* Compute mlo -- check for special case
1827 * that d is a normalized power of 2.
1830 mlo = mhi;
1831 if (spec_case) {
1832 mhi = Balloc(mhi->k);
1833 Bcopy(mhi, mlo);
1834 mhi = lshift(mhi, Log2P);
1837 for(i = 1;;i++) {
1838 dig = quorem(b,S) + '0';
1839 /* Do we yet have the shortest decimal string
1840 * that will round to d?
1842 j = cmp(b, mlo);
1843 delta = diff(S, mhi);
1844 j1 = delta->sign ? 1 : cmp(b, delta);
1845 Bfree(delta);
1846 #ifndef ROUND_BIASED
1847 if (j1 == 0 && !mode && !(word1(d) & 1)) {
1848 if (dig == '9')
1849 goto round_9_up;
1850 if (j > 0)
1851 dig++;
1852 *s++ = dig;
1853 goto ret;
1855 #endif
1856 if (j < 0 || (j == 0 && !mode
1857 #ifndef ROUND_BIASED
1858 && !(word1(d) & 1)
1859 #endif
1860 )) {
1861 if (j1 > 0) {
1862 b = lshift(b, 1);
1863 j1 = cmp(b, S);
1864 if ((j1 > 0 || (j1 == 0 && (dig & 1)))
1865 && dig++ == '9')
1866 goto round_9_up;
1868 *s++ = dig;
1869 goto ret;
1871 if (j1 > 0) {
1872 if (dig == '9') { /* possible if i == 1 */
1873 round_9_up:
1874 *s++ = '9';
1875 goto roundoff;
1877 *s++ = dig + 1;
1878 goto ret;
1880 *s++ = dig;
1881 if (i == ilim)
1882 break;
1883 b = multadd(b, 10, 0);
1884 if (mlo == mhi)
1885 mlo = mhi = multadd(mhi, 10, 0);
1886 else {
1887 mlo = multadd(mlo, 10, 0);
1888 mhi = multadd(mhi, 10, 0);
1892 else
1893 for(i = 1;; i++) {
1894 *s++ = dig = quorem(b,S) + '0';
1895 if (i >= ilim)
1896 break;
1897 b = multadd(b, 10, 0);
1900 /* Round off last digit */
1902 b = lshift(b, 1);
1903 j = cmp(b, S);
1904 if (j > 0 || (j == 0 && (dig & 1))) {
1905 roundoff:
1906 while(*--s == '9')
1907 if (s == s0) {
1908 k++;
1909 *s++ = '1';
1910 goto ret;
1912 ++*s++;
1914 else {
1915 while(*--s == '0');
1916 s++;
1918 ret:
1919 Bfree(S);
1920 if (mhi) {
1921 if (mlo && mlo != mhi)
1922 Bfree(mlo);
1923 Bfree(mhi);
1925 ret1:
1928 Bigint *&p5s = s_bigint_data->p5s;
1929 while (p5s) {
1930 tmp = p5s;
1931 p5s = p5s->next;
1932 free(tmp);
1936 Bfree(b);
1938 if (s == s0) { /* don't return empty string */
1939 *s++ = '0';
1940 k = 0;
1942 *s = 0;
1943 *decpt = k + 1;
1944 if (rve)
1945 *rve = s;
1946 return s0;
1949 double zend_strtod (CONST char *s00, const char **se)
1951 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1952 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1953 CONST char *s, *s0, *s1;
1954 double aadj, aadj1, adj;
1955 _double rv, rv0;
1956 ULong L;
1957 ULong y, z;
1958 Bigint *bb = 0, *bb1, *bd = 0, *bd0, *bs = 0, *delta = 0, *tmp;
1959 double result;
1961 CONST char decimal_point = '.';
1963 sign = nz0 = nz = 0;
1964 value(rv) = 0.;
1967 for(s = s00; isspace((unsigned char) *s); s++)
1970 if (*s == '-') {
1971 sign = 1;
1972 s++;
1973 } else if (*s == '+') {
1974 s++;
1977 if (*s == '\0') {
1978 s = s00;
1979 goto ret;
1982 if (*s == '0') {
1983 nz0 = 1;
1984 while(*++s == '0') ;
1985 if (!*s)
1986 goto ret;
1988 s0 = s;
1989 y = z = 0;
1990 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1991 if (nd < 9)
1992 y = 10*y + c - '0';
1993 else if (nd < 16)
1994 z = 10*z + c - '0';
1995 nd0 = nd;
1996 if (c == decimal_point) {
1997 c = *++s;
1998 if (!nd) {
1999 for(; c == '0'; c = *++s)
2000 nz++;
2001 if (c > '0' && c <= '9') {
2002 s0 = s;
2003 nf += nz;
2004 nz = 0;
2005 goto have_dig;
2007 goto dig_done;
2009 for(; c >= '0' && c <= '9'; c = *++s) {
2010 have_dig:
2011 nz++;
2012 if (c -= '0') {
2013 nf += nz;
2014 for(i = 1; i < nz; i++)
2015 if (nd++ < 9)
2016 y *= 10;
2017 else if (nd <= DBL_DIG + 1)
2018 z *= 10;
2019 if (nd++ < 9)
2020 y = 10*y + c;
2021 else if (nd <= DBL_DIG + 1)
2022 z = 10*z + c;
2023 nz = 0;
2027 dig_done:
2028 e = 0;
2029 if (c == 'e' || c == 'E') {
2030 if (!nd && !nz && !nz0) {
2031 s = s00;
2032 goto ret;
2034 s00 = s;
2035 esign = 0;
2036 switch(c = *++s) {
2037 case '-':
2038 esign = 1;
2039 case '+':
2040 c = *++s;
2042 if (c >= '0' && c <= '9') {
2043 while(c == '0')
2044 c = *++s;
2045 if (c > '0' && c <= '9') {
2046 L = c - '0';
2047 s1 = s;
2048 while((c = *++s) >= '0' && c <= '9')
2049 L = 10*L + c - '0';
2050 if (s - s1 > 8 || L > 19999)
2051 /* Avoid confusion from exponents
2052 * so large that e might overflow.
2054 e = 19999; /* safe for 16 bit ints */
2055 else
2056 e = (int)L;
2057 if (esign)
2058 e = -e;
2060 else
2061 e = 0;
2063 else
2064 s = s00;
2066 if (!nd) {
2067 if (!nz && !nz0)
2068 s = s00;
2069 goto ret;
2071 e1 = e -= nf;
2073 /* Now we have nd0 digits, starting at s0, followed by a
2074 * decimal point, followed by nd-nd0 digits. The number we're
2075 * after is the integer represented by those digits times
2076 * 10**e */
2078 if (!nd0)
2079 nd0 = nd;
2080 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
2081 value(rv) = y;
2082 if (k > 9)
2083 value(rv) = tens[k - 9] * value(rv) + z;
2084 bd0 = 0;
2085 if (nd <= DBL_DIG
2086 #ifndef RND_PRODQUOT
2087 && FLT_ROUNDS == 1
2088 #endif
2090 if (!e)
2091 goto ret;
2092 if (e > 0) {
2093 if (e <= Ten_pmax) {
2094 #ifdef VAX
2095 goto vax_ovfl_check;
2096 #else
2097 /* value(rv) = */ rounded_product(value(rv),
2098 tens[e]);
2099 goto ret;
2100 #endif
2102 i = DBL_DIG - nd;
2103 if (e <= Ten_pmax + i) {
2104 /* A fancier test would sometimes let us do
2105 * this for larger i values.
2107 e -= i;
2108 value(rv) *= tens[i];
2109 #ifdef VAX
2110 /* VAX exponent range is so narrow we must
2111 * worry about overflow here...
2113 vax_ovfl_check:
2114 word0(rv) -= P*Exp_msk1;
2115 /* value(rv) = */ rounded_product(value(rv),
2116 tens[e]);
2117 if ((word0(rv) & Exp_mask)
2118 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2119 goto ovfl;
2120 word0(rv) += P*Exp_msk1;
2121 #else
2122 /* value(rv) = */ rounded_product(value(rv),
2123 tens[e]);
2124 #endif
2125 goto ret;
2128 #ifndef Inaccurate_Divide
2129 else if (e >= -Ten_pmax) {
2130 /* value(rv) = */ rounded_quotient(value(rv),
2131 tens[-e]);
2132 goto ret;
2134 #endif
2136 e1 += nd - k;
2138 /* Get starting approximation = rv * 10**e1 */
2140 if (e1 > 0) {
2141 if ((i = e1 & 15))
2142 value(rv) *= tens[i];
2143 if (e1 &= ~15) {
2144 if (e1 > DBL_MAX_10_EXP) {
2145 ovfl:
2146 errno = ERANGE;
2147 #ifndef Bad_float_h
2148 value(rv) = HUGE_VAL;
2149 #else
2150 /* Can't trust HUGE_VAL */
2151 #ifdef IEEE_Arith
2152 word0(rv) = Exp_mask;
2153 word1(rv) = 0;
2154 #else
2155 word0(rv) = Big0;
2156 word1(rv) = Big1;
2157 #endif
2158 #endif
2159 if (bd0)
2160 goto retfree;
2161 goto ret;
2163 if (e1 >>= 4) {
2164 for(j = 0; e1 > 1; j++, e1 >>= 1)
2165 if (e1 & 1)
2166 value(rv) *= bigtens[j];
2167 /* The last multiplication could overflow. */
2168 word0(rv) -= P*Exp_msk1;
2169 value(rv) *= bigtens[j];
2170 if ((z = word0(rv) & Exp_mask)
2171 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2172 goto ovfl;
2173 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2174 /* set to largest number */
2175 /* (Can't trust DBL_MAX) */
2176 word0(rv) = Big0;
2177 word1(rv) = Big1;
2179 else
2180 word0(rv) += P*Exp_msk1;
2185 else if (e1 < 0) {
2186 e1 = -e1;
2187 if ((i = e1 & 15))
2188 value(rv) /= tens[i];
2189 if (e1 &= ~15) {
2190 e1 >>= 4;
2191 if (e1 >= 1 << n_bigtens)
2192 goto undfl;
2193 for(j = 0; e1 > 1; j++, e1 >>= 1)
2194 if (e1 & 1)
2195 value(rv) *= tinytens[j];
2196 /* The last multiplication could underflow. */
2197 value(rv0) = value(rv);
2198 value(rv) *= tinytens[j];
2199 if (!value(rv)) {
2200 value(rv) = 2.*value(rv0);
2201 value(rv) *= tinytens[j];
2202 if (!value(rv)) {
2203 undfl:
2204 value(rv) = 0.;
2205 errno = ERANGE;
2206 if (bd0)
2207 goto retfree;
2208 goto ret;
2210 word0(rv) = Tiny0;
2211 word1(rv) = Tiny1;
2212 /* The refinement below will clean
2213 * this approximation up.
2219 /* Now the hard part -- adjusting rv to the correct value.*/
2221 /* Put digits into bd: true value = bd * 10^e */
2223 bd0 = s2b(s0, nd0, nd, y);
2225 for(;;) {
2226 bd = Balloc(bd0->k);
2227 Bcopy(bd, bd0);
2228 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
2229 bs = i2b(1);
2231 if (e >= 0) {
2232 bb2 = bb5 = 0;
2233 bd2 = bd5 = e;
2235 else {
2236 bb2 = bb5 = -e;
2237 bd2 = bd5 = 0;
2239 if (bbe >= 0)
2240 bb2 += bbe;
2241 else
2242 bd2 -= bbe;
2243 bs2 = bb2;
2244 #ifdef Sudden_Underflow
2245 #ifdef IBM
2246 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2247 #else
2248 j = P + 1 - bbbits;
2249 #endif
2250 #else
2251 i = bbe + bbbits - 1; /* logb(rv) */
2252 if (i < Emin) /* denormal */
2253 j = bbe + (P-Emin);
2254 else
2255 j = P + 1 - bbbits;
2256 #endif
2257 bb2 += j;
2258 bd2 += j;
2259 i = bb2 < bd2 ? bb2 : bd2;
2260 if (i > bs2)
2261 i = bs2;
2262 if (i > 0) {
2263 bb2 -= i;
2264 bd2 -= i;
2265 bs2 -= i;
2267 if (bb5 > 0) {
2268 bs = pow5mult(bs, bb5);
2269 bb1 = mult(bs, bb);
2270 Bfree(bb);
2271 bb = bb1;
2273 if (bb2 > 0)
2274 bb = lshift(bb, bb2);
2275 if (bd5 > 0)
2276 bd = pow5mult(bd, bd5);
2277 if (bd2 > 0)
2278 bd = lshift(bd, bd2);
2279 if (bs2 > 0)
2280 bs = lshift(bs, bs2);
2281 delta = diff(bb, bd);
2282 dsign = delta->sign;
2283 delta->sign = 0;
2284 i = cmp(delta, bs);
2285 if (i < 0) {
2286 /* Error is less than half an ulp -- check for
2287 * special case of mantissa a power of two.
2289 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
2290 break;
2291 delta = lshift(delta,Log2P);
2292 if (cmp(delta, bs) > 0)
2293 goto drop_down;
2294 break;
2296 if (i == 0) {
2297 /* exactly half-way between */
2298 if (dsign) {
2299 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2300 && word1(rv) == 0xffffffff) {
2301 /*boundary case -- increment exponent*/
2302 word0(rv) = (word0(rv) & Exp_mask)
2303 + Exp_msk1
2304 #ifdef IBM
2305 | Exp_msk1 >> 4
2306 #endif
2308 word1(rv) = 0;
2309 break;
2312 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2313 drop_down:
2314 /* boundary case -- decrement exponent */
2315 #ifdef Sudden_Underflow
2316 L = word0(rv) & Exp_mask;
2317 #ifdef IBM
2318 if (L < Exp_msk1)
2319 #else
2320 if (L <= Exp_msk1)
2321 #endif
2322 goto undfl;
2323 L -= Exp_msk1;
2324 #else
2325 L = (word0(rv) & Exp_mask) - Exp_msk1;
2326 #endif
2327 word0(rv) = L | Bndry_mask1;
2328 word1(rv) = 0xffffffff;
2329 #ifdef IBM
2330 goto cont;
2331 #else
2332 break;
2333 #endif
2335 #ifndef ROUND_BIASED
2336 if (!(word1(rv) & LSB))
2337 break;
2338 #endif
2339 if (dsign)
2340 value(rv) += ulp(value(rv));
2341 #ifndef ROUND_BIASED
2342 else {
2343 value(rv) -= ulp(value(rv));
2344 #ifndef Sudden_Underflow
2345 if (!value(rv))
2346 goto undfl;
2347 #endif
2349 #endif
2350 break;
2352 if ((aadj = ratio(delta, bs)) <= 2.) {
2353 if (dsign)
2354 aadj = aadj1 = 1.;
2355 else if (word1(rv) || word0(rv) & Bndry_mask) {
2356 #ifndef Sudden_Underflow
2357 if (word1(rv) == Tiny1 && !word0(rv))
2358 goto undfl;
2359 #endif
2360 aadj = 1.;
2361 aadj1 = -1.;
2363 else {
2364 /* special case -- power of FLT_RADIX to be */
2365 /* rounded down... */
2367 if (aadj < 2./FLT_RADIX)
2368 aadj = 1./FLT_RADIX;
2369 else
2370 aadj *= 0.5;
2371 aadj1 = -aadj;
2374 else {
2375 aadj *= 0.5;
2376 aadj1 = dsign ? aadj : -aadj;
2377 #ifdef Check_FLT_ROUNDS
2378 switch(FLT_ROUNDS) {
2379 case 2: /* towards +infinity */
2380 aadj1 -= 0.5;
2381 break;
2382 case 0: /* towards 0 */
2383 case 3: /* towards -infinity */
2384 aadj1 += 0.5;
2386 #else
2387 if (FLT_ROUNDS == 0)
2388 aadj1 += 0.5;
2389 #endif
2391 y = word0(rv) & Exp_mask;
2393 /* Check for overflow */
2395 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2396 value(rv0) = value(rv);
2397 word0(rv) -= P*Exp_msk1;
2398 adj = aadj1 * ulp(value(rv));
2399 value(rv) += adj;
2400 if ((word0(rv) & Exp_mask) >=
2401 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2402 if (word0(rv0) == Big0 && word1(rv0) == Big1)
2403 goto ovfl;
2404 word0(rv) = Big0;
2405 word1(rv) = Big1;
2406 goto cont;
2408 else
2409 word0(rv) += P*Exp_msk1;
2411 else {
2412 #ifdef Sudden_Underflow
2413 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2414 value(rv0) = value(rv);
2415 word0(rv) += P*Exp_msk1;
2416 adj = aadj1 * ulp(value(rv));
2417 value(rv) += adj;
2418 #ifdef IBM
2419 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2420 #else
2421 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2422 #endif
2424 if (word0(rv0) == Tiny0
2425 && word1(rv0) == Tiny1)
2426 goto undfl;
2427 word0(rv) = Tiny0;
2428 word1(rv) = Tiny1;
2429 goto cont;
2431 else
2432 word0(rv) -= P*Exp_msk1;
2434 else {
2435 adj = aadj1 * ulp(value(rv));
2436 value(rv) += adj;
2438 #else
2439 /* Compute adj so that the IEEE rounding rules will
2440 * correctly round rv + adj in some half-way cases.
2441 * If rv * ulp(rv) is denormalized (i.e.,
2442 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2443 * trouble from bits lost to denormalization;
2444 * example: 1.2e-307 .
2446 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
2447 aadj1 = (double)(int)(aadj + 0.5);
2448 if (!dsign)
2449 aadj1 = -aadj1;
2451 adj = aadj1 * ulp(value(rv));
2452 value(rv) += adj;
2453 #endif
2455 z = word0(rv) & Exp_mask;
2456 if (y == z) {
2457 /* Can we stop now? */
2458 L = (int32_t)aadj;
2459 aadj -= L;
2460 /* The tolerances below are conservative. */
2461 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2462 if (aadj < .4999999 || aadj > .5000001)
2463 break;
2465 else if (aadj < .4999999/FLT_RADIX)
2466 break;
2468 cont:
2469 Bfree(bb);
2470 Bfree(bd);
2471 Bfree(bs);
2472 Bfree(delta);
2474 retfree:
2475 Bfree(bb);
2476 Bfree(bd);
2477 Bfree(bs);
2478 Bfree(bd0);
2479 Bfree(delta);
2480 ret:
2481 if (se)
2482 *se = (char *)s;
2483 result = sign ? -value(rv) : value(rv);
2485 if (s_bigint_data.isNull()) {
2486 return result;
2489 Bigint *&p5s = s_bigint_data->p5s;
2490 while (p5s) {
2491 tmp = p5s;
2492 p5s = p5s->next;
2493 free(tmp);
2496 return result;
2499 double zend_hex_strtod(const char *str, const char **endptr)
2501 const char *s = str;
2502 char c;
2503 int any = 0;
2504 double value = 0;
2506 if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
2507 s += 2;
2510 while ((c = *s++)) {
2511 if (c >= '0' && c <= '9') {
2512 c -= '0';
2513 } else if (c >= 'A' && c <= 'F') {
2514 c -= 'A' - 10;
2515 } else if (c >= 'a' && c <= 'f') {
2516 c -= 'a' - 10;
2517 } else {
2518 break;
2521 any = 1;
2522 value = value * 16 + c;
2525 if (endptr != nullptr) {
2526 *endptr = (char *)(any ? s - 1 : str);
2529 return value;
2532 double zend_oct_strtod(const char *str, const char **endptr)
2534 const char *s = str;
2535 char c;
2536 double value = 0;
2537 int any = 0;
2539 /* skip leading zero */
2540 s++;
2542 while ((c = *s++)) {
2543 if (c > '7') {
2544 /* break and return the current value if the number is not well-formed
2545 * that's what Linux strtol() does
2547 break;
2549 // Note parentheses: convert digit into integer before adding as a double
2550 // in order to avoid accumulating floating point inaccuracies
2551 value = value * 8 + (c - '0');
2552 any = 1;
2555 if (endptr != nullptr) {
2556 *endptr = (char *)(any ? s - 1 : str);
2559 return value;
2562 double zend_bin_strtod(const char *str, const char **endptr)
2564 const char *s = str;
2565 char c;
2566 double value = 0;
2567 int any = 0;
2569 if (strlen(str) < 2) {
2570 *endptr = str;
2571 return 0.0;
2574 if ('0' == *s && ('b' == s[1] || 'B' == s[1])) {
2575 s += 2;
2578 while ((c = *s++)) {
2580 * Verify the validity of the current character as a base-2 digit. In
2581 * the event that an invalid digit is found, halt the conversion and
2582 * return the portion which has been converted thus far.
2584 if ('0' == c || '1' == c)
2585 // Note parentheses: convert digit into integer before adding as a double
2586 // in order to avoid accumulating floating point inaccuracies
2587 value = value * 2 + (c - '0');
2588 else
2589 break;
2591 any = 1;
2595 * As with many strtoX implementations, should the subject sequence be
2596 * empty or not well-formed, no conversion is performed and the original
2597 * value of str is stored in *endptr, provided that endptr is not a null
2598 * pointer.
2600 if (nullptr != endptr) {
2601 *endptr = (char *)(any ? s - 1 : str);
2604 return value;
2607 ///////////////////////////////////////////////////////////////////////////////