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