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