michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ michael@0: /**************************************************************** michael@0: * michael@0: * The author of this software is David M. Gay. michael@0: * michael@0: * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose without fee is hereby granted, provided that this entire notice michael@0: * is included in all copies of any software which is or includes a copy michael@0: * or modification of this software and in all copies of the supporting michael@0: * documentation for such software. michael@0: * michael@0: * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED michael@0: * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY michael@0: * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY michael@0: * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. michael@0: * michael@0: ***************************************************************/ michael@0: michael@0: /* Please send bug reports to David M. Gay (dmg at acm dot org, michael@0: * with " at " changed at "@" and " dot " changed to "."). */ michael@0: michael@0: /* On a machine with IEEE extended-precision registers, it is michael@0: * necessary to specify double-precision (53-bit) rounding precision michael@0: * before invoking strtod or dtoa. If the machine uses (the equivalent michael@0: * of) Intel 80x87 arithmetic, the call michael@0: * _control87(PC_53, MCW_PC); michael@0: * does this with many compilers. Whether this or another call is michael@0: * appropriate depends on the compiler; for this to work, it may be michael@0: * necessary to #include "float.h" or another system-dependent header michael@0: * file. michael@0: */ michael@0: michael@0: /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. michael@0: * michael@0: * This strtod returns a nearest machine number to the input decimal michael@0: * string (or sets errno to ERANGE). With IEEE arithmetic, ties are michael@0: * broken by the IEEE round-even rule. Otherwise ties are broken by michael@0: * biased rounding (add half and chop). michael@0: * michael@0: * Inspired loosely by William D. Clinger's paper "How to Read Floating michael@0: * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. michael@0: * michael@0: * Modifications: michael@0: * michael@0: * 1. We only require IEEE, IBM, or VAX double-precision michael@0: * arithmetic (not IEEE double-extended). michael@0: * 2. We get by with floating-point arithmetic in a case that michael@0: * Clinger missed -- when we're computing d * 10^n michael@0: * for a small integer d and the integer n is not too michael@0: * much larger than 22 (the maximum integer k for which michael@0: * we can represent 10^k exactly), we may be able to michael@0: * compute (d*10^k) * 10^(e-k) with just one roundoff. michael@0: * 3. Rather than a bit-at-a-time adjustment of the binary michael@0: * result in the hard case, we use floating-point michael@0: * arithmetic to determine the adjustment to within michael@0: * one bit; only in really hard cases do we need to michael@0: * compute a second residual. michael@0: * 4. Because of 3., we don't need a large table of powers of 10 michael@0: * for ten-to-e (just some small tables, e.g. of 10^k michael@0: * for 0 <= k <= 22). michael@0: */ michael@0: michael@0: /* michael@0: * #define IEEE_8087 for IEEE-arithmetic machines where the least michael@0: * significant byte has the lowest address. michael@0: * #define IEEE_MC68k for IEEE-arithmetic machines where the most michael@0: * significant byte has the lowest address. michael@0: * #define Long int on machines with 32-bit ints and 64-bit longs. michael@0: * #define IBM for IBM mainframe-style floating-point arithmetic. michael@0: * #define VAX for VAX-style floating-point arithmetic (D_floating). michael@0: * #define No_leftright to omit left-right logic in fast floating-point michael@0: * computation of dtoa. michael@0: * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 michael@0: * and strtod and dtoa should round accordingly. michael@0: * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 michael@0: * and Honor_FLT_ROUNDS is not #defined. michael@0: * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines michael@0: * that use extended-precision instructions to compute rounded michael@0: * products and quotients) with IBM. michael@0: * #define ROUND_BIASED for IEEE-format with biased rounding. michael@0: * #define Inaccurate_Divide for IEEE-format with correctly rounded michael@0: * products but inaccurate quotients, e.g., for Intel i860. michael@0: * #define NO_LONG_LONG on machines that do not have a "long long" michael@0: * integer type (of >= 64 bits). On such machines, you can michael@0: * #define Just_16 to store 16 bits per 32-bit Long when doing michael@0: * high-precision integer arithmetic. Whether this speeds things michael@0: * up or slows things down depends on the machine and the number michael@0: * being converted. If long long is available and the name is michael@0: * something other than "long long", #define Llong to be the name, michael@0: * and if "unsigned Llong" does not work as an unsigned version of michael@0: * Llong, #define #ULLong to be the corresponding unsigned type. michael@0: * #define KR_headers for old-style C function headers. michael@0: * #define Bad_float_h if your system lacks a float.h or if it does not michael@0: * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, michael@0: * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. michael@0: * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) michael@0: * if memory is available and otherwise does something you deem michael@0: * appropriate. If MALLOC is undefined, malloc will be invoked michael@0: * directly -- and assumed always to succeed. Similarly, if you michael@0: * want something other than the system's free() to be called to michael@0: * recycle memory acquired from MALLOC, #define FREE to be the michael@0: * name of the alternate routine. (Unless you #define michael@0: * NO_GLOBAL_STATE and call destroydtoa, FREE or free is only michael@0: * called in pathological cases, e.g., in a dtoa call after a dtoa michael@0: * return in mode 3 with thousands of digits requested.) michael@0: * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making michael@0: * memory allocations from a private pool of memory when possible. michael@0: * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, michael@0: * unless #defined to be a different length. This default length michael@0: * suffices to get rid of MALLOC calls except for unusual cases, michael@0: * such as decimal-to-binary conversion of a very long string of michael@0: * digits. The longest string dtoa can return is about 751 bytes michael@0: * long. For conversions by strtod of strings of 800 digits and michael@0: * all dtoa conversions in single-threaded executions with 8-byte michael@0: * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte michael@0: * pointers, PRIVATE_MEM >= 7112 appears adequate. michael@0: * #define MULTIPLE_THREADS if the system offers preemptively scheduled michael@0: * multiple threads. In this case, you must provide (or suitably michael@0: * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed michael@0: * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed michael@0: * in pow5mult, ensures lazy evaluation of only one copy of high michael@0: * powers of 5; omitting this lock would introduce a small michael@0: * probability of wasting memory, but would otherwise be harmless.) michael@0: * You must also invoke freedtoa(s) to free the value s returned by michael@0: * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. michael@0: * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that michael@0: * avoids underflows on inputs whose result does not underflow. michael@0: * If you #define NO_IEEE_Scale on a machine that uses IEEE-format michael@0: * floating-point numbers and flushes underflows to zero rather michael@0: * than implementing gradual underflow, then you must also #define michael@0: * Sudden_Underflow. michael@0: * #define USE_LOCALE to use the current locale's decimal_point value. michael@0: * #define SET_INEXACT if IEEE arithmetic is being used and extra michael@0: * computation should be done to set the inexact flag when the michael@0: * result is inexact and avoid setting inexact when the result michael@0: * is exact. In this case, dtoa.c must be compiled in michael@0: * an environment, perhaps provided by #include "dtoa.c" in a michael@0: * suitable wrapper, that defines two functions, michael@0: * int get_inexact(void); michael@0: * void clear_inexact(void); michael@0: * such that get_inexact() returns a nonzero value if the michael@0: * inexact bit is already set, and clear_inexact() sets the michael@0: * inexact bit to 0. When SET_INEXACT is #defined, strtod michael@0: * also does extra computations to set the underflow and overflow michael@0: * flags when appropriate (i.e., when the result is tiny and michael@0: * inexact or when it is a numeric value rounded to +-infinity). michael@0: * #define NO_ERRNO if strtod should not assign errno = ERANGE when michael@0: * the result overflows to +-Infinity or underflows to 0. michael@0: * #define NO_GLOBAL_STATE to avoid defining any non-const global or michael@0: * static variables. Instead the necessary state is stored in an michael@0: * opaque struct, DtoaState, a pointer to which must be passed to michael@0: * every entry point. Two new functions are added to the API: michael@0: * DtoaState *newdtoa(void); michael@0: * void destroydtoa(DtoaState *); michael@0: */ michael@0: michael@0: #ifndef Long michael@0: #define Long long michael@0: #endif michael@0: #ifndef ULong michael@0: typedef unsigned Long ULong; michael@0: #endif michael@0: michael@0: #ifdef DEBUG michael@0: #include michael@0: #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #ifdef USE_LOCALE michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef MALLOC michael@0: #ifdef KR_headers michael@0: extern char *MALLOC(); michael@0: #else michael@0: extern void *MALLOC(size_t); michael@0: #endif michael@0: #else michael@0: #define MALLOC malloc michael@0: #endif michael@0: michael@0: #ifndef FREE michael@0: #define FREE free michael@0: #endif michael@0: michael@0: #ifndef Omit_Private_Memory michael@0: #ifndef PRIVATE_MEM michael@0: #define PRIVATE_MEM 2304 michael@0: #endif michael@0: #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) michael@0: #endif michael@0: michael@0: #undef IEEE_Arith michael@0: #undef Avoid_Underflow michael@0: #ifdef IEEE_MC68k michael@0: #define IEEE_Arith michael@0: #endif michael@0: #ifdef IEEE_8087 michael@0: #define IEEE_Arith michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #ifdef Bad_float_h michael@0: michael@0: #ifdef IEEE_Arith michael@0: #define DBL_DIG 15 michael@0: #define DBL_MAX_10_EXP 308 michael@0: #define DBL_MAX_EXP 1024 michael@0: #define FLT_RADIX 2 michael@0: #endif /*IEEE_Arith*/ michael@0: michael@0: #ifdef IBM michael@0: #define DBL_DIG 16 michael@0: #define DBL_MAX_10_EXP 75 michael@0: #define DBL_MAX_EXP 63 michael@0: #define FLT_RADIX 16 michael@0: #define DBL_MAX 7.2370055773322621e+75 michael@0: #endif michael@0: michael@0: #ifdef VAX michael@0: #define DBL_DIG 16 michael@0: #define DBL_MAX_10_EXP 38 michael@0: #define DBL_MAX_EXP 127 michael@0: #define FLT_RADIX 2 michael@0: #define DBL_MAX 1.7014118346046923e+38 michael@0: #endif michael@0: michael@0: #ifndef LONG_MAX michael@0: #define LONG_MAX 2147483647 michael@0: #endif michael@0: michael@0: #else /* ifndef Bad_float_h */ michael@0: #include michael@0: #endif /* Bad_float_h */ michael@0: michael@0: #ifndef __MATH_H__ michael@0: #include michael@0: #endif michael@0: michael@0: #ifndef CONST michael@0: #ifdef KR_headers michael@0: #define CONST /* blank */ michael@0: #else michael@0: #define CONST const michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 michael@0: Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. michael@0: #endif michael@0: michael@0: typedef union { double d; ULong L[2]; } U; michael@0: michael@0: #define dval(x) ((x).d) michael@0: #ifdef IEEE_8087 michael@0: #define word0(x) ((x).L[1]) michael@0: #define word1(x) ((x).L[0]) michael@0: #else michael@0: #define word0(x) ((x).L[0]) michael@0: #define word1(x) ((x).L[1]) michael@0: #endif michael@0: michael@0: /* The following definition of Storeinc is appropriate for MIPS processors. michael@0: * An alternative that might be better on some machines is michael@0: * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) michael@0: */ michael@0: #if defined(IEEE_8087) + defined(VAX) michael@0: #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ michael@0: ((unsigned short *)a)[0] = (unsigned short)c, a++) michael@0: #else michael@0: #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ michael@0: ((unsigned short *)a)[1] = (unsigned short)c, a++) michael@0: #endif michael@0: michael@0: /* #define P DBL_MANT_DIG */ michael@0: /* Ten_pmax = floor(P*log(2)/log(5)) */ michael@0: /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ michael@0: /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ michael@0: /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ michael@0: michael@0: #ifdef IEEE_Arith michael@0: #define Exp_shift 20 michael@0: #define Exp_shift1 20 michael@0: #define Exp_msk1 0x100000 michael@0: #define Exp_msk11 0x100000 michael@0: #define Exp_mask 0x7ff00000 michael@0: #define P 53 michael@0: #define Bias 1023 michael@0: #define Emin (-1022) michael@0: #define Exp_1 0x3ff00000 michael@0: #define Exp_11 0x3ff00000 michael@0: #define Ebits 11 michael@0: #define Frac_mask 0xfffff michael@0: #define Frac_mask1 0xfffff michael@0: #define Ten_pmax 22 michael@0: #define Bletch 0x10 michael@0: #define Bndry_mask 0xfffff michael@0: #define Bndry_mask1 0xfffff michael@0: #define LSB 1 michael@0: #define Sign_bit 0x80000000 michael@0: #define Log2P 1 michael@0: #define Tiny0 0 michael@0: #define Tiny1 1 michael@0: #define Quick_max 14 michael@0: #define Int_max 14 michael@0: #ifndef NO_IEEE_Scale michael@0: #define Avoid_Underflow michael@0: #ifdef Flush_Denorm /* debugging option */ michael@0: #undef Sudden_Underflow michael@0: #endif michael@0: #endif michael@0: michael@0: #ifndef Flt_Rounds michael@0: #ifdef FLT_ROUNDS michael@0: #define Flt_Rounds FLT_ROUNDS michael@0: #else michael@0: #define Flt_Rounds 1 michael@0: #endif michael@0: #endif /*Flt_Rounds*/ michael@0: michael@0: #ifdef Honor_FLT_ROUNDS michael@0: #define Rounding rounding michael@0: #undef Check_FLT_ROUNDS michael@0: #define Check_FLT_ROUNDS michael@0: #else michael@0: #define Rounding Flt_Rounds michael@0: #endif michael@0: michael@0: #else /* ifndef IEEE_Arith */ michael@0: #undef Check_FLT_ROUNDS michael@0: #undef Honor_FLT_ROUNDS michael@0: #undef SET_INEXACT michael@0: #undef Sudden_Underflow michael@0: #define Sudden_Underflow michael@0: #ifdef IBM michael@0: #undef Flt_Rounds michael@0: #define Flt_Rounds 0 michael@0: #define Exp_shift 24 michael@0: #define Exp_shift1 24 michael@0: #define Exp_msk1 0x1000000 michael@0: #define Exp_msk11 0x1000000 michael@0: #define Exp_mask 0x7f000000 michael@0: #define P 14 michael@0: #define Bias 65 michael@0: #define Exp_1 0x41000000 michael@0: #define Exp_11 0x41000000 michael@0: #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ michael@0: #define Frac_mask 0xffffff michael@0: #define Frac_mask1 0xffffff michael@0: #define Bletch 4 michael@0: #define Ten_pmax 22 michael@0: #define Bndry_mask 0xefffff michael@0: #define Bndry_mask1 0xffffff michael@0: #define LSB 1 michael@0: #define Sign_bit 0x80000000 michael@0: #define Log2P 4 michael@0: #define Tiny0 0x100000 michael@0: #define Tiny1 0 michael@0: #define Quick_max 14 michael@0: #define Int_max 15 michael@0: #else /* VAX */ michael@0: #undef Flt_Rounds michael@0: #define Flt_Rounds 1 michael@0: #define Exp_shift 23 michael@0: #define Exp_shift1 7 michael@0: #define Exp_msk1 0x80 michael@0: #define Exp_msk11 0x800000 michael@0: #define Exp_mask 0x7f80 michael@0: #define P 56 michael@0: #define Bias 129 michael@0: #define Exp_1 0x40800000 michael@0: #define Exp_11 0x4080 michael@0: #define Ebits 8 michael@0: #define Frac_mask 0x7fffff michael@0: #define Frac_mask1 0xffff007f michael@0: #define Ten_pmax 24 michael@0: #define Bletch 2 michael@0: #define Bndry_mask 0xffff007f michael@0: #define Bndry_mask1 0xffff007f michael@0: #define LSB 0x10000 michael@0: #define Sign_bit 0x8000 michael@0: #define Log2P 1 michael@0: #define Tiny0 0x80 michael@0: #define Tiny1 0 michael@0: #define Quick_max 15 michael@0: #define Int_max 15 michael@0: #endif /* IBM, VAX */ michael@0: #endif /* IEEE_Arith */ michael@0: michael@0: #ifndef IEEE_Arith michael@0: #define ROUND_BIASED michael@0: #endif michael@0: michael@0: #ifdef RND_PRODQUOT michael@0: #define rounded_product(a,b) a = rnd_prod(a, b) michael@0: #define rounded_quotient(a,b) a = rnd_quot(a, b) michael@0: #ifdef KR_headers michael@0: extern double rnd_prod(), rnd_quot(); michael@0: #else michael@0: extern double rnd_prod(double, double), rnd_quot(double, double); michael@0: #endif michael@0: #else michael@0: #define rounded_product(a,b) a *= b michael@0: #define rounded_quotient(a,b) a /= b michael@0: #endif michael@0: michael@0: #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) michael@0: #define Big1 0xffffffff michael@0: michael@0: #ifndef Pack_32 michael@0: #define Pack_32 michael@0: #endif michael@0: michael@0: #ifdef KR_headers michael@0: #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff) michael@0: #else michael@0: #define FFFFFFFF 0xffffffffUL michael@0: #endif michael@0: michael@0: #ifdef NO_LONG_LONG michael@0: #undef ULLong michael@0: #ifdef Just_16 michael@0: #undef Pack_32 michael@0: /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. michael@0: * This makes some inner loops simpler and sometimes saves work michael@0: * during multiplications, but it often seems to make things slightly michael@0: * slower. Hence the default is now to store 32 bits per Long. michael@0: */ michael@0: #endif michael@0: #else /* long long available */ michael@0: #ifndef Llong michael@0: #define Llong long long michael@0: #endif michael@0: #ifndef ULLong michael@0: #define ULLong unsigned Llong michael@0: #endif michael@0: #endif /* NO_LONG_LONG */ michael@0: michael@0: #ifndef MULTIPLE_THREADS michael@0: #define ACQUIRE_DTOA_LOCK(n) /*nothing*/ michael@0: #define FREE_DTOA_LOCK(n) /*nothing*/ michael@0: #endif michael@0: michael@0: #define Kmax 7 michael@0: michael@0: struct michael@0: Bigint { michael@0: struct Bigint *next; michael@0: int k, maxwds, sign, wds; michael@0: ULong x[1]; michael@0: }; michael@0: michael@0: typedef struct Bigint Bigint; michael@0: michael@0: #ifdef NO_GLOBAL_STATE michael@0: #ifdef MULTIPLE_THREADS michael@0: #error "cannot have both NO_GLOBAL_STATE and MULTIPLE_THREADS" michael@0: #endif michael@0: struct michael@0: DtoaState { michael@0: #define DECLARE_GLOBAL_STATE /* nothing */ michael@0: #else michael@0: #define DECLARE_GLOBAL_STATE static michael@0: #endif michael@0: michael@0: DECLARE_GLOBAL_STATE Bigint *freelist[Kmax+1]; michael@0: DECLARE_GLOBAL_STATE Bigint *p5s; michael@0: #ifndef Omit_Private_Memory michael@0: DECLARE_GLOBAL_STATE double private_mem[PRIVATE_mem]; michael@0: DECLARE_GLOBAL_STATE double *pmem_next michael@0: #ifndef NO_GLOBAL_STATE michael@0: = private_mem michael@0: #endif michael@0: ; michael@0: #endif michael@0: #ifdef NO_GLOBAL_STATE michael@0: }; michael@0: typedef struct DtoaState DtoaState; michael@0: #ifdef KR_headers michael@0: #define STATE_PARAM state, michael@0: #define STATE_PARAM_DECL DtoaState *state; michael@0: #else michael@0: #define STATE_PARAM DtoaState *state, michael@0: #endif michael@0: #define PASS_STATE state, michael@0: #define GET_STATE(field) (state->field) michael@0: michael@0: static DtoaState * michael@0: newdtoa(void) michael@0: { michael@0: DtoaState *state = (DtoaState *) MALLOC(sizeof(DtoaState)); michael@0: if (state) { michael@0: memset(state, 0, sizeof(DtoaState)); michael@0: #ifndef Omit_Private_Memory michael@0: state->pmem_next = state->private_mem; michael@0: #endif michael@0: } michael@0: return state; michael@0: } michael@0: michael@0: static void michael@0: destroydtoa michael@0: #ifdef KR_headers michael@0: (state) STATE_PARAM_DECL michael@0: #else michael@0: (DtoaState *state) michael@0: #endif michael@0: { michael@0: int i; michael@0: Bigint *v, *next; michael@0: michael@0: for (i = 0; i <= Kmax; i++) { michael@0: for (v = GET_STATE(freelist)[i]; v; v = next) { michael@0: next = v->next; michael@0: #ifndef Omit_Private_Memory michael@0: if ((double*)v < GET_STATE(private_mem) || michael@0: (double*)v >= GET_STATE(private_mem) + PRIVATE_mem) michael@0: #endif michael@0: FREE((void*)v); michael@0: } michael@0: } michael@0: FREE((void *)state); michael@0: } michael@0: michael@0: #else michael@0: #define STATE_PARAM /* nothing */ michael@0: #define STATE_PARAM_DECL /* nothing */ michael@0: #define PASS_STATE /* nothing */ michael@0: #define GET_STATE(name) name michael@0: #endif michael@0: michael@0: static Bigint * michael@0: Balloc michael@0: #ifdef KR_headers michael@0: (STATE_PARAM k) STATE_PARAM_DECL int k; michael@0: #else michael@0: (STATE_PARAM int k) michael@0: #endif michael@0: { michael@0: int x; michael@0: Bigint *rv; michael@0: #ifndef Omit_Private_Memory michael@0: size_t len; michael@0: #endif michael@0: michael@0: ACQUIRE_DTOA_LOCK(0); michael@0: /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */ michael@0: /* but this case seems very unlikely. */ michael@0: if (k <= Kmax && (rv = GET_STATE(freelist)[k])) michael@0: GET_STATE(freelist)[k] = rv->next; michael@0: else { michael@0: x = 1 << k; michael@0: #ifdef Omit_Private_Memory michael@0: rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); michael@0: #else michael@0: len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) michael@0: /sizeof(double); michael@0: if (k <= Kmax && GET_STATE(pmem_next) - GET_STATE(private_mem) + len <= PRIVATE_mem) { michael@0: rv = (Bigint*)GET_STATE(pmem_next); michael@0: GET_STATE(pmem_next) += len; michael@0: } michael@0: else michael@0: rv = (Bigint*)MALLOC(len*sizeof(double)); michael@0: #endif michael@0: rv->k = k; michael@0: rv->maxwds = x; michael@0: } michael@0: FREE_DTOA_LOCK(0); michael@0: rv->sign = rv->wds = 0; michael@0: return rv; michael@0: } michael@0: michael@0: static void michael@0: Bfree michael@0: #ifdef KR_headers michael@0: (STATE_PARAM v) STATE_PARAM_DECL Bigint *v; michael@0: #else michael@0: (STATE_PARAM Bigint *v) michael@0: #endif michael@0: { michael@0: if (v) { michael@0: if (v->k > Kmax) michael@0: FREE((void*)v); michael@0: else { michael@0: ACQUIRE_DTOA_LOCK(0); michael@0: v->next = GET_STATE(freelist)[v->k]; michael@0: GET_STATE(freelist)[v->k] = v; michael@0: FREE_DTOA_LOCK(0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ michael@0: y->wds*sizeof(Long) + 2*sizeof(int)) michael@0: michael@0: static Bigint * michael@0: multadd michael@0: #ifdef KR_headers michael@0: (STATE_PARAM b, m, a) STATE_PARAM_DECL Bigint *b; int m, a; michael@0: #else michael@0: (STATE_PARAM Bigint *b, int m, int a) /* multiply by m and add a */ michael@0: #endif michael@0: { michael@0: int i, wds; michael@0: #ifdef ULLong michael@0: ULong *x; michael@0: ULLong carry, y; michael@0: #else michael@0: ULong carry, *x, y; michael@0: #ifdef Pack_32 michael@0: ULong xi, z; michael@0: #endif michael@0: #endif michael@0: Bigint *b1; michael@0: michael@0: wds = b->wds; michael@0: x = b->x; michael@0: i = 0; michael@0: carry = a; michael@0: do { michael@0: #ifdef ULLong michael@0: y = *x * (ULLong)m + carry; michael@0: carry = y >> 32; michael@0: *x++ = (ULong) y & FFFFFFFF; michael@0: #else michael@0: #ifdef Pack_32 michael@0: xi = *x; michael@0: y = (xi & 0xffff) * m + carry; michael@0: z = (xi >> 16) * m + (y >> 16); michael@0: carry = z >> 16; michael@0: *x++ = (z << 16) + (y & 0xffff); michael@0: #else michael@0: y = *x * m + carry; michael@0: carry = y >> 16; michael@0: *x++ = y & 0xffff; michael@0: #endif michael@0: #endif michael@0: } michael@0: while(++i < wds); michael@0: if (carry) { michael@0: if (wds >= b->maxwds) { michael@0: b1 = Balloc(PASS_STATE b->k+1); michael@0: Bcopy(b1, b); michael@0: Bfree(PASS_STATE b); michael@0: b = b1; michael@0: } michael@0: b->x[wds++] = (ULong) carry; michael@0: b->wds = wds; michael@0: } michael@0: return b; michael@0: } michael@0: michael@0: static Bigint * michael@0: s2b michael@0: #ifdef KR_headers michael@0: (STATE_PARAM s, nd0, nd, y9) STATE_PARAM_DECL CONST char *s; int nd0, nd; ULong y9; michael@0: #else michael@0: (STATE_PARAM CONST char *s, int nd0, int nd, ULong y9) michael@0: #endif michael@0: { michael@0: Bigint *b; michael@0: int i, k; michael@0: Long x, y; michael@0: michael@0: x = (nd + 8) / 9; michael@0: for(k = 0, y = 1; x > y; y <<= 1, k++) ; michael@0: #ifdef Pack_32 michael@0: b = Balloc(PASS_STATE k); michael@0: b->x[0] = y9; michael@0: b->wds = 1; michael@0: #else michael@0: b = Balloc(PASS_STATE k+1); michael@0: b->x[0] = y9 & 0xffff; michael@0: b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; michael@0: #endif michael@0: michael@0: i = 9; michael@0: if (9 < nd0) { michael@0: s += 9; michael@0: do b = multadd(PASS_STATE b, 10, *s++ - '0'); michael@0: while(++i < nd0); michael@0: s++; michael@0: } michael@0: else michael@0: s += 10; michael@0: for(; i < nd; i++) michael@0: b = multadd(PASS_STATE b, 10, *s++ - '0'); michael@0: return b; michael@0: } michael@0: michael@0: static int michael@0: hi0bits michael@0: #ifdef KR_headers michael@0: (x) ULong x; michael@0: #else michael@0: (ULong x) michael@0: #endif michael@0: { michael@0: int k = 0; michael@0: michael@0: if (!(x & 0xffff0000)) { michael@0: k = 16; michael@0: x <<= 16; michael@0: } michael@0: if (!(x & 0xff000000)) { michael@0: k += 8; michael@0: x <<= 8; michael@0: } michael@0: if (!(x & 0xf0000000)) { michael@0: k += 4; michael@0: x <<= 4; michael@0: } michael@0: if (!(x & 0xc0000000)) { michael@0: k += 2; michael@0: x <<= 2; michael@0: } michael@0: if (!(x & 0x80000000)) { michael@0: k++; michael@0: if (!(x & 0x40000000)) michael@0: return 32; michael@0: } michael@0: return k; michael@0: } michael@0: michael@0: static int michael@0: lo0bits michael@0: #ifdef KR_headers michael@0: (y) ULong *y; michael@0: #else michael@0: (ULong *y) michael@0: #endif michael@0: { michael@0: int k; michael@0: ULong x = *y; michael@0: michael@0: if (x & 7) { michael@0: if (x & 1) michael@0: return 0; michael@0: if (x & 2) { michael@0: *y = x >> 1; michael@0: return 1; michael@0: } michael@0: *y = x >> 2; michael@0: return 2; michael@0: } michael@0: k = 0; michael@0: if (!(x & 0xffff)) { michael@0: k = 16; michael@0: x >>= 16; michael@0: } michael@0: if (!(x & 0xff)) { michael@0: k += 8; michael@0: x >>= 8; michael@0: } michael@0: if (!(x & 0xf)) { michael@0: k += 4; michael@0: x >>= 4; michael@0: } michael@0: if (!(x & 0x3)) { michael@0: k += 2; michael@0: x >>= 2; michael@0: } michael@0: if (!(x & 1)) { michael@0: k++; michael@0: x >>= 1; michael@0: if (!x) michael@0: return 32; michael@0: } michael@0: *y = x; michael@0: return k; michael@0: } michael@0: michael@0: static Bigint * michael@0: i2b michael@0: #ifdef KR_headers michael@0: (STATE_PARAM i) STATE_PARAM_DECL int i; michael@0: #else michael@0: (STATE_PARAM int i) michael@0: #endif michael@0: { michael@0: Bigint *b; michael@0: michael@0: b = Balloc(PASS_STATE 1); michael@0: b->x[0] = i; michael@0: b->wds = 1; michael@0: return b; michael@0: } michael@0: michael@0: static Bigint * michael@0: mult michael@0: #ifdef KR_headers michael@0: (STATE_PARAM a, b) STATE_PARAM_DECL Bigint *a, *b; michael@0: #else michael@0: (STATE_PARAM Bigint *a, Bigint *b) michael@0: #endif michael@0: { michael@0: Bigint *c; michael@0: int k, wa, wb, wc; michael@0: ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; michael@0: ULong y; michael@0: #ifdef ULLong michael@0: ULLong carry, z; michael@0: #else michael@0: ULong carry, z; michael@0: #ifdef Pack_32 michael@0: ULong z2; michael@0: #endif michael@0: #endif michael@0: michael@0: if (a->wds < b->wds) { michael@0: c = a; michael@0: a = b; michael@0: b = c; michael@0: } michael@0: k = a->k; michael@0: wa = a->wds; michael@0: wb = b->wds; michael@0: wc = wa + wb; michael@0: if (wc > a->maxwds) michael@0: k++; michael@0: c = Balloc(PASS_STATE k); michael@0: for(x = c->x, xa = x + wc; x < xa; x++) michael@0: *x = 0; michael@0: xa = a->x; michael@0: xae = xa + wa; michael@0: xb = b->x; michael@0: xbe = xb + wb; michael@0: xc0 = c->x; michael@0: #ifdef ULLong michael@0: for(; xb < xbe; xc0++) { michael@0: if ((y = *xb++)) { michael@0: x = xa; michael@0: xc = xc0; michael@0: carry = 0; michael@0: do { michael@0: z = *x++ * (ULLong)y + *xc + carry; michael@0: carry = z >> 32; michael@0: *xc++ = (ULong) z & FFFFFFFF; michael@0: } michael@0: while(x < xae); michael@0: *xc = (ULong) carry; michael@0: } michael@0: } michael@0: #else michael@0: #ifdef Pack_32 michael@0: for(; xb < xbe; xb++, xc0++) { michael@0: if (y = *xb & 0xffff) { michael@0: x = xa; michael@0: xc = xc0; michael@0: carry = 0; michael@0: do { michael@0: z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; michael@0: carry = z >> 16; michael@0: z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; michael@0: carry = z2 >> 16; michael@0: Storeinc(xc, z2, z); michael@0: } michael@0: while(x < xae); michael@0: *xc = carry; michael@0: } michael@0: if (y = *xb >> 16) { michael@0: x = xa; michael@0: xc = xc0; michael@0: carry = 0; michael@0: z2 = *xc; michael@0: do { michael@0: z = (*x & 0xffff) * y + (*xc >> 16) + carry; michael@0: carry = z >> 16; michael@0: Storeinc(xc, z, z2); michael@0: z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; michael@0: carry = z2 >> 16; michael@0: } michael@0: while(x < xae); michael@0: *xc = z2; michael@0: } michael@0: } michael@0: #else michael@0: for(; xb < xbe; xc0++) { michael@0: if (y = *xb++) { michael@0: x = xa; michael@0: xc = xc0; michael@0: carry = 0; michael@0: do { michael@0: z = *x++ * y + *xc + carry; michael@0: carry = z >> 16; michael@0: *xc++ = z & 0xffff; michael@0: } michael@0: while(x < xae); michael@0: *xc = carry; michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; michael@0: c->wds = wc; michael@0: return c; michael@0: } michael@0: michael@0: static Bigint * michael@0: pow5mult michael@0: #ifdef KR_headers michael@0: (STATE_PARAM b, k) STATE_PARAM_DECL Bigint *b; int k; michael@0: #else michael@0: (STATE_PARAM Bigint *b, int k) michael@0: #endif michael@0: { michael@0: Bigint *b1, *p5, *p51; michael@0: int i; michael@0: static CONST int p05[3] = { 5, 25, 125 }; michael@0: michael@0: if ((i = k & 3)) michael@0: b = multadd(PASS_STATE b, p05[i-1], 0); michael@0: michael@0: if (!(k >>= 2)) michael@0: return b; michael@0: if (!(p5 = GET_STATE(p5s))) { michael@0: /* first time */ michael@0: #ifdef MULTIPLE_THREADS michael@0: ACQUIRE_DTOA_LOCK(1); michael@0: if (!(p5 = p5s)) { michael@0: p5 = p5s = i2b(625); michael@0: p5->next = 0; michael@0: } michael@0: FREE_DTOA_LOCK(1); michael@0: #else michael@0: p5 = GET_STATE(p5s) = i2b(PASS_STATE 625); michael@0: p5->next = 0; michael@0: #endif michael@0: } michael@0: for(;;) { michael@0: if (k & 1) { michael@0: b1 = mult(PASS_STATE b, p5); michael@0: Bfree(PASS_STATE b); michael@0: b = b1; michael@0: } michael@0: if (!(k >>= 1)) michael@0: break; michael@0: if (!(p51 = p5->next)) { michael@0: #ifdef MULTIPLE_THREADS michael@0: ACQUIRE_DTOA_LOCK(1); michael@0: if (!(p51 = p5->next)) { michael@0: p51 = p5->next = mult(p5,p5); michael@0: p51->next = 0; michael@0: } michael@0: FREE_DTOA_LOCK(1); michael@0: #else michael@0: p51 = p5->next = mult(PASS_STATE p5,p5); michael@0: p51->next = 0; michael@0: #endif michael@0: } michael@0: p5 = p51; michael@0: } michael@0: return b; michael@0: } michael@0: michael@0: static Bigint * michael@0: lshift michael@0: #ifdef KR_headers michael@0: (STATE_PARAM b, k) STATE_PARAM_DECL Bigint *b; int k; michael@0: #else michael@0: (STATE_PARAM Bigint *b, int k) michael@0: #endif michael@0: { michael@0: int i, k1, n, n1; michael@0: Bigint *b1; michael@0: ULong *x, *x1, *xe, z; michael@0: michael@0: #ifdef Pack_32 michael@0: n = k >> 5; michael@0: #else michael@0: n = k >> 4; michael@0: #endif michael@0: k1 = b->k; michael@0: n1 = n + b->wds + 1; michael@0: for(i = b->maxwds; n1 > i; i <<= 1) michael@0: k1++; michael@0: b1 = Balloc(PASS_STATE k1); michael@0: x1 = b1->x; michael@0: for(i = 0; i < n; i++) michael@0: *x1++ = 0; michael@0: x = b->x; michael@0: xe = x + b->wds; michael@0: #ifdef Pack_32 michael@0: if (k &= 0x1f) { michael@0: k1 = 32 - k; michael@0: z = 0; michael@0: do { michael@0: *x1++ = *x << k | z; michael@0: z = *x++ >> k1; michael@0: } michael@0: while(x < xe); michael@0: if ((*x1 = z)) michael@0: ++n1; michael@0: } michael@0: #else michael@0: if (k &= 0xf) { michael@0: k1 = 16 - k; michael@0: z = 0; michael@0: do { michael@0: *x1++ = *x << k & 0xffff | z; michael@0: z = *x++ >> k1; michael@0: } michael@0: while(x < xe); michael@0: if (*x1 = z) michael@0: ++n1; michael@0: } michael@0: #endif michael@0: else do michael@0: *x1++ = *x++; michael@0: while(x < xe); michael@0: b1->wds = n1 - 1; michael@0: Bfree(PASS_STATE b); michael@0: return b1; michael@0: } michael@0: michael@0: static int michael@0: cmp michael@0: #ifdef KR_headers michael@0: (a, b) Bigint *a, *b; michael@0: #else michael@0: (Bigint *a, Bigint *b) michael@0: #endif michael@0: { michael@0: ULong *xa, *xa0, *xb, *xb0; michael@0: int i, j; michael@0: michael@0: i = a->wds; michael@0: j = b->wds; michael@0: #ifdef DEBUG michael@0: if (i > 1 && !a->x[i-1]) michael@0: Bug("cmp called with a->x[a->wds-1] == 0"); michael@0: if (j > 1 && !b->x[j-1]) michael@0: Bug("cmp called with b->x[b->wds-1] == 0"); michael@0: #endif michael@0: if (i -= j) michael@0: return i; michael@0: xa0 = a->x; michael@0: xa = xa0 + j; michael@0: xb0 = b->x; michael@0: xb = xb0 + j; michael@0: for(;;) { michael@0: if (*--xa != *--xb) michael@0: return *xa < *xb ? -1 : 1; michael@0: if (xa <= xa0) michael@0: break; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static Bigint * michael@0: diff michael@0: #ifdef KR_headers michael@0: (STATE_PARAM a, b) STATE_PARAM_DECL Bigint *a, *b; michael@0: #else michael@0: (STATE_PARAM Bigint *a, Bigint *b) michael@0: #endif michael@0: { michael@0: Bigint *c; michael@0: int i, wa, wb; michael@0: ULong *xa, *xae, *xb, *xbe, *xc; michael@0: #ifdef ULLong michael@0: ULLong borrow, y; michael@0: #else michael@0: ULong borrow, y; michael@0: #ifdef Pack_32 michael@0: ULong z; michael@0: #endif michael@0: #endif michael@0: michael@0: i = cmp(a,b); michael@0: if (!i) { michael@0: c = Balloc(PASS_STATE 0); michael@0: c->wds = 1; michael@0: c->x[0] = 0; michael@0: return c; michael@0: } michael@0: if (i < 0) { michael@0: c = a; michael@0: a = b; michael@0: b = c; michael@0: i = 1; michael@0: } michael@0: else michael@0: i = 0; michael@0: c = Balloc(PASS_STATE a->k); michael@0: c->sign = i; michael@0: wa = a->wds; michael@0: xa = a->x; michael@0: xae = xa + wa; michael@0: wb = b->wds; michael@0: xb = b->x; michael@0: xbe = xb + wb; michael@0: xc = c->x; michael@0: borrow = 0; michael@0: #ifdef ULLong michael@0: do { michael@0: y = (ULLong)*xa++ - *xb++ - borrow; michael@0: borrow = y >> 32 & (ULong)1; michael@0: *xc++ = (ULong) y & FFFFFFFF; michael@0: } michael@0: while(xb < xbe); michael@0: while(xa < xae) { michael@0: y = *xa++ - borrow; michael@0: borrow = y >> 32 & (ULong)1; michael@0: *xc++ = (ULong) y & FFFFFFFF; michael@0: } michael@0: #else michael@0: #ifdef Pack_32 michael@0: do { michael@0: y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; michael@0: borrow = (z & 0x10000) >> 16; michael@0: Storeinc(xc, z, y); michael@0: } michael@0: while(xb < xbe); michael@0: while(xa < xae) { michael@0: y = (*xa & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: z = (*xa++ >> 16) - borrow; michael@0: borrow = (z & 0x10000) >> 16; michael@0: Storeinc(xc, z, y); michael@0: } michael@0: #else michael@0: do { michael@0: y = *xa++ - *xb++ - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: *xc++ = y & 0xffff; michael@0: } michael@0: while(xb < xbe); michael@0: while(xa < xae) { michael@0: y = *xa++ - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: *xc++ = y & 0xffff; michael@0: } michael@0: #endif michael@0: #endif michael@0: while(!*--xc) michael@0: wa--; michael@0: c->wds = wa; michael@0: return c; michael@0: } michael@0: michael@0: static double michael@0: ulp michael@0: #ifdef KR_headers michael@0: (x) U x; michael@0: #else michael@0: (U x) michael@0: #endif michael@0: { michael@0: Long L; michael@0: U a; michael@0: michael@0: L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; michael@0: #ifndef Avoid_Underflow michael@0: #ifndef Sudden_Underflow michael@0: if (L > 0) { michael@0: #endif michael@0: #endif michael@0: #ifdef IBM michael@0: L |= Exp_msk1 >> 4; michael@0: #endif michael@0: word0(a) = L; michael@0: word1(a) = 0; michael@0: #ifndef Avoid_Underflow michael@0: #ifndef Sudden_Underflow michael@0: } michael@0: else { michael@0: L = -L >> Exp_shift; michael@0: if (L < Exp_shift) { michael@0: word0(a) = 0x80000 >> L; michael@0: word1(a) = 0; michael@0: } michael@0: else { michael@0: word0(a) = 0; michael@0: L -= Exp_shift; michael@0: word1(a) = L >= 31 ? 1 : 1 << 31 - L; michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: return dval(a); michael@0: } michael@0: michael@0: static double michael@0: b2d michael@0: #ifdef KR_headers michael@0: (a, e) Bigint *a; int *e; michael@0: #else michael@0: (Bigint *a, int *e) michael@0: #endif michael@0: { michael@0: ULong *xa, *xa0, w, y, z; michael@0: int k; michael@0: U d; michael@0: #ifdef VAX michael@0: ULong d0, d1; michael@0: #else michael@0: #define d0 word0(d) michael@0: #define d1 word1(d) michael@0: #endif michael@0: michael@0: xa0 = a->x; michael@0: xa = xa0 + a->wds; michael@0: y = *--xa; michael@0: #ifdef DEBUG michael@0: if (!y) Bug("zero y in b2d"); michael@0: #endif michael@0: k = hi0bits(y); michael@0: *e = 32 - k; michael@0: #ifdef Pack_32 michael@0: if (k < Ebits) { michael@0: d0 = Exp_1 | y >> (Ebits - k); michael@0: w = xa > xa0 ? *--xa : 0; michael@0: d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); michael@0: goto ret_d; michael@0: } michael@0: z = xa > xa0 ? *--xa : 0; michael@0: if (k -= Ebits) { michael@0: d0 = Exp_1 | y << k | z >> (32 - k); michael@0: y = xa > xa0 ? *--xa : 0; michael@0: d1 = z << k | y >> (32 - k); michael@0: } michael@0: else { michael@0: d0 = Exp_1 | y; michael@0: d1 = z; michael@0: } michael@0: #else michael@0: if (k < Ebits + 16) { michael@0: z = xa > xa0 ? *--xa : 0; michael@0: d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; michael@0: w = xa > xa0 ? *--xa : 0; michael@0: y = xa > xa0 ? *--xa : 0; michael@0: d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; michael@0: goto ret_d; michael@0: } michael@0: z = xa > xa0 ? *--xa : 0; michael@0: w = xa > xa0 ? *--xa : 0; michael@0: k -= Ebits + 16; michael@0: d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; michael@0: y = xa > xa0 ? *--xa : 0; michael@0: d1 = w << k + 16 | y << k; michael@0: #endif michael@0: ret_d: michael@0: #ifdef VAX michael@0: word0(d) = d0 >> 16 | d0 << 16; michael@0: word1(d) = d1 >> 16 | d1 << 16; michael@0: #else michael@0: #undef d0 michael@0: #undef d1 michael@0: #endif michael@0: return dval(d); michael@0: } michael@0: michael@0: static Bigint * michael@0: d2b michael@0: #ifdef KR_headers michael@0: (STATE_PARAM d, e, bits) STATE_PARAM_DECL U d; int *e, *bits; michael@0: #else michael@0: (STATE_PARAM U d, int *e, int *bits) michael@0: #endif michael@0: { michael@0: Bigint *b; michael@0: int de, k; michael@0: ULong *x, y, z; michael@0: #ifndef Sudden_Underflow michael@0: int i; michael@0: #endif michael@0: #ifdef VAX michael@0: ULong d0, d1; michael@0: d0 = word0(d) >> 16 | word0(d) << 16; michael@0: d1 = word1(d) >> 16 | word1(d) << 16; michael@0: #else michael@0: #define d0 word0(d) michael@0: #define d1 word1(d) michael@0: #endif michael@0: michael@0: #ifdef Pack_32 michael@0: b = Balloc(PASS_STATE 1); michael@0: #else michael@0: b = Balloc(PASS_STATE 2); michael@0: #endif michael@0: x = b->x; michael@0: michael@0: z = d0 & Frac_mask; michael@0: d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ michael@0: #ifdef Sudden_Underflow michael@0: de = (int)(d0 >> Exp_shift); michael@0: #ifndef IBM michael@0: z |= Exp_msk11; michael@0: #endif michael@0: #else michael@0: if ((de = (int)(d0 >> Exp_shift))) michael@0: z |= Exp_msk1; michael@0: #endif michael@0: #ifdef Pack_32 michael@0: if ((y = d1)) { michael@0: if ((k = lo0bits(&y))) { michael@0: x[0] = y | z << (32 - k); michael@0: z >>= k; michael@0: } michael@0: else michael@0: x[0] = y; michael@0: #ifndef Sudden_Underflow michael@0: i = michael@0: #endif michael@0: b->wds = (x[1] = z) ? 2 : 1; michael@0: } michael@0: else { michael@0: k = lo0bits(&z); michael@0: x[0] = z; michael@0: #ifndef Sudden_Underflow michael@0: i = michael@0: #endif michael@0: b->wds = 1; michael@0: k += 32; michael@0: } michael@0: #else michael@0: if (y = d1) { michael@0: if (k = lo0bits(&y)) michael@0: if (k >= 16) { michael@0: x[0] = y | z << 32 - k & 0xffff; michael@0: x[1] = z >> k - 16 & 0xffff; michael@0: x[2] = z >> k; michael@0: i = 2; michael@0: } michael@0: else { michael@0: x[0] = y & 0xffff; michael@0: x[1] = y >> 16 | z << 16 - k & 0xffff; michael@0: x[2] = z >> k & 0xffff; michael@0: x[3] = z >> k+16; michael@0: i = 3; michael@0: } michael@0: else { michael@0: x[0] = y & 0xffff; michael@0: x[1] = y >> 16; michael@0: x[2] = z & 0xffff; michael@0: x[3] = z >> 16; michael@0: i = 3; michael@0: } michael@0: } michael@0: else { michael@0: #ifdef DEBUG michael@0: if (!z) michael@0: Bug("Zero passed to d2b"); michael@0: #endif michael@0: k = lo0bits(&z); michael@0: if (k >= 16) { michael@0: x[0] = z; michael@0: i = 0; michael@0: } michael@0: else { michael@0: x[0] = z & 0xffff; michael@0: x[1] = z >> 16; michael@0: i = 1; michael@0: } michael@0: k += 32; michael@0: } michael@0: while(!x[i]) michael@0: --i; michael@0: b->wds = i + 1; michael@0: #endif michael@0: #ifndef Sudden_Underflow michael@0: if (de) { michael@0: #endif michael@0: #ifdef IBM michael@0: *e = (de - Bias - (P-1) << 2) + k; michael@0: *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); michael@0: #else michael@0: *e = de - Bias - (P-1) + k; michael@0: *bits = P - k; michael@0: #endif michael@0: #ifndef Sudden_Underflow michael@0: } michael@0: else { michael@0: *e = de - Bias - (P-1) + 1 + k; michael@0: #ifdef Pack_32 michael@0: *bits = 32*i - hi0bits(x[i-1]); michael@0: #else michael@0: *bits = (i+2)*16 - hi0bits(x[i]); michael@0: #endif michael@0: } michael@0: #endif michael@0: return b; michael@0: } michael@0: #undef d0 michael@0: #undef d1 michael@0: michael@0: static double michael@0: ratio michael@0: #ifdef KR_headers michael@0: (a, b) Bigint *a, *b; michael@0: #else michael@0: (Bigint *a, Bigint *b) michael@0: #endif michael@0: { michael@0: U da, db; michael@0: int k, ka, kb; michael@0: michael@0: dval(da) = b2d(a, &ka); michael@0: dval(db) = b2d(b, &kb); michael@0: #ifdef Pack_32 michael@0: k = ka - kb + 32*(a->wds - b->wds); michael@0: #else michael@0: k = ka - kb + 16*(a->wds - b->wds); michael@0: #endif michael@0: #ifdef IBM michael@0: if (k > 0) { michael@0: word0(da) += (k >> 2)*Exp_msk1; michael@0: if (k &= 3) michael@0: dval(da) *= 1 << k; michael@0: } michael@0: else { michael@0: k = -k; michael@0: word0(db) += (k >> 2)*Exp_msk1; michael@0: if (k &= 3) michael@0: dval(db) *= 1 << k; michael@0: } michael@0: #else michael@0: if (k > 0) michael@0: word0(da) += k*Exp_msk1; michael@0: else { michael@0: k = -k; michael@0: word0(db) += k*Exp_msk1; michael@0: } michael@0: #endif michael@0: return dval(da) / dval(db); michael@0: } michael@0: michael@0: static CONST double michael@0: tens[] = { michael@0: 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, michael@0: 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, michael@0: 1e20, 1e21, 1e22 michael@0: #ifdef VAX michael@0: , 1e23, 1e24 michael@0: #endif michael@0: }; michael@0: michael@0: static CONST double michael@0: #ifdef IEEE_Arith michael@0: bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; michael@0: static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, michael@0: #ifdef Avoid_Underflow michael@0: 9007199254740992.*9007199254740992.e-256 michael@0: /* = 2^106 * 1e-53 */ michael@0: #else michael@0: 1e-256 michael@0: #endif michael@0: }; michael@0: /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ michael@0: /* flag unnecessarily. It leads to a song and dance at the end of strtod. */ michael@0: #define Scale_Bit 0x10 michael@0: #define n_bigtens 5 michael@0: #else michael@0: #ifdef IBM michael@0: bigtens[] = { 1e16, 1e32, 1e64 }; michael@0: static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; michael@0: #define n_bigtens 3 michael@0: #else michael@0: bigtens[] = { 1e16, 1e32 }; michael@0: static CONST double tinytens[] = { 1e-16, 1e-32 }; michael@0: #define n_bigtens 2 michael@0: #endif michael@0: #endif michael@0: michael@0: static double michael@0: _strtod michael@0: #ifdef KR_headers michael@0: (STATE_PARAM s00, se) STATE_PARAM_DECL CONST char *s00; char **se; michael@0: #else michael@0: (STATE_PARAM CONST char *s00, char **se) michael@0: #endif michael@0: { michael@0: #ifdef Avoid_Underflow michael@0: int scale; michael@0: #endif michael@0: int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, michael@0: e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; michael@0: CONST char *s, *s0, *s1; michael@0: double aadj, adj; michael@0: U aadj1, rv, rv0; michael@0: Long L; michael@0: ULong y, z; michael@0: Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; michael@0: #ifdef SET_INEXACT michael@0: int inexact, oldinexact; michael@0: #endif michael@0: #ifdef Honor_FLT_ROUNDS michael@0: int rounding; michael@0: #endif michael@0: #ifdef USE_LOCALE michael@0: CONST char *s2; michael@0: #endif michael@0: michael@0: #ifdef __GNUC__ michael@0: delta = bb = bd = bs = 0; michael@0: #endif michael@0: michael@0: sign = nz0 = nz = 0; michael@0: dval(rv) = 0.; michael@0: for(s = s00;;s++) switch(*s) { michael@0: case '-': michael@0: sign = 1; michael@0: /* no break */ michael@0: case '+': michael@0: if (*++s) michael@0: goto break2; michael@0: /* no break */ michael@0: case 0: michael@0: goto ret0; michael@0: case '\t': michael@0: case '\n': michael@0: case '\v': michael@0: case '\f': michael@0: case '\r': michael@0: case ' ': michael@0: continue; michael@0: default: michael@0: goto break2; michael@0: } michael@0: break2: michael@0: if (*s == '0') { michael@0: nz0 = 1; michael@0: while(*++s == '0') ; michael@0: if (!*s) michael@0: goto ret; michael@0: } michael@0: s0 = s; michael@0: y = z = 0; michael@0: for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) michael@0: if (nd < 9) michael@0: y = 10*y + c - '0'; michael@0: else if (nd < 16) michael@0: z = 10*z + c - '0'; michael@0: nd0 = nd; michael@0: #ifdef USE_LOCALE michael@0: s1 = localeconv()->decimal_point; michael@0: if (c == *s1) { michael@0: c = '.'; michael@0: if (*++s1) { michael@0: s2 = s; michael@0: for(;;) { michael@0: if (*++s2 != *s1) { michael@0: c = 0; michael@0: break; michael@0: } michael@0: if (!*++s1) { michael@0: s = s2; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: if (c == '.') { michael@0: c = *++s; michael@0: if (!nd) { michael@0: for(; c == '0'; c = *++s) michael@0: nz++; michael@0: if (c > '0' && c <= '9') { michael@0: s0 = s; michael@0: nf += nz; michael@0: nz = 0; michael@0: goto have_dig; michael@0: } michael@0: goto dig_done; michael@0: } michael@0: for(; c >= '0' && c <= '9'; c = *++s) { michael@0: have_dig: michael@0: nz++; michael@0: if (c -= '0') { michael@0: nf += nz; michael@0: for(i = 1; i < nz; i++) michael@0: if (nd++ < 9) michael@0: y *= 10; michael@0: else if (nd <= DBL_DIG + 1) michael@0: z *= 10; michael@0: if (nd++ < 9) michael@0: y = 10*y + c; michael@0: else if (nd <= DBL_DIG + 1) michael@0: z = 10*z + c; michael@0: nz = 0; michael@0: } michael@0: } michael@0: } michael@0: dig_done: michael@0: e = 0; michael@0: if (c == 'e' || c == 'E') { michael@0: if (!nd && !nz && !nz0) { michael@0: goto ret0; michael@0: } michael@0: s00 = s; michael@0: esign = 0; michael@0: switch(c = *++s) { michael@0: case '-': michael@0: esign = 1; michael@0: case '+': michael@0: c = *++s; michael@0: } michael@0: if (c >= '0' && c <= '9') { michael@0: while(c == '0') michael@0: c = *++s; michael@0: if (c > '0' && c <= '9') { michael@0: L = c - '0'; michael@0: s1 = s; michael@0: while((c = *++s) >= '0' && c <= '9') michael@0: L = 10*L + c - '0'; michael@0: if (s - s1 > 8 || L > 19999) michael@0: /* Avoid confusion from exponents michael@0: * so large that e might overflow. michael@0: */ michael@0: e = 19999; /* safe for 16 bit ints */ michael@0: else michael@0: e = (int)L; michael@0: if (esign) michael@0: e = -e; michael@0: } michael@0: else michael@0: e = 0; michael@0: } michael@0: else michael@0: s = s00; michael@0: } michael@0: if (!nd) { michael@0: if (!nz && !nz0) { michael@0: ret0: michael@0: s = s00; michael@0: sign = 0; michael@0: } michael@0: goto ret; michael@0: } michael@0: e1 = e -= nf; michael@0: michael@0: /* Now we have nd0 digits, starting at s0, followed by a michael@0: * decimal point, followed by nd-nd0 digits. The number we're michael@0: * after is the integer represented by those digits times michael@0: * 10**e */ michael@0: michael@0: if (!nd0) michael@0: nd0 = nd; michael@0: k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; michael@0: dval(rv) = y; michael@0: if (k > 9) { michael@0: #ifdef SET_INEXACT michael@0: if (k > DBL_DIG) michael@0: oldinexact = get_inexact(); michael@0: #endif michael@0: dval(rv) = tens[k - 9] * dval(rv) + z; michael@0: } michael@0: bd0 = 0; michael@0: if (nd <= DBL_DIG michael@0: #ifndef RND_PRODQUOT michael@0: #ifndef Honor_FLT_ROUNDS michael@0: && Flt_Rounds == 1 michael@0: #endif michael@0: #endif michael@0: ) { michael@0: if (!e) michael@0: goto ret; michael@0: if (e > 0) { michael@0: if (e <= Ten_pmax) { michael@0: #ifdef VAX michael@0: goto vax_ovfl_check; michael@0: #else michael@0: #ifdef Honor_FLT_ROUNDS michael@0: /* round correctly FLT_ROUNDS = 2 or 3 */ michael@0: if (sign) { michael@0: rv = -rv; michael@0: sign = 0; michael@0: } michael@0: #endif michael@0: /* rv = */ rounded_product(dval(rv), tens[e]); michael@0: goto ret; michael@0: #endif michael@0: } michael@0: i = DBL_DIG - nd; michael@0: if (e <= Ten_pmax + i) { michael@0: /* A fancier test would sometimes let us do michael@0: * this for larger i values. michael@0: */ michael@0: #ifdef Honor_FLT_ROUNDS michael@0: /* round correctly FLT_ROUNDS = 2 or 3 */ michael@0: if (sign) { michael@0: rv = -rv; michael@0: sign = 0; michael@0: } michael@0: #endif michael@0: e -= i; michael@0: dval(rv) *= tens[i]; michael@0: #ifdef VAX michael@0: /* VAX exponent range is so narrow we must michael@0: * worry about overflow here... michael@0: */ michael@0: vax_ovfl_check: michael@0: word0(rv) -= P*Exp_msk1; michael@0: /* rv = */ rounded_product(dval(rv), tens[e]); michael@0: if ((word0(rv) & Exp_mask) michael@0: > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) michael@0: goto ovfl; michael@0: word0(rv) += P*Exp_msk1; michael@0: #else michael@0: /* rv = */ rounded_product(dval(rv), tens[e]); michael@0: #endif michael@0: goto ret; michael@0: } michael@0: } michael@0: #ifndef Inaccurate_Divide michael@0: else if (e >= -Ten_pmax) { michael@0: #ifdef Honor_FLT_ROUNDS michael@0: /* round correctly FLT_ROUNDS = 2 or 3 */ michael@0: if (sign) { michael@0: rv = -rv; michael@0: sign = 0; michael@0: } michael@0: #endif michael@0: /* rv = */ rounded_quotient(dval(rv), tens[-e]); michael@0: goto ret; michael@0: } michael@0: #endif michael@0: } michael@0: e1 += nd - k; michael@0: michael@0: #ifdef IEEE_Arith michael@0: #ifdef SET_INEXACT michael@0: inexact = 1; michael@0: if (k <= DBL_DIG) michael@0: oldinexact = get_inexact(); michael@0: #endif michael@0: #ifdef Avoid_Underflow michael@0: scale = 0; michael@0: #endif michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if ((rounding = Flt_Rounds) >= 2) { michael@0: if (sign) michael@0: rounding = rounding == 2 ? 0 : 2; michael@0: else michael@0: if (rounding != 2) michael@0: rounding = 0; michael@0: } michael@0: #endif michael@0: #endif /*IEEE_Arith*/ michael@0: michael@0: /* Get starting approximation = rv * 10**e1 */ michael@0: michael@0: if (e1 > 0) { michael@0: if ((i = e1 & 15)) michael@0: dval(rv) *= tens[i]; michael@0: if (e1 &= ~15) { michael@0: if (e1 > DBL_MAX_10_EXP) { michael@0: ovfl: michael@0: #ifndef NO_ERRNO michael@0: errno = ERANGE; michael@0: #endif michael@0: /* Can't trust HUGE_VAL */ michael@0: #ifdef IEEE_Arith michael@0: #ifdef Honor_FLT_ROUNDS michael@0: switch(rounding) { michael@0: case 0: /* toward 0 */ michael@0: case 3: /* toward -infinity */ michael@0: word0(rv) = Big0; michael@0: word1(rv) = Big1; michael@0: break; michael@0: default: michael@0: word0(rv) = Exp_mask; michael@0: word1(rv) = 0; michael@0: } michael@0: #else /*Honor_FLT_ROUNDS*/ michael@0: word0(rv) = Exp_mask; michael@0: word1(rv) = 0; michael@0: #endif /*Honor_FLT_ROUNDS*/ michael@0: #ifdef SET_INEXACT michael@0: /* set overflow bit */ michael@0: dval(rv0) = 1e300; michael@0: dval(rv0) *= dval(rv0); michael@0: #endif michael@0: #else /*IEEE_Arith*/ michael@0: word0(rv) = Big0; michael@0: word1(rv) = Big1; michael@0: #endif /*IEEE_Arith*/ michael@0: if (bd0) michael@0: goto retfree; michael@0: goto ret; michael@0: } michael@0: e1 >>= 4; michael@0: for(j = 0; e1 > 1; j++, e1 >>= 1) michael@0: if (e1 & 1) michael@0: dval(rv) *= bigtens[j]; michael@0: /* The last multiplication could overflow. */ michael@0: word0(rv) -= P*Exp_msk1; michael@0: dval(rv) *= bigtens[j]; michael@0: if ((z = word0(rv) & Exp_mask) michael@0: > Exp_msk1*(DBL_MAX_EXP+Bias-P)) michael@0: goto ovfl; michael@0: if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { michael@0: /* set to largest number */ michael@0: /* (Can't trust DBL_MAX) */ michael@0: word0(rv) = Big0; michael@0: word1(rv) = Big1; michael@0: } michael@0: else michael@0: word0(rv) += P*Exp_msk1; michael@0: } michael@0: } michael@0: else if (e1 < 0) { michael@0: e1 = -e1; michael@0: if ((i = e1 & 15)) michael@0: dval(rv) /= tens[i]; michael@0: if (e1 >>= 4) { michael@0: if (e1 >= 1 << n_bigtens) michael@0: goto undfl; michael@0: #ifdef Avoid_Underflow michael@0: if (e1 & Scale_Bit) michael@0: scale = 2*P; michael@0: for(j = 0; e1 > 0; j++, e1 >>= 1) michael@0: if (e1 & 1) michael@0: dval(rv) *= tinytens[j]; michael@0: if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask) michael@0: >> Exp_shift)) > 0) { michael@0: /* scaled rv is denormal; zap j low bits */ michael@0: if (j >= 32) { michael@0: word1(rv) = 0; michael@0: if (j >= 53) michael@0: word0(rv) = (P+2)*Exp_msk1; michael@0: else michael@0: word0(rv) &= 0xffffffff << (j-32); michael@0: } michael@0: else michael@0: word1(rv) &= 0xffffffff << j; michael@0: } michael@0: #else michael@0: for(j = 0; e1 > 1; j++, e1 >>= 1) michael@0: if (e1 & 1) michael@0: dval(rv) *= tinytens[j]; michael@0: /* The last multiplication could underflow. */ michael@0: dval(rv0) = dval(rv); michael@0: dval(rv) *= tinytens[j]; michael@0: if (!dval(rv)) { michael@0: dval(rv) = 2.*dval(rv0); michael@0: dval(rv) *= tinytens[j]; michael@0: #endif michael@0: if (!dval(rv)) { michael@0: undfl: michael@0: dval(rv) = 0.; michael@0: #ifndef NO_ERRNO michael@0: errno = ERANGE; michael@0: #endif michael@0: if (bd0) michael@0: goto retfree; michael@0: goto ret; michael@0: } michael@0: #ifndef Avoid_Underflow michael@0: word0(rv) = Tiny0; michael@0: word1(rv) = Tiny1; michael@0: /* The refinement below will clean michael@0: * this approximation up. michael@0: */ michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: /* Now the hard part -- adjusting rv to the correct value.*/ michael@0: michael@0: /* Put digits into bd: true value = bd * 10^e */ michael@0: michael@0: bd0 = s2b(PASS_STATE s0, nd0, nd, y); michael@0: michael@0: for(;;) { michael@0: bd = Balloc(PASS_STATE bd0->k); michael@0: Bcopy(bd, bd0); michael@0: bb = d2b(PASS_STATE rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ michael@0: bs = i2b(PASS_STATE 1); michael@0: michael@0: if (e >= 0) { michael@0: bb2 = bb5 = 0; michael@0: bd2 = bd5 = e; michael@0: } michael@0: else { michael@0: bb2 = bb5 = -e; michael@0: bd2 = bd5 = 0; michael@0: } michael@0: if (bbe >= 0) michael@0: bb2 += bbe; michael@0: else michael@0: bd2 -= bbe; michael@0: bs2 = bb2; michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (rounding != 1) michael@0: bs2++; michael@0: #endif michael@0: #ifdef Avoid_Underflow michael@0: j = bbe - scale; michael@0: i = j + bbbits - 1; /* logb(rv) */ michael@0: if (i < Emin) /* denormal */ michael@0: j += P - Emin; michael@0: else michael@0: j = P + 1 - bbbits; michael@0: #else /*Avoid_Underflow*/ michael@0: #ifdef Sudden_Underflow michael@0: #ifdef IBM michael@0: j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); michael@0: #else michael@0: j = P + 1 - bbbits; michael@0: #endif michael@0: #else /*Sudden_Underflow*/ michael@0: j = bbe; michael@0: i = j + bbbits - 1; /* logb(rv) */ michael@0: if (i < Emin) /* denormal */ michael@0: j += P - Emin; michael@0: else michael@0: j = P + 1 - bbbits; michael@0: #endif /*Sudden_Underflow*/ michael@0: #endif /*Avoid_Underflow*/ michael@0: bb2 += j; michael@0: bd2 += j; michael@0: #ifdef Avoid_Underflow michael@0: bd2 += scale; michael@0: #endif michael@0: i = bb2 < bd2 ? bb2 : bd2; michael@0: if (i > bs2) michael@0: i = bs2; michael@0: if (i > 0) { michael@0: bb2 -= i; michael@0: bd2 -= i; michael@0: bs2 -= i; michael@0: } michael@0: if (bb5 > 0) { michael@0: bs = pow5mult(PASS_STATE bs, bb5); michael@0: bb1 = mult(PASS_STATE bs, bb); michael@0: Bfree(PASS_STATE bb); michael@0: bb = bb1; michael@0: } michael@0: if (bb2 > 0) michael@0: bb = lshift(PASS_STATE bb, bb2); michael@0: if (bd5 > 0) michael@0: bd = pow5mult(PASS_STATE bd, bd5); michael@0: if (bd2 > 0) michael@0: bd = lshift(PASS_STATE bd, bd2); michael@0: if (bs2 > 0) michael@0: bs = lshift(PASS_STATE bs, bs2); michael@0: delta = diff(PASS_STATE bb, bd); michael@0: dsign = delta->sign; michael@0: delta->sign = 0; michael@0: i = cmp(delta, bs); michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (rounding != 1) { michael@0: if (i < 0) { michael@0: /* Error is less than an ulp */ michael@0: if (!delta->x[0] && delta->wds <= 1) { michael@0: /* exact */ michael@0: #ifdef SET_INEXACT michael@0: inexact = 0; michael@0: #endif michael@0: break; michael@0: } michael@0: if (rounding) { michael@0: if (dsign) { michael@0: adj = 1.; michael@0: goto apply_adj; michael@0: } michael@0: } michael@0: else if (!dsign) { michael@0: adj = -1.; michael@0: if (!word1(rv) michael@0: && !(word0(rv) & Frac_mask)) { michael@0: y = word0(rv) & Exp_mask; michael@0: #ifdef Avoid_Underflow michael@0: if (!scale || y > 2*P*Exp_msk1) michael@0: #else michael@0: if (y) michael@0: #endif michael@0: { michael@0: delta = lshift(PASS_STATE delta,Log2P); michael@0: if (cmp(delta, bs) <= 0) michael@0: adj = -0.5; michael@0: } michael@0: } michael@0: apply_adj: michael@0: #ifdef Avoid_Underflow michael@0: if (scale && (y = word0(rv) & Exp_mask) michael@0: <= 2*P*Exp_msk1) michael@0: word0(adj) += (2*P+1)*Exp_msk1 - y; michael@0: #else michael@0: #ifdef Sudden_Underflow michael@0: if ((word0(rv) & Exp_mask) <= michael@0: P*Exp_msk1) { michael@0: word0(rv) += P*Exp_msk1; michael@0: dval(rv) += adj*ulp(rv); michael@0: word0(rv) -= P*Exp_msk1; michael@0: } michael@0: else michael@0: #endif /*Sudden_Underflow*/ michael@0: #endif /*Avoid_Underflow*/ michael@0: dval(rv) += adj*ulp(rv); michael@0: } michael@0: break; michael@0: } michael@0: adj = ratio(delta, bs); michael@0: if (adj < 1.) michael@0: adj = 1.; michael@0: if (adj <= 0x7ffffffe) { michael@0: /* adj = rounding ? ceil(adj) : floor(adj); */ michael@0: y = adj; michael@0: if (y != adj) { michael@0: if (!((rounding>>1) ^ dsign)) michael@0: y++; michael@0: adj = y; michael@0: } michael@0: } michael@0: #ifdef Avoid_Underflow michael@0: if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) michael@0: word0(adj) += (2*P+1)*Exp_msk1 - y; michael@0: #else michael@0: #ifdef Sudden_Underflow michael@0: if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { michael@0: word0(rv) += P*Exp_msk1; michael@0: adj *= ulp(rv); michael@0: if (dsign) michael@0: dval(rv) += adj; michael@0: else michael@0: dval(rv) -= adj; michael@0: word0(rv) -= P*Exp_msk1; michael@0: goto cont; michael@0: } michael@0: #endif /*Sudden_Underflow*/ michael@0: #endif /*Avoid_Underflow*/ michael@0: adj *= ulp(rv); michael@0: if (dsign) michael@0: dval(rv) += adj; michael@0: else michael@0: dval(rv) -= adj; michael@0: goto cont; michael@0: } michael@0: #endif /*Honor_FLT_ROUNDS*/ michael@0: michael@0: if (i < 0) { michael@0: /* Error is less than half an ulp -- check for michael@0: * special case of mantissa a power of two. michael@0: */ michael@0: if (dsign || word1(rv) || word0(rv) & Bndry_mask michael@0: #ifdef IEEE_Arith michael@0: #ifdef Avoid_Underflow michael@0: || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1 michael@0: #else michael@0: || (word0(rv) & Exp_mask) <= Exp_msk1 michael@0: #endif michael@0: #endif michael@0: ) { michael@0: #ifdef SET_INEXACT michael@0: if (!delta->x[0] && delta->wds <= 1) michael@0: inexact = 0; michael@0: #endif michael@0: break; michael@0: } michael@0: if (!delta->x[0] && delta->wds <= 1) { michael@0: /* exact result */ michael@0: #ifdef SET_INEXACT michael@0: inexact = 0; michael@0: #endif michael@0: break; michael@0: } michael@0: delta = lshift(PASS_STATE delta,Log2P); michael@0: if (cmp(delta, bs) > 0) michael@0: goto drop_down; michael@0: break; michael@0: } michael@0: if (i == 0) { michael@0: /* exactly half-way between */ michael@0: if (dsign) { michael@0: if ((word0(rv) & Bndry_mask1) == Bndry_mask1 michael@0: && word1(rv) == ( michael@0: #ifdef Avoid_Underflow michael@0: (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) michael@0: ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : michael@0: #endif michael@0: 0xffffffff)) { michael@0: /*boundary case -- increment exponent*/ michael@0: word0(rv) = (word0(rv) & Exp_mask) michael@0: + Exp_msk1 michael@0: #ifdef IBM michael@0: | Exp_msk1 >> 4 michael@0: #endif michael@0: ; michael@0: word1(rv) = 0; michael@0: #ifdef Avoid_Underflow michael@0: dsign = 0; michael@0: #endif michael@0: break; michael@0: } michael@0: } michael@0: else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { michael@0: drop_down: michael@0: /* boundary case -- decrement exponent */ michael@0: #ifdef Sudden_Underflow /*{{*/ michael@0: L = word0(rv) & Exp_mask; michael@0: #ifdef IBM michael@0: if (L < Exp_msk1) michael@0: #else michael@0: #ifdef Avoid_Underflow michael@0: if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) michael@0: #else michael@0: if (L <= Exp_msk1) michael@0: #endif /*Avoid_Underflow*/ michael@0: #endif /*IBM*/ michael@0: goto undfl; michael@0: L -= Exp_msk1; michael@0: #else /*Sudden_Underflow}{*/ michael@0: #ifdef Avoid_Underflow michael@0: if (scale) { michael@0: L = word0(rv) & Exp_mask; michael@0: if (L <= (2*P+1)*Exp_msk1) { michael@0: if (L > (P+2)*Exp_msk1) michael@0: /* round even ==> */ michael@0: /* accept rv */ michael@0: break; michael@0: /* rv = smallest denormal */ michael@0: goto undfl; michael@0: } michael@0: } michael@0: #endif /*Avoid_Underflow*/ michael@0: L = (word0(rv) & Exp_mask) - Exp_msk1; michael@0: #endif /*Sudden_Underflow}}*/ michael@0: word0(rv) = L | Bndry_mask1; michael@0: word1(rv) = 0xffffffff; michael@0: #ifdef IBM michael@0: goto cont; michael@0: #else michael@0: break; michael@0: #endif michael@0: } michael@0: #ifndef ROUND_BIASED michael@0: if (!(word1(rv) & LSB)) michael@0: break; michael@0: #endif michael@0: if (dsign) michael@0: dval(rv) += ulp(rv); michael@0: #ifndef ROUND_BIASED michael@0: else { michael@0: dval(rv) -= ulp(rv); michael@0: #ifndef Sudden_Underflow michael@0: if (!dval(rv)) michael@0: goto undfl; michael@0: #endif michael@0: } michael@0: #ifdef Avoid_Underflow michael@0: dsign = 1 - dsign; michael@0: #endif michael@0: #endif michael@0: break; michael@0: } michael@0: if ((aadj = ratio(delta, bs)) <= 2.) { michael@0: if (dsign) michael@0: aadj = dval(aadj1) = 1.; michael@0: else if (word1(rv) || word0(rv) & Bndry_mask) { michael@0: #ifndef Sudden_Underflow michael@0: if (word1(rv) == Tiny1 && !word0(rv)) michael@0: goto undfl; michael@0: #endif michael@0: aadj = 1.; michael@0: dval(aadj1) = -1.; michael@0: } michael@0: else { michael@0: /* special case -- power of FLT_RADIX to be */ michael@0: /* rounded down... */ michael@0: michael@0: if (aadj < 2./FLT_RADIX) michael@0: aadj = 1./FLT_RADIX; michael@0: else michael@0: aadj *= 0.5; michael@0: dval(aadj1) = -aadj; michael@0: } michael@0: } michael@0: else { michael@0: aadj *= 0.5; michael@0: dval(aadj1) = dsign ? aadj : -aadj; michael@0: #ifdef Check_FLT_ROUNDS michael@0: switch(Rounding) { michael@0: case 2: /* towards +infinity */ michael@0: dval(aadj1) -= 0.5; michael@0: break; michael@0: case 0: /* towards 0 */ michael@0: case 3: /* towards -infinity */ michael@0: dval(aadj1) += 0.5; michael@0: } michael@0: #else michael@0: if (Flt_Rounds == 0) michael@0: dval(aadj1) += 0.5; michael@0: #endif /*Check_FLT_ROUNDS*/ michael@0: } michael@0: y = word0(rv) & Exp_mask; michael@0: michael@0: /* Check for overflow */ michael@0: michael@0: if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { michael@0: dval(rv0) = dval(rv); michael@0: word0(rv) -= P*Exp_msk1; michael@0: adj = dval(aadj1) * ulp(rv); michael@0: dval(rv) += adj; michael@0: if ((word0(rv) & Exp_mask) >= michael@0: Exp_msk1*(DBL_MAX_EXP+Bias-P)) { michael@0: if (word0(rv0) == Big0 && word1(rv0) == Big1) michael@0: goto ovfl; michael@0: word0(rv) = Big0; michael@0: word1(rv) = Big1; michael@0: goto cont; michael@0: } michael@0: else michael@0: word0(rv) += P*Exp_msk1; michael@0: } michael@0: else { michael@0: #ifdef Avoid_Underflow michael@0: if (scale && y <= 2*P*Exp_msk1) { michael@0: if (aadj <= 0x7fffffff) { michael@0: if ((z = (ULong) aadj) <= 0) michael@0: z = 1; michael@0: aadj = z; michael@0: dval(aadj1) = dsign ? aadj : -aadj; michael@0: } michael@0: word0(aadj1) += (2*P+1)*Exp_msk1 - y; michael@0: } michael@0: adj = dval(aadj1) * ulp(rv); michael@0: dval(rv) += adj; michael@0: #else michael@0: #ifdef Sudden_Underflow michael@0: if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { michael@0: dval(rv0) = dval(rv); michael@0: word0(rv) += P*Exp_msk1; michael@0: adj = dval(aadj1) * ulp(rv); michael@0: dval(rv) += adj; michael@0: #ifdef IBM michael@0: if ((word0(rv) & Exp_mask) < P*Exp_msk1) michael@0: #else michael@0: if ((word0(rv) & Exp_mask) <= P*Exp_msk1) michael@0: #endif michael@0: { michael@0: if (word0(rv0) == Tiny0 michael@0: && word1(rv0) == Tiny1) michael@0: goto undfl; michael@0: word0(rv) = Tiny0; michael@0: word1(rv) = Tiny1; michael@0: goto cont; michael@0: } michael@0: else michael@0: word0(rv) -= P*Exp_msk1; michael@0: } michael@0: else { michael@0: adj = dval(aadj1) * ulp(rv); michael@0: dval(rv) += adj; michael@0: } michael@0: #else /*Sudden_Underflow*/ michael@0: /* Compute adj so that the IEEE rounding rules will michael@0: * correctly round rv + adj in some half-way cases. michael@0: * If rv * ulp(rv) is denormalized (i.e., michael@0: * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid michael@0: * trouble from bits lost to denormalization; michael@0: * example: 1.2e-307 . michael@0: */ michael@0: if (y <= (P-1)*Exp_msk1 && aadj > 1.) { michael@0: dval(aadj1) = (double)(int)(aadj + 0.5); michael@0: if (!dsign) michael@0: dval(aadj1) = -dval(aadj1); michael@0: } michael@0: adj = dval(aadj1) * ulp(rv); michael@0: dval(rv) += adj; michael@0: #endif /*Sudden_Underflow*/ michael@0: #endif /*Avoid_Underflow*/ michael@0: } michael@0: z = word0(rv) & Exp_mask; michael@0: #ifndef SET_INEXACT michael@0: #ifdef Avoid_Underflow michael@0: if (!scale) michael@0: #endif michael@0: if (y == z) { michael@0: /* Can we stop now? */ michael@0: L = (Long)aadj; michael@0: aadj -= L; michael@0: /* The tolerances below are conservative. */ michael@0: if (dsign || word1(rv) || word0(rv) & Bndry_mask) { michael@0: if (aadj < .4999999 || aadj > .5000001) michael@0: break; michael@0: } michael@0: else if (aadj < .4999999/FLT_RADIX) michael@0: break; michael@0: } michael@0: #endif michael@0: cont: michael@0: Bfree(PASS_STATE bb); michael@0: Bfree(PASS_STATE bd); michael@0: Bfree(PASS_STATE bs); michael@0: Bfree(PASS_STATE delta); michael@0: } michael@0: #ifdef SET_INEXACT michael@0: if (inexact) { michael@0: if (!oldinexact) { michael@0: word0(rv0) = Exp_1 + (70 << Exp_shift); michael@0: word1(rv0) = 0; michael@0: dval(rv0) += 1.; michael@0: } michael@0: } michael@0: else if (!oldinexact) michael@0: clear_inexact(); michael@0: #endif michael@0: #ifdef Avoid_Underflow michael@0: if (scale) { michael@0: word0(rv0) = Exp_1 - 2*P*Exp_msk1; michael@0: word1(rv0) = 0; michael@0: dval(rv) *= dval(rv0); michael@0: #ifndef NO_ERRNO michael@0: /* try to avoid the bug of testing an 8087 register value */ michael@0: if (word0(rv) == 0 && word1(rv) == 0) michael@0: errno = ERANGE; michael@0: #endif michael@0: } michael@0: #endif /* Avoid_Underflow */ michael@0: #ifdef SET_INEXACT michael@0: if (inexact && !(word0(rv) & Exp_mask)) { michael@0: /* set underflow bit */ michael@0: dval(rv0) = 1e-300; michael@0: dval(rv0) *= dval(rv0); michael@0: } michael@0: #endif michael@0: retfree: michael@0: Bfree(PASS_STATE bb); michael@0: Bfree(PASS_STATE bd); michael@0: Bfree(PASS_STATE bs); michael@0: Bfree(PASS_STATE bd0); michael@0: Bfree(PASS_STATE delta); michael@0: ret: michael@0: if (se) michael@0: *se = (char *)s; michael@0: return sign ? -dval(rv) : dval(rv); michael@0: } michael@0: michael@0: static int michael@0: quorem michael@0: #ifdef KR_headers michael@0: (b, S) Bigint *b, *S; michael@0: #else michael@0: (Bigint *b, Bigint *S) michael@0: #endif michael@0: { michael@0: int n; michael@0: ULong *bx, *bxe, q, *sx, *sxe; michael@0: #ifdef ULLong michael@0: ULLong borrow, carry, y, ys; michael@0: #else michael@0: ULong borrow, carry, y, ys; michael@0: #ifdef Pack_32 michael@0: ULong si, z, zs; michael@0: #endif michael@0: #endif michael@0: michael@0: n = S->wds; michael@0: #ifdef DEBUG michael@0: /*debug*/ if (b->wds > n) michael@0: /*debug*/ Bug("oversize b in quorem"); michael@0: #endif michael@0: if (b->wds < n) michael@0: return 0; michael@0: sx = S->x; michael@0: sxe = sx + --n; michael@0: bx = b->x; michael@0: bxe = bx + n; michael@0: q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ michael@0: #ifdef DEBUG michael@0: /*debug*/ if (q > 9) michael@0: /*debug*/ Bug("oversized quotient in quorem"); michael@0: #endif michael@0: if (q) { michael@0: borrow = 0; michael@0: carry = 0; michael@0: do { michael@0: #ifdef ULLong michael@0: ys = *sx++ * (ULLong)q + carry; michael@0: carry = ys >> 32; michael@0: y = *bx - (ys & FFFFFFFF) - borrow; michael@0: borrow = y >> 32 & (ULong)1; michael@0: *bx++ = (ULong) y & FFFFFFFF; michael@0: #else michael@0: #ifdef Pack_32 michael@0: si = *sx++; michael@0: ys = (si & 0xffff) * q + carry; michael@0: zs = (si >> 16) * q + (ys >> 16); michael@0: carry = zs >> 16; michael@0: y = (*bx & 0xffff) - (ys & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: z = (*bx >> 16) - (zs & 0xffff) - borrow; michael@0: borrow = (z & 0x10000) >> 16; michael@0: Storeinc(bx, z, y); michael@0: #else michael@0: ys = *sx++ * q + carry; michael@0: carry = ys >> 16; michael@0: y = *bx - (ys & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: *bx++ = y & 0xffff; michael@0: #endif michael@0: #endif michael@0: } michael@0: while(sx <= sxe); michael@0: if (!*bxe) { michael@0: bx = b->x; michael@0: while(--bxe > bx && !*bxe) michael@0: --n; michael@0: b->wds = n; michael@0: } michael@0: } michael@0: if (cmp(b, S) >= 0) { michael@0: q++; michael@0: borrow = 0; michael@0: carry = 0; michael@0: bx = b->x; michael@0: sx = S->x; michael@0: do { michael@0: #ifdef ULLong michael@0: ys = *sx++ + carry; michael@0: carry = ys >> 32; michael@0: y = *bx - (ys & FFFFFFFF) - borrow; michael@0: borrow = y >> 32 & (ULong)1; michael@0: *bx++ = (ULong) y & FFFFFFFF; michael@0: #else michael@0: #ifdef Pack_32 michael@0: si = *sx++; michael@0: ys = (si & 0xffff) + carry; michael@0: zs = (si >> 16) + (ys >> 16); michael@0: carry = zs >> 16; michael@0: y = (*bx & 0xffff) - (ys & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: z = (*bx >> 16) - (zs & 0xffff) - borrow; michael@0: borrow = (z & 0x10000) >> 16; michael@0: Storeinc(bx, z, y); michael@0: #else michael@0: ys = *sx++ + carry; michael@0: carry = ys >> 16; michael@0: y = *bx - (ys & 0xffff) - borrow; michael@0: borrow = (y & 0x10000) >> 16; michael@0: *bx++ = y & 0xffff; michael@0: #endif michael@0: #endif michael@0: } michael@0: while(sx <= sxe); michael@0: bx = b->x; michael@0: bxe = bx + n; michael@0: if (!*bxe) { michael@0: while(--bxe > bx && !*bxe) michael@0: --n; michael@0: b->wds = n; michael@0: } michael@0: } michael@0: return q; michael@0: } michael@0: michael@0: #if !defined(MULTIPLE_THREADS) && !defined(NO_GLOBAL_STATE) michael@0: #define USE_DTOA_RESULT 1 michael@0: static char *dtoa_result; michael@0: #endif michael@0: michael@0: static char * michael@0: #ifdef KR_headers michael@0: rv_alloc(STATE_PARAM i) STATE_PARAM_DECL int i; michael@0: #else michael@0: rv_alloc(STATE_PARAM int i) michael@0: #endif michael@0: { michael@0: int j, k, *r; michael@0: michael@0: j = sizeof(ULong); michael@0: for(k = 0; michael@0: sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned) i; michael@0: j <<= 1) michael@0: k++; michael@0: r = (int*)Balloc(PASS_STATE k); michael@0: *r = k; michael@0: return michael@0: #ifdef USE_DTOA_RESULT michael@0: dtoa_result = michael@0: #endif michael@0: (char *)(r+1); michael@0: } michael@0: michael@0: static char * michael@0: #ifdef KR_headers michael@0: nrv_alloc(STATE_PARAM s, rve, n) STATE_PARAM_DECL char *s, **rve; int n; michael@0: #else michael@0: nrv_alloc(STATE_PARAM CONST char *s, char **rve, int n) michael@0: #endif michael@0: { michael@0: char *rv, *t; michael@0: michael@0: t = rv = rv_alloc(PASS_STATE n); michael@0: while((*t = *s++)) t++; michael@0: if (rve) michael@0: *rve = t; michael@0: return rv; michael@0: } michael@0: michael@0: /* freedtoa(s) must be used to free values s returned by dtoa michael@0: * when MULTIPLE_THREADS is #defined. It should be used in all cases, michael@0: * but for consistency with earlier versions of dtoa, it is optional michael@0: * when MULTIPLE_THREADS is not defined. michael@0: */ michael@0: michael@0: static void michael@0: #ifdef KR_headers michael@0: freedtoa(STATE_PARAM s) STATE_PARAM_DECL char *s; michael@0: #else michael@0: freedtoa(STATE_PARAM char *s) michael@0: #endif michael@0: { michael@0: Bigint *b = (Bigint *)((int *)s - 1); michael@0: b->maxwds = 1 << (b->k = *(int*)b); michael@0: Bfree(PASS_STATE b); michael@0: #ifdef USE_DTOA_RESULT michael@0: if (s == dtoa_result) michael@0: dtoa_result = 0; michael@0: #endif michael@0: } michael@0: michael@0: /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. michael@0: * michael@0: * Inspired by "How to Print Floating-Point Numbers Accurately" by michael@0: * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. michael@0: * michael@0: * Modifications: michael@0: * 1. Rather than iterating, we use a simple numeric overestimate michael@0: * to determine k = floor(log10(d)). We scale relevant michael@0: * quantities using O(log2(k)) rather than O(k) multiplications. michael@0: * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't michael@0: * try to generate digits strictly left to right. Instead, we michael@0: * compute with fewer bits and propagate the carry if necessary michael@0: * when rounding the final digit up. This is often faster. michael@0: * 3. Under the assumption that input will be rounded nearest, michael@0: * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. michael@0: * That is, we allow equality in stopping tests when the michael@0: * round-nearest rule will give the same floating-point value michael@0: * as would satisfaction of the stopping test with strict michael@0: * inequality. michael@0: * 4. We remove common factors of powers of 2 from relevant michael@0: * quantities. michael@0: * 5. When converting floating-point integers less than 1e16, michael@0: * we use floating-point arithmetic rather than resorting michael@0: * to multiple-precision integers. michael@0: * 6. When asked to produce fewer than 15 digits, we first try michael@0: * to get by with floating-point arithmetic; we resort to michael@0: * multiple-precision integer arithmetic only if we cannot michael@0: * guarantee that the floating-point calculation has given michael@0: * the correctly rounded result. For k requested digits and michael@0: * "uniformly" distributed input, the probability is michael@0: * something like 10^(k-15) that we must resort to the Long michael@0: * calculation. michael@0: */ michael@0: michael@0: static char * michael@0: dtoa michael@0: #ifdef KR_headers michael@0: (STATE_PARAM d, mode, ndigits, decpt, sign, rve) michael@0: STATE_PARAM_DECL U d; int mode, ndigits, *decpt, *sign; char **rve; michael@0: #else michael@0: (STATE_PARAM U d, int mode, int ndigits, int *decpt, int *sign, char **rve) michael@0: #endif michael@0: { michael@0: /* Arguments ndigits, decpt, sign are similar to those michael@0: of ecvt and fcvt; trailing zeros are suppressed from michael@0: the returned string. If not null, *rve is set to point michael@0: to the end of the return value. If d is +-Infinity or NaN, michael@0: then *decpt is set to 9999. michael@0: michael@0: mode: michael@0: 0 ==> shortest string that yields d when read in michael@0: and rounded to nearest. michael@0: 1 ==> like 0, but with Steele & White stopping rule; michael@0: e.g. with IEEE P754 arithmetic , mode 0 gives michael@0: 1e23 whereas mode 1 gives 9.999999999999999e22. michael@0: 2 ==> max(1,ndigits) significant digits. This gives a michael@0: return value similar to that of ecvt, except michael@0: that trailing zeros are suppressed. michael@0: 3 ==> through ndigits past the decimal point. This michael@0: gives a return value similar to that from fcvt, michael@0: except that trailing zeros are suppressed, and michael@0: ndigits can be negative. michael@0: 4,5 ==> similar to 2 and 3, respectively, but (in michael@0: round-nearest mode) with the tests of mode 0 to michael@0: possibly return a shorter string that rounds to d. michael@0: With IEEE arithmetic and compilation with michael@0: -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same michael@0: as modes 2 and 3 when FLT_ROUNDS != 1. michael@0: 6-9 ==> Debugging modes similar to mode - 4: don't try michael@0: fast floating-point estimate (if applicable). michael@0: michael@0: Values of mode other than 0-9 are treated as mode 0. michael@0: michael@0: Sufficient space is allocated to the return value michael@0: to hold the suppressed trailing zeros. michael@0: */ michael@0: michael@0: int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, michael@0: j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, michael@0: spec_case, try_quick; michael@0: Long L; michael@0: #ifndef Sudden_Underflow michael@0: int denorm; michael@0: ULong x; michael@0: #endif michael@0: Bigint *b, *b1, *delta, *mlo, *mhi, *S; michael@0: U d2, eps; michael@0: double ds; michael@0: char *s, *s0; michael@0: #ifdef Honor_FLT_ROUNDS michael@0: int rounding; michael@0: #endif michael@0: #ifdef SET_INEXACT michael@0: int inexact, oldinexact; michael@0: #endif michael@0: michael@0: #ifdef __GNUC__ michael@0: ilim = ilim1 = 0; michael@0: mlo = NULL; michael@0: #endif michael@0: michael@0: #ifdef USE_DTOA_RESULT michael@0: if (dtoa_result) { michael@0: freedtoa(PASS_STATE dtoa_result); michael@0: dtoa_result = 0; michael@0: } michael@0: #endif michael@0: michael@0: if (word0(d) & Sign_bit) { michael@0: /* set sign for everything, including 0's and NaNs */ michael@0: *sign = 1; michael@0: word0(d) &= ~Sign_bit; /* clear sign bit */ michael@0: } michael@0: else michael@0: *sign = 0; michael@0: michael@0: #if defined(IEEE_Arith) + defined(VAX) michael@0: #ifdef IEEE_Arith michael@0: if ((word0(d) & Exp_mask) == Exp_mask) michael@0: #else michael@0: if (word0(d) == 0x8000) michael@0: #endif michael@0: { michael@0: /* Infinity or NaN */ michael@0: *decpt = 9999; michael@0: #ifdef IEEE_Arith michael@0: if (!word1(d) && !(word0(d) & 0xfffff)) michael@0: return nrv_alloc(PASS_STATE "Infinity", rve, 8); michael@0: #endif michael@0: return nrv_alloc(PASS_STATE "NaN", rve, 3); michael@0: } michael@0: #endif michael@0: #ifdef IBM michael@0: dval(d) += 0; /* normalize */ michael@0: #endif michael@0: if (!dval(d)) { michael@0: *decpt = 1; michael@0: return nrv_alloc(PASS_STATE "0", rve, 1); michael@0: } michael@0: michael@0: #ifdef SET_INEXACT michael@0: try_quick = oldinexact = get_inexact(); michael@0: inexact = 1; michael@0: #endif michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if ((rounding = Flt_Rounds) >= 2) { michael@0: if (*sign) michael@0: rounding = rounding == 2 ? 0 : 2; michael@0: else michael@0: if (rounding != 2) michael@0: rounding = 0; michael@0: } michael@0: #endif michael@0: michael@0: b = d2b(PASS_STATE d, &be, &bbits); michael@0: #ifdef Sudden_Underflow michael@0: i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); michael@0: #else michael@0: if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) { michael@0: #endif michael@0: dval(d2) = dval(d); michael@0: word0(d2) &= Frac_mask1; michael@0: word0(d2) |= Exp_11; michael@0: #ifdef IBM michael@0: if (j = 11 - hi0bits(word0(d2) & Frac_mask)) michael@0: dval(d2) /= 1 << j; michael@0: #endif michael@0: michael@0: /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 michael@0: * log10(x) = log(x) / log(10) michael@0: * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) michael@0: * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) michael@0: * michael@0: * This suggests computing an approximation k to log10(d) by michael@0: * michael@0: * k = (i - Bias)*0.301029995663981 michael@0: * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); michael@0: * michael@0: * We want k to be too large rather than too small. michael@0: * The error in the first-order Taylor series approximation michael@0: * is in our favor, so we just round up the constant enough michael@0: * to compensate for any error in the multiplication of michael@0: * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, michael@0: * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, michael@0: * adding 1e-13 to the constant term more than suffices. michael@0: * Hence we adjust the constant term to 0.1760912590558. michael@0: * (We could get a more accurate k by invoking log10, michael@0: * but this is probably not worthwhile.) michael@0: */ michael@0: michael@0: i -= Bias; michael@0: #ifdef IBM michael@0: i <<= 2; michael@0: i += j; michael@0: #endif michael@0: #ifndef Sudden_Underflow michael@0: denorm = 0; michael@0: } michael@0: else { michael@0: /* d is denormalized */ michael@0: michael@0: i = bbits + be + (Bias + (P-1) - 1); michael@0: x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32) michael@0: : word1(d) << (32 - i); michael@0: dval(d2) = x; michael@0: word0(d2) -= 31*Exp_msk1; /* adjust exponent */ michael@0: i -= (Bias + (P-1) - 1) + 1; michael@0: denorm = 1; michael@0: } michael@0: #endif michael@0: ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; michael@0: k = (int)ds; michael@0: if (ds < 0. && ds != k) michael@0: k--; /* want k = floor(ds) */ michael@0: k_check = 1; michael@0: if (k >= 0 && k <= Ten_pmax) { michael@0: if (dval(d) < tens[k]) michael@0: k--; michael@0: k_check = 0; michael@0: } michael@0: j = bbits - i - 1; michael@0: if (j >= 0) { michael@0: b2 = 0; michael@0: s2 = j; michael@0: } michael@0: else { michael@0: b2 = -j; michael@0: s2 = 0; michael@0: } michael@0: if (k >= 0) { michael@0: b5 = 0; michael@0: s5 = k; michael@0: s2 += k; michael@0: } michael@0: else { michael@0: b2 -= k; michael@0: b5 = -k; michael@0: s5 = 0; michael@0: } michael@0: if (mode < 0 || mode > 9) michael@0: mode = 0; michael@0: michael@0: #ifndef SET_INEXACT michael@0: #ifdef Check_FLT_ROUNDS michael@0: try_quick = Rounding == 1; michael@0: #else michael@0: try_quick = 1; michael@0: #endif michael@0: #endif /*SET_INEXACT*/ michael@0: michael@0: if (mode > 5) { michael@0: mode -= 4; michael@0: try_quick = 0; michael@0: } michael@0: leftright = 1; michael@0: switch(mode) { michael@0: case 0: michael@0: case 1: michael@0: ilim = ilim1 = -1; michael@0: i = 18; michael@0: ndigits = 0; michael@0: break; michael@0: case 2: michael@0: leftright = 0; michael@0: /* no break */ michael@0: case 4: michael@0: if (ndigits <= 0) michael@0: ndigits = 1; michael@0: ilim = ilim1 = i = ndigits; michael@0: break; michael@0: case 3: michael@0: leftright = 0; michael@0: /* no break */ michael@0: case 5: michael@0: i = ndigits + k + 1; michael@0: ilim = i; michael@0: ilim1 = i - 1; michael@0: if (i <= 0) michael@0: i = 1; michael@0: } michael@0: s = s0 = rv_alloc(PASS_STATE i); michael@0: michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (mode > 1 && rounding != 1) michael@0: leftright = 0; michael@0: #endif michael@0: michael@0: if (ilim >= 0 && ilim <= Quick_max && try_quick) { michael@0: michael@0: /* Try to get by with floating-point arithmetic. */ michael@0: michael@0: i = 0; michael@0: dval(d2) = dval(d); michael@0: k0 = k; michael@0: ilim0 = ilim; michael@0: ieps = 2; /* conservative */ michael@0: if (k > 0) { michael@0: ds = tens[k&0xf]; michael@0: j = k >> 4; michael@0: if (j & Bletch) { michael@0: /* prevent overflows */ michael@0: j &= Bletch - 1; michael@0: dval(d) /= bigtens[n_bigtens-1]; michael@0: ieps++; michael@0: } michael@0: for(; j; j >>= 1, i++) michael@0: if (j & 1) { michael@0: ieps++; michael@0: ds *= bigtens[i]; michael@0: } michael@0: dval(d) /= ds; michael@0: } michael@0: else if ((j1 = -k)) { michael@0: dval(d) *= tens[j1 & 0xf]; michael@0: for(j = j1 >> 4; j; j >>= 1, i++) michael@0: if (j & 1) { michael@0: ieps++; michael@0: dval(d) *= bigtens[i]; michael@0: } michael@0: } michael@0: if (k_check && dval(d) < 1. && ilim > 0) { michael@0: if (ilim1 <= 0) michael@0: goto fast_failed; michael@0: ilim = ilim1; michael@0: k--; michael@0: dval(d) *= 10.; michael@0: ieps++; michael@0: } michael@0: dval(eps) = ieps*dval(d) + 7.; michael@0: word0(eps) -= (P-1)*Exp_msk1; michael@0: if (ilim == 0) { michael@0: S = mhi = 0; michael@0: dval(d) -= 5.; michael@0: if (dval(d) > dval(eps)) michael@0: goto one_digit; michael@0: if (dval(d) < -dval(eps)) michael@0: goto no_digits; michael@0: goto fast_failed; michael@0: } michael@0: #ifndef No_leftright michael@0: if (leftright) { michael@0: /* Use Steele & White method of only michael@0: * generating digits needed. michael@0: */ michael@0: dval(eps) = 0.5/tens[ilim-1] - dval(eps); michael@0: for(i = 0;;) { michael@0: L = (ULong) dval(d); michael@0: dval(d) -= L; michael@0: *s++ = '0' + (int)L; michael@0: if (dval(d) < dval(eps)) michael@0: goto ret1; michael@0: if (1. - dval(d) < dval(eps)) michael@0: goto bump_up; michael@0: if (++i >= ilim) michael@0: break; michael@0: dval(eps) *= 10.; michael@0: dval(d) *= 10.; michael@0: } michael@0: } michael@0: else { michael@0: #endif michael@0: /* Generate ilim digits, then fix them up. */ michael@0: dval(eps) *= tens[ilim-1]; michael@0: for(i = 1;; i++, dval(d) *= 10.) { michael@0: L = (Long)(dval(d)); michael@0: if (!(dval(d) -= L)) michael@0: ilim = i; michael@0: *s++ = '0' + (int)L; michael@0: if (i == ilim) { michael@0: if (dval(d) > 0.5 + dval(eps)) michael@0: goto bump_up; michael@0: else if (dval(d) < 0.5 - dval(eps)) { michael@0: while(*--s == '0'); michael@0: s++; michael@0: goto ret1; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: #ifndef No_leftright michael@0: } michael@0: #endif michael@0: fast_failed: michael@0: s = s0; michael@0: dval(d) = dval(d2); michael@0: k = k0; michael@0: ilim = ilim0; michael@0: } michael@0: michael@0: /* Do we have a "small" integer? */ michael@0: michael@0: if (be >= 0 && k <= Int_max) { michael@0: /* Yes. */ michael@0: ds = tens[k]; michael@0: if (ndigits < 0 && ilim <= 0) { michael@0: S = mhi = 0; michael@0: if (ilim < 0 || dval(d) < 5*ds) michael@0: goto no_digits; michael@0: goto one_digit; michael@0: } michael@0: for(i = 1;; i++, dval(d) *= 10.) { michael@0: L = (Long)(dval(d) / ds); michael@0: dval(d) -= L*ds; michael@0: #ifdef Check_FLT_ROUNDS michael@0: /* If FLT_ROUNDS == 2, L will usually be high by 1 */ michael@0: if (dval(d) < 0) { michael@0: L--; michael@0: dval(d) += ds; michael@0: } michael@0: #endif michael@0: *s++ = '0' + (int)L; michael@0: if (!dval(d)) { michael@0: #ifdef SET_INEXACT michael@0: inexact = 0; michael@0: #endif michael@0: break; michael@0: } michael@0: if (i == ilim) { michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (mode > 1) michael@0: switch(rounding) { michael@0: case 0: goto ret1; michael@0: case 2: goto bump_up; michael@0: } michael@0: #endif michael@0: dval(d) += dval(d); michael@0: if (dval(d) > ds || (dval(d) == ds && L & 1)) { michael@0: bump_up: michael@0: while(*--s == '9') michael@0: if (s == s0) { michael@0: k++; michael@0: *s = '0'; michael@0: break; michael@0: } michael@0: ++*s++; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: goto ret1; michael@0: } michael@0: michael@0: m2 = b2; michael@0: m5 = b5; michael@0: mhi = mlo = 0; michael@0: if (leftright) { michael@0: i = michael@0: #ifndef Sudden_Underflow michael@0: denorm ? be + (Bias + (P-1) - 1 + 1) : michael@0: #endif michael@0: #ifdef IBM michael@0: 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); michael@0: #else michael@0: 1 + P - bbits; michael@0: #endif michael@0: b2 += i; michael@0: s2 += i; michael@0: mhi = i2b(PASS_STATE 1); michael@0: } michael@0: if (m2 > 0 && s2 > 0) { michael@0: i = m2 < s2 ? m2 : s2; michael@0: b2 -= i; michael@0: m2 -= i; michael@0: s2 -= i; michael@0: } michael@0: if (b5 > 0) { michael@0: if (leftright) { michael@0: if (m5 > 0) { michael@0: mhi = pow5mult(PASS_STATE mhi, m5); michael@0: b1 = mult(PASS_STATE mhi, b); michael@0: Bfree(PASS_STATE b); michael@0: b = b1; michael@0: } michael@0: if ((j = b5 - m5)) michael@0: b = pow5mult(PASS_STATE b, j); michael@0: } michael@0: else michael@0: b = pow5mult(PASS_STATE b, b5); michael@0: } michael@0: S = i2b(PASS_STATE 1); michael@0: if (s5 > 0) michael@0: S = pow5mult(PASS_STATE S, s5); michael@0: michael@0: /* Check for special case that d is a normalized power of 2. */ michael@0: michael@0: spec_case = 0; michael@0: if ((mode < 2 || leftright) michael@0: #ifdef Honor_FLT_ROUNDS michael@0: && rounding == 1 michael@0: #endif michael@0: ) { michael@0: if (!word1(d) && !(word0(d) & Bndry_mask) michael@0: #ifndef Sudden_Underflow michael@0: && word0(d) & (Exp_mask & ~Exp_msk1) michael@0: #endif michael@0: ) { michael@0: /* The special case */ michael@0: b2 += Log2P; michael@0: s2 += Log2P; michael@0: spec_case = 1; michael@0: } michael@0: } michael@0: michael@0: /* Arrange for convenient computation of quotients: michael@0: * shift left if necessary so divisor has 4 leading 0 bits. michael@0: * michael@0: * Perhaps we should just compute leading 28 bits of S once michael@0: * and for all and pass them and a shift to quorem, so it michael@0: * can do shifts and ors to compute the numerator for q. michael@0: */ michael@0: #ifdef Pack_32 michael@0: if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)) michael@0: i = 32 - i; michael@0: #else michael@0: if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) michael@0: i = 16 - i; michael@0: #endif michael@0: if (i > 4) { michael@0: i -= 4; michael@0: b2 += i; michael@0: m2 += i; michael@0: s2 += i; michael@0: } michael@0: else if (i < 4) { michael@0: i += 28; michael@0: b2 += i; michael@0: m2 += i; michael@0: s2 += i; michael@0: } michael@0: if (b2 > 0) michael@0: b = lshift(PASS_STATE b, b2); michael@0: if (s2 > 0) michael@0: S = lshift(PASS_STATE S, s2); michael@0: if (k_check) { michael@0: if (cmp(b,S) < 0) { michael@0: k--; michael@0: b = multadd(PASS_STATE b, 10, 0); /* we botched the k estimate */ michael@0: if (leftright) michael@0: mhi = multadd(PASS_STATE mhi, 10, 0); michael@0: ilim = ilim1; michael@0: } michael@0: } michael@0: if (ilim <= 0 && (mode == 3 || mode == 5)) { michael@0: if (ilim < 0 || cmp(b,S = multadd(PASS_STATE S,5,0)) < 0) { michael@0: /* no digits, fcvt style */ michael@0: no_digits: michael@0: /* MOZILLA CHANGE: Always return a non-empty string. */ michael@0: *s++ = '0'; michael@0: k = 0; michael@0: goto ret; michael@0: } michael@0: one_digit: michael@0: *s++ = '1'; michael@0: k++; michael@0: goto ret; michael@0: } michael@0: if (leftright) { michael@0: if (m2 > 0) michael@0: mhi = lshift(PASS_STATE mhi, m2); michael@0: michael@0: /* Compute mlo -- check for special case michael@0: * that d is a normalized power of 2. michael@0: */ michael@0: michael@0: mlo = mhi; michael@0: if (spec_case) { michael@0: mhi = Balloc(PASS_STATE mhi->k); michael@0: Bcopy(mhi, mlo); michael@0: mhi = lshift(PASS_STATE mhi, Log2P); michael@0: } michael@0: michael@0: for(i = 1;;i++) { michael@0: dig = quorem(b,S) + '0'; michael@0: /* Do we yet have the shortest decimal string michael@0: * that will round to d? michael@0: */ michael@0: j = cmp(b, mlo); michael@0: delta = diff(PASS_STATE S, mhi); michael@0: j1 = delta->sign ? 1 : cmp(b, delta); michael@0: Bfree(PASS_STATE delta); michael@0: #ifndef ROUND_BIASED michael@0: if (j1 == 0 && mode != 1 && !(word1(d) & 1) michael@0: #ifdef Honor_FLT_ROUNDS michael@0: && rounding >= 1 michael@0: #endif michael@0: ) { michael@0: if (dig == '9') michael@0: goto round_9_up; michael@0: if (j > 0) michael@0: dig++; michael@0: #ifdef SET_INEXACT michael@0: else if (!b->x[0] && b->wds <= 1) michael@0: inexact = 0; michael@0: #endif michael@0: *s++ = dig; michael@0: goto ret; michael@0: } michael@0: #endif michael@0: if (j < 0 || (j == 0 && mode != 1 michael@0: #ifndef ROUND_BIASED michael@0: && !(word1(d) & 1) michael@0: #endif michael@0: )) { michael@0: if (!b->x[0] && b->wds <= 1) { michael@0: #ifdef SET_INEXACT michael@0: inexact = 0; michael@0: #endif michael@0: goto accept_dig; michael@0: } michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (mode > 1) michael@0: switch(rounding) { michael@0: case 0: goto accept_dig; michael@0: case 2: goto keep_dig; michael@0: } michael@0: #endif /*Honor_FLT_ROUNDS*/ michael@0: if (j1 > 0) { michael@0: b = lshift(PASS_STATE b, 1); michael@0: j1 = cmp(b, S); michael@0: if ((j1 > 0 || (j1 == 0 && dig & 1)) michael@0: && dig++ == '9') michael@0: goto round_9_up; michael@0: } michael@0: accept_dig: michael@0: *s++ = dig; michael@0: goto ret; michael@0: } michael@0: if (j1 > 0) { michael@0: #ifdef Honor_FLT_ROUNDS michael@0: if (!rounding) michael@0: goto accept_dig; michael@0: #endif michael@0: if (dig == '9') { /* possible if i == 1 */ michael@0: round_9_up: michael@0: *s++ = '9'; michael@0: goto roundoff; michael@0: } michael@0: *s++ = dig + 1; michael@0: goto ret; michael@0: } michael@0: #ifdef Honor_FLT_ROUNDS michael@0: keep_dig: michael@0: #endif michael@0: *s++ = dig; michael@0: if (i == ilim) michael@0: break; michael@0: b = multadd(PASS_STATE b, 10, 0); michael@0: if (mlo == mhi) michael@0: mlo = mhi = multadd(PASS_STATE mhi, 10, 0); michael@0: else { michael@0: mlo = multadd(PASS_STATE mlo, 10, 0); michael@0: mhi = multadd(PASS_STATE mhi, 10, 0); michael@0: } michael@0: } michael@0: } michael@0: else michael@0: for(i = 1;; i++) { michael@0: *s++ = dig = quorem(b,S) + '0'; michael@0: if (!b->x[0] && b->wds <= 1) { michael@0: #ifdef SET_INEXACT michael@0: inexact = 0; michael@0: #endif michael@0: goto ret; michael@0: } michael@0: if (i >= ilim) michael@0: break; michael@0: b = multadd(PASS_STATE b, 10, 0); michael@0: } michael@0: michael@0: /* Round off last digit */ michael@0: michael@0: #ifdef Honor_FLT_ROUNDS michael@0: switch(rounding) { michael@0: case 0: goto trimzeros; michael@0: case 2: goto roundoff; michael@0: } michael@0: #endif michael@0: b = lshift(PASS_STATE b, 1); michael@0: j = cmp(b, S); michael@0: if (j >= 0) { /* ECMA compatible rounding needed by Spidermonkey */ michael@0: roundoff: michael@0: while(*--s == '9') michael@0: if (s == s0) { michael@0: k++; michael@0: *s++ = '1'; michael@0: goto ret; michael@0: } michael@0: ++*s++; michael@0: } michael@0: else { michael@0: #ifdef Honor_FLT_ROUNDS michael@0: trimzeros: michael@0: #endif michael@0: while(*--s == '0'); michael@0: s++; michael@0: } michael@0: ret: michael@0: Bfree(PASS_STATE S); michael@0: if (mhi) { michael@0: if (mlo && mlo != mhi) michael@0: Bfree(PASS_STATE mlo); michael@0: Bfree(PASS_STATE mhi); michael@0: } michael@0: ret1: michael@0: #ifdef SET_INEXACT michael@0: if (inexact) { michael@0: if (!oldinexact) { michael@0: word0(d) = Exp_1 + (70 << Exp_shift); michael@0: word1(d) = 0; michael@0: dval(d) += 1.; michael@0: } michael@0: } michael@0: else if (!oldinexact) michael@0: clear_inexact(); michael@0: #endif michael@0: Bfree(PASS_STATE b); michael@0: *s = 0; michael@0: *decpt = k + 1; michael@0: if (rve) michael@0: *rve = s; michael@0: return s0; michael@0: }