1 : /****************************************************************
2 : *
3 : * The author of this software is David M. Gay.
4 : *
5 : * Copyright (c) 1991 by AT&T.
6 : *
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.
12 : *
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.
17 : *
18 : ***************************************************************/
19 :
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
27 : */
28 :
29 : /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
30 : *
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).
35 : *
36 : * Inspired loosely by William D. Clinger's paper "How to Read Floating
37 : * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
38 : *
39 : * Modifications:
40 : *
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).
57 : */
58 :
59 : /*
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.
90 : */
91 :
92 : /* $Id: zend_strtod.c 282162 2009-06-15 14:06:30Z pajoye $ */
93 :
94 : #include <zend.h>
95 : #include <unicode/utypes.h>
96 : #include <zend_strtod.h>
97 :
98 : #ifdef ZTS
99 : #include <TSRM.h>
100 : #endif
101 :
102 : #include <stddef.h>
103 : #include <stdio.h>
104 : #include <ctype.h>
105 : #include <stdarg.h>
106 : #include <string.h>
107 : #include <stdlib.h>
108 : #include <math.h>
109 :
110 : #ifdef HAVE_LOCALE_H
111 : #include <locale.h>
112 : #endif
113 :
114 : #ifdef HAVE_SYS_TYPES_H
115 : #include <sys/types.h>
116 : #endif
117 :
118 : #if defined(HAVE_INTTYPES_H)
119 : #include <inttypes.h>
120 : #elif defined(HAVE_STDINT_H)
121 : #include <stdint.h>
122 : #endif
123 :
124 : #ifndef HAVE_INT32_T
125 : # if SIZEOF_INT == 4
126 : typedef int int32_t;
127 : # elif SIZEOF_LONG == 4
128 : typedef long int int32_t;
129 : # endif
130 : #endif
131 :
132 : #ifndef HAVE_UINT32_T
133 : # if SIZEOF_INT == 4
134 : typedef unsigned int uint32_t;
135 : # elif SIZEOF_LONG == 4
136 : typedef unsigned long int uint32_t;
137 : # endif
138 : #endif
139 :
140 : #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
141 : # if defined(__LITTLE_ENDIAN__)
142 : # undef WORDS_BIGENDIAN
143 : # else
144 : # if defined(__BIG_ENDIAN__)
145 : # define WORDS_BIGENDIAN
146 : # endif
147 : # endif
148 : #endif
149 :
150 : #ifdef WORDS_BIGENDIAN
151 : #define IEEE_BIG_ENDIAN
152 : #else
153 : #define IEEE_LITTLE_ENDIAN
154 : #endif
155 :
156 : #if defined(__arm__) && !defined(__VFP_FP__)
157 : /*
158 : * * Although the CPU is little endian the FP has different
159 : * * byte and word endianness. The byte order is still little endian
160 : * * but the word order is big endian.
161 : * */
162 : #define IEEE_BIG_ENDIAN
163 : #undef IEEE_LITTLE_ENDIAN
164 : #endif
165 :
166 : #ifdef __vax__
167 : #define VAX
168 : #endif
169 :
170 : #if defined(_MSC_VER)
171 : #define int32_t __int32
172 : #define uint32_t unsigned __int32
173 : #define IEEE_LITTLE_ENDIAN
174 : #endif
175 :
176 : #define Long int32_t
177 : #define ULong uint32_t
178 :
179 : #ifdef __cplusplus
180 : #include "malloc.h"
181 : #include "memory.h"
182 : #else
183 : #ifndef KR_headers
184 : #include "stdlib.h"
185 : #include "string.h"
186 : #include "locale.h"
187 : #else
188 : #include "malloc.h"
189 : #include "memory.h"
190 : #endif
191 : #endif
192 :
193 : #ifdef MALLOC
194 : #ifdef KR_headers
195 : extern char *MALLOC();
196 : #else
197 : extern void *MALLOC(size_t);
198 : #endif
199 : #else
200 : #define MALLOC malloc
201 : #endif
202 :
203 : #include "ctype.h"
204 : #include "errno.h"
205 :
206 : #ifdef Bad_float_h
207 : #ifdef IEEE_BIG_ENDIAN
208 : #define IEEE_ARITHMETIC
209 : #endif
210 : #ifdef IEEE_LITTLE_ENDIAN
211 : #define IEEE_ARITHMETIC
212 : #endif
213 :
214 : #ifdef IEEE_ARITHMETIC
215 : #define DBL_DIG 15
216 : #define DBL_MAX_10_EXP 308
217 : #define DBL_MAX_EXP 1024
218 : #define FLT_RADIX 2
219 : #define FLT_ROUNDS 1
220 : #define DBL_MAX 1.7976931348623157e+308
221 : #endif
222 :
223 : #ifdef IBM
224 : #define DBL_DIG 16
225 : #define DBL_MAX_10_EXP 75
226 : #define DBL_MAX_EXP 63
227 : #define FLT_RADIX 16
228 : #define FLT_ROUNDS 0
229 : #define DBL_MAX 7.2370055773322621e+75
230 : #endif
231 :
232 : #ifdef VAX
233 : #define DBL_DIG 16
234 : #define DBL_MAX_10_EXP 38
235 : #define DBL_MAX_EXP 127
236 : #define FLT_RADIX 2
237 : #define FLT_ROUNDS 1
238 : #define DBL_MAX 1.7014118346046923e+38
239 : #endif
240 :
241 :
242 : #ifndef LONG_MAX
243 : #define LONG_MAX 2147483647
244 : #endif
245 : #else
246 : #include "float.h"
247 : #endif
248 : #ifndef __MATH_H__
249 : #include "math.h"
250 : #endif
251 :
252 : BEGIN_EXTERN_C()
253 :
254 : #ifndef CONST
255 : #ifdef KR_headers
256 : #define CONST /* blank */
257 : #else
258 : #define CONST const
259 : #endif
260 : #endif
261 :
262 : #ifdef Unsigned_Shifts
263 : #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
264 : #else
265 : #define Sign_Extend(a,b) /*no-op*/
266 : #endif
267 :
268 : #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
269 : defined(IBM) != 1
270 : Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
271 : IBM should be defined.
272 : #endif
273 :
274 : typedef union {
275 : double d;
276 : ULong ul[2];
277 : } _double;
278 : #define value(x) ((x).d)
279 : #ifdef IEEE_LITTLE_ENDIAN
280 : #define word0(x) ((x).ul[1])
281 : #define word1(x) ((x).ul[0])
282 : #else
283 : #define word0(x) ((x).ul[0])
284 : #define word1(x) ((x).ul[1])
285 : #endif
286 :
287 : /* The following definition of Storeinc is appropriate for MIPS processors.
288 : * An alternative that might be better on some machines is
289 : * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
290 : */
291 : #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
292 : #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
293 : ((unsigned short *)a)[0] = (unsigned short)c, a++)
294 : #else
295 : #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
296 : ((unsigned short *)a)[1] = (unsigned short)c, a++)
297 : #endif
298 :
299 : /* #define P DBL_MANT_DIG */
300 : /* Ten_pmax = floor(P*log(2)/log(5)) */
301 : /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
302 : /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
303 : /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
304 :
305 : #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
306 : #define Exp_shift 20
307 : #define Exp_shift1 20
308 : #define Exp_msk1 0x100000
309 : #define Exp_msk11 0x100000
310 : #define Exp_mask 0x7ff00000
311 : #define P 53
312 : #define Bias 1023
313 : #define IEEE_Arith
314 : #define Emin (-1022)
315 : #define Exp_1 0x3ff00000
316 : #define Exp_11 0x3ff00000
317 : #define Ebits 11
318 : #define Frac_mask 0xfffff
319 : #define Frac_mask1 0xfffff
320 : #define Ten_pmax 22
321 : #define Bletch 0x10
322 : #define Bndry_mask 0xfffff
323 : #define Bndry_mask1 0xfffff
324 : #define LSB 1
325 : #define Sign_bit 0x80000000
326 : #define Log2P 1
327 : #define Tiny0 0
328 : #define Tiny1 1
329 : #define Quick_max 14
330 : #define Int_max 14
331 : #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
332 : #else
333 : #undef Sudden_Underflow
334 : #define Sudden_Underflow
335 : #ifdef IBM
336 : #define Exp_shift 24
337 : #define Exp_shift1 24
338 : #define Exp_msk1 0x1000000
339 : #define Exp_msk11 0x1000000
340 : #define Exp_mask 0x7f000000
341 : #define P 14
342 : #define Bias 65
343 : #define Exp_1 0x41000000
344 : #define Exp_11 0x41000000
345 : #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
346 : #define Frac_mask 0xffffff
347 : #define Frac_mask1 0xffffff
348 : #define Bletch 4
349 : #define Ten_pmax 22
350 : #define Bndry_mask 0xefffff
351 : #define Bndry_mask1 0xffffff
352 : #define LSB 1
353 : #define Sign_bit 0x80000000
354 : #define Log2P 4
355 : #define Tiny0 0x100000
356 : #define Tiny1 0
357 : #define Quick_max 14
358 : #define Int_max 15
359 : #else /* VAX */
360 : #define Exp_shift 23
361 : #define Exp_shift1 7
362 : #define Exp_msk1 0x80
363 : #define Exp_msk11 0x800000
364 : #define Exp_mask 0x7f80
365 : #define P 56
366 : #define Bias 129
367 : #define Exp_1 0x40800000
368 : #define Exp_11 0x4080
369 : #define Ebits 8
370 : #define Frac_mask 0x7fffff
371 : #define Frac_mask1 0xffff007f
372 : #define Ten_pmax 24
373 : #define Bletch 2
374 : #define Bndry_mask 0xffff007f
375 : #define Bndry_mask1 0xffff007f
376 : #define LSB 0x10000
377 : #define Sign_bit 0x8000
378 : #define Log2P 1
379 : #define Tiny0 0x80
380 : #define Tiny1 0
381 : #define Quick_max 15
382 : #define Int_max 15
383 : #endif
384 : #endif
385 :
386 : #ifndef IEEE_Arith
387 : #define ROUND_BIASED
388 : #endif
389 :
390 : #ifdef RND_PRODQUOT
391 : #define rounded_product(a,b) a = rnd_prod(a, b)
392 : #define rounded_quotient(a,b) a = rnd_quot(a, b)
393 : #ifdef KR_headers
394 : extern double rnd_prod(), rnd_quot();
395 : #else
396 : extern double rnd_prod(double, double), rnd_quot(double, double);
397 : #endif
398 : #else
399 : #define rounded_product(a,b) a *= b
400 : #define rounded_quotient(a,b) a /= b
401 : #endif
402 :
403 : #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
404 : #define Big1 0xffffffff
405 :
406 : #ifndef Just_16
407 : /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
408 : * * This makes some inner loops simpler and sometimes saves work
409 : * * during multiplications, but it often seems to make things slightly
410 : * * slower. Hence the default is now to store 32 bits per Long.
411 : * */
412 : #ifndef Pack_32
413 : #define Pack_32
414 : #endif
415 : #endif
416 :
417 : #define Kmax 15
418 :
419 : struct Bigint {
420 : struct Bigint *next;
421 : int k, maxwds, sign, wds;
422 : ULong x[1];
423 : };
424 :
425 : typedef struct Bigint Bigint;
426 :
427 : /* static variables, multithreading fun! */
428 : static Bigint *freelist[Kmax+1];
429 : static Bigint *p5s;
430 :
431 : static void destroy_freelist(void);
432 :
433 : #ifdef ZTS
434 :
435 : static MUTEX_T dtoa_mutex;
436 : static MUTEX_T pow5mult_mutex;
437 :
438 : #define _THREAD_PRIVATE_MUTEX_LOCK(x) tsrm_mutex_lock(x);
439 : #define _THREAD_PRIVATE_MUTEX_UNLOCK(x) tsrm_mutex_unlock(x);
440 :
441 : #else
442 :
443 : #define _THREAD_PRIVATE_MUTEX_LOCK(x)
444 : #define _THREAD_PRIVATE_MUTEX_UNLOCK(x)
445 :
446 : #endif /* ZTS */
447 :
448 : ZEND_API int zend_startup_strtod(void) /* {{{ */
449 17007 : {
450 : #ifdef ZTS
451 : dtoa_mutex = tsrm_mutex_alloc();
452 : pow5mult_mutex = tsrm_mutex_alloc();
453 : #endif
454 17007 : return 1;
455 : }
456 : /* }}} */
457 :
458 : ZEND_API int zend_shutdown_strtod(void) /* {{{ */
459 17039 : {
460 17039 : destroy_freelist();
461 : #ifdef ZTS
462 : tsrm_mutex_free(dtoa_mutex);
463 : dtoa_mutex = NULL;
464 :
465 : tsrm_mutex_free(pow5mult_mutex);
466 : pow5mult_mutex = NULL;
467 : #endif
468 17039 : return 1;
469 : }
470 : /* }}} */
471 :
472 : static Bigint * Balloc(int k) /* {{{ */
473 239382 : {
474 : int x;
475 : Bigint *rv;
476 :
477 : _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
478 239382 : if ((rv = freelist[k])) {
479 235784 : freelist[k] = rv->next;
480 : } else {
481 3598 : x = 1 << k;
482 3598 : rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
483 3598 : rv->k = k;
484 3598 : rv->maxwds = x;
485 : }
486 : _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
487 239382 : rv->sign = rv->wds = 0;
488 239382 : return rv;
489 : }
490 : /* }}} */
491 :
492 : static void Bfree(Bigint *v) /* {{{ */
493 238343 : {
494 238343 : if (v) {
495 : _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
496 238337 : v->next = freelist[v->k];
497 238337 : freelist[v->k] = v;
498 : _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
499 : }
500 238343 : }
501 : /* }}} */
502 :
503 : #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
504 : y->wds*sizeof(Long) + 2*sizeof(int))
505 :
506 : /* return value is only used as a simple string, so mis-aligned parts
507 : * inside the Bigint are not at risk on strict align architectures
508 : */
509 : static char * rv_alloc(unsigned int i) /* {{{ */
510 112056 : {
511 : unsigned int k;
512 : int j, *r;
513 :
514 112056 : j = sizeof(ULong);
515 112056 : for(k = 0;
516 224590 : sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
517 478 : j <<= 1) {
518 478 : k++;
519 : }
520 112056 : r = (int*)Balloc(k);
521 112056 : *r = k;
522 112056 : return (char *)(r+1);
523 : }
524 : /* }}} */
525 :
526 : static char * nrv_alloc(char *s, char **rve, int n) /* {{{ */
527 557 : {
528 : char *rv, *t;
529 :
530 557 : t = rv = rv_alloc(n);
531 1735 : while((*t = *s++) !=0) {
532 621 : t++;
533 : }
534 557 : if (rve) {
535 0 : *rve = t;
536 : }
537 557 : return rv;
538 : }
539 : /* }}} */
540 :
541 : static Bigint * multadd(Bigint *b, int m, int a) /* multiply by m and add a */ /* {{{ */
542 46275 : {
543 : int i, wds;
544 : ULong *x, y;
545 : #ifdef Pack_32
546 : ULong xi, z;
547 : #endif
548 : Bigint *b1;
549 :
550 46275 : wds = b->wds;
551 46275 : x = b->x;
552 46275 : i = 0;
553 : do {
554 : #ifdef Pack_32
555 130094 : xi = *x;
556 130094 : y = (xi & 0xffff) * m + a;
557 130094 : z = (xi >> 16) * m + (y >> 16);
558 130094 : a = (int)(z >> 16);
559 130094 : *x++ = (z << 16) + (y & 0xffff);
560 : #else
561 : y = *x * m + a;
562 : a = (int)(y >> 16);
563 : *x++ = y & 0xffff;
564 : #endif
565 : }
566 130094 : while(++i < wds);
567 46275 : if (a) {
568 4564 : if (wds >= b->maxwds) {
569 0 : b1 = Balloc(b->k+1);
570 0 : Bcopy(b1, b);
571 0 : Bfree(b);
572 0 : b = b1;
573 : }
574 4564 : b->x[wds++] = a;
575 4564 : b->wds = wds;
576 : }
577 46275 : return b;
578 : }
579 : /* }}} */
580 :
581 : static int hi0bits(ULong x) /* {{{ */
582 1572 : {
583 1572 : int k = 0;
584 :
585 1572 : if (!(x & 0xffff0000)) {
586 868 : k = 16;
587 868 : x <<= 16;
588 : }
589 1572 : if (!(x & 0xff000000)) {
590 753 : k += 8;
591 753 : x <<= 8;
592 : }
593 1572 : if (!(x & 0xf0000000)) {
594 852 : k += 4;
595 852 : x <<= 4;
596 : }
597 1572 : if (!(x & 0xc0000000)) {
598 778 : k += 2;
599 778 : x <<= 2;
600 : }
601 1572 : if (!(x & 0x80000000)) {
602 870 : k++;
603 870 : if (!(x & 0x40000000)) {
604 0 : return 32;
605 : }
606 : }
607 1572 : return k;
608 : }
609 : /* }}} */
610 :
611 : static int lo0bits(ULong *y) /* {{{ */
612 113428 : {
613 : int k;
614 113428 : ULong x = *y;
615 :
616 113428 : if (x & 7) {
617 92593 : if (x & 1) {
618 53076 : return 0;
619 : }
620 39517 : if (x & 2) {
621 26338 : *y = x >> 1;
622 26338 : return 1;
623 : }
624 13179 : *y = x >> 2;
625 13179 : return 2;
626 : }
627 20835 : k = 0;
628 20835 : if (!(x & 0xffff)) {
629 4333 : k = 16;
630 4333 : x >>= 16;
631 : }
632 20835 : if (!(x & 0xff)) {
633 3691 : k += 8;
634 3691 : x >>= 8;
635 : }
636 20835 : if (!(x & 0xf)) {
637 10424 : k += 4;
638 10424 : x >>= 4;
639 : }
640 20835 : if (!(x & 0x3)) {
641 10735 : k += 2;
642 10735 : x >>= 2;
643 : }
644 20835 : if (!(x & 1)) {
645 11626 : k++;
646 11626 : x >>= 1;
647 11626 : if (!(x & 1)) {
648 0 : return 32;
649 : }
650 : }
651 20835 : *y = x;
652 20835 : return k;
653 : }
654 : /* }}} */
655 :
656 : static Bigint * i2b(int i) /* {{{ */
657 2381 : {
658 : Bigint *b;
659 :
660 2381 : b = Balloc(1);
661 2381 : b->x[0] = i;
662 2381 : b->wds = 1;
663 2381 : return b;
664 : }
665 : /* }}} */
666 :
667 : static Bigint * mult(Bigint *a, Bigint *b) /* {{{ */
668 1408 : {
669 : Bigint *c;
670 : int k, wa, wb, wc;
671 : ULong carry, y, z;
672 : ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
673 : #ifdef Pack_32
674 : ULong z2;
675 : #endif
676 :
677 1408 : if (a->wds < b->wds) {
678 305 : c = a;
679 305 : a = b;
680 305 : b = c;
681 : }
682 1408 : k = a->k;
683 1408 : wa = a->wds;
684 1408 : wb = b->wds;
685 1408 : wc = wa + wb;
686 1408 : if (wc > a->maxwds) {
687 522 : k++;
688 : }
689 1408 : c = Balloc(k);
690 6650 : for(x = c->x, xa = x + wc; x < xa; x++) {
691 5242 : *x = 0;
692 : }
693 1408 : xa = a->x;
694 1408 : xae = xa + wa;
695 1408 : xb = b->x;
696 1408 : xbe = xb + wb;
697 1408 : xc0 = c->x;
698 : #ifdef Pack_32
699 3523 : for(; xb < xbe; xb++, xc0++) {
700 2115 : if ((y = *xb & 0xffff)) {
701 2112 : x = xa;
702 2112 : xc = xc0;
703 2112 : carry = 0;
704 : do {
705 6779 : z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
706 6779 : carry = z >> 16;
707 6779 : z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
708 6779 : carry = z2 >> 16;
709 6779 : Storeinc(xc, z2, z);
710 : }
711 6779 : while(x < xae);
712 2112 : *xc = carry;
713 : }
714 2115 : if ((y = *xb >> 16)) {
715 1296 : x = xa;
716 1296 : xc = xc0;
717 1296 : carry = 0;
718 1296 : z2 = *xc;
719 : do {
720 4965 : z = (*x & 0xffff) * y + (*xc >> 16) + carry;
721 4965 : carry = z >> 16;
722 4965 : Storeinc(xc, z, z2);
723 4965 : z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
724 4965 : carry = z2 >> 16;
725 : }
726 4965 : while(x < xae);
727 1296 : *xc = z2;
728 : }
729 : }
730 : #else
731 : for(; xb < xbe; xc0++) {
732 : if (y = *xb++) {
733 : x = xa;
734 : xc = xc0;
735 : carry = 0;
736 : do {
737 : z = *x++ * y + *xc + carry;
738 : carry = z >> 16;
739 : *xc++ = z & 0xffff;
740 : }
741 : while(x < xae);
742 : *xc = carry;
743 : }
744 : }
745 : #endif
746 1408 : for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
747 1408 : c->wds = wc;
748 1408 : return c;
749 : }
750 : /* }}} */
751 :
752 : static Bigint * s2b (CONST char *s, int nd0, int nd, ULong y9) /* {{{ */
753 1906 : {
754 : Bigint *b;
755 : int i, k;
756 : Long x, y;
757 :
758 1906 : x = (nd + 8) / 9;
759 1906 : for(k = 0, y = 1; x > y; y <<= 1, k++) ;
760 : #ifdef Pack_32
761 1906 : b = Balloc(k);
762 1906 : b->x[0] = y9;
763 1906 : b->wds = 1;
764 : #else
765 : b = Balloc(k+1);
766 : b->x[0] = y9 & 0xffff;
767 : b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
768 : #endif
769 :
770 1906 : i = 9;
771 1906 : if (9 < nd0) {
772 1737 : s += 9;
773 35915 : do b = multadd(b, 10, *s++ - '0');
774 35915 : while(++i < nd0);
775 1737 : s++;
776 : } else {
777 169 : s += 10;
778 : }
779 3700 : for(; i < nd; i++) {
780 1794 : b = multadd(b, 10, *s++ - '0');
781 : }
782 1906 : return b;
783 : }
784 : /* }}} */
785 :
786 : static Bigint * pow5mult(Bigint *b, int k) /* {{{ */
787 341 : {
788 : Bigint *b1, *p5, *p51;
789 : int i;
790 : static int p05[3] = { 5, 25, 125 };
791 :
792 : _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
793 341 : if ((i = k & 3)) {
794 292 : b = multadd(b, p05[i-1], 0);
795 : }
796 :
797 341 : if (!(k >>= 2)) {
798 : _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
799 59 : return b;
800 : }
801 282 : if (!(p5 = p5s)) {
802 : /* first time */
803 282 : p5 = p5s = i2b(625);
804 282 : p5->next = 0;
805 : }
806 : for(;;) {
807 1045 : if (k & 1) {
808 504 : b1 = mult(b, p5);
809 504 : Bfree(b);
810 504 : b = b1;
811 : }
812 1045 : if (!(k >>= 1)) {
813 282 : break;
814 : }
815 763 : if (!(p51 = p5->next)) {
816 763 : if (!(p51 = p5->next)) {
817 763 : p51 = p5->next = mult(p5,p5);
818 763 : p51->next = 0;
819 : }
820 : }
821 763 : p5 = p51;
822 763 : }
823 : _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
824 282 : return b;
825 : }
826 : /* }}} */
827 :
828 : static Bigint *lshift(Bigint *b, int k) /* {{{ */
829 4345 : {
830 : int i, k1, n, n1;
831 : Bigint *b1;
832 : ULong *x, *x1, *xe, z;
833 :
834 : #ifdef Pack_32
835 4345 : n = k >> 5;
836 : #else
837 : n = k >> 4;
838 : #endif
839 4345 : k1 = b->k;
840 4345 : n1 = n + b->wds + 1;
841 8482 : for(i = b->maxwds; n1 > i; i <<= 1) {
842 4137 : k1++;
843 : }
844 4345 : b1 = Balloc(k1);
845 4345 : x1 = b1->x;
846 8636 : for(i = 0; i < n; i++) {
847 4291 : *x1++ = 0;
848 : }
849 4345 : x = b->x;
850 4345 : xe = x + b->wds;
851 : #ifdef Pack_32
852 4345 : if (k &= 0x1f) {
853 4269 : k1 = 32 - k;
854 4269 : z = 0;
855 : do {
856 7348 : *x1++ = *x << k | z;
857 7348 : z = *x++ >> k1;
858 : }
859 7348 : while(x < xe);
860 4269 : if ((*x1 = z)) {
861 1110 : ++n1;
862 : }
863 : }
864 : #else
865 : if (k &= 0xf) {
866 : k1 = 16 - k;
867 : z = 0;
868 : do {
869 : *x1++ = *x << k & 0xffff | z;
870 : z = *x++ >> k1;
871 : }
872 : while(x < xe);
873 : if (*x1 = z) {
874 : ++n1;
875 : }
876 : }
877 : #endif
878 : else do
879 122 : *x1++ = *x++;
880 122 : while(x < xe);
881 4345 : b1->wds = n1 - 1;
882 4345 : Bfree(b);
883 4345 : return b1;
884 : }
885 : /* }}} */
886 :
887 : static int cmp(Bigint *a, Bigint *b) /* {{{ */
888 9650 : {
889 : ULong *xa, *xa0, *xb, *xb0;
890 : int i, j;
891 :
892 9650 : i = a->wds;
893 9650 : j = b->wds;
894 : #ifdef DEBUG
895 : if (i > 1 && !a->x[i-1])
896 : Bug("cmp called with a->x[a->wds-1] == 0");
897 : if (j > 1 && !b->x[j-1])
898 : Bug("cmp called with b->x[b->wds-1] == 0");
899 : #endif
900 9650 : if (i -= j)
901 160 : return i;
902 9490 : xa0 = a->x;
903 9490 : xa = xa0 + j;
904 9490 : xb0 = b->x;
905 9490 : xb = xb0 + j;
906 : for(;;) {
907 12960 : if (*--xa != *--xb)
908 9253 : return *xa < *xb ? -1 : 1;
909 3707 : if (xa <= xa0)
910 237 : break;
911 3470 : }
912 237 : return 0;
913 : }
914 : /* }}} */
915 :
916 : static Bigint * diff(Bigint *a, Bigint *b) /* {{{ */
917 1929 : {
918 : Bigint *c;
919 : int i, wa, wb;
920 : Long borrow, y; /* We need signed shifts here. */
921 : ULong *xa, *xae, *xb, *xbe, *xc;
922 : #ifdef Pack_32
923 : Long z;
924 : #endif
925 :
926 1929 : i = cmp(a,b);
927 1929 : if (!i) {
928 113 : c = Balloc(0);
929 113 : c->wds = 1;
930 113 : c->x[0] = 0;
931 113 : return c;
932 : }
933 1816 : if (i < 0) {
934 1347 : c = a;
935 1347 : a = b;
936 1347 : b = c;
937 1347 : i = 1;
938 : } else {
939 469 : i = 0;
940 : }
941 1816 : c = Balloc(a->k);
942 1816 : c->sign = i;
943 1816 : wa = a->wds;
944 1816 : xa = a->x;
945 1816 : xae = xa + wa;
946 1816 : wb = b->wds;
947 1816 : xb = b->x;
948 1816 : xbe = xb + wb;
949 1816 : xc = c->x;
950 1816 : borrow = 0;
951 : #ifdef Pack_32
952 : do {
953 6848 : y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
954 6848 : borrow = y >> 16;
955 : Sign_Extend(borrow, y);
956 6848 : z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
957 6848 : borrow = z >> 16;
958 : Sign_Extend(borrow, z);
959 6848 : Storeinc(xc, z, y);
960 6848 : } while(xb < xbe);
961 3655 : while(xa < xae) {
962 23 : y = (*xa & 0xffff) + borrow;
963 23 : borrow = y >> 16;
964 : Sign_Extend(borrow, y);
965 23 : z = (*xa++ >> 16) + borrow;
966 23 : borrow = z >> 16;
967 : Sign_Extend(borrow, z);
968 23 : Storeinc(xc, z, y);
969 : }
970 : #else
971 : do {
972 : y = *xa++ - *xb++ + borrow;
973 : borrow = y >> 16;
974 : Sign_Extend(borrow, y);
975 : *xc++ = y & 0xffff;
976 : } while(xb < xbe);
977 : while(xa < xae) {
978 : y = *xa++ + borrow;
979 : borrow = y >> 16;
980 : Sign_Extend(borrow, y);
981 : *xc++ = y & 0xffff;
982 : }
983 : #endif
984 6671 : while(!*--xc) {
985 3039 : wa--;
986 : }
987 1816 : c->wds = wa;
988 1816 : return c;
989 : }
990 : /* }}} */
991 :
992 : static double ulp (double _x) /* {{{ */
993 750 : {
994 : volatile _double x;
995 : register Long L;
996 : volatile _double a;
997 :
998 750 : value(x) = _x;
999 750 : L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1000 : #ifndef Sudden_Underflow
1001 750 : if (L > 0) {
1002 : #endif
1003 : #ifdef IBM
1004 : L |= Exp_msk1 >> 4;
1005 : #endif
1006 750 : word0(a) = L;
1007 750 : word1(a) = 0;
1008 : #ifndef Sudden_Underflow
1009 : }
1010 : else {
1011 0 : L = -L >> Exp_shift;
1012 0 : if (L < Exp_shift) {
1013 0 : word0(a) = 0x80000 >> L;
1014 0 : word1(a) = 0;
1015 : }
1016 : else {
1017 0 : word0(a) = 0;
1018 0 : L -= Exp_shift;
1019 0 : word1(a) = L >= 31 ? 1 : 1 << (31 - L);
1020 : }
1021 : }
1022 : #endif
1023 750 : return value(a);
1024 : }
1025 : /* }}} */
1026 :
1027 : /* static double b2d () {{{ */
1028 : static double
1029 : b2d
1030 : #ifdef KR_headers
1031 : (a, e) Bigint *a; int *e;
1032 : #else
1033 : (Bigint *a, int *e)
1034 : #endif
1035 1470 : {
1036 : ULong *xa, *xa0, w, y, z;
1037 : int k;
1038 : volatile _double d;
1039 : #ifdef VAX
1040 : ULong d0, d1;
1041 : #else
1042 : #define d0 word0(d)
1043 : #define d1 word1(d)
1044 : #endif
1045 :
1046 1470 : xa0 = a->x;
1047 1470 : xa = xa0 + a->wds;
1048 1470 : y = *--xa;
1049 : #ifdef DEBUG
1050 : if (!y) Bug("zero y in b2d");
1051 : #endif
1052 1470 : k = hi0bits(y);
1053 1470 : *e = 32 - k;
1054 : #ifdef Pack_32
1055 1470 : if (k < Ebits) {
1056 446 : d0 = Exp_1 | y >> (Ebits - k);
1057 446 : w = xa > xa0 ? *--xa : 0;
1058 446 : d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1059 446 : goto ret_d;
1060 : }
1061 1024 : z = xa > xa0 ? *--xa : 0;
1062 1024 : if (k -= Ebits) {
1063 976 : d0 = Exp_1 | y << k | z >> (32 - k);
1064 976 : y = xa > xa0 ? *--xa : 0;
1065 976 : d1 = z << k | y >> (32 - k);
1066 : }
1067 : else {
1068 48 : d0 = Exp_1 | y;
1069 48 : d1 = z;
1070 : }
1071 : #else
1072 : if (k < Ebits + 16) {
1073 : z = xa > xa0 ? *--xa : 0;
1074 : d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1075 : w = xa > xa0 ? *--xa : 0;
1076 : y = xa > xa0 ? *--xa : 0;
1077 : d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1078 : goto ret_d;
1079 : }
1080 : z = xa > xa0 ? *--xa : 0;
1081 : w = xa > xa0 ? *--xa : 0;
1082 : k -= Ebits + 16;
1083 : d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1084 : y = xa > xa0 ? *--xa : 0;
1085 : d1 = w << k + 16 | y << k;
1086 : #endif
1087 1470 : ret_d:
1088 : #ifdef VAX
1089 : word0(d) = d0 >> 16 | d0 << 16;
1090 : word1(d) = d1 >> 16 | d1 << 16;
1091 : #else
1092 : #undef d0
1093 : #undef d1
1094 : #endif
1095 1470 : return value(d);
1096 : }
1097 : /* }}} */
1098 :
1099 : static Bigint * d2b(double _d, int *e, int *bits) /* {{{ */
1100 113428 : {
1101 : Bigint *b;
1102 : int de, i, k;
1103 : ULong *x, y, z;
1104 : volatile _double d;
1105 : #ifdef VAX
1106 : ULong d0, d1;
1107 : #endif
1108 :
1109 113428 : value(d) = _d;
1110 : #ifdef VAX
1111 : d0 = word0(d) >> 16 | word0(d) << 16;
1112 : d1 = word1(d) >> 16 | word1(d) << 16;
1113 : #else
1114 : #define d0 word0(d)
1115 : #define d1 word1(d)
1116 : #endif
1117 :
1118 : #ifdef Pack_32
1119 113428 : b = Balloc(1);
1120 : #else
1121 : b = Balloc(2);
1122 : #endif
1123 113428 : x = b->x;
1124 :
1125 113428 : z = d0 & Frac_mask;
1126 113428 : d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1127 : #ifdef Sudden_Underflow
1128 : de = (int)(d0 >> Exp_shift);
1129 : #ifndef IBM
1130 : z |= Exp_msk11;
1131 : #endif
1132 : #else
1133 113428 : if ((de = (int)(d0 >> Exp_shift)))
1134 113413 : z |= Exp_msk1;
1135 : #endif
1136 : #ifdef Pack_32
1137 113428 : if ((y = d1)) {
1138 106793 : if ((k = lo0bits(&y))) {
1139 53786 : x[0] = y | (z << (32 - k));
1140 53786 : z >>= k;
1141 : } else {
1142 53007 : x[0] = y;
1143 : }
1144 106793 : i = b->wds = (x[1] = z) ? 2 : 1;
1145 : } else {
1146 : #ifdef DEBUG
1147 : if (!z)
1148 : Bug("Zero passed to d2b");
1149 : #endif
1150 6635 : k = lo0bits(&z);
1151 6635 : x[0] = z;
1152 6635 : i = b->wds = 1;
1153 6635 : k += 32;
1154 : }
1155 : #else
1156 : if (y = d1) {
1157 : if (k = lo0bits(&y)) {
1158 : if (k >= 16) {
1159 : x[0] = y | z << 32 - k & 0xffff;
1160 : x[1] = z >> k - 16 & 0xffff;
1161 : x[2] = z >> k;
1162 : i = 2;
1163 : } else {
1164 : x[0] = y & 0xffff;
1165 : x[1] = y >> 16 | z << 16 - k & 0xffff;
1166 : x[2] = z >> k & 0xffff;
1167 : x[3] = z >> k+16;
1168 : i = 3;
1169 : }
1170 : } else {
1171 : x[0] = y & 0xffff;
1172 : x[1] = y >> 16;
1173 : x[2] = z & 0xffff;
1174 : x[3] = z >> 16;
1175 : i = 3;
1176 : }
1177 : } else {
1178 : #ifdef DEBUG
1179 : if (!z)
1180 : Bug("Zero passed to d2b");
1181 : #endif
1182 : k = lo0bits(&z);
1183 : if (k >= 16) {
1184 : x[0] = z;
1185 : i = 0;
1186 : } else {
1187 : x[0] = z & 0xffff;
1188 : x[1] = z >> 16;
1189 : i = 1;
1190 : }
1191 : k += 32;
1192 : }
1193 : while(!x[i])
1194 : --i;
1195 : b->wds = i + 1;
1196 : #endif
1197 : #ifndef Sudden_Underflow
1198 113428 : if (de) {
1199 : #endif
1200 : #ifdef IBM
1201 : *e = (de - Bias - (P-1) << 2) + k;
1202 : *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1203 : #else
1204 113413 : *e = de - Bias - (P-1) + k;
1205 113413 : *bits = P - k;
1206 : #endif
1207 : #ifndef Sudden_Underflow
1208 : } else {
1209 15 : *e = de - Bias - (P-1) + 1 + k;
1210 : #ifdef Pack_32
1211 15 : *bits = 32*i - hi0bits(x[i-1]);
1212 : #else
1213 : *bits = (i+2)*16 - hi0bits(x[i]);
1214 : #endif
1215 : }
1216 : #endif
1217 113428 : return b;
1218 : }
1219 : /* }}} */
1220 :
1221 : #undef d0
1222 : #undef d1
1223 :
1224 : static double ratio (Bigint *a, Bigint *b) /* {{{ */
1225 735 : {
1226 : volatile _double da, db;
1227 : int k, ka, kb;
1228 :
1229 735 : value(da) = b2d(a, &ka);
1230 735 : value(db) = b2d(b, &kb);
1231 : #ifdef Pack_32
1232 735 : k = ka - kb + 32*(a->wds - b->wds);
1233 : #else
1234 : k = ka - kb + 16*(a->wds - b->wds);
1235 : #endif
1236 : #ifdef IBM
1237 : if (k > 0) {
1238 : word0(da) += (k >> 2)*Exp_msk1;
1239 : if (k &= 3) {
1240 : da *= 1 << k;
1241 : }
1242 : } else {
1243 : k = -k;
1244 : word0(db) += (k >> 2)*Exp_msk1;
1245 : if (k &= 3)
1246 : db *= 1 << k;
1247 : }
1248 : #else
1249 735 : if (k > 0) {
1250 232 : word0(da) += k*Exp_msk1;
1251 : } else {
1252 503 : k = -k;
1253 503 : word0(db) += k*Exp_msk1;
1254 : }
1255 : #endif
1256 735 : return value(da) / value(db);
1257 : }
1258 : /* }}} */
1259 :
1260 : static CONST double
1261 : tens[] = {
1262 : 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1263 : 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1264 : 1e20, 1e21, 1e22
1265 : #ifdef VAX
1266 : , 1e23, 1e24
1267 : #endif
1268 : };
1269 :
1270 : #ifdef IEEE_Arith
1271 : static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1272 : static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1273 : #define n_bigtens 5
1274 : #else
1275 : #ifdef IBM
1276 : static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1277 : static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1278 : #define n_bigtens 3
1279 : #else
1280 : static CONST double bigtens[] = { 1e16, 1e32 };
1281 : static CONST double tinytens[] = { 1e-16, 1e-32 };
1282 : #define n_bigtens 2
1283 : #endif
1284 : #endif
1285 :
1286 : static int quorem(Bigint *b, Bigint *S) /* {{{ */
1287 8408 : {
1288 : int n;
1289 : Long borrow, y;
1290 : ULong carry, q, ys;
1291 : ULong *bx, *bxe, *sx, *sxe;
1292 : #ifdef Pack_32
1293 : Long z;
1294 : ULong si, zs;
1295 : #endif
1296 :
1297 8408 : n = S->wds;
1298 : #ifdef DEBUG
1299 : /*debug*/ if (b->wds > n)
1300 : /*debug*/ Bug("oversize b in quorem");
1301 : #endif
1302 8408 : if (b->wds < n)
1303 2921 : return 0;
1304 5487 : sx = S->x;
1305 5487 : sxe = sx + --n;
1306 5487 : bx = b->x;
1307 5487 : bxe = bx + n;
1308 5487 : q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1309 : #ifdef DEBUG
1310 : /*debug*/ if (q > 9)
1311 : /*debug*/ Bug("oversized quotient in quorem");
1312 : #endif
1313 5487 : if (q) {
1314 4621 : borrow = 0;
1315 4621 : carry = 0;
1316 : do {
1317 : #ifdef Pack_32
1318 25414 : si = *sx++;
1319 25414 : ys = (si & 0xffff) * q + carry;
1320 25414 : zs = (si >> 16) * q + (ys >> 16);
1321 25414 : carry = zs >> 16;
1322 25414 : y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1323 25414 : borrow = y >> 16;
1324 : Sign_Extend(borrow, y);
1325 25414 : z = (*bx >> 16) - (zs & 0xffff) + borrow;
1326 25414 : borrow = z >> 16;
1327 : Sign_Extend(borrow, z);
1328 25414 : Storeinc(bx, z, y);
1329 : #else
1330 : ys = *sx++ * q + carry;
1331 : carry = ys >> 16;
1332 : y = *bx - (ys & 0xffff) + borrow;
1333 : borrow = y >> 16;
1334 : Sign_Extend(borrow, y);
1335 : *bx++ = y & 0xffff;
1336 : #endif
1337 : }
1338 25414 : while(sx <= sxe);
1339 4621 : if (!*bxe) {
1340 0 : bx = b->x;
1341 0 : while(--bxe > bx && !*bxe)
1342 0 : --n;
1343 0 : b->wds = n;
1344 : }
1345 : }
1346 5487 : if (cmp(b, S) >= 0) {
1347 133 : q++;
1348 133 : borrow = 0;
1349 133 : carry = 0;
1350 133 : bx = b->x;
1351 133 : sx = S->x;
1352 : do {
1353 : #ifdef Pack_32
1354 492 : si = *sx++;
1355 492 : ys = (si & 0xffff) + carry;
1356 492 : zs = (si >> 16) + (ys >> 16);
1357 492 : carry = zs >> 16;
1358 492 : y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1359 492 : borrow = y >> 16;
1360 : Sign_Extend(borrow, y);
1361 492 : z = (*bx >> 16) - (zs & 0xffff) + borrow;
1362 492 : borrow = z >> 16;
1363 : Sign_Extend(borrow, z);
1364 492 : Storeinc(bx, z, y);
1365 : #else
1366 : ys = *sx++ + carry;
1367 : carry = ys >> 16;
1368 : y = *bx - (ys & 0xffff) + borrow;
1369 : borrow = y >> 16;
1370 : Sign_Extend(borrow, y);
1371 : *bx++ = y & 0xffff;
1372 : #endif
1373 : }
1374 492 : while(sx <= sxe);
1375 133 : bx = b->x;
1376 133 : bxe = bx + n;
1377 133 : if (!*bxe) {
1378 433 : while(--bxe > bx && !*bxe)
1379 171 : --n;
1380 131 : b->wds = n;
1381 : }
1382 : }
1383 5487 : return q;
1384 : }
1385 : /* }}} */
1386 :
1387 : static void destroy_freelist(void) /* {{{ */
1388 17039 : {
1389 : int i;
1390 : Bigint *tmp;
1391 :
1392 : _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
1393 289663 : for (i = 0; i <= Kmax; i++) {
1394 272624 : Bigint **listp = &freelist[i];
1395 547801 : while ((tmp = *listp) != NULL) {
1396 2553 : *listp = tmp->next;
1397 2553 : free(tmp);
1398 : }
1399 272624 : freelist[i] = NULL;
1400 : }
1401 : _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
1402 :
1403 17039 : }
1404 : /* }}} */
1405 :
1406 : ZEND_API void zend_freedtoa(char *s) /* {{{ */
1407 112056 : {
1408 112056 : Bigint *b = (Bigint *)((int *)s - 1);
1409 112056 : b->maxwds = 1 << (b->k = *(int*)b);
1410 112056 : Bfree(b);
1411 112056 : }
1412 : /* }}} */
1413 :
1414 : /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1415 : *
1416 : * Inspired by "How to Print Floating-Point Numbers Accurately" by
1417 : * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1418 : *
1419 : * Modifications:
1420 : * 1. Rather than iterating, we use a simple numeric overestimate
1421 : * to determine k = floor(log10(d)). We scale relevant
1422 : * quantities using O(log2(k)) rather than O(k) multiplications.
1423 : * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1424 : * try to generate digits strictly left to right. Instead, we
1425 : * compute with fewer bits and propagate the carry if necessary
1426 : * when rounding the final digit up. This is often faster.
1427 : * 3. Under the assumption that input will be rounded nearest,
1428 : * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1429 : * That is, we allow equality in stopping tests when the
1430 : * round-nearest rule will give the same floating-point value
1431 : * as would satisfaction of the stopping test with strict
1432 : * inequality.
1433 : * 4. We remove common factors of powers of 2 from relevant
1434 : * quantities.
1435 : * 5. When converting floating-point integers less than 1e16,
1436 : * we use floating-point arithmetic rather than resorting
1437 : * to multiple-precision integers.
1438 : * 6. When asked to produce fewer than 15 digits, we first try
1439 : * to get by with floating-point arithmetic; we resort to
1440 : * multiple-precision integer arithmetic only if we cannot
1441 : * guarantee that the floating-point calculation has given
1442 : * the correctly rounded result. For k requested digits and
1443 : * "uniformly" distributed input, the probability is
1444 : * something like 10^(k-15) that we must resort to the Long
1445 : * calculation.
1446 : */
1447 :
1448 : ZEND_API char * zend_dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve) /* {{{ */
1449 112056 : {
1450 : /* Arguments ndigits, decpt, sign are similar to those
1451 : of ecvt and fcvt; trailing zeros are suppressed from
1452 : the returned string. If not null, *rve is set to point
1453 : to the end of the return value. If d is +-Infinity or NaN,
1454 : then *decpt is set to 9999.
1455 :
1456 : mode:
1457 : 0 ==> shortest string that yields d when read in
1458 : and rounded to nearest.
1459 : 1 ==> like 0, but with Steele & White stopping rule;
1460 : e.g. with IEEE P754 arithmetic , mode 0 gives
1461 : 1e23 whereas mode 1 gives 9.999999999999999e22.
1462 : 2 ==> max(1,ndigits) significant digits. This gives a
1463 : return value similar to that of ecvt, except
1464 : that trailing zeros are suppressed.
1465 : 3 ==> through ndigits past the decimal point. This
1466 : gives a return value similar to that from fcvt,
1467 : except that trailing zeros are suppressed, and
1468 : ndigits can be negative.
1469 : 4-9 should give the same return values as 2-3, i.e.,
1470 : 4 <= mode <= 9 ==> same return as mode
1471 : 2 + (mode & 1). These modes are mainly for
1472 : debugging; often they run slower but sometimes
1473 : faster than modes 2-3.
1474 : 4,5,8,9 ==> left-to-right digit generation.
1475 : 6-9 ==> don't try fast floating-point estimate
1476 : (if applicable).
1477 :
1478 : Values of mode other than 0-9 are treated as mode 0.
1479 :
1480 : Sufficient space is allocated to the return value
1481 : to hold the suppressed trailing zeros.
1482 : */
1483 :
1484 112056 : int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1,
1485 : j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1486 112056 : spec_case = 0, try_quick;
1487 : Long L;
1488 : #ifndef Sudden_Underflow
1489 : int denorm;
1490 : ULong x;
1491 : #endif
1492 : Bigint *b, *b1, *delta, *mlo, *mhi, *S, *tmp;
1493 : double ds;
1494 : char *s, *s0;
1495 : volatile _double d, d2, eps;
1496 :
1497 112056 : value(d) = _d;
1498 :
1499 112056 : if (word0(d) & Sign_bit) {
1500 : /* set sign for everything, including 0's and NaNs */
1501 2539 : *sign = 1;
1502 2539 : word0(d) &= ~Sign_bit; /* clear sign bit */
1503 : }
1504 : else
1505 109517 : *sign = 0;
1506 :
1507 : #if defined(IEEE_Arith) + defined(VAX)
1508 : #ifdef IEEE_Arith
1509 112056 : if ((word0(d) & Exp_mask) == Exp_mask)
1510 : #else
1511 : if (word0(d) == 0x8000)
1512 : #endif
1513 : {
1514 : /* Infinity or NaN */
1515 12 : *decpt = 9999;
1516 : #ifdef IEEE_Arith
1517 12 : if (!word1(d) && !(word0(d) & 0xfffff))
1518 8 : return nrv_alloc("Infinity", rve, 8);
1519 : #endif
1520 4 : return nrv_alloc("NaN", rve, 3);
1521 : }
1522 : #endif
1523 : #ifdef IBM
1524 : value(d) += 0; /* normalize */
1525 : #endif
1526 112044 : if (!value(d)) {
1527 545 : *decpt = 1;
1528 545 : return nrv_alloc("0", rve, 1);
1529 : }
1530 :
1531 111499 : b = d2b(value(d), &be, &bbits);
1532 : #ifdef Sudden_Underflow
1533 : i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1534 : #else
1535 111499 : if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
1536 : #endif
1537 111490 : value(d2) = value(d);
1538 111490 : word0(d2) &= Frac_mask1;
1539 111490 : word0(d2) |= Exp_11;
1540 : #ifdef IBM
1541 : if (j = 11 - hi0bits(word0(d2) & Frac_mask))
1542 : value(d2) /= 1 << j;
1543 : #endif
1544 :
1545 : /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1546 : * log10(x) = log(x) / log(10)
1547 : * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1548 : * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1549 : *
1550 : * This suggests computing an approximation k to log10(d) by
1551 : *
1552 : * k = (i - Bias)*0.301029995663981
1553 : * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1554 : *
1555 : * We want k to be too large rather than too small.
1556 : * The error in the first-order Taylor series approximation
1557 : * is in our favor, so we just round up the constant enough
1558 : * to compensate for any error in the multiplication of
1559 : * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1560 : * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1561 : * adding 1e-13 to the constant term more than suffices.
1562 : * Hence we adjust the constant term to 0.1760912590558.
1563 : * (We could get a more accurate k by invoking log10,
1564 : * but this is probably not worthwhile.)
1565 : */
1566 :
1567 111490 : i -= Bias;
1568 : #ifdef IBM
1569 : i <<= 2;
1570 : i += j;
1571 : #endif
1572 : #ifndef Sudden_Underflow
1573 111490 : denorm = 0;
1574 : }
1575 : else {
1576 : /* d is denormalized */
1577 :
1578 9 : i = bbits + be + (Bias + (P-1) - 1);
1579 9 : x = i > 32 ? (word0(d) << (64 - i)) | (word1(d) >> (i - 32))
1580 : : (word1(d) << (32 - i));
1581 9 : value(d2) = x;
1582 9 : word0(d2) -= 31*Exp_msk1; /* adjust exponent */
1583 9 : i -= (Bias + (P-1) - 1) + 1;
1584 9 : denorm = 1;
1585 : }
1586 : #endif
1587 111499 : ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
1588 111499 : k = (int)ds;
1589 111499 : if (ds < 0. && ds != k)
1590 97995 : k--; /* want k = floor(ds) */
1591 111499 : k_check = 1;
1592 111499 : if (k >= 0 && k <= Ten_pmax) {
1593 13395 : if (value(d) < tens[k])
1594 3516 : k--;
1595 13395 : k_check = 0;
1596 : }
1597 111499 : j = bbits - i - 1;
1598 111499 : if (j >= 0) {
1599 108206 : b2 = 0;
1600 108206 : s2 = j;
1601 : }
1602 : else {
1603 3293 : b2 = -j;
1604 3293 : s2 = 0;
1605 : }
1606 111499 : if (k >= 0) {
1607 10078 : b5 = 0;
1608 10078 : s5 = k;
1609 10078 : s2 += k;
1610 : }
1611 : else {
1612 101421 : b2 -= k;
1613 101421 : b5 = -k;
1614 101421 : s5 = 0;
1615 : }
1616 111499 : if (mode < 0 || mode > 9)
1617 0 : mode = 0;
1618 111499 : try_quick = 1;
1619 111499 : if (mode > 5) {
1620 0 : mode -= 4;
1621 0 : try_quick = 0;
1622 : }
1623 111499 : leftright = 1;
1624 111499 : switch(mode) {
1625 : case 0:
1626 : case 1:
1627 0 : ilim = ilim1 = -1;
1628 0 : i = 18;
1629 0 : ndigits = 0;
1630 0 : break;
1631 : case 2:
1632 7396 : leftright = 0;
1633 : /* no break */
1634 : case 4:
1635 7396 : if (ndigits <= 0)
1636 0 : ndigits = 1;
1637 7396 : ilim = ilim1 = i = ndigits;
1638 7396 : break;
1639 : case 3:
1640 104103 : leftright = 0;
1641 : /* no break */
1642 : case 5:
1643 104103 : i = ndigits + k + 1;
1644 104103 : ilim = i;
1645 104103 : ilim1 = i - 1;
1646 104103 : if (i <= 0)
1647 41 : i = 1;
1648 : }
1649 111499 : s = s0 = rv_alloc(i);
1650 :
1651 111499 : if (ilim >= 0 && ilim <= Quick_max && try_quick) {
1652 :
1653 : /* Try to get by with floating-point arithmetic. */
1654 :
1655 111232 : i = 0;
1656 111232 : value(d2) = value(d);
1657 111232 : k0 = k;
1658 111232 : ilim0 = ilim;
1659 111232 : ieps = 2; /* conservative */
1660 111232 : if (k > 0) {
1661 6837 : ds = tens[k&0xf];
1662 6837 : j = k >> 4;
1663 6837 : if (j & Bletch) {
1664 : /* prevent overflows */
1665 3 : j &= Bletch - 1;
1666 3 : value(d) /= bigtens[n_bigtens-1];
1667 3 : ieps++;
1668 : }
1669 6988 : for(; j; j >>= 1, i++)
1670 151 : if (j & 1) {
1671 96 : ieps++;
1672 96 : ds *= bigtens[i];
1673 : }
1674 6837 : value(d) /= ds;
1675 : }
1676 104395 : else if ((j1 = -k)) {
1677 101367 : value(d) *= tens[j1 & 0xf];
1678 101529 : for(j = j1 >> 4; j; j >>= 1, i++)
1679 162 : if (j & 1) {
1680 96 : ieps++;
1681 96 : value(d) *= bigtens[i];
1682 : }
1683 : }
1684 111232 : if (k_check && value(d) < 1. && ilim > 0) {
1685 48 : if (ilim1 <= 0)
1686 0 : goto fast_failed;
1687 48 : ilim = ilim1;
1688 48 : k--;
1689 48 : value(d) *= 10.;
1690 48 : ieps++;
1691 : }
1692 111232 : value(eps) = ieps*value(d) + 7.;
1693 111232 : word0(eps) -= (P-1)*Exp_msk1;
1694 111232 : if (ilim == 0) {
1695 6 : S = mhi = 0;
1696 6 : value(d) -= 5.;
1697 6 : if (value(d) > value(eps))
1698 0 : goto one_digit;
1699 6 : if (value(d) < -value(eps))
1700 6 : goto no_digits;
1701 0 : goto fast_failed;
1702 : }
1703 : #ifndef No_leftright
1704 111226 : if (leftright) {
1705 : /* Use Steele & White method of only
1706 : * generating digits needed.
1707 : */
1708 0 : value(eps) = 0.5/tens[ilim-1] - value(eps);
1709 0 : for(i = 0;;) {
1710 0 : L = value(d);
1711 0 : value(d) -= L;
1712 0 : *s++ = '0' + (int)L;
1713 0 : if (value(d) < value(eps))
1714 0 : goto ret1;
1715 0 : if (1. - value(d) < value(eps))
1716 0 : goto bump_up;
1717 0 : if (++i >= ilim)
1718 0 : break;
1719 0 : value(eps) *= 10.;
1720 0 : value(d) *= 10.;
1721 0 : }
1722 : }
1723 : else {
1724 : #endif
1725 : /* Generate ilim digits, then fix them up. */
1726 111226 : value(eps) *= tens[ilim-1];
1727 912468 : for(i = 1;; i++, value(d) *= 10.) {
1728 912468 : L = value(d);
1729 912468 : value(d) -= L;
1730 912468 : *s++ = '0' + (int)L;
1731 912468 : if (i == ilim) {
1732 111226 : if (value(d) > 0.5 + value(eps))
1733 53730 : goto bump_up;
1734 57496 : else if (value(d) < 0.5 - value(eps)) {
1735 219435 : while(*--s == '0');
1736 57449 : s++;
1737 57449 : goto ret1;
1738 : }
1739 47 : break;
1740 : }
1741 801242 : }
1742 : #ifndef No_leftright
1743 : }
1744 : #endif
1745 47 : fast_failed:
1746 47 : s = s0;
1747 47 : value(d) = value(d2);
1748 47 : k = k0;
1749 47 : ilim = ilim0;
1750 : }
1751 :
1752 : /* Do we have a "small" integer? */
1753 :
1754 314 : if (be >= 0 && k <= Int_max) {
1755 : /* Yes. */
1756 144 : ds = tens[k];
1757 144 : if (ndigits < 0 && ilim <= 0) {
1758 0 : S = mhi = 0;
1759 0 : if (ilim < 0 || value(d) <= 5*ds)
1760 : goto no_digits;
1761 0 : goto one_digit;
1762 : }
1763 1388 : for(i = 1;; i++) {
1764 1388 : L = value(d) / ds;
1765 1388 : value(d) -= L*ds;
1766 : #ifdef Check_FLT_ROUNDS
1767 : /* If FLT_ROUNDS == 2, L will usually be high by 1 */
1768 : if (value(d) < 0) {
1769 : L--;
1770 : value(d) += ds;
1771 : }
1772 : #endif
1773 1388 : *s++ = '0' + (int)L;
1774 1388 : if (i == ilim) {
1775 0 : value(d) += value(d);
1776 0 : if (value(d) > ds || (value(d) == ds && (L & 1))) {
1777 53730 : bump_up:
1778 230592 : while(*--s == '9')
1779 123136 : if (s == s0) {
1780 4 : k++;
1781 4 : *s = '0';
1782 4 : break;
1783 : }
1784 53730 : ++*s++;
1785 : }
1786 53730 : break;
1787 : }
1788 1388 : if (!(value(d) *= 10.))
1789 144 : break;
1790 1244 : }
1791 53874 : goto ret1;
1792 : }
1793 :
1794 170 : m2 = b2;
1795 170 : m5 = b5;
1796 170 : mhi = mlo = 0;
1797 170 : if (leftright) {
1798 0 : if (mode < 2) {
1799 0 : i =
1800 : #ifndef Sudden_Underflow
1801 : denorm ? be + (Bias + (P-1) - 1 + 1) :
1802 : #endif
1803 : #ifdef IBM
1804 : 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
1805 : #else
1806 : 1 + P - bbits;
1807 : #endif
1808 : }
1809 : else {
1810 0 : j = ilim - 1;
1811 0 : if (m5 >= j)
1812 0 : m5 -= j;
1813 : else {
1814 0 : s5 += j -= m5;
1815 0 : b5 += j;
1816 0 : m5 = 0;
1817 : }
1818 0 : if ((i = ilim) < 0) {
1819 0 : m2 -= i;
1820 0 : i = 0;
1821 : }
1822 : }
1823 0 : b2 += i;
1824 0 : s2 += i;
1825 0 : mhi = i2b(1);
1826 : }
1827 170 : if (m2 > 0 && s2 > 0) {
1828 107 : i = m2 < s2 ? m2 : s2;
1829 107 : b2 -= i;
1830 107 : m2 -= i;
1831 107 : s2 -= i;
1832 : }
1833 170 : if (b5 > 0) {
1834 64 : if (leftright) {
1835 0 : if (m5 > 0) {
1836 0 : mhi = pow5mult(mhi, m5);
1837 0 : b1 = mult(mhi, b);
1838 0 : Bfree(b);
1839 0 : b = b1;
1840 : }
1841 0 : if ((j = b5 - m5)) {
1842 0 : b = pow5mult(b, j);
1843 : }
1844 : } else {
1845 64 : b = pow5mult(b, b5);
1846 : }
1847 : }
1848 170 : S = i2b(1);
1849 170 : if (s5 > 0)
1850 87 : S = pow5mult(S, s5);
1851 : /* Check for special case that d is a normalized power of 2. */
1852 :
1853 170 : if (mode < 2) {
1854 0 : if (!word1(d) && !(word0(d) & Bndry_mask)
1855 : #ifndef Sudden_Underflow
1856 : && word0(d) & Exp_mask
1857 : #endif
1858 : ) {
1859 : /* The special case */
1860 0 : b2 += Log2P;
1861 0 : s2 += Log2P;
1862 0 : spec_case = 1;
1863 : } else {
1864 0 : spec_case = 0;
1865 : }
1866 : }
1867 :
1868 : /* Arrange for convenient computation of quotients:
1869 : * shift left if necessary so divisor has 4 leading 0 bits.
1870 : *
1871 : * Perhaps we should just compute leading 28 bits of S once
1872 : * and for all and pass them and a shift to quorem, so it
1873 : * can do shifts and ors to compute the numerator for q.
1874 : */
1875 : #ifdef Pack_32
1876 170 : if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
1877 170 : i = 32 - i;
1878 : #else
1879 : if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
1880 : i = 16 - i;
1881 : #endif
1882 170 : if (i > 4) {
1883 153 : i -= 4;
1884 153 : b2 += i;
1885 153 : m2 += i;
1886 153 : s2 += i;
1887 : }
1888 17 : else if (i < 4) {
1889 17 : i += 28;
1890 17 : b2 += i;
1891 17 : m2 += i;
1892 17 : s2 += i;
1893 : }
1894 170 : if (b2 > 0)
1895 170 : b = lshift(b, b2);
1896 170 : if (s2 > 0)
1897 170 : S = lshift(S, s2);
1898 170 : if (k_check) {
1899 104 : if (cmp(b,S) < 0) {
1900 1 : k--;
1901 1 : b = multadd(b, 10, 0); /* we botched the k estimate */
1902 1 : if (leftright)
1903 0 : mhi = multadd(mhi, 10, 0);
1904 1 : ilim = ilim1;
1905 : }
1906 : }
1907 170 : if (ilim <= 0 && mode > 2) {
1908 35 : if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
1909 : /* no digits, fcvt style */
1910 41 : no_digits:
1911 41 : k = -1 - ndigits;
1912 41 : goto ret;
1913 : }
1914 0 : one_digit:
1915 0 : *s++ = '1';
1916 0 : k++;
1917 0 : goto ret;
1918 : }
1919 135 : if (leftright) {
1920 0 : if (m2 > 0)
1921 0 : mhi = lshift(mhi, m2);
1922 :
1923 : /* Compute mlo -- check for special case
1924 : * that d is a normalized power of 2.
1925 : */
1926 :
1927 0 : mlo = mhi;
1928 0 : if (spec_case) {
1929 0 : mhi = Balloc(mhi->k);
1930 0 : Bcopy(mhi, mlo);
1931 0 : mhi = lshift(mhi, Log2P);
1932 : }
1933 :
1934 0 : for(i = 1;;i++) {
1935 0 : dig = quorem(b,S) + '0';
1936 : /* Do we yet have the shortest decimal string
1937 : * that will round to d?
1938 : */
1939 0 : j = cmp(b, mlo);
1940 0 : delta = diff(S, mhi);
1941 0 : j1 = delta->sign ? 1 : cmp(b, delta);
1942 0 : Bfree(delta);
1943 : #ifndef ROUND_BIASED
1944 0 : if (j1 == 0 && !mode && !(word1(d) & 1)) {
1945 0 : if (dig == '9')
1946 0 : goto round_9_up;
1947 0 : if (j > 0)
1948 0 : dig++;
1949 0 : *s++ = dig;
1950 0 : goto ret;
1951 : }
1952 : #endif
1953 0 : if (j < 0 || (j == 0 && !mode
1954 : #ifndef ROUND_BIASED
1955 : && !(word1(d) & 1)
1956 : #endif
1957 : )) {
1958 0 : if (j1 > 0) {
1959 0 : b = lshift(b, 1);
1960 0 : j1 = cmp(b, S);
1961 0 : if ((j1 > 0 || (j1 == 0 && (dig & 1)))
1962 : && dig++ == '9')
1963 0 : goto round_9_up;
1964 : }
1965 0 : *s++ = dig;
1966 0 : goto ret;
1967 : }
1968 0 : if (j1 > 0) {
1969 0 : if (dig == '9') { /* possible if i == 1 */
1970 0 : round_9_up:
1971 0 : *s++ = '9';
1972 0 : goto roundoff;
1973 : }
1974 0 : *s++ = dig + 1;
1975 0 : goto ret;
1976 : }
1977 0 : *s++ = dig;
1978 0 : if (i == ilim)
1979 0 : break;
1980 0 : b = multadd(b, 10, 0);
1981 0 : if (mlo == mhi)
1982 0 : mlo = mhi = multadd(mhi, 10, 0);
1983 : else {
1984 0 : mlo = multadd(mlo, 10, 0);
1985 0 : mhi = multadd(mhi, 10, 0);
1986 : }
1987 0 : }
1988 : }
1989 : else
1990 8408 : for(i = 1;; i++) {
1991 8408 : *s++ = dig = quorem(b,S) + '0';
1992 8408 : if (i >= ilim)
1993 135 : break;
1994 8273 : b = multadd(b, 10, 0);
1995 8273 : }
1996 :
1997 : /* Round off last digit */
1998 :
1999 135 : b = lshift(b, 1);
2000 135 : j = cmp(b, S);
2001 172 : if (j > 0 || (j == 0 && (dig & 1))) {
2002 37 : roundoff:
2003 75 : while(*--s == '9')
2004 1 : if (s == s0) {
2005 0 : k++;
2006 0 : *s++ = '1';
2007 0 : goto ret;
2008 : }
2009 37 : ++*s++;
2010 : }
2011 : else {
2012 2752 : while(*--s == '0');
2013 98 : s++;
2014 : }
2015 176 : ret:
2016 176 : Bfree(S);
2017 176 : if (mhi) {
2018 0 : if (mlo && mlo != mhi)
2019 0 : Bfree(mlo);
2020 0 : Bfree(mhi);
2021 : }
2022 111499 : ret1:
2023 :
2024 : _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
2025 223329 : while (p5s) {
2026 331 : tmp = p5s;
2027 331 : p5s = p5s->next;
2028 331 : free(tmp);
2029 : }
2030 : _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
2031 :
2032 111499 : Bfree(b);
2033 :
2034 111499 : if (s == s0) { /* don't return empty string */
2035 41 : *s++ = '0';
2036 41 : k = 0;
2037 : }
2038 111499 : *s = 0;
2039 111499 : *decpt = k + 1;
2040 111499 : if (rve)
2041 104843 : *rve = s;
2042 111499 : return s0;
2043 : }
2044 : /* }}} */
2045 :
2046 : ZEND_API double zend_strtod (CONST char *s00, char **se) /* {{{ */
2047 232110 : {
2048 : int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
2049 : e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
2050 : CONST char *s, *s0, *s1;
2051 : double aadj, aadj1, adj;
2052 : volatile _double rv, rv0;
2053 : Long L;
2054 : ULong y, z;
2055 : Bigint *bb, *bb1, *bd, *bd0, *bs, *delta, *tmp;
2056 : double result;
2057 :
2058 232110 : CONST char decimal_point = '.';
2059 :
2060 232110 : sign = nz0 = nz = 0;
2061 232110 : value(rv) = 0.;
2062 :
2063 :
2064 232110 : for(s = s00; isspace((unsigned char) *s); s++)
2065 : ;
2066 :
2067 232110 : if (*s == '-') {
2068 1005 : sign = 1;
2069 1005 : s++;
2070 231105 : } else if (*s == '+') {
2071 23 : s++;
2072 : }
2073 :
2074 232110 : if (*s == '\0') {
2075 0 : s = s00;
2076 0 : goto ret;
2077 : }
2078 :
2079 232110 : if (*s == '0') {
2080 218204 : nz0 = 1;
2081 218892 : while(*++s == '0') ;
2082 218204 : if (!*s)
2083 17139 : goto ret;
2084 : }
2085 214971 : s0 = s;
2086 214971 : y = z = 0;
2087 305423 : for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
2088 90452 : if (nd < 9)
2089 50722 : y = 10*y + c - '0';
2090 39730 : else if (nd < 16)
2091 16137 : z = 10*z + c - '0';
2092 214971 : nd0 = nd;
2093 214971 : if (c == decimal_point) {
2094 209425 : c = *++s;
2095 209425 : if (!nd) {
2096 224227 : for(; c == '0'; c = *++s)
2097 22401 : nz++;
2098 201826 : if (c > '0' && c <= '9') {
2099 201595 : s0 = s;
2100 201595 : nf += nz;
2101 201595 : nz = 0;
2102 201595 : goto have_dig;
2103 : }
2104 231 : goto dig_done;
2105 : }
2106 1611782 : for(; c >= '0' && c <= '9'; c = *++s) {
2107 1604183 : have_dig:
2108 1604183 : nz++;
2109 1604183 : if (c -= '0') {
2110 1101201 : nf += nz;
2111 1178799 : for(i = 1; i < nz; i++)
2112 77598 : if (nd++ < 9)
2113 76871 : y *= 10;
2114 727 : else if (nd <= DBL_DIG + 1)
2115 483 : z *= 10;
2116 1101201 : if (nd++ < 9)
2117 1098909 : y = 10*y + c;
2118 2292 : else if (nd <= DBL_DIG + 1)
2119 1034 : z = 10*z + c;
2120 1101201 : nz = 0;
2121 : }
2122 : }
2123 : }
2124 214971 : dig_done:
2125 214971 : e = 0;
2126 214971 : if (c == 'e' || c == 'E') {
2127 2689 : if (!nd && !nz && !nz0) {
2128 0 : s = s00;
2129 0 : goto ret;
2130 : }
2131 2689 : s00 = s;
2132 2689 : esign = 0;
2133 2689 : switch(c = *++s) {
2134 : case '-':
2135 700 : esign = 1;
2136 : case '+':
2137 918 : c = *++s;
2138 : }
2139 5376 : if (c >= '0' && c <= '9') {
2140 5390 : while(c == '0')
2141 16 : c = *++s;
2142 5364 : if (c > '0' && c <= '9') {
2143 2677 : L = c - '0';
2144 2677 : s1 = s;
2145 6734 : while((c = *++s) >= '0' && c <= '9')
2146 1380 : L = 10*L + c - '0';
2147 2677 : if (s - s1 > 8 || L > 19999)
2148 : /* Avoid confusion from exponents
2149 : * so large that e might overflow.
2150 : */
2151 0 : e = 19999; /* safe for 16 bit ints */
2152 : else
2153 2677 : e = (int)L;
2154 2677 : if (esign)
2155 699 : e = -e;
2156 : }
2157 : else
2158 10 : e = 0;
2159 : }
2160 : else
2161 2 : s = s00;
2162 : }
2163 214971 : if (!nd) {
2164 246 : if (!nz && !nz0)
2165 82 : s = s00;
2166 246 : goto ret;
2167 : }
2168 214725 : e1 = e -= nf;
2169 :
2170 : /* Now we have nd0 digits, starting at s0, followed by a
2171 : * decimal point, followed by nd-nd0 digits. The number we're
2172 : * after is the integer represented by those digits times
2173 : * 10**e */
2174 :
2175 214725 : if (!nd0)
2176 201595 : nd0 = nd;
2177 214725 : k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
2178 214725 : value(rv) = y;
2179 214725 : if (k > 9)
2180 3894 : value(rv) = tens[k - 9] * value(rv) + z;
2181 214725 : bd0 = 0;
2182 214725 : if (nd <= DBL_DIG
2183 : #ifndef RND_PRODQUOT
2184 : && FLT_ROUNDS == 1
2185 : #endif
2186 : ) {
2187 212923 : if (!e)
2188 4147 : goto ret;
2189 208776 : if (e > 0) {
2190 1632 : if (e <= Ten_pmax) {
2191 : #ifdef VAX
2192 : goto vax_ovfl_check;
2193 : #else
2194 1545 : /* value(rv) = */ rounded_product(value(rv),
2195 : tens[e]);
2196 1545 : goto ret;
2197 : #endif
2198 : }
2199 87 : i = DBL_DIG - nd;
2200 87 : if (e <= Ten_pmax + i) {
2201 : /* A fancier test would sometimes let us do
2202 : * this for larger i values.
2203 : */
2204 25 : e -= i;
2205 25 : value(rv) *= tens[i];
2206 : #ifdef VAX
2207 : /* VAX exponent range is so narrow we must
2208 : * worry about overflow here...
2209 : */
2210 : vax_ovfl_check:
2211 : word0(rv) -= P*Exp_msk1;
2212 : /* value(rv) = */ rounded_product(value(rv),
2213 : tens[e]);
2214 : if ((word0(rv) & Exp_mask)
2215 : > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2216 : goto ovfl;
2217 : word0(rv) += P*Exp_msk1;
2218 : #else
2219 25 : /* value(rv) = */ rounded_product(value(rv),
2220 : tens[e]);
2221 : #endif
2222 25 : goto ret;
2223 : }
2224 : }
2225 : #ifndef Inaccurate_Divide
2226 207144 : else if (e >= -Ten_pmax) {
2227 207087 : /* value(rv) = */ rounded_quotient(value(rv),
2228 : tens[-e]);
2229 207087 : goto ret;
2230 : }
2231 : #endif
2232 : }
2233 1921 : e1 += nd - k;
2234 :
2235 : /* Get starting approximation = rv * 10**e1 */
2236 :
2237 1921 : if (e1 > 0) {
2238 1725 : if ((i = e1 & 15))
2239 1640 : value(rv) *= tens[i];
2240 1725 : if (e1 &= ~15) {
2241 795 : if (e1 > DBL_MAX_10_EXP) {
2242 13 : ovfl:
2243 13 : errno = ERANGE;
2244 : #ifndef Bad_float_h
2245 13 : value(rv) = HUGE_VAL;
2246 : #else
2247 : /* Can't trust HUGE_VAL */
2248 : #ifdef IEEE_Arith
2249 : word0(rv) = Exp_mask;
2250 : word1(rv) = 0;
2251 : #else
2252 : word0(rv) = Big0;
2253 : word1(rv) = Big1;
2254 : #endif
2255 : #endif
2256 13 : if (bd0)
2257 0 : goto retfree;
2258 13 : goto ret;
2259 : }
2260 785 : if (e1 >>= 4) {
2261 871 : for(j = 0; e1 > 1; j++, e1 >>= 1)
2262 86 : if (e1 & 1)
2263 22 : value(rv) *= bigtens[j];
2264 : /* The last multiplication could overflow. */
2265 785 : word0(rv) -= P*Exp_msk1;
2266 785 : value(rv) *= bigtens[j];
2267 785 : if ((z = word0(rv) & Exp_mask)
2268 : > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2269 3 : goto ovfl;
2270 782 : if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2271 : /* set to largest number */
2272 : /* (Can't trust DBL_MAX) */
2273 0 : word0(rv) = Big0;
2274 0 : word1(rv) = Big1;
2275 : }
2276 : else
2277 782 : word0(rv) += P*Exp_msk1;
2278 : }
2279 :
2280 : }
2281 : }
2282 196 : else if (e1 < 0) {
2283 140 : e1 = -e1;
2284 140 : if ((i = e1 & 15))
2285 135 : value(rv) /= tens[i];
2286 140 : if (e1 &= ~15) {
2287 71 : e1 >>= 4;
2288 71 : if (e1 >= 1 << n_bigtens)
2289 1 : goto undfl;
2290 142 : for(j = 0; e1 > 1; j++, e1 >>= 1)
2291 72 : if (e1 & 1)
2292 13 : value(rv) *= tinytens[j];
2293 : /* The last multiplication could underflow. */
2294 70 : value(rv0) = value(rv);
2295 70 : value(rv) *= tinytens[j];
2296 70 : if (!value(rv)) {
2297 2 : value(rv) = 2.*value(rv0);
2298 2 : value(rv) *= tinytens[j];
2299 2 : if (!value(rv)) {
2300 3 : undfl:
2301 3 : value(rv) = 0.;
2302 3 : errno = ERANGE;
2303 3 : if (bd0)
2304 1 : goto retfree;
2305 2 : goto ret;
2306 : }
2307 1 : word0(rv) = Tiny0;
2308 1 : word1(rv) = Tiny1;
2309 : /* The refinement below will clean
2310 : * this approximation up.
2311 : */
2312 : }
2313 : }
2314 : }
2315 :
2316 : /* Now the hard part -- adjusting rv to the correct value.*/
2317 :
2318 : /* Put digits into bd: true value = bd * 10^e */
2319 :
2320 1906 : bd0 = s2b(s0, nd0, nd, y);
2321 :
2322 : for(;;) {
2323 1929 : bd = Balloc(bd0->k);
2324 1929 : Bcopy(bd, bd0);
2325 1929 : bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
2326 1929 : bs = i2b(1);
2327 :
2328 1929 : if (e >= 0) {
2329 1788 : bb2 = bb5 = 0;
2330 1788 : bd2 = bd5 = e;
2331 : }
2332 : else {
2333 141 : bb2 = bb5 = -e;
2334 141 : bd2 = bd5 = 0;
2335 : }
2336 1929 : if (bbe >= 0)
2337 1819 : bb2 += bbe;
2338 : else
2339 110 : bd2 -= bbe;
2340 1929 : bs2 = bb2;
2341 : #ifdef Sudden_Underflow
2342 : #ifdef IBM
2343 : j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2344 : #else
2345 : j = P + 1 - bbbits;
2346 : #endif
2347 : #else
2348 1929 : i = bbe + bbbits - 1; /* logb(rv) */
2349 1929 : if (i < Emin) /* denormal */
2350 6 : j = bbe + (P-Emin);
2351 : else
2352 1923 : j = P + 1 - bbbits;
2353 : #endif
2354 1929 : bb2 += j;
2355 1929 : bd2 += j;
2356 1929 : i = bb2 < bd2 ? bb2 : bd2;
2357 1929 : if (i > bs2)
2358 192 : i = bs2;
2359 1929 : if (i > 0) {
2360 1893 : bb2 -= i;
2361 1893 : bd2 -= i;
2362 1893 : bs2 -= i;
2363 : }
2364 1929 : if (bb5 > 0) {
2365 141 : bs = pow5mult(bs, bb5);
2366 141 : bb1 = mult(bs, bb);
2367 141 : Bfree(bb);
2368 141 : bb = bb1;
2369 : }
2370 1929 : if (bb2 > 0)
2371 1929 : bb = lshift(bb, bb2);
2372 1929 : if (bd5 > 0)
2373 49 : bd = pow5mult(bd, bd5);
2374 1929 : if (bd2 > 0)
2375 192 : bd = lshift(bd, bd2);
2376 1929 : if (bs2 > 0)
2377 1683 : bs = lshift(bs, bs2);
2378 1929 : delta = diff(bb, bd);
2379 1929 : dsign = delta->sign;
2380 1929 : delta->sign = 0;
2381 1929 : i = cmp(delta, bs);
2382 1929 : if (i < 0) {
2383 : /* Error is less than half an ulp -- check for
2384 : * special case of mantissa a power of two.
2385 : */
2386 1157 : if (dsign || word1(rv) || word0(rv) & Bndry_mask)
2387 : break;
2388 66 : delta = lshift(delta,Log2P);
2389 66 : if (cmp(delta, bs) > 0)
2390 0 : goto drop_down;
2391 66 : break;
2392 : }
2393 772 : if (i == 0) {
2394 : /* exactly half-way between */
2395 37 : if (dsign) {
2396 36 : if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2397 : && word1(rv) == 0xffffffff) {
2398 : /*boundary case -- increment exponent*/
2399 0 : word0(rv) = (word0(rv) & Exp_mask)
2400 : + Exp_msk1
2401 : #ifdef IBM
2402 : | Exp_msk1 >> 4
2403 : #endif
2404 : ;
2405 0 : word1(rv) = 0;
2406 0 : break;
2407 : }
2408 : }
2409 1 : else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2410 0 : drop_down:
2411 : /* boundary case -- decrement exponent */
2412 : #ifdef Sudden_Underflow
2413 : L = word0(rv) & Exp_mask;
2414 : #ifdef IBM
2415 : if (L < Exp_msk1)
2416 : #else
2417 : if (L <= Exp_msk1)
2418 : #endif
2419 : goto undfl;
2420 : L -= Exp_msk1;
2421 : #else
2422 0 : L = (word0(rv) & Exp_mask) - Exp_msk1;
2423 : #endif
2424 0 : word0(rv) = L | Bndry_mask1;
2425 0 : word1(rv) = 0xffffffff;
2426 : #ifdef IBM
2427 : goto cont;
2428 : #else
2429 0 : break;
2430 : #endif
2431 : }
2432 : #ifndef ROUND_BIASED
2433 37 : if (!(word1(rv) & LSB))
2434 21 : break;
2435 : #endif
2436 16 : if (dsign)
2437 16 : value(rv) += ulp(value(rv));
2438 : #ifndef ROUND_BIASED
2439 : else {
2440 0 : value(rv) -= ulp(value(rv));
2441 : #ifndef Sudden_Underflow
2442 0 : if (!value(rv))
2443 0 : goto undfl;
2444 : #endif
2445 : }
2446 : #endif
2447 16 : break;
2448 : }
2449 735 : if ((aadj = ratio(delta, bs)) <= 2.) {
2450 516 : if (dsign)
2451 464 : aadj = aadj1 = 1.;
2452 103 : else if (word1(rv) || word0(rv) & Bndry_mask) {
2453 : #ifndef Sudden_Underflow
2454 52 : if (word1(rv) == Tiny1 && !word0(rv))
2455 1 : goto undfl;
2456 : #endif
2457 51 : aadj = 1.;
2458 51 : aadj1 = -1.;
2459 : }
2460 : else {
2461 : /* special case -- power of FLT_RADIX to be */
2462 : /* rounded down... */
2463 :
2464 0 : if (aadj < 2./FLT_RADIX)
2465 0 : aadj = 1./FLT_RADIX;
2466 : else
2467 0 : aadj *= 0.5;
2468 0 : aadj1 = -aadj;
2469 : }
2470 : }
2471 : else {
2472 219 : aadj *= 0.5;
2473 219 : aadj1 = dsign ? aadj : -aadj;
2474 : #ifdef Check_FLT_ROUNDS
2475 : switch(FLT_ROUNDS) {
2476 : case 2: /* towards +infinity */
2477 : aadj1 -= 0.5;
2478 : break;
2479 : case 0: /* towards 0 */
2480 : case 3: /* towards -infinity */
2481 : aadj1 += 0.5;
2482 : }
2483 : #else
2484 : if (FLT_ROUNDS == 0)
2485 : aadj1 += 0.5;
2486 : #endif
2487 : }
2488 734 : y = word0(rv) & Exp_mask;
2489 :
2490 : /* Check for overflow */
2491 :
2492 734 : if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2493 0 : value(rv0) = value(rv);
2494 0 : word0(rv) -= P*Exp_msk1;
2495 0 : adj = aadj1 * ulp(value(rv));
2496 0 : value(rv) += adj;
2497 0 : if ((word0(rv) & Exp_mask) >=
2498 : Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2499 0 : if (word0(rv0) == Big0 && word1(rv0) == Big1)
2500 0 : goto ovfl;
2501 0 : word0(rv) = Big0;
2502 0 : word1(rv) = Big1;
2503 0 : goto cont;
2504 : }
2505 : else
2506 0 : word0(rv) += P*Exp_msk1;
2507 : }
2508 : else {
2509 : #ifdef Sudden_Underflow
2510 : if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2511 : value(rv0) = value(rv);
2512 : word0(rv) += P*Exp_msk1;
2513 : adj = aadj1 * ulp(value(rv));
2514 : value(rv) += adj;
2515 : #ifdef IBM
2516 : if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2517 : #else
2518 : if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2519 : #endif
2520 : {
2521 : if (word0(rv0) == Tiny0
2522 : && word1(rv0) == Tiny1)
2523 : goto undfl;
2524 : word0(rv) = Tiny0;
2525 : word1(rv) = Tiny1;
2526 : goto cont;
2527 : }
2528 : else
2529 : word0(rv) -= P*Exp_msk1;
2530 : }
2531 : else {
2532 : adj = aadj1 * ulp(value(rv));
2533 : value(rv) += adj;
2534 : }
2535 : #else
2536 : /* Compute adj so that the IEEE rounding rules will
2537 : * correctly round rv + adj in some half-way cases.
2538 : * If rv * ulp(rv) is denormalized (i.e.,
2539 : * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2540 : * trouble from bits lost to denormalization;
2541 : * example: 1.2e-307 .
2542 : */
2543 734 : if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
2544 0 : aadj1 = (double)(int)(aadj + 0.5);
2545 0 : if (!dsign)
2546 0 : aadj1 = -aadj1;
2547 : }
2548 734 : adj = aadj1 * ulp(value(rv));
2549 734 : value(rv) += adj;
2550 : #endif
2551 : }
2552 734 : z = word0(rv) & Exp_mask;
2553 734 : if (y == z) {
2554 : /* Can we stop now? */
2555 711 : L = aadj;
2556 711 : aadj -= L;
2557 : /* The tolerances below are conservative. */
2558 711 : if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2559 711 : if (aadj < .4999999 || aadj > .5000001)
2560 : break;
2561 : }
2562 0 : else if (aadj < .4999999/FLT_RADIX)
2563 0 : break;
2564 : }
2565 23 : cont:
2566 23 : Bfree(bb);
2567 23 : Bfree(bd);
2568 23 : Bfree(bs);
2569 23 : Bfree(delta);
2570 23 : }
2571 1906 : retfree:
2572 1906 : Bfree(bb);
2573 1906 : Bfree(bd);
2574 1906 : Bfree(bs);
2575 1906 : Bfree(bd0);
2576 1906 : Bfree(delta);
2577 232110 : ret:
2578 232110 : if (se)
2579 824 : *se = (char *)s;
2580 232110 : result = sign ? -value(rv) : value(rv);
2581 :
2582 : _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
2583 464934 : while (p5s) {
2584 714 : tmp = p5s;
2585 714 : p5s = p5s->next;
2586 714 : free(tmp);
2587 : }
2588 : _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
2589 :
2590 232110 : return result;
2591 : }
2592 : /* }}} */
2593 :
2594 : ZEND_API double zend_u_strtod(const UChar *nptr, UChar **endptr) /* {{{ */
2595 272077 : {
2596 272077 : const UChar *u = nptr, *nstart;
2597 272077 : UChar c = *u;
2598 272077 : int any = 0;
2599 : ALLOCA_FLAG(use_heap)
2600 :
2601 544331 : while (u_isspace(c)) {
2602 177 : c = *++u;
2603 : }
2604 272077 : nstart = u;
2605 :
2606 272077 : if (c == 0x2D /*'-'*/ || c == 0x2B /*'+'*/) {
2607 29970 : c = *++u;
2608 : }
2609 :
2610 814430 : while (c >= 0x30 /*'0'*/ && c <= 0x39 /*'9'*/) {
2611 270276 : any = 1;
2612 270276 : c = *++u;
2613 : }
2614 :
2615 272077 : if (c == 0x2E /*'.'*/) {
2616 238207 : c = *++u;
2617 2077818 : while (c >= 0x30 /*'0'*/ && c <= 0x39 /*'9'*/) {
2618 1601404 : any = 1;
2619 1601404 : c = *++u;
2620 : }
2621 : }
2622 :
2623 272077 : if ((c == 0x65 /*'e'*/ || c == 0x45 /*'E'*/) && any) {
2624 447 : const UChar *e = u;
2625 447 : int any_exp = 0;
2626 :
2627 447 : c = *++u;
2628 447 : if (c == 0x2D /*'-'*/ || c == 0x2B /*'+'*/) {
2629 21 : c = *++u;
2630 : }
2631 :
2632 1347 : while (c >= 0x30 /*'0'*/ && c <= 0x39 /*'9'*/) {
2633 453 : any_exp = 1;
2634 453 : c = *++u;
2635 : }
2636 :
2637 447 : if (!any_exp) {
2638 9 : u = e;
2639 : }
2640 : }
2641 :
2642 272077 : if (any) {
2643 : char buf[64], *numbuf, *bufpos;
2644 205947 : int length = u - nstart;
2645 : double value;
2646 :
2647 205947 : if (length < sizeof(buf)) {
2648 205947 : numbuf = buf;
2649 : } else {
2650 0 : numbuf = (char *) do_alloca(length + 1, use_heap);
2651 : }
2652 :
2653 205947 : bufpos = numbuf;
2654 :
2655 2487032 : while (nstart < u) {
2656 2075138 : *bufpos++ = (char) *nstart++;
2657 : }
2658 :
2659 205947 : *bufpos = '\0';
2660 205947 : value = zend_strtod(numbuf, NULL);
2661 :
2662 205947 : if (numbuf != buf) {
2663 0 : free_alloca(numbuf, use_heap);
2664 : }
2665 :
2666 205947 : if (endptr != NULL) {
2667 205593 : *endptr = (UChar *)u;
2668 : }
2669 :
2670 205947 : return value;
2671 : }
2672 :
2673 66130 : if (endptr != NULL) {
2674 65673 : *endptr = (UChar *)nptr;
2675 : }
2676 :
2677 66130 : return 0;
2678 : }
2679 : /* }}} */
2680 :
2681 : ZEND_API double zend_hex_strtod(const char *str, char **endptr) /* {{{ */
2682 96 : {
2683 96 : const char *s = str;
2684 : char c;
2685 96 : int any = 0;
2686 96 : double value = 0;
2687 :
2688 96 : if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
2689 0 : s += 2;
2690 : }
2691 :
2692 1043 : while ((c = *s++)) {
2693 1666 : if (c >= '0' && c <= '9') {
2694 719 : c -= '0';
2695 287 : } else if (c >= 'A' && c <= 'F') {
2696 59 : c -= 'A' - 10;
2697 169 : } else if (c >= 'a' && c <= 'f') {
2698 73 : c -= 'a' - 10;
2699 : } else {
2700 : break;
2701 : }
2702 :
2703 851 : any = 1;
2704 851 : value = value * 16 + c;
2705 : }
2706 :
2707 96 : if (endptr != NULL) {
2708 0 : *endptr = (char *)(any ? s - 1 : str);
2709 : }
2710 :
2711 96 : return value;
2712 : }
2713 : /* }}} */
2714 :
2715 : ZEND_API double zend_oct_strtod(const char *str, char **endptr) /* {{{ */
2716 82 : {
2717 82 : const char *s = str;
2718 : char c;
2719 82 : double value = 0;
2720 82 : int any = 0;
2721 :
2722 : /* skip leading zero */
2723 82 : s++;
2724 :
2725 1125 : while ((c = *s++)) {
2726 1043 : if (c < '0' || c > '7') {
2727 : /* break and return the current value if the number is not well-formed
2728 : * that's what Linux strtol() does
2729 : */
2730 : break;
2731 : }
2732 961 : value = value * 8 + c - '0';
2733 961 : any = 1;
2734 : }
2735 :
2736 82 : if (endptr != NULL) {
2737 0 : *endptr = (char *)(any ? s - 1 : str);
2738 : }
2739 :
2740 82 : return value;
2741 : }
2742 : /* }}} */
2743 :
2744 : /*
2745 : * Local variables:
2746 : * tab-width: 4
2747 : * c-basic-offset: 4
2748 : * End:
2749 : * vim600: sw=4 ts=4 fdm=marker
2750 : * vim<600: sw=4 ts=4
2751 : */
|