Skip to content
Myr

jv_dtoa.c

This file

Path

src/jv_dtoa.c

4,276 lines · 86.7 KiB · C

Last changed by

Fix whitespace in number parsing (#3195)Thalia Archibaldbefore adoption9ac6dda

History of this file

Take it

The walk stopped after 200 commits: lines older than fa6a2ff point at it.

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1–2370why, one step back
1
2/****************************************************************3 *4 * The author of this software is David M. Gay.5 *6 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.7 *8 * Permission to use, copy, modify, and distribute this software for any9 * purpose without fee is hereby granted, provided that this entire notice10 * is included in all copies of any software which is or includes a copy11 * or modification of this software and in all copies of the supporting12 * documentation for such software.13 *14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED15 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY17 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.18 *19 ***************************************************************/2021/* Please send bug reports to David M. Gay (dmg at acm dot org,22 * with " at " changed at "@" and " dot " changed to "."). */2324/* On a machine with IEEE extended-precision registers, it is25 * necessary to specify double-precision (53-bit) rounding precision26 * before invoking strtod or dtoa. If the machine uses (the equivalent27 * of) Intel 80x87 arithmetic, the call28 * _control87(PC_53, MCW_PC);29 * does this with many compilers. Whether this or another call is30 * appropriate depends on the compiler; for this to work, it may be31 * necessary to #include "float.h" or another system-dependent header32 * file.33 */3435/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.36 * (Note that IEEE arithmetic is disabled by gcc's -ffast-math flag.)37 *38 * This strtod returns a nearest machine number to the input decimal39 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are40 * broken by the IEEE round-even rule. Otherwise ties are broken by41 * biased rounding (add half and chop).42 *43 * Inspired loosely by William D. Clinger's paper "How to Read Floating44 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].45 *46 * Modifications:47 *48 * 1. We only require IEEE, IBM, or VAX double-precision49 * arithmetic (not IEEE double-extended).50 * 2. We get by with floating-point arithmetic in a case that51 * Clinger missed -- when we're computing d * 10^n52 * for a small integer d and the integer n is not too53 * much larger than 22 (the maximum integer k for which54 * we can represent 10^k exactly), we may be able to55 * compute (d*10^k) * 10^(e-k) with just one roundoff.56 * 3. Rather than a bit-at-a-time adjustment of the binary57 * result in the hard case, we use floating-point58 * arithmetic to determine the adjustment to within59 * one bit; only in really hard cases do we need to60 * compute a second residual.61 * 4. Because of 3., we don't need a large table of powers of 1062 * for ten-to-e (just some small tables, e.g. of 10^k63 * for 0 <= k <= 22).64 */6566/*67 * #define IEEE_8087 for IEEE-arithmetic machines where the least68 * significant byte has the lowest address.69 * #define IEEE_MC68k for IEEE-arithmetic machines where the most70 * significant byte has the lowest address.71 * #define Long int on machines with 32-bit ints and 64-bit longs.72 * #define IBM for IBM mainframe-style floating-point arithmetic.73 * #define VAX for VAX-style floating-point arithmetic (D_floating).74 * #define No_leftright to omit left-right logic in fast floating-point75 * computation of dtoa. This will cause dtoa modes 4 and 5 to be76 * treated the same as modes 2 and 3 for some inputs.77 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 378 * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS79 * is also #defined, fegetround() will be queried for the rounding mode.80 * Note that both FLT_ROUNDS and fegetround() are specified by the C9981 * standard (and are specified to be consistent, with fesetround()82 * affecting the value of FLT_ROUNDS), but that some (Linux) systems83 * do not work correctly in this regard, so using fegetround() is more84 * portable than using FLT_ROUNDS directly.85 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 386 * and Honor_FLT_ROUNDS is not #defined.87 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines88 * that use extended-precision instructions to compute rounded89 * products and quotients) with IBM.90 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic91 * that rounds toward +Infinity.92 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased93 * rounding when the underlying floating-point arithmetic uses94 * unbiased rounding. This prevent using ordinary floating-point95 * arithmetic when the result could be computed with one rounding error.96 * #define Inaccurate_Divide for IEEE-format with correctly rounded97 * products but inaccurate quotients, e.g., for Intel i860.98 * #define NO_LONG_LONG on machines that do not have a "long long"99 * integer type (of >= 64 bits). On such machines, you can100 * #define Just_16 to store 16 bits per 32-bit Long when doing101 * high-precision integer arithmetic. Whether this speeds things102 * up or slows things down depends on the machine and the number103 * being converted. If long long is available and the name is104 * something other than "long long", #define Llong to be the name,105 * and if "unsigned Llong" does not work as an unsigned version of106 * Llong, #define #ULLong to be the corresponding unsigned type.107 * #define KR_headers for old-style C function headers.108 * #define Bad_float_h if your system lacks a float.h or if it does not109 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,110 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.111 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)112 * if memory is available and otherwise does something you deem113 * appropriate. If MALLOC is undefined, malloc will be invoked114 * directly -- and assumed always to succeed. Similarly, if you115 * want something other than the system's free() to be called to116 * recycle memory acquired from MALLOC, #define FREE to be the117 * name of the alternate routine. (FREE or free is only called in118 * pathological cases, e.g., in a dtoa call after a dtoa return in119 * mode 3 with thousands of digits requested.)120 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making121 * memory allocations from a private pool of memory when possible.122 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,123 * unless #defined to be a different length. This default length124 * suffices to get rid of MALLOC calls except for unusual cases,125 * such as decimal-to-binary conversion of a very long string of126 * digits. The longest string dtoa can return is about 751 bytes127 * long. For conversions by strtod of strings of 800 digits and128 * all dtoa conversions in single-threaded executions with 8-byte129 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte130 * pointers, PRIVATE_MEM >= 7112 appears adequate.131 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK132 * #defined automatically on IEEE systems. On such systems,133 * when INFNAN_CHECK is #defined, strtod checks134 * for Infinity and NaN (case insensitively). On some systems135 * (e.g., some HP systems), it may be necessary to #define NAN_WORD0136 * appropriately -- to the most significant word of a quiet NaN.137 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)138 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,139 * strtod also accepts (case insensitively) strings of the form140 * NaN(x), where x is a string of hexadecimal digits and spaces;141 * if there is only one string of hexadecimal digits, it is taken142 * for the 52 fraction bits of the resulting NaN; if there are two143 * or more strings of hex digits, the first is for the high 20 bits,144 * the second and subsequent for the low 32 bits, with intervening145 * white space ignored; but if this results in none of the 52146 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0147 * and NAN_WORD1 are used instead.148 * #define MULTIPLE_THREADS if the system offers preemptively scheduled149 * multiple threads. In this case, you must provide (or suitably150 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed151 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed152 * in pow5mult, ensures lazy evaluation of only one copy of high153 * powers of 5; omitting this lock would introduce a small154 * probability of wasting memory, but would otherwise be harmless.)155 * You must also invoke freedtoa(s) to free the value s returned by156 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.157 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that158 * avoids underflows on inputs whose result does not underflow.159 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format160 * floating-point numbers and flushes underflows to zero rather161 * than implementing gradual underflow, then you must also #define162 * Sudden_Underflow.163 * #define USE_LOCALE to use the current locale's decimal_point value.164 * #define SET_INEXACT if IEEE arithmetic is being used and extra165 * computation should be done to set the inexact flag when the166 * result is inexact and avoid setting inexact when the result167 * is exact. In this case, dtoa.c must be compiled in168 * an environment, perhaps provided by #include "dtoa.c" in a169 * suitable wrapper, that defines two functions,170 * int get_inexact(void);171 * void clear_inexact(void);172 * such that get_inexact() returns a nonzero value if the173 * inexact bit is already set, and clear_inexact() sets the174 * inexact bit to 0. When SET_INEXACT is #defined, strtod175 * also does extra computations to set the underflow and overflow176 * flags when appropriate (i.e., when the result is tiny and177 * inexact or when it is a numeric value rounded to +-infinity).178 * #define NO_ERRNO if strtod should not assign errno = ERANGE when179 * the result overflows to +-Infinity or underflows to 0.180 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point181 * values by strtod.182 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)183 * to disable logic for "fast" testing of very long input strings184 * to strtod. This testing proceeds by initially truncating the185 * input string, then if necessary comparing the whole string with186 * a decimal expansion to decide close cases. This logic is only187 * used for input more than STRTOD_DIGLIM digits long (default 40).188 */189190#define NO_ERRNO191#define NO_HEX_FP192#define No_Hex_NaN193#define Long int194195#include "jv_dtoa.h"196197#include "jv_alloc.h"198#include "jv.h"199#define MALLOC jv_mem_alloc200#define FREE jv_mem_free201202203204#ifndef Long205#define Long long206#endif207#ifndef ULong208typedef unsigned Long ULong;209#endif210211#ifdef DEBUG212#include "stdio.h"213#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}214#endif215216#include "stdlib.h"217#include "string.h"218219#ifdef USE_LOCALE220#include "locale.h"221#endif222223#ifdef Honor_FLT_ROUNDS224#ifndef Trust_FLT_ROUNDS225#include <fenv.h>226#endif227#endif228229#ifdef MALLOC230extern void *MALLOC(size_t);231#else232#define MALLOC malloc233#endif234235#undef IEEE_Arith236#undef Avoid_Underflow237#ifdef IEEE_MC68k238#define IEEE_Arith239#endif240#ifdef IEEE_8087241#define IEEE_Arith242#endif243244#ifdef IEEE_Arith245#ifndef NO_INFNAN_CHECK246#undef INFNAN_CHECK247#define INFNAN_CHECK248#endif249#else250#undef INFNAN_CHECK251#define NO_STRTOD_BIGCOMP252#endif253254#include "errno.h"255256#ifdef Bad_float_h257258#ifdef IEEE_Arith259#define DBL_DIG 15260#define DBL_MAX_10_EXP 308261#define DBL_MAX_EXP 1024262#define FLT_RADIX 2263#endif /*IEEE_Arith*/264265#ifdef IBM266#define DBL_DIG 16267#define DBL_MAX_10_EXP 75268#define DBL_MAX_EXP 63269#define FLT_RADIX 16270#define DBL_MAX 7.2370055773322621e+75271#endif272273#ifdef VAX274#define DBL_DIG 16275#define DBL_MAX_10_EXP 38276#define DBL_MAX_EXP 127277#define FLT_RADIX 2278#define DBL_MAX 1.7014118346046923e+38279#endif280281#ifndef LONG_MAX282#define LONG_MAX 2147483647283#endif284285#else /* ifndef Bad_float_h */286#include "float.h"287#endif /* Bad_float_h */288289#ifndef __MATH_H__290#include "math.h"291#endif292293#ifdef __cplusplus294extern "C" {295#endif296297#ifndef CONST298#define CONST const299#endif300301#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1302# error Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.303#endif304305typedef union { double d; ULong L[2]; } U;306307#ifdef IEEE_8087308#define word0(x) (x)->L[1]309#define word1(x) (x)->L[0]310#else311#define word0(x) (x)->L[0]312#define word1(x) (x)->L[1]313#endif314#define dval(x) (x)->d315316#ifndef STRTOD_DIGLIM317#define STRTOD_DIGLIM 40318#endif319320#ifdef DIGLIM_DEBUG321extern int strtod_diglim;322#else323#define strtod_diglim STRTOD_DIGLIM324#endif325326/* The following definition of Storeinc is appropriate for MIPS processors.327 * An alternative that might be better on some machines is328 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)329 */330#if defined(IEEE_8087) + defined(VAX)331#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \332((unsigned short *)a)[0] = (unsigned short)c, a++)333#else334#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \335((unsigned short *)a)[1] = (unsigned short)c, a++)336#endif337338/* #define P DBL_MANT_DIG */339/* Ten_pmax = floor(P*log(2)/log(5)) */340/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */341/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */342/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */343344#ifdef IEEE_Arith345#define Exp_shift 20346#define Exp_shift1 20347#define Exp_msk1 0x100000348#define Exp_msk11 0x100000349#define Exp_mask 0x7ff00000350#define P 53351#define Nbits 53352#define Bias 1023353#define Emax 1023354#define Emin (-1022)355#define Exp_1 0x3ff00000356#define Exp_11 0x3ff00000357#define Ebits 11358#define Frac_mask 0xfffff359#define Frac_mask1 0xfffff360#define Ten_pmax 22361#define Bletch 0x10362#define Bndry_mask 0xfffff363#define Bndry_mask1 0xfffff364#define LSB 1365#define Sign_bit 0x80000000366#define Log2P 1367#define Tiny0 0368#define Tiny1 1369#define Quick_max 14370#define Int_max 14371#ifndef NO_IEEE_Scale372#define Avoid_Underflow373#ifdef Flush_Denorm /* debugging option */374#undef Sudden_Underflow375#endif376#endif377378#ifndef Flt_Rounds379#ifdef FLT_ROUNDS380#define Flt_Rounds FLT_ROUNDS381#else382#define Flt_Rounds 1383#endif384#endif /*Flt_Rounds*/385386#ifdef Honor_FLT_ROUNDS387#undef Check_FLT_ROUNDS388#define Check_FLT_ROUNDS389#else390#define Rounding Flt_Rounds391#endif392393#else /* ifndef IEEE_Arith */394#undef Check_FLT_ROUNDS395#undef Honor_FLT_ROUNDS396#undef SET_INEXACT397#undef Sudden_Underflow398#define Sudden_Underflow399#ifdef IBM400#undef Flt_Rounds401#define Flt_Rounds 0402#define Exp_shift 24403#define Exp_shift1 24404#define Exp_msk1 0x1000000405#define Exp_msk11 0x1000000406#define Exp_mask 0x7f000000407#define P 14408#define Nbits 56409#define Bias 65410#define Emax 248411#define Emin (-260)412#define Exp_1 0x41000000413#define Exp_11 0x41000000414#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */415#define Frac_mask 0xffffff416#define Frac_mask1 0xffffff417#define Bletch 4418#define Ten_pmax 22419#define Bndry_mask 0xefffff420#define Bndry_mask1 0xffffff421#define LSB 1422#define Sign_bit 0x80000000423#define Log2P 4424#define Tiny0 0x100000425#define Tiny1 0426#define Quick_max 14427#define Int_max 15428#else /* VAX */429#undef Flt_Rounds430#define Flt_Rounds 1431#define Exp_shift 23432#define Exp_shift1 7433#define Exp_msk1 0x80434#define Exp_msk11 0x800000435#define Exp_mask 0x7f80436#define P 56437#define Nbits 56438#define Bias 129439#define Emax 126440#define Emin (-129)441#define Exp_1 0x40800000442#define Exp_11 0x4080443#define Ebits 8444#define Frac_mask 0x7fffff445#define Frac_mask1 0xffff007f446#define Ten_pmax 24447#define Bletch 2448#define Bndry_mask 0xffff007f449#define Bndry_mask1 0xffff007f450#define LSB 0x10000451#define Sign_bit 0x8000452#define Log2P 1453#define Tiny0 0x80454#define Tiny1 0455#define Quick_max 15456#define Int_max 15457#endif /* IBM, VAX */458#endif /* IEEE_Arith */459460#ifndef IEEE_Arith461#define ROUND_BIASED462#else463#ifdef ROUND_BIASED_without_Round_Up464#undef ROUND_BIASED465#define ROUND_BIASED466#endif467#endif468469#ifdef RND_PRODQUOT470#define rounded_product(a,b) a = rnd_prod(a, b)471#define rounded_quotient(a,b) a = rnd_quot(a, b)472extern double rnd_prod(double, double), rnd_quot(double, double);473#else474#define rounded_product(a,b) a *= b475#define rounded_quotient(a,b) a /= b476#endif477478#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))479#define Big1 0xffffffff480481#ifndef Pack_32482#define Pack_32483#endif484485typedef struct BCinfo BCinfo;486 struct487BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; };488489#define FFFFFFFF 0xffffffffUL490491#ifdef NO_LONG_LONG492#undef ULLong493#ifdef Just_16494#undef Pack_32495/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.496 * This makes some inner loops simpler and sometimes saves work497 * during multiplications, but it often seems to make things slightly498 * slower. Hence the default is now to store 32 bits per Long.499 */500#endif501#else /* long long available */502#ifndef Llong503#define Llong long long504#endif505#ifndef ULLong506#define ULLong unsigned Llong507#endif508#endif /* NO_LONG_LONG */509510511512 struct513Bigint {514 struct Bigint *next;515 int k, maxwds, sign, wds;516 ULong x[1];517 };518519 typedef struct Bigint Bigint;520521522void jvp_dtoa_context_init(struct dtoa_context* C) {523 int i;524 for (i=0; i<(int)(sizeof(C->freelist)/sizeof(C->freelist[0])); i++) {525 C->freelist[i] = 0;526 }527 C->p5s = 0;528}529530 static Bigint *531 Balloc(struct dtoa_context* C, int k)532{533 int x;534 Bigint *rv;535536 /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */537 /* but this case seems very unlikely. */538 if (k <= Kmax && (rv = C->freelist[k]))539 C->freelist[k] = rv->next;540 else {541 x = 1 << k;542 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));543 rv->k = k;544 rv->maxwds = x;545 }546 rv->sign = rv->wds = 0;547 return rv;548 }549550 static void551Bfree552 (struct dtoa_context* C, Bigint *v)553{554 if (v) {555 if (v->k > Kmax)556#ifdef FREE557 FREE((void*)v);558#else559 free((void*)v);560#endif561 else {562 v->next = C->freelist[v->k];563 C->freelist[v->k] = v;564 }565 }566 }567568569void jvp_dtoa_context_free(struct dtoa_context* C) {570 int k;571 while (C->p5s) {572 Bigint* p5 = C->p5s;573 C->p5s = p5->next;574 Bfree(C, p5);575 }576 for (k=0; k<(int)(sizeof(C->freelist)/sizeof(C->freelist[0])); k++) {577 while (C->freelist[k]) {578 Bigint* v = C->freelist[k];579 C->freelist[k] = v->next;580 FREE(v);581 }582 }583}584585586#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \587y->wds*sizeof(Long) + 2*sizeof(int))588589 static Bigint *590multadd591 (struct dtoa_context* C, Bigint *b, int m, int a) /* multiply by m and add a */592{593 int i, wds;594#ifdef ULLong595 ULong *x;596 ULLong carry, y;597#else598 ULong carry, *x, y;599#ifdef Pack_32600 ULong xi, z;601#endif602#endif603 Bigint *b1;604605 wds = b->wds;606 x = b->x;607 i = 0;608 carry = a;609 do {610#ifdef ULLong611 y = *x * (ULLong)m + carry;612 carry = y >> 32;613 *x++ = y & FFFFFFFF;614#else615#ifdef Pack_32616 xi = *x;617 y = (xi & 0xffff) * m + carry;618 z = (xi >> 16) * m + (y >> 16);619 carry = z >> 16;620 *x++ = (z << 16) + (y & 0xffff);621#else622 y = *x * m + carry;623 carry = y >> 16;624 *x++ = y & 0xffff;625#endif626#endif627 }628 while(++i < wds);629 if (carry) {630 if (wds >= b->maxwds) {631 b1 = Balloc(C, b->k+1);632 Bcopy(b1, b);633 Bfree(C, b);634 b = b1;635 }636 b->x[wds++] = carry;637 b->wds = wds;638 }639 return b;640 }641642 static Bigint *643s2b644 (struct dtoa_context* C, const char *s, int nd0, int nd, ULong y9, int dplen)645{646 Bigint *b;647 int i, k;648 Long x, y;649650 x = (nd + 8) / 9;651 for(k = 0, y = 1; x > y; y <<= 1, k++) ;652#ifdef Pack_32653 b = Balloc(C, k);654 b->x[0] = y9;655 b->wds = 1;656#else657 b = Balloc(C, k+1);658 b->x[0] = y9 & 0xffff;659 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;660#endif661662 i = 9;663 if (9 < nd0) {664 s += 9;665 do b = multadd(C, b, 10, *s++ - '0');666 while(++i < nd0);667 s += dplen;668 }669 else670 s += dplen + 9;671 for(; i < nd; i++)672 b = multadd(C, b, 10, *s++ - '0');673 return b;674 }675676 static int677hi0bits678 (struct dtoa_context* C, ULong x)679{680 int k = 0;681682 if (!(x & 0xffff0000)) {683 k = 16;684 x <<= 16;685 }686 if (!(x & 0xff000000)) {687 k += 8;688 x <<= 8;689 }690 if (!(x & 0xf0000000)) {691 k += 4;692 x <<= 4;693 }694 if (!(x & 0xc0000000)) {695 k += 2;696 x <<= 2;697 }698 if (!(x & 0x80000000)) {699 k++;700 if (!(x & 0x40000000))701 return 32;702 }703 return k;704 }705706 static int707lo0bits708 (struct dtoa_context* C, ULong *y)709{710 int k;711 ULong x = *y;712713 if (x & 7) {714 if (x & 1)715 return 0;716 if (x & 2) {717 *y = x >> 1;718 return 1;719 }720 *y = x >> 2;721 return 2;722 }723 k = 0;724 if (!(x & 0xffff)) {725 k = 16;726 x >>= 16;727 }728 if (!(x & 0xff)) {729 k += 8;730 x >>= 8;731 }732 if (!(x & 0xf)) {733 k += 4;734 x >>= 4;735 }736 if (!(x & 0x3)) {737 k += 2;738 x >>= 2;739 }740 if (!(x & 1)) {741 k++;742 x >>= 1;743 if (!x)744 return 32;745 }746 *y = x;747 return k;748 }749750 static Bigint *751i2b752 (struct dtoa_context* C, int i)753{754 Bigint *b;755756 b = Balloc(C, 1);757 b->x[0] = i;758 b->wds = 1;759 return b;760 }761762 static Bigint *763mult764 (struct dtoa_context* C, Bigint *a, Bigint *b)765{766 Bigint *c;767 int k, wa, wb, wc;768 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;769 ULong y;770#ifdef ULLong771 ULLong carry, z;772#else773 ULong carry, z;774#ifdef Pack_32775 ULong z2;776#endif777#endif778779 if (a->wds < b->wds) {780 c = a;781 a = b;782 b = c;783 }784 k = a->k;785 wa = a->wds;786 wb = b->wds;787 wc = wa + wb;788 if (wc > a->maxwds)789 k++;790 c = Balloc(C, k);791 for(x = c->x, xa = x + wc; x < xa; x++)792 *x = 0;793 xa = a->x;794 xae = xa + wa;795 xb = b->x;796 xbe = xb + wb;797 xc0 = c->x;798#ifdef ULLong799 for(; xb < xbe; xc0++) {800 if ((y = *xb++)) {801 x = xa;802 xc = xc0;803 carry = 0;804 do {805 z = *x++ * (ULLong)y + *xc + carry;806 carry = z >> 32;807 *xc++ = z & FFFFFFFF;808 }809 while(x < xae);810 *xc = carry;811 }812 }813#else814#ifdef Pack_32815 for(; xb < xbe; xb++, xc0++) {816 if (y = *xb & 0xffff) {817 x = xa;818 xc = xc0;819 carry = 0;820 do {821 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;822 carry = z >> 16;823 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;824 carry = z2 >> 16;825 Storeinc(xc, z2, z);826 }827 while(x < xae);828 *xc = carry;829 }830 if (y = *xb >> 16) {831 x = xa;832 xc = xc0;833 carry = 0;834 z2 = *xc;835 do {836 z = (*x & 0xffff) * y + (*xc >> 16) + carry;837 carry = z >> 16;838 Storeinc(xc, z, z2);839 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;840 carry = z2 >> 16;841 }842 while(x < xae);843 *xc = z2;844 }845 }846#else847 for(; xb < xbe; xc0++) {848 if (y = *xb++) {849 x = xa;850 xc = xc0;851 carry = 0;852 do {853 z = *x++ * y + *xc + carry;854 carry = z >> 16;855 *xc++ = z & 0xffff;856 }857 while(x < xae);858 *xc = carry;859 }860 }861#endif862#endif863 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;864 c->wds = wc;865 return c;866 }867868869 static Bigint *870pow5mult871 (struct dtoa_context* C, Bigint *b, int k)872{873 Bigint *b1, *p5, *p51;874 int i;875 static const int p05[3] = { 5, 25, 125 };876877 if ((i = k & 3))878 b = multadd(C, b, p05[i-1], 0);879880 if (!(k >>= 2))881 return b;882 if (!(p5 = C->p5s)) {883 /* first time */884 p5 = C->p5s = i2b(C, 625);885 p5->next = 0;886 }887 for(;;) {888 if (k & 1) {889 b1 = mult(C, b, p5);890 Bfree(C, b);891 b = b1;892 }893 if (!(k >>= 1))894 break;895 if (!(p51 = p5->next)) {896 p51 = p5->next = mult(C, p5,p5);897 p51->next = 0;898 }899 p5 = p51;900 }901 return b;902 }903904 static Bigint *905lshift906 (struct dtoa_context* C, Bigint *b, int k)907{908 int i, k1, n, n1;909 Bigint *b1;910 ULong *x, *x1, *xe, z;911912#ifdef Pack_32913 n = k >> 5;914#else915 n = k >> 4;916#endif917 k1 = b->k;918 n1 = n + b->wds + 1;919 for(i = b->maxwds; n1 > i; i <<= 1)920 k1++;921 b1 = Balloc(C, k1);922 x1 = b1->x;923 for(i = 0; i < n; i++)924 *x1++ = 0;925 x = b->x;926 xe = x + b->wds;927#ifdef Pack_32928 if (k &= 0x1f) {929 k1 = 32 - k;930 z = 0;931 do {932 *x1++ = *x << k | z;933 z = *x++ >> k1;934 }935 while(x < xe);936 if ((*x1 = z))937 ++n1;938 }939#else940 if (k &= 0xf) {941 k1 = 16 - k;942 z = 0;943 do {944 *x1++ = *x << k & 0xffff | z;945 z = *x++ >> k1;946 }947 while(x < xe);948 if (*x1 = z)949 ++n1;950 }951#endif952 else do953 *x1++ = *x++;954 while(x < xe);955 b1->wds = n1 - 1;956 Bfree(C, b);957 return b1;958 }959960 static int961cmp962 (struct dtoa_context* C, Bigint *a, Bigint *b)963{964 ULong *xa, *xa0, *xb, *xb0;965 int i, j;966967 i = a->wds;968 j = b->wds;969#ifdef DEBUG970 if (i > 1 && !a->x[i-1])971 Bug("cmp called with a->x[a->wds-1] == 0");972 if (j > 1 && !b->x[j-1])973 Bug("cmp called with b->x[b->wds-1] == 0");974#endif975 if (i -= j)976 return i;977 xa0 = a->x;978 xa = xa0 + j;979 xb0 = b->x;980 xb = xb0 + j;981 for(;;) {982 if (*--xa != *--xb)983 return *xa < *xb ? -1 : 1;984 if (xa <= xa0)985 break;986 }987 return 0;988 }989990 static Bigint *991diff992 (struct dtoa_context* C, Bigint *a, Bigint *b)993{994 Bigint *c;995 int i, wa, wb;996 ULong *xa, *xae, *xb, *xbe, *xc;997#ifdef ULLong998 ULLong borrow, y;999#else1000 ULong borrow, y;1001#ifdef Pack_321002 ULong z;1003#endif1004#endif10051006 i = cmp(C, a,b);1007 if (!i) {1008 c = Balloc(C, 0);1009 c->wds = 1;1010 c->x[0] = 0;1011 return c;1012 }1013 if (i < 0) {1014 c = a;1015 a = b;1016 b = c;1017 i = 1;1018 }1019 else1020 i = 0;1021 c = Balloc(C, a->k);1022 c->sign = i;1023 wa = a->wds;1024 xa = a->x;1025 xae = xa + wa;1026 wb = b->wds;1027 xb = b->x;1028 xbe = xb + wb;1029 xc = c->x;1030 borrow = 0;1031#ifdef ULLong1032 do {1033 y = (ULLong)*xa++ - *xb++ - borrow;1034 borrow = y >> 32 & (ULong)1;1035 *xc++ = y & FFFFFFFF;1036 }1037 while(xb < xbe);1038 while(xa < xae) {1039 y = *xa++ - borrow;1040 borrow = y >> 32 & (ULong)1;1041 *xc++ = y & FFFFFFFF;1042 }1043#else1044#ifdef Pack_321045 do {1046 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;1047 borrow = (y & 0x10000) >> 16;1048 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;1049 borrow = (z & 0x10000) >> 16;1050 Storeinc(xc, z, y);1051 }1052 while(xb < xbe);1053 while(xa < xae) {1054 y = (*xa & 0xffff) - borrow;1055 borrow = (y & 0x10000) >> 16;1056 z = (*xa++ >> 16) - borrow;1057 borrow = (z & 0x10000) >> 16;1058 Storeinc(xc, z, y);1059 }1060#else1061 do {1062 y = *xa++ - *xb++ - borrow;1063 borrow = (y & 0x10000) >> 16;1064 *xc++ = y & 0xffff;1065 }1066 while(xb < xbe);1067 while(xa < xae) {1068 y = *xa++ - borrow;1069 borrow = (y & 0x10000) >> 16;1070 *xc++ = y & 0xffff;1071 }1072#endif1073#endif1074 while(!*--xc)1075 wa--;1076 c->wds = wa;1077 return c;1078 }10791080 static double1081ulp1082 (struct dtoa_context* C, U *x)1083{1084 Long L;1085 U u;10861087 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;1088#ifndef Avoid_Underflow1089#ifndef Sudden_Underflow1090 if (L > 0) {1091#endif1092#endif1093#ifdef IBM1094 L |= Exp_msk1 >> 4;1095#endif1096 word0(&u) = L;1097 word1(&u) = 0;1098#ifndef Avoid_Underflow1099#ifndef Sudden_Underflow1100 }1101 else {1102 L = -L >> Exp_shift;1103 if (L < Exp_shift) {1104 word0(&u) = 0x80000 >> L;1105 word1(&u) = 0;1106 }1107 else {1108 word0(&u) = 0;1109 L -= Exp_shift;1110 word1(&u) = L >= 31 ? 1 : 1 << 31 - L;1111 }1112 }1113#endif1114#endif1115 return dval(&u);1116 }11171118 static double1119b2d1120 (struct dtoa_context* C, Bigint *a, int *e)1121{1122 ULong *xa, *xa0, w, y, z;1123 int k;1124 U d;1125#ifdef VAX1126 ULong d0, d1;1127#else1128#define d0 word0(&d)1129#define d1 word1(&d)1130#endif11311132 xa0 = a->x;1133 xa = xa0 + a->wds;1134 y = *--xa;1135#ifdef DEBUG1136 if (!y) Bug("zero y in b2d");1137#endif1138 k = hi0bits(C, y);1139 *e = 32 - k;1140#ifdef Pack_321141 if (k < Ebits) {1142 d0 = Exp_1 | y >> (Ebits - k);1143 w = xa > xa0 ? *--xa : 0;1144 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);1145 goto ret_d;1146 }1147 z = xa > xa0 ? *--xa : 0;1148 if (k -= Ebits) {1149 d0 = Exp_1 | y << k | z >> (32 - k);1150 y = xa > xa0 ? *--xa : 0;1151 d1 = z << k | y >> (32 - k);1152 }1153 else {1154 d0 = Exp_1 | y;1155 d1 = z;1156 }1157#else1158 if (k < Ebits + 16) {1159 z = xa > xa0 ? *--xa : 0;1160 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;1161 w = xa > xa0 ? *--xa : 0;1162 y = xa > xa0 ? *--xa : 0;1163 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;1164 goto ret_d;1165 }1166 z = xa > xa0 ? *--xa : 0;1167 w = xa > xa0 ? *--xa : 0;1168 k -= Ebits + 16;1169 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;1170 y = xa > xa0 ? *--xa : 0;1171 d1 = w << k + 16 | y << k;1172#endif1173 ret_d:1174#ifdef VAX1175 word0(&d) = d0 >> 16 | d0 << 16;1176 word1(&d) = d1 >> 16 | d1 << 16;1177#else1178#undef d01179#undef d11180#endif1181 return dval(&d);1182 }11831184 static Bigint *1185d2b1186 (struct dtoa_context* C, U *d, int *e, int *bits)1187{1188 Bigint *b;1189 int de, k;1190 ULong *x, y, z;1191#ifndef Sudden_Underflow1192 int i;1193#endif1194#ifdef VAX1195 ULong d0, d1;1196 d0 = word0(d) >> 16 | word0(d) << 16;1197 d1 = word1(d) >> 16 | word1(d) << 16;1198#else1199#define d0 word0(d)1200#define d1 word1(d)1201#endif12021203#ifdef Pack_321204 b = Balloc(C, 1);1205#else1206 b = Balloc(C, 2);1207#endif1208 x = b->x;12091210 z = d0 & Frac_mask;1211 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */1212#ifdef Sudden_Underflow1213 de = (int)(d0 >> Exp_shift);1214#ifndef IBM1215 z |= Exp_msk11;1216#endif1217#else1218 if ((de = (int)(d0 >> Exp_shift)))1219 z |= Exp_msk1;1220#endif1221#ifdef Pack_321222 if ((y = d1)) {1223 if ((k = lo0bits(C, &y))) {1224 x[0] = y | z << (32 - k);1225 z >>= k;1226 }1227 else1228 x[0] = y;1229#ifndef Sudden_Underflow1230 i =1231#endif1232 b->wds = (x[1] = z) ? 2 : 1;1233 }1234 else {1235 k = lo0bits(C, &z);1236 x[0] = z;1237#ifndef Sudden_Underflow1238 i =1239#endif1240 b->wds = 1;1241 k += 32;1242 }1243#else1244 if (y = d1) {1245 if (k = lo0bits(C, &y))1246 if (k >= 16) {1247 x[0] = y | z << 32 - k & 0xffff;1248 x[1] = z >> k - 16 & 0xffff;1249 x[2] = z >> k;1250 i = 2;1251 }1252 else {1253 x[0] = y & 0xffff;1254 x[1] = y >> 16 | z << 16 - k & 0xffff;1255 x[2] = z >> k & 0xffff;1256 x[3] = z >> k+16;1257 i = 3;1258 }1259 else {1260 x[0] = y & 0xffff;1261 x[1] = y >> 16;1262 x[2] = z & 0xffff;1263 x[3] = z >> 16;1264 i = 3;1265 }1266 }1267 else {1268#ifdef DEBUG1269 if (!z)1270 Bug("Zero passed to d2b");1271#endif1272 k = lo0bits(C, &z);1273 if (k >= 16) {1274 x[0] = z;1275 i = 0;1276 }1277 else {1278 x[0] = z & 0xffff;1279 x[1] = z >> 16;1280 i = 1;1281 }1282 k += 32;1283 }1284 while(!x[i])1285 --i;1286 b->wds = i + 1;1287#endif1288#ifndef Sudden_Underflow1289 if (de) {1290#endif1291#ifdef IBM1292 *e = (de - Bias - (P-1) << 2) + k;1293 *bits = 4*P + 8 - k - hi0bits(C, word0(d) & Frac_mask);1294#else1295 *e = de - Bias - (P-1) + k;1296 *bits = P - k;1297#endif1298#ifndef Sudden_Underflow1299 }1300 else {1301 *e = de - Bias - (P-1) + 1 + k;1302#ifdef Pack_321303 *bits = 32*i - hi0bits(C, x[i-1]);1304#else1305 *bits = (i+2)*16 - hi0bits(C, x[i]);1306#endif1307 }1308#endif1309 return b;1310 }1311#undef d01312#undef d113131314 static double1315ratio1316 (struct dtoa_context* C, Bigint *a, Bigint *b)1317{1318 U da, db;1319 int k, ka, kb;13201321 dval(&da) = b2d(C, a, &ka);1322 dval(&db) = b2d(C, b, &kb);1323#ifdef Pack_321324 k = ka - kb + 32*(a->wds - b->wds);1325#else1326 k = ka - kb + 16*(a->wds - b->wds);1327#endif1328#ifdef IBM1329 if (k > 0) {1330 word0(&da) += (k >> 2)*Exp_msk1;1331 if (k &= 3)1332 dval(&da) *= 1 << k;1333 }1334 else {1335 k = -k;1336 word0(&db) += (k >> 2)*Exp_msk1;1337 if (k &= 3)1338 dval(&db) *= 1 << k;1339 }1340#else1341 if (k > 0)1342 word0(&da) += k*Exp_msk1;1343 else {1344 k = -k;1345 word0(&db) += k*Exp_msk1;1346 }1347#endif1348 return dval(&da) / dval(&db);1349 }13501351 static CONST double1352tens[] = {1353 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,1354 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,1355 1e20, 1e21, 1e221356#ifdef VAX1357 , 1e23, 1e241358#endif1359 };13601361 static CONST double1362#ifdef IEEE_Arith1363bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };1364static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,1365#ifdef Avoid_Underflow1366 9007199254740992.*9007199254740992.e-2561367 /* = 2^106 * 1e-256 */1368#else1369 1e-2561370#endif1371 };1372/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */1373/* flag unnecessarily. It leads to a song and dance at the end of strtod. */1374#define Scale_Bit 0x101375#define n_bigtens 51376#else1377#ifdef IBM1378bigtens[] = { 1e16, 1e32, 1e64 };1379static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };1380#define n_bigtens 31381#else1382bigtens[] = { 1e16, 1e32 };1383static CONST double tinytens[] = { 1e-16, 1e-32 };1384#define n_bigtens 21385#endif1386#endif13871388#undef Need_Hexdig1389#ifdef INFNAN_CHECK1390#ifndef No_Hex_NaN1391#define Need_Hexdig1392#endif1393#endif13941395#ifndef Need_Hexdig1396#ifndef NO_HEX_FP1397#define Need_Hexdig1398#endif1399#endif14001401#ifdef Need_Hexdig /*{*/1402static unsigned char hexdig[256];14031404 static void1405htinit(unsigned char *h, unsigned char *s, int inc)1406{1407 int i, j;1408 for(i = 0; (j = s[i]) !=0; i++)1409 h[j] = i + inc;1410 }14111412 static void1413hexdig_init(void)1414{1415#define USC (unsigned char *)1416 htinit(hexdig, USC "0123456789", 0x10);1417 htinit(hexdig, USC "abcdef", 0x10 + 10);1418 htinit(hexdig, USC "ABCDEF", 0x10 + 10);1419 }1420#endif /* } Need_Hexdig */14211422#ifdef INFNAN_CHECK14231424#ifndef NAN_WORD01425#define NAN_WORD0 0x7ff800001426#endif14271428#ifndef NAN_WORD11429#define NAN_WORD1 01430#endif14311432 static int1433match1434 (struct dtoa_context* C, const char **sp, const char *t)1435{1436 int c, d;1437 CONST char *s = *sp;14381439 while((d = *t++)) {1440 if ((c = *++s) >= 'A' && c <= 'Z')1441 c += 'a' - 'A';1442 if (c != d)1443 return 0;1444 }1445 *sp = s + 1;1446 return 1;1447 }14481449#ifndef No_Hex_NaN1450 static void1451hexnan1452 (struct dtoa_context* C, U *rvp, const char **sp)1453{1454 ULong c, x[2];1455 CONST char *s;1456 int c1, havedig, udx0, xshift;14571458 if (!hexdig['0'])1459 hexdig_init();1460 x[0] = x[1] = 0;1461 havedig = xshift = 0;1462 udx0 = 1;1463 s = *sp;1464 /* allow optional initial 0x or 0X */1465 while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')1466 ++s;1467 if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))1468 s += 2;1469 while((c = *(CONST unsigned char*)++s)) {1470 if ((c1 = hexdig[c]))1471 c = c1 & 0xf;1472 else if (c <= ' ') {1473 if (udx0 && havedig) {1474 udx0 = 0;1475 xshift = 1;1476 }1477 continue;1478 }1479#ifdef GDTOA_NON_PEDANTIC_NANCHECK1480 else if (/*(*/ c == ')' && havedig) {1481 *sp = s + 1;1482 break;1483 }1484 else1485 return; /* invalid form: don't change *sp */1486#else1487 else {1488 do {1489 if (/*(*/ c == ')') {1490 *sp = s + 1;1491 break;1492 }1493 } while((c = *++s));1494 break;1495 }1496#endif1497 havedig = 1;1498 if (xshift) {1499 xshift = 0;1500 x[0] = x[1];1501 x[1] = 0;1502 }1503 if (udx0)1504 x[0] = (x[0] << 4) | (x[1] >> 28);1505 x[1] = (x[1] << 4) | c;1506 }1507 if ((x[0] &= 0xfffff) || x[1]) {1508 word0(rvp) = Exp_mask | x[0];1509 word1(rvp) = x[1];1510 }1511 }1512#endif /*No_Hex_NaN*/1513#endif /* INFNAN_CHECK */15141515#ifdef Pack_321516#define ULbits 321517#define kshift 51518#define kmask 311519#else1520#define ULbits 161521#define kshift 41522#define kmask 151523#endif15241525#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/1526 static Bigint *1527increment(struct dtoa_context* C, Bigint *b)1528{1529 ULong *x, *xe;1530 Bigint *b1;15311532 x = b->x;1533 xe = x + b->wds;1534 do {1535 if (*x < (ULong)0xffffffffL) {1536 ++*x;1537 return b;1538 }1539 *x++ = 0;1540 } while(x < xe);1541 {1542 if (b->wds >= b->maxwds) {1543 b1 = Balloc(C, b->k+1);1544 Bcopy(b1,b);1545 Bfree(C, b);1546 b = b1;1547 }1548 b->x[b->wds++] = 1;1549 }1550 return b;1551 }15521553#endif /*}*/15541555#ifndef NO_HEX_FP /*{*/15561557 static void1558rshift(struct dtoa_context* C, Bigint *b, int k)1559{1560 ULong *x, *x1, *xe, y;1561 int n;15621563 x = x1 = b->x;1564 n = k >> kshift;1565 if (n < b->wds) {1566 xe = x + b->wds;1567 x += n;1568 if (k &= kmask) {1569 n = 32 - k;1570 y = *x++ >> k;1571 while(x < xe) {1572 *x1++ = (y | (*x << n)) & 0xffffffff;1573 y = *x++ >> k;1574 }1575 if ((*x1 = y) !=0)1576 x1++;1577 }1578 else1579 while(x < xe)1580 *x1++ = *x++;1581 }1582 if ((b->wds = x1 - b->x) == 0)1583 b->x[0] = 0;1584 }15851586 static ULong1587any_on(Bigint *b, int k)1588{1589 int n, nwds;1590 ULong *x, *x0, x1, x2;15911592 x = b->x;1593 nwds = b->wds;1594 n = k >> kshift;1595 if (n > nwds)1596 n = nwds;1597 else if (n < nwds && (k &= kmask)) {1598 x1 = x2 = x[n];1599 x1 >>= k;1600 x1 <<= k;1601 if (x1 != x2)1602 return 1;1603 }1604 x0 = x;1605 x += n;1606 while(x > x0)1607 if (*--x)1608 return 1;1609 return 0;1610 }16111612enum { /* rounding values: same as FLT_ROUNDS */1613 Round_zero = 0,1614 Round_near = 1,1615 Round_up = 2,1616 Round_down = 31617 };16181619static void1620gethex(struct dtoa_context* C, CONST char **sp, U *rvp, int rounding, int sign)1621{1622 Bigint *b;1623 CONST unsigned char *decpt, *s0, *s, *s1;1624 Long e, e1;1625 ULong L, lostbits, *x;1626 int big, denorm, esign, havedig, k, n, nbits, up, zret;1627#ifdef IBM1628 int j;1629#endif1630 enum {1631#ifdef IEEE_Arith /*{{*/1632 emax = 0x7fe - Bias - P + 1,1633 emin = Emin - P + 11634#else /*}{*/1635 emin = Emin - P,1636#ifdef VAX1637 emax = 0x7ff - Bias - P + 11638#endif1639#ifdef IBM1640 emax = 0x7f - Bias - P1641#endif1642#endif /*}}*/1643 };1644#ifdef USE_LOCALE1645 int i;1646#ifdef NO_LOCALE_CACHE1647 const unsigned char *decimalpoint = (unsigned char*)1648 localeconv()->decimal_point;1649#else1650 const unsigned char *decimalpoint;1651 static unsigned char *decimalpoint_cache;1652 if (!(s0 = decimalpoint_cache)) {1653 s0 = (unsigned char*)localeconv()->decimal_point;1654 if ((decimalpoint_cache = (unsigned char*)1655 MALLOC(strlen((CONST char*)s0) + 1))) {1656 strcpy((char*)decimalpoint_cache, (CONST char*)s0);1657 s0 = decimalpoint_cache;1658 }1659 }1660 decimalpoint = s0;1661#endif1662#endif16631664 if (!hexdig['0'])1665 hexdig_init();1666 havedig = 0;1667 s0 = *(CONST unsigned char **)sp + 2;1668 while(s0[havedig] == '0')1669 havedig++;1670 s0 += havedig;1671 s = s0;1672 decpt = 0;1673 zret = 0;1674 e = 0;1675 if (hexdig[*s])1676 havedig++;1677 else {1678 zret = 1;1679#ifdef USE_LOCALE1680 for(i = 0; decimalpoint[i]; ++i) {1681 if (s[i] != decimalpoint[i])1682 goto pcheck;1683 }1684 decpt = s += i;1685#else1686 if (*s != '.')1687 goto pcheck;1688 decpt = ++s;1689#endif1690 if (!hexdig[*s])1691 goto pcheck;1692 while(*s == '0')1693 s++;1694 if (hexdig[*s])1695 zret = 0;1696 havedig = 1;1697 s0 = s;1698 }1699 while(hexdig[*s])1700 s++;1701#ifdef USE_LOCALE1702 if (*s == *decimalpoint && !decpt) {1703 for(i = 1; decimalpoint[i]; ++i) {1704 if (s[i] != decimalpoint[i])1705 goto pcheck;1706 }1707 decpt = s += i;1708#else1709 if (*s == '.' && !decpt) {1710 decpt = ++s;1711#endif1712 while(hexdig[*s])1713 s++;1714 }/*}*/1715 if (decpt)1716 e = -(((Long)(s-decpt)) << 2);1717 pcheck:1718 s1 = s;1719 big = esign = 0;1720 switch(*s) {1721 case 'p':1722 case 'P':1723 switch(*++s) {1724 case '-':1725 esign = 1;1726 /* no break */1727 JQ_FALLTHROUGH;1728 case '+':1729 s++;1730 }1731 if ((n = hexdig[*s]) == 0 || n > 0x19) {1732 s = s1;1733 break;1734 }1735 e1 = n - 0x10;1736 while((n = hexdig[*++s]) !=0 && n <= 0x19) {1737 if (e1 & 0xf8000000)1738 big = 1;1739 e1 = 10*e1 + n - 0x10;1740 }1741 if (esign)1742 e1 = -e1;1743 e += e1;1744 }1745 *sp = (char*)s;1746 if (!havedig)1747 *sp = (char*)s0 - 1;1748 if (zret)1749 goto retz1;1750 if (big) {1751 if (esign) {1752#ifdef IEEE_Arith1753 switch(rounding) {1754 case Round_up:1755 if (sign)1756 break;1757 goto ret_tiny;1758 case Round_down:1759 if (!sign)1760 break;1761 goto ret_tiny;1762 }1763#endif1764 goto retz;1765#ifdef IEEE_Arith1766 ret_tiny:1767#ifndef NO_ERRNO1768 errno = ERANGE;1769#endif1770 word0(rvp) = 0;1771 word1(rvp) = 1;1772 return;1773#endif /* IEEE_Arith */1774 }1775 switch(rounding) {1776 case Round_near:1777 goto ovfl1;1778 case Round_up:1779 if (!sign)1780 goto ovfl1;1781 goto ret_big;1782 case Round_down:1783 if (sign)1784 goto ovfl1;1785 goto ret_big;1786 }1787 ret_big:1788 word0(rvp) = Big0;1789 word1(rvp) = Big1;1790 return;1791 }1792 n = s1 - s0 - 1;1793 for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)1794 k++;1795 b = Balloc(C, k);1796 x = b->x;1797 n = 0;1798 L = 0;1799#ifdef USE_LOCALE1800 for(i = 0; decimalpoint[i+1]; ++i);1801#endif1802 while(s1 > s0) {1803#ifdef USE_LOCALE1804 if (*--s1 == decimalpoint[i]) {1805 s1 -= i;1806 continue;1807 }1808#else1809 if (*--s1 == '.')1810 continue;1811#endif1812 if (n == ULbits) {1813 *x++ = L;1814 L = 0;1815 n = 0;1816 }1817 L |= (hexdig[*s1] & 0x0f) << n;1818 n += 4;1819 }1820 *x++ = L;1821 b->wds = n = x - b->x;1822 n = ULbits*n - hi0bits(C, L);1823 nbits = Nbits;1824 lostbits = 0;1825 x = b->x;1826 if (n > nbits) {1827 n -= nbits;1828 if (any_on(b,n)) {1829 lostbits = 1;1830 k = n - 1;1831 if (x[k>>kshift] & 1 << (k & kmask)) {1832 lostbits = 2;1833 if (k > 0 && any_on(b,k))1834 lostbits = 3;1835 }1836 }1837 rshift(C, b, n);1838 e += n;1839 }1840 else if (n < nbits) {1841 n = nbits - n;1842 b = lshift(C, b, n);1843 e -= n;1844 x = b->x;1845 }1846 if (e > Emax) {1847 ovfl:1848 Bfree(C, b);1849 ovfl1:1850#ifndef NO_ERRNO1851 errno = ERANGE;1852#endif1853 word0(rvp) = Exp_mask;1854 word1(rvp) = 0;1855 return;1856 }1857 denorm = 0;1858 if (e < emin) {1859 denorm = 1;1860 n = emin - e;1861 if (n >= nbits) {1862#ifdef IEEE_Arith /*{*/1863 switch (rounding) {1864 case Round_near:1865 if (n == nbits && (n < 2 || any_on(b,n-1)))1866 goto ret_tiny;1867 break;1868 case Round_up:1869 if (!sign)1870 goto ret_tiny;1871 break;1872 case Round_down:1873 if (sign)1874 goto ret_tiny;1875 }1876#endif /* } IEEE_Arith */1877 Bfree(C, b);1878 retz:1879#ifndef NO_ERRNO1880 errno = ERANGE;1881#endif1882 retz1:1883 rvp->d = 0.;1884 return;1885 }1886 k = n - 1;1887 if (lostbits)1888 lostbits = 1;1889 else if (k > 0)1890 lostbits = any_on(b,k);1891 if (x[k>>kshift] & 1 << (k & kmask))1892 lostbits |= 2;1893 nbits -= n;1894 rshift(C, b,n);1895 e = emin;1896 }1897 if (lostbits) {1898 up = 0;1899 switch(rounding) {1900 case Round_zero:1901 break;1902 case Round_near:1903 if (lostbits & 21904 && (lostbits & 1) | (x[0] & 1))1905 up = 1;1906 break;1907 case Round_up:1908 up = 1 - sign;1909 break;1910 case Round_down:1911 up = sign;1912 }1913 if (up) {1914 k = b->wds;1915 b = increment(C, b);1916 x = b->x;1917 if (denorm) {1918#if 01919 if (nbits == Nbits - 11920 && x[nbits >> kshift] & 1 << (nbits & kmask))1921 denorm = 0; /* not currently used */1922#endif1923 }1924 else if (b->wds > k1925 || ((n = nbits & kmask) !=01926 && hi0bits(C, x[k-1]) < 32-n)) {1927 rshift(C, b,1);1928 if (++e > Emax)1929 goto ovfl;1930 }1931 }1932 }1933#ifdef IEEE_Arith1934 if (denorm)1935 word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;1936 else1937 word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);1938 word1(rvp) = b->x[0];1939#endif1940#ifdef IBM1941 if ((j = e & 3)) {1942 k = b->x[0] & ((1 << j) - 1);1943 rshift(C, b,j);1944 if (k) {1945 switch(rounding) {1946 case Round_up:1947 if (!sign)1948 increment(b);1949 break;1950 case Round_down:1951 if (sign)1952 increment(b);1953 break;1954 case Round_near:1955 j = 1 << (j-1);1956 if (k & j && ((k & (j-1)) | lostbits))1957 increment(b);1958 }1959 }1960 }1961 e >>= 2;1962 word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);1963 word1(rvp) = b->x[0];1964#endif1965#ifdef VAX1966 /* The next two lines ignore swap of low- and high-order 2 bytes. */1967 /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */1968 /* word1(rvp) = b->x[0]; */1969 word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);1970 word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);1971#endif1972 Bfree(C, b);1973 }1974#endif /*!NO_HEX_FP}*/19751976 static int1977dshift(struct dtoa_context* C, Bigint *b, int p2)1978{1979 int rv = hi0bits(C, b->x[b->wds-1]) - 4;1980 if (p2 > 0)1981 rv -= p2;1982 return rv & kmask;1983 }19841985 static int1986quorem1987 (struct dtoa_context* C, Bigint *b, Bigint *S)1988{1989 int n;1990 ULong *bx, *bxe, q, *sx, *sxe;1991#ifdef ULLong1992 ULLong borrow, carry, y, ys;1993#else1994 ULong borrow, carry, y, ys;1995#ifdef Pack_321996 ULong si, z, zs;1997#endif1998#endif19992000 n = S->wds;

The first 2,000 of 4,276 lines.