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