1 /**************************************************************** 2 3 The author of this software is David M. Gay. 4 5 Copyright (C) 1998-2000 by Lucent Technologies 6 All Rights Reserved 7 8 Permission to use, copy, modify, and distribute this software and 9 its documentation for any purpose and without fee is hereby 10 granted, provided that the above copyright notice appear in all 11 copies and that both that the copyright notice and this 12 permission notice and warranty disclaimer appear in supporting 13 documentation, and that the name of Lucent or any of its entities 14 not be used in advertising or publicity pertaining to 15 distribution of the software without specific, written prior 16 permission. 17 18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 THIS SOFTWARE. 26 27 ****************************************************************/ 28 29 /* This is a variation on dtoa.c that converts arbitary binary 30 floating-point formats to and from decimal notation. It uses 31 double-precision arithmetic internally, so there are still 32 various #ifdefs that adapt the calculations to the native 33 double-precision arithmetic (any of IEEE, VAX D_floating, 34 or IBM mainframe arithmetic). 35 36 Please send bug reports to David M. Gay (dmg at acm dot org, 37 with " at " changed at "@" and " dot " changed to "."). 38 */ 39 40 /* On a machine with IEEE extended-precision registers, it is 41 * necessary to specify double-precision (53-bit) rounding precision 42 * before invoking strtod or dtoa. If the machine uses (the equivalent 43 * of) Intel 80x87 arithmetic, the call 44 * _control87(PC_53, MCW_PC); 45 * does this with many compilers. Whether this or another call is 46 * appropriate depends on the compiler; for this to work, it may be 47 * necessary to #include "float.h" or another system-dependent header 48 * file. 49 */ 50 51 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. 52 * 53 * This strtod returns a nearest machine number to the input decimal 54 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are 55 * broken by the IEEE round-even rule. Otherwise ties are broken by 56 * biased rounding (add half and chop). 57 * 58 * Inspired loosely by William D. Clinger's paper "How to Read Floating 59 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 112-126]. 60 * 61 * Modifications: 62 * 63 * 1. We only require IEEE, IBM, or VAX double-precision 64 * arithmetic (not IEEE double-extended). 65 * 2. We get by with floating-point arithmetic in a case that 66 * Clinger missed -- when we're computing d * 10^n 67 * for a small integer d and the integer n is not too 68 * much larger than 22 (the maximum integer k for which 69 * we can represent 10^k exactly), we may be able to 70 * compute (d*10^k) * 10^(e-k) with just one roundoff. 71 * 3. Rather than a bit-at-a-time adjustment of the binary 72 * result in the hard case, we use floating-point 73 * arithmetic to determine the adjustment to within 74 * one bit; only in really hard cases do we need to 75 * compute a second residual. 76 * 4. Because of 3., we don't need a large table of powers of 10 77 * for ten-to-e (just some small tables, e.g. of 10^k 78 * for 0 <= k <= 22). 79 */ 80 81 /* 82 * #define IEEE_8087 for IEEE-arithmetic machines where the least 83 * significant byte has the lowest address. 84 * #define IEEE_MC68k for IEEE-arithmetic machines where the most 85 * significant byte has the lowest address. 86 * #define Long int on machines with 32-bit ints and 64-bit longs. 87 * #define Sudden_Underflow for IEEE-format machines without gradual 88 * underflow (i.e., that flush to zero on underflow). 89 * #define IBM for IBM mainframe-style floating-point arithmetic. 90 * #define VAX for VAX-style floating-point arithmetic (D_floating). 91 * #define No_leftright to omit left-right logic in fast floating-point 92 * computation of dtoa and gdtoa. This will cause modes 4 and 5 to be 93 * treated the same as modes 2 and 3 for some inputs. 94 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. 95 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines 96 * that use extended-precision instructions to compute rounded 97 * products and quotients) with IBM. 98 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic 99 * that rounds toward +Infinity. 100 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased 101 * rounding when the underlying floating-point arithmetic uses 102 * unbiased rounding. This prevent using ordinary floating-point 103 * arithmetic when the result could be computed with one rounding error. 104 * #define Inaccurate_Divide for IEEE-format with correctly rounded 105 * products but inaccurate quotients, e.g., for Intel i860. 106 * #define NO_LONG_LONG on machines that do not have a "long long" 107 * integer type (of >= 64 bits). On such machines, you can 108 * #define Just_16 to store 16 bits per 32-bit Long when doing 109 * high-precision integer arithmetic. Whether this speeds things 110 * up or slows things down depends on the machine and the number 111 * being converted. If long long is available and the name is 112 * something other than "long long", #define Llong to be the name, 113 * and if "unsigned Llong" does not work as an unsigned version of 114 * Llong, #define #ULLong to be the corresponding unsigned type. 115 * #define KR_headers for old-style C function headers. 116 * #define Bad_float_h if your system lacks a float.h or if it does not 117 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, 118 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. 119 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) 120 * if memory is available and otherwise does something you deem 121 * appropriate. If MALLOC is undefined, malloc will be invoked 122 * directly -- and assumed always to succeed. Similarly, if you 123 * want something other than the system's free() to be called to 124 * recycle memory acquired from MALLOC, #define FREE to be the 125 * name of the alternate routine. (FREE or free is only called in 126 * pathological cases, e.g., in a gdtoa call after a gdtoa return in 127 * mode 3 with thousands of digits requested.) 128 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making 129 * memory allocations from a private pool of memory when possible. 130 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, 131 * unless #defined to be a different length. This default length 132 * suffices to get rid of MALLOC calls except for unusual cases, 133 * such as decimal-to-binary conversion of a very long string of 134 * digits. When converting IEEE double precision values, the 135 * longest string gdtoa can return is about 751 bytes long. For 136 * conversions by strtod of strings of 800 digits and all gdtoa 137 * conversions of IEEE doubles in single-threaded executions with 138 * 8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with 139 * 4-byte pointers, PRIVATE_MEM >= 7112 appears adequate. 140 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK 141 * #defined automatically on IEEE systems. On such systems, 142 * when INFNAN_CHECK is #defined, strtod checks 143 * for Infinity and NaN (case insensitively). 144 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, 145 * strtodg also accepts (case insensitively) strings of the form 146 * NaN(x), where x is a string of hexadecimal digits (optionally 147 * preceded by 0x or 0X) and spaces; if there is only one string 148 * of hexadecimal digits, it is taken for the fraction bits of the 149 * resulting NaN; if there are two or more strings of hexadecimal 150 * digits, each string is assigned to the next available sequence 151 * of 32-bit words of fractions bits (starting with the most 152 * significant), right-aligned in each sequence. 153 * Unless GDTOA_NON_PEDANTIC_NANCHECK is #defined, input "NaN(...)" 154 * is consumed even when ... has the wrong form (in which case the 155 * "(...)" is consumed but ignored). 156 * #define MULTIPLE_THREADS if the system offers preemptively scheduled 157 * multiple threads. In this case, you must provide (or suitably 158 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed 159 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed 160 * in pow5mult, ensures lazy evaluation of only one copy of high 161 * powers of 5; omitting this lock would introduce a small 162 * probability of wasting memory, but would otherwise be harmless.) 163 * You must also invoke freedtoa(s) to free the value s returned by 164 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. 165 * #define IMPRECISE_INEXACT if you do not care about the setting of 166 * the STRTOG_Inexact bits in the special case of doing IEEE double 167 * precision conversions (which could also be done by the strtod in 168 * dtoa.c). 169 * #define NO_HEX_FP to disable recognition of C9x's hexadecimal 170 * floating-point constants. 171 * #define -DNO_ERRNO to suppress setting errno (in strtod.c and 172 * strtodg.c). 173 * #define NO_STRING_H to use private versions of memcpy. 174 * On some K&R systems, it may also be necessary to 175 * #define DECLARE_SIZE_T in this case. 176 * #define USE_LOCALE to use the current locale's decimal_point value. 177 */ 178 179 #ifndef GDTOAIMP_H_INCLUDED 180 #define GDTOAIMP_H_INCLUDED 181 #include "mprec.h" 182 #include "gdtoa.h" 183 184 #ifndef __SINGLE_THREAD__ 185 #define MULTIPLE_THREADS 186 #endif 187 188 #define dtoa __dtoa 189 #define gdtoa __gdtoa 190 #define freedtoa __freedtoa 191 192 #define dtoa_result __dtoa_result_D2A 193 #define nrv_alloc __nrv_alloc_D2A 194 #define quorem __quorem_D2A 195 #define rshift __rshift_D2A 196 #define rv_alloc __rv_alloc_D2A 197 #define trailz __trailz_D2A 198 199 extern char *dtoa_result; 200 extern char *nrv_alloc ANSI((struct _reent *, char*, char **, int)); 201 extern int quorem ANSI((Bigint*, Bigint*)); 202 extern void rshift ANSI((Bigint*, int)); 203 extern char *rv_alloc ANSI((struct _reent *, int)); 204 extern int trailz ANSI((Bigint*)); 205 206 #endif /* GDTOAIMP_H_INCLUDED */ 207