1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998-2000 by Lucent Technologies
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
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
27 ****************************************************************/
31 /* This is a variation on dtoa.c that converts arbitary binary
32 floating-point formats to and from decimal notation. It uses
33 double-precision arithmetic internally, so there are still
34 various #ifdefs that adapt the calculations to the native
35 double-precision arithmetic (any of IEEE, VAX D_floating,
36 or IBM mainframe arithmetic).
38 Please send bug reports to David M. Gay (dmg at acm dot org,
39 with " at " changed at "@" and " dot " changed to ".").
42 /* On a machine with IEEE extended-precision registers, it is
43 * necessary to specify double-precision (53-bit) rounding precision
44 * before invoking strtod or dtoa. If the machine uses (the equivalent
45 * of) Intel 80x87 arithmetic, the call
46 * _control87(PC_53, MCW_PC);
47 * does this with many compilers. Whether this or another call is
48 * appropriate depends on the compiler; for this to work, it may be
49 * necessary to #include "float.h" or another system-dependent header
53 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
55 * This strtod returns a nearest machine number to the input decimal
56 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
57 * broken by the IEEE round-even rule. Otherwise ties are broken by
58 * biased rounding (add half and chop).
60 * Inspired loosely by William D. Clinger's paper "How to Read Floating
61 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 112-126].
65 * 1. We only require IEEE, IBM, or VAX double-precision
66 * arithmetic (not IEEE double-extended).
67 * 2. We get by with floating-point arithmetic in a case that
68 * Clinger missed -- when we're computing d * 10^n
69 * for a small integer d and the integer n is not too
70 * much larger than 22 (the maximum integer k for which
71 * we can represent 10^k exactly), we may be able to
72 * compute (d*10^k) * 10^(e-k) with just one roundoff.
73 * 3. Rather than a bit-at-a-time adjustment of the binary
74 * result in the hard case, we use floating-point
75 * arithmetic to determine the adjustment to within
76 * one bit; only in really hard cases do we need to
77 * compute a second residual.
78 * 4. Because of 3., we don't need a large table of powers of 10
79 * for ten-to-e (just some small tables, e.g. of 10^k
84 * #define IEEE_8087 for IEEE-arithmetic machines where the least
85 * significant byte has the lowest address.
86 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
87 * significant byte has the lowest address.
88 * #define Long int on machines with 32-bit ints and 64-bit longs.
89 * #define Sudden_Underflow for IEEE-format machines without gradual
90 * underflow (i.e., that flush to zero on underflow).
91 * #define IBM for IBM mainframe-style floating-point arithmetic.
92 * #define VAX for VAX-style floating-point arithmetic (D_floating).
93 * #define No_leftright to omit left-right logic in fast floating-point
94 * computation of dtoa.
95 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
96 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
97 * that use extended-precision instructions to compute rounded
98 * products and quotients) with IBM.
99 * #define ROUND_BIASED for IEEE-format with biased rounding.
100 * #define Inaccurate_Divide for IEEE-format with correctly rounded
101 * products but inaccurate quotients, e.g., for Intel i860.
102 * #define NO_LONG_LONG on machines that do not have a "long long"
103 * integer type (of >= 64 bits). On such machines, you can
104 * #define Just_16 to store 16 bits per 32-bit Long when doing
105 * high-precision integer arithmetic. Whether this speeds things
106 * up or slows things down depends on the machine and the number
107 * being converted. If long long is available and the name is
108 * something other than "long long", #define Llong to be the name,
109 * and if "unsigned Llong" does not work as an unsigned version of
110 * Llong, #define #ULLong to be the corresponding unsigned type.
111 * #define KR_headers for old-style C function headers.
112 * #define Bad_float_h if your system lacks a float.h or if it does not
113 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
114 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
115 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
116 * if memory is available and otherwise does something you deem
117 * appropriate. If MALLOC is undefined, malloc will be invoked
118 * directly -- and assumed always to succeed.
119 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
120 * memory allocations from a private pool of memory when possible.
121 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
122 * unless #defined to be a different length. This default length
123 * suffices to get rid of MALLOC calls except for unusual cases,
124 * such as decimal-to-binary conversion of a very long string of
125 * digits. When converting IEEE double precision values, the
126 * longest string gdtoa can return is about 751 bytes long. For
127 * conversions by strtod of strings of 800 digits and all gdtoa
128 * conversions of IEEE doubles in single-threaded executions with
129 * 8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with
130 * 4-byte pointers, PRIVATE_MEM >= 7112 appears adequate.
131 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
132 * Infinity and NaN (case insensitively).
133 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
134 * strtodg also accepts (case insensitively) strings of the form
135 * NaN(x), where x is a string of hexadecimal digits and spaces;
136 * if there is only one string of hexadecimal digits, it is taken
137 * for the fraction bits of the resulting NaN; if there are two or
138 * more strings of hexadecimal digits, each string is assigned
139 * to the next available sequence of 32-bit words of fractions
140 * bits (starting with the most significant), right-aligned in
142 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
143 * multiple threads. In this case, you must provide (or suitably
144 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
145 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
146 * in pow5mult, ensures lazy evaluation of only one copy of high
147 * powers of 5; omitting this lock would introduce a small
148 * probability of wasting memory, but would otherwise be harmless.)
149 * You must also invoke freedtoa(s) to free the value s returned by
150 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
151 * #define IMPRECISE_INEXACT if you do not care about the setting of
152 * the STRTOG_Inexact bits in the special case of doing IEEE double
153 * precision conversions (which could also be done by the strtog in
155 * #define NO_HEX_FP to disable recognition of C9x's hexadecimal
156 * floating-point constants.
157 * #define -DNO_ERRNO to suppress setting errno (in strtod.c and
159 * #define NO_STRING_H to use private versions of memcpy.
160 * On some K&R systems, it may also be necessary to
161 * #define DECLARE_SIZE_T in this case.
162 * #define YES_ALIAS to permit aliasing certain double values with
163 * arrays of ULongs. This leads to slightly better code with
164 * some compilers and was always used prior to 19990916, but it
165 * is not strictly legal and can cause trouble with aggressively
166 * optimizing compilers (e.g., gcc 2.95.1 under -O2).
167 * #define USE_LOCALE to use the current locale's decimal_point value.
170 #ifndef GDTOAIMP_H_INCLUDED
171 #define GDTOAIMP_H_INCLUDED
177 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
183 #include "libc_private.h"
185 #include "namespace.h"
187 #include "un-namespace.h"
196 extern Char *MALLOC ANSI((size_t));
198 #define MALLOC malloc
203 #define Honor_FLT_ROUNDS
206 #undef Avoid_Underflow
219 #define DBL_MAX_10_EXP 308
220 #define DBL_MAX_EXP 1024
222 #define DBL_MAX 1.7976931348623157e+308
227 #define DBL_MAX_10_EXP 75
228 #define DBL_MAX_EXP 63
230 #define DBL_MAX 7.2370055773322621e+75
235 #define DBL_MAX_10_EXP 38
236 #define DBL_MAX_EXP 127
238 #define DBL_MAX 1.7014118346046923e+38
243 #define LONG_MAX 2147483647
246 #else /* ifndef Bad_float_h */
248 #endif /* Bad_float_h */
251 #define Scale_Bit 0x10
271 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
272 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
275 typedef union { double d; ULong L[2]; } U;
280 #define word0(x) ((ULong *)&x)[1]
281 #define word1(x) ((ULong *)&x)[0]
283 #define word0(x) ((ULong *)&x)[0]
284 #define word1(x) ((ULong *)&x)[1]
286 #else /* !YES_ALIAS */
288 #define word0(x) ((U*)&x)->L[1]
289 #define word1(x) ((U*)&x)->L[0]
291 #define word0(x) ((U*)&x)->L[0]
292 #define word1(x) ((U*)&x)->L[1]
294 #define dval(x) ((U*)&x)->d
295 #endif /* YES_ALIAS */
297 /* The following definition of Storeinc is appropriate for MIPS processors.
298 * An alternative that might be better on some machines is
299 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
301 #if defined(IEEE_8087) + defined(VAX)
302 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
303 ((unsigned short *)a)[0] = (unsigned short)c, a++)
305 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
306 ((unsigned short *)a)[1] = (unsigned short)c, a++)
309 /* #define P DBL_MANT_DIG */
310 /* Ten_pmax = floor(P*log(2)/log(5)) */
311 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
312 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
313 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
317 #define Exp_shift1 20
318 #define Exp_msk1 0x100000
319 #define Exp_msk11 0x100000
320 #define Exp_mask 0x7ff00000
324 #define Exp_1 0x3ff00000
325 #define Exp_11 0x3ff00000
327 #define Frac_mask 0xfffff
328 #define Frac_mask1 0xfffff
331 #define Bndry_mask 0xfffff
332 #define Bndry_mask1 0xfffff
334 #define Sign_bit 0x80000000
343 #define Flt_Rounds FLT_ROUNDS
347 #endif /*Flt_Rounds*/
349 #else /* ifndef IEEE_Arith */
350 #undef Sudden_Underflow
351 #define Sudden_Underflow
356 #define Exp_shift1 24
357 #define Exp_msk1 0x1000000
358 #define Exp_msk11 0x1000000
359 #define Exp_mask 0x7f000000
362 #define Exp_1 0x41000000
363 #define Exp_11 0x41000000
364 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
365 #define Frac_mask 0xffffff
366 #define Frac_mask1 0xffffff
369 #define Bndry_mask 0xefffff
370 #define Bndry_mask1 0xffffff
372 #define Sign_bit 0x80000000
374 #define Tiny0 0x100000
383 #define Exp_msk1 0x80
384 #define Exp_msk11 0x800000
385 #define Exp_mask 0x7f80
388 #define Exp_1 0x40800000
389 #define Exp_11 0x4080
391 #define Frac_mask 0x7fffff
392 #define Frac_mask1 0xffff007f
395 #define Bndry_mask 0xffff007f
396 #define Bndry_mask1 0xffff007f
398 #define Sign_bit 0x8000
404 #endif /* IBM, VAX */
405 #endif /* IEEE_Arith */
412 #define rounded_product(a,b) a = rnd_prod(a, b)
413 #define rounded_quotient(a,b) a = rnd_quot(a, b)
415 extern double rnd_prod(), rnd_quot();
417 extern double rnd_prod(double, double), rnd_quot(double, double);
420 #define rounded_product(a,b) a *= b
421 #define rounded_quotient(a,b) a /= b
424 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
425 #define Big1 0xffffffff
437 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
438 * This makes some inner loops simpler and sometimes saves work
439 * during multiplications, but it often seems to make things slightly
440 * slower. Hence the default is now to store 32 bits per Long.
443 #else /* long long available */
445 #define Llong long long
448 #define ULLong unsigned Llong
450 #endif /* NO_LONG_LONG */
456 #define ALL_ON 0xffffffff
461 #define ALL_ON 0xffff
464 #define MULTIPLE_THREADS
465 extern pthread_mutex_t __gdtoa_locks[2];
466 #define ACQUIRE_DTOA_LOCK(n) do { \
468 _pthread_mutex_lock(&__gdtoa_locks[n]); \
470 #define FREE_DTOA_LOCK(n) do { \
472 _pthread_mutex_unlock(&__gdtoa_locks[n]); \
480 int k, maxwds, sign, wds;
484 typedef struct Bigint Bigint;
487 #ifdef DECLARE_SIZE_T
488 typedef unsigned int size_t;
490 extern void memcpy_D2A ANSI((void*, const void*, size_t));
491 #define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
492 #else /* !NO_STRING_H */
493 #define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
494 #endif /* NO_STRING_H */
497 * Paranoia: Protect exported symbols, including ones in files we don't
498 * compile right now. The standard strtof and strtod survive.
501 #define gdtoa __gdtoa
502 #define freedtoa __freedtoa
503 #define strtodg __strtodg
504 #define g_ddfmt __g_ddfmt
505 #define g_dfmt __g_dfmt
506 #define g_ffmt __g_ffmt
507 #define g_Qfmt __g_Qfmt
508 #define g_xfmt __g_xfmt
509 #define g_xLfmt __g_xLfmt
510 #define strtoId __strtoId
511 #define strtoIdd __strtoIdd
512 #define strtoIf __strtoIf
513 #define strtoIQ __strtoIQ
514 #define strtoIx __strtoIx
515 #define strtoIxL __strtoIxL
516 #define strtord __strtord
517 #define strtordd __strtordd
518 #define strtorf __strtorf
519 #define strtorQ __strtorQ
520 #define strtorx __strtorx
521 #define strtorxL __strtorxL
522 #define strtodI __strtodI
523 #define strtopd __strtopd
524 #define strtopdd __strtopdd
525 #define strtopf __strtopf
526 #define strtopQ __strtopQ
527 #define strtopx __strtopx
528 #define strtopxL __strtopxL
530 /* Protect gdtoa-internal symbols */
531 #define Balloc __Balloc_D2A
532 #define Bfree __Bfree_D2A
533 #define ULtoQ __ULtoQ_D2A
534 #define ULtof __ULtof_D2A
535 #define ULtod __ULtod_D2A
536 #define ULtodd __ULtodd_D2A
537 #define ULtox __ULtox_D2A
538 #define ULtoxL __ULtoxL_D2A
539 #define any_on __any_on_D2A
540 #define b2d __b2d_D2A
541 #define bigtens __bigtens_D2A
542 #define cmp __cmp_D2A
543 #define copybits __copybits_D2A
544 #define d2b __d2b_D2A
545 #define decrement __decrement_D2A
546 #define diff __diff_D2A
547 #define dtoa_result __dtoa_result_D2A
548 #define g__fmt __g__fmt_D2A
549 #define gethex __gethex_D2A
550 #define hexdig __hexdig_D2A
551 #define hexdig_init_D2A __hexdig_init_D2A
552 #define hexnan __hexnan_D2A
553 #define hi0bits __hi0bits_D2A
554 #define hi0bits_D2A __hi0bits_D2A
555 #define i2b __i2b_D2A
556 #define increment __increment_D2A
557 #define lo0bits __lo0bits_D2A
558 #define lshift __lshift_D2A
559 #define match __match_D2A
560 #define mult __mult_D2A
561 #define multadd __multadd_D2A
562 #define nrv_alloc __nrv_alloc_D2A
563 #define pow5mult __pow5mult_D2A
564 #define quorem __quorem_D2A
565 #define ratio __ratio_D2A
566 #define rshift __rshift_D2A
567 #define rv_alloc __rv_alloc_D2A
568 #define s2b __s2b_D2A
569 #define set_ones __set_ones_D2A
570 #define strcp __strcp_D2A
571 #define strcp_D2A __strcp_D2A
572 #define strtoIg __strtoIg_D2A
573 #define sum __sum_D2A
574 #define tens __tens_D2A
575 #define tinytens __tinytens_D2A
576 #define tinytens __tinytens_D2A
577 #define trailz __trailz_D2A
578 #define ulp __ulp_D2A
580 extern char *dtoa_result;
581 extern CONST double bigtens[], tens[], tinytens[];
582 extern unsigned char hexdig[];
584 extern Bigint *Balloc ANSI((int));
585 extern void Bfree ANSI((Bigint*));
586 extern void ULtof ANSI((ULong*, ULong*, Long, int));
587 extern void ULtod ANSI((ULong*, ULong*, Long, int));
588 extern void ULtodd ANSI((ULong*, ULong*, Long, int));
589 extern void ULtoQ ANSI((ULong*, ULong*, Long, int));
590 extern void ULtox ANSI((UShort*, ULong*, Long, int));
591 extern void ULtoxL ANSI((ULong*, ULong*, Long, int));
592 extern ULong any_on ANSI((Bigint*, int));
593 extern double b2d ANSI((Bigint*, int*));
594 extern int cmp ANSI((Bigint*, Bigint*));
595 extern void copybits ANSI((ULong*, int, Bigint*));
596 extern Bigint *d2b ANSI((double, int*, int*));
597 extern int decrement ANSI((Bigint*));
598 extern Bigint *diff ANSI((Bigint*, Bigint*));
599 extern char *dtoa ANSI((double d, int mode, int ndigits,
600 int *decpt, int *sign, char **rve));
601 extern void freedtoa ANSI((char*));
602 extern char *gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp,
603 int mode, int ndigits, int *decpt, char **rve));
604 extern char *g__fmt ANSI((char*, char*, char*, int, ULong));
605 extern int gethex ANSI((CONST char**, FPI*, Long*, Bigint**, int));
606 extern void hexdig_init_D2A(Void);
607 extern int hexnan ANSI((CONST char**, FPI*, ULong*));
608 extern int hi0bits ANSI((ULong));
609 extern Bigint *i2b ANSI((int));
610 extern Bigint *increment ANSI((Bigint*));
611 extern int lo0bits ANSI((ULong*));
612 extern Bigint *lshift ANSI((Bigint*, int));
613 extern int match ANSI((CONST char**, char*));
614 extern Bigint *mult ANSI((Bigint*, Bigint*));
615 extern Bigint *multadd ANSI((Bigint*, int, int));
616 extern char *nrv_alloc ANSI((char*, char **, int));
617 extern Bigint *pow5mult ANSI((Bigint*, int));
618 extern int quorem ANSI((Bigint*, Bigint*));
619 extern double ratio ANSI((Bigint*, Bigint*));
620 extern void rshift ANSI((Bigint*, int));
621 extern char *rv_alloc ANSI((int));
622 extern Bigint *s2b ANSI((CONST char*, int, int, ULong));
623 extern Bigint *set_ones ANSI((Bigint*, int));
624 extern char *strcp ANSI((char*, const char*));
625 extern int strtodg ANSI((CONST char*, char**, FPI*, Long*, ULong*));
627 extern int strtoId ANSI((CONST char *, char **, double *, double *));
628 extern int strtoIdd ANSI((CONST char *, char **, double *, double *));
629 extern int strtoIf ANSI((CONST char *, char **, float *, float *));
630 extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*));
631 extern int strtoIQ ANSI((CONST char *, char **, void *, void *));
632 extern int strtoIx ANSI((CONST char *, char **, void *, void *));
633 extern int strtoIxL ANSI((CONST char *, char **, void *, void *));
634 extern double strtod ANSI((const char *s00, char **se));
635 extern int strtopQ ANSI((CONST char *, char **, Void *));
636 extern int strtopf ANSI((CONST char *, char **, float *));
637 extern int strtopd ANSI((CONST char *, char **, double *));
638 extern int strtopdd ANSI((CONST char *, char **, double *));
639 extern int strtopx ANSI((CONST char *, char **, Void *));
640 extern int strtopxL ANSI((CONST char *, char **, Void *));
641 extern int strtord ANSI((CONST char *, char **, int, double *));
642 extern int strtordd ANSI((CONST char *, char **, int, double *));
643 extern int strtorf ANSI((CONST char *, char **, int, float *));
644 extern int strtorQ ANSI((CONST char *, char **, int, void *));
645 extern int strtorx ANSI((CONST char *, char **, int, void *));
646 extern int strtorxL ANSI((CONST char *, char **, int, void *));
647 extern Bigint *sum ANSI((Bigint*, Bigint*));
648 extern int trailz ANSI((Bigint*));
649 extern double ulp ANSI((double));
655 * NAN_WORD0 and NAN_WORD1 are only referenced in strtod.c. Prior to
656 * 20050115, they used to be hard-wired here (to 0x7ff80000 and 0,
657 * respectively), but now are determined by compiling and running
658 * qnan.c to generate gd_qnan.h, which specifies d_QNAN0 and d_QNAN1.
659 * Formerly gdtoaimp.h recommended supplying suitable -DNAN_WORD0=...
660 * and -DNAN_WORD1=... values if necessary. This should still work.
661 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
668 #define NAN_WORD0 d_QNAN0
671 #define NAN_WORD1 d_QNAN1
677 #define NAN_WORD0 d_QNAN1
680 #define NAN_WORD1 d_QNAN0
688 #ifdef Sudden_Underflow
694 #endif /* GDTOAIMP_H_INCLUDED */