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