Back to index

php5  5.3.10
zend_strtod.c
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * The author of this software is David M. Gay.
00004  *
00005  * Copyright (c) 1991 by AT&T.
00006  *
00007  * Permission to use, copy, modify, and distribute this software for any
00008  * purpose without fee is hereby granted, provided that this entire notice
00009  * is included in all copies of any software which is or includes a copy
00010  * or modification of this software and in all copies of the supporting
00011  * documentation for such software.
00012  *
00013  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
00014  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
00015  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
00016  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
00017  *
00018  ***************************************************************/
00019 
00020 /* Please send bug reports to
00021    David M. Gay
00022    AT&T Bell Laboratories, Room 2C-463
00023    600 Mountain Avenue
00024    Murray Hill, NJ 07974-2070
00025    U.S.A.
00026    dmg@research.att.com or research!dmg
00027    */
00028 
00029 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
00030  *
00031  * This strtod returns a nearest machine number to the input decimal
00032  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
00033  * broken by the IEEE round-even rule.  Otherwise ties are broken by
00034  * biased rounding (add half and chop).
00035  *
00036  * Inspired loosely by William D. Clinger's paper "How to Read Floating
00037  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
00038  *
00039  * Modifications:
00040  *
00041  *     1. We only require IEEE, IBM, or VAX double-precision
00042  *            arithmetic (not IEEE double-extended).
00043  *     2. We get by with floating-point arithmetic in a case that
00044  *            Clinger missed -- when we're computing d * 10^n
00045  *            for a small integer d and the integer n is not too
00046  *            much larger than 22 (the maximum integer k for which
00047  *            we can represent 10^k exactly), we may be able to
00048  *            compute (d*10^k) * 10^(e-k) with just one roundoff.
00049  *     3. Rather than a bit-at-a-time adjustment of the binary
00050  *            result in the hard case, we use floating-point
00051  *            arithmetic to determine the adjustment to within
00052  *            one bit; only in really hard cases do we need to
00053  *            compute a second residual.
00054  *     4. Because of 3., we don't need a large table of powers of 10
00055  *            for ten-to-e (just some small tables, e.g. of 10^k
00056  *            for 0 <= k <= 22).
00057  */
00058 
00059 /*
00060  * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
00061  *     significant byte has the lowest address.
00062  * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
00063  *     significant byte has the lowest address.
00064  * #define Long int on machines with 32-bit ints and 64-bit longs.
00065  * #define Sudden_Underflow for IEEE-format machines without gradual
00066  *     underflow (i.e., that flush to zero on underflow).
00067  * #define IBM for IBM mainframe-style floating-point arithmetic.
00068  * #define VAX for VAX-style floating-point arithmetic.
00069  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
00070  * #define No_leftright to omit left-right logic in fast floating-point
00071  *     computation of dtoa.
00072  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
00073  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
00074  *     that use extended-precision instructions to compute rounded
00075  *     products and quotients) with IBM.
00076  * #define ROUND_BIASED for IEEE-format with biased rounding.
00077  * #define Inaccurate_Divide for IEEE-format with correctly rounded
00078  *     products but inaccurate quotients, e.g., for Intel i860.
00079  * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
00080  *     integer arithmetic.  Whether this speeds things up or slows things
00081  *     down depends on the machine and the number being converted.
00082  * #define KR_headers for old-style C function headers.
00083  * #define Bad_float_h if your system lacks a float.h or if it does not
00084  *     define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
00085  *     FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
00086  * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
00087  *     if memory is available and otherwise does something you deem
00088  *     appropriate.  If MALLOC is undefined, malloc will be invoked
00089  *     directly -- and assumed always to succeed.
00090  */
00091 
00092 /* $Id: zend_strtod.c 316591 2011-09-13 07:07:06Z dmitry $ */
00093 
00094 #include <zend_operators.h>
00095 #include <zend_strtod.h>
00096 
00097 #ifdef ZTS
00098 #include <TSRM.h>
00099 #endif
00100 
00101 #include <stddef.h>
00102 #include <stdio.h>
00103 #include <ctype.h>
00104 #include <stdarg.h>
00105 #include <string.h>
00106 #include <stdlib.h>
00107 #include <math.h>
00108 
00109 #ifdef HAVE_LOCALE_H
00110 #include <locale.h>
00111 #endif
00112 
00113 #ifdef HAVE_SYS_TYPES_H
00114 #include <sys/types.h>
00115 #endif
00116 
00117 #if defined(HAVE_INTTYPES_H)
00118 #include <inttypes.h>
00119 #elif defined(HAVE_STDINT_H)
00120 #include <stdint.h>
00121 #endif
00122 
00123 #ifndef HAVE_INT32_T
00124 # if SIZEOF_INT == 4
00125 typedef int int32_t;
00126 # elif SIZEOF_LONG == 4
00127 typedef long int int32_t;
00128 # endif
00129 #endif
00130 
00131 #ifndef HAVE_UINT32_T
00132 # if SIZEOF_INT == 4
00133 typedef unsigned int uint32_t;
00134 # elif SIZEOF_LONG == 4
00135 typedef unsigned long int uint32_t;
00136 # endif
00137 #endif
00138 
00139 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
00140 # if defined(__LITTLE_ENDIAN__)
00141 #  undef WORDS_BIGENDIAN
00142 # else 
00143 #  if defined(__BIG_ENDIAN__)
00144 #   define WORDS_BIGENDIAN
00145 #  endif
00146 # endif
00147 #endif
00148 
00149 #ifdef WORDS_BIGENDIAN
00150 #define IEEE_BIG_ENDIAN
00151 #else
00152 #define IEEE_LITTLE_ENDIAN
00153 #endif
00154 
00155 #if defined(__arm__) && !defined(__VFP_FP__)
00156 /*
00157  *  * Although the CPU is little endian the FP has different
00158  *   * byte and word endianness. The byte order is still little endian
00159  *    * but the word order is big endian.
00160  *     */
00161 #define IEEE_BIG_ENDIAN
00162 #undef IEEE_LITTLE_ENDIAN
00163 #endif
00164 
00165 #ifdef __vax__
00166 #define VAX
00167 #undef IEEE_LITTLE_ENDIAN
00168 #endif
00169 
00170 #if defined(_MSC_VER)
00171 #define int32_t __int32
00172 #define uint32_t unsigned __int32
00173 #define IEEE_LITTLE_ENDIAN
00174 #endif
00175 
00176 #define Long    int32_t
00177 #define ULong   uint32_t
00178 
00179 #ifdef __cplusplus
00180 #include "malloc.h"
00181 #include "memory.h"
00182 #else
00183 #ifndef KR_headers
00184 #include "stdlib.h"
00185 #include "string.h"
00186 #include "locale.h"
00187 #else
00188 #include "malloc.h"
00189 #include "memory.h"
00190 #endif
00191 #endif
00192 
00193 #ifdef MALLOC
00194 #ifdef KR_headers
00195 extern char *MALLOC();
00196 #else
00197 extern void *MALLOC(size_t);
00198 #endif
00199 #else
00200 #define MALLOC malloc
00201 #endif
00202 
00203 #include "ctype.h"
00204 #include "errno.h"
00205 
00206 #ifdef Bad_float_h
00207 #ifdef IEEE_BIG_ENDIAN
00208 #define IEEE_ARITHMETIC
00209 #endif
00210 #ifdef IEEE_LITTLE_ENDIAN
00211 #define IEEE_ARITHMETIC
00212 #endif
00213 
00214 #ifdef IEEE_ARITHMETIC
00215 #define DBL_DIG 15
00216 #define DBL_MAX_10_EXP 308
00217 #define DBL_MAX_EXP 1024
00218 #define FLT_RADIX 2
00219 #define FLT_ROUNDS 1
00220 #define DBL_MAX 1.7976931348623157e+308
00221 #endif
00222 
00223 #ifdef IBM
00224 #define DBL_DIG 16
00225 #define DBL_MAX_10_EXP 75
00226 #define DBL_MAX_EXP 63
00227 #define FLT_RADIX 16
00228 #define FLT_ROUNDS 0
00229 #define DBL_MAX 7.2370055773322621e+75
00230 #endif
00231 
00232 #ifdef VAX
00233 #define DBL_DIG 16
00234 #define DBL_MAX_10_EXP 38
00235 #define DBL_MAX_EXP 127
00236 #define FLT_RADIX 2
00237 #define FLT_ROUNDS 1
00238 #define DBL_MAX 1.7014118346046923e+38
00239 #endif
00240 
00241 
00242 #ifndef LONG_MAX
00243 #define LONG_MAX 2147483647
00244 #endif
00245 #else
00246 #include "float.h"
00247 #endif
00248 #ifndef __MATH_H__
00249 #include "math.h"
00250 #endif
00251 
00252 BEGIN_EXTERN_C()
00253 
00254 #ifndef CONST
00255 #ifdef KR_headers
00256 #define CONST /* blank */
00257 #else
00258 #define CONST const
00259 #endif
00260 #endif
00261 
00262 #ifdef Unsigned_Shifts
00263 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
00264 #else
00265 #define Sign_Extend(a,b) /*no-op*/
00266 #endif
00267 
00268 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
00269                   defined(IBM) != 1
00270        Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
00271        IBM should be defined.
00272 #endif
00273 
00274        typedef union {
00275                   double d;
00276                          ULong ul[2];
00277        } _double;
00278 #define value(x) ((x).d)
00279 #ifdef IEEE_LITTLE_ENDIAN
00280 #define word0(x) ((x).ul[1])
00281 #define word1(x) ((x).ul[0])
00282 #else
00283 #define word0(x) ((x).ul[0])
00284 #define word1(x) ((x).ul[1])
00285 #endif
00286 
00287 /* The following definition of Storeinc is appropriate for MIPS processors.
00288  * An alternative that might be better on some machines is
00289  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
00290  */
00291 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
00292 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
00293               ((unsigned short *)a)[0] = (unsigned short)c, a++)
00294 #else
00295 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
00296               ((unsigned short *)a)[1] = (unsigned short)c, a++)
00297 #endif
00298 
00299 /* #define P DBL_MANT_DIG */
00300 /* Ten_pmax = floor(P*log(2)/log(5)) */
00301 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
00302 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
00303 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
00304 
00305 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
00306 #define Exp_shift  20
00307 #define Exp_shift1 20
00308 #define Exp_msk1    0x100000
00309 #define Exp_msk11   0x100000
00310 #define Exp_mask  0x7ff00000
00311 #define P 53
00312 #define Bias 1023
00313 #define IEEE_Arith
00314 #define Emin (-1022)
00315 #define Exp_1  0x3ff00000
00316 #define Exp_11 0x3ff00000
00317 #define Ebits 11
00318 #define Frac_mask  0xfffff
00319 #define Frac_mask1 0xfffff
00320 #define Ten_pmax 22
00321 #define Bletch 0x10
00322 #define Bndry_mask  0xfffff
00323 #define Bndry_mask1 0xfffff
00324 #define LSB 1
00325 #define Sign_bit 0x80000000
00326 #define Log2P 1
00327 #define Tiny0 0
00328 #define Tiny1 1
00329 #define Quick_max 14
00330 #define Int_max 14
00331 #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
00332 #else
00333 #undef  Sudden_Underflow
00334 #define Sudden_Underflow
00335 #ifdef IBM
00336 #define Exp_shift  24
00337 #define Exp_shift1 24
00338 #define Exp_msk1   0x1000000
00339 #define Exp_msk11  0x1000000
00340 #define Exp_mask  0x7f000000
00341 #define P 14
00342 #define Bias 65
00343 #define Exp_1  0x41000000
00344 #define Exp_11 0x41000000
00345 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
00346 #define Frac_mask  0xffffff
00347 #define Frac_mask1 0xffffff
00348 #define Bletch 4
00349 #define Ten_pmax 22
00350 #define Bndry_mask  0xefffff
00351 #define Bndry_mask1 0xffffff
00352 #define LSB 1
00353 #define Sign_bit 0x80000000
00354 #define Log2P 4
00355 #define Tiny0 0x100000
00356 #define Tiny1 0
00357 #define Quick_max 14
00358 #define Int_max 15
00359 #else /* VAX */
00360 #define Exp_shift  23
00361 #define Exp_shift1 7
00362 #define Exp_msk1    0x80
00363 #define Exp_msk11   0x800000
00364 #define Exp_mask  0x7f80
00365 #define P 56
00366 #define Bias 129
00367 #define Exp_1  0x40800000
00368 #define Exp_11 0x4080
00369 #define Ebits 8
00370 #define Frac_mask  0x7fffff
00371 #define Frac_mask1 0xffff007f
00372 #define Ten_pmax 24
00373 #define Bletch 2
00374 #define Bndry_mask  0xffff007f
00375 #define Bndry_mask1 0xffff007f
00376 #define LSB 0x10000
00377 #define Sign_bit 0x8000
00378 #define Log2P 1
00379 #define Tiny0 0x80
00380 #define Tiny1 0
00381 #define Quick_max 15
00382 #define Int_max 15
00383 #endif
00384 #endif
00385 
00386 #ifndef IEEE_Arith
00387 #define ROUND_BIASED
00388 #endif
00389 
00390 #ifdef RND_PRODQUOT
00391 #define rounded_product(a,b) a = rnd_prod(a, b)
00392 #define rounded_quotient(a,b) a = rnd_quot(a, b)
00393 #ifdef KR_headers
00394 extern double rnd_prod(), rnd_quot();
00395 #else
00396 extern double rnd_prod(double, double), rnd_quot(double, double);
00397 #endif
00398 #else
00399 #define rounded_product(a,b) a *= b
00400 #define rounded_quotient(a,b) a /= b
00401 #endif
00402 
00403 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
00404 #define Big1 0xffffffff
00405 
00406 #ifndef Just_16
00407 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
00408  *  * This makes some inner loops simpler and sometimes saves work
00409  *   * during multiplications, but it often seems to make things slightly
00410  *    * slower.  Hence the default is now to store 32 bits per Long.
00411  *     */
00412 #ifndef Pack_32
00413 #define Pack_32
00414 #endif
00415 #endif
00416 
00417 #define Kmax 15
00418 
00419 struct Bigint {
00420        struct Bigint *next;
00421        int k, maxwds, sign, wds;
00422        ULong x[1];
00423 };
00424 
00425 typedef struct Bigint Bigint;
00426 
00427 /* static variables, multithreading fun! */
00428 static Bigint *freelist[Kmax+1];
00429 static Bigint *p5s;
00430 
00431 static void destroy_freelist(void);
00432 
00433 #ifdef ZTS
00434 
00435 static MUTEX_T dtoa_mutex;
00436 static MUTEX_T pow5mult_mutex; 
00437 
00438 #define _THREAD_PRIVATE_MUTEX_LOCK(x) tsrm_mutex_lock(x);
00439 #define _THREAD_PRIVATE_MUTEX_UNLOCK(x) tsrm_mutex_unlock(x);
00440 
00441 #else 
00442 
00443 #define _THREAD_PRIVATE_MUTEX_LOCK(x)
00444 #define _THREAD_PRIVATE_MUTEX_UNLOCK(x)
00445 
00446 #endif /* ZTS */
00447 
00448 #ifdef DEBUG
00449 static void Bug(const char *message) {
00450        fprintf(stderr, "%s\n", message);
00451 }
00452 #endif
00453 
00454 ZEND_API int zend_startup_strtod(void) /* {{{ */
00455 {
00456 #ifdef ZTS
00457        dtoa_mutex = tsrm_mutex_alloc();
00458        pow5mult_mutex = tsrm_mutex_alloc();
00459 #endif
00460        return 1;
00461 }
00462 /* }}} */
00463 ZEND_API int zend_shutdown_strtod(void) /* {{{ */
00464 {
00465        destroy_freelist();
00466 #ifdef ZTS
00467        tsrm_mutex_free(dtoa_mutex);
00468        dtoa_mutex = NULL;
00469 
00470        tsrm_mutex_free(pow5mult_mutex);
00471        pow5mult_mutex = NULL;
00472 #endif
00473        return 1;
00474 }
00475 /* }}} */
00476 
00477 static Bigint * Balloc(int k)
00478 {
00479        int x;
00480        Bigint *rv;
00481 
00482        if (k > Kmax) {
00483               zend_error(E_ERROR, "Balloc() allocation exceeds list boundary");
00484        }
00485 
00486        _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
00487        if ((rv = freelist[k])) {
00488               freelist[k] = rv->next;
00489        } else {
00490               x = 1 << k;
00491               rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
00492               if (!rv) {
00493                      _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
00494                      zend_error(E_ERROR, "Balloc() failed to allocate memory");
00495               }
00496               rv->k = k;
00497               rv->maxwds = x;
00498        }
00499        _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
00500        rv->sign = rv->wds = 0;
00501        return rv;
00502 }
00503 
00504 static void Bfree(Bigint *v)
00505 {
00506        if (v) {
00507               _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
00508               v->next = freelist[v->k];
00509               freelist[v->k] = v;
00510               _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
00511        }
00512 }
00513 
00514 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
00515               y->wds*sizeof(Long) + 2*sizeof(int))
00516 
00517 /* return value is only used as a simple string, so mis-aligned parts
00518  * inside the Bigint are not at risk on strict align architectures
00519  */
00520 static char * rv_alloc(int i) {
00521        int j, k, *r;
00522 
00523        j = sizeof(ULong);
00524        for(k = 0;
00525                      sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
00526                      j <<= 1) {
00527               k++;
00528        }
00529        r = (int*)Balloc(k);
00530        *r = k;
00531        return (char *)(r+1);
00532 }
00533 
00534 
00535 static char * nrv_alloc(char *s, char **rve, int n)
00536 {
00537        char *rv, *t;
00538 
00539        t = rv = rv_alloc(n);
00540        while((*t = *s++) !=0) {
00541               t++;
00542        }
00543        if (rve) {
00544               *rve = t;
00545        }
00546        return rv;
00547 }
00548 
00549 static Bigint * multadd(Bigint *b, int m, int a) /* multiply by m and add a */
00550 {
00551        int i, wds;
00552        ULong *x, y;
00553 #ifdef Pack_32
00554        ULong xi, z;
00555 #endif
00556        Bigint *b1;
00557 
00558        wds = b->wds;
00559        x = b->x;
00560        i = 0;
00561        do {
00562 #ifdef Pack_32
00563               xi = *x;
00564               y = (xi & 0xffff) * m + a;
00565               z = (xi >> 16) * m + (y >> 16);
00566               a = (int)(z >> 16);
00567               *x++ = (z << 16) + (y & 0xffff);
00568 #else
00569               y = *x * m + a;
00570               a = (int)(y >> 16);
00571               *x++ = y & 0xffff;
00572 #endif
00573        }
00574        while(++i < wds);
00575        if (a) {
00576               if (wds >= b->maxwds) {
00577                      b1 = Balloc(b->k+1);
00578                      Bcopy(b1, b);
00579                      Bfree(b);
00580                      b = b1;
00581               }
00582               b->x[wds++] = a;
00583               b->wds = wds;
00584        }
00585        return b;
00586 }
00587 
00588 static int hi0bits(ULong x)
00589 {
00590        int k = 0;
00591 
00592        if (!(x & 0xffff0000)) {
00593               k = 16;
00594               x <<= 16;
00595        }
00596        if (!(x & 0xff000000)) {
00597               k += 8;
00598               x <<= 8;
00599        }
00600        if (!(x & 0xf0000000)) {
00601               k += 4;
00602               x <<= 4;
00603        }
00604        if (!(x & 0xc0000000)) {
00605               k += 2;
00606               x <<= 2;
00607        }
00608        if (!(x & 0x80000000)) {
00609               k++;
00610               if (!(x & 0x40000000)) {
00611                      return 32;
00612               }
00613        }
00614        return k;
00615 }
00616 
00617 static int lo0bits(ULong *y)
00618 {
00619        int k;
00620        ULong x = *y;
00621 
00622        if (x & 7) {
00623               if (x & 1) {
00624                      return 0;
00625               }
00626               if (x & 2) {
00627                      *y = x >> 1;
00628                      return 1;
00629               }
00630               *y = x >> 2;
00631               return 2;
00632        }
00633        k = 0;
00634        if (!(x & 0xffff)) {
00635               k = 16;
00636               x >>= 16;
00637        }
00638        if (!(x & 0xff)) {
00639               k += 8;
00640               x >>= 8;
00641        }
00642        if (!(x & 0xf)) {
00643               k += 4;
00644               x >>= 4;
00645        }
00646        if (!(x & 0x3)) {
00647               k += 2;
00648               x >>= 2;
00649        }
00650        if (!(x & 1)) {
00651               k++;
00652               x >>= 1;
00653               if (!(x & 1)) {
00654                      return 32;
00655               }
00656        }
00657        *y = x;
00658        return k;
00659 }
00660 
00661 static Bigint * i2b(int i)
00662 {
00663        Bigint *b;
00664 
00665        b = Balloc(1);
00666        b->x[0] = i;
00667        b->wds = 1;
00668        return b;
00669 }
00670 
00671 static Bigint * mult(Bigint *a, Bigint *b)
00672 {
00673        Bigint *c;
00674        int k, wa, wb, wc;
00675        ULong carry, y, z;
00676        ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
00677 #ifdef Pack_32
00678        ULong z2;
00679 #endif
00680 
00681        if (a->wds < b->wds) {
00682               c = a;
00683               a = b;
00684               b = c;
00685        }
00686        k = a->k;
00687        wa = a->wds;
00688        wb = b->wds;
00689        wc = wa + wb;
00690        if (wc > a->maxwds) {
00691               k++;
00692        }
00693        c = Balloc(k);
00694        for(x = c->x, xa = x + wc; x < xa; x++) {
00695               *x = 0;
00696        }
00697        xa = a->x;
00698        xae = xa + wa;
00699        xb = b->x;
00700        xbe = xb + wb;
00701        xc0 = c->x;
00702 #ifdef Pack_32
00703        for(; xb < xbe; xb++, xc0++) {
00704               if ((y = *xb & 0xffff)) {
00705                      x = xa;
00706                      xc = xc0;
00707                      carry = 0;
00708                      do {
00709                             z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
00710                             carry = z >> 16;
00711                             z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
00712                             carry = z2 >> 16;
00713                             Storeinc(xc, z2, z);
00714                      }
00715                      while(x < xae);
00716                      *xc = carry;
00717               }
00718               if ((y = *xb >> 16)) {
00719                      x = xa;
00720                      xc = xc0;
00721                      carry = 0;
00722                      z2 = *xc;
00723                      do {
00724                             z = (*x & 0xffff) * y + (*xc >> 16) + carry;
00725                             carry = z >> 16;
00726                             Storeinc(xc, z, z2);
00727                             z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
00728                             carry = z2 >> 16;
00729                      }
00730                      while(x < xae);
00731                      *xc = z2;
00732               }
00733        }
00734 #else
00735        for(; xb < xbe; xc0++) {
00736               if (y = *xb++) {
00737                      x = xa;
00738                      xc = xc0;
00739                      carry = 0;
00740                      do {
00741                             z = *x++ * y + *xc + carry;
00742                             carry = z >> 16;
00743                             *xc++ = z & 0xffff;
00744                      }
00745                      while(x < xae);
00746                      *xc = carry;
00747               }
00748        }
00749 #endif
00750        for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
00751        c->wds = wc;
00752        return c;
00753 }
00754 
00755 static Bigint * s2b (CONST char *s, int nd0, int nd, ULong y9)
00756 {
00757        Bigint *b;
00758        int i, k;
00759        Long x, y;
00760 
00761        x = (nd + 8) / 9;
00762        for(k = 0, y = 1; x > y; y <<= 1, k++) ;
00763 #ifdef Pack_32
00764        b = Balloc(k);
00765        b->x[0] = y9;
00766        b->wds = 1;
00767 #else
00768        b = Balloc(k+1);
00769        b->x[0] = y9 & 0xffff;
00770        b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
00771 #endif
00772 
00773        i = 9;
00774        if (9 < nd0) {
00775               s += 9;
00776               do b = multadd(b, 10, *s++ - '0');
00777               while(++i < nd0);
00778               s++;
00779        } else {
00780               s += 10;
00781        }
00782        for(; i < nd; i++) {
00783               b = multadd(b, 10, *s++ - '0');
00784        }
00785        return b;
00786 }
00787 
00788 static Bigint * pow5mult(Bigint *b, int k)
00789 {
00790        Bigint *b1, *p5, *p51;
00791        int i;
00792        static int p05[3] = { 5, 25, 125 };
00793 
00794        _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
00795        if ((i = k & 3)) {
00796               b = multadd(b, p05[i-1], 0);
00797        }
00798 
00799        if (!(k >>= 2)) {
00800               _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
00801               return b;
00802        }
00803        if (!(p5 = p5s)) {
00804               /* first time */
00805               p5 = p5s = i2b(625);
00806               p5->next = 0;
00807        }
00808        for(;;) {
00809               if (k & 1) {
00810                      b1 = mult(b, p5);
00811                      Bfree(b);
00812                      b = b1;
00813               }
00814               if (!(k >>= 1)) {
00815                      break;
00816               }
00817               if (!(p51 = p5->next)) {
00818                      if (!(p51 = p5->next)) {
00819                             p51 = p5->next = mult(p5,p5);
00820                             p51->next = 0;
00821                      }
00822               }
00823               p5 = p51;
00824        }
00825        _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
00826        return b;
00827 }
00828 
00829 
00830 static Bigint *lshift(Bigint *b, int k)
00831 {
00832        int i, k1, n, n1;
00833        Bigint *b1;
00834        ULong *x, *x1, *xe, z;
00835 
00836 #ifdef Pack_32
00837        n = k >> 5;
00838 #else
00839        n = k >> 4;
00840 #endif
00841        k1 = b->k;
00842        n1 = n + b->wds + 1;
00843        for(i = b->maxwds; n1 > i; i <<= 1) {
00844               k1++;
00845        }
00846        b1 = Balloc(k1);
00847        x1 = b1->x;
00848        for(i = 0; i < n; i++) {
00849               *x1++ = 0;
00850        }
00851        x = b->x;
00852        xe = x + b->wds;
00853 #ifdef Pack_32
00854        if (k &= 0x1f) {
00855               k1 = 32 - k;
00856               z = 0;
00857               do {
00858                      *x1++ = *x << k | z;
00859                      z = *x++ >> k1;
00860               }
00861               while(x < xe);
00862               if ((*x1 = z)) {
00863                      ++n1;
00864               }
00865        }
00866 #else
00867        if (k &= 0xf) {
00868               k1 = 16 - k;
00869               z = 0;
00870               do {
00871                      *x1++ = *x << k  & 0xffff | z;
00872                      z = *x++ >> k1;
00873               }
00874               while(x < xe);
00875               if (*x1 = z) {
00876                      ++n1;
00877               }
00878        }
00879 #endif
00880        else do
00881               *x1++ = *x++;
00882        while(x < xe);
00883        b1->wds = n1 - 1;
00884        Bfree(b);
00885        return b1;
00886 }
00887 
00888 static int cmp(Bigint *a, Bigint *b)
00889 {
00890        ULong *xa, *xa0, *xb, *xb0;
00891        int i, j;
00892 
00893        i = a->wds;
00894        j = b->wds;
00895 #ifdef DEBUG
00896        if (i > 1 && !a->x[i-1])
00897               Bug("cmp called with a->x[a->wds-1] == 0");
00898        if (j > 1 && !b->x[j-1])
00899               Bug("cmp called with b->x[b->wds-1] == 0");
00900 #endif
00901        if (i -= j)
00902               return i;
00903        xa0 = a->x;
00904        xa = xa0 + j;
00905        xb0 = b->x;
00906        xb = xb0 + j;
00907        for(;;) {
00908               if (*--xa != *--xb)
00909                      return *xa < *xb ? -1 : 1;
00910               if (xa <= xa0)
00911                      break;
00912        }
00913        return 0;
00914 }
00915 
00916 
00917 static Bigint * diff(Bigint *a, Bigint *b)
00918 {
00919        Bigint *c;
00920        int i, wa, wb;
00921        Long borrow, y; /* We need signed shifts here. */
00922        ULong *xa, *xae, *xb, *xbe, *xc;
00923 #ifdef Pack_32
00924        Long z;
00925 #endif
00926 
00927        i = cmp(a,b);
00928        if (!i) {
00929               c = Balloc(0);
00930               c->wds = 1;
00931               c->x[0] = 0;
00932               return c;
00933        }
00934        if (i < 0) {
00935               c = a;
00936               a = b;
00937               b = c;
00938               i = 1;
00939        } else {
00940               i = 0;
00941        }
00942        c = Balloc(a->k);
00943        c->sign = i;
00944        wa = a->wds;
00945        xa = a->x;
00946        xae = xa + wa;
00947        wb = b->wds;
00948        xb = b->x;
00949        xbe = xb + wb;
00950        xc = c->x;
00951        borrow = 0;
00952 #ifdef Pack_32
00953        do {
00954               y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
00955               borrow = y >> 16;
00956               Sign_Extend(borrow, y);
00957               z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
00958               borrow = z >> 16;
00959               Sign_Extend(borrow, z);
00960               Storeinc(xc, z, y);
00961        } while(xb < xbe);
00962        while(xa < xae) {
00963               y = (*xa & 0xffff) + borrow;
00964               borrow = y >> 16;
00965               Sign_Extend(borrow, y);
00966               z = (*xa++ >> 16) + borrow;
00967               borrow = z >> 16;
00968               Sign_Extend(borrow, z);
00969               Storeinc(xc, z, y);
00970        }
00971 #else
00972        do {
00973               y = *xa++ - *xb++ + borrow;
00974               borrow = y >> 16;
00975               Sign_Extend(borrow, y);
00976               *xc++ = y & 0xffff;
00977        } while(xb < xbe);
00978        while(xa < xae) {
00979               y = *xa++ + borrow;
00980               borrow = y >> 16;
00981               Sign_Extend(borrow, y);
00982               *xc++ = y & 0xffff;
00983        }
00984 #endif
00985        while(!*--xc) {
00986               wa--;
00987        }
00988        c->wds = wa;
00989        return c;
00990 }
00991 
00992 static double ulp (double _x)
00993 {
00994        volatile _double x;
00995        register Long L;
00996        volatile _double a;
00997 
00998        value(x) = _x;
00999        L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
01000 #ifndef Sudden_Underflow
01001        if (L > 0) {
01002 #endif
01003 #ifdef IBM
01004               L |= Exp_msk1 >> 4;
01005 #endif
01006               word0(a) = L;
01007               word1(a) = 0;
01008 #ifndef Sudden_Underflow
01009        }
01010        else {
01011               L = -L >> Exp_shift;
01012               if (L < Exp_shift) {
01013                      word0(a) = 0x80000 >> L;
01014                      word1(a) = 0;
01015               }
01016               else {
01017                      word0(a) = 0;
01018                      L -= Exp_shift;
01019                      word1(a) = L >= 31 ? 1 : 1 << (31 - L);
01020               }
01021        }
01022 #endif
01023        return value(a);
01024 }
01025 
01026 static double
01027 b2d
01028 #ifdef KR_headers
01029 (a, e) Bigint *a; int *e;
01030 #else
01031 (Bigint *a, int *e)
01032 #endif
01033 {
01034        ULong *xa, *xa0, w, y, z;
01035        int k;
01036        volatile _double d;
01037 #ifdef VAX
01038        ULong d0, d1;
01039 #else
01040 #define d0 word0(d)
01041 #define d1 word1(d)
01042 #endif
01043 
01044        xa0 = a->x;
01045        xa = xa0 + a->wds;
01046        y = *--xa;
01047 #ifdef DEBUG
01048        if (!y) Bug("zero y in b2d");
01049 #endif
01050        k = hi0bits(y);
01051        *e = 32 - k;
01052 #ifdef Pack_32
01053        if (k < Ebits) {
01054               d0 = Exp_1 | y >> (Ebits - k);
01055               w = xa > xa0 ? *--xa : 0;
01056               d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
01057               goto ret_d;
01058        }
01059        z = xa > xa0 ? *--xa : 0;
01060        if (k -= Ebits) {
01061               d0 = Exp_1 | y << k | z >> (32 - k);
01062               y = xa > xa0 ? *--xa : 0;
01063               d1 = z << k | y >> (32 - k);
01064        }
01065        else {
01066               d0 = Exp_1 | y;
01067               d1 = z;
01068        }
01069 #else
01070        if (k < Ebits + 16) {
01071               z = xa > xa0 ? *--xa : 0;
01072               d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
01073               w = xa > xa0 ? *--xa : 0;
01074               y = xa > xa0 ? *--xa : 0;
01075               d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
01076               goto ret_d;
01077        }
01078        z = xa > xa0 ? *--xa : 0;
01079        w = xa > xa0 ? *--xa : 0;
01080        k -= Ebits + 16;
01081        d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
01082        y = xa > xa0 ? *--xa : 0;
01083        d1 = w << k + 16 | y << k;
01084 #endif
01085 ret_d:
01086 #ifdef VAX
01087        word0(d) = d0 >> 16 | d0 << 16;
01088        word1(d) = d1 >> 16 | d1 << 16;
01089 #else
01090 #undef d0
01091 #undef d1
01092 #endif
01093        return value(d);
01094 }
01095 
01096 
01097 static Bigint * d2b(double _d, int *e, int *bits)
01098 {
01099        Bigint *b;
01100        int de, i, k;
01101        ULong *x, y, z;
01102        volatile _double d;
01103 #ifdef VAX
01104        ULong d0, d1;
01105 #endif
01106 
01107        value(d) = _d;
01108 #ifdef VAX
01109        d0 = word0(d) >> 16 | word0(d) << 16;
01110        d1 = word1(d) >> 16 | word1(d) << 16;
01111 #else
01112 #define d0 word0(d)
01113 #define d1 word1(d)
01114 #endif
01115 
01116 #ifdef Pack_32
01117        b = Balloc(1);
01118 #else
01119        b = Balloc(2);
01120 #endif
01121        x = b->x;
01122 
01123        z = d0 & Frac_mask;
01124        d0 &= 0x7fffffff;   /* clear sign bit, which we ignore */
01125 #ifdef Sudden_Underflow
01126        de = (int)(d0 >> Exp_shift);
01127 #ifndef IBM
01128        z |= Exp_msk11;
01129 #endif
01130 #else
01131        if ((de = (int)(d0 >> Exp_shift)))
01132               z |= Exp_msk1;
01133 #endif
01134 #ifdef Pack_32
01135        if ((y = d1)) {
01136               if ((k = lo0bits(&y))) {
01137                      x[0] = y | (z << (32 - k));
01138                      z >>= k;
01139               } else {
01140                      x[0] = y;
01141               }
01142               i = b->wds = (x[1] = z) ? 2 : 1;
01143        } else {
01144 #ifdef DEBUG
01145               if (!z)
01146                      Bug("Zero passed to d2b");
01147 #endif
01148               k = lo0bits(&z);
01149               x[0] = z;
01150               i = b->wds = 1;
01151               k += 32;
01152        }
01153 #else
01154        if (y = d1) {
01155               if (k = lo0bits(&y)) {
01156                      if (k >= 16) {
01157                             x[0] = y | z << 32 - k & 0xffff;
01158                             x[1] = z >> k - 16 & 0xffff;
01159                             x[2] = z >> k;
01160                             i = 2;
01161                      } else {
01162                             x[0] = y & 0xffff;
01163                             x[1] = y >> 16 | z << 16 - k & 0xffff;
01164                             x[2] = z >> k & 0xffff;
01165                             x[3] = z >> k+16;
01166                             i = 3;
01167                      }
01168               } else {
01169                      x[0] = y & 0xffff;
01170                      x[1] = y >> 16;
01171                      x[2] = z & 0xffff;
01172                      x[3] = z >> 16;
01173                      i = 3;
01174               }
01175        } else {
01176 #ifdef DEBUG
01177               if (!z)
01178                      Bug("Zero passed to d2b");
01179 #endif
01180               k = lo0bits(&z);
01181               if (k >= 16) {
01182                      x[0] = z;
01183                      i = 0;
01184               } else {
01185                      x[0] = z & 0xffff;
01186                      x[1] = z >> 16;
01187                      i = 1;
01188               }
01189               k += 32;
01190        }
01191        while(!x[i])
01192               --i;
01193        b->wds = i + 1;
01194 #endif
01195 #ifndef Sudden_Underflow
01196        if (de) {
01197 #endif
01198 #ifdef IBM
01199               *e = (de - Bias - (P-1) << 2) + k;
01200               *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
01201 #else
01202               *e = de - Bias - (P-1) + k;
01203               *bits = P - k;
01204 #endif
01205 #ifndef Sudden_Underflow
01206        } else {
01207               *e = de - Bias - (P-1) + 1 + k;
01208 #ifdef Pack_32
01209               *bits = 32*i - hi0bits(x[i-1]);
01210 #else
01211               *bits = (i+2)*16 - hi0bits(x[i]);
01212 #endif
01213        }
01214 #endif
01215        return b;
01216 }
01217 #undef d0
01218 #undef d1
01219 
01220 
01221 static double ratio (Bigint *a, Bigint *b)
01222 {
01223        volatile _double da, db;
01224        int k, ka, kb;
01225 
01226        value(da) = b2d(a, &ka);
01227        value(db) = b2d(b, &kb);
01228 #ifdef Pack_32
01229        k = ka - kb + 32*(a->wds - b->wds);
01230 #else
01231        k = ka - kb + 16*(a->wds - b->wds);
01232 #endif
01233 #ifdef IBM
01234        if (k > 0) {
01235               word0(da) += (k >> 2)*Exp_msk1;
01236               if (k &= 3) {
01237                      da *= 1 << k;
01238               }
01239        } else {
01240               k = -k;
01241               word0(db) += (k >> 2)*Exp_msk1;
01242               if (k &= 3)
01243                      db *= 1 << k;
01244        }
01245 #else
01246        if (k > 0) {
01247               word0(da) += k*Exp_msk1;
01248        } else {
01249               k = -k;
01250               word0(db) += k*Exp_msk1;
01251        }
01252 #endif
01253        return value(da) / value(db);
01254 }
01255 
01256 static CONST double
01257 tens[] = {
01258        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
01259        1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
01260        1e20, 1e21, 1e22
01261 #ifdef VAX
01262               , 1e23, 1e24
01263 #endif
01264 };
01265 
01266 #ifdef IEEE_Arith
01267 static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
01268 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
01269 #define n_bigtens 5
01270 #else
01271 #ifdef IBM
01272 static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
01273 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
01274 #define n_bigtens 3
01275 #else
01276 static CONST double bigtens[] = { 1e16, 1e32 };
01277 static CONST double tinytens[] = { 1e-16, 1e-32 };
01278 #define n_bigtens 2
01279 #endif
01280 #endif
01281 
01282 
01283 static int quorem(Bigint *b, Bigint *S)
01284 {
01285        int n;
01286        Long borrow, y;
01287        ULong carry, q, ys;
01288        ULong *bx, *bxe, *sx, *sxe;
01289 #ifdef Pack_32
01290        Long z;
01291        ULong si, zs;
01292 #endif
01293 
01294        n = S->wds;
01295 #ifdef DEBUG
01296        /*debug*/ if (b->wds > n)
01297               /*debug*/   Bug("oversize b in quorem");
01298 #endif
01299        if (b->wds < n)
01300               return 0;
01301        sx = S->x;
01302        sxe = sx + --n;
01303        bx = b->x;
01304        bxe = bx + n;
01305        q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
01306 #ifdef DEBUG
01307        /*debug*/ if (q > 9)
01308               /*debug*/   Bug("oversized quotient in quorem");
01309 #endif
01310        if (q) {
01311               borrow = 0;
01312               carry = 0;
01313               do {
01314 #ifdef Pack_32
01315                      si = *sx++;
01316                      ys = (si & 0xffff) * q + carry;
01317                      zs = (si >> 16) * q + (ys >> 16);
01318                      carry = zs >> 16;
01319                      y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
01320                      borrow = y >> 16;
01321                      Sign_Extend(borrow, y);
01322                      z = (*bx >> 16) - (zs & 0xffff) + borrow;
01323                      borrow = z >> 16;
01324                      Sign_Extend(borrow, z);
01325                      Storeinc(bx, z, y);
01326 #else
01327                      ys = *sx++ * q + carry;
01328                      carry = ys >> 16;
01329                      y = *bx - (ys & 0xffff) + borrow;
01330                      borrow = y >> 16;
01331                      Sign_Extend(borrow, y);
01332                      *bx++ = y & 0xffff;
01333 #endif
01334               }
01335               while(sx <= sxe);
01336               if (!*bxe) {
01337                      bx = b->x;
01338                      while(--bxe > bx && !*bxe)
01339                             --n;
01340                      b->wds = n;
01341               }
01342        }
01343        if (cmp(b, S) >= 0) {
01344               q++;
01345               borrow = 0;
01346               carry = 0;
01347               bx = b->x;
01348               sx = S->x;
01349               do {
01350 #ifdef Pack_32
01351                      si = *sx++;
01352                      ys = (si & 0xffff) + carry;
01353                      zs = (si >> 16) + (ys >> 16);
01354                      carry = zs >> 16;
01355                      y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
01356                      borrow = y >> 16;
01357                      Sign_Extend(borrow, y);
01358                      z = (*bx >> 16) - (zs & 0xffff) + borrow;
01359                      borrow = z >> 16;
01360                      Sign_Extend(borrow, z);
01361                      Storeinc(bx, z, y);
01362 #else
01363                      ys = *sx++ + carry;
01364                      carry = ys >> 16;
01365                      y = *bx - (ys & 0xffff) + borrow;
01366                      borrow = y >> 16;
01367                      Sign_Extend(borrow, y);
01368                      *bx++ = y & 0xffff;
01369 #endif
01370               }
01371               while(sx <= sxe);
01372               bx = b->x;
01373               bxe = bx + n;
01374               if (!*bxe) {
01375                      while(--bxe > bx && !*bxe)
01376                             --n;
01377                      b->wds = n;
01378               }
01379        }
01380        return q;
01381 }
01382 
01383 static void destroy_freelist(void)
01384 {
01385        int i;
01386        Bigint *tmp;
01387 
01388        _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
01389        for (i = 0; i <= Kmax; i++) {
01390               Bigint **listp = &freelist[i];
01391               while ((tmp = *listp) != NULL) {
01392                      *listp = tmp->next;
01393                      free(tmp);
01394               }
01395               freelist[i] = NULL;
01396        }
01397        _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
01398        
01399 }
01400 
01401 
01402 ZEND_API void zend_freedtoa(char *s)
01403 {
01404        Bigint *b = (Bigint *)((int *)s - 1);
01405        b->maxwds = 1 << (b->k = *(int*)b);
01406        Bfree(b);
01407 }
01408 
01409 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
01410  *
01411  * Inspired by "How to Print Floating-Point Numbers Accurately" by
01412  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
01413  *
01414  * Modifications:
01415  *  1. Rather than iterating, we use a simple numeric overestimate
01416  *     to determine k = floor(log10(d)).  We scale relevant
01417  *     quantities using O(log2(k)) rather than O(k) multiplications.
01418  *  2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
01419  *     try to generate digits strictly left to right.  Instead, we
01420  *     compute with fewer bits and propagate the carry if necessary
01421  *     when rounding the final digit up.  This is often faster.
01422  *  3. Under the assumption that input will be rounded nearest,
01423  *     mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
01424  *     That is, we allow equality in stopping tests when the
01425  *     round-nearest rule will give the same floating-point value
01426  *     as would satisfaction of the stopping test with strict
01427  *     inequality.
01428  *  4. We remove common factors of powers of 2 from relevant
01429  *     quantities.
01430  *  5. When converting floating-point integers less than 1e16,
01431  *     we use floating-point arithmetic rather than resorting
01432  *     to multiple-precision integers.
01433  *  6. When asked to produce fewer than 15 digits, we first try
01434  *     to get by with floating-point arithmetic; we resort to
01435  *     multiple-precision integer arithmetic only if we cannot
01436  *     guarantee that the floating-point calculation has given
01437  *     the correctly rounded result.  For k requested digits and
01438  *     "uniformly" distributed input, the probability is
01439  *     something like 10^(k-15) that we must resort to the Long
01440  *     calculation.
01441  */
01442 
01443 ZEND_API char * zend_dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
01444 {
01445  /* Arguments ndigits, decpt, sign are similar to those
01446     of ecvt and fcvt; trailing zeros are suppressed from
01447     the returned string.  If not null, *rve is set to point
01448     to the end of the return value.  If d is +-Infinity or NaN,
01449     then *decpt is set to 9999.
01450 
01451     mode:
01452         0 ==> shortest string that yields d when read in
01453             and rounded to nearest.
01454         1 ==> like 0, but with Steele & White stopping rule;
01455             e.g. with IEEE P754 arithmetic , mode 0 gives
01456             1e23 whereas mode 1 gives 9.999999999999999e22.
01457         2 ==> max(1,ndigits) significant digits.  This gives a
01458             return value similar to that of ecvt, except
01459             that trailing zeros are suppressed.
01460         3 ==> through ndigits past the decimal point.  This
01461             gives a return value similar to that from fcvt,
01462             except that trailing zeros are suppressed, and
01463             ndigits can be negative.
01464         4-9 should give the same return values as 2-3, i.e.,
01465             4 <= mode <= 9 ==> same return as mode
01466             2 + (mode & 1).  These modes are mainly for
01467             debugging; often they run slower but sometimes
01468             faster than modes 2-3.
01469         4,5,8,9 ==> left-to-right digit generation.
01470         6-9 ==> don't try fast floating-point estimate
01471             (if applicable).
01472 
01473         Values of mode other than 0-9 are treated as mode 0.
01474 
01475         Sufficient space is allocated to the return value
01476         to hold the suppressed trailing zeros.
01477     */
01478 
01479        int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1,
01480               j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
01481               spec_case = 0, try_quick;
01482        Long L;
01483 #ifndef Sudden_Underflow
01484        int denorm;
01485        ULong x;
01486 #endif
01487        Bigint *b, *b1, *delta, *mlo, *mhi, *S, *tmp;
01488        double ds;
01489        char *s, *s0;
01490        volatile _double d, d2, eps;
01491 
01492        value(d) = _d;
01493 
01494        if (word0(d) & Sign_bit) {
01495               /* set sign for everything, including 0's and NaNs */
01496               *sign = 1;
01497               word0(d) &= ~Sign_bit;  /* clear sign bit */
01498        }
01499        else
01500               *sign = 0;
01501 
01502 #if defined(IEEE_Arith) + defined(VAX)
01503 #ifdef IEEE_Arith
01504        if ((word0(d) & Exp_mask) == Exp_mask)
01505 #else
01506               if (word0(d)  == 0x8000)
01507 #endif
01508               {
01509                      /* Infinity or NaN */
01510                      *decpt = 9999;
01511 #ifdef IEEE_Arith
01512                      if (!word1(d) && !(word0(d) & 0xfffff))
01513                             return nrv_alloc("Infinity", rve, 8);
01514 #endif
01515                      return nrv_alloc("NaN", rve, 3);
01516               }
01517 #endif
01518 #ifdef IBM
01519        value(d) += 0; /* normalize */
01520 #endif
01521        if (!value(d)) {
01522               *decpt = 1;
01523               return nrv_alloc("0", rve, 1);
01524        }
01525 
01526        b = d2b(value(d), &be, &bbits);
01527 #ifdef Sudden_Underflow
01528        i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
01529 #else
01530        if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
01531 #endif
01532               value(d2) = value(d);
01533               word0(d2) &= Frac_mask1;
01534               word0(d2) |= Exp_11;
01535 #ifdef IBM
01536               if (j = 11 - hi0bits(word0(d2) & Frac_mask))
01537                      value(d2) /= 1 << j;
01538 #endif
01539 
01540               /* log(x)   ~=~ log(1.5) + (x-1.5)/1.5
01541                * log10(x)  =  log(x) / log(10)
01542                *      ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
01543                * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
01544                *
01545                * This suggests computing an approximation k to log10(d) by
01546                *
01547                * k = (i - Bias)*0.301029995663981
01548                *  + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
01549                *
01550                * We want k to be too large rather than too small.
01551                * The error in the first-order Taylor series approximation
01552                * is in our favor, so we just round up the constant enough
01553                * to compensate for any error in the multiplication of
01554                * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
01555                * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
01556                * adding 1e-13 to the constant term more than suffices.
01557                * Hence we adjust the constant term to 0.1760912590558.
01558                * (We could get a more accurate k by invoking log10,
01559                *  but this is probably not worthwhile.)
01560                */
01561 
01562               i -= Bias;
01563 #ifdef IBM
01564               i <<= 2;
01565               i += j;
01566 #endif
01567 #ifndef Sudden_Underflow
01568               denorm = 0;
01569        }
01570        else {
01571               /* d is denormalized */
01572 
01573               i = bbits + be + (Bias + (P-1) - 1);
01574               x = i > 32  ? (word0(d) << (64 - i)) | (word1(d) >> (i - 32))
01575                      : (word1(d) << (32 - i));
01576               value(d2) = x;
01577               word0(d2) -= 31*Exp_msk1; /* adjust exponent */
01578               i -= (Bias + (P-1) - 1) + 1;
01579               denorm = 1;
01580        }
01581 #endif
01582        ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
01583        k = (int)ds;
01584        if (ds < 0. && ds != k)
01585               k--;    /* want k = floor(ds) */
01586        k_check = 1;
01587        if (k >= 0 && k <= Ten_pmax) {
01588               if (value(d) < tens[k])
01589                      k--;
01590               k_check = 0;
01591        }
01592        j = bbits - i - 1;
01593        if (j >= 0) {
01594               b2 = 0;
01595               s2 = j;
01596        }
01597        else {
01598               b2 = -j;
01599               s2 = 0;
01600        }
01601        if (k >= 0) {
01602               b5 = 0;
01603               s5 = k;
01604               s2 += k;
01605        }
01606        else {
01607               b2 -= k;
01608               b5 = -k;
01609               s5 = 0;
01610        }
01611        if (mode < 0 || mode > 9)
01612               mode = 0;
01613        try_quick = 1;
01614        if (mode > 5) {
01615               mode -= 4;
01616               try_quick = 0;
01617        }
01618        leftright = 1;
01619        switch(mode) {
01620               case 0:
01621               case 1:
01622                      ilim = ilim1 = -1;
01623                      i = 18;
01624                      ndigits = 0;
01625                      break;
01626               case 2:
01627                      leftright = 0;
01628                      /* no break */
01629               case 4:
01630                      if (ndigits <= 0)
01631                             ndigits = 1;
01632                      ilim = ilim1 = i = ndigits;
01633                      break;
01634               case 3:
01635                      leftright = 0;
01636                      /* no break */
01637               case 5:
01638                      i = ndigits + k + 1;
01639                      ilim = i;
01640                      ilim1 = i - 1;
01641                      if (i <= 0)
01642                             i = 1;
01643        }
01644        s = s0 = rv_alloc(i);
01645 
01646        if (ilim >= 0 && ilim <= Quick_max && try_quick) {
01647 
01648               /* Try to get by with floating-point arithmetic. */
01649 
01650               i = 0;
01651               value(d2) = value(d);
01652               k0 = k;
01653               ilim0 = ilim;
01654               ieps = 2; /* conservative */
01655               if (k > 0) {
01656                      ds = tens[k&0xf];
01657                      j = k >> 4;
01658                      if (j & Bletch) {
01659                             /* prevent overflows */
01660                             j &= Bletch - 1;
01661                             value(d) /= bigtens[n_bigtens-1];
01662                             ieps++;
01663                      }
01664                      for(; j; j >>= 1, i++)
01665                             if (j & 1) {
01666                                    ieps++;
01667                                    ds *= bigtens[i];
01668                             }
01669                      value(d) /= ds;
01670               }
01671               else if ((j1 = -k)) {
01672                      value(d) *= tens[j1 & 0xf];
01673                      for(j = j1 >> 4; j; j >>= 1, i++)
01674                             if (j & 1) {
01675                                    ieps++;
01676                                    value(d) *= bigtens[i];
01677                             }
01678               }
01679               if (k_check && value(d) < 1. && ilim > 0) {
01680                      if (ilim1 <= 0)
01681                             goto fast_failed;
01682                      ilim = ilim1;
01683                      k--;
01684                      value(d) *= 10.;
01685                      ieps++;
01686               }
01687               value(eps) = ieps*value(d) + 7.;
01688               word0(eps) -= (P-1)*Exp_msk1;
01689               if (ilim == 0) {
01690                      S = mhi = 0;
01691                      value(d) -= 5.;
01692                      if (value(d) > value(eps))
01693                             goto one_digit;
01694                      if (value(d) < -value(eps))
01695                             goto no_digits;
01696                      goto fast_failed;
01697               }
01698 #ifndef No_leftright
01699               if (leftright) {
01700                      /* Use Steele & White method of only
01701                       * generating digits needed.
01702                       */
01703                      value(eps) = 0.5/tens[ilim-1] - value(eps);
01704                      for(i = 0;;) {
01705                             L = value(d);
01706                             value(d) -= L;
01707                             *s++ = '0' + (int)L;
01708                             if (value(d) < value(eps))
01709                                    goto ret1;
01710                             if (1. - value(d) < value(eps))
01711                                    goto bump_up;
01712                             if (++i >= ilim)
01713                                    break;
01714                             value(eps) *= 10.;
01715                             value(d) *= 10.;
01716                      }
01717               }
01718               else {
01719 #endif
01720                      /* Generate ilim digits, then fix them up. */
01721                      value(eps) *= tens[ilim-1];
01722                      for(i = 1;; i++, value(d) *= 10.) {
01723                             L = value(d);
01724                             value(d) -= L;
01725                             *s++ = '0' + (int)L;
01726                             if (i == ilim) {
01727                                    if (value(d) > 0.5 + value(eps))
01728                                           goto bump_up;
01729                                    else if (value(d) < 0.5 - value(eps)) {
01730                                           while(*--s == '0');
01731                                           s++;
01732                                           goto ret1;
01733                                    }
01734                                    break;
01735                             }
01736                      }
01737 #ifndef No_leftright
01738               }
01739 #endif
01740 fast_failed:
01741               s = s0;
01742               value(d) = value(d2);
01743               k = k0;
01744               ilim = ilim0;
01745        }
01746 
01747        /* Do we have a "small" integer? */
01748 
01749        if (be >= 0 && k <= Int_max) {
01750               /* Yes. */
01751               ds = tens[k];
01752               if (ndigits < 0 && ilim <= 0) {
01753                      S = mhi = 0;
01754                      if (ilim < 0 || value(d) <= 5*ds)
01755                             goto no_digits;
01756                      goto one_digit;
01757               }
01758               for(i = 1;; i++) {
01759                      L = value(d) / ds;
01760                      value(d) -= L*ds;
01761 #ifdef Check_FLT_ROUNDS
01762                      /* If FLT_ROUNDS == 2, L will usually be high by 1 */
01763                      if (value(d) < 0) {
01764                             L--;
01765                             value(d) += ds;
01766                      }
01767 #endif
01768                      *s++ = '0' + (int)L;
01769                      if (i == ilim) {
01770                             value(d) += value(d);
01771                             if (value(d) > ds || (value(d) == ds && (L & 1))) {
01772 bump_up:
01773                                    while(*--s == '9')
01774                                           if (s == s0) {
01775                                                  k++;
01776                                                  *s = '0';
01777                                                  break;
01778                                           }
01779                                    ++*s++;
01780                             }
01781                             break;
01782                      }
01783                      if (!(value(d) *= 10.))
01784                             break;
01785               }
01786               goto ret1;
01787        }
01788 
01789        m2 = b2;
01790        m5 = b5;
01791        mhi = mlo = 0;
01792        if (leftright) {
01793               if (mode < 2) {
01794                      i =
01795 #ifndef Sudden_Underflow
01796                             denorm ? be + (Bias + (P-1) - 1 + 1) :
01797 #endif
01798 #ifdef IBM
01799                             1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
01800 #else
01801                      1 + P - bbits;
01802 #endif
01803               }
01804               else {
01805                      j = ilim - 1;
01806                      if (m5 >= j)
01807                             m5 -= j;
01808                      else {
01809                             s5 += j -= m5;
01810                             b5 += j;
01811                             m5 = 0;
01812                      }
01813                      if ((i = ilim) < 0) {
01814                             m2 -= i;
01815                             i = 0;
01816                      }
01817               }
01818               b2 += i;
01819               s2 += i;
01820               mhi = i2b(1);
01821        }
01822        if (m2 > 0 && s2 > 0) {
01823               i = m2 < s2 ? m2 : s2;
01824               b2 -= i;
01825               m2 -= i;
01826               s2 -= i;
01827        }
01828        if (b5 > 0) {
01829               if (leftright) {
01830                      if (m5 > 0) {
01831                             mhi = pow5mult(mhi, m5);
01832                             b1 = mult(mhi, b);
01833                             Bfree(b);
01834                             b = b1;
01835                      }
01836                      if ((j = b5 - m5)) {
01837                             b = pow5mult(b, j);
01838                      }
01839               } else {
01840                      b = pow5mult(b, b5);
01841               }
01842        }
01843        S = i2b(1);
01844        if (s5 > 0)
01845               S = pow5mult(S, s5);
01846        /* Check for special case that d is a normalized power of 2. */
01847 
01848        if (mode < 2) {
01849               if (!word1(d) && !(word0(d) & Bndry_mask)
01850 #ifndef Sudden_Underflow
01851                             && word0(d) & Exp_mask
01852 #endif
01853                  ) {
01854                      /* The special case */
01855                      b2 += Log2P;
01856                      s2 += Log2P;
01857                      spec_case = 1;
01858               } else {
01859                      spec_case = 0;
01860               }
01861        }
01862 
01863        /* Arrange for convenient computation of quotients:
01864         * shift left if necessary so divisor has 4 leading 0 bits.
01865         *
01866         * Perhaps we should just compute leading 28 bits of S once
01867         * and for all and pass them and a shift to quorem, so it
01868         * can do shifts and ors to compute the numerator for q.
01869         */
01870 #ifdef Pack_32
01871        if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
01872               i = 32 - i;
01873 #else
01874        if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
01875               i = 16 - i;
01876 #endif
01877        if (i > 4) {
01878               i -= 4;
01879               b2 += i;
01880               m2 += i;
01881               s2 += i;
01882        }
01883        else if (i < 4) {
01884               i += 28;
01885               b2 += i;
01886               m2 += i;
01887               s2 += i;
01888        }
01889        if (b2 > 0)
01890               b = lshift(b, b2);
01891        if (s2 > 0)
01892               S = lshift(S, s2);
01893        if (k_check) {
01894               if (cmp(b,S) < 0) {
01895                      k--;
01896                      b = multadd(b, 10, 0);  /* we botched the k estimate */
01897                      if (leftright)
01898                             mhi = multadd(mhi, 10, 0);
01899                      ilim = ilim1;
01900               }
01901        }
01902        if (ilim <= 0 && mode > 2) {
01903               if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
01904                      /* no digits, fcvt style */
01905 no_digits:
01906                      k = -1 - ndigits;
01907                      goto ret;
01908               }
01909 one_digit:
01910               *s++ = '1';
01911               k++;
01912               goto ret;
01913        }
01914        if (leftright) {
01915               if (m2 > 0)
01916                      mhi = lshift(mhi, m2);
01917 
01918               /* Compute mlo -- check for special case
01919                * that d is a normalized power of 2.
01920                */
01921 
01922               mlo = mhi;
01923               if (spec_case) {
01924                      mhi = Balloc(mhi->k);
01925                      Bcopy(mhi, mlo);
01926                      mhi = lshift(mhi, Log2P);
01927               }
01928 
01929               for(i = 1;;i++) {
01930                      dig = quorem(b,S) + '0';
01931                      /* Do we yet have the shortest decimal string
01932                       * that will round to d?
01933                       */
01934                      j = cmp(b, mlo);
01935                      delta = diff(S, mhi);
01936                      j1 = delta->sign ? 1 : cmp(b, delta);
01937                      Bfree(delta);
01938 #ifndef ROUND_BIASED
01939                      if (j1 == 0 && !mode && !(word1(d) & 1)) {
01940                             if (dig == '9')
01941                                    goto round_9_up;
01942                             if (j > 0)
01943                                    dig++;
01944                             *s++ = dig;
01945                             goto ret;
01946                      }
01947 #endif
01948                      if (j < 0 || (j == 0 && !mode
01949 #ifndef ROUND_BIASED
01950                                           && !(word1(d) & 1)
01951 #endif
01952                                           )) {
01953                             if (j1 > 0) {
01954                                    b = lshift(b, 1);
01955                                    j1 = cmp(b, S);
01956                                    if ((j1 > 0 || (j1 == 0 && (dig & 1)))
01957                                                  && dig++ == '9')
01958                                           goto round_9_up;
01959                             }
01960                             *s++ = dig;
01961                             goto ret;
01962                      }
01963                      if (j1 > 0) {
01964                             if (dig == '9') { /* possible if i == 1 */
01965 round_9_up:
01966                                    *s++ = '9';
01967                                    goto roundoff;
01968                             }
01969                             *s++ = dig + 1;
01970                             goto ret;
01971                      }
01972                      *s++ = dig;
01973                      if (i == ilim)
01974                             break;
01975                      b = multadd(b, 10, 0);
01976                      if (mlo == mhi)
01977                             mlo = mhi = multadd(mhi, 10, 0);
01978                      else {
01979                             mlo = multadd(mlo, 10, 0);
01980                             mhi = multadd(mhi, 10, 0);
01981                      }
01982               }
01983        }
01984        else
01985               for(i = 1;; i++) {
01986                      *s++ = dig = quorem(b,S) + '0';
01987                      if (i >= ilim)
01988                             break;
01989                      b = multadd(b, 10, 0);
01990               }
01991 
01992        /* Round off last digit */
01993 
01994        b = lshift(b, 1);
01995        j = cmp(b, S);
01996        if (j > 0 || (j == 0 && (dig & 1))) {
01997 roundoff:
01998               while(*--s == '9')
01999                      if (s == s0) {
02000                             k++;
02001                             *s++ = '1';
02002                             goto ret;
02003                      }
02004               ++*s++;
02005        }
02006        else {
02007               while(*--s == '0');
02008               s++;
02009        }
02010 ret:
02011        Bfree(S);
02012        if (mhi) {
02013               if (mlo && mlo != mhi)
02014                      Bfree(mlo);
02015               Bfree(mhi);
02016        }
02017 ret1:
02018 
02019        _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
02020        while (p5s) {
02021               tmp = p5s;
02022               p5s = p5s->next;
02023               free(tmp);
02024        }
02025        _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
02026 
02027        Bfree(b);
02028 
02029        if (s == s0) {              /* don't return empty string */
02030               *s++ = '0';
02031               k = 0;
02032        }
02033        *s = 0;
02034        *decpt = k + 1;
02035        if (rve)
02036               *rve = s;
02037        return s0;
02038 }
02039 
02040 /* F* VC6 */
02041 #if _MSC_VER <= 1300
02042 # pragma optimize( "", off )
02043 #endif
02044 ZEND_API double zend_strtod (CONST char *s00, char **se)
02045 {
02046        int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
02047               e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
02048        CONST char *s, *s0, *s1;
02049        volatile double aadj, aadj1, adj;
02050        volatile _double rv, rv0;
02051        Long L;
02052        ULong y, z;
02053        Bigint *bb, *bb1, *bd, *bd0, *bs, *delta, *tmp;
02054        double result;
02055 
02056        CONST char decimal_point = '.';
02057 
02058        sign = nz0 = nz = 0;
02059        value(rv) = 0.;
02060 
02061 
02062        for(s = s00; isspace((unsigned char) *s); s++)
02063               ;
02064 
02065        if (*s == '-') {
02066               sign = 1;
02067               s++;
02068        } else if (*s == '+') {
02069               s++;
02070        }
02071 
02072        if (*s == '\0') {
02073               s = s00;
02074               goto ret;
02075        }
02076 
02077        if (*s == '0') {
02078               nz0 = 1;
02079               while(*++s == '0') ;
02080               if (!*s)
02081                      goto ret;
02082        }
02083        s0 = s;
02084        y = z = 0;
02085        for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
02086               if (nd < 9)
02087                      y = 10*y + c - '0';
02088               else if (nd < 16)
02089                      z = 10*z + c - '0';
02090        nd0 = nd;
02091        if (c == decimal_point) {
02092               c = *++s;
02093               if (!nd) {
02094                      for(; c == '0'; c = *++s)
02095                             nz++;
02096                      if (c > '0' && c <= '9') {
02097                             s0 = s;
02098                             nf += nz;
02099                             nz = 0;
02100                             goto have_dig;
02101                      }
02102                      goto dig_done;
02103               }
02104               for(; c >= '0' && c <= '9'; c = *++s) {
02105 have_dig:
02106                      nz++;
02107                      if (c -= '0') {
02108                             nf += nz;
02109                             for(i = 1; i < nz; i++)
02110                                    if (nd++ < 9)
02111                                           y *= 10;
02112                                    else if (nd <= DBL_DIG + 1)
02113                                           z *= 10;
02114                             if (nd++ < 9)
02115                                    y = 10*y + c;
02116                             else if (nd <= DBL_DIG + 1)
02117                                    z = 10*z + c;
02118                             nz = 0;
02119                      }
02120               }
02121        }
02122 dig_done:
02123        e = 0;
02124        if (c == 'e' || c == 'E') {
02125               if (!nd && !nz && !nz0) {
02126                      s = s00;
02127                      goto ret;
02128               }
02129               s00 = s;
02130               esign = 0;
02131               switch(c = *++s) {
02132                      case '-':
02133                             esign = 1;
02134                      case '+':
02135                             c = *++s;
02136               }
02137               if (c >= '0' && c <= '9') {
02138                      while(c == '0')
02139                             c = *++s;
02140                      if (c > '0' && c <= '9') {
02141                             L = c - '0';
02142                             s1 = s;
02143                             while((c = *++s) >= '0' && c <= '9')
02144                                    L = 10*L + c - '0';
02145                             if (s - s1 > 8 || L > 19999)
02146                                    /* Avoid confusion from exponents
02147                                     * so large that e might overflow.
02148                                     */
02149                                    e = 19999; /* safe for 16 bit ints */
02150                             else
02151                                    e = (int)L;
02152                             if (esign)
02153                                    e = -e;
02154                      }
02155                      else
02156                             e = 0;
02157               }
02158               else
02159                      s = s00;
02160        }
02161        if (!nd) {
02162               if (!nz && !nz0)
02163                      s = s00;
02164               goto ret;
02165        }
02166        e1 = e -= nf;
02167 
02168        /* Now we have nd0 digits, starting at s0, followed by a
02169         * decimal point, followed by nd-nd0 digits.  The number we're
02170         * after is the integer represented by those digits times
02171         * 10**e */
02172 
02173        if (!nd0)
02174               nd0 = nd;
02175        k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
02176        value(rv) = y;
02177        if (k > 9)
02178               value(rv) = tens[k - 9] * value(rv) + z;
02179        bd0 = 0;
02180        if (nd <= DBL_DIG
02181 #ifndef RND_PRODQUOT
02182                      && FLT_ROUNDS == 1
02183 #endif
02184           ) {
02185               if (!e)
02186                      goto ret;
02187               if (e > 0) {
02188                      if (e <= Ten_pmax) {
02189 #ifdef VAX
02190                             goto vax_ovfl_check;
02191 #else
02192                             /* value(rv) = */ rounded_product(value(rv),
02193                                           tens[e]);
02194                             goto ret;
02195 #endif
02196                      }
02197                      i = DBL_DIG - nd;
02198                      if (e <= Ten_pmax + i) {
02199                             /* A fancier test would sometimes let us do
02200                              * this for larger i values.
02201                              */
02202                             e -= i;
02203                             value(rv) *= tens[i];
02204 #ifdef VAX
02205                             /* VAX exponent range is so narrow we must
02206                              * worry about overflow here...
02207                              */
02208 vax_ovfl_check:
02209                             word0(rv) -= P*Exp_msk1;
02210                             /* value(rv) = */ rounded_product(value(rv),
02211                                           tens[e]);
02212                             if ((word0(rv) & Exp_mask)
02213                                           > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
02214                                    goto ovfl;
02215                             word0(rv) += P*Exp_msk1;
02216 #else
02217                             /* value(rv) = */ rounded_product(value(rv),
02218                                           tens[e]);
02219 #endif
02220                             goto ret;
02221                      }
02222               }
02223 #ifndef Inaccurate_Divide
02224               else if (e >= -Ten_pmax) {
02225                      /* value(rv) = */ rounded_quotient(value(rv),
02226                                    tens[-e]);
02227                      goto ret;
02228               }
02229 #endif
02230        }
02231        e1 += nd - k;
02232 
02233        /* Get starting approximation = rv * 10**e1 */
02234 
02235        if (e1 > 0) {
02236               if ((i = e1 & 15))
02237                      value(rv) *= tens[i];
02238               if (e1 &= ~15) {
02239                      if (e1 > DBL_MAX_10_EXP) {
02240 ovfl:
02241                             errno = ERANGE;
02242 #ifndef Bad_float_h
02243                             value(rv) = HUGE_VAL;
02244 #else
02245                             /* Can't trust HUGE_VAL */
02246 #ifdef IEEE_Arith
02247                             word0(rv) = Exp_mask;
02248                             word1(rv) = 0;
02249 #else
02250                             word0(rv) = Big0;
02251                             word1(rv) = Big1;
02252 #endif
02253 #endif
02254                             if (bd0)
02255                                    goto retfree;
02256                             goto ret;
02257                      }
02258                      if (e1 >>= 4) {
02259                             for(j = 0; e1 > 1; j++, e1 >>= 1)
02260                                    if (e1 & 1)
02261                                           value(rv) *= bigtens[j];
02262                             /* The last multiplication could overflow. */
02263                             word0(rv) -= P*Exp_msk1;
02264                             value(rv) *= bigtens[j];
02265                             if ((z = word0(rv) & Exp_mask)
02266                                           > Exp_msk1*(DBL_MAX_EXP+Bias-P))
02267                                    goto ovfl;
02268                             if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
02269                                    /* set to largest number */
02270                                    /* (Can't trust DBL_MAX) */
02271                                    word0(rv) = Big0;
02272                                    word1(rv) = Big1;
02273                             }
02274                             else
02275                                    word0(rv) += P*Exp_msk1;
02276                      }
02277 
02278               }
02279        }
02280        else if (e1 < 0) {
02281               e1 = -e1;
02282               if ((i = e1 & 15))
02283                      value(rv) /= tens[i];
02284               if (e1 &= ~15) {
02285                      e1 >>= 4;
02286                      if (e1 >= 1 << n_bigtens)
02287                             goto undfl;
02288                      for(j = 0; e1 > 1; j++, e1 >>= 1)
02289                             if (e1 & 1)
02290                                    value(rv) *= tinytens[j];
02291                      /* The last multiplication could underflow. */
02292                      value(rv0) = value(rv);
02293                      value(rv) *= tinytens[j];
02294                      if (!value(rv)) {
02295                             value(rv) = 2.*value(rv0);
02296                             value(rv) *= tinytens[j];
02297                             if (!value(rv)) {
02298 undfl:
02299                                    value(rv) = 0.;
02300                                    errno = ERANGE;
02301                                    if (bd0)
02302                                           goto retfree;
02303                                    goto ret;
02304                             }
02305                             word0(rv) = Tiny0;
02306                             word1(rv) = Tiny1;
02307                             /* The refinement below will clean
02308                              * this approximation up.
02309                              */
02310                      }
02311               }
02312        }
02313 
02314        /* Now the hard part -- adjusting rv to the correct value.*/
02315 
02316        /* Put digits into bd: true value = bd * 10^e */
02317 
02318        bd0 = s2b(s0, nd0, nd, y);
02319 
02320        for(;;) {
02321               bd = Balloc(bd0->k);
02322               Bcopy(bd, bd0);
02323               bb = d2b(value(rv), &bbe, &bbbits);       /* rv = bb * 2^bbe */
02324               bs = i2b(1);
02325 
02326               if (e >= 0) {
02327                      bb2 = bb5 = 0;
02328                      bd2 = bd5 = e;
02329               }
02330               else {
02331                      bb2 = bb5 = -e;
02332                      bd2 = bd5 = 0;
02333               }
02334               if (bbe >= 0)
02335                      bb2 += bbe;
02336               else
02337                      bd2 -= bbe;
02338               bs2 = bb2;
02339 #ifdef Sudden_Underflow
02340 #ifdef IBM
02341               j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
02342 #else
02343               j = P + 1 - bbbits;
02344 #endif
02345 #else
02346               i = bbe + bbbits - 1;       /* logb(rv) */
02347               if (i < Emin) /* denormal */
02348                      j = bbe + (P-Emin);
02349               else
02350                      j = P + 1 - bbbits;
02351 #endif
02352               bb2 += j;
02353               bd2 += j;
02354               i = bb2 < bd2 ? bb2 : bd2;
02355               if (i > bs2)
02356                      i = bs2;
02357               if (i > 0) {
02358                      bb2 -= i;
02359                      bd2 -= i;
02360                      bs2 -= i;
02361               }
02362               if (bb5 > 0) {
02363                      bs = pow5mult(bs, bb5);
02364                      bb1 = mult(bs, bb);
02365                      Bfree(bb);
02366                      bb = bb1;
02367               }
02368               if (bb2 > 0)
02369                      bb = lshift(bb, bb2);
02370               if (bd5 > 0)
02371                      bd = pow5mult(bd, bd5);
02372               if (bd2 > 0)
02373                      bd = lshift(bd, bd2);
02374               if (bs2 > 0)
02375                      bs = lshift(bs, bs2);
02376               delta = diff(bb, bd);
02377               dsign = delta->sign;
02378               delta->sign = 0;
02379               i = cmp(delta, bs);
02380               if (i < 0) {
02381                      /* Error is less than half an ulp -- check for
02382                       * special case of mantissa a power of two.
02383                       */
02384                      if (dsign || word1(rv) || word0(rv) & Bndry_mask)
02385                             break;
02386                      delta = lshift(delta,Log2P);
02387                      if (cmp(delta, bs) > 0)
02388                             goto drop_down;
02389                      break;
02390               }
02391               if (i == 0) {
02392                      /* exactly half-way between */
02393                      if (dsign) {
02394                             if ((word0(rv) & Bndry_mask1) == Bndry_mask1
02395                                           &&  word1(rv) == 0xffffffff) {
02396                                    /*boundary case -- increment exponent*/
02397                                    word0(rv) = (word0(rv) & Exp_mask)
02398                                           + Exp_msk1
02399 #ifdef IBM
02400                                           | Exp_msk1 >> 4
02401 #endif
02402                                           ;
02403                                    word1(rv) = 0;
02404                                    break;
02405                             }
02406                      }
02407                      else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
02408 drop_down:
02409                             /* boundary case -- decrement exponent */
02410 #ifdef Sudden_Underflow
02411                             L = word0(rv) & Exp_mask;
02412 #ifdef IBM
02413                             if (L <  Exp_msk1)
02414 #else
02415                                    if (L <= Exp_msk1)
02416 #endif
02417                                           goto undfl;
02418                             L -= Exp_msk1;
02419 #else
02420                             L = (word0(rv) & Exp_mask) - Exp_msk1;
02421 #endif
02422                             word0(rv) = L | Bndry_mask1;
02423                             word1(rv) = 0xffffffff;
02424 #ifdef IBM
02425                             goto cont;
02426 #else
02427                             break;
02428 #endif
02429                      }
02430 #ifndef ROUND_BIASED
02431                      if (!(word1(rv) & LSB))
02432                             break;
02433 #endif
02434                      if (dsign)
02435                             value(rv) += ulp(value(rv));
02436 #ifndef ROUND_BIASED
02437                      else {
02438                             value(rv) -= ulp(value(rv));
02439 #ifndef Sudden_Underflow
02440                             if (!value(rv))
02441                                    goto undfl;
02442 #endif
02443                      }
02444 #endif
02445                      break;
02446               }
02447               if ((aadj = ratio(delta, bs)) <= 2.) {
02448                      if (dsign)
02449                             aadj = aadj1 = 1.;
02450                      else if (word1(rv) || word0(rv) & Bndry_mask) {
02451 #ifndef Sudden_Underflow
02452                             if (word1(rv) == Tiny1 && !word0(rv))
02453                                    goto undfl;
02454 #endif
02455                             aadj = 1.;
02456                             aadj1 = -1.;
02457                      }
02458                      else {
02459                             /* special case -- power of FLT_RADIX to be */
02460                             /* rounded down... */
02461 
02462                             if (aadj < 2./FLT_RADIX)
02463                                    aadj = 1./FLT_RADIX;
02464                             else
02465                                    aadj *= 0.5;
02466                             aadj1 = -aadj;
02467                      }
02468               }
02469               else {
02470                      aadj *= 0.5;
02471                      aadj1 = dsign ? aadj : -aadj;
02472 #ifdef Check_FLT_ROUNDS
02473                      switch(FLT_ROUNDS) {
02474                             case 2: /* towards +infinity */
02475                                    aadj1 -= 0.5;
02476                                    break;
02477                             case 0: /* towards 0 */
02478                             case 3: /* towards -infinity */
02479                                    aadj1 += 0.5;
02480                      }
02481 #else
02482                      if (FLT_ROUNDS == 0)
02483                             aadj1 += 0.5;
02484 #endif
02485               }
02486               y = word0(rv) & Exp_mask;
02487 
02488               /* Check for overflow */
02489 
02490               if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
02491                      value(rv0) = value(rv);
02492                      word0(rv) -= P*Exp_msk1;
02493                      adj = aadj1 * ulp(value(rv));
02494                      value(rv) += adj;
02495                      if ((word0(rv) & Exp_mask) >=
02496                                    Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
02497                             if (word0(rv0) == Big0 && word1(rv0) == Big1)
02498                                    goto ovfl;
02499                             word0(rv) = Big0;
02500                             word1(rv) = Big1;
02501                             goto cont;
02502                      }
02503                      else
02504                             word0(rv) += P*Exp_msk1;
02505               }
02506               else {
02507 #ifdef Sudden_Underflow
02508                      if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
02509                             value(rv0) = value(rv);
02510                             word0(rv) += P*Exp_msk1;
02511                             adj = aadj1 * ulp(value(rv));
02512                             value(rv) += adj;
02513 #ifdef IBM
02514                             if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
02515 #else
02516                                    if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
02517 #endif
02518                                    {
02519                                           if (word0(rv0) == Tiny0
02520                                                         && word1(rv0) == Tiny1)
02521                                                  goto undfl;
02522                                           word0(rv) = Tiny0;
02523                                           word1(rv) = Tiny1;
02524                                           goto cont;
02525                                    }
02526                                    else
02527                                           word0(rv) -= P*Exp_msk1;
02528                      }
02529                      else {
02530                             adj = aadj1 * ulp(value(rv));
02531                             value(rv) += adj;
02532                      }
02533 #else
02534                      /* Compute adj so that the IEEE rounding rules will
02535                       * correctly round rv + adj in some half-way cases.
02536                       * If rv * ulp(rv) is denormalized (i.e.,
02537                       * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
02538                       * trouble from bits lost to denormalization;
02539                       * example: 1.2e-307 .
02540                       */
02541                      if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
02542                             aadj1 = (double)(int)(aadj + 0.5);
02543                             if (!dsign)
02544                                    aadj1 = -aadj1;
02545                      }
02546                      adj = aadj1 * ulp(value(rv));
02547                      value(rv) += adj;
02548 #endif
02549               }
02550               z = word0(rv) & Exp_mask;
02551               if (y == z) {
02552                      /* Can we stop now? */
02553                      L = aadj;
02554                      aadj -= L;
02555                      /* The tolerances below are conservative. */
02556                      if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
02557                             if (aadj < .4999999 || aadj > .5000001)
02558                                    break;
02559                      }
02560                      else if (aadj < .4999999/FLT_RADIX)
02561                             break;
02562               }
02563 cont:
02564               Bfree(bb);
02565               Bfree(bd);
02566               Bfree(bs);
02567               Bfree(delta);
02568        }
02569 retfree:
02570        Bfree(bb);
02571        Bfree(bd);
02572        Bfree(bs);
02573        Bfree(bd0);
02574        Bfree(delta);
02575 ret:
02576        if (se)
02577               *se = (char *)s;
02578        result = sign ? -value(rv) : value(rv);
02579 
02580        _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
02581        while (p5s) {
02582               tmp = p5s;
02583               p5s = p5s->next;
02584               free(tmp);
02585        }
02586        _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
02587 
02588        return result;
02589 }
02590 
02591 ZEND_API double zend_hex_strtod(const char *str, char **endptr)
02592 {
02593        const char *s = str;
02594        char c;
02595        int any = 0;
02596        double value = 0;
02597 
02598        if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
02599               s += 2;
02600        }
02601 
02602        while ((c = *s++)) {
02603               if (c >= '0' && c <= '9') {
02604                      c -= '0';
02605               } else if (c >= 'A' && c <= 'F') {
02606                      c -= 'A' - 10;
02607               } else if (c >= 'a' && c <= 'f') {
02608                      c -= 'a' - 10;
02609               } else {
02610                      break;
02611               }
02612 
02613               any = 1;
02614               value = value * 16 + c;
02615        }
02616 
02617        if (endptr != NULL) {
02618               *endptr = (char *)(any ? s - 1 : str);
02619        }
02620 
02621        return value;
02622 }
02623 
02624 ZEND_API double zend_oct_strtod(const char *str, char **endptr)
02625 {
02626        const char *s = str;
02627        char c;
02628        double value = 0;
02629        int any = 0;
02630 
02631        /* skip leading zero */
02632        s++;
02633 
02634        while ((c = *s++)) {
02635               if (c < '0' || c > '7') {
02636                      /* break and return the current value if the number is not well-formed
02637                       * that's what Linux strtol() does 
02638                       */
02639                      break;
02640               }
02641               value = value * 8 + c - '0';
02642               any = 1;
02643        }
02644 
02645        if (endptr != NULL) {
02646               *endptr = (char *)(any ? s - 1 : str);
02647        }
02648 
02649        return value;
02650 }
02651 
02652 /*
02653  * Local variables:
02654  * tab-width: 4
02655  * c-basic-offset: 4
02656  * End:
02657  * vim600: sw=4 ts=4 fdm=marker
02658  * vim<600: sw=4 ts=4
02659  */