File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / Zend / zend_strtod.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:34:36 2012 UTC (12 years, 1 month ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, HEAD
php 5.4.3+patches

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>