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