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