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