156731Sbostic #if defined(LIBC_SCCS) && !defined(lint) 2*58103Sralph static char sccsid[] = "@(#)strtod.c 5.2 (Berkeley) 02/20/93"; 356731Sbostic #endif /* LIBC_SCCS and not lint */ 456731Sbostic 556731Sbostic /**************************************************************** 656731Sbostic * 756731Sbostic * The author of this software is David M. Gay. 856731Sbostic * 956731Sbostic * Copyright (c) 1991 by AT&T. 1056731Sbostic * 1156731Sbostic * Permission to use, copy, modify, and distribute this software for any 1256731Sbostic * purpose without fee is hereby granted, provided that this entire notice 1356731Sbostic * is included in all copies of any software which is or includes a copy 1456731Sbostic * or modification of this software and in all copies of the supporting 1556731Sbostic * documentation for such software. 1656731Sbostic * 1756731Sbostic * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED 1856731Sbostic * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY 1956731Sbostic * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY 2056731Sbostic * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. 2156731Sbostic * 2256731Sbostic ***************************************************************/ 2356731Sbostic 2456731Sbostic /* Please send bug reports to 2556731Sbostic David M. Gay 2656731Sbostic AT&T Bell Laboratories, Room 2C-463 2756731Sbostic 600 Mountain Avenue 2856731Sbostic Murray Hill, NJ 07974-2070 2956731Sbostic U.S.A. 3056731Sbostic dmg@research.att.com or research!dmg 3156731Sbostic */ 3256731Sbostic 3356731Sbostic /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. 3456731Sbostic * 3556731Sbostic * This strtod returns a nearest machine number to the input decimal 3656731Sbostic * string (or sets errno to ERANGE). With IEEE arithmetic, ties are 3756731Sbostic * broken by the IEEE round-even rule. Otherwise ties are broken by 3856731Sbostic * biased rounding (add half and chop). 3956731Sbostic * 4056731Sbostic * Inspired loosely by William D. Clinger's paper "How to Read Floating 4156731Sbostic * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. 4256731Sbostic * 4356731Sbostic * Modifications: 4456731Sbostic * 4556731Sbostic * 1. We only require IEEE, IBM, or VAX double-precision 4656731Sbostic * arithmetic (not IEEE double-extended). 4756731Sbostic * 2. We get by with floating-point arithmetic in a case that 4856731Sbostic * Clinger missed -- when we're computing d * 10^n 4956731Sbostic * for a small integer d and the integer n is not too 5056731Sbostic * much larger than 22 (the maximum integer k for which 5156731Sbostic * we can represent 10^k exactly), we may be able to 5256731Sbostic * compute (d*10^k) * 10^(e-k) with just one roundoff. 5356731Sbostic * 3. Rather than a bit-at-a-time adjustment of the binary 5456731Sbostic * result in the hard case, we use floating-point 5556731Sbostic * arithmetic to determine the adjustment to within 5656731Sbostic * one bit; only in really hard cases do we need to 5756731Sbostic * compute a second residual. 5856731Sbostic * 4. Because of 3., we don't need a large table of powers of 10 5956731Sbostic * for ten-to-e (just some small tables, e.g. of 10^k 6056731Sbostic * for 0 <= k <= 22). 6156731Sbostic */ 6256731Sbostic 6356731Sbostic /* 6456731Sbostic * #define IEEE_8087 for IEEE-arithmetic machines where the least 6556731Sbostic * significant byte has the lowest address. 6656731Sbostic * #define IEEE_MC68k for IEEE-arithmetic machines where the most 6756731Sbostic * significant byte has the lowest address. 6856731Sbostic * #define Sudden_Underflow for IEEE-format machines without gradual 6956731Sbostic * underflow (i.e., that flush to zero on underflow). 7056731Sbostic * #define IBM for IBM mainframe-style floating-point arithmetic. 7156731Sbostic * #define VAX for VAX-style floating-point arithmetic. 7256731Sbostic * #define Unsigned_Shifts if >> does treats its left operand as unsigned. 7356731Sbostic * #define No_leftright to omit left-right logic in fast floating-point 7456731Sbostic * computation of dtoa. 7556731Sbostic * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. 7656731Sbostic * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines 7756731Sbostic * that use extended-precision instructions to compute rounded 7856731Sbostic * products and quotients) with IBM. 7956731Sbostic * #define ROUND_BIASED for IEEE-format with biased rounding. 8056731Sbostic * #define Inaccurate_Divide for IEEE-format with correctly rounded 8156731Sbostic * products but inaccurate quotients, e.g., for Intel i860. 8256731Sbostic * #define Just_16 to store 16 bits per 32-bit long when doing high-precision 8356731Sbostic * integer arithmetic. Whether this speeds things up or slows things 8456731Sbostic * down depends on the machine and the number being converted. 8556731Sbostic * #define KR_headers for old-style C function headers. 8656731Sbostic * #define Bad_float_h if your system lacks a float.h or if it does not 8756731Sbostic * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, 8856731Sbostic * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. 8956731Sbostic */ 9056731Sbostic 91*58103Sralph #if defined(i386) || defined(mips) && defined(MIPSEL) 92*58103Sralph #define IEEE_8087 93*58103Sralph #else 9456731Sbostic #define IEEE_MC68k 95*58103Sralph #endif 9656731Sbostic 9756731Sbostic #ifdef DEBUG 9856731Sbostic #include "stdio.h" 9956731Sbostic #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} 10056731Sbostic #endif 10156731Sbostic 10256731Sbostic #ifdef __cplusplus 10356731Sbostic #include "malloc.h" 10456731Sbostic #include "memory.h" 10556731Sbostic #else 10656731Sbostic #ifndef KR_headers 10756731Sbostic #include "stdlib.h" 10856731Sbostic #include "string.h" 10956731Sbostic #else 11056731Sbostic #include "malloc.h" 11156731Sbostic #include "memory.h" 11256731Sbostic #endif 11356731Sbostic #endif 11456731Sbostic 11556731Sbostic #include "errno.h" 11656731Sbostic #ifdef Bad_float_h 11756731Sbostic #undef __STDC__ 11856731Sbostic #ifdef IEEE_MC68k 11956731Sbostic #define IEEE_ARITHMETIC 12056731Sbostic #endif 12156731Sbostic #ifdef IEEE_8087 12256731Sbostic #define IEEE_ARITHMETIC 12356731Sbostic #endif 12456731Sbostic #ifdef IEEE_ARITHMETIC 12556731Sbostic #define DBL_DIG 15 12656731Sbostic #define DBL_MAX_10_EXP 308 12756731Sbostic #define DBL_MAX_EXP 1024 12856731Sbostic #define FLT_RADIX 2 12956731Sbostic #define FLT_ROUNDS 1 13056731Sbostic #define DBL_MAX 1.7976931348623157e+308 13156731Sbostic #endif 13256731Sbostic 13356731Sbostic #ifdef IBM 13456731Sbostic #define DBL_DIG 16 13556731Sbostic #define DBL_MAX_10_EXP 75 13656731Sbostic #define DBL_MAX_EXP 63 13756731Sbostic #define FLT_RADIX 16 13856731Sbostic #define FLT_ROUNDS 0 13956731Sbostic #define DBL_MAX 7.2370055773322621e+75 14056731Sbostic #endif 14156731Sbostic 14256731Sbostic #ifdef VAX 14356731Sbostic #define DBL_DIG 16 14456731Sbostic #define DBL_MAX_10_EXP 38 14556731Sbostic #define DBL_MAX_EXP 127 14656731Sbostic #define FLT_RADIX 2 14756731Sbostic #define FLT_ROUNDS 1 14856731Sbostic #define DBL_MAX 1.7014118346046923e+38 14956731Sbostic #endif 15056731Sbostic 15156731Sbostic #ifndef LONG_MAX 15256731Sbostic #define LONG_MAX 2147483647 15356731Sbostic #endif 15456731Sbostic #else 15556731Sbostic #include "float.h" 15656731Sbostic #endif 15756731Sbostic #ifndef __MATH_H__ 15856731Sbostic #include "math.h" 15956731Sbostic #endif 16056731Sbostic 16156731Sbostic #ifdef __cplusplus 16256731Sbostic extern "C" { 16356731Sbostic #endif 16456731Sbostic 16556731Sbostic #ifndef CONST 16656731Sbostic #ifdef KR_headers 16756731Sbostic #define CONST /* blank */ 16856731Sbostic #else 16956731Sbostic #define CONST const 17056731Sbostic #endif 17156731Sbostic #endif 17256731Sbostic 17356731Sbostic #ifdef Unsigned_Shifts 17456731Sbostic #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; 17556731Sbostic #else 17656731Sbostic #define Sign_Extend(a,b) /*no-op*/ 17756731Sbostic #endif 17856731Sbostic 17956731Sbostic #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 18056731Sbostic Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. 18156731Sbostic #endif 18256731Sbostic 18356731Sbostic #ifdef IEEE_8087 18456731Sbostic #define word0(x) ((unsigned long *)&x)[1] 18556731Sbostic #define word1(x) ((unsigned long *)&x)[0] 18656731Sbostic #else 18756731Sbostic #define word0(x) ((unsigned long *)&x)[0] 18856731Sbostic #define word1(x) ((unsigned long *)&x)[1] 18956731Sbostic #endif 19056731Sbostic 19156731Sbostic /* The following definition of Storeinc is appropriate for MIPS processors. 19256731Sbostic * An alternative that might be better on some machines is 19356731Sbostic * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) 19456731Sbostic */ 19556731Sbostic #if defined(IEEE_8087) + defined(VAX) 19656731Sbostic #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ 19756731Sbostic ((unsigned short *)a)[0] = (unsigned short)c, a++) 19856731Sbostic #else 19956731Sbostic #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ 20056731Sbostic ((unsigned short *)a)[1] = (unsigned short)c, a++) 20156731Sbostic #endif 20256731Sbostic 20356731Sbostic /* #define P DBL_MANT_DIG */ 20456731Sbostic /* Ten_pmax = floor(P*log(2)/log(5)) */ 20556731Sbostic /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ 20656731Sbostic /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ 20756731Sbostic /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ 20856731Sbostic 20956731Sbostic #if defined(IEEE_8087) + defined(IEEE_MC68k) 21056731Sbostic #define Exp_shift 20 21156731Sbostic #define Exp_shift1 20 21256731Sbostic #define Exp_msk1 0x100000 21356731Sbostic #define Exp_msk11 0x100000 21456731Sbostic #define Exp_mask 0x7ff00000 21556731Sbostic #define P 53 21656731Sbostic #define Bias 1023 21756731Sbostic #define IEEE_Arith 21856731Sbostic #define Emin (-1022) 21956731Sbostic #define Exp_1 0x3ff00000 22056731Sbostic #define Exp_11 0x3ff00000 22156731Sbostic #define Ebits 11 22256731Sbostic #define Frac_mask 0xfffff 22356731Sbostic #define Frac_mask1 0xfffff 22456731Sbostic #define Ten_pmax 22 22556731Sbostic #define Bletch 0x10 22656731Sbostic #define Bndry_mask 0xfffff 22756731Sbostic #define Bndry_mask1 0xfffff 22856731Sbostic #define LSB 1 22956731Sbostic #define Sign_bit 0x80000000 23056731Sbostic #define Log2P 1 23156731Sbostic #define Tiny0 0 23256731Sbostic #define Tiny1 1 23356731Sbostic #define Quick_max 14 23456731Sbostic #define Int_max 14 23556731Sbostic #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */ 23656731Sbostic #else 23756731Sbostic #undef Sudden_Underflow 23856731Sbostic #define Sudden_Underflow 23956731Sbostic #ifdef IBM 24056731Sbostic #define Exp_shift 24 24156731Sbostic #define Exp_shift1 24 24256731Sbostic #define Exp_msk1 0x1000000 24356731Sbostic #define Exp_msk11 0x1000000 24456731Sbostic #define Exp_mask 0x7f000000 24556731Sbostic #define P 14 24656731Sbostic #define Bias 65 24756731Sbostic #define Exp_1 0x41000000 24856731Sbostic #define Exp_11 0x41000000 24956731Sbostic #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ 25056731Sbostic #define Frac_mask 0xffffff 25156731Sbostic #define Frac_mask1 0xffffff 25256731Sbostic #define Bletch 4 25356731Sbostic #define Ten_pmax 22 25456731Sbostic #define Bndry_mask 0xefffff 25556731Sbostic #define Bndry_mask1 0xffffff 25656731Sbostic #define LSB 1 25756731Sbostic #define Sign_bit 0x80000000 25856731Sbostic #define Log2P 4 25956731Sbostic #define Tiny0 0x100000 26056731Sbostic #define Tiny1 0 26156731Sbostic #define Quick_max 14 26256731Sbostic #define Int_max 15 26356731Sbostic #else /* VAX */ 26456731Sbostic #define Exp_shift 23 26556731Sbostic #define Exp_shift1 7 26656731Sbostic #define Exp_msk1 0x80 26756731Sbostic #define Exp_msk11 0x800000 26856731Sbostic #define Exp_mask 0x7f80 26956731Sbostic #define P 56 27056731Sbostic #define Bias 129 27156731Sbostic #define Exp_1 0x40800000 27256731Sbostic #define Exp_11 0x4080 27356731Sbostic #define Ebits 8 27456731Sbostic #define Frac_mask 0x7fffff 27556731Sbostic #define Frac_mask1 0xffff007f 27656731Sbostic #define Ten_pmax 24 27756731Sbostic #define Bletch 2 27856731Sbostic #define Bndry_mask 0xffff007f 27956731Sbostic #define Bndry_mask1 0xffff007f 28056731Sbostic #define LSB 0x10000 28156731Sbostic #define Sign_bit 0x8000 28256731Sbostic #define Log2P 1 28356731Sbostic #define Tiny0 0x80 28456731Sbostic #define Tiny1 0 28556731Sbostic #define Quick_max 15 28656731Sbostic #define Int_max 15 28756731Sbostic #endif 28856731Sbostic #endif 28956731Sbostic 29056731Sbostic #ifndef IEEE_Arith 29156731Sbostic #define ROUND_BIASED 29256731Sbostic #endif 29356731Sbostic 29456731Sbostic #ifdef RND_PRODQUOT 29556731Sbostic #define rounded_product(a,b) a = rnd_prod(a, b) 29656731Sbostic #define rounded_quotient(a,b) a = rnd_quot(a, b) 29756731Sbostic #ifdef KR_headers 29856731Sbostic extern double rnd_prod(), rnd_quot(); 29956731Sbostic #else 30056731Sbostic extern double rnd_prod(double, double), rnd_quot(double, double); 30156731Sbostic #endif 30256731Sbostic #else 30356731Sbostic #define rounded_product(a,b) a *= b 30456731Sbostic #define rounded_quotient(a,b) a /= b 30556731Sbostic #endif 30656731Sbostic 30756731Sbostic #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) 30856731Sbostic #define Big1 0xffffffff 30956731Sbostic 31056731Sbostic #ifndef Just_16 31156731Sbostic /* When Pack_32 is not defined, we store 16 bits per 32-bit long. 31256731Sbostic * This makes some inner loops simpler and sometimes saves work 31356731Sbostic * during multiplications, but it often seems to make things slightly 31456731Sbostic * slower. Hence the default is now to store 32 bits per long. 31556731Sbostic */ 31656731Sbostic #ifndef Pack_32 31756731Sbostic #define Pack_32 31856731Sbostic #endif 31956731Sbostic #endif 32056731Sbostic 32156731Sbostic #define Kmax 15 32256731Sbostic 32356731Sbostic #ifdef __cplusplus 32456731Sbostic extern "C" double strtod(const char *s00, char **se); 32556731Sbostic extern "C" char *dtoa(double d, int mode, int ndigits, 32656731Sbostic int *decpt, int *sign, char **rve); 32756731Sbostic #endif 32856731Sbostic 32956731Sbostic struct 33056731Sbostic Bigint { 33156731Sbostic struct Bigint *next; 33256731Sbostic int k, maxwds, sign, wds; 33356731Sbostic unsigned long x[1]; 33456731Sbostic }; 33556731Sbostic 33656731Sbostic typedef struct Bigint Bigint; 33756731Sbostic 33856731Sbostic static Bigint *freelist[Kmax+1]; 33956731Sbostic 34056731Sbostic static Bigint * 34156731Sbostic Balloc 34256731Sbostic #ifdef KR_headers 34356731Sbostic (k) int k; 34456731Sbostic #else 34556731Sbostic (int k) 34656731Sbostic #endif 34756731Sbostic { 34856731Sbostic int x; 34956731Sbostic Bigint *rv; 35056731Sbostic 35156731Sbostic if (rv = freelist[k]) { 35256731Sbostic freelist[k] = rv->next; 35356731Sbostic } else { 35456731Sbostic x = 1 << k; 35556731Sbostic rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long)); 35656731Sbostic rv->k = k; 35756731Sbostic rv->maxwds = x; 35856731Sbostic } 35956731Sbostic rv->sign = rv->wds = 0; 36056731Sbostic return rv; 36156731Sbostic } 36256731Sbostic 36356731Sbostic static void 36456731Sbostic Bfree 36556731Sbostic #ifdef KR_headers 36656731Sbostic (v) Bigint *v; 36756731Sbostic #else 36856731Sbostic (Bigint *v) 36956731Sbostic #endif 37056731Sbostic { 37156731Sbostic if (v) { 37256731Sbostic v->next = freelist[v->k]; 37356731Sbostic freelist[v->k] = v; 37456731Sbostic } 37556731Sbostic } 37656731Sbostic 37756731Sbostic #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ 37856731Sbostic y->wds*sizeof(long) + 2*sizeof(int)) 37956731Sbostic 38056731Sbostic static Bigint * 38156731Sbostic multadd 38256731Sbostic #ifdef KR_headers 38356731Sbostic (b, m, a) Bigint *b; int m, a; 38456731Sbostic #else 38556731Sbostic (Bigint *b, int m, int a) /* multiply by m and add a */ 38656731Sbostic #endif 38756731Sbostic { 38856731Sbostic int i, wds; 38956731Sbostic unsigned long *x, y; 39056731Sbostic #ifdef Pack_32 39156731Sbostic unsigned long xi, z; 39256731Sbostic #endif 39356731Sbostic Bigint *b1; 39456731Sbostic 39556731Sbostic wds = b->wds; 39656731Sbostic x = b->x; 39756731Sbostic i = 0; 39856731Sbostic do { 39956731Sbostic #ifdef Pack_32 40056731Sbostic xi = *x; 40156731Sbostic y = (xi & 0xffff) * m + a; 40256731Sbostic z = (xi >> 16) * m + (y >> 16); 40356731Sbostic a = (int)(z >> 16); 40456731Sbostic *x++ = (z << 16) + (y & 0xffff); 40556731Sbostic #else 40656731Sbostic y = *x * m + a; 40756731Sbostic a = (int)(y >> 16); 40856731Sbostic *x++ = y & 0xffff; 40956731Sbostic #endif 41056731Sbostic } while (++i < wds); 41156731Sbostic if (a) { 41256731Sbostic if (wds >= b->maxwds) { 41356731Sbostic b1 = Balloc(b->k+1); 41456731Sbostic Bcopy(b1, b); 41556731Sbostic Bfree(b); 41656731Sbostic b = b1; 41756731Sbostic } 41856731Sbostic b->x[wds++] = a; 41956731Sbostic b->wds = wds; 42056731Sbostic } 42156731Sbostic return b; 42256731Sbostic } 42356731Sbostic 42456731Sbostic static Bigint * 42556731Sbostic s2b 42656731Sbostic #ifdef KR_headers 42756731Sbostic (s, nd0, nd, y9) CONST char *s; int nd0, nd; unsigned long y9; 42856731Sbostic #else 42956731Sbostic (CONST char *s, int nd0, int nd, unsigned long y9) 43056731Sbostic #endif 43156731Sbostic { 43256731Sbostic Bigint *b; 43356731Sbostic int i, k; 43456731Sbostic long x, y; 43556731Sbostic 43656731Sbostic x = (nd + 8) / 9; 43756731Sbostic for (k = 0, y = 1; x > y; y <<= 1, k++) ; 43856731Sbostic #ifdef Pack_32 43956731Sbostic b = Balloc(k); 44056731Sbostic b->x[0] = y9; 44156731Sbostic b->wds = 1; 44256731Sbostic #else 44356731Sbostic b = Balloc(k+1); 44456731Sbostic b->x[0] = y9 & 0xffff; 44556731Sbostic b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; 44656731Sbostic #endif 44756731Sbostic 44856731Sbostic i = 9; 44956731Sbostic if (9 < nd0) { 45056731Sbostic s += 9; 45156731Sbostic do 45256731Sbostic b = multadd(b, 10, *s++ - '0'); 45356731Sbostic while (++i < nd0); 45456731Sbostic s++; 45556731Sbostic } else 45656731Sbostic s += 10; 45756731Sbostic for (; i < nd; i++) 45856731Sbostic b = multadd(b, 10, *s++ - '0'); 45956731Sbostic return b; 46056731Sbostic } 46156731Sbostic 46256731Sbostic static int 46356731Sbostic hi0bits 46456731Sbostic #ifdef KR_headers 46556731Sbostic (x) register unsigned long x; 46656731Sbostic #else 46756731Sbostic (register unsigned long x) 46856731Sbostic #endif 46956731Sbostic { 47056731Sbostic register int k = 0; 47156731Sbostic 47256731Sbostic if (!(x & 0xffff0000)) { 47356731Sbostic k = 16; 47456731Sbostic x <<= 16; 47556731Sbostic } 47656731Sbostic if (!(x & 0xff000000)) { 47756731Sbostic k += 8; 47856731Sbostic x <<= 8; 47956731Sbostic } 48056731Sbostic if (!(x & 0xf0000000)) { 48156731Sbostic k += 4; 48256731Sbostic x <<= 4; 48356731Sbostic } 48456731Sbostic if (!(x & 0xc0000000)) { 48556731Sbostic k += 2; 48656731Sbostic x <<= 2; 48756731Sbostic } 48856731Sbostic if (!(x & 0x80000000)) { 48956731Sbostic k++; 49056731Sbostic if (!(x & 0x40000000)) 49156731Sbostic return 32; 49256731Sbostic } 49356731Sbostic return k; 49456731Sbostic } 49556731Sbostic 49656731Sbostic static int 49756731Sbostic lo0bits 49856731Sbostic #ifdef KR_headers 49956731Sbostic (y) unsigned long *y; 50056731Sbostic #else 50156731Sbostic (unsigned long *y) 50256731Sbostic #endif 50356731Sbostic { 50456731Sbostic register int k; 50556731Sbostic register unsigned long x = *y; 50656731Sbostic 50756731Sbostic if (x & 7) { 50856731Sbostic if (x & 1) 50956731Sbostic return 0; 51056731Sbostic if (x & 2) { 51156731Sbostic *y = x >> 1; 51256731Sbostic return 1; 51356731Sbostic } 51456731Sbostic *y = x >> 2; 51556731Sbostic return 2; 51656731Sbostic } 51756731Sbostic k = 0; 51856731Sbostic if (!(x & 0xffff)) { 51956731Sbostic k = 16; 52056731Sbostic x >>= 16; 52156731Sbostic } 52256731Sbostic if (!(x & 0xff)) { 52356731Sbostic k += 8; 52456731Sbostic x >>= 8; 52556731Sbostic } 52656731Sbostic if (!(x & 0xf)) { 52756731Sbostic k += 4; 52856731Sbostic x >>= 4; 52956731Sbostic } 53056731Sbostic if (!(x & 0x3)) { 53156731Sbostic k += 2; 53256731Sbostic x >>= 2; 53356731Sbostic } 53456731Sbostic if (!(x & 1)) { 53556731Sbostic k++; 53656731Sbostic x >>= 1; 53756731Sbostic if (!x & 1) 53856731Sbostic return 32; 53956731Sbostic } 54056731Sbostic *y = x; 54156731Sbostic return k; 54256731Sbostic } 54356731Sbostic 54456731Sbostic static Bigint * 54556731Sbostic i2b 54656731Sbostic #ifdef KR_headers 54756731Sbostic (i) int i; 54856731Sbostic #else 54956731Sbostic (int i) 55056731Sbostic #endif 55156731Sbostic { 55256731Sbostic Bigint *b; 55356731Sbostic 55456731Sbostic b = Balloc(1); 55556731Sbostic b->x[0] = i; 55656731Sbostic b->wds = 1; 55756731Sbostic return b; 55856731Sbostic } 55956731Sbostic 56056731Sbostic static Bigint * 56156731Sbostic mult 56256731Sbostic #ifdef KR_headers 56356731Sbostic (a, b) Bigint *a, *b; 56456731Sbostic #else 56556731Sbostic (Bigint *a, Bigint *b) 56656731Sbostic #endif 56756731Sbostic { 56856731Sbostic Bigint *c; 56956731Sbostic int k, wa, wb, wc; 57056731Sbostic unsigned long carry, y, z; 57156731Sbostic unsigned long *x, *xa, *xae, *xb, *xbe, *xc, *xc0; 57256731Sbostic #ifdef Pack_32 57356731Sbostic unsigned long z2; 57456731Sbostic #endif 57556731Sbostic 57656731Sbostic if (a->wds < b->wds) { 57756731Sbostic c = a; 57856731Sbostic a = b; 57956731Sbostic b = c; 58056731Sbostic } 58156731Sbostic k = a->k; 58256731Sbostic wa = a->wds; 58356731Sbostic wb = b->wds; 58456731Sbostic wc = wa + wb; 58556731Sbostic if (wc > a->maxwds) 58656731Sbostic k++; 58756731Sbostic c = Balloc(k); 58856731Sbostic for (x = c->x, xa = x + wc; x < xa; x++) 58956731Sbostic *x = 0; 59056731Sbostic xa = a->x; 59156731Sbostic xae = xa + wa; 59256731Sbostic xb = b->x; 59356731Sbostic xbe = xb + wb; 59456731Sbostic xc0 = c->x; 59556731Sbostic #ifdef Pack_32 59656731Sbostic for (; xb < xbe; xb++, xc0++) { 59756731Sbostic if (y = *xb & 0xffff) { 59856731Sbostic x = xa; 59956731Sbostic xc = xc0; 60056731Sbostic carry = 0; 60156731Sbostic do { 60256731Sbostic z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; 60356731Sbostic carry = z >> 16; 60456731Sbostic z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; 60556731Sbostic carry = z2 >> 16; 60656731Sbostic Storeinc(xc, z2, z); 60756731Sbostic } while (x < xae); 60856731Sbostic *xc = carry; 60956731Sbostic } 61056731Sbostic if (y = *xb >> 16) { 61156731Sbostic x = xa; 61256731Sbostic xc = xc0; 61356731Sbostic carry = 0; 61456731Sbostic z2 = *xc; 61556731Sbostic do { 61656731Sbostic z = (*x & 0xffff) * y + (*xc >> 16) + carry; 61756731Sbostic carry = z >> 16; 61856731Sbostic Storeinc(xc, z, z2); 61956731Sbostic z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; 62056731Sbostic carry = z2 >> 16; 62156731Sbostic } while (x < xae); 62256731Sbostic *xc = z2; 62356731Sbostic } 62456731Sbostic } 62556731Sbostic #else 62656731Sbostic for (; xb < xbe; xc0++) { 62756731Sbostic if (y = *xb++) { 62856731Sbostic x = xa; 62956731Sbostic xc = xc0; 63056731Sbostic carry = 0; 63156731Sbostic do { 63256731Sbostic z = *x++ * y + *xc + carry; 63356731Sbostic carry = z >> 16; 63456731Sbostic *xc++ = z & 0xffff; 63556731Sbostic } while (x < xae); 63656731Sbostic *xc = carry; 63756731Sbostic } 63856731Sbostic } 63956731Sbostic #endif 64056731Sbostic for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; 64156731Sbostic c->wds = wc; 64256731Sbostic return c; 64356731Sbostic } 64456731Sbostic 64556731Sbostic static Bigint *p5s; 64656731Sbostic 64756731Sbostic static Bigint * 64856731Sbostic pow5mult 64956731Sbostic #ifdef KR_headers 65056731Sbostic (b, k) Bigint *b; int k; 65156731Sbostic #else 65256731Sbostic (Bigint *b, int k) 65356731Sbostic #endif 65456731Sbostic { 65556731Sbostic Bigint *b1, *p5, *p51; 65656731Sbostic int i; 65756731Sbostic static int p05[3] = { 5, 25, 125 }; 65856731Sbostic 65956731Sbostic if (i = k & 3) 66056731Sbostic b = multadd(b, p05[i-1], 0); 66156731Sbostic 66256731Sbostic if (!(k >>= 2)) 66356731Sbostic return b; 66456731Sbostic if (!(p5 = p5s)) { 66556731Sbostic /* first time */ 66656731Sbostic p5 = p5s = i2b(625); 66756731Sbostic p5->next = 0; 66856731Sbostic } 66956731Sbostic for (;;) { 67056731Sbostic if (k & 1) { 67156731Sbostic b1 = mult(b, p5); 67256731Sbostic Bfree(b); 67356731Sbostic b = b1; 67456731Sbostic } 67556731Sbostic if (!(k >>= 1)) 67656731Sbostic break; 67756731Sbostic if (!(p51 = p5->next)) { 67856731Sbostic p51 = p5->next = mult(p5,p5); 67956731Sbostic p51->next = 0; 68056731Sbostic } 68156731Sbostic p5 = p51; 68256731Sbostic } 68356731Sbostic return b; 68456731Sbostic } 68556731Sbostic 68656731Sbostic static Bigint * 68756731Sbostic lshift 68856731Sbostic #ifdef KR_headers 68956731Sbostic (b, k) Bigint *b; int k; 69056731Sbostic #else 69156731Sbostic (Bigint *b, int k) 69256731Sbostic #endif 69356731Sbostic { 69456731Sbostic int i, k1, n, n1; 69556731Sbostic Bigint *b1; 69656731Sbostic unsigned long *x, *x1, *xe, z; 69756731Sbostic 69856731Sbostic #ifdef Pack_32 69956731Sbostic n = k >> 5; 70056731Sbostic #else 70156731Sbostic n = k >> 4; 70256731Sbostic #endif 70356731Sbostic k1 = b->k; 70456731Sbostic n1 = n + b->wds + 1; 70556731Sbostic for (i = b->maxwds; n1 > i; i <<= 1) 70656731Sbostic k1++; 70756731Sbostic b1 = Balloc(k1); 70856731Sbostic x1 = b1->x; 70956731Sbostic for (i = 0; i < n; i++) 71056731Sbostic *x1++ = 0; 71156731Sbostic x = b->x; 71256731Sbostic xe = x + b->wds; 71356731Sbostic #ifdef Pack_32 71456731Sbostic if (k &= 0x1f) { 71556731Sbostic k1 = 32 - k; 71656731Sbostic z = 0; 71756731Sbostic do { 71856731Sbostic *x1++ = *x << k | z; 71956731Sbostic z = *x++ >> k1; 72056731Sbostic } while (x < xe); 72156731Sbostic if (*x1 = z) 72256731Sbostic ++n1; 72356731Sbostic } 72456731Sbostic #else 72556731Sbostic if (k &= 0xf) { 72656731Sbostic k1 = 16 - k; 72756731Sbostic z = 0; 72856731Sbostic do { 72956731Sbostic *x1++ = *x << k & 0xffff | z; 73056731Sbostic z = *x++ >> k1; 73156731Sbostic } while (x < xe); 73256731Sbostic if (*x1 = z) 73356731Sbostic ++n1; 73456731Sbostic } 73556731Sbostic #endif 73656731Sbostic else 73756731Sbostic do 73856731Sbostic *x1++ = *x++; 73956731Sbostic while (x < xe); 74056731Sbostic b1->wds = n1 - 1; 74156731Sbostic Bfree(b); 74256731Sbostic return b1; 74356731Sbostic } 74456731Sbostic 74556731Sbostic static int 74656731Sbostic cmp 74756731Sbostic #ifdef KR_headers 74856731Sbostic (a, b) Bigint *a, *b; 74956731Sbostic #else 75056731Sbostic (Bigint *a, Bigint *b) 75156731Sbostic #endif 75256731Sbostic { 75356731Sbostic unsigned long *xa, *xa0, *xb, *xb0; 75456731Sbostic int i, j; 75556731Sbostic 75656731Sbostic i = a->wds; 75756731Sbostic j = b->wds; 75856731Sbostic #ifdef DEBUG 75956731Sbostic if (i > 1 && !a->x[i-1]) 76056731Sbostic Bug("cmp called with a->x[a->wds-1] == 0"); 76156731Sbostic if (j > 1 && !b->x[j-1]) 76256731Sbostic Bug("cmp called with b->x[b->wds-1] == 0"); 76356731Sbostic #endif 76456731Sbostic if (i -= j) 76556731Sbostic return i; 76656731Sbostic xa0 = a->x; 76756731Sbostic xa = xa0 + j; 76856731Sbostic xb0 = b->x; 76956731Sbostic xb = xb0 + j; 77056731Sbostic for (;;) { 77156731Sbostic if (*--xa != *--xb) 77256731Sbostic return *xa < *xb ? -1 : 1; 77356731Sbostic if (xa <= xa0) 77456731Sbostic break; 77556731Sbostic } 77656731Sbostic return 0; 77756731Sbostic } 77856731Sbostic 77956731Sbostic static Bigint * 78056731Sbostic diff 78156731Sbostic #ifdef KR_headers 78256731Sbostic (a, b) Bigint *a, *b; 78356731Sbostic #else 78456731Sbostic (Bigint *a, Bigint *b) 78556731Sbostic #endif 78656731Sbostic { 78756731Sbostic Bigint *c; 78856731Sbostic int i, wa, wb; 78956731Sbostic long borrow, y; /* We need signed shifts here. */ 79056731Sbostic unsigned long *xa, *xae, *xb, *xbe, *xc; 79156731Sbostic #ifdef Pack_32 79256731Sbostic long z; 79356731Sbostic #endif 79456731Sbostic 79556731Sbostic i = cmp(a,b); 79656731Sbostic if (!i) { 79756731Sbostic c = Balloc(0); 79856731Sbostic c->wds = 1; 79956731Sbostic c->x[0] = 0; 80056731Sbostic return c; 80156731Sbostic } 80256731Sbostic if (i < 0) { 80356731Sbostic c = a; 80456731Sbostic a = b; 80556731Sbostic b = c; 80656731Sbostic i = 1; 80756731Sbostic } else 80856731Sbostic i = 0; 80956731Sbostic c = Balloc(a->k); 81056731Sbostic c->sign = i; 81156731Sbostic wa = a->wds; 81256731Sbostic xa = a->x; 81356731Sbostic xae = xa + wa; 81456731Sbostic wb = b->wds; 81556731Sbostic xb = b->x; 81656731Sbostic xbe = xb + wb; 81756731Sbostic xc = c->x; 81856731Sbostic borrow = 0; 81956731Sbostic #ifdef Pack_32 82056731Sbostic do { 82156731Sbostic y = (*xa & 0xffff) - (*xb & 0xffff) + borrow; 82256731Sbostic borrow = y >> 16; 82356731Sbostic Sign_Extend(borrow, y); 82456731Sbostic z = (*xa++ >> 16) - (*xb++ >> 16) + borrow; 82556731Sbostic borrow = z >> 16; 82656731Sbostic Sign_Extend(borrow, z); 82756731Sbostic Storeinc(xc, z, y); 82856731Sbostic } while (xb < xbe); 82956731Sbostic while (xa < xae) { 83056731Sbostic y = (*xa & 0xffff) + borrow; 83156731Sbostic borrow = y >> 16; 83256731Sbostic Sign_Extend(borrow, y); 83356731Sbostic z = (*xa++ >> 16) + borrow; 83456731Sbostic borrow = z >> 16; 83556731Sbostic Sign_Extend(borrow, z); 83656731Sbostic Storeinc(xc, z, y); 83756731Sbostic } 83856731Sbostic #else 83956731Sbostic do { 84056731Sbostic y = *xa++ - *xb++ + borrow; 84156731Sbostic borrow = y >> 16; 84256731Sbostic Sign_Extend(borrow, y); 84356731Sbostic *xc++ = y & 0xffff; 84456731Sbostic } while (xb < xbe); 84556731Sbostic while (xa < xae) { 84656731Sbostic y = *xa++ + borrow; 84756731Sbostic borrow = y >> 16; 84856731Sbostic Sign_Extend(borrow, y); 84956731Sbostic *xc++ = y & 0xffff; 85056731Sbostic } 85156731Sbostic #endif 85256731Sbostic while (!*--xc) 85356731Sbostic wa--; 85456731Sbostic c->wds = wa; 85556731Sbostic return c; 85656731Sbostic } 85756731Sbostic 85856731Sbostic static double 85956731Sbostic ulp 86056731Sbostic #ifdef KR_headers 86156731Sbostic (x) double x; 86256731Sbostic #else 86356731Sbostic (double x) 86456731Sbostic #endif 86556731Sbostic { 86656731Sbostic register long L; 86756731Sbostic double a; 86856731Sbostic 86956731Sbostic L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; 87056731Sbostic #ifndef Sudden_Underflow 87156731Sbostic if (L > 0) { 87256731Sbostic #endif 87356731Sbostic #ifdef IBM 87456731Sbostic L |= Exp_msk1 >> 4; 87556731Sbostic #endif 87656731Sbostic word0(a) = L; 87756731Sbostic word1(a) = 0; 87856731Sbostic #ifndef Sudden_Underflow 87956731Sbostic } else { 88056731Sbostic L = -L >> Exp_shift; 88156731Sbostic if (L < Exp_shift) { 88256731Sbostic word0(a) = 0x80000 >> L; 88356731Sbostic word1(a) = 0; 88456731Sbostic } else { 88556731Sbostic word0(a) = 0; 88656731Sbostic L -= Exp_shift; 88756731Sbostic word1(a) = L >= 31 ? 1 : 1 << 31 - L; 88856731Sbostic } 88956731Sbostic } 89056731Sbostic #endif 89156731Sbostic return a; 89256731Sbostic } 89356731Sbostic 89456731Sbostic static double 89556731Sbostic b2d 89656731Sbostic #ifdef KR_headers 89756731Sbostic (a, e) Bigint *a; int *e; 89856731Sbostic #else 89956731Sbostic (Bigint *a, int *e) 90056731Sbostic #endif 90156731Sbostic { 90256731Sbostic unsigned long *xa, *xa0, w, y, z; 90356731Sbostic int k; 90456731Sbostic double d; 90556731Sbostic #ifdef VAX 90656731Sbostic unsigned long d0, d1; 90756731Sbostic #else 90856731Sbostic #define d0 word0(d) 90956731Sbostic #define d1 word1(d) 91056731Sbostic #endif 91156731Sbostic 91256731Sbostic xa0 = a->x; 91356731Sbostic xa = xa0 + a->wds; 91456731Sbostic y = *--xa; 91556731Sbostic #ifdef DEBUG 91656731Sbostic if (!y) Bug("zero y in b2d"); 91756731Sbostic #endif 91856731Sbostic k = hi0bits(y); 91956731Sbostic *e = 32 - k; 92056731Sbostic #ifdef Pack_32 92156731Sbostic if (k < Ebits) { 92256731Sbostic d0 = Exp_1 | y >> Ebits - k; 92356731Sbostic w = xa > xa0 ? *--xa : 0; 92456731Sbostic d1 = y << (32-Ebits) + k | w >> Ebits - k; 92556731Sbostic goto ret_d; 92656731Sbostic } 92756731Sbostic z = xa > xa0 ? *--xa : 0; 92856731Sbostic if (k -= Ebits) { 92956731Sbostic d0 = Exp_1 | y << k | z >> 32 - k; 93056731Sbostic y = xa > xa0 ? *--xa : 0; 93156731Sbostic d1 = z << k | y >> 32 - k; 93256731Sbostic } else { 93356731Sbostic d0 = Exp_1 | y; 93456731Sbostic d1 = z; 93556731Sbostic } 93656731Sbostic #else 93756731Sbostic if (k < Ebits + 16) { 93856731Sbostic z = xa > xa0 ? *--xa : 0; 93956731Sbostic d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; 94056731Sbostic w = xa > xa0 ? *--xa : 0; 94156731Sbostic y = xa > xa0 ? *--xa : 0; 94256731Sbostic d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; 94356731Sbostic goto ret_d; 94456731Sbostic } 94556731Sbostic z = xa > xa0 ? *--xa : 0; 94656731Sbostic w = xa > xa0 ? *--xa : 0; 94756731Sbostic k -= Ebits + 16; 94856731Sbostic d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; 94956731Sbostic y = xa > xa0 ? *--xa : 0; 95056731Sbostic d1 = w << k + 16 | y << k; 95156731Sbostic #endif 95256731Sbostic ret_d: 95356731Sbostic #ifdef VAX 95456731Sbostic word0(d) = d0 >> 16 | d0 << 16; 95556731Sbostic word1(d) = d1 >> 16 | d1 << 16; 95656731Sbostic #else 95756731Sbostic #undef d0 95856731Sbostic #undef d1 95956731Sbostic #endif 96056731Sbostic return d; 96156731Sbostic } 96256731Sbostic 96356731Sbostic static Bigint * 96456731Sbostic d2b 96556731Sbostic #ifdef KR_headers 96656731Sbostic (d, e, bits) double d; int *e, *bits; 96756731Sbostic #else 96856731Sbostic (double d, int *e, int *bits) 96956731Sbostic #endif 97056731Sbostic { 97156731Sbostic Bigint *b; 97256731Sbostic int de, i, k; 97356731Sbostic unsigned long *x, y, z; 97456731Sbostic #ifdef VAX 97556731Sbostic unsigned long d0, d1; 97656731Sbostic d0 = word0(d) >> 16 | word0(d) << 16; 97756731Sbostic d1 = word1(d) >> 16 | word1(d) << 16; 97856731Sbostic #else 97956731Sbostic #define d0 word0(d) 98056731Sbostic #define d1 word1(d) 98156731Sbostic #endif 98256731Sbostic 98356731Sbostic #ifdef Pack_32 98456731Sbostic b = Balloc(1); 98556731Sbostic #else 98656731Sbostic b = Balloc(2); 98756731Sbostic #endif 98856731Sbostic x = b->x; 98956731Sbostic 99056731Sbostic z = d0 & Frac_mask; 99156731Sbostic d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ 99256731Sbostic #ifdef Sudden_Underflow 99356731Sbostic de = (int)(d0 >> Exp_shift); 99456731Sbostic #ifndef IBM 99556731Sbostic z |= Exp_msk11; 99656731Sbostic #endif 99756731Sbostic #else 99856731Sbostic if (de = (int)(d0 >> Exp_shift)) 99956731Sbostic z |= Exp_msk1; 100056731Sbostic #endif 100156731Sbostic #ifdef Pack_32 100256731Sbostic if (y = d1) { 100356731Sbostic if (k = lo0bits(&y)) { 100456731Sbostic x[0] = y | z << 32 - k; 100556731Sbostic z >>= k; 100656731Sbostic } 100756731Sbostic else 100856731Sbostic x[0] = y; 100956731Sbostic i = b->wds = (x[1] = z) ? 2 : 1; 101056731Sbostic } else { 101156731Sbostic #ifdef DEBUG 101256731Sbostic if (!z) 101356731Sbostic Bug("Zero passed to d2b"); 101456731Sbostic #endif 101556731Sbostic k = lo0bits(&z); 101656731Sbostic x[0] = z; 101756731Sbostic i = b->wds = 1; 101856731Sbostic k += 32; 101956731Sbostic } 102056731Sbostic #else 102156731Sbostic if (y = d1) { 102256731Sbostic if (k = lo0bits(&y)) 102356731Sbostic if (k >= 16) { 102456731Sbostic x[0] = y | z << 32 - k & 0xffff; 102556731Sbostic x[1] = z >> k - 16 & 0xffff; 102656731Sbostic x[2] = z >> k; 102756731Sbostic i = 2; 102856731Sbostic } else { 102956731Sbostic x[0] = y & 0xffff; 103056731Sbostic x[1] = y >> 16 | z << 16 - k & 0xffff; 103156731Sbostic x[2] = z >> k & 0xffff; 103256731Sbostic x[3] = z >> k+16; 103356731Sbostic i = 3; 103456731Sbostic } 103556731Sbostic else { 103656731Sbostic x[0] = y & 0xffff; 103756731Sbostic x[1] = y >> 16; 103856731Sbostic x[2] = z & 0xffff; 103956731Sbostic x[3] = z >> 16; 104056731Sbostic i = 3; 104156731Sbostic } 104256731Sbostic } else { 104356731Sbostic #ifdef DEBUG 104456731Sbostic if (!z) 104556731Sbostic Bug("Zero passed to d2b"); 104656731Sbostic #endif 104756731Sbostic k = lo0bits(&z); 104856731Sbostic if (k >= 16) { 104956731Sbostic x[0] = z; 105056731Sbostic i = 0; 105156731Sbostic } else { 105256731Sbostic x[0] = z & 0xffff; 105356731Sbostic x[1] = z >> 16; 105456731Sbostic i = 1; 105556731Sbostic } 105656731Sbostic k += 32; 105756731Sbostic } 105856731Sbostic while (!x[i]) 105956731Sbostic --i; 106056731Sbostic b->wds = i + 1; 106156731Sbostic #endif 106256731Sbostic #ifndef Sudden_Underflow 106356731Sbostic if (de) { 106456731Sbostic #endif 106556731Sbostic #ifdef IBM 106656731Sbostic *e = (de - Bias - (P-1) << 2) + k; 106756731Sbostic *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); 106856731Sbostic #else 106956731Sbostic *e = de - Bias - (P-1) + k; 107056731Sbostic *bits = P - k; 107156731Sbostic #endif 107256731Sbostic #ifndef Sudden_Underflow 107356731Sbostic } else { 107456731Sbostic *e = de - Bias - (P-1) + 1 + k; 107556731Sbostic #ifdef Pack_32 107656731Sbostic *bits = 32*i - hi0bits(x[i-1]); 107756731Sbostic #else 107856731Sbostic *bits = (i+2)*16 - hi0bits(x[i]); 107956731Sbostic #endif 108056731Sbostic } 108156731Sbostic #endif 108256731Sbostic return b; 108356731Sbostic } 108456731Sbostic #undef d0 108556731Sbostic #undef d1 108656731Sbostic 108756731Sbostic static double 108856731Sbostic ratio 108956731Sbostic #ifdef KR_headers 109056731Sbostic (a, b) Bigint *a, *b; 109156731Sbostic #else 109256731Sbostic (Bigint *a, Bigint *b) 109356731Sbostic #endif 109456731Sbostic { 109556731Sbostic double da, db; 109656731Sbostic int k, ka, kb; 109756731Sbostic 109856731Sbostic da = b2d(a, &ka); 109956731Sbostic db = b2d(b, &kb); 110056731Sbostic #ifdef Pack_32 110156731Sbostic k = ka - kb + 32*(a->wds - b->wds); 110256731Sbostic #else 110356731Sbostic k = ka - kb + 16*(a->wds - b->wds); 110456731Sbostic #endif 110556731Sbostic #ifdef IBM 110656731Sbostic if (k > 0) { 110756731Sbostic word0(da) += (k >> 2)*Exp_msk1; 110856731Sbostic if (k &= 3) 110956731Sbostic da *= 1 << k; 111056731Sbostic } else { 111156731Sbostic k = -k; 111256731Sbostic word0(db) += (k >> 2)*Exp_msk1; 111356731Sbostic if (k &= 3) 111456731Sbostic db *= 1 << k; 111556731Sbostic } 111656731Sbostic #else 111756731Sbostic if (k > 0) 111856731Sbostic word0(da) += k*Exp_msk1; 111956731Sbostic else { 112056731Sbostic k = -k; 112156731Sbostic word0(db) += k*Exp_msk1; 112256731Sbostic } 112356731Sbostic #endif 112456731Sbostic return da / db; 112556731Sbostic } 112656731Sbostic 112756731Sbostic static double 112856731Sbostic tens[] = { 112956731Sbostic 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 113056731Sbostic 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 113156731Sbostic 1e20, 1e21, 1e22 113256731Sbostic #ifdef VAX 113356731Sbostic , 1e23, 1e24 113456731Sbostic #endif 113556731Sbostic }; 113656731Sbostic 113756731Sbostic static double 113856731Sbostic #ifdef IEEE_Arith 113956731Sbostic bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; 114056731Sbostic static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; 114156731Sbostic #define n_bigtens 5 114256731Sbostic #else 114356731Sbostic #ifdef IBM 114456731Sbostic bigtens[] = { 1e16, 1e32, 1e64 }; 114556731Sbostic static double tinytens[] = { 1e-16, 1e-32, 1e-64 }; 114656731Sbostic #define n_bigtens 3 114756731Sbostic #else 114856731Sbostic bigtens[] = { 1e16, 1e32 }; 114956731Sbostic static double tinytens[] = { 1e-16, 1e-32 }; 115056731Sbostic #define n_bigtens 2 115156731Sbostic #endif 115256731Sbostic #endif 115356731Sbostic 115456731Sbostic double 115556731Sbostic strtod 115656731Sbostic #ifdef KR_headers 115756731Sbostic (s00, se) CONST char *s00; char **se; 115856731Sbostic #else 115956731Sbostic (CONST char *s00, char **se) 116056731Sbostic #endif 116156731Sbostic { 116256731Sbostic int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, 116356731Sbostic e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; 116456731Sbostic CONST char *s, *s0, *s1; 116556731Sbostic double aadj, aadj1, adj, rv, rv0; 116656731Sbostic long L; 116756731Sbostic unsigned long y, z; 116856731Sbostic Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; 116956731Sbostic sign = nz0 = nz = 0; 117056731Sbostic rv = 0.; 117156731Sbostic for (s = s00;;s++) switch(*s) { 117256731Sbostic case '-': 117356731Sbostic sign = 1; 117456731Sbostic /* no break */ 117556731Sbostic case '+': 117656731Sbostic if (*++s) 117756731Sbostic goto break2; 117856731Sbostic /* no break */ 117956731Sbostic case 0: 118056731Sbostic s = s00; 118156731Sbostic goto ret; 118256731Sbostic case '\t': 118356731Sbostic case '\n': 118456731Sbostic case '\v': 118556731Sbostic case '\f': 118656731Sbostic case '\r': 118756731Sbostic case ' ': 118856731Sbostic continue; 118956731Sbostic default: 119056731Sbostic goto break2; 119156731Sbostic } 119256731Sbostic break2: 119356731Sbostic if (*s == '0') { 119456731Sbostic nz0 = 1; 119556731Sbostic while (*++s == '0') ; 119656731Sbostic if (!*s) 119756731Sbostic goto ret; 119856731Sbostic } 119956731Sbostic s0 = s; 120056731Sbostic y = z = 0; 120156731Sbostic for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) 120256731Sbostic if (nd < 9) 120356731Sbostic y = 10*y + c - '0'; 120456731Sbostic else if (nd < 16) 120556731Sbostic z = 10*z + c - '0'; 120656731Sbostic nd0 = nd; 120756731Sbostic if (c == '.') { 120856731Sbostic c = *++s; 120956731Sbostic if (!nd) { 121056731Sbostic for (; c == '0'; c = *++s) 121156731Sbostic nz++; 121256731Sbostic if (c > '0' && c <= '9') { 121356731Sbostic s0 = s; 121456731Sbostic nf += nz; 121556731Sbostic nz = 0; 121656731Sbostic goto have_dig; 121756731Sbostic } 121856731Sbostic goto dig_done; 121956731Sbostic } 122056731Sbostic for (; c >= '0' && c <= '9'; c = *++s) { 122156731Sbostic have_dig: 122256731Sbostic nz++; 122356731Sbostic if (c -= '0') { 122456731Sbostic nf += nz; 122556731Sbostic for (i = 1; i < nz; i++) 122656731Sbostic if (nd++ < 9) 122756731Sbostic y *= 10; 122856731Sbostic else if (nd <= DBL_DIG + 1) 122956731Sbostic z *= 10; 123056731Sbostic if (nd++ < 9) 123156731Sbostic y = 10*y + c; 123256731Sbostic else if (nd <= DBL_DIG + 1) 123356731Sbostic z = 10*z + c; 123456731Sbostic nz = 0; 123556731Sbostic } 123656731Sbostic } 123756731Sbostic } 123856731Sbostic dig_done: 123956731Sbostic e = 0; 124056731Sbostic if (c == 'e' || c == 'E') { 124156731Sbostic if (!nd && !nz && !nz0) { 124256731Sbostic s = s00; 124356731Sbostic goto ret; 124456731Sbostic } 124556731Sbostic s00 = s; 124656731Sbostic esign = 0; 124756731Sbostic switch(c = *++s) { 124856731Sbostic case '-': 124956731Sbostic esign = 1; 125056731Sbostic case '+': 125156731Sbostic c = *++s; 125256731Sbostic } 125356731Sbostic if (c >= '0' && c <= '9') { 125456731Sbostic while (c == '0') 125556731Sbostic c = *++s; 125656731Sbostic if (c > '0' && c <= '9') { 125756731Sbostic L = c - '0'; 125856731Sbostic s1 = s; 125956731Sbostic while ((c = *++s) >= '0' && c <= '9') 126056731Sbostic L = 10*L + c - '0'; 126156731Sbostic if (s - s1 > 8 || L > 19999) 126256731Sbostic /* Avoid confusion from exponents 126356731Sbostic * so large that e might overflow. 126456731Sbostic */ 126556731Sbostic e = 19999; /* safe for 16 bit ints */ 126656731Sbostic else 126756731Sbostic e = (int)L; 126856731Sbostic if (esign) 126956731Sbostic e = -e; 127056731Sbostic } else 127156731Sbostic e = 0; 127256731Sbostic } else 127356731Sbostic s = s00; 127456731Sbostic } 127556731Sbostic if (!nd) { 127656731Sbostic if (!nz && !nz0) 127756731Sbostic s = s00; 127856731Sbostic goto ret; 127956731Sbostic } 128056731Sbostic e1 = e -= nf; 128156731Sbostic 128256731Sbostic /* Now we have nd0 digits, starting at s0, followed by a 128356731Sbostic * decimal point, followed by nd-nd0 digits. The number we're 128456731Sbostic * after is the integer represented by those digits times 128556731Sbostic * 10**e */ 128656731Sbostic 128756731Sbostic if (!nd0) 128856731Sbostic nd0 = nd; 128956731Sbostic k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; 129056731Sbostic rv = y; 129156731Sbostic if (k > 9) 129256731Sbostic rv = tens[k - 9] * rv + z; 129356731Sbostic if (nd <= DBL_DIG 129456731Sbostic #ifndef RND_PRODQUOT 129556731Sbostic && FLT_ROUNDS == 1 129656731Sbostic #endif 129756731Sbostic ) { 129856731Sbostic if (!e) 129956731Sbostic goto ret; 130056731Sbostic if (e > 0) { 130156731Sbostic if (e <= Ten_pmax) { 130256731Sbostic #ifdef VAX 130356731Sbostic goto vax_ovfl_check; 130456731Sbostic #else 130556731Sbostic /* rv = */ rounded_product(rv, tens[e]); 130656731Sbostic goto ret; 130756731Sbostic #endif 130856731Sbostic } 130956731Sbostic i = DBL_DIG - nd; 131056731Sbostic if (e <= Ten_pmax + i) { 131156731Sbostic /* A fancier test would sometimes let us do 131256731Sbostic * this for larger i values. 131356731Sbostic */ 131456731Sbostic e -= i; 131556731Sbostic rv *= tens[i]; 131656731Sbostic #ifdef VAX 131756731Sbostic /* VAX exponent range is so narrow we must 131856731Sbostic * worry about overflow here... 131956731Sbostic */ 132056731Sbostic vax_ovfl_check: 132156731Sbostic word0(rv) -= P*Exp_msk1; 132256731Sbostic /* rv = */ rounded_product(rv, tens[e]); 132356731Sbostic if ((word0(rv) & Exp_mask) 132456731Sbostic > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) 132556731Sbostic goto ovfl; 132656731Sbostic word0(rv) += P*Exp_msk1; 132756731Sbostic #else 132856731Sbostic /* rv = */ rounded_product(rv, tens[e]); 132956731Sbostic #endif 133056731Sbostic goto ret; 133156731Sbostic } 133256731Sbostic } 133356731Sbostic #ifndef Inaccurate_Divide 133456731Sbostic else if (e >= -Ten_pmax) { 133556731Sbostic /* rv = */ rounded_quotient(rv, tens[-e]); 133656731Sbostic goto ret; 133756731Sbostic } 133856731Sbostic #endif 133956731Sbostic } 134056731Sbostic e1 += nd - k; 134156731Sbostic 134256731Sbostic /* Get starting approximation = rv * 10**e1 */ 134356731Sbostic 134456731Sbostic if (e1 > 0) { 134556731Sbostic if (i = e1 & 15) 134656731Sbostic rv *= tens[i]; 134756731Sbostic if (e1 &= ~15) { 134856731Sbostic if (e1 > DBL_MAX_10_EXP) { 134956731Sbostic ovfl: 135056731Sbostic errno = ERANGE; 135156731Sbostic #ifdef __STDC__ 135256731Sbostic rv = HUGE_VAL; 135356731Sbostic #else 135456731Sbostic /* Can't trust HUGE_VAL */ 135556731Sbostic #ifdef IEEE_Arith 135656731Sbostic word0(rv) = Exp_mask; 135756731Sbostic word1(rv) = 0; 135856731Sbostic #else 135956731Sbostic word0(rv) = Big0; 136056731Sbostic word1(rv) = Big1; 136156731Sbostic #endif 136256731Sbostic #endif 136356731Sbostic goto ret; 136456731Sbostic } 136556731Sbostic if (e1 >>= 4) { 136656731Sbostic for (j = 0; e1 > 1; j++, e1 >>= 1) 136756731Sbostic if (e1 & 1) 136856731Sbostic rv *= bigtens[j]; 136956731Sbostic /* The last multiplication could overflow. */ 137056731Sbostic word0(rv) -= P*Exp_msk1; 137156731Sbostic rv *= bigtens[j]; 137256731Sbostic if ((z = word0(rv) & Exp_mask) 137356731Sbostic > Exp_msk1*(DBL_MAX_EXP+Bias-P)) 137456731Sbostic goto ovfl; 137556731Sbostic if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { 137656731Sbostic /* set to largest number */ 137756731Sbostic /* (Can't trust DBL_MAX) */ 137856731Sbostic word0(rv) = Big0; 137956731Sbostic word1(rv) = Big1; 138056731Sbostic } 138156731Sbostic else 138256731Sbostic word0(rv) += P*Exp_msk1; 138356731Sbostic } 138456731Sbostic } 138556731Sbostic } else if (e1 < 0) { 138656731Sbostic e1 = -e1; 138756731Sbostic if (i = e1 & 15) 138856731Sbostic rv /= tens[i]; 138956731Sbostic if (e1 &= ~15) { 139056731Sbostic e1 >>= 4; 139156731Sbostic for (j = 0; e1 > 1; j++, e1 >>= 1) 139256731Sbostic if (e1 & 1) 139356731Sbostic rv *= tinytens[j]; 139456731Sbostic /* The last multiplication could underflow. */ 139556731Sbostic rv0 = rv; 139656731Sbostic rv *= tinytens[j]; 139756731Sbostic if (!rv) { 139856731Sbostic rv = 2.*rv0; 139956731Sbostic rv *= tinytens[j]; 140056731Sbostic if (!rv) { 140156731Sbostic undfl: 140256731Sbostic rv = 0.; 140356731Sbostic errno = ERANGE; 140456731Sbostic goto ret; 140556731Sbostic } 140656731Sbostic word0(rv) = Tiny0; 140756731Sbostic word1(rv) = Tiny1; 140856731Sbostic /* The refinement below will clean 140956731Sbostic * this approximation up. 141056731Sbostic */ 141156731Sbostic } 141256731Sbostic } 141356731Sbostic } 141456731Sbostic 141556731Sbostic /* Now the hard part -- adjusting rv to the correct value.*/ 141656731Sbostic 141756731Sbostic /* Put digits into bd: true value = bd * 10^e */ 141856731Sbostic 141956731Sbostic bd0 = s2b(s0, nd0, nd, y); 142056731Sbostic 142156731Sbostic for (;;) { 142256731Sbostic bd = Balloc(bd0->k); 142356731Sbostic Bcopy(bd, bd0); 142456731Sbostic bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ 142556731Sbostic bs = i2b(1); 142656731Sbostic 142756731Sbostic if (e >= 0) { 142856731Sbostic bb2 = bb5 = 0; 142956731Sbostic bd2 = bd5 = e; 143056731Sbostic } else { 143156731Sbostic bb2 = bb5 = -e; 143256731Sbostic bd2 = bd5 = 0; 143356731Sbostic } 143456731Sbostic if (bbe >= 0) 143556731Sbostic bb2 += bbe; 143656731Sbostic else 143756731Sbostic bd2 -= bbe; 143856731Sbostic bs2 = bb2; 143956731Sbostic #ifdef Sudden_Underflow 144056731Sbostic #ifdef IBM 144156731Sbostic j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); 144256731Sbostic #else 144356731Sbostic j = P + 1 - bbbits; 144456731Sbostic #endif 144556731Sbostic #else 144656731Sbostic i = bbe + bbbits - 1; /* logb(rv) */ 144756731Sbostic if (i < Emin) /* denormal */ 144856731Sbostic j = bbe + (P-Emin); 144956731Sbostic else 145056731Sbostic j = P + 1 - bbbits; 145156731Sbostic #endif 145256731Sbostic bb2 += j; 145356731Sbostic bd2 += j; 145456731Sbostic i = bb2 < bd2 ? bb2 : bd2; 145556731Sbostic if (i > bs2) 145656731Sbostic i = bs2; 145756731Sbostic if (i > 0) { 145856731Sbostic bb2 -= i; 145956731Sbostic bd2 -= i; 146056731Sbostic bs2 -= i; 146156731Sbostic } 146256731Sbostic if (bb5 > 0) { 146356731Sbostic bs = pow5mult(bs, bb5); 146456731Sbostic bb1 = mult(bs, bb); 146556731Sbostic Bfree(bb); 146656731Sbostic bb = bb1; 146756731Sbostic } 146856731Sbostic if (bb2 > 0) 146956731Sbostic bb = lshift(bb, bb2); 147056731Sbostic if (bd5 > 0) 147156731Sbostic bd = pow5mult(bd, bd5); 147256731Sbostic if (bd2 > 0) 147356731Sbostic bd = lshift(bd, bd2); 147456731Sbostic if (bs2 > 0) 147556731Sbostic bs = lshift(bs, bs2); 147656731Sbostic delta = diff(bb, bd); 147756731Sbostic dsign = delta->sign; 147856731Sbostic delta->sign = 0; 147956731Sbostic i = cmp(delta, bs); 148056731Sbostic if (i < 0) { 148156731Sbostic /* Error is less than half an ulp -- check for 148256731Sbostic * special case of mantissa a power of two. 148356731Sbostic */ 148456731Sbostic if (dsign || word1(rv) || word0(rv) & Bndry_mask) 148556731Sbostic break; 148656731Sbostic delta = lshift(delta,Log2P); 148756731Sbostic if (cmp(delta, bs) > 0) 148856731Sbostic goto drop_down; 148956731Sbostic break; 149056731Sbostic } 149156731Sbostic if (i == 0) { 149256731Sbostic /* exactly half-way between */ 149356731Sbostic if (dsign) { 149456731Sbostic if ((word0(rv) & Bndry_mask1) == Bndry_mask1 149556731Sbostic && word1(rv) == 0xffffffff) { 149656731Sbostic /*boundary case -- increment exponent*/ 149756731Sbostic word0(rv) = (word0(rv) & Exp_mask) 149856731Sbostic + Exp_msk1 149956731Sbostic #ifdef IBM 150056731Sbostic | Exp_msk1 >> 4 150156731Sbostic #endif 150256731Sbostic ; 150356731Sbostic word1(rv) = 0; 150456731Sbostic break; 150556731Sbostic } 150656731Sbostic } else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { 150756731Sbostic drop_down: 150856731Sbostic /* boundary case -- decrement exponent */ 150956731Sbostic #ifdef Sudden_Underflow 151056731Sbostic L = word0(rv) & Exp_mask; 151156731Sbostic #ifdef IBM 151256731Sbostic if (L < Exp_msk1) 151356731Sbostic #else 151456731Sbostic if (L <= Exp_msk1) 151556731Sbostic #endif 151656731Sbostic goto undfl; 151756731Sbostic L -= Exp_msk1; 151856731Sbostic #else 151956731Sbostic L = (word0(rv) & Exp_mask) - Exp_msk1; 152056731Sbostic #endif 152156731Sbostic word0(rv) = L | Bndry_mask1; 152256731Sbostic word1(rv) = 0xffffffff; 152356731Sbostic #ifdef IBM 152456731Sbostic goto cont; 152556731Sbostic #else 152656731Sbostic break; 152756731Sbostic #endif 152856731Sbostic } 152956731Sbostic #ifndef ROUND_BIASED 153056731Sbostic if (!(word1(rv) & LSB)) 153156731Sbostic break; 153256731Sbostic #endif 153356731Sbostic if (dsign) 153456731Sbostic rv += ulp(rv); 153556731Sbostic #ifndef ROUND_BIASED 153656731Sbostic else { 153756731Sbostic rv -= ulp(rv); 153856731Sbostic #ifndef Sudden_Underflow 153956731Sbostic if (!rv) 154056731Sbostic goto undfl; 154156731Sbostic #endif 154256731Sbostic } 154356731Sbostic #endif 154456731Sbostic break; 154556731Sbostic } 154656731Sbostic if ((aadj = ratio(delta, bs)) <= 2.) { 154756731Sbostic if (dsign) 154856731Sbostic aadj = aadj1 = 1.; 154956731Sbostic else if (word1(rv) || word0(rv) & Bndry_mask) { 155056731Sbostic #ifndef Sudden_Underflow 155156731Sbostic if (word1(rv) == Tiny1 && !word0(rv)) 155256731Sbostic goto undfl; 155356731Sbostic #endif 155456731Sbostic aadj = 1.; 155556731Sbostic aadj1 = -1.; 155656731Sbostic } else { 155756731Sbostic /* special case -- power of FLT_RADIX to be */ 155856731Sbostic /* rounded down... */ 155956731Sbostic 156056731Sbostic if (aadj < 2./FLT_RADIX) 156156731Sbostic aadj = 1./FLT_RADIX; 156256731Sbostic else 156356731Sbostic aadj *= 0.5; 156456731Sbostic aadj1 = -aadj; 156556731Sbostic } 156656731Sbostic } else { 156756731Sbostic aadj *= 0.5; 156856731Sbostic aadj1 = dsign ? aadj : -aadj; 156956731Sbostic #ifdef Check_FLT_ROUNDS 157056731Sbostic switch(FLT_ROUNDS) { 157156731Sbostic case 2: /* towards +infinity */ 157256731Sbostic aadj1 -= 0.5; 157356731Sbostic break; 157456731Sbostic case 0: /* towards 0 */ 157556731Sbostic case 3: /* towards -infinity */ 157656731Sbostic aadj1 += 0.5; 157756731Sbostic } 157856731Sbostic #else 157956731Sbostic if (FLT_ROUNDS == 0) 158056731Sbostic aadj1 += 0.5; 158156731Sbostic #endif 158256731Sbostic } 158356731Sbostic y = word0(rv) & Exp_mask; 158456731Sbostic 158556731Sbostic /* Check for overflow */ 158656731Sbostic 158756731Sbostic if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { 158856731Sbostic rv0 = rv; 158956731Sbostic word0(rv) -= P*Exp_msk1; 159056731Sbostic adj = aadj1 * ulp(rv); 159156731Sbostic rv += adj; 159256731Sbostic if ((word0(rv) & Exp_mask) >= 159356731Sbostic Exp_msk1*(DBL_MAX_EXP+Bias-P)) { 159456731Sbostic if (word0(rv0) == Big0 && word1(rv0) == Big1) 159556731Sbostic goto ovfl; 159656731Sbostic word0(rv) = Big0; 159756731Sbostic word1(rv) = Big1; 159856731Sbostic goto cont; 159956731Sbostic } else 160056731Sbostic word0(rv) += P*Exp_msk1; 160156731Sbostic } else { 160256731Sbostic #ifdef Sudden_Underflow 160356731Sbostic if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { 160456731Sbostic rv0 = rv; 160556731Sbostic word0(rv) += P*Exp_msk1; 160656731Sbostic adj = aadj1 * ulp(rv); 160756731Sbostic rv += adj; 160856731Sbostic #ifdef IBM 160956731Sbostic if ((word0(rv) & Exp_mask) < P*Exp_msk1) 161056731Sbostic #else 161156731Sbostic if ((word0(rv) & Exp_mask) <= P*Exp_msk1) 161256731Sbostic #endif 161356731Sbostic { 161456731Sbostic if (word0(rv0) == Tiny0 161556731Sbostic && word1(rv0) == Tiny1) 161656731Sbostic goto undfl; 161756731Sbostic word0(rv) = Tiny0; 161856731Sbostic word1(rv) = Tiny1; 161956731Sbostic goto cont; 162056731Sbostic } else 162156731Sbostic word0(rv) -= P*Exp_msk1; 162256731Sbostic } else { 162356731Sbostic adj = aadj1 * ulp(rv); 162456731Sbostic rv += adj; 162556731Sbostic } 162656731Sbostic #else 162756731Sbostic /* Compute adj so that the IEEE rounding rules will 162856731Sbostic * correctly round rv + adj in some half-way cases. 162956731Sbostic * If rv * ulp(rv) is denormalized (i.e., 163056731Sbostic * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid 163156731Sbostic * trouble from bits lost to denormalization; 163256731Sbostic * example: 1.2e-307 . 163356731Sbostic */ 163456731Sbostic if (y <= (P-1)*Exp_msk1 && aadj >= 1.) { 163556731Sbostic aadj1 = (double)(int)(aadj + 0.5); 163656731Sbostic if (!dsign) 163756731Sbostic aadj1 = -aadj1; 163856731Sbostic } 163956731Sbostic adj = aadj1 * ulp(rv); 164056731Sbostic rv += adj; 164156731Sbostic #endif 164256731Sbostic } 164356731Sbostic z = word0(rv) & Exp_mask; 164456731Sbostic if (y == z) { 164556731Sbostic /* Can we stop now? */ 164656731Sbostic L = aadj; 164756731Sbostic aadj -= L; 164856731Sbostic /* The tolerances below are conservative. */ 164956731Sbostic if (dsign || word1(rv) || word0(rv) & Bndry_mask) { 165056731Sbostic if (aadj < .4999999 || aadj > .5000001) 165156731Sbostic break; 165256731Sbostic } else if (aadj < .4999999/FLT_RADIX) 165356731Sbostic break; 165456731Sbostic } 165556731Sbostic cont: 165656731Sbostic Bfree(bb); 165756731Sbostic Bfree(bd); 165856731Sbostic Bfree(bs); 165956731Sbostic Bfree(delta); 166056731Sbostic } 166156731Sbostic Bfree(bb); 166256731Sbostic Bfree(bd); 166356731Sbostic Bfree(bs); 166456731Sbostic Bfree(bd0); 166556731Sbostic Bfree(delta); 166656731Sbostic ret: 166756731Sbostic if (se) 166856731Sbostic *se = (char *)s; 166956731Sbostic return sign ? -rv : rv; 167056731Sbostic } 167156731Sbostic 167256731Sbostic static int 167356731Sbostic quorem 167456731Sbostic #ifdef KR_headers 167556731Sbostic (b, S) Bigint *b, *S; 167656731Sbostic #else 167756731Sbostic (Bigint *b, Bigint *S) 167856731Sbostic #endif 167956731Sbostic { 168056731Sbostic int n; 168156731Sbostic long borrow, y; 168256731Sbostic unsigned long carry, q, ys; 168356731Sbostic unsigned long *bx, *bxe, *sx, *sxe; 168456731Sbostic #ifdef Pack_32 168556731Sbostic long z; 168656731Sbostic unsigned long si, zs; 168756731Sbostic #endif 168856731Sbostic 168956731Sbostic n = S->wds; 169056731Sbostic #ifdef DEBUG 169156731Sbostic /*debug*/ if (b->wds > n) 169256731Sbostic /*debug*/ Bug("oversize b in quorem"); 169356731Sbostic #endif 169456731Sbostic if (b->wds < n) 169556731Sbostic return 0; 169656731Sbostic sx = S->x; 169756731Sbostic sxe = sx + --n; 169856731Sbostic bx = b->x; 169956731Sbostic bxe = bx + n; 170056731Sbostic q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ 170156731Sbostic #ifdef DEBUG 170256731Sbostic /*debug*/ if (q > 9) 170356731Sbostic /*debug*/ Bug("oversized quotient in quorem"); 170456731Sbostic #endif 170556731Sbostic if (q) { 170656731Sbostic borrow = 0; 170756731Sbostic carry = 0; 170856731Sbostic do { 170956731Sbostic #ifdef Pack_32 171056731Sbostic si = *sx++; 171156731Sbostic ys = (si & 0xffff) * q + carry; 171256731Sbostic zs = (si >> 16) * q + (ys >> 16); 171356731Sbostic carry = zs >> 16; 171456731Sbostic y = (*bx & 0xffff) - (ys & 0xffff) + borrow; 171556731Sbostic borrow = y >> 16; 171656731Sbostic Sign_Extend(borrow, y); 171756731Sbostic z = (*bx >> 16) - (zs & 0xffff) + borrow; 171856731Sbostic borrow = z >> 16; 171956731Sbostic Sign_Extend(borrow, z); 172056731Sbostic Storeinc(bx, z, y); 172156731Sbostic #else 172256731Sbostic ys = *sx++ * q + carry; 172356731Sbostic carry = ys >> 16; 172456731Sbostic y = *bx - (ys & 0xffff) + borrow; 172556731Sbostic borrow = y >> 16; 172656731Sbostic Sign_Extend(borrow, y); 172756731Sbostic *bx++ = y & 0xffff; 172856731Sbostic #endif 172956731Sbostic } while (sx <= sxe); 173056731Sbostic if (!*bxe) { 173156731Sbostic bx = b->x; 173256731Sbostic while (--bxe > bx && !*bxe) 173356731Sbostic --n; 173456731Sbostic b->wds = n; 173556731Sbostic } 173656731Sbostic } 173756731Sbostic if (cmp(b, S) >= 0) { 173856731Sbostic q++; 173956731Sbostic borrow = 0; 174056731Sbostic carry = 0; 174156731Sbostic bx = b->x; 174256731Sbostic sx = S->x; 174356731Sbostic do { 174456731Sbostic #ifdef Pack_32 174556731Sbostic si = *sx++; 174656731Sbostic ys = (si & 0xffff) + carry; 174756731Sbostic zs = (si >> 16) + (ys >> 16); 174856731Sbostic carry = zs >> 16; 174956731Sbostic y = (*bx & 0xffff) - (ys & 0xffff) + borrow; 175056731Sbostic borrow = y >> 16; 175156731Sbostic Sign_Extend(borrow, y); 175256731Sbostic z = (*bx >> 16) - (zs & 0xffff) + borrow; 175356731Sbostic borrow = z >> 16; 175456731Sbostic Sign_Extend(borrow, z); 175556731Sbostic Storeinc(bx, z, y); 175656731Sbostic #else 175756731Sbostic ys = *sx++ + carry; 175856731Sbostic carry = ys >> 16; 175956731Sbostic y = *bx - (ys & 0xffff) + borrow; 176056731Sbostic borrow = y >> 16; 176156731Sbostic Sign_Extend(borrow, y); 176256731Sbostic *bx++ = y & 0xffff; 176356731Sbostic #endif 176456731Sbostic } while (sx <= sxe); 176556731Sbostic bx = b->x; 176656731Sbostic bxe = bx + n; 176756731Sbostic if (!*bxe) { 176856731Sbostic while (--bxe > bx && !*bxe) 176956731Sbostic --n; 177056731Sbostic b->wds = n; 177156731Sbostic } 177256731Sbostic } 177356731Sbostic return q; 177456731Sbostic } 177556731Sbostic 177656731Sbostic /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. 177756731Sbostic * 177856731Sbostic * Inspired by "How to Print Floating-Point Numbers Accurately" by 177956731Sbostic * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101]. 178056731Sbostic * 178156731Sbostic * Modifications: 178256731Sbostic * 1. Rather than iterating, we use a simple numeric overestimate 178356731Sbostic * to determine k = floor(log10(d)). We scale relevant 178456731Sbostic * quantities using O(log2(k)) rather than O(k) multiplications. 178556731Sbostic * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't 178656731Sbostic * try to generate digits strictly left to right. Instead, we 178756731Sbostic * compute with fewer bits and propagate the carry if necessary 178856731Sbostic * when rounding the final digit up. This is often faster. 178956731Sbostic * 3. Under the assumption that input will be rounded nearest, 179056731Sbostic * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. 179156731Sbostic * That is, we allow equality in stopping tests when the 179256731Sbostic * round-nearest rule will give the same floating-point value 179356731Sbostic * as would satisfaction of the stopping test with strict 179456731Sbostic * inequality. 179556731Sbostic * 4. We remove common factors of powers of 2 from relevant 179656731Sbostic * quantities. 179756731Sbostic * 5. When converting floating-point integers less than 1e16, 179856731Sbostic * we use floating-point arithmetic rather than resorting 179956731Sbostic * to multiple-precision integers. 180056731Sbostic * 6. When asked to produce fewer than 15 digits, we first try 180156731Sbostic * to get by with floating-point arithmetic; we resort to 180256731Sbostic * multiple-precision integer arithmetic only if we cannot 180356731Sbostic * guarantee that the floating-point calculation has given 180456731Sbostic * the correctly rounded result. For k requested digits and 180556731Sbostic * "uniformly" distributed input, the probability is 180656731Sbostic * something like 10^(k-15) that we must resort to the long 180756731Sbostic * calculation. 180856731Sbostic */ 180956731Sbostic 181056731Sbostic char * 181156731Sbostic __dtoa 181256731Sbostic #ifdef KR_headers 181356731Sbostic (d, mode, ndigits, decpt, sign, rve) 181456731Sbostic double d; int mode, ndigits, *decpt, *sign; char **rve; 181556731Sbostic #else 181656731Sbostic (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) 181756731Sbostic #endif 181856731Sbostic { 181956731Sbostic /* Arguments ndigits, decpt, sign are similar to those 182056731Sbostic of ecvt and fcvt; trailing zeros are suppressed from 182156731Sbostic the returned string. If not null, *rve is set to point 182256731Sbostic to the end of the return value. If d is +-Infinity or NaN, 182356731Sbostic then *decpt is set to 9999. 182456731Sbostic 182556731Sbostic mode: 182656731Sbostic 0 ==> shortest string that yields d when read in 182756731Sbostic and rounded to nearest. 182856731Sbostic 1 ==> like 0, but with Steele & White stopping rule; 182956731Sbostic e.g. with IEEE P754 arithmetic , mode 0 gives 183056731Sbostic 1e23 whereas mode 1 gives 9.999999999999999e22. 183156731Sbostic 2 ==> max(1,ndigits) significant digits. This gives a 183256731Sbostic return value similar to that of ecvt, except 183356731Sbostic that trailing zeros are suppressed. 183456731Sbostic 3 ==> through ndigits past the decimal point. This 183556731Sbostic gives a return value similar to that from fcvt, 183656731Sbostic except that trailing zeros are suppressed, and 183756731Sbostic ndigits can be negative. 183856731Sbostic 4-9 should give the same return values as 2-3, i.e., 183956731Sbostic 4 <= mode <= 9 ==> same return as mode 184056731Sbostic 2 + (mode & 1). These modes are mainly for 184156731Sbostic debugging; often they run slower but sometimes 184256731Sbostic faster than modes 2-3. 184356731Sbostic 4,5,8,9 ==> left-to-right digit generation. 184456731Sbostic 6-9 ==> don't try fast floating-point estimate 184556731Sbostic (if applicable). 184656731Sbostic 184756731Sbostic Values of mode other than 0-9 are treated as mode 0. 184856731Sbostic 184956731Sbostic Sufficient space is allocated to the return value 185056731Sbostic to hold the suppressed trailing zeros. 185156731Sbostic */ 185256731Sbostic 185356731Sbostic int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, 185456731Sbostic j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, 185556731Sbostic spec_case, try_quick; 185656731Sbostic long L; 185756731Sbostic #ifndef Sudden_Underflow 185856731Sbostic int denorm; 185956731Sbostic unsigned long x; 186056731Sbostic #endif 186156731Sbostic Bigint *b, *b1, *delta, *mlo, *mhi, *S; 186256731Sbostic double d2, ds, eps; 186356731Sbostic char *s, *s0; 186456731Sbostic static Bigint *result; 186556731Sbostic static int result_k; 186656731Sbostic 186756731Sbostic if (result) { 186856731Sbostic result->k = result_k; 186956731Sbostic result->maxwds = 1 << result_k; 187056731Sbostic Bfree(result); 187156731Sbostic result = 0; 187256731Sbostic } 187356731Sbostic 187456731Sbostic if (word0(d) & Sign_bit) { 187556731Sbostic /* set sign for everything, including 0's and NaNs */ 187656731Sbostic *sign = 1; 187756731Sbostic word0(d) &= ~Sign_bit; /* clear sign bit */ 187856731Sbostic } 187956731Sbostic else 188056731Sbostic *sign = 0; 188156731Sbostic 188256731Sbostic #if defined(IEEE_Arith) + defined(VAX) 188356731Sbostic #ifdef IEEE_Arith 188456731Sbostic if ((word0(d) & Exp_mask) == Exp_mask) 188556731Sbostic #else 188656731Sbostic if (word0(d) == 0x8000) 188756731Sbostic #endif 188856731Sbostic { 188956731Sbostic /* Infinity or NaN */ 189056731Sbostic *decpt = 9999; 189156731Sbostic s = 189256731Sbostic #ifdef IEEE_Arith 189356731Sbostic !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" : 189456731Sbostic #endif 189556731Sbostic "NaN"; 189656731Sbostic if (rve) 189756731Sbostic *rve = 189856731Sbostic #ifdef IEEE_Arith 189956731Sbostic s[3] ? s + 8 : 190056731Sbostic #endif 190156731Sbostic s + 3; 190256731Sbostic return s; 190356731Sbostic } 190456731Sbostic #endif 190556731Sbostic #ifdef IBM 190656731Sbostic d += 0; /* normalize */ 190756731Sbostic #endif 190856731Sbostic if (!d) { 190956731Sbostic *decpt = 1; 191056731Sbostic s = "0"; 191156731Sbostic if (rve) 191256731Sbostic *rve = s + 1; 191356731Sbostic return s; 191456731Sbostic } 191556731Sbostic 191656731Sbostic b = d2b(d, &be, &bbits); 191756731Sbostic #ifdef Sudden_Underflow 191856731Sbostic i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); 191956731Sbostic #else 192056731Sbostic if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { 192156731Sbostic #endif 192256731Sbostic d2 = d; 192356731Sbostic word0(d2) &= Frac_mask1; 192456731Sbostic word0(d2) |= Exp_11; 192556731Sbostic #ifdef IBM 192656731Sbostic if (j = 11 - hi0bits(word0(d2) & Frac_mask)) 192756731Sbostic d2 /= 1 << j; 192856731Sbostic #endif 192956731Sbostic 193056731Sbostic /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 193156731Sbostic * log10(x) = log(x) / log(10) 193256731Sbostic * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) 193356731Sbostic * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) 193456731Sbostic * 193556731Sbostic * This suggests computing an approximation k to log10(d) by 193656731Sbostic * 193756731Sbostic * k = (i - Bias)*0.301029995663981 193856731Sbostic * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); 193956731Sbostic * 194056731Sbostic * We want k to be too large rather than too small. 194156731Sbostic * The error in the first-order Taylor series approximation 194256731Sbostic * is in our favor, so we just round up the constant enough 194356731Sbostic * to compensate for any error in the multiplication of 194456731Sbostic * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, 194556731Sbostic * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, 194656731Sbostic * adding 1e-13 to the constant term more than suffices. 194756731Sbostic * Hence we adjust the constant term to 0.1760912590558. 194856731Sbostic * (We could get a more accurate k by invoking log10, 194956731Sbostic * but this is probably not worthwhile.) 195056731Sbostic */ 195156731Sbostic 195256731Sbostic i -= Bias; 195356731Sbostic #ifdef IBM 195456731Sbostic i <<= 2; 195556731Sbostic i += j; 195656731Sbostic #endif 195756731Sbostic #ifndef Sudden_Underflow 195856731Sbostic denorm = 0; 195956731Sbostic } else { 196056731Sbostic /* d is denormalized */ 196156731Sbostic 196256731Sbostic i = bbits + be + (Bias + (P-1) - 1); 196356731Sbostic x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 196456731Sbostic : word1(d) << 32 - i; 196556731Sbostic d2 = x; 196656731Sbostic word0(d2) -= 31*Exp_msk1; /* adjust exponent */ 196756731Sbostic i -= (Bias + (P-1) - 1) + 1; 196856731Sbostic denorm = 1; 196956731Sbostic } 197056731Sbostic #endif 197156731Sbostic ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; 197256731Sbostic k = (int)ds; 197356731Sbostic if (ds < 0. && ds != k) 197456731Sbostic k--; /* want k = floor(ds) */ 197556731Sbostic k_check = 1; 197656731Sbostic if (k >= 0 && k <= Ten_pmax) { 197756731Sbostic if (d < tens[k]) 197856731Sbostic k--; 197956731Sbostic k_check = 0; 198056731Sbostic } 198156731Sbostic j = bbits - i - 1; 198256731Sbostic if (j >= 0) { 198356731Sbostic b2 = 0; 198456731Sbostic s2 = j; 198556731Sbostic } else { 198656731Sbostic b2 = -j; 198756731Sbostic s2 = 0; 198856731Sbostic } 198956731Sbostic if (k >= 0) { 199056731Sbostic b5 = 0; 199156731Sbostic s5 = k; 199256731Sbostic s2 += k; 199356731Sbostic } else { 199456731Sbostic b2 -= k; 199556731Sbostic b5 = -k; 199656731Sbostic s5 = 0; 199756731Sbostic } 199856731Sbostic if (mode < 0 || mode > 9) 199956731Sbostic mode = 0; 200056731Sbostic try_quick = 1; 200156731Sbostic if (mode > 5) { 200256731Sbostic mode -= 4; 200356731Sbostic try_quick = 0; 200456731Sbostic } 200556731Sbostic leftright = 1; 200656731Sbostic switch(mode) { 200756731Sbostic case 0: 200856731Sbostic case 1: 200956731Sbostic ilim = ilim1 = -1; 201056731Sbostic i = 18; 201156731Sbostic ndigits = 0; 201256731Sbostic break; 201356731Sbostic case 2: 201456731Sbostic leftright = 0; 201556731Sbostic /* no break */ 201656731Sbostic case 4: 201756731Sbostic if (ndigits <= 0) 201856731Sbostic ndigits = 1; 201956731Sbostic ilim = ilim1 = i = ndigits; 202056731Sbostic break; 202156731Sbostic case 3: 202256731Sbostic leftright = 0; 202356731Sbostic /* no break */ 202456731Sbostic case 5: 202556731Sbostic i = ndigits + k + 1; 202656731Sbostic ilim = i; 202756731Sbostic ilim1 = i - 1; 202856731Sbostic if (i <= 0) 202956731Sbostic i = 1; 203056731Sbostic } 203156731Sbostic j = sizeof(unsigned long); 203256731Sbostic for (result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i; 203356731Sbostic j <<= 1) result_k++; 203456731Sbostic result = Balloc(result_k); 203556731Sbostic s = s0 = (char *)result; 203656731Sbostic 203756731Sbostic if (ilim >= 0 && ilim <= Quick_max && try_quick) { 203856731Sbostic 203956731Sbostic /* Try to get by with floating-point arithmetic. */ 204056731Sbostic 204156731Sbostic i = 0; 204256731Sbostic d2 = d; 204356731Sbostic k0 = k; 204456731Sbostic ilim0 = ilim; 204556731Sbostic ieps = 2; /* conservative */ 204656731Sbostic if (k > 0) { 204756731Sbostic ds = tens[k&0xf]; 204856731Sbostic j = k >> 4; 204956731Sbostic if (j & Bletch) { 205056731Sbostic /* prevent overflows */ 205156731Sbostic j &= Bletch - 1; 205256731Sbostic d /= bigtens[n_bigtens-1]; 205356731Sbostic ieps++; 205456731Sbostic } 205556731Sbostic for (; j; j >>= 1, i++) 205656731Sbostic if (j & 1) { 205756731Sbostic ieps++; 205856731Sbostic ds *= bigtens[i]; 205956731Sbostic } 206056731Sbostic d /= ds; 206156731Sbostic } else if (j1 = -k) { 206256731Sbostic d *= tens[j1 & 0xf]; 206356731Sbostic for (j = j1 >> 4; j; j >>= 1, i++) 206456731Sbostic if (j & 1) { 206556731Sbostic ieps++; 206656731Sbostic d *= bigtens[i]; 206756731Sbostic } 206856731Sbostic } 206956731Sbostic if (k_check && d < 1. && ilim > 0) { 207056731Sbostic if (ilim1 <= 0) 207156731Sbostic goto fast_failed; 207256731Sbostic ilim = ilim1; 207356731Sbostic k--; 207456731Sbostic d *= 10.; 207556731Sbostic ieps++; 207656731Sbostic } 207756731Sbostic eps = ieps*d + 7.; 207856731Sbostic word0(eps) -= (P-1)*Exp_msk1; 207956731Sbostic if (ilim == 0) { 208056731Sbostic S = mhi = 0; 208156731Sbostic d -= 5.; 208256731Sbostic if (d > eps) 208356731Sbostic goto one_digit; 208456731Sbostic if (d < -eps) 208556731Sbostic goto no_digits; 208656731Sbostic goto fast_failed; 208756731Sbostic } 208856731Sbostic #ifndef No_leftright 208956731Sbostic if (leftright) { 209056731Sbostic /* Use Steele & White method of only 209156731Sbostic * generating digits needed. 209256731Sbostic */ 209356731Sbostic eps = 0.5/tens[ilim-1] - eps; 209456731Sbostic for (i = 0;;) { 209556731Sbostic L = d; 209656731Sbostic d -= L; 209756731Sbostic *s++ = '0' + (int)L; 209856731Sbostic if (d < eps) 209956731Sbostic goto ret1; 210056731Sbostic if (1. - d < eps) 210156731Sbostic goto bump_up; 210256731Sbostic if (++i >= ilim) 210356731Sbostic break; 210456731Sbostic eps *= 10.; 210556731Sbostic d *= 10.; 210656731Sbostic } 210756731Sbostic } else { 210856731Sbostic #endif 210956731Sbostic /* Generate ilim digits, then fix them up. */ 211056731Sbostic eps *= tens[ilim-1]; 211156731Sbostic for (i = 1;; i++, d *= 10.) { 211256731Sbostic L = d; 211356731Sbostic d -= L; 211456731Sbostic *s++ = '0' + (int)L; 211556731Sbostic if (i == ilim) { 211656731Sbostic if (d > 0.5 + eps) 211756731Sbostic goto bump_up; 211856731Sbostic else if (d < 0.5 - eps) { 211956731Sbostic while (*--s == '0'); 212056731Sbostic s++; 212156731Sbostic goto ret1; 212256731Sbostic } 212356731Sbostic break; 212456731Sbostic } 212556731Sbostic } 212656731Sbostic #ifndef No_leftright 212756731Sbostic } 212856731Sbostic #endif 212956731Sbostic fast_failed: 213056731Sbostic s = s0; 213156731Sbostic d = d2; 213256731Sbostic k = k0; 213356731Sbostic ilim = ilim0; 213456731Sbostic } 213556731Sbostic 213656731Sbostic /* Do we have a "small" integer? */ 213756731Sbostic 213856731Sbostic if (be >= 0 && k <= Int_max) { 213956731Sbostic /* Yes. */ 214056731Sbostic ds = tens[k]; 214156731Sbostic if (ndigits < 0 && ilim <= 0) { 214256731Sbostic S = mhi = 0; 214356731Sbostic if (ilim < 0 || d <= 5*ds) 214456731Sbostic goto no_digits; 214556731Sbostic goto one_digit; 214656731Sbostic } 214756731Sbostic for (i = 1;; i++) { 214856731Sbostic L = d / ds; 214956731Sbostic d -= L*ds; 215056731Sbostic #ifdef Check_FLT_ROUNDS 215156731Sbostic /* If FLT_ROUNDS == 2, L will usually be high by 1 */ 215256731Sbostic if (d < 0) { 215356731Sbostic L--; 215456731Sbostic d += ds; 215556731Sbostic } 215656731Sbostic #endif 215756731Sbostic *s++ = '0' + (int)L; 215856731Sbostic if (i == ilim) { 215956731Sbostic d += d; 216056731Sbostic if (d > ds || d == ds && L & 1) { 216156731Sbostic bump_up: 216256731Sbostic while (*--s == '9') 216356731Sbostic if (s == s0) { 216456731Sbostic k++; 216556731Sbostic *s = '0'; 216656731Sbostic break; 216756731Sbostic } 216856731Sbostic ++*s++; 216956731Sbostic } 217056731Sbostic break; 217156731Sbostic } 217256731Sbostic if (!(d *= 10.)) 217356731Sbostic break; 217456731Sbostic } 217556731Sbostic goto ret1; 217656731Sbostic } 217756731Sbostic 217856731Sbostic m2 = b2; 217956731Sbostic m5 = b5; 218056731Sbostic mhi = mlo = 0; 218156731Sbostic if (leftright) { 218256731Sbostic if (mode < 2) { 218356731Sbostic i = 218456731Sbostic #ifndef Sudden_Underflow 218556731Sbostic denorm ? be + (Bias + (P-1) - 1 + 1) : 218656731Sbostic #endif 218756731Sbostic #ifdef IBM 218856731Sbostic 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); 218956731Sbostic #else 219056731Sbostic 1 + P - bbits; 219156731Sbostic #endif 219256731Sbostic } else { 219356731Sbostic j = ilim - 1; 219456731Sbostic if (m5 >= j) 219556731Sbostic m5 -= j; 219656731Sbostic else { 219756731Sbostic s5 += j -= m5; 219856731Sbostic b5 += j; 219956731Sbostic m5 = 0; 220056731Sbostic } 220156731Sbostic if ((i = ilim) < 0) { 220256731Sbostic m2 -= i; 220356731Sbostic i = 0; 220456731Sbostic } 220556731Sbostic } 220656731Sbostic b2 += i; 220756731Sbostic s2 += i; 220856731Sbostic mhi = i2b(1); 220956731Sbostic } 221056731Sbostic if (m2 > 0 && s2 > 0) { 221156731Sbostic i = m2 < s2 ? m2 : s2; 221256731Sbostic b2 -= i; 221356731Sbostic m2 -= i; 221456731Sbostic s2 -= i; 221556731Sbostic } 221656731Sbostic if (b5 > 0) { 221756731Sbostic if (leftright) { 221856731Sbostic if (m5 > 0) { 221956731Sbostic mhi = pow5mult(mhi, m5); 222056731Sbostic b1 = mult(mhi, b); 222156731Sbostic Bfree(b); 222256731Sbostic b = b1; 222356731Sbostic } 222456731Sbostic if (j = b5 - m5) 222556731Sbostic b = pow5mult(b, j); 222656731Sbostic } else 222756731Sbostic b = pow5mult(b, b5); 222856731Sbostic } 222956731Sbostic S = i2b(1); 223056731Sbostic if (s5 > 0) 223156731Sbostic S = pow5mult(S, s5); 223256731Sbostic 223356731Sbostic /* Check for special case that d is a normalized power of 2. */ 223456731Sbostic 223556731Sbostic if (mode < 2) { 223656731Sbostic if (!word1(d) && !(word0(d) & Bndry_mask) 223756731Sbostic #ifndef Sudden_Underflow 223856731Sbostic && word0(d) & Exp_mask 223956731Sbostic #endif 224056731Sbostic ) { 224156731Sbostic /* The special case */ 224256731Sbostic b2 += Log2P; 224356731Sbostic s2 += Log2P; 224456731Sbostic spec_case = 1; 224556731Sbostic } else 224656731Sbostic spec_case = 0; 224756731Sbostic } 224856731Sbostic 224956731Sbostic /* Arrange for convenient computation of quotients: 225056731Sbostic * shift left if necessary so divisor has 4 leading 0 bits. 225156731Sbostic * 225256731Sbostic * Perhaps we should just compute leading 28 bits of S once 225356731Sbostic * and for all and pass them and a shift to quorem, so it 225456731Sbostic * can do shifts and ors to compute the numerator for q. 225556731Sbostic */ 225656731Sbostic #ifdef Pack_32 225756731Sbostic if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) 225856731Sbostic i = 32 - i; 225956731Sbostic #else 226056731Sbostic if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) 226156731Sbostic i = 16 - i; 226256731Sbostic #endif 226356731Sbostic if (i > 4) { 226456731Sbostic i -= 4; 226556731Sbostic b2 += i; 226656731Sbostic m2 += i; 226756731Sbostic s2 += i; 226856731Sbostic } else if (i < 4) { 226956731Sbostic i += 28; 227056731Sbostic b2 += i; 227156731Sbostic m2 += i; 227256731Sbostic s2 += i; 227356731Sbostic } 227456731Sbostic if (b2 > 0) 227556731Sbostic b = lshift(b, b2); 227656731Sbostic if (s2 > 0) 227756731Sbostic S = lshift(S, s2); 227856731Sbostic if (k_check) { 227956731Sbostic if (cmp(b,S) < 0) { 228056731Sbostic k--; 228156731Sbostic b = multadd(b, 10, 0); /* we botched the k estimate */ 228256731Sbostic if (leftright) 228356731Sbostic mhi = multadd(mhi, 10, 0); 228456731Sbostic ilim = ilim1; 228556731Sbostic } 228656731Sbostic } 228756731Sbostic if (ilim <= 0 && mode > 2) { 228856731Sbostic if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { 228956731Sbostic /* no digits, fcvt style */ 229056731Sbostic no_digits: 229156731Sbostic k = -1 - ndigits; 229256731Sbostic goto ret; 229356731Sbostic } 229456731Sbostic one_digit: 229556731Sbostic *s++ = '1'; 229656731Sbostic k++; 229756731Sbostic goto ret; 229856731Sbostic } 229956731Sbostic if (leftright) { 230056731Sbostic if (m2 > 0) 230156731Sbostic mhi = lshift(mhi, m2); 230256731Sbostic 230356731Sbostic /* Compute mlo -- check for special case 230456731Sbostic * that d is a normalized power of 2. 230556731Sbostic */ 230656731Sbostic 230756731Sbostic mlo = mhi; 230856731Sbostic if (spec_case) { 230956731Sbostic mhi = Balloc(mhi->k); 231056731Sbostic Bcopy(mhi, mlo); 231156731Sbostic mhi = lshift(mhi, Log2P); 231256731Sbostic } 231356731Sbostic 231456731Sbostic for (i = 1;;i++) { 231556731Sbostic dig = quorem(b,S) + '0'; 231656731Sbostic /* Do we yet have the shortest decimal string 231756731Sbostic * that will round to d? 231856731Sbostic */ 231956731Sbostic j = cmp(b, mlo); 232056731Sbostic delta = diff(S, mhi); 232156731Sbostic j1 = delta->sign ? 1 : cmp(b, delta); 232256731Sbostic Bfree(delta); 232356731Sbostic #ifndef ROUND_BIASED 232456731Sbostic if (j1 == 0 && !mode && !(word1(d) & 1)) { 232556731Sbostic if (dig == '9') 232656731Sbostic goto round_9_up; 232756731Sbostic if (j > 0) 232856731Sbostic dig++; 232956731Sbostic *s++ = dig; 233056731Sbostic goto ret; 233156731Sbostic } 233256731Sbostic #endif 233356731Sbostic if (j < 0 || j == 0 && !mode 233456731Sbostic #ifndef ROUND_BIASED 233556731Sbostic && !(word1(d) & 1) 233656731Sbostic #endif 233756731Sbostic ) { 233856731Sbostic if (j1 > 0) { 233956731Sbostic b = lshift(b, 1); 234056731Sbostic j1 = cmp(b, S); 234156731Sbostic if ((j1 > 0 || j1 == 0 && dig & 1) 234256731Sbostic && dig++ == '9') 234356731Sbostic goto round_9_up; 234456731Sbostic } 234556731Sbostic *s++ = dig; 234656731Sbostic goto ret; 234756731Sbostic } 234856731Sbostic if (j1 > 0) { 234956731Sbostic if (dig == '9') { /* possible if i == 1 */ 235056731Sbostic round_9_up: 235156731Sbostic *s++ = '9'; 235256731Sbostic goto roundoff; 235356731Sbostic } 235456731Sbostic *s++ = dig + 1; 235556731Sbostic goto ret; 235656731Sbostic } 235756731Sbostic *s++ = dig; 235856731Sbostic if (i == ilim) 235956731Sbostic break; 236056731Sbostic b = multadd(b, 10, 0); 236156731Sbostic if (mlo == mhi) 236256731Sbostic mlo = mhi = multadd(mhi, 10, 0); 236356731Sbostic else { 236456731Sbostic mlo = multadd(mlo, 10, 0); 236556731Sbostic mhi = multadd(mhi, 10, 0); 236656731Sbostic } 236756731Sbostic } 236856731Sbostic } else 236956731Sbostic for (i = 1;; i++) { 237056731Sbostic *s++ = dig = quorem(b,S) + '0'; 237156731Sbostic if (i >= ilim) 237256731Sbostic break; 237356731Sbostic b = multadd(b, 10, 0); 237456731Sbostic } 237556731Sbostic 237656731Sbostic /* Round off last digit */ 237756731Sbostic 237856731Sbostic b = lshift(b, 1); 237956731Sbostic j = cmp(b, S); 238056731Sbostic if (j > 0 || j == 0 && dig & 1) { 238156731Sbostic roundoff: 238256731Sbostic while (*--s == '9') 238356731Sbostic if (s == s0) { 238456731Sbostic k++; 238556731Sbostic *s++ = '1'; 238656731Sbostic goto ret; 238756731Sbostic } 238856731Sbostic ++*s++; 238956731Sbostic } else { 239056731Sbostic while (*--s == '0'); 239156731Sbostic s++; 239256731Sbostic } 239356731Sbostic ret: 239456731Sbostic Bfree(S); 239556731Sbostic if (mhi) { 239656731Sbostic if (mlo && mlo != mhi) 239756731Sbostic Bfree(mlo); 239856731Sbostic Bfree(mhi); 239956731Sbostic } 240056731Sbostic ret1: 240156731Sbostic Bfree(b); 240256731Sbostic if (s == s0) { /* don't return empty string */ 240356731Sbostic *s++ = '0'; 240456731Sbostic k = 0; 240556731Sbostic } 240656731Sbostic *s = 0; 240756731Sbostic *decpt = k + 1; 240856731Sbostic if (rve) 240956731Sbostic *rve = s; 241056731Sbostic return s0; 241156731Sbostic } 241256731Sbostic #ifdef __cplusplus 241356731Sbostic } 241456731Sbostic #endif 2415