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