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