1$NetBSD: README,v 1.1.1.1 2006/01/25 15:18:40 kleink Exp $ 2 3This directory contains source for a library of binary -> decimal 4and decimal -> binary conversion routines, for single-, double-, 5and extended-precision IEEE binary floating-point arithmetic, and 6other IEEE-like binary floating-point, including "double double", 7as in 8 9 T. J. Dekker, "A Floating-Point Technique for Extending the 10 Available Precision", Numer. Math. 18 (1971), pp. 224-242 11 12and 13 14 "Inside Macintosh: PowerPC Numerics", Addison-Wesley, 1994 15 16The conversion routines use double-precision floating-point arithmetic 17and, where necessary, high precision integer arithmetic. The routines 18are generalizations of the strtod and dtoa routines described in 19 20 David M. Gay, "Correctly Rounded Binary-Decimal and 21 Decimal-Binary Conversions", Numerical Analysis Manuscript 22 No. 90-10, Bell Labs, Murray Hill, 1990; 23 http://cm.bell-labs.com/cm/cs/what/ampl/REFS/rounding.ps.gz 24 25(based in part on papers by Clinger and Steele & White: see the 26references in the above paper). 27 28The present conversion routines should be able to use any of IEEE binary, 29VAX, or IBM-mainframe double-precision arithmetic internally, but I (dmg) 30have so far only had a chance to test them with IEEE double precision 31arithmetic. 32 33The core conversion routines are strtodg for decimal -> binary conversions 34and gdtoa for binary -> decimal conversions. These routines operate 35on arrays of unsigned 32-bit integers of type ULong, a signed 32-bit 36exponent of type Long, and arithmetic characteristics described in 37struct FPI; FPI, Long, and ULong are defined in gdtoa.h. File arith.h 38is supposed to provide #defines that cause gdtoa.h to define its 39types correctly. File arithchk.c is source for a program that 40generates a suitable arith.h on all systems where I've been able to 41test it. 42 43The core conversion routines are meant to be called by helper routines 44that know details of the particular binary arithmetic of interest and 45convert. The present directory provides helper routines for 5 variants 46of IEEE binary floating-point arithmetic, each indicated by one or 47two letters: 48 49 f IEEE single precision 50 d IEEE double precision 51 x IEEE extended precision, as on Intel 80x87 52 and software emulations of Motorola 68xxx chips 53 that do not pad the way the 68xxx does, but 54 only store 80 bits 55 xL IEEE extended precision, as on Motorola 68xxx chips 56 Q quad precision, as on Sun Sparc chips 57 dd double double, pairs of IEEE double numbers 58 whose sum is the desired value 59 60For decimal -> binary conversions, there are three families of 61helper routines: one for round-nearest: 62 63 strtof 64 strtod 65 strtodd 66 strtopd 67 strtopf 68 strtopx 69 strtopxL 70 strtopQ 71 72one with rounding direction specified: 73 74 strtorf 75 strtord 76 strtordd 77 strtorx 78 strtorxL 79 strtorQ 80 81and one for computing an interval (at most one bit wide) that contains 82the decimal number: 83 84 strtoIf 85 strtoId 86 strtoIdd 87 strtoIx 88 strtoIxL 89 strtoIQ 90 91The latter call strtoIg, which makes one call on strtodg and adjusts 92the result to provide the desired interval. On systems where native 93arithmetic can easily make one-ulp adjustments on values in the 94desired floating-point format, it might be more efficient to use the 95native arithmetic. Routine strtodI is a variant of strtoId that 96illustrates one way to do this for IEEE binary double-precision 97arithmetic -- but whether this is more efficient remains to be seen. 98 99Functions strtod and strtof have "natural" return types, float and 100double -- strtod is specified by the C standard, and strtof appears 101in the stdlib.h of some systems, such as (at least some) Linux systems. 102The other functions write their results to their final argument(s): 103to the final two argument for the strtoI... (interval) functions, 104and to the final argument for the others (strtop... and strtor...). 105Where possible, these arguments have "natural" return types (double* 106or float*), to permit at least some type checking. In reality, they 107are viewed as arrays of ULong (or, for the "x" functions, UShort) 108values. On systems where long double is the appropriate type, one can 109pass long double* final argument(s) to these routines. The int value 110that these routines return is the return value from the call they make 111on strtodg; see the enum of possible return values in gdtoa.h. 112 113Source files g_ddfmt.c, misc.c, smisc.c, strtod.c, strtodg.c, and ulp.c 114should use true IEEE double arithmetic (not, e.g., double extended), 115at least for storing (and viewing the bits of) the variables declared 116"double" within them. 117 118One detail indicated in struct FPI is whether the target binary 119arithmetic departs from the IEEE standard by flushing denormalized 120numbers to 0. On systems that do this, the helper routines for 121conversion to double-double format (when compiled with 122Sudden_Underflow #defined) penalize the bottom of the exponent 123range so that they return a nonzero result only when the least 124significant bit of the less significant member of the pair of 125double values returned can be expressed as a normalized double 126value. An alternative would be to drop to 53-bit precision near 127the bottom of the exponent range. To get correct rounding, this 128would (in general) require two calls on strtodg (one specifying 129126-bit arithmetic, then, if necessary, one specifying 53-bit 130arithmetic). 131 132By default, the core routine strtodg and strtod set errno to ERANGE 133if the result overflows to +Infinity or underflows to 0. Compile 134these routines with NO_ERRNO #defined to inhibit errno assignments. 135 136Routine strtod is based on netlib's "dtoa.c from fp", and 137(f = strtod(s,se)) is more efficient for some conversions than, say, 138strtord(s,se,1,&f). Parts of strtod require true IEEE double 139arithmetic with the default rounding mode (round-to-nearest) and, on 140systems with IEEE extended-precision registers, double-precision 141(53-bit) rounding precision. If the machine uses (the equivalent of) 142Intel 80x87 arithmetic, the call 143 _control87(PC_53, MCW_PC); 144does this with many compilers. Whether this or another call is 145appropriate depends on the compiler; for this to work, it may be 146necessary to #include "float.h" or another system-dependent header 147file. 148 149Source file strtodnrp.c gives a strtod that does not require 53-bit 150rounding precision on systems (such as Intel IA32 systems) that may 151suffer double rounding due to use of extended-precision registers. 152For some conversions this variant of strtod is less efficient than the 153one in strtod.c when the latter is run with 53-bit rounding precision. 154 155The values that the strto* routines return for NaNs are determined by 156gd_qnan.h, which the makefile generates by running the program whose 157source is qnan.c. Note that the rules for distinguishing signaling 158from quiet NaNs are system-dependent. For cross-compilation, you need 159to determine arith.h and gd_qnan.h suitably, e.g., using the 160arithmetic of the target machine. 161 162C99's hexadecimal floating-point constants are recognized by the 163strto* routines (but this feature has not yet been heavily tested). 164Compiling with NO_HEX_FP #defined disables this feature. 165 166When compiled with -DINFNAN_CHECK, the strto* routines recognize C99's 167NaN and Infinity syntax. Moreover, unless No_Hex_NaN is #defined, the 168strto* routines also recognize C99's NaN(...) syntax: they accept 169(case insensitively) strings of the form NaN(x), where x is a string 170of hexadecimal digits and spaces; if there is only one string of 171hexadecimal digits, it is taken for the fraction bits of the resulting 172NaN; if there are two or more strings of hexadecimal digits, each 173string is assigned to the next available sequence of 32-bit words of 174fractions bits (starting with the most significant), right-aligned in 175each sequence. 176 177For binary -> decimal conversions, I've provided just one family 178of helper routines: 179 180 g_ffmt 181 g_dfmt 182 g_ddfmt 183 g_xfmt 184 g_xLfmt 185 g_Qfmt 186 187which do a "%g" style conversion either to a specified number of decimal 188places (if their ndig argument is positive), or to the shortest 189decimal string that rounds to the given binary floating-point value 190(if ndig <= 0). They write into a buffer supplied as an argument 191and return either a pointer to the end of the string (a null character) 192in the buffer, if the buffer was long enough, or 0. Other forms of 193conversion are easily done with the help of gdtoa(), such as %e or %f 194style and conversions with direction of rounding specified (so that, if 195desired, the decimal value is either >= or <= the binary value). 196 197For an example of more general conversions based on dtoa(), see 198netlib's "printf.c from ampl/solvers". 199 200For double-double -> decimal, g_ddfmt() assumes IEEE-like arithmetic 201of precision max(126, #bits(input)) bits, where #bits(input) is the 202number of mantissa bits needed to represent the sum of the two double 203values in the input. 204 205The makefile creates a library, gdtoa.a. To use the helper 206routines, a program only needs to include gdtoa.h. All the 207source files for gdtoa.a include a more extensive gdtoaimp.h; 208among other things, gdtoaimp.h has #defines that make "internal" 209names end in _D2A. To make a "system" library, one could modify 210these #defines to make the names start with __. 211 212Various comments about possible #defines appear in gdtoaimp.h, 213but for most purposes, arith.h should set suitable #defines. 214 215Systems with preemptive scheduling of multiple threads require some 216manual intervention. On such systems, it's necessary to compile 217dmisc.c, dtoa.c gdota.c, and misc.c with MULTIPLE_THREADS #defined, 218and to provide (or suitably #define) two locks, acquired by 219ACQUIRE_DTOA_LOCK(n) and freed by FREE_DTOA_LOCK(n) for n = 0 or 1. 220(The second lock, accessed in pow5mult, ensures lazy evaluation of 221only one copy of high powers of 5; omitting this lock would introduce 222a small probability of wasting memory, but would otherwise be harmless.) 223Routines that call dtoa or gdtoa directly must also invoke freedtoa(s) 224to free the value s returned by dtoa or gdtoa. It's OK to do so whether 225or not MULTIPLE_THREADS is #defined, and the helper g_*fmt routines 226listed above all do this indirectly (in gfmt_D2A(), which they all call). 227 228By default, there is a private pool of memory of length 2000 bytes 229for intermediate quantities, and MALLOC (see gdtoaimp.h) is called only 230if the private pool does not suffice. 2000 is large enough that MALLOC 231is called only under very unusual circumstances (decimal -> binary 232conversion of very long strings) for conversions to and from double 233precision. For systems with preemptively scheduled multiple threads 234or for conversions to extended or quad, it may be appropriate to 235#define PRIVATE_MEM nnnn, where nnnn is a suitable value > 2000. 236For extended and quad precisions, -DPRIVATE_MEM=20000 is probably 237plenty even for many digits at the ends of the exponent range. 238Use of the private pool avoids some overhead. 239 240Directory test provides some test routines. See its README. 241I've also tested this stuff (except double double conversions) 242with Vern Paxson's testbase program: see 243 244 V. Paxson and W. Kahan, "A Program for Testing IEEE Binary-Decimal 245 Conversion", manuscript, May 1991, 246 ftp://ftp.ee.lbl.gov/testbase-report.ps.Z . 247 248(The same ftp directory has source for testbase.) 249 250Some system-dependent additions to CFLAGS in the makefile: 251 252 HU-UX: -Aa -Ae 253 OSF (DEC Unix): -ieee_with_no_inexact 254 SunOS 4.1x: -DKR_headers -DBad_float_h 255 256If you want to put this stuff into a shared library and your 257operating system requires export lists for shared libraries, 258the following would be an appropriate export list: 259 260 dtoa 261 freedtoa 262 g_Qfmt 263 g_ddfmt 264 g_dfmt 265 g_ffmt 266 g_xLfmt 267 g_xfmt 268 gdtoa 269 strtoIQ 270 strtoId 271 strtoIdd 272 strtoIf 273 strtoIx 274 strtoIxL 275 strtod 276 strtodI 277 strtodg 278 strtof 279 strtopQ 280 strtopd 281 strtopdd 282 strtopf 283 strtopx 284 strtopxL 285 strtorQ 286 strtord 287 strtordd 288 strtorf 289 strtorx 290 strtorxL 291 292When time permits, I (dmg) hope to write in more detail about the 293present conversion routines; for now, this README file must suffice. 294Meanwhile, if you wish to write helper functions for other kinds of 295IEEE-like arithmetic, some explanation of struct FPI and the bits 296array may be helpful. Both gdtoa and strtodg operate on a bits array 297described by FPI *fpi. The bits array is of type ULong, a 32-bit 298unsigned integer type. Floating-point numbers have fpi->nbits bits, 299with the least significant 32 bits in bits[0], the next 32 bits in 300bits[1], etc. These numbers are regarded as integers multiplied by 3012^e (i.e., 2 to the power of the exponent e), where e is the second 302argument (be) to gdtoa and is stored in *exp by strtodg. The minimum 303and maximum exponent values fpi->emin and fpi->emax for normalized 304floating-point numbers reflect this arrangement. For example, the 305P754 standard for binary IEEE arithmetic specifies doubles as having 30653 bits, with normalized values of the form 1.xxxxx... times 2^(b-1023), 307with 52 bits (the x's) and the biased exponent b represented explicitly; 308b is an unsigned integer in the range 1 <= b <= 2046 for normalized 309finite doubles, b = 0 for denormals, and b = 2047 for Infinities and NaNs. 310To turn an IEEE double into the representation used by strtodg and gdtoa, 311we multiply 1.xxxx... by 2^52 (to make it an integer) and reduce the 312exponent e = (b-1023) by 52: 313 314 fpi->emin = 1 - 1023 - 52 315 fpi->emax = 1046 - 1023 - 52 316 317In various wrappers for IEEE double, we actually write -53 + 1 rather 318than -52, to emphasize that there are 53 bits including one implicit bit. 319Field fpi->rounding indicates the desired rounding direction, with 320possible values 321 FPI_Round_zero = toward 0, 322 FPI_Round_near = unbiased rounding -- the IEEE default, 323 FPI_Round_up = toward +Infinity, and 324 FPI_Round_down = toward -Infinity 325given in gdtoa.h. 326 327Field fpi->sudden_underflow indicates whether strtodg should return 328denormals or flush them to zero. Normal floating-point numbers have 329bit fpi->nbits in the bits array on. Denormals have it off, with 330exponent = fpi->emin. Strtodg provides distinct return values for normals 331and denormals; see gdtoa.h. 332 333Compiling g__fmt.c, strtod.c, and strtodg.c with -DUSE_LOCALE causes 334the decimal-point character to be taken from the current locale; otherwise 335it is '.'. 336 337Please send comments to David M. Gay (dmg at acm dot org, with " at " 338changed at "@" and " dot " changed to "."). 339