OLD | NEW |
(Empty) | |
| 1 /**************************************************************** |
| 2 * |
| 3 * The author of this software is David M. Gay. |
| 4 * |
| 5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. |
| 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 LUCENT 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 David M. Gay (dmg at acm dot org, |
| 21 * with " at " changed at "@" and " dot " changed to "."). */ |
| 22 |
| 23 /* On a machine with IEEE extended-precision registers, it is |
| 24 * necessary to specify double-precision (53-bit) rounding precision |
| 25 * before invoking strtod or dtoa. If the machine uses (the equivalent |
| 26 * of) Intel 80x87 arithmetic, the call |
| 27 * _control87(PC_53, MCW_PC); |
| 28 * does this with many compilers. Whether this or another call is |
| 29 * appropriate depends on the compiler; for this to work, it may be |
| 30 * necessary to #include "float.h" or another system-dependent header |
| 31 * file. |
| 32 */ |
| 33 |
| 34 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. |
| 35 * |
| 36 * This strtod returns a nearest machine number to the input decimal |
| 37 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are |
| 38 * broken by the IEEE round-even rule. Otherwise ties are broken by |
| 39 * biased rounding (add half and chop). |
| 40 * |
| 41 * Inspired loosely by William D. Clinger's paper "How to Read Floating |
| 42 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. |
| 43 * |
| 44 * Modifications: |
| 45 * |
| 46 * 1. We only require IEEE, IBM, or VAX double-precision |
| 47 * arithmetic (not IEEE double-extended). |
| 48 * 2. We get by with floating-point arithmetic in a case that |
| 49 * Clinger missed -- when we're computing d * 10^n |
| 50 * for a small integer d and the integer n is not too |
| 51 * much larger than 22 (the maximum integer k for which |
| 52 * we can represent 10^k exactly), we may be able to |
| 53 * compute (d*10^k) * 10^(e-k) with just one roundoff. |
| 54 * 3. Rather than a bit-at-a-time adjustment of the binary |
| 55 * result in the hard case, we use floating-point |
| 56 * arithmetic to determine the adjustment to within |
| 57 * one bit; only in really hard cases do we need to |
| 58 * compute a second residual. |
| 59 * 4. Because of 3., we don't need a large table of powers of 10 |
| 60 * for ten-to-e (just some small tables, e.g. of 10^k |
| 61 * for 0 <= k <= 22). |
| 62 */ |
| 63 |
| 64 /* |
| 65 * #define IEEE_8087 for IEEE-arithmetic machines where the least |
| 66 * significant byte has the lowest address. |
| 67 * #define IEEE_MC68k for IEEE-arithmetic machines where the most |
| 68 * significant byte has the lowest address. |
| 69 * #define Long int on machines with 32-bit ints and 64-bit longs. |
| 70 * #define IBM for IBM mainframe-style floating-point arithmetic. |
| 71 * #define VAX for VAX-style floating-point arithmetic (D_floating). |
| 72 * #define No_leftright to omit left-right logic in fast floating-point |
| 73 * computation of dtoa. |
| 74 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 |
| 75 * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS |
| 76 * is also #defined, fegetround() will be queried for the rounding mode. |
| 77 * Note that both FLT_ROUNDS and fegetround() are specified by the C99 |
| 78 * standard (and are specified to be consistent, with fesetround() |
| 79 * affecting the value of FLT_ROUNDS), but that some (Linux) systems |
| 80 * do not work correctly in this regard, so using fegetround() is more |
| 81 * portable than using FLT_FOUNDS directly. |
| 82 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 |
| 83 * and Honor_FLT_ROUNDS is not #defined. |
| 84 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines |
| 85 * that use extended-precision instructions to compute rounded |
| 86 * products and quotients) with IBM. |
| 87 * #define ROUND_BIASED for IEEE-format with biased rounding. |
| 88 * #define Inaccurate_Divide for IEEE-format with correctly rounded |
| 89 * products but inaccurate quotients, e.g., for Intel i860. |
| 90 * #define NO_LONG_LONG on machines that do not have a "long long" |
| 91 * integer type (of >= 64 bits). On such machines, you can |
| 92 * #define Just_16 to store 16 bits per 32-bit Long when doing |
| 93 * high-precision integer arithmetic. Whether this speeds things |
| 94 * up or slows things down depends on the machine and the number |
| 95 * being converted. If long long is available and the name is |
| 96 * something other than "long long", #define Llong to be the name, |
| 97 * and if "unsigned Llong" does not work as an unsigned version of |
| 98 * Llong, #define #ULLong to be the corresponding unsigned type. |
| 99 * #define KR_headers for old-style C function headers. |
| 100 * #define Bad_float_h if your system lacks a float.h or if it does not |
| 101 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, |
| 102 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. |
| 103 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) |
| 104 * if memory is available and otherwise does something you deem |
| 105 * appropriate. If MALLOC is undefined, malloc will be invoked |
| 106 * directly -- and assumed always to succeed. |
| 107 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making |
| 108 * memory allocations from a private pool of memory when possible. |
| 109 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, |
| 110 * unless #defined to be a different length. This default length |
| 111 * suffices to get rid of MALLOC calls except for unusual cases, |
| 112 * such as decimal-to-binary conversion of a very long string of |
| 113 * digits. The longest string dtoa can return is about 751 bytes |
| 114 * long. For conversions by strtod of strings of 800 digits and |
| 115 * all dtoa conversions in single-threaded executions with 8-byte |
| 116 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte |
| 117 * pointers, PRIVATE_MEM >= 7112 appears adequate. |
| 118 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK |
| 119 * #defined automatically on IEEE systems. On such systems, |
| 120 * when INFNAN_CHECK is #defined, strtod checks |
| 121 * for Infinity and NaN (case insensitively). On some systems |
| 122 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0 |
| 123 * appropriately -- to the most significant word of a quiet NaN. |
| 124 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.) |
| 125 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, |
| 126 * strtod also accepts (case insensitively) strings of the form |
| 127 * NaN(x), where x is a string of hexadecimal digits and spaces; |
| 128 * if there is only one string of hexadecimal digits, it is taken |
| 129 * for the 52 fraction bits of the resulting NaN; if there are two |
| 130 * or more strings of hex digits, the first is for the high 20 bits, |
| 131 * the second and subsequent for the low 32 bits, with intervening |
| 132 * white space ignored; but if this results in none of the 52 |
| 133 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0 |
| 134 * and NAN_WORD1 are used instead. |
| 135 * #define MULTIPLE_THREADS if the system offers preemptively scheduled |
| 136 * multiple threads. In this case, you must provide (or suitably |
| 137 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed |
| 138 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed |
| 139 * in pow5mult, ensures lazy evaluation of only one copy of high |
| 140 * powers of 5; omitting this lock would introduce a small |
| 141 * probability of wasting memory, but would otherwise be harmless.) |
| 142 * You must also invoke freedtoa(s) to free the value s returned by |
| 143 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. |
| 144 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that |
| 145 * avoids underflows on inputs whose result does not underflow. |
| 146 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format |
| 147 * floating-point numbers and flushes underflows to zero rather |
| 148 * than implementing gradual underflow, then you must also #define |
| 149 * Sudden_Underflow. |
| 150 * #define YES_ALIAS to permit aliasing certain double values with |
| 151 * arrays of ULongs. This leads to slightly better code with |
| 152 * some compilers and was always used prior to 19990916, but it |
| 153 * is not strictly legal and can cause trouble with aggressively |
| 154 * optimizing compilers (e.g., gcc 2.95.1 under -O2). |
| 155 * #define USE_LOCALE to use the current locale's decimal_point value. |
| 156 * #define SET_INEXACT if IEEE arithmetic is being used and extra |
| 157 * computation should be done to set the inexact flag when the |
| 158 * result is inexact and avoid setting inexact when the result |
| 159 * is exact. In this case, dtoa.c must be compiled in |
| 160 * an environment, perhaps provided by #include "dtoa.c" in a |
| 161 * suitable wrapper, that defines two functions, |
| 162 * int get_inexact(void); |
| 163 * void clear_inexact(void); |
| 164 * such that get_inexact() returns a nonzero value if the |
| 165 * inexact bit is already set, and clear_inexact() sets the |
| 166 * inexact bit to 0. When SET_INEXACT is #defined, strtod |
| 167 * also does extra computations to set the underflow and overflow |
| 168 * flags when appropriate (i.e., when the result is tiny and |
| 169 * inexact or when it is a numeric value rounded to +-infinity). |
| 170 * #define NO_ERRNO if strtod should not assign errno = ERANGE when |
| 171 * the result overflows to +-Infinity or underflows to 0. |
| 172 */ |
| 173 |
| 174 #define IEEE_8087 |
| 175 |
| 176 #ifndef Long |
| 177 #define Long long |
| 178 #endif |
| 179 #ifndef ULong |
| 180 typedef unsigned Long ULong; |
| 181 #endif |
| 182 |
| 183 #ifdef DEBUG |
| 184 #include "stdio.h" |
| 185 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} |
| 186 #endif |
| 187 |
| 188 #include "stdlib.h" |
| 189 #include "string.h" |
| 190 |
| 191 #ifdef USE_LOCALE |
| 192 #include "locale.h" |
| 193 #endif |
| 194 |
| 195 #ifdef MALLOC |
| 196 #ifdef KR_headers |
| 197 extern char *MALLOC(); |
| 198 #else |
| 199 extern void *MALLOC(size_t); |
| 200 #endif |
| 201 #else |
| 202 #define MALLOC malloc |
| 203 #endif |
| 204 |
| 205 #ifndef Omit_Private_Memory |
| 206 #ifndef PRIVATE_MEM |
| 207 #define PRIVATE_MEM 2304 |
| 208 #endif |
| 209 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) |
| 210 static double private_mem[PRIVATE_mem], *pmem_next = private_mem; |
| 211 #endif |
| 212 |
| 213 #undef IEEE_Arith |
| 214 #undef Avoid_Underflow |
| 215 #ifdef IEEE_MC68k |
| 216 #define IEEE_Arith |
| 217 #endif |
| 218 #ifdef IEEE_8087 |
| 219 #define IEEE_Arith |
| 220 #endif |
| 221 |
| 222 #ifdef IEEE_Arith |
| 223 #ifndef NO_INFNAN_CHECK |
| 224 #undef INFNAN_CHECK |
| 225 #define INFNAN_CHECK |
| 226 #endif |
| 227 #else |
| 228 #undef INFNAN_CHECK |
| 229 #endif |
| 230 |
| 231 #include "errno.h" |
| 232 |
| 233 #ifdef Bad_float_h |
| 234 |
| 235 #ifdef IEEE_Arith |
| 236 #define DBL_DIG 15 |
| 237 #define DBL_MAX_10_EXP 308 |
| 238 #define DBL_MAX_EXP 1024 |
| 239 #define FLT_RADIX 2 |
| 240 #endif /*IEEE_Arith*/ |
| 241 |
| 242 #ifdef IBM |
| 243 #define DBL_DIG 16 |
| 244 #define DBL_MAX_10_EXP 75 |
| 245 #define DBL_MAX_EXP 63 |
| 246 #define FLT_RADIX 16 |
| 247 #define DBL_MAX 7.2370055773322621e+75 |
| 248 #endif |
| 249 |
| 250 #ifdef VAX |
| 251 #define DBL_DIG 16 |
| 252 #define DBL_MAX_10_EXP 38 |
| 253 #define DBL_MAX_EXP 127 |
| 254 #define FLT_RADIX 2 |
| 255 #define DBL_MAX 1.7014118346046923e+38 |
| 256 #endif |
| 257 |
| 258 #ifndef LONG_MAX |
| 259 #define LONG_MAX 2147483647 |
| 260 #endif |
| 261 |
| 262 #else /* ifndef Bad_float_h */ |
| 263 #include "float.h" |
| 264 #endif /* Bad_float_h */ |
| 265 |
| 266 #ifndef __MATH_H__ |
| 267 #include "math.h" |
| 268 #endif |
| 269 |
| 270 namespace dmg_fp { |
| 271 |
| 272 #ifndef CONST |
| 273 #ifdef KR_headers |
| 274 #define CONST /* blank */ |
| 275 #else |
| 276 #define CONST const |
| 277 #endif |
| 278 #endif |
| 279 |
| 280 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 |
| 281 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. |
| 282 #endif |
| 283 |
| 284 typedef union { double d; ULong L[2]; } U; |
| 285 |
| 286 #ifdef YES_ALIAS |
| 287 #define dval(x) x |
| 288 #ifdef IEEE_8087 |
| 289 #define word0(x) ((ULong *)&x)[1] |
| 290 #define word1(x) ((ULong *)&x)[0] |
| 291 #else |
| 292 #define word0(x) ((ULong *)&x)[0] |
| 293 #define word1(x) ((ULong *)&x)[1] |
| 294 #endif |
| 295 #else |
| 296 #ifdef IEEE_8087 |
| 297 #define word0(x) ((U*)&x)->L[1] |
| 298 #define word1(x) ((U*)&x)->L[0] |
| 299 #else |
| 300 #define word0(x) ((U*)&x)->L[0] |
| 301 #define word1(x) ((U*)&x)->L[1] |
| 302 #endif |
| 303 #define dval(x) ((U*)&x)->d |
| 304 #endif |
| 305 |
| 306 /* The following definition of Storeinc is appropriate for MIPS processors. |
| 307 * An alternative that might be better on some machines is |
| 308 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) |
| 309 */ |
| 310 #if defined(IEEE_8087) + defined(VAX) |
| 311 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ |
| 312 ((unsigned short *)a)[0] = (unsigned short)c, a++) |
| 313 #else |
| 314 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ |
| 315 ((unsigned short *)a)[1] = (unsigned short)c, a++) |
| 316 #endif |
| 317 |
| 318 /* #define P DBL_MANT_DIG */ |
| 319 /* Ten_pmax = floor(P*log(2)/log(5)) */ |
| 320 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ |
| 321 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ |
| 322 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ |
| 323 |
| 324 #ifdef IEEE_Arith |
| 325 #define Exp_shift 20 |
| 326 #define Exp_shift1 20 |
| 327 #define Exp_msk1 0x100000 |
| 328 #define Exp_msk11 0x100000 |
| 329 #define Exp_mask 0x7ff00000 |
| 330 #define P 53 |
| 331 #define Bias 1023 |
| 332 #define Emin (-1022) |
| 333 #define Exp_1 0x3ff00000 |
| 334 #define Exp_11 0x3ff00000 |
| 335 #define Ebits 11 |
| 336 #define Frac_mask 0xfffff |
| 337 #define Frac_mask1 0xfffff |
| 338 #define Ten_pmax 22 |
| 339 #define Bletch 0x10 |
| 340 #define Bndry_mask 0xfffff |
| 341 #define Bndry_mask1 0xfffff |
| 342 #define LSB 1 |
| 343 #define Sign_bit 0x80000000 |
| 344 #define Log2P 1 |
| 345 #define Tiny0 0 |
| 346 #define Tiny1 1 |
| 347 #define Quick_max 14 |
| 348 #define Int_max 14 |
| 349 #ifndef NO_IEEE_Scale |
| 350 #define Avoid_Underflow |
| 351 #ifdef Flush_Denorm /* debugging option */ |
| 352 #undef Sudden_Underflow |
| 353 #endif |
| 354 #endif |
| 355 |
| 356 #ifndef Flt_Rounds |
| 357 #ifdef FLT_ROUNDS |
| 358 #define Flt_Rounds FLT_ROUNDS |
| 359 #else |
| 360 #define Flt_Rounds 1 |
| 361 #endif |
| 362 #endif /*Flt_Rounds*/ |
| 363 |
| 364 #ifdef Honor_FLT_ROUNDS |
| 365 #undef Check_FLT_ROUNDS |
| 366 #define Check_FLT_ROUNDS |
| 367 #else |
| 368 #define Rounding Flt_Rounds |
| 369 #endif |
| 370 |
| 371 #else /* ifndef IEEE_Arith */ |
| 372 #undef Check_FLT_ROUNDS |
| 373 #undef Honor_FLT_ROUNDS |
| 374 #undef SET_INEXACT |
| 375 #undef Sudden_Underflow |
| 376 #define Sudden_Underflow |
| 377 #ifdef IBM |
| 378 #undef Flt_Rounds |
| 379 #define Flt_Rounds 0 |
| 380 #define Exp_shift 24 |
| 381 #define Exp_shift1 24 |
| 382 #define Exp_msk1 0x1000000 |
| 383 #define Exp_msk11 0x1000000 |
| 384 #define Exp_mask 0x7f000000 |
| 385 #define P 14 |
| 386 #define Bias 65 |
| 387 #define Exp_1 0x41000000 |
| 388 #define Exp_11 0x41000000 |
| 389 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ |
| 390 #define Frac_mask 0xffffff |
| 391 #define Frac_mask1 0xffffff |
| 392 #define Bletch 4 |
| 393 #define Ten_pmax 22 |
| 394 #define Bndry_mask 0xefffff |
| 395 #define Bndry_mask1 0xffffff |
| 396 #define LSB 1 |
| 397 #define Sign_bit 0x80000000 |
| 398 #define Log2P 4 |
| 399 #define Tiny0 0x100000 |
| 400 #define Tiny1 0 |
| 401 #define Quick_max 14 |
| 402 #define Int_max 15 |
| 403 #else /* VAX */ |
| 404 #undef Flt_Rounds |
| 405 #define Flt_Rounds 1 |
| 406 #define Exp_shift 23 |
| 407 #define Exp_shift1 7 |
| 408 #define Exp_msk1 0x80 |
| 409 #define Exp_msk11 0x800000 |
| 410 #define Exp_mask 0x7f80 |
| 411 #define P 56 |
| 412 #define Bias 129 |
| 413 #define Exp_1 0x40800000 |
| 414 #define Exp_11 0x4080 |
| 415 #define Ebits 8 |
| 416 #define Frac_mask 0x7fffff |
| 417 #define Frac_mask1 0xffff007f |
| 418 #define Ten_pmax 24 |
| 419 #define Bletch 2 |
| 420 #define Bndry_mask 0xffff007f |
| 421 #define Bndry_mask1 0xffff007f |
| 422 #define LSB 0x10000 |
| 423 #define Sign_bit 0x8000 |
| 424 #define Log2P 1 |
| 425 #define Tiny0 0x80 |
| 426 #define Tiny1 0 |
| 427 #define Quick_max 15 |
| 428 #define Int_max 15 |
| 429 #endif /* IBM, VAX */ |
| 430 #endif /* IEEE_Arith */ |
| 431 |
| 432 #ifndef IEEE_Arith |
| 433 #define ROUND_BIASED |
| 434 #endif |
| 435 |
| 436 #ifdef RND_PRODQUOT |
| 437 #define rounded_product(a,b) a = rnd_prod(a, b) |
| 438 #define rounded_quotient(a,b) a = rnd_quot(a, b) |
| 439 #ifdef KR_headers |
| 440 extern double rnd_prod(), rnd_quot(); |
| 441 #else |
| 442 extern double rnd_prod(double, double), rnd_quot(double, double); |
| 443 #endif |
| 444 #else |
| 445 #define rounded_product(a,b) a *= b |
| 446 #define rounded_quotient(a,b) a /= b |
| 447 #endif |
| 448 |
| 449 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) |
| 450 #define Big1 0xffffffff |
| 451 |
| 452 #ifndef Pack_32 |
| 453 #define Pack_32 |
| 454 #endif |
| 455 |
| 456 #ifdef KR_headers |
| 457 #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff) |
| 458 #else |
| 459 #define FFFFFFFF 0xffffffffUL |
| 460 #endif |
| 461 |
| 462 #ifdef NO_LONG_LONG |
| 463 #undef ULLong |
| 464 #ifdef Just_16 |
| 465 #undef Pack_32 |
| 466 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. |
| 467 * This makes some inner loops simpler and sometimes saves work |
| 468 * during multiplications, but it often seems to make things slightly |
| 469 * slower. Hence the default is now to store 32 bits per Long. |
| 470 */ |
| 471 #endif |
| 472 #else /* long long available */ |
| 473 #ifndef Llong |
| 474 #define Llong long long |
| 475 #endif |
| 476 #ifndef ULLong |
| 477 #define ULLong unsigned Llong |
| 478 #endif |
| 479 #endif /* NO_LONG_LONG */ |
| 480 |
| 481 #ifndef MULTIPLE_THREADS |
| 482 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/ |
| 483 #define FREE_DTOA_LOCK(n) /*nothing*/ |
| 484 #endif |
| 485 |
| 486 #define Kmax 15 |
| 487 |
| 488 double strtod(const char *s00, char **se); |
| 489 char *dtoa(double d, int mode, int ndigits, |
| 490 int *decpt, int *sign, char **rve); |
| 491 |
| 492 struct |
| 493 Bigint { |
| 494 struct Bigint *next; |
| 495 int k, maxwds, sign, wds; |
| 496 ULong x[1]; |
| 497 }; |
| 498 |
| 499 typedef struct Bigint Bigint; |
| 500 |
| 501 static Bigint *freelist[Kmax+1]; |
| 502 |
| 503 static Bigint * |
| 504 Balloc |
| 505 #ifdef KR_headers |
| 506 (k) int k; |
| 507 #else |
| 508 (int k) |
| 509 #endif |
| 510 { |
| 511 int x; |
| 512 Bigint *rv; |
| 513 #ifndef Omit_Private_Memory |
| 514 unsigned int len; |
| 515 #endif |
| 516 |
| 517 ACQUIRE_DTOA_LOCK(0); |
| 518 if (rv = freelist[k]) { |
| 519 freelist[k] = rv->next; |
| 520 } |
| 521 else { |
| 522 x = 1 << k; |
| 523 #ifdef Omit_Private_Memory |
| 524 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); |
| 525 #else |
| 526 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1
) |
| 527 /sizeof(double); |
| 528 if (pmem_next - private_mem + len <= PRIVATE_mem) { |
| 529 rv = (Bigint*)pmem_next; |
| 530 pmem_next += len; |
| 531 } |
| 532 else |
| 533 rv = (Bigint*)MALLOC(len*sizeof(double)); |
| 534 #endif |
| 535 rv->k = k; |
| 536 rv->maxwds = x; |
| 537 } |
| 538 FREE_DTOA_LOCK(0); |
| 539 rv->sign = rv->wds = 0; |
| 540 return rv; |
| 541 } |
| 542 |
| 543 static void |
| 544 Bfree |
| 545 #ifdef KR_headers |
| 546 (v) Bigint *v; |
| 547 #else |
| 548 (Bigint *v) |
| 549 #endif |
| 550 { |
| 551 if (v) { |
| 552 ACQUIRE_DTOA_LOCK(0); |
| 553 v->next = freelist[v->k]; |
| 554 freelist[v->k] = v; |
| 555 FREE_DTOA_LOCK(0); |
| 556 } |
| 557 } |
| 558 |
| 559 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ |
| 560 y->wds*sizeof(Long) + 2*sizeof(int)) |
| 561 |
| 562 static Bigint * |
| 563 multadd |
| 564 #ifdef KR_headers |
| 565 (b, m, a) Bigint *b; int m, a; |
| 566 #else |
| 567 (Bigint *b, int m, int a) /* multiply by m and add a */ |
| 568 #endif |
| 569 { |
| 570 int i, wds; |
| 571 #ifdef ULLong |
| 572 ULong *x; |
| 573 ULLong carry, y; |
| 574 #else |
| 575 ULong carry, *x, y; |
| 576 #ifdef Pack_32 |
| 577 ULong xi, z; |
| 578 #endif |
| 579 #endif |
| 580 Bigint *b1; |
| 581 |
| 582 wds = b->wds; |
| 583 x = b->x; |
| 584 i = 0; |
| 585 carry = a; |
| 586 do { |
| 587 #ifdef ULLong |
| 588 y = *x * (ULLong)m + carry; |
| 589 carry = y >> 32; |
| 590 *x++ = y & FFFFFFFF; |
| 591 #else |
| 592 #ifdef Pack_32 |
| 593 xi = *x; |
| 594 y = (xi & 0xffff) * m + carry; |
| 595 z = (xi >> 16) * m + (y >> 16); |
| 596 carry = z >> 16; |
| 597 *x++ = (z << 16) + (y & 0xffff); |
| 598 #else |
| 599 y = *x * m + carry; |
| 600 carry = y >> 16; |
| 601 *x++ = y & 0xffff; |
| 602 #endif |
| 603 #endif |
| 604 } |
| 605 while(++i < wds); |
| 606 if (carry) { |
| 607 if (wds >= b->maxwds) { |
| 608 b1 = Balloc(b->k+1); |
| 609 Bcopy(b1, b); |
| 610 Bfree(b); |
| 611 b = b1; |
| 612 } |
| 613 b->x[wds++] = carry; |
| 614 b->wds = wds; |
| 615 } |
| 616 return b; |
| 617 } |
| 618 |
| 619 static Bigint * |
| 620 s2b |
| 621 #ifdef KR_headers |
| 622 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; |
| 623 #else |
| 624 (CONST char *s, int nd0, int nd, ULong y9) |
| 625 #endif |
| 626 { |
| 627 Bigint *b; |
| 628 int i, k; |
| 629 Long x, y; |
| 630 |
| 631 x = (nd + 8) / 9; |
| 632 for(k = 0, y = 1; x > y; y <<= 1, k++) ; |
| 633 #ifdef Pack_32 |
| 634 b = Balloc(k); |
| 635 b->x[0] = y9; |
| 636 b->wds = 1; |
| 637 #else |
| 638 b = Balloc(k+1); |
| 639 b->x[0] = y9 & 0xffff; |
| 640 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; |
| 641 #endif |
| 642 |
| 643 i = 9; |
| 644 if (9 < nd0) { |
| 645 s += 9; |
| 646 do b = multadd(b, 10, *s++ - '0'); |
| 647 while(++i < nd0); |
| 648 s++; |
| 649 } |
| 650 else |
| 651 s += 10; |
| 652 for(; i < nd; i++) |
| 653 b = multadd(b, 10, *s++ - '0'); |
| 654 return b; |
| 655 } |
| 656 |
| 657 static int |
| 658 hi0bits |
| 659 #ifdef KR_headers |
| 660 (x) register ULong x; |
| 661 #else |
| 662 (register ULong x) |
| 663 #endif |
| 664 { |
| 665 register int k = 0; |
| 666 |
| 667 if (!(x & 0xffff0000)) { |
| 668 k = 16; |
| 669 x <<= 16; |
| 670 } |
| 671 if (!(x & 0xff000000)) { |
| 672 k += 8; |
| 673 x <<= 8; |
| 674 } |
| 675 if (!(x & 0xf0000000)) { |
| 676 k += 4; |
| 677 x <<= 4; |
| 678 } |
| 679 if (!(x & 0xc0000000)) { |
| 680 k += 2; |
| 681 x <<= 2; |
| 682 } |
| 683 if (!(x & 0x80000000)) { |
| 684 k++; |
| 685 if (!(x & 0x40000000)) |
| 686 return 32; |
| 687 } |
| 688 return k; |
| 689 } |
| 690 |
| 691 static int |
| 692 lo0bits |
| 693 #ifdef KR_headers |
| 694 (y) ULong *y; |
| 695 #else |
| 696 (ULong *y) |
| 697 #endif |
| 698 { |
| 699 register int k; |
| 700 register ULong x = *y; |
| 701 |
| 702 if (x & 7) { |
| 703 if (x & 1) |
| 704 return 0; |
| 705 if (x & 2) { |
| 706 *y = x >> 1; |
| 707 return 1; |
| 708 } |
| 709 *y = x >> 2; |
| 710 return 2; |
| 711 } |
| 712 k = 0; |
| 713 if (!(x & 0xffff)) { |
| 714 k = 16; |
| 715 x >>= 16; |
| 716 } |
| 717 if (!(x & 0xff)) { |
| 718 k += 8; |
| 719 x >>= 8; |
| 720 } |
| 721 if (!(x & 0xf)) { |
| 722 k += 4; |
| 723 x >>= 4; |
| 724 } |
| 725 if (!(x & 0x3)) { |
| 726 k += 2; |
| 727 x >>= 2; |
| 728 } |
| 729 if (!(x & 1)) { |
| 730 k++; |
| 731 x >>= 1; |
| 732 if (!x) |
| 733 return 32; |
| 734 } |
| 735 *y = x; |
| 736 return k; |
| 737 } |
| 738 |
| 739 static Bigint * |
| 740 i2b |
| 741 #ifdef KR_headers |
| 742 (i) int i; |
| 743 #else |
| 744 (int i) |
| 745 #endif |
| 746 { |
| 747 Bigint *b; |
| 748 |
| 749 b = Balloc(1); |
| 750 b->x[0] = i; |
| 751 b->wds = 1; |
| 752 return b; |
| 753 } |
| 754 |
| 755 static Bigint * |
| 756 mult |
| 757 #ifdef KR_headers |
| 758 (a, b) Bigint *a, *b; |
| 759 #else |
| 760 (Bigint *a, Bigint *b) |
| 761 #endif |
| 762 { |
| 763 Bigint *c; |
| 764 int k, wa, wb, wc; |
| 765 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; |
| 766 ULong y; |
| 767 #ifdef ULLong |
| 768 ULLong carry, z; |
| 769 #else |
| 770 ULong carry, z; |
| 771 #ifdef Pack_32 |
| 772 ULong z2; |
| 773 #endif |
| 774 #endif |
| 775 |
| 776 if (a->wds < b->wds) { |
| 777 c = a; |
| 778 a = b; |
| 779 b = c; |
| 780 } |
| 781 k = a->k; |
| 782 wa = a->wds; |
| 783 wb = b->wds; |
| 784 wc = wa + wb; |
| 785 if (wc > a->maxwds) |
| 786 k++; |
| 787 c = Balloc(k); |
| 788 for(x = c->x, xa = x + wc; x < xa; x++) |
| 789 *x = 0; |
| 790 xa = a->x; |
| 791 xae = xa + wa; |
| 792 xb = b->x; |
| 793 xbe = xb + wb; |
| 794 xc0 = c->x; |
| 795 #ifdef ULLong |
| 796 for(; xb < xbe; xc0++) { |
| 797 if (y = *xb++) { |
| 798 x = xa; |
| 799 xc = xc0; |
| 800 carry = 0; |
| 801 do { |
| 802 z = *x++ * (ULLong)y + *xc + carry; |
| 803 carry = z >> 32; |
| 804 *xc++ = z & FFFFFFFF; |
| 805 } |
| 806 while(x < xae); |
| 807 *xc = carry; |
| 808 } |
| 809 } |
| 810 #else |
| 811 #ifdef Pack_32 |
| 812 for(; xb < xbe; xb++, xc0++) { |
| 813 if (y = *xb & 0xffff) { |
| 814 x = xa; |
| 815 xc = xc0; |
| 816 carry = 0; |
| 817 do { |
| 818 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; |
| 819 carry = z >> 16; |
| 820 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; |
| 821 carry = z2 >> 16; |
| 822 Storeinc(xc, z2, z); |
| 823 } |
| 824 while(x < xae); |
| 825 *xc = carry; |
| 826 } |
| 827 if (y = *xb >> 16) { |
| 828 x = xa; |
| 829 xc = xc0; |
| 830 carry = 0; |
| 831 z2 = *xc; |
| 832 do { |
| 833 z = (*x & 0xffff) * y + (*xc >> 16) + carry; |
| 834 carry = z >> 16; |
| 835 Storeinc(xc, z, z2); |
| 836 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; |
| 837 carry = z2 >> 16; |
| 838 } |
| 839 while(x < xae); |
| 840 *xc = z2; |
| 841 } |
| 842 } |
| 843 #else |
| 844 for(; xb < xbe; xc0++) { |
| 845 if (y = *xb++) { |
| 846 x = xa; |
| 847 xc = xc0; |
| 848 carry = 0; |
| 849 do { |
| 850 z = *x++ * y + *xc + carry; |
| 851 carry = z >> 16; |
| 852 *xc++ = z & 0xffff; |
| 853 } |
| 854 while(x < xae); |
| 855 *xc = carry; |
| 856 } |
| 857 } |
| 858 #endif |
| 859 #endif |
| 860 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; |
| 861 c->wds = wc; |
| 862 return c; |
| 863 } |
| 864 |
| 865 static Bigint *p5s; |
| 866 |
| 867 static Bigint * |
| 868 pow5mult |
| 869 #ifdef KR_headers |
| 870 (b, k) Bigint *b; int k; |
| 871 #else |
| 872 (Bigint *b, int k) |
| 873 #endif |
| 874 { |
| 875 Bigint *b1, *p5, *p51; |
| 876 int i; |
| 877 static int p05[3] = { 5, 25, 125 }; |
| 878 |
| 879 if (i = k & 3) |
| 880 b = multadd(b, p05[i-1], 0); |
| 881 |
| 882 if (!(k >>= 2)) |
| 883 return b; |
| 884 if (!(p5 = p5s)) { |
| 885 /* first time */ |
| 886 #ifdef MULTIPLE_THREADS |
| 887 ACQUIRE_DTOA_LOCK(1); |
| 888 if (!(p5 = p5s)) { |
| 889 p5 = p5s = i2b(625); |
| 890 p5->next = 0; |
| 891 } |
| 892 FREE_DTOA_LOCK(1); |
| 893 #else |
| 894 p5 = p5s = i2b(625); |
| 895 p5->next = 0; |
| 896 #endif |
| 897 } |
| 898 for(;;) { |
| 899 if (k & 1) { |
| 900 b1 = mult(b, p5); |
| 901 Bfree(b); |
| 902 b = b1; |
| 903 } |
| 904 if (!(k >>= 1)) |
| 905 break; |
| 906 if (!(p51 = p5->next)) { |
| 907 #ifdef MULTIPLE_THREADS |
| 908 ACQUIRE_DTOA_LOCK(1); |
| 909 if (!(p51 = p5->next)) { |
| 910 p51 = p5->next = mult(p5,p5); |
| 911 p51->next = 0; |
| 912 } |
| 913 FREE_DTOA_LOCK(1); |
| 914 #else |
| 915 p51 = p5->next = mult(p5,p5); |
| 916 p51->next = 0; |
| 917 #endif |
| 918 } |
| 919 p5 = p51; |
| 920 } |
| 921 return b; |
| 922 } |
| 923 |
| 924 static Bigint * |
| 925 lshift |
| 926 #ifdef KR_headers |
| 927 (b, k) Bigint *b; int k; |
| 928 #else |
| 929 (Bigint *b, int k) |
| 930 #endif |
| 931 { |
| 932 int i, k1, n, n1; |
| 933 Bigint *b1; |
| 934 ULong *x, *x1, *xe, z; |
| 935 |
| 936 #ifdef Pack_32 |
| 937 n = k >> 5; |
| 938 #else |
| 939 n = k >> 4; |
| 940 #endif |
| 941 k1 = b->k; |
| 942 n1 = n + b->wds + 1; |
| 943 for(i = b->maxwds; n1 > i; i <<= 1) |
| 944 k1++; |
| 945 b1 = Balloc(k1); |
| 946 x1 = b1->x; |
| 947 for(i = 0; i < n; i++) |
| 948 *x1++ = 0; |
| 949 x = b->x; |
| 950 xe = x + b->wds; |
| 951 #ifdef Pack_32 |
| 952 if (k &= 0x1f) { |
| 953 k1 = 32 - k; |
| 954 z = 0; |
| 955 do { |
| 956 *x1++ = *x << k | z; |
| 957 z = *x++ >> k1; |
| 958 } |
| 959 while(x < xe); |
| 960 if (*x1 = z) |
| 961 ++n1; |
| 962 } |
| 963 #else |
| 964 if (k &= 0xf) { |
| 965 k1 = 16 - k; |
| 966 z = 0; |
| 967 do { |
| 968 *x1++ = *x << k & 0xffff | z; |
| 969 z = *x++ >> k1; |
| 970 } |
| 971 while(x < xe); |
| 972 if (*x1 = z) |
| 973 ++n1; |
| 974 } |
| 975 #endif |
| 976 else do |
| 977 *x1++ = *x++; |
| 978 while(x < xe); |
| 979 b1->wds = n1 - 1; |
| 980 Bfree(b); |
| 981 return b1; |
| 982 } |
| 983 |
| 984 static int |
| 985 cmp |
| 986 #ifdef KR_headers |
| 987 (a, b) Bigint *a, *b; |
| 988 #else |
| 989 (Bigint *a, Bigint *b) |
| 990 #endif |
| 991 { |
| 992 ULong *xa, *xa0, *xb, *xb0; |
| 993 int i, j; |
| 994 |
| 995 i = a->wds; |
| 996 j = b->wds; |
| 997 #ifdef DEBUG |
| 998 if (i > 1 && !a->x[i-1]) |
| 999 Bug("cmp called with a->x[a->wds-1] == 0"); |
| 1000 if (j > 1 && !b->x[j-1]) |
| 1001 Bug("cmp called with b->x[b->wds-1] == 0"); |
| 1002 #endif |
| 1003 if (i -= j) |
| 1004 return i; |
| 1005 xa0 = a->x; |
| 1006 xa = xa0 + j; |
| 1007 xb0 = b->x; |
| 1008 xb = xb0 + j; |
| 1009 for(;;) { |
| 1010 if (*--xa != *--xb) |
| 1011 return *xa < *xb ? -1 : 1; |
| 1012 if (xa <= xa0) |
| 1013 break; |
| 1014 } |
| 1015 return 0; |
| 1016 } |
| 1017 |
| 1018 static Bigint * |
| 1019 diff |
| 1020 #ifdef KR_headers |
| 1021 (a, b) Bigint *a, *b; |
| 1022 #else |
| 1023 (Bigint *a, Bigint *b) |
| 1024 #endif |
| 1025 { |
| 1026 Bigint *c; |
| 1027 int i, wa, wb; |
| 1028 ULong *xa, *xae, *xb, *xbe, *xc; |
| 1029 #ifdef ULLong |
| 1030 ULLong borrow, y; |
| 1031 #else |
| 1032 ULong borrow, y; |
| 1033 #ifdef Pack_32 |
| 1034 ULong z; |
| 1035 #endif |
| 1036 #endif |
| 1037 |
| 1038 i = cmp(a,b); |
| 1039 if (!i) { |
| 1040 c = Balloc(0); |
| 1041 c->wds = 1; |
| 1042 c->x[0] = 0; |
| 1043 return c; |
| 1044 } |
| 1045 if (i < 0) { |
| 1046 c = a; |
| 1047 a = b; |
| 1048 b = c; |
| 1049 i = 1; |
| 1050 } |
| 1051 else |
| 1052 i = 0; |
| 1053 c = Balloc(a->k); |
| 1054 c->sign = i; |
| 1055 wa = a->wds; |
| 1056 xa = a->x; |
| 1057 xae = xa + wa; |
| 1058 wb = b->wds; |
| 1059 xb = b->x; |
| 1060 xbe = xb + wb; |
| 1061 xc = c->x; |
| 1062 borrow = 0; |
| 1063 #ifdef ULLong |
| 1064 do { |
| 1065 y = (ULLong)*xa++ - *xb++ - borrow; |
| 1066 borrow = y >> 32 & (ULong)1; |
| 1067 *xc++ = y & FFFFFFFF; |
| 1068 } |
| 1069 while(xb < xbe); |
| 1070 while(xa < xae) { |
| 1071 y = *xa++ - borrow; |
| 1072 borrow = y >> 32 & (ULong)1; |
| 1073 *xc++ = y & FFFFFFFF; |
| 1074 } |
| 1075 #else |
| 1076 #ifdef Pack_32 |
| 1077 do { |
| 1078 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; |
| 1079 borrow = (y & 0x10000) >> 16; |
| 1080 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; |
| 1081 borrow = (z & 0x10000) >> 16; |
| 1082 Storeinc(xc, z, y); |
| 1083 } |
| 1084 while(xb < xbe); |
| 1085 while(xa < xae) { |
| 1086 y = (*xa & 0xffff) - borrow; |
| 1087 borrow = (y & 0x10000) >> 16; |
| 1088 z = (*xa++ >> 16) - borrow; |
| 1089 borrow = (z & 0x10000) >> 16; |
| 1090 Storeinc(xc, z, y); |
| 1091 } |
| 1092 #else |
| 1093 do { |
| 1094 y = *xa++ - *xb++ - borrow; |
| 1095 borrow = (y & 0x10000) >> 16; |
| 1096 *xc++ = y & 0xffff; |
| 1097 } |
| 1098 while(xb < xbe); |
| 1099 while(xa < xae) { |
| 1100 y = *xa++ - borrow; |
| 1101 borrow = (y & 0x10000) >> 16; |
| 1102 *xc++ = y & 0xffff; |
| 1103 } |
| 1104 #endif |
| 1105 #endif |
| 1106 while(!*--xc) |
| 1107 wa--; |
| 1108 c->wds = wa; |
| 1109 return c; |
| 1110 } |
| 1111 |
| 1112 static double |
| 1113 ulp |
| 1114 #ifdef KR_headers |
| 1115 (x) double x; |
| 1116 #else |
| 1117 (double x) |
| 1118 #endif |
| 1119 { |
| 1120 register Long L; |
| 1121 double a; |
| 1122 |
| 1123 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; |
| 1124 #ifndef Avoid_Underflow |
| 1125 #ifndef Sudden_Underflow |
| 1126 if (L > 0) { |
| 1127 #endif |
| 1128 #endif |
| 1129 #ifdef IBM |
| 1130 L |= Exp_msk1 >> 4; |
| 1131 #endif |
| 1132 word0(a) = L; |
| 1133 word1(a) = 0; |
| 1134 #ifndef Avoid_Underflow |
| 1135 #ifndef Sudden_Underflow |
| 1136 } |
| 1137 else { |
| 1138 L = -L >> Exp_shift; |
| 1139 if (L < Exp_shift) { |
| 1140 word0(a) = 0x80000 >> L; |
| 1141 word1(a) = 0; |
| 1142 } |
| 1143 else { |
| 1144 word0(a) = 0; |
| 1145 L -= Exp_shift; |
| 1146 word1(a) = L >= 31 ? 1 : 1 << 31 - L; |
| 1147 } |
| 1148 } |
| 1149 #endif |
| 1150 #endif |
| 1151 return dval(a); |
| 1152 } |
| 1153 |
| 1154 static double |
| 1155 b2d |
| 1156 #ifdef KR_headers |
| 1157 (a, e) Bigint *a; int *e; |
| 1158 #else |
| 1159 (Bigint *a, int *e) |
| 1160 #endif |
| 1161 { |
| 1162 ULong *xa, *xa0, w, y, z; |
| 1163 int k; |
| 1164 double d; |
| 1165 #ifdef VAX |
| 1166 ULong d0, d1; |
| 1167 #else |
| 1168 #define d0 word0(d) |
| 1169 #define d1 word1(d) |
| 1170 #endif |
| 1171 |
| 1172 xa0 = a->x; |
| 1173 xa = xa0 + a->wds; |
| 1174 y = *--xa; |
| 1175 #ifdef DEBUG |
| 1176 if (!y) Bug("zero y in b2d"); |
| 1177 #endif |
| 1178 k = hi0bits(y); |
| 1179 *e = 32 - k; |
| 1180 #ifdef Pack_32 |
| 1181 if (k < Ebits) { |
| 1182 d0 = Exp_1 | y >> Ebits - k; |
| 1183 w = xa > xa0 ? *--xa : 0; |
| 1184 d1 = y << (32-Ebits) + k | w >> Ebits - k; |
| 1185 goto ret_d; |
| 1186 } |
| 1187 z = xa > xa0 ? *--xa : 0; |
| 1188 if (k -= Ebits) { |
| 1189 d0 = Exp_1 | y << k | z >> 32 - k; |
| 1190 y = xa > xa0 ? *--xa : 0; |
| 1191 d1 = z << k | y >> 32 - k; |
| 1192 } |
| 1193 else { |
| 1194 d0 = Exp_1 | y; |
| 1195 d1 = z; |
| 1196 } |
| 1197 #else |
| 1198 if (k < Ebits + 16) { |
| 1199 z = xa > xa0 ? *--xa : 0; |
| 1200 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; |
| 1201 w = xa > xa0 ? *--xa : 0; |
| 1202 y = xa > xa0 ? *--xa : 0; |
| 1203 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; |
| 1204 goto ret_d; |
| 1205 } |
| 1206 z = xa > xa0 ? *--xa : 0; |
| 1207 w = xa > xa0 ? *--xa : 0; |
| 1208 k -= Ebits + 16; |
| 1209 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; |
| 1210 y = xa > xa0 ? *--xa : 0; |
| 1211 d1 = w << k + 16 | y << k; |
| 1212 #endif |
| 1213 ret_d: |
| 1214 #ifdef VAX |
| 1215 word0(d) = d0 >> 16 | d0 << 16; |
| 1216 word1(d) = d1 >> 16 | d1 << 16; |
| 1217 #else |
| 1218 #undef d0 |
| 1219 #undef d1 |
| 1220 #endif |
| 1221 return dval(d); |
| 1222 } |
| 1223 |
| 1224 static Bigint * |
| 1225 d2b |
| 1226 #ifdef KR_headers |
| 1227 (d, e, bits) double d; int *e, *bits; |
| 1228 #else |
| 1229 (double d, int *e, int *bits) |
| 1230 #endif |
| 1231 { |
| 1232 Bigint *b; |
| 1233 int de, k; |
| 1234 ULong *x, y, z; |
| 1235 #ifndef Sudden_Underflow |
| 1236 int i; |
| 1237 #endif |
| 1238 #ifdef VAX |
| 1239 ULong d0, d1; |
| 1240 d0 = word0(d) >> 16 | word0(d) << 16; |
| 1241 d1 = word1(d) >> 16 | word1(d) << 16; |
| 1242 #else |
| 1243 #define d0 word0(d) |
| 1244 #define d1 word1(d) |
| 1245 #endif |
| 1246 |
| 1247 #ifdef Pack_32 |
| 1248 b = Balloc(1); |
| 1249 #else |
| 1250 b = Balloc(2); |
| 1251 #endif |
| 1252 x = b->x; |
| 1253 |
| 1254 z = d0 & Frac_mask; |
| 1255 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ |
| 1256 #ifdef Sudden_Underflow |
| 1257 de = (int)(d0 >> Exp_shift); |
| 1258 #ifndef IBM |
| 1259 z |= Exp_msk11; |
| 1260 #endif |
| 1261 #else |
| 1262 if (de = (int)(d0 >> Exp_shift)) |
| 1263 z |= Exp_msk1; |
| 1264 #endif |
| 1265 #ifdef Pack_32 |
| 1266 if (y = d1) { |
| 1267 if (k = lo0bits(&y)) { |
| 1268 x[0] = y | z << 32 - k; |
| 1269 z >>= k; |
| 1270 } |
| 1271 else |
| 1272 x[0] = y; |
| 1273 #ifndef Sudden_Underflow |
| 1274 i = |
| 1275 #endif |
| 1276 b->wds = (x[1] = z) ? 2 : 1; |
| 1277 } |
| 1278 else { |
| 1279 #ifdef DEBUG |
| 1280 if (!z) |
| 1281 Bug("Zero passed to d2b"); |
| 1282 #endif |
| 1283 k = lo0bits(&z); |
| 1284 x[0] = z; |
| 1285 #ifndef Sudden_Underflow |
| 1286 i = |
| 1287 #endif |
| 1288 b->wds = 1; |
| 1289 k += 32; |
| 1290 } |
| 1291 #else |
| 1292 if (y = d1) { |
| 1293 if (k = lo0bits(&y)) |
| 1294 if (k >= 16) { |
| 1295 x[0] = y | z << 32 - k & 0xffff; |
| 1296 x[1] = z >> k - 16 & 0xffff; |
| 1297 x[2] = z >> k; |
| 1298 i = 2; |
| 1299 } |
| 1300 else { |
| 1301 x[0] = y & 0xffff; |
| 1302 x[1] = y >> 16 | z << 16 - k & 0xffff; |
| 1303 x[2] = z >> k & 0xffff; |
| 1304 x[3] = z >> k+16; |
| 1305 i = 3; |
| 1306 } |
| 1307 else { |
| 1308 x[0] = y & 0xffff; |
| 1309 x[1] = y >> 16; |
| 1310 x[2] = z & 0xffff; |
| 1311 x[3] = z >> 16; |
| 1312 i = 3; |
| 1313 } |
| 1314 } |
| 1315 else { |
| 1316 #ifdef DEBUG |
| 1317 if (!z) |
| 1318 Bug("Zero passed to d2b"); |
| 1319 #endif |
| 1320 k = lo0bits(&z); |
| 1321 if (k >= 16) { |
| 1322 x[0] = z; |
| 1323 i = 0; |
| 1324 } |
| 1325 else { |
| 1326 x[0] = z & 0xffff; |
| 1327 x[1] = z >> 16; |
| 1328 i = 1; |
| 1329 } |
| 1330 k += 32; |
| 1331 } |
| 1332 while(!x[i]) |
| 1333 --i; |
| 1334 b->wds = i + 1; |
| 1335 #endif |
| 1336 #ifndef Sudden_Underflow |
| 1337 if (de) { |
| 1338 #endif |
| 1339 #ifdef IBM |
| 1340 *e = (de - Bias - (P-1) << 2) + k; |
| 1341 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); |
| 1342 #else |
| 1343 *e = de - Bias - (P-1) + k; |
| 1344 *bits = P - k; |
| 1345 #endif |
| 1346 #ifndef Sudden_Underflow |
| 1347 } |
| 1348 else { |
| 1349 *e = de - Bias - (P-1) + 1 + k; |
| 1350 #ifdef Pack_32 |
| 1351 *bits = 32*i - hi0bits(x[i-1]); |
| 1352 #else |
| 1353 *bits = (i+2)*16 - hi0bits(x[i]); |
| 1354 #endif |
| 1355 } |
| 1356 #endif |
| 1357 return b; |
| 1358 } |
| 1359 #undef d0 |
| 1360 #undef d1 |
| 1361 |
| 1362 static double |
| 1363 ratio |
| 1364 #ifdef KR_headers |
| 1365 (a, b) Bigint *a, *b; |
| 1366 #else |
| 1367 (Bigint *a, Bigint *b) |
| 1368 #endif |
| 1369 { |
| 1370 double da, db; |
| 1371 int k, ka, kb; |
| 1372 |
| 1373 dval(da) = b2d(a, &ka); |
| 1374 dval(db) = b2d(b, &kb); |
| 1375 #ifdef Pack_32 |
| 1376 k = ka - kb + 32*(a->wds - b->wds); |
| 1377 #else |
| 1378 k = ka - kb + 16*(a->wds - b->wds); |
| 1379 #endif |
| 1380 #ifdef IBM |
| 1381 if (k > 0) { |
| 1382 word0(da) += (k >> 2)*Exp_msk1; |
| 1383 if (k &= 3) |
| 1384 dval(da) *= 1 << k; |
| 1385 } |
| 1386 else { |
| 1387 k = -k; |
| 1388 word0(db) += (k >> 2)*Exp_msk1; |
| 1389 if (k &= 3) |
| 1390 dval(db) *= 1 << k; |
| 1391 } |
| 1392 #else |
| 1393 if (k > 0) |
| 1394 word0(da) += k*Exp_msk1; |
| 1395 else { |
| 1396 k = -k; |
| 1397 word0(db) += k*Exp_msk1; |
| 1398 } |
| 1399 #endif |
| 1400 return dval(da) / dval(db); |
| 1401 } |
| 1402 |
| 1403 static CONST double |
| 1404 tens[] = { |
| 1405 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 1406 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 1407 1e20, 1e21, 1e22 |
| 1408 #ifdef VAX |
| 1409 , 1e23, 1e24 |
| 1410 #endif |
| 1411 }; |
| 1412 |
| 1413 static CONST double |
| 1414 #ifdef IEEE_Arith |
| 1415 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; |
| 1416 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, |
| 1417 #ifdef Avoid_Underflow |
| 1418 9007199254740992.*9007199254740992.e-256 |
| 1419 /* = 2^106 * 1e-256 */ |
| 1420 #else |
| 1421 1e-256 |
| 1422 #endif |
| 1423 }; |
| 1424 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ |
| 1425 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */ |
| 1426 #define Scale_Bit 0x10 |
| 1427 #define n_bigtens 5 |
| 1428 #else |
| 1429 #ifdef IBM |
| 1430 bigtens[] = { 1e16, 1e32, 1e64 }; |
| 1431 static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; |
| 1432 #define n_bigtens 3 |
| 1433 #else |
| 1434 bigtens[] = { 1e16, 1e32 }; |
| 1435 static CONST double tinytens[] = { 1e-16, 1e-32 }; |
| 1436 #define n_bigtens 2 |
| 1437 #endif |
| 1438 #endif |
| 1439 |
| 1440 #ifdef INFNAN_CHECK |
| 1441 |
| 1442 #ifndef NAN_WORD0 |
| 1443 #define NAN_WORD0 0x7ff80000 |
| 1444 #endif |
| 1445 |
| 1446 #ifndef NAN_WORD1 |
| 1447 #define NAN_WORD1 0 |
| 1448 #endif |
| 1449 |
| 1450 static int |
| 1451 match |
| 1452 #ifdef KR_headers |
| 1453 (sp, t) char **sp, *t; |
| 1454 #else |
| 1455 (CONST char **sp, char *t) |
| 1456 #endif |
| 1457 { |
| 1458 int c, d; |
| 1459 CONST char *s = *sp; |
| 1460 |
| 1461 while(d = *t++) { |
| 1462 if ((c = *++s) >= 'A' && c <= 'Z') |
| 1463 c += 'a' - 'A'; |
| 1464 if (c != d) |
| 1465 return 0; |
| 1466 } |
| 1467 *sp = s + 1; |
| 1468 return 1; |
| 1469 } |
| 1470 |
| 1471 #ifndef No_Hex_NaN |
| 1472 static void |
| 1473 hexnan |
| 1474 #ifdef KR_headers |
| 1475 (rvp, sp) double *rvp; CONST char **sp; |
| 1476 #else |
| 1477 (double *rvp, CONST char **sp) |
| 1478 #endif |
| 1479 { |
| 1480 ULong c, x[2]; |
| 1481 CONST char *s; |
| 1482 int havedig, udx0, xshift; |
| 1483 |
| 1484 x[0] = x[1] = 0; |
| 1485 havedig = xshift = 0; |
| 1486 udx0 = 1; |
| 1487 s = *sp; |
| 1488 /* allow optional initial 0x or 0X */ |
| 1489 while((c = *(CONST unsigned char*)(s+1)) && c <= ' ') |
| 1490 ++s; |
| 1491 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X')) |
| 1492 s += 2; |
| 1493 while(c = *(CONST unsigned char*)++s) { |
| 1494 if (c >= '0' && c <= '9') |
| 1495 c -= '0'; |
| 1496 else if (c >= 'a' && c <= 'f') |
| 1497 c += 10 - 'a'; |
| 1498 else if (c >= 'A' && c <= 'F') |
| 1499 c += 10 - 'A'; |
| 1500 else if (c <= ' ') { |
| 1501 if (udx0 && havedig) { |
| 1502 udx0 = 0; |
| 1503 xshift = 1; |
| 1504 } |
| 1505 continue; |
| 1506 } |
| 1507 #ifdef GDTOA_NON_PEDANTIC_NANCHECK |
| 1508 else if (/*(*/ c == ')' && havedig) { |
| 1509 *sp = s + 1; |
| 1510 break; |
| 1511 } |
| 1512 else |
| 1513 return; /* invalid form: don't change *sp */ |
| 1514 #else |
| 1515 else { |
| 1516 do { |
| 1517 if (/*(*/ c == ')') { |
| 1518 *sp = s + 1; |
| 1519 break; |
| 1520 } |
| 1521 } while(c = *++s); |
| 1522 break; |
| 1523 } |
| 1524 #endif |
| 1525 havedig = 1; |
| 1526 if (xshift) { |
| 1527 xshift = 0; |
| 1528 x[0] = x[1]; |
| 1529 x[1] = 0; |
| 1530 } |
| 1531 if (udx0) |
| 1532 x[0] = (x[0] << 4) | (x[1] >> 28); |
| 1533 x[1] = (x[1] << 4) | c; |
| 1534 } |
| 1535 if ((x[0] &= 0xfffff) || x[1]) { |
| 1536 word0(*rvp) = Exp_mask | x[0]; |
| 1537 word1(*rvp) = x[1]; |
| 1538 } |
| 1539 } |
| 1540 #endif /*No_Hex_NaN*/ |
| 1541 #endif /* INFNAN_CHECK */ |
| 1542 |
| 1543 double |
| 1544 strtod |
| 1545 #ifdef KR_headers |
| 1546 (s00, se) CONST char *s00; char **se; |
| 1547 #else |
| 1548 (CONST char *s00, char **se) |
| 1549 #endif |
| 1550 { |
| 1551 #ifdef Avoid_Underflow |
| 1552 int scale; |
| 1553 #endif |
| 1554 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, |
| 1555 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; |
| 1556 CONST char *s, *s0, *s1; |
| 1557 double aadj, aadj1, adj, rv, rv0; |
| 1558 Long L; |
| 1559 ULong y, z; |
| 1560 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; |
| 1561 #ifdef SET_INEXACT |
| 1562 int inexact, oldinexact; |
| 1563 #endif |
| 1564 #ifdef Honor_FLT_ROUNDS /*{*/ |
| 1565 int Rounding; |
| 1566 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ |
| 1567 Rounding = Flt_Rounds; |
| 1568 #else /*}{*/ |
| 1569 Rounding = 1; |
| 1570 switch(fegetround()) { |
| 1571 case FE_TOWARDZERO: Rounding = 0; break; |
| 1572 case FE_UPWARD: Rounding = 2; break; |
| 1573 case FE_DOWNWARD: Rounding = 3; |
| 1574 } |
| 1575 #endif /*}}*/ |
| 1576 #endif /*}*/ |
| 1577 #ifdef USE_LOCALE |
| 1578 CONST char *s2; |
| 1579 #endif |
| 1580 |
| 1581 sign = nz0 = nz = 0; |
| 1582 dval(rv) = 0.; |
| 1583 for(s = s00;;s++) switch(*s) { |
| 1584 case '-': |
| 1585 sign = 1; |
| 1586 /* no break */ |
| 1587 case '+': |
| 1588 if (*++s) |
| 1589 goto break2; |
| 1590 /* no break */ |
| 1591 case 0: |
| 1592 goto ret0; |
| 1593 case '\t': |
| 1594 case '\n': |
| 1595 case '\v': |
| 1596 case '\f': |
| 1597 case '\r': |
| 1598 case ' ': |
| 1599 continue; |
| 1600 default: |
| 1601 goto break2; |
| 1602 } |
| 1603 break2: |
| 1604 if (*s == '0') { |
| 1605 nz0 = 1; |
| 1606 while(*++s == '0') ; |
| 1607 if (!*s) |
| 1608 goto ret; |
| 1609 } |
| 1610 s0 = s; |
| 1611 y = z = 0; |
| 1612 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) |
| 1613 if (nd < 9) |
| 1614 y = 10*y + c - '0'; |
| 1615 else if (nd < 16) |
| 1616 z = 10*z + c - '0'; |
| 1617 nd0 = nd; |
| 1618 #ifdef USE_LOCALE |
| 1619 s1 = localeconv()->decimal_point; |
| 1620 if (c == *s1) { |
| 1621 c = '.'; |
| 1622 if (*++s1) { |
| 1623 s2 = s; |
| 1624 for(;;) { |
| 1625 if (*++s2 != *s1) { |
| 1626 c = 0; |
| 1627 break; |
| 1628 } |
| 1629 if (!*++s1) { |
| 1630 s = s2; |
| 1631 break; |
| 1632 } |
| 1633 } |
| 1634 } |
| 1635 } |
| 1636 #endif |
| 1637 if (c == '.') { |
| 1638 c = *++s; |
| 1639 if (!nd) { |
| 1640 for(; c == '0'; c = *++s) |
| 1641 nz++; |
| 1642 if (c > '0' && c <= '9') { |
| 1643 s0 = s; |
| 1644 nf += nz; |
| 1645 nz = 0; |
| 1646 goto have_dig; |
| 1647 } |
| 1648 goto dig_done; |
| 1649 } |
| 1650 for(; c >= '0' && c <= '9'; c = *++s) { |
| 1651 have_dig: |
| 1652 nz++; |
| 1653 if (c -= '0') { |
| 1654 nf += nz; |
| 1655 for(i = 1; i < nz; i++) |
| 1656 if (nd++ < 9) |
| 1657 y *= 10; |
| 1658 else if (nd <= DBL_DIG + 1) |
| 1659 z *= 10; |
| 1660 if (nd++ < 9) |
| 1661 y = 10*y + c; |
| 1662 else if (nd <= DBL_DIG + 1) |
| 1663 z = 10*z + c; |
| 1664 nz = 0; |
| 1665 } |
| 1666 } |
| 1667 } |
| 1668 dig_done: |
| 1669 e = 0; |
| 1670 if (c == 'e' || c == 'E') { |
| 1671 if (!nd && !nz && !nz0) { |
| 1672 goto ret0; |
| 1673 } |
| 1674 s00 = s; |
| 1675 esign = 0; |
| 1676 switch(c = *++s) { |
| 1677 case '-': |
| 1678 esign = 1; |
| 1679 case '+': |
| 1680 c = *++s; |
| 1681 } |
| 1682 if (c >= '0' && c <= '9') { |
| 1683 while(c == '0') |
| 1684 c = *++s; |
| 1685 if (c > '0' && c <= '9') { |
| 1686 L = c - '0'; |
| 1687 s1 = s; |
| 1688 while((c = *++s) >= '0' && c <= '9') |
| 1689 L = 10*L + c - '0'; |
| 1690 if (s - s1 > 8 || L > 19999) |
| 1691 /* Avoid confusion from exponents |
| 1692 * so large that e might overflow. |
| 1693 */ |
| 1694 e = 19999; /* safe for 16 bit ints */ |
| 1695 else |
| 1696 e = (int)L; |
| 1697 if (esign) |
| 1698 e = -e; |
| 1699 } |
| 1700 else |
| 1701 e = 0; |
| 1702 } |
| 1703 else |
| 1704 s = s00; |
| 1705 } |
| 1706 if (!nd) { |
| 1707 if (!nz && !nz0) { |
| 1708 #ifdef INFNAN_CHECK |
| 1709 /* Check for Nan and Infinity */ |
| 1710 switch(c) { |
| 1711 case 'i': |
| 1712 case 'I': |
| 1713 if (match(&s,"nf")) { |
| 1714 --s; |
| 1715 if (!match(&s,"inity")) |
| 1716 ++s; |
| 1717 word0(rv) = 0x7ff00000; |
| 1718 word1(rv) = 0; |
| 1719 goto ret; |
| 1720 } |
| 1721 break; |
| 1722 case 'n': |
| 1723 case 'N': |
| 1724 if (match(&s, "an")) { |
| 1725 word0(rv) = NAN_WORD0; |
| 1726 word1(rv) = NAN_WORD1; |
| 1727 #ifndef No_Hex_NaN |
| 1728 if (*s == '(') /*)*/ |
| 1729 hexnan(&rv, &s); |
| 1730 #endif |
| 1731 goto ret; |
| 1732 } |
| 1733 } |
| 1734 #endif /* INFNAN_CHECK */ |
| 1735 ret0: |
| 1736 s = s00; |
| 1737 sign = 0; |
| 1738 } |
| 1739 goto ret; |
| 1740 } |
| 1741 e1 = e -= nf; |
| 1742 |
| 1743 /* Now we have nd0 digits, starting at s0, followed by a |
| 1744 * decimal point, followed by nd-nd0 digits. The number we're |
| 1745 * after is the integer represented by those digits times |
| 1746 * 10**e */ |
| 1747 |
| 1748 if (!nd0) |
| 1749 nd0 = nd; |
| 1750 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; |
| 1751 dval(rv) = y; |
| 1752 if (k > 9) { |
| 1753 #ifdef SET_INEXACT |
| 1754 if (k > DBL_DIG) |
| 1755 oldinexact = get_inexact(); |
| 1756 #endif |
| 1757 dval(rv) = tens[k - 9] * dval(rv) + z; |
| 1758 } |
| 1759 bd0 = 0; |
| 1760 if (nd <= DBL_DIG |
| 1761 #ifndef RND_PRODQUOT |
| 1762 #ifndef Honor_FLT_ROUNDS |
| 1763 && Flt_Rounds == 1 |
| 1764 #endif |
| 1765 #endif |
| 1766 ) { |
| 1767 if (!e) |
| 1768 goto ret; |
| 1769 if (e > 0) { |
| 1770 if (e <= Ten_pmax) { |
| 1771 #ifdef VAX |
| 1772 goto vax_ovfl_check; |
| 1773 #else |
| 1774 #ifdef Honor_FLT_ROUNDS |
| 1775 /* round correctly FLT_ROUNDS = 2 or 3 */ |
| 1776 if (sign) { |
| 1777 rv = -rv; |
| 1778 sign = 0; |
| 1779 } |
| 1780 #endif |
| 1781 /* rv = */ rounded_product(dval(rv), tens[e]); |
| 1782 goto ret; |
| 1783 #endif |
| 1784 } |
| 1785 i = DBL_DIG - nd; |
| 1786 if (e <= Ten_pmax + i) { |
| 1787 /* A fancier test would sometimes let us do |
| 1788 * this for larger i values. |
| 1789 */ |
| 1790 #ifdef Honor_FLT_ROUNDS |
| 1791 /* round correctly FLT_ROUNDS = 2 or 3 */ |
| 1792 if (sign) { |
| 1793 rv = -rv; |
| 1794 sign = 0; |
| 1795 } |
| 1796 #endif |
| 1797 e -= i; |
| 1798 dval(rv) *= tens[i]; |
| 1799 #ifdef VAX |
| 1800 /* VAX exponent range is so narrow we must |
| 1801 * worry about overflow here... |
| 1802 */ |
| 1803 vax_ovfl_check: |
| 1804 word0(rv) -= P*Exp_msk1; |
| 1805 /* rv = */ rounded_product(dval(rv), tens[e]); |
| 1806 if ((word0(rv) & Exp_mask) |
| 1807 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) |
| 1808 goto ovfl; |
| 1809 word0(rv) += P*Exp_msk1; |
| 1810 #else |
| 1811 /* rv = */ rounded_product(dval(rv), tens[e]); |
| 1812 #endif |
| 1813 goto ret; |
| 1814 } |
| 1815 } |
| 1816 #ifndef Inaccurate_Divide |
| 1817 else if (e >= -Ten_pmax) { |
| 1818 #ifdef Honor_FLT_ROUNDS |
| 1819 /* round correctly FLT_ROUNDS = 2 or 3 */ |
| 1820 if (sign) { |
| 1821 rv = -rv; |
| 1822 sign = 0; |
| 1823 } |
| 1824 #endif |
| 1825 /* rv = */ rounded_quotient(dval(rv), tens[-e]); |
| 1826 goto ret; |
| 1827 } |
| 1828 #endif |
| 1829 } |
| 1830 e1 += nd - k; |
| 1831 |
| 1832 #ifdef IEEE_Arith |
| 1833 #ifdef SET_INEXACT |
| 1834 inexact = 1; |
| 1835 if (k <= DBL_DIG) |
| 1836 oldinexact = get_inexact(); |
| 1837 #endif |
| 1838 #ifdef Avoid_Underflow |
| 1839 scale = 0; |
| 1840 #endif |
| 1841 #ifdef Honor_FLT_ROUNDS |
| 1842 if (Rounding >= 2) { |
| 1843 if (sign) |
| 1844 Rounding = Rounding == 2 ? 0 : 2; |
| 1845 else |
| 1846 if (Rounding != 2) |
| 1847 Rounding = 0; |
| 1848 } |
| 1849 #endif |
| 1850 #endif /*IEEE_Arith*/ |
| 1851 |
| 1852 /* Get starting approximation = rv * 10**e1 */ |
| 1853 |
| 1854 if (e1 > 0) { |
| 1855 if (i = e1 & 15) |
| 1856 dval(rv) *= tens[i]; |
| 1857 if (e1 &= ~15) { |
| 1858 if (e1 > DBL_MAX_10_EXP) { |
| 1859 ovfl: |
| 1860 #ifndef NO_ERRNO |
| 1861 errno = ERANGE; |
| 1862 #endif |
| 1863 /* Can't trust HUGE_VAL */ |
| 1864 #ifdef IEEE_Arith |
| 1865 #ifdef Honor_FLT_ROUNDS |
| 1866 switch(Rounding) { |
| 1867 case 0: /* toward 0 */ |
| 1868 case 3: /* toward -infinity */ |
| 1869 word0(rv) = Big0; |
| 1870 word1(rv) = Big1; |
| 1871 break; |
| 1872 default: |
| 1873 word0(rv) = Exp_mask; |
| 1874 word1(rv) = 0; |
| 1875 } |
| 1876 #else /*Honor_FLT_ROUNDS*/ |
| 1877 word0(rv) = Exp_mask; |
| 1878 word1(rv) = 0; |
| 1879 #endif /*Honor_FLT_ROUNDS*/ |
| 1880 #ifdef SET_INEXACT |
| 1881 /* set overflow bit */ |
| 1882 dval(rv0) = 1e300; |
| 1883 dval(rv0) *= dval(rv0); |
| 1884 #endif |
| 1885 #else /*IEEE_Arith*/ |
| 1886 word0(rv) = Big0; |
| 1887 word1(rv) = Big1; |
| 1888 #endif /*IEEE_Arith*/ |
| 1889 if (bd0) |
| 1890 goto retfree; |
| 1891 goto ret; |
| 1892 } |
| 1893 e1 >>= 4; |
| 1894 for(j = 0; e1 > 1; j++, e1 >>= 1) |
| 1895 if (e1 & 1) |
| 1896 dval(rv) *= bigtens[j]; |
| 1897 /* The last multiplication could overflow. */ |
| 1898 word0(rv) -= P*Exp_msk1; |
| 1899 dval(rv) *= bigtens[j]; |
| 1900 if ((z = word0(rv) & Exp_mask) |
| 1901 > Exp_msk1*(DBL_MAX_EXP+Bias-P)) |
| 1902 goto ovfl; |
| 1903 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { |
| 1904 /* set to largest number */ |
| 1905 /* (Can't trust DBL_MAX) */ |
| 1906 word0(rv) = Big0; |
| 1907 word1(rv) = Big1; |
| 1908 } |
| 1909 else |
| 1910 word0(rv) += P*Exp_msk1; |
| 1911 } |
| 1912 } |
| 1913 else if (e1 < 0) { |
| 1914 e1 = -e1; |
| 1915 if (i = e1 & 15) |
| 1916 dval(rv) /= tens[i]; |
| 1917 if (e1 >>= 4) { |
| 1918 if (e1 >= 1 << n_bigtens) |
| 1919 goto undfl; |
| 1920 #ifdef Avoid_Underflow |
| 1921 if (e1 & Scale_Bit) |
| 1922 scale = 2*P; |
| 1923 for(j = 0; e1 > 0; j++, e1 >>= 1) |
| 1924 if (e1 & 1) |
| 1925 dval(rv) *= tinytens[j]; |
| 1926 if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask) |
| 1927 >> Exp_shift)) > 0) { |
| 1928 /* scaled rv is denormal; clear j low bits */ |
| 1929 if (j >= 32) { |
| 1930 word1(rv) = 0; |
| 1931 if (j >= 53) |
| 1932 word0(rv) = (P+2)*Exp_msk1; |
| 1933 else |
| 1934 word0(rv) &= 0xffffffff << j-32; |
| 1935 } |
| 1936 else |
| 1937 word1(rv) &= 0xffffffff << j; |
| 1938 } |
| 1939 #else |
| 1940 for(j = 0; e1 > 1; j++, e1 >>= 1) |
| 1941 if (e1 & 1) |
| 1942 dval(rv) *= tinytens[j]; |
| 1943 /* The last multiplication could underflow. */ |
| 1944 dval(rv0) = dval(rv); |
| 1945 dval(rv) *= tinytens[j]; |
| 1946 if (!dval(rv)) { |
| 1947 dval(rv) = 2.*dval(rv0); |
| 1948 dval(rv) *= tinytens[j]; |
| 1949 #endif |
| 1950 if (!dval(rv)) { |
| 1951 undfl: |
| 1952 dval(rv) = 0.; |
| 1953 #ifndef NO_ERRNO |
| 1954 errno = ERANGE; |
| 1955 #endif |
| 1956 if (bd0) |
| 1957 goto retfree; |
| 1958 goto ret; |
| 1959 } |
| 1960 #ifndef Avoid_Underflow |
| 1961 word0(rv) = Tiny0; |
| 1962 word1(rv) = Tiny1; |
| 1963 /* The refinement below will clean |
| 1964 * this approximation up. |
| 1965 */ |
| 1966 } |
| 1967 #endif |
| 1968 } |
| 1969 } |
| 1970 |
| 1971 /* Now the hard part -- adjusting rv to the correct value.*/ |
| 1972 |
| 1973 /* Put digits into bd: true value = bd * 10^e */ |
| 1974 |
| 1975 bd0 = s2b(s0, nd0, nd, y); |
| 1976 |
| 1977 for(;;) { |
| 1978 bd = Balloc(bd0->k); |
| 1979 Bcopy(bd, bd0); |
| 1980 bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ |
| 1981 bs = i2b(1); |
| 1982 |
| 1983 if (e >= 0) { |
| 1984 bb2 = bb5 = 0; |
| 1985 bd2 = bd5 = e; |
| 1986 } |
| 1987 else { |
| 1988 bb2 = bb5 = -e; |
| 1989 bd2 = bd5 = 0; |
| 1990 } |
| 1991 if (bbe >= 0) |
| 1992 bb2 += bbe; |
| 1993 else |
| 1994 bd2 -= bbe; |
| 1995 bs2 = bb2; |
| 1996 #ifdef Honor_FLT_ROUNDS |
| 1997 if (Rounding != 1) |
| 1998 bs2++; |
| 1999 #endif |
| 2000 #ifdef Avoid_Underflow |
| 2001 j = bbe - scale; |
| 2002 i = j + bbbits - 1; /* logb(rv) */ |
| 2003 if (i < Emin) /* denormal */ |
| 2004 j += P - Emin; |
| 2005 else |
| 2006 j = P + 1 - bbbits; |
| 2007 #else /*Avoid_Underflow*/ |
| 2008 #ifdef Sudden_Underflow |
| 2009 #ifdef IBM |
| 2010 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); |
| 2011 #else |
| 2012 j = P + 1 - bbbits; |
| 2013 #endif |
| 2014 #else /*Sudden_Underflow*/ |
| 2015 j = bbe; |
| 2016 i = j + bbbits - 1; /* logb(rv) */ |
| 2017 if (i < Emin) /* denormal */ |
| 2018 j += P - Emin; |
| 2019 else |
| 2020 j = P + 1 - bbbits; |
| 2021 #endif /*Sudden_Underflow*/ |
| 2022 #endif /*Avoid_Underflow*/ |
| 2023 bb2 += j; |
| 2024 bd2 += j; |
| 2025 #ifdef Avoid_Underflow |
| 2026 bd2 += scale; |
| 2027 #endif |
| 2028 i = bb2 < bd2 ? bb2 : bd2; |
| 2029 if (i > bs2) |
| 2030 i = bs2; |
| 2031 if (i > 0) { |
| 2032 bb2 -= i; |
| 2033 bd2 -= i; |
| 2034 bs2 -= i; |
| 2035 } |
| 2036 if (bb5 > 0) { |
| 2037 bs = pow5mult(bs, bb5); |
| 2038 bb1 = mult(bs, bb); |
| 2039 Bfree(bb); |
| 2040 bb = bb1; |
| 2041 } |
| 2042 if (bb2 > 0) |
| 2043 bb = lshift(bb, bb2); |
| 2044 if (bd5 > 0) |
| 2045 bd = pow5mult(bd, bd5); |
| 2046 if (bd2 > 0) |
| 2047 bd = lshift(bd, bd2); |
| 2048 if (bs2 > 0) |
| 2049 bs = lshift(bs, bs2); |
| 2050 delta = diff(bb, bd); |
| 2051 dsign = delta->sign; |
| 2052 delta->sign = 0; |
| 2053 i = cmp(delta, bs); |
| 2054 #ifdef Honor_FLT_ROUNDS |
| 2055 if (Rounding != 1) { |
| 2056 if (i < 0) { |
| 2057 /* Error is less than an ulp */ |
| 2058 if (!delta->x[0] && delta->wds <= 1) { |
| 2059 /* exact */ |
| 2060 #ifdef SET_INEXACT |
| 2061 inexact = 0; |
| 2062 #endif |
| 2063 break; |
| 2064 } |
| 2065 if (Rounding) { |
| 2066 if (dsign) { |
| 2067 adj = 1.; |
| 2068 goto apply_adj; |
| 2069 } |
| 2070 } |
| 2071 else if (!dsign) { |
| 2072 adj = -1.; |
| 2073 if (!word1(rv) |
| 2074 && !(word0(rv) & Frac_mask)) { |
| 2075 y = word0(rv) & Exp_mask; |
| 2076 #ifdef Avoid_Underflow |
| 2077 if (!scale || y > 2*P*Exp_msk1) |
| 2078 #else |
| 2079 if (y) |
| 2080 #endif |
| 2081 { |
| 2082 delta = lshift(delta,Log2P); |
| 2083 if (cmp(delta, bs) <= 0) |
| 2084 adj = -0.5; |
| 2085 } |
| 2086 } |
| 2087 apply_adj: |
| 2088 #ifdef Avoid_Underflow |
| 2089 if (scale && (y = word0(rv) & Exp_mask) |
| 2090 <= 2*P*Exp_msk1) |
| 2091 word0(adj) += (2*P+1)*Exp_msk1 - y; |
| 2092 #else |
| 2093 #ifdef Sudden_Underflow |
| 2094 if ((word0(rv) & Exp_mask) <= |
| 2095 P*Exp_msk1) { |
| 2096 word0(rv) += P*Exp_msk1; |
| 2097 dval(rv) += adj*ulp(dval(rv)); |
| 2098 word0(rv) -= P*Exp_msk1; |
| 2099 } |
| 2100 else |
| 2101 #endif /*Sudden_Underflow*/ |
| 2102 #endif /*Avoid_Underflow*/ |
| 2103 dval(rv) += adj*ulp(dval(rv)); |
| 2104 } |
| 2105 break; |
| 2106 } |
| 2107 adj = ratio(delta, bs); |
| 2108 if (adj < 1.) |
| 2109 adj = 1.; |
| 2110 if (adj <= 0x7ffffffe) { |
| 2111 /* adj = rounding ? ceil(adj) : floor(adj); */ |
| 2112 y = adj; |
| 2113 if (y != adj) { |
| 2114 if (!((Rounding>>1) ^ dsign)) |
| 2115 y++; |
| 2116 adj = y; |
| 2117 } |
| 2118 } |
| 2119 #ifdef Avoid_Underflow |
| 2120 if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) |
| 2121 word0(adj) += (2*P+1)*Exp_msk1 - y; |
| 2122 #else |
| 2123 #ifdef Sudden_Underflow |
| 2124 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { |
| 2125 word0(rv) += P*Exp_msk1; |
| 2126 adj *= ulp(dval(rv)); |
| 2127 if (dsign) |
| 2128 dval(rv) += adj; |
| 2129 else |
| 2130 dval(rv) -= adj; |
| 2131 word0(rv) -= P*Exp_msk1; |
| 2132 goto cont; |
| 2133 } |
| 2134 #endif /*Sudden_Underflow*/ |
| 2135 #endif /*Avoid_Underflow*/ |
| 2136 adj *= ulp(dval(rv)); |
| 2137 if (dsign) { |
| 2138 if (word0(rv) == Big0 && word1(rv) == Big1) |
| 2139 goto ovfl; |
| 2140 dval(rv) += adj; |
| 2141 } |
| 2142 else |
| 2143 dval(rv) -= adj; |
| 2144 goto cont; |
| 2145 } |
| 2146 #endif /*Honor_FLT_ROUNDS*/ |
| 2147 |
| 2148 if (i < 0) { |
| 2149 /* Error is less than half an ulp -- check for |
| 2150 * special case of mantissa a power of two. |
| 2151 */ |
| 2152 if (dsign || word1(rv) || word0(rv) & Bndry_mask |
| 2153 #ifdef IEEE_Arith |
| 2154 #ifdef Avoid_Underflow |
| 2155 || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1 |
| 2156 #else |
| 2157 || (word0(rv) & Exp_mask) <= Exp_msk1 |
| 2158 #endif |
| 2159 #endif |
| 2160 ) { |
| 2161 #ifdef SET_INEXACT |
| 2162 if (!delta->x[0] && delta->wds <= 1) |
| 2163 inexact = 0; |
| 2164 #endif |
| 2165 break; |
| 2166 } |
| 2167 if (!delta->x[0] && delta->wds <= 1) { |
| 2168 /* exact result */ |
| 2169 #ifdef SET_INEXACT |
| 2170 inexact = 0; |
| 2171 #endif |
| 2172 break; |
| 2173 } |
| 2174 delta = lshift(delta,Log2P); |
| 2175 if (cmp(delta, bs) > 0) |
| 2176 goto drop_down; |
| 2177 break; |
| 2178 } |
| 2179 if (i == 0) { |
| 2180 /* exactly half-way between */ |
| 2181 if (dsign) { |
| 2182 if ((word0(rv) & Bndry_mask1) == Bndry_mask1 |
| 2183 && word1(rv) == ( |
| 2184 #ifdef Avoid_Underflow |
| 2185 (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) |
| 2186 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : |
| 2187 #endif |
| 2188 0xffffffff)) { |
| 2189 /*boundary case -- increment exponent*/ |
| 2190 word0(rv) = (word0(rv) & Exp_mask) |
| 2191 + Exp_msk1 |
| 2192 #ifdef IBM |
| 2193 | Exp_msk1 >> 4 |
| 2194 #endif |
| 2195 ; |
| 2196 word1(rv) = 0; |
| 2197 #ifdef Avoid_Underflow |
| 2198 dsign = 0; |
| 2199 #endif |
| 2200 break; |
| 2201 } |
| 2202 } |
| 2203 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { |
| 2204 drop_down: |
| 2205 /* boundary case -- decrement exponent */ |
| 2206 #ifdef Sudden_Underflow /*{{*/ |
| 2207 L = word0(rv) & Exp_mask; |
| 2208 #ifdef IBM |
| 2209 if (L < Exp_msk1) |
| 2210 #else |
| 2211 #ifdef Avoid_Underflow |
| 2212 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) |
| 2213 #else |
| 2214 if (L <= Exp_msk1) |
| 2215 #endif /*Avoid_Underflow*/ |
| 2216 #endif /*IBM*/ |
| 2217 goto undfl; |
| 2218 L -= Exp_msk1; |
| 2219 #else /*Sudden_Underflow}{*/ |
| 2220 #ifdef Avoid_Underflow |
| 2221 if (scale) { |
| 2222 L = word0(rv) & Exp_mask; |
| 2223 if (L <= (2*P+1)*Exp_msk1) { |
| 2224 if (L > (P+2)*Exp_msk1) |
| 2225 /* round even ==> */ |
| 2226 /* accept rv */ |
| 2227 break; |
| 2228 /* rv = smallest denormal */ |
| 2229 goto undfl; |
| 2230 } |
| 2231 } |
| 2232 #endif /*Avoid_Underflow*/ |
| 2233 L = (word0(rv) & Exp_mask) - Exp_msk1; |
| 2234 #endif /*Sudden_Underflow}}*/ |
| 2235 word0(rv) = L | Bndry_mask1; |
| 2236 word1(rv) = 0xffffffff; |
| 2237 #ifdef IBM |
| 2238 goto cont; |
| 2239 #else |
| 2240 break; |
| 2241 #endif |
| 2242 } |
| 2243 #ifndef ROUND_BIASED |
| 2244 if (!(word1(rv) & LSB)) |
| 2245 break; |
| 2246 #endif |
| 2247 if (dsign) |
| 2248 dval(rv) += ulp(dval(rv)); |
| 2249 #ifndef ROUND_BIASED |
| 2250 else { |
| 2251 dval(rv) -= ulp(dval(rv)); |
| 2252 #ifndef Sudden_Underflow |
| 2253 if (!dval(rv)) |
| 2254 goto undfl; |
| 2255 #endif |
| 2256 } |
| 2257 #ifdef Avoid_Underflow |
| 2258 dsign = 1 - dsign; |
| 2259 #endif |
| 2260 #endif |
| 2261 break; |
| 2262 } |
| 2263 if ((aadj = ratio(delta, bs)) <= 2.) { |
| 2264 if (dsign) |
| 2265 aadj = aadj1 = 1.; |
| 2266 else if (word1(rv) || word0(rv) & Bndry_mask) { |
| 2267 #ifndef Sudden_Underflow |
| 2268 if (word1(rv) == Tiny1 && !word0(rv)) |
| 2269 goto undfl; |
| 2270 #endif |
| 2271 aadj = 1.; |
| 2272 aadj1 = -1.; |
| 2273 } |
| 2274 else { |
| 2275 /* special case -- power of FLT_RADIX to be */ |
| 2276 /* rounded down... */ |
| 2277 |
| 2278 if (aadj < 2./FLT_RADIX) |
| 2279 aadj = 1./FLT_RADIX; |
| 2280 else |
| 2281 aadj *= 0.5; |
| 2282 aadj1 = -aadj; |
| 2283 } |
| 2284 } |
| 2285 else { |
| 2286 aadj *= 0.5; |
| 2287 aadj1 = dsign ? aadj : -aadj; |
| 2288 #ifdef Check_FLT_ROUNDS |
| 2289 switch(Rounding) { |
| 2290 case 2: /* towards +infinity */ |
| 2291 aadj1 -= 0.5; |
| 2292 break; |
| 2293 case 0: /* towards 0 */ |
| 2294 case 3: /* towards -infinity */ |
| 2295 aadj1 += 0.5; |
| 2296 } |
| 2297 #else |
| 2298 if (Flt_Rounds == 0) |
| 2299 aadj1 += 0.5; |
| 2300 #endif /*Check_FLT_ROUNDS*/ |
| 2301 } |
| 2302 y = word0(rv) & Exp_mask; |
| 2303 |
| 2304 /* Check for overflow */ |
| 2305 |
| 2306 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { |
| 2307 dval(rv0) = dval(rv); |
| 2308 word0(rv) -= P*Exp_msk1; |
| 2309 adj = aadj1 * ulp(dval(rv)); |
| 2310 dval(rv) += adj; |
| 2311 if ((word0(rv) & Exp_mask) >= |
| 2312 Exp_msk1*(DBL_MAX_EXP+Bias-P)) { |
| 2313 if (word0(rv0) == Big0 && word1(rv0) == Big1) |
| 2314 goto ovfl; |
| 2315 word0(rv) = Big0; |
| 2316 word1(rv) = Big1; |
| 2317 goto cont; |
| 2318 } |
| 2319 else |
| 2320 word0(rv) += P*Exp_msk1; |
| 2321 } |
| 2322 else { |
| 2323 #ifdef Avoid_Underflow |
| 2324 if (scale && y <= 2*P*Exp_msk1) { |
| 2325 if (aadj <= 0x7fffffff) { |
| 2326 if ((z = aadj) <= 0) |
| 2327 z = 1; |
| 2328 aadj = z; |
| 2329 aadj1 = dsign ? aadj : -aadj; |
| 2330 } |
| 2331 word0(aadj1) += (2*P+1)*Exp_msk1 - y; |
| 2332 } |
| 2333 adj = aadj1 * ulp(dval(rv)); |
| 2334 dval(rv) += adj; |
| 2335 #else |
| 2336 #ifdef Sudden_Underflow |
| 2337 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { |
| 2338 dval(rv0) = dval(rv); |
| 2339 word0(rv) += P*Exp_msk1; |
| 2340 adj = aadj1 * ulp(dval(rv)); |
| 2341 dval(rv) += adj; |
| 2342 #ifdef IBM |
| 2343 if ((word0(rv) & Exp_mask) < P*Exp_msk1) |
| 2344 #else |
| 2345 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) |
| 2346 #endif |
| 2347 { |
| 2348 if (word0(rv0) == Tiny0 |
| 2349 && word1(rv0) == Tiny1) |
| 2350 goto undfl; |
| 2351 word0(rv) = Tiny0; |
| 2352 word1(rv) = Tiny1; |
| 2353 goto cont; |
| 2354 } |
| 2355 else |
| 2356 word0(rv) -= P*Exp_msk1; |
| 2357 } |
| 2358 else { |
| 2359 adj = aadj1 * ulp(dval(rv)); |
| 2360 dval(rv) += adj; |
| 2361 } |
| 2362 #else /*Sudden_Underflow*/ |
| 2363 /* Compute adj so that the IEEE rounding rules will |
| 2364 * correctly round rv + adj in some half-way cases. |
| 2365 * If rv * ulp(rv) is denormalized (i.e., |
| 2366 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid |
| 2367 * trouble from bits lost to denormalization; |
| 2368 * example: 1.2e-307 . |
| 2369 */ |
| 2370 if (y <= (P-1)*Exp_msk1 && aadj > 1.) { |
| 2371 aadj1 = (double)(int)(aadj + 0.5); |
| 2372 if (!dsign) |
| 2373 aadj1 = -aadj1; |
| 2374 } |
| 2375 adj = aadj1 * ulp(dval(rv)); |
| 2376 dval(rv) += adj; |
| 2377 #endif /*Sudden_Underflow*/ |
| 2378 #endif /*Avoid_Underflow*/ |
| 2379 } |
| 2380 z = word0(rv) & Exp_mask; |
| 2381 #ifndef SET_INEXACT |
| 2382 #ifdef Avoid_Underflow |
| 2383 if (!scale) |
| 2384 #endif |
| 2385 if (y == z) { |
| 2386 /* Can we stop now? */ |
| 2387 L = (Long)aadj; |
| 2388 aadj -= L; |
| 2389 /* The tolerances below are conservative. */ |
| 2390 if (dsign || word1(rv) || word0(rv) & Bndry_mask) { |
| 2391 if (aadj < .4999999 || aadj > .5000001) |
| 2392 break; |
| 2393 } |
| 2394 else if (aadj < .4999999/FLT_RADIX) |
| 2395 break; |
| 2396 } |
| 2397 #endif |
| 2398 cont: |
| 2399 Bfree(bb); |
| 2400 Bfree(bd); |
| 2401 Bfree(bs); |
| 2402 Bfree(delta); |
| 2403 } |
| 2404 #ifdef SET_INEXACT |
| 2405 if (inexact) { |
| 2406 if (!oldinexact) { |
| 2407 word0(rv0) = Exp_1 + (70 << Exp_shift); |
| 2408 word1(rv0) = 0; |
| 2409 dval(rv0) += 1.; |
| 2410 } |
| 2411 } |
| 2412 else if (!oldinexact) |
| 2413 clear_inexact(); |
| 2414 #endif |
| 2415 #ifdef Avoid_Underflow |
| 2416 if (scale) { |
| 2417 word0(rv0) = Exp_1 - 2*P*Exp_msk1; |
| 2418 word1(rv0) = 0; |
| 2419 dval(rv) *= dval(rv0); |
| 2420 #ifndef NO_ERRNO |
| 2421 /* try to avoid the bug of testing an 8087 register value */ |
| 2422 #ifdef IEEE_Arith |
| 2423 if (!(word0(rv) & Exp_mask)) |
| 2424 #else |
| 2425 if (word0(rv) == 0 && word1(rv) == 0) |
| 2426 #endif |
| 2427 errno = ERANGE; |
| 2428 #endif |
| 2429 } |
| 2430 #endif /* Avoid_Underflow */ |
| 2431 #ifdef SET_INEXACT |
| 2432 if (inexact && !(word0(rv) & Exp_mask)) { |
| 2433 /* set underflow bit */ |
| 2434 dval(rv0) = 1e-300; |
| 2435 dval(rv0) *= dval(rv0); |
| 2436 } |
| 2437 #endif |
| 2438 retfree: |
| 2439 Bfree(bb); |
| 2440 Bfree(bd); |
| 2441 Bfree(bs); |
| 2442 Bfree(bd0); |
| 2443 Bfree(delta); |
| 2444 ret: |
| 2445 if (se) |
| 2446 *se = (char *)s; |
| 2447 return sign ? -dval(rv) : dval(rv); |
| 2448 } |
| 2449 |
| 2450 static int |
| 2451 quorem |
| 2452 #ifdef KR_headers |
| 2453 (b, S) Bigint *b, *S; |
| 2454 #else |
| 2455 (Bigint *b, Bigint *S) |
| 2456 #endif |
| 2457 { |
| 2458 int n; |
| 2459 ULong *bx, *bxe, q, *sx, *sxe; |
| 2460 #ifdef ULLong |
| 2461 ULLong borrow, carry, y, ys; |
| 2462 #else |
| 2463 ULong borrow, carry, y, ys; |
| 2464 #ifdef Pack_32 |
| 2465 ULong si, z, zs; |
| 2466 #endif |
| 2467 #endif |
| 2468 |
| 2469 n = S->wds; |
| 2470 #ifdef DEBUG |
| 2471 /*debug*/ if (b->wds > n) |
| 2472 /*debug*/ Bug("oversize b in quorem"); |
| 2473 #endif |
| 2474 if (b->wds < n) |
| 2475 return 0; |
| 2476 sx = S->x; |
| 2477 sxe = sx + --n; |
| 2478 bx = b->x; |
| 2479 bxe = bx + n; |
| 2480 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ |
| 2481 #ifdef DEBUG |
| 2482 /*debug*/ if (q > 9) |
| 2483 /*debug*/ Bug("oversized quotient in quorem"); |
| 2484 #endif |
| 2485 if (q) { |
| 2486 borrow = 0; |
| 2487 carry = 0; |
| 2488 do { |
| 2489 #ifdef ULLong |
| 2490 ys = *sx++ * (ULLong)q + carry; |
| 2491 carry = ys >> 32; |
| 2492 y = *bx - (ys & FFFFFFFF) - borrow; |
| 2493 borrow = y >> 32 & (ULong)1; |
| 2494 *bx++ = y & FFFFFFFF; |
| 2495 #else |
| 2496 #ifdef Pack_32 |
| 2497 si = *sx++; |
| 2498 ys = (si & 0xffff) * q + carry; |
| 2499 zs = (si >> 16) * q + (ys >> 16); |
| 2500 carry = zs >> 16; |
| 2501 y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
| 2502 borrow = (y & 0x10000) >> 16; |
| 2503 z = (*bx >> 16) - (zs & 0xffff) - borrow; |
| 2504 borrow = (z & 0x10000) >> 16; |
| 2505 Storeinc(bx, z, y); |
| 2506 #else |
| 2507 ys = *sx++ * q + carry; |
| 2508 carry = ys >> 16; |
| 2509 y = *bx - (ys & 0xffff) - borrow; |
| 2510 borrow = (y & 0x10000) >> 16; |
| 2511 *bx++ = y & 0xffff; |
| 2512 #endif |
| 2513 #endif |
| 2514 } |
| 2515 while(sx <= sxe); |
| 2516 if (!*bxe) { |
| 2517 bx = b->x; |
| 2518 while(--bxe > bx && !*bxe) |
| 2519 --n; |
| 2520 b->wds = n; |
| 2521 } |
| 2522 } |
| 2523 if (cmp(b, S) >= 0) { |
| 2524 q++; |
| 2525 borrow = 0; |
| 2526 carry = 0; |
| 2527 bx = b->x; |
| 2528 sx = S->x; |
| 2529 do { |
| 2530 #ifdef ULLong |
| 2531 ys = *sx++ + carry; |
| 2532 carry = ys >> 32; |
| 2533 y = *bx - (ys & FFFFFFFF) - borrow; |
| 2534 borrow = y >> 32 & (ULong)1; |
| 2535 *bx++ = y & FFFFFFFF; |
| 2536 #else |
| 2537 #ifdef Pack_32 |
| 2538 si = *sx++; |
| 2539 ys = (si & 0xffff) + carry; |
| 2540 zs = (si >> 16) + (ys >> 16); |
| 2541 carry = zs >> 16; |
| 2542 y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
| 2543 borrow = (y & 0x10000) >> 16; |
| 2544 z = (*bx >> 16) - (zs & 0xffff) - borrow; |
| 2545 borrow = (z & 0x10000) >> 16; |
| 2546 Storeinc(bx, z, y); |
| 2547 #else |
| 2548 ys = *sx++ + carry; |
| 2549 carry = ys >> 16; |
| 2550 y = *bx - (ys & 0xffff) - borrow; |
| 2551 borrow = (y & 0x10000) >> 16; |
| 2552 *bx++ = y & 0xffff; |
| 2553 #endif |
| 2554 #endif |
| 2555 } |
| 2556 while(sx <= sxe); |
| 2557 bx = b->x; |
| 2558 bxe = bx + n; |
| 2559 if (!*bxe) { |
| 2560 while(--bxe > bx && !*bxe) |
| 2561 --n; |
| 2562 b->wds = n; |
| 2563 } |
| 2564 } |
| 2565 return q; |
| 2566 } |
| 2567 |
| 2568 #ifndef MULTIPLE_THREADS |
| 2569 static char *dtoa_result; |
| 2570 #endif |
| 2571 |
| 2572 static char * |
| 2573 #ifdef KR_headers |
| 2574 rv_alloc(i) int i; |
| 2575 #else |
| 2576 rv_alloc(int i) |
| 2577 #endif |
| 2578 { |
| 2579 int j, k, *r; |
| 2580 |
| 2581 j = sizeof(ULong); |
| 2582 for(k = 0; |
| 2583 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i; |
| 2584 j <<= 1) |
| 2585 k++; |
| 2586 r = (int*)Balloc(k); |
| 2587 *r = k; |
| 2588 return |
| 2589 #ifndef MULTIPLE_THREADS |
| 2590 dtoa_result = |
| 2591 #endif |
| 2592 (char *)(r+1); |
| 2593 } |
| 2594 |
| 2595 static char * |
| 2596 #ifdef KR_headers |
| 2597 nrv_alloc(s, rve, n) char *s, **rve; int n; |
| 2598 #else |
| 2599 nrv_alloc(char *s, char **rve, int n) |
| 2600 #endif |
| 2601 { |
| 2602 char *rv, *t; |
| 2603 |
| 2604 t = rv = rv_alloc(n); |
| 2605 while(*t = *s++) t++; |
| 2606 if (rve) |
| 2607 *rve = t; |
| 2608 return rv; |
| 2609 } |
| 2610 |
| 2611 /* freedtoa(s) must be used to free values s returned by dtoa |
| 2612 * when MULTIPLE_THREADS is #defined. It should be used in all cases, |
| 2613 * but for consistency with earlier versions of dtoa, it is optional |
| 2614 * when MULTIPLE_THREADS is not defined. |
| 2615 */ |
| 2616 |
| 2617 void |
| 2618 #ifdef KR_headers |
| 2619 freedtoa(s) char *s; |
| 2620 #else |
| 2621 freedtoa(char *s) |
| 2622 #endif |
| 2623 { |
| 2624 Bigint *b = (Bigint *)((int *)s - 1); |
| 2625 b->maxwds = 1 << (b->k = *(int*)b); |
| 2626 Bfree(b); |
| 2627 #ifndef MULTIPLE_THREADS |
| 2628 if (s == dtoa_result) |
| 2629 dtoa_result = 0; |
| 2630 #endif |
| 2631 } |
| 2632 |
| 2633 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. |
| 2634 * |
| 2635 * Inspired by "How to Print Floating-Point Numbers Accurately" by |
| 2636 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. |
| 2637 * |
| 2638 * Modifications: |
| 2639 * 1. Rather than iterating, we use a simple numeric overestimate |
| 2640 * to determine k = floor(log10(d)). We scale relevant |
| 2641 * quantities using O(log2(k)) rather than O(k) multiplications. |
| 2642 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't |
| 2643 * try to generate digits strictly left to right. Instead, we |
| 2644 * compute with fewer bits and propagate the carry if necessary |
| 2645 * when rounding the final digit up. This is often faster. |
| 2646 * 3. Under the assumption that input will be rounded nearest, |
| 2647 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. |
| 2648 * That is, we allow equality in stopping tests when the |
| 2649 * round-nearest rule will give the same floating-point value |
| 2650 * as would satisfaction of the stopping test with strict |
| 2651 * inequality. |
| 2652 * 4. We remove common factors of powers of 2 from relevant |
| 2653 * quantities. |
| 2654 * 5. When converting floating-point integers less than 1e16, |
| 2655 * we use floating-point arithmetic rather than resorting |
| 2656 * to multiple-precision integers. |
| 2657 * 6. When asked to produce fewer than 15 digits, we first try |
| 2658 * to get by with floating-point arithmetic; we resort to |
| 2659 * multiple-precision integer arithmetic only if we cannot |
| 2660 * guarantee that the floating-point calculation has given |
| 2661 * the correctly rounded result. For k requested digits and |
| 2662 * "uniformly" distributed input, the probability is |
| 2663 * something like 10^(k-15) that we must resort to the Long |
| 2664 * calculation. |
| 2665 */ |
| 2666 |
| 2667 char * |
| 2668 dtoa |
| 2669 #ifdef KR_headers |
| 2670 (d, mode, ndigits, decpt, sign, rve) |
| 2671 double d; int mode, ndigits, *decpt, *sign; char **rve; |
| 2672 #else |
| 2673 (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) |
| 2674 #endif |
| 2675 { |
| 2676 /* Arguments ndigits, decpt, sign are similar to those |
| 2677 of ecvt and fcvt; trailing zeros are suppressed from |
| 2678 the returned string. If not null, *rve is set to point |
| 2679 to the end of the return value. If d is +-Infinity or NaN, |
| 2680 then *decpt is set to 9999. |
| 2681 |
| 2682 mode: |
| 2683 0 ==> shortest string that yields d when read in |
| 2684 and rounded to nearest. |
| 2685 1 ==> like 0, but with Steele & White stopping rule; |
| 2686 e.g. with IEEE P754 arithmetic , mode 0 gives |
| 2687 1e23 whereas mode 1 gives 9.999999999999999e22. |
| 2688 2 ==> max(1,ndigits) significant digits. This gives a |
| 2689 return value similar to that of ecvt, except |
| 2690 that trailing zeros are suppressed. |
| 2691 3 ==> through ndigits past the decimal point. This |
| 2692 gives a return value similar to that from fcvt, |
| 2693 except that trailing zeros are suppressed, and |
| 2694 ndigits can be negative. |
| 2695 4,5 ==> similar to 2 and 3, respectively, but (in |
| 2696 round-nearest mode) with the tests of mode 0 to |
| 2697 possibly return a shorter string that rounds to d. |
| 2698 With IEEE arithmetic and compilation with |
| 2699 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same |
| 2700 as modes 2 and 3 when FLT_ROUNDS != 1. |
| 2701 6-9 ==> Debugging modes similar to mode - 4: don't try |
| 2702 fast floating-point estimate (if applicable). |
| 2703 |
| 2704 Values of mode other than 0-9 are treated as mode 0. |
| 2705 |
| 2706 Sufficient space is allocated to the return value |
| 2707 to hold the suppressed trailing zeros. |
| 2708 */ |
| 2709 |
| 2710 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, |
| 2711 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, |
| 2712 spec_case, try_quick; |
| 2713 Long L; |
| 2714 #ifndef Sudden_Underflow |
| 2715 int denorm; |
| 2716 ULong x; |
| 2717 #endif |
| 2718 Bigint *b, *b1, *delta, *mlo, *mhi, *S; |
| 2719 double d2, ds, eps; |
| 2720 char *s, *s0; |
| 2721 #ifdef SET_INEXACT |
| 2722 int inexact, oldinexact; |
| 2723 #endif |
| 2724 #ifdef Honor_FLT_ROUNDS /*{*/ |
| 2725 int Rounding; |
| 2726 #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */ |
| 2727 Rounding = Flt_Rounds; |
| 2728 #else /*}{*/ |
| 2729 Rounding = 1; |
| 2730 switch(fegetround()) { |
| 2731 case FE_TOWARDZERO: Rounding = 0; break; |
| 2732 case FE_UPWARD: Rounding = 2; break; |
| 2733 case FE_DOWNWARD: Rounding = 3; |
| 2734 } |
| 2735 #endif /*}}*/ |
| 2736 #endif /*}*/ |
| 2737 |
| 2738 #ifndef MULTIPLE_THREADS |
| 2739 if (dtoa_result) { |
| 2740 freedtoa(dtoa_result); |
| 2741 dtoa_result = 0; |
| 2742 } |
| 2743 #endif |
| 2744 |
| 2745 if (word0(d) & Sign_bit) { |
| 2746 /* set sign for everything, including 0's and NaNs */ |
| 2747 *sign = 1; |
| 2748 word0(d) &= ~Sign_bit; /* clear sign bit */ |
| 2749 } |
| 2750 else |
| 2751 *sign = 0; |
| 2752 |
| 2753 #if defined(IEEE_Arith) + defined(VAX) |
| 2754 #ifdef IEEE_Arith |
| 2755 if ((word0(d) & Exp_mask) == Exp_mask) |
| 2756 #else |
| 2757 if (word0(d) == 0x8000) |
| 2758 #endif |
| 2759 { |
| 2760 /* Infinity or NaN */ |
| 2761 *decpt = 9999; |
| 2762 #ifdef IEEE_Arith |
| 2763 if (!word1(d) && !(word0(d) & 0xfffff)) |
| 2764 return nrv_alloc("Infinity", rve, 8); |
| 2765 #endif |
| 2766 return nrv_alloc("NaN", rve, 3); |
| 2767 } |
| 2768 #endif |
| 2769 #ifdef IBM |
| 2770 dval(d) += 0; /* normalize */ |
| 2771 #endif |
| 2772 if (!dval(d)) { |
| 2773 *decpt = 1; |
| 2774 return nrv_alloc("0", rve, 1); |
| 2775 } |
| 2776 |
| 2777 #ifdef SET_INEXACT |
| 2778 try_quick = oldinexact = get_inexact(); |
| 2779 inexact = 1; |
| 2780 #endif |
| 2781 #ifdef Honor_FLT_ROUNDS |
| 2782 if (Rounding >= 2) { |
| 2783 if (*sign) |
| 2784 Rounding = Rounding == 2 ? 0 : 2; |
| 2785 else |
| 2786 if (Rounding != 2) |
| 2787 Rounding = 0; |
| 2788 } |
| 2789 #endif |
| 2790 |
| 2791 b = d2b(dval(d), &be, &bbits); |
| 2792 #ifdef Sudden_Underflow |
| 2793 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); |
| 2794 #else |
| 2795 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { |
| 2796 #endif |
| 2797 dval(d2) = dval(d); |
| 2798 word0(d2) &= Frac_mask1; |
| 2799 word0(d2) |= Exp_11; |
| 2800 #ifdef IBM |
| 2801 if (j = 11 - hi0bits(word0(d2) & Frac_mask)) |
| 2802 dval(d2) /= 1 << j; |
| 2803 #endif |
| 2804 |
| 2805 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 |
| 2806 * log10(x) = log(x) / log(10) |
| 2807 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) |
| 2808 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) |
| 2809 * |
| 2810 * This suggests computing an approximation k to log10(d) by |
| 2811 * |
| 2812 * k = (i - Bias)*0.301029995663981 |
| 2813 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); |
| 2814 * |
| 2815 * We want k to be too large rather than too small. |
| 2816 * The error in the first-order Taylor series approximation |
| 2817 * is in our favor, so we just round up the constant enough |
| 2818 * to compensate for any error in the multiplication of |
| 2819 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, |
| 2820 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, |
| 2821 * adding 1e-13 to the constant term more than suffices. |
| 2822 * Hence we adjust the constant term to 0.1760912590558. |
| 2823 * (We could get a more accurate k by invoking log10, |
| 2824 * but this is probably not worthwhile.) |
| 2825 */ |
| 2826 |
| 2827 i -= Bias; |
| 2828 #ifdef IBM |
| 2829 i <<= 2; |
| 2830 i += j; |
| 2831 #endif |
| 2832 #ifndef Sudden_Underflow |
| 2833 denorm = 0; |
| 2834 } |
| 2835 else { |
| 2836 /* d is denormalized */ |
| 2837 |
| 2838 i = bbits + be + (Bias + (P-1) - 1); |
| 2839 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 |
| 2840 : word1(d) << 32 - i; |
| 2841 dval(d2) = x; |
| 2842 word0(d2) -= 31*Exp_msk1; /* adjust exponent */ |
| 2843 i -= (Bias + (P-1) - 1) + 1; |
| 2844 denorm = 1; |
| 2845 } |
| 2846 #endif |
| 2847 ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.3010299956
63981; |
| 2848 k = (int)ds; |
| 2849 if (ds < 0. && ds != k) |
| 2850 k--; /* want k = floor(ds) */ |
| 2851 k_check = 1; |
| 2852 if (k >= 0 && k <= Ten_pmax) { |
| 2853 if (dval(d) < tens[k]) |
| 2854 k--; |
| 2855 k_check = 0; |
| 2856 } |
| 2857 j = bbits - i - 1; |
| 2858 if (j >= 0) { |
| 2859 b2 = 0; |
| 2860 s2 = j; |
| 2861 } |
| 2862 else { |
| 2863 b2 = -j; |
| 2864 s2 = 0; |
| 2865 } |
| 2866 if (k >= 0) { |
| 2867 b5 = 0; |
| 2868 s5 = k; |
| 2869 s2 += k; |
| 2870 } |
| 2871 else { |
| 2872 b2 -= k; |
| 2873 b5 = -k; |
| 2874 s5 = 0; |
| 2875 } |
| 2876 if (mode < 0 || mode > 9) |
| 2877 mode = 0; |
| 2878 |
| 2879 #ifndef SET_INEXACT |
| 2880 #ifdef Check_FLT_ROUNDS |
| 2881 try_quick = Rounding == 1; |
| 2882 #else |
| 2883 try_quick = 1; |
| 2884 #endif |
| 2885 #endif /*SET_INEXACT*/ |
| 2886 |
| 2887 if (mode > 5) { |
| 2888 mode -= 4; |
| 2889 try_quick = 0; |
| 2890 } |
| 2891 leftright = 1; |
| 2892 switch(mode) { |
| 2893 case 0: |
| 2894 case 1: |
| 2895 ilim = ilim1 = -1; |
| 2896 i = 18; |
| 2897 ndigits = 0; |
| 2898 break; |
| 2899 case 2: |
| 2900 leftright = 0; |
| 2901 /* no break */ |
| 2902 case 4: |
| 2903 if (ndigits <= 0) |
| 2904 ndigits = 1; |
| 2905 ilim = ilim1 = i = ndigits; |
| 2906 break; |
| 2907 case 3: |
| 2908 leftright = 0; |
| 2909 /* no break */ |
| 2910 case 5: |
| 2911 i = ndigits + k + 1; |
| 2912 ilim = i; |
| 2913 ilim1 = i - 1; |
| 2914 if (i <= 0) |
| 2915 i = 1; |
| 2916 } |
| 2917 s = s0 = rv_alloc(i); |
| 2918 |
| 2919 #ifdef Honor_FLT_ROUNDS |
| 2920 if (mode > 1 && Rounding != 1) |
| 2921 leftright = 0; |
| 2922 #endif |
| 2923 |
| 2924 if (ilim >= 0 && ilim <= Quick_max && try_quick) { |
| 2925 |
| 2926 /* Try to get by with floating-point arithmetic. */ |
| 2927 |
| 2928 i = 0; |
| 2929 dval(d2) = dval(d); |
| 2930 k0 = k; |
| 2931 ilim0 = ilim; |
| 2932 ieps = 2; /* conservative */ |
| 2933 if (k > 0) { |
| 2934 ds = tens[k&0xf]; |
| 2935 j = k >> 4; |
| 2936 if (j & Bletch) { |
| 2937 /* prevent overflows */ |
| 2938 j &= Bletch - 1; |
| 2939 dval(d) /= bigtens[n_bigtens-1]; |
| 2940 ieps++; |
| 2941 } |
| 2942 for(; j; j >>= 1, i++) |
| 2943 if (j & 1) { |
| 2944 ieps++; |
| 2945 ds *= bigtens[i]; |
| 2946 } |
| 2947 dval(d) /= ds; |
| 2948 } |
| 2949 else if (j1 = -k) { |
| 2950 dval(d) *= tens[j1 & 0xf]; |
| 2951 for(j = j1 >> 4; j; j >>= 1, i++) |
| 2952 if (j & 1) { |
| 2953 ieps++; |
| 2954 dval(d) *= bigtens[i]; |
| 2955 } |
| 2956 } |
| 2957 if (k_check && dval(d) < 1. && ilim > 0) { |
| 2958 if (ilim1 <= 0) |
| 2959 goto fast_failed; |
| 2960 ilim = ilim1; |
| 2961 k--; |
| 2962 dval(d) *= 10.; |
| 2963 ieps++; |
| 2964 } |
| 2965 dval(eps) = ieps*dval(d) + 7.; |
| 2966 word0(eps) -= (P-1)*Exp_msk1; |
| 2967 if (ilim == 0) { |
| 2968 S = mhi = 0; |
| 2969 dval(d) -= 5.; |
| 2970 if (dval(d) > dval(eps)) |
| 2971 goto one_digit; |
| 2972 if (dval(d) < -dval(eps)) |
| 2973 goto no_digits; |
| 2974 goto fast_failed; |
| 2975 } |
| 2976 #ifndef No_leftright |
| 2977 if (leftright) { |
| 2978 /* Use Steele & White method of only |
| 2979 * generating digits needed. |
| 2980 */ |
| 2981 dval(eps) = 0.5/tens[ilim-1] - dval(eps); |
| 2982 for(i = 0;;) { |
| 2983 L = dval(d); |
| 2984 dval(d) -= L; |
| 2985 *s++ = '0' + (int)L; |
| 2986 if (dval(d) < dval(eps)) |
| 2987 goto ret1; |
| 2988 if (1. - dval(d) < dval(eps)) |
| 2989 goto bump_up; |
| 2990 if (++i >= ilim) |
| 2991 break; |
| 2992 dval(eps) *= 10.; |
| 2993 dval(d) *= 10.; |
| 2994 } |
| 2995 } |
| 2996 else { |
| 2997 #endif |
| 2998 /* Generate ilim digits, then fix them up. */ |
| 2999 dval(eps) *= tens[ilim-1]; |
| 3000 for(i = 1;; i++, dval(d) *= 10.) { |
| 3001 L = (Long)(dval(d)); |
| 3002 if (!(dval(d) -= L)) |
| 3003 ilim = i; |
| 3004 *s++ = '0' + (int)L; |
| 3005 if (i == ilim) { |
| 3006 if (dval(d) > 0.5 + dval(eps)) |
| 3007 goto bump_up; |
| 3008 else if (dval(d) < 0.5 - dval(eps)) { |
| 3009 while(*--s == '0'); |
| 3010 s++; |
| 3011 goto ret1; |
| 3012 } |
| 3013 break; |
| 3014 } |
| 3015 } |
| 3016 #ifndef No_leftright |
| 3017 } |
| 3018 #endif |
| 3019 fast_failed: |
| 3020 s = s0; |
| 3021 dval(d) = dval(d2); |
| 3022 k = k0; |
| 3023 ilim = ilim0; |
| 3024 } |
| 3025 |
| 3026 /* Do we have a "small" integer? */ |
| 3027 |
| 3028 if (be >= 0 && k <= Int_max) { |
| 3029 /* Yes. */ |
| 3030 ds = tens[k]; |
| 3031 if (ndigits < 0 && ilim <= 0) { |
| 3032 S = mhi = 0; |
| 3033 if (ilim < 0 || dval(d) <= 5*ds) |
| 3034 goto no_digits; |
| 3035 goto one_digit; |
| 3036 } |
| 3037 for(i = 1;; i++, dval(d) *= 10.) { |
| 3038 L = (Long)(dval(d) / ds); |
| 3039 dval(d) -= L*ds; |
| 3040 #ifdef Check_FLT_ROUNDS |
| 3041 /* If FLT_ROUNDS == 2, L will usually be high by 1 */ |
| 3042 if (dval(d) < 0) { |
| 3043 L--; |
| 3044 dval(d) += ds; |
| 3045 } |
| 3046 #endif |
| 3047 *s++ = '0' + (int)L; |
| 3048 if (!dval(d)) { |
| 3049 #ifdef SET_INEXACT |
| 3050 inexact = 0; |
| 3051 #endif |
| 3052 break; |
| 3053 } |
| 3054 if (i == ilim) { |
| 3055 #ifdef Honor_FLT_ROUNDS |
| 3056 if (mode > 1) |
| 3057 switch(Rounding) { |
| 3058 case 0: goto ret1; |
| 3059 case 2: goto bump_up; |
| 3060 } |
| 3061 #endif |
| 3062 dval(d) += dval(d); |
| 3063 if (dval(d) > ds || dval(d) == ds && L & 1) { |
| 3064 bump_up: |
| 3065 while(*--s == '9') |
| 3066 if (s == s0) { |
| 3067 k++; |
| 3068 *s = '0'; |
| 3069 break; |
| 3070 } |
| 3071 ++*s++; |
| 3072 } |
| 3073 break; |
| 3074 } |
| 3075 } |
| 3076 goto ret1; |
| 3077 } |
| 3078 |
| 3079 m2 = b2; |
| 3080 m5 = b5; |
| 3081 mhi = mlo = 0; |
| 3082 if (leftright) { |
| 3083 i = |
| 3084 #ifndef Sudden_Underflow |
| 3085 denorm ? be + (Bias + (P-1) - 1 + 1) : |
| 3086 #endif |
| 3087 #ifdef IBM |
| 3088 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); |
| 3089 #else |
| 3090 1 + P - bbits; |
| 3091 #endif |
| 3092 b2 += i; |
| 3093 s2 += i; |
| 3094 mhi = i2b(1); |
| 3095 } |
| 3096 if (m2 > 0 && s2 > 0) { |
| 3097 i = m2 < s2 ? m2 : s2; |
| 3098 b2 -= i; |
| 3099 m2 -= i; |
| 3100 s2 -= i; |
| 3101 } |
| 3102 if (b5 > 0) { |
| 3103 if (leftright) { |
| 3104 if (m5 > 0) { |
| 3105 mhi = pow5mult(mhi, m5); |
| 3106 b1 = mult(mhi, b); |
| 3107 Bfree(b); |
| 3108 b = b1; |
| 3109 } |
| 3110 if (j = b5 - m5) |
| 3111 b = pow5mult(b, j); |
| 3112 } |
| 3113 else |
| 3114 b = pow5mult(b, b5); |
| 3115 } |
| 3116 S = i2b(1); |
| 3117 if (s5 > 0) |
| 3118 S = pow5mult(S, s5); |
| 3119 |
| 3120 /* Check for special case that d is a normalized power of 2. */ |
| 3121 |
| 3122 spec_case = 0; |
| 3123 if ((mode < 2 || leftright) |
| 3124 #ifdef Honor_FLT_ROUNDS |
| 3125 && Rounding == 1 |
| 3126 #endif |
| 3127 ) { |
| 3128 if (!word1(d) && !(word0(d) & Bndry_mask) |
| 3129 #ifndef Sudden_Underflow |
| 3130 && word0(d) & (Exp_mask & ~Exp_msk1) |
| 3131 #endif |
| 3132 ) { |
| 3133 /* The special case */ |
| 3134 b2 += Log2P; |
| 3135 s2 += Log2P; |
| 3136 spec_case = 1; |
| 3137 } |
| 3138 } |
| 3139 |
| 3140 /* Arrange for convenient computation of quotients: |
| 3141 * shift left if necessary so divisor has 4 leading 0 bits. |
| 3142 * |
| 3143 * Perhaps we should just compute leading 28 bits of S once |
| 3144 * and for all and pass them and a shift to quorem, so it |
| 3145 * can do shifts and ors to compute the numerator for q. |
| 3146 */ |
| 3147 #ifdef Pack_32 |
| 3148 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) |
| 3149 i = 32 - i; |
| 3150 #else |
| 3151 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) |
| 3152 i = 16 - i; |
| 3153 #endif |
| 3154 if (i > 4) { |
| 3155 i -= 4; |
| 3156 b2 += i; |
| 3157 m2 += i; |
| 3158 s2 += i; |
| 3159 } |
| 3160 else if (i < 4) { |
| 3161 i += 28; |
| 3162 b2 += i; |
| 3163 m2 += i; |
| 3164 s2 += i; |
| 3165 } |
| 3166 if (b2 > 0) |
| 3167 b = lshift(b, b2); |
| 3168 if (s2 > 0) |
| 3169 S = lshift(S, s2); |
| 3170 if (k_check) { |
| 3171 if (cmp(b,S) < 0) { |
| 3172 k--; |
| 3173 b = multadd(b, 10, 0); /* we botched the k estimate */ |
| 3174 if (leftright) |
| 3175 mhi = multadd(mhi, 10, 0); |
| 3176 ilim = ilim1; |
| 3177 } |
| 3178 } |
| 3179 if (ilim <= 0 && (mode == 3 || mode == 5)) { |
| 3180 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { |
| 3181 /* no digits, fcvt style */ |
| 3182 no_digits: |
| 3183 k = -1 - ndigits; |
| 3184 goto ret; |
| 3185 } |
| 3186 one_digit: |
| 3187 *s++ = '1'; |
| 3188 k++; |
| 3189 goto ret; |
| 3190 } |
| 3191 if (leftright) { |
| 3192 if (m2 > 0) |
| 3193 mhi = lshift(mhi, m2); |
| 3194 |
| 3195 /* Compute mlo -- check for special case |
| 3196 * that d is a normalized power of 2. |
| 3197 */ |
| 3198 |
| 3199 mlo = mhi; |
| 3200 if (spec_case) { |
| 3201 mhi = Balloc(mhi->k); |
| 3202 Bcopy(mhi, mlo); |
| 3203 mhi = lshift(mhi, Log2P); |
| 3204 } |
| 3205 |
| 3206 for(i = 1;;i++) { |
| 3207 dig = quorem(b,S) + '0'; |
| 3208 /* Do we yet have the shortest decimal string |
| 3209 * that will round to d? |
| 3210 */ |
| 3211 j = cmp(b, mlo); |
| 3212 delta = diff(S, mhi); |
| 3213 j1 = delta->sign ? 1 : cmp(b, delta); |
| 3214 Bfree(delta); |
| 3215 #ifndef ROUND_BIASED |
| 3216 if (j1 == 0 && mode != 1 && !(word1(d) & 1) |
| 3217 #ifdef Honor_FLT_ROUNDS |
| 3218 && Rounding >= 1 |
| 3219 #endif |
| 3220 ) { |
| 3221 if (dig == '9') |
| 3222 goto round_9_up; |
| 3223 if (j > 0) |
| 3224 dig++; |
| 3225 #ifdef SET_INEXACT |
| 3226 else if (!b->x[0] && b->wds <= 1) |
| 3227 inexact = 0; |
| 3228 #endif |
| 3229 *s++ = dig; |
| 3230 goto ret; |
| 3231 } |
| 3232 #endif |
| 3233 if (j < 0 || j == 0 && mode != 1 |
| 3234 #ifndef ROUND_BIASED |
| 3235 && !(word1(d) & 1) |
| 3236 #endif |
| 3237 ) { |
| 3238 if (!b->x[0] && b->wds <= 1) { |
| 3239 #ifdef SET_INEXACT |
| 3240 inexact = 0; |
| 3241 #endif |
| 3242 goto accept_dig; |
| 3243 } |
| 3244 #ifdef Honor_FLT_ROUNDS |
| 3245 if (mode > 1) |
| 3246 switch(Rounding) { |
| 3247 case 0: goto accept_dig; |
| 3248 case 2: goto keep_dig; |
| 3249 } |
| 3250 #endif /*Honor_FLT_ROUNDS*/ |
| 3251 if (j1 > 0) { |
| 3252 b = lshift(b, 1); |
| 3253 j1 = cmp(b, S); |
| 3254 if ((j1 > 0 || j1 == 0 && dig & 1) |
| 3255 && dig++ == '9') |
| 3256 goto round_9_up; |
| 3257 } |
| 3258 accept_dig: |
| 3259 *s++ = dig; |
| 3260 goto ret; |
| 3261 } |
| 3262 if (j1 > 0) { |
| 3263 #ifdef Honor_FLT_ROUNDS |
| 3264 if (!Rounding) |
| 3265 goto accept_dig; |
| 3266 #endif |
| 3267 if (dig == '9') { /* possible if i == 1 */ |
| 3268 round_9_up: |
| 3269 *s++ = '9'; |
| 3270 goto roundoff; |
| 3271 } |
| 3272 *s++ = dig + 1; |
| 3273 goto ret; |
| 3274 } |
| 3275 #ifdef Honor_FLT_ROUNDS |
| 3276 keep_dig: |
| 3277 #endif |
| 3278 *s++ = dig; |
| 3279 if (i == ilim) |
| 3280 break; |
| 3281 b = multadd(b, 10, 0); |
| 3282 if (mlo == mhi) |
| 3283 mlo = mhi = multadd(mhi, 10, 0); |
| 3284 else { |
| 3285 mlo = multadd(mlo, 10, 0); |
| 3286 mhi = multadd(mhi, 10, 0); |
| 3287 } |
| 3288 } |
| 3289 } |
| 3290 else |
| 3291 for(i = 1;; i++) { |
| 3292 *s++ = dig = quorem(b,S) + '0'; |
| 3293 if (!b->x[0] && b->wds <= 1) { |
| 3294 #ifdef SET_INEXACT |
| 3295 inexact = 0; |
| 3296 #endif |
| 3297 goto ret; |
| 3298 } |
| 3299 if (i >= ilim) |
| 3300 break; |
| 3301 b = multadd(b, 10, 0); |
| 3302 } |
| 3303 |
| 3304 /* Round off last digit */ |
| 3305 |
| 3306 #ifdef Honor_FLT_ROUNDS |
| 3307 switch(Rounding) { |
| 3308 case 0: goto trimzeros; |
| 3309 case 2: goto roundoff; |
| 3310 } |
| 3311 #endif |
| 3312 b = lshift(b, 1); |
| 3313 j = cmp(b, S); |
| 3314 if (j > 0 || j == 0 && dig & 1) { |
| 3315 roundoff: |
| 3316 while(*--s == '9') |
| 3317 if (s == s0) { |
| 3318 k++; |
| 3319 *s++ = '1'; |
| 3320 goto ret; |
| 3321 } |
| 3322 ++*s++; |
| 3323 } |
| 3324 else { |
| 3325 trimzeros: |
| 3326 while(*--s == '0'); |
| 3327 s++; |
| 3328 } |
| 3329 ret: |
| 3330 Bfree(S); |
| 3331 if (mhi) { |
| 3332 if (mlo && mlo != mhi) |
| 3333 Bfree(mlo); |
| 3334 Bfree(mhi); |
| 3335 } |
| 3336 ret1: |
| 3337 #ifdef SET_INEXACT |
| 3338 if (inexact) { |
| 3339 if (!oldinexact) { |
| 3340 word0(d) = Exp_1 + (70 << Exp_shift); |
| 3341 word1(d) = 0; |
| 3342 dval(d) += 1.; |
| 3343 } |
| 3344 } |
| 3345 else if (!oldinexact) |
| 3346 clear_inexact(); |
| 3347 #endif |
| 3348 Bfree(b); |
| 3349 *s = 0; |
| 3350 *decpt = k + 1; |
| 3351 if (rve) |
| 3352 *rve = s; |
| 3353 return s0; |
| 3354 } |
| 3355 |
| 3356 } // namespace dmg_fp |
OLD | NEW |