gdtoaimp.h revision 112158
1112158Sdas/****************************************************************
2112158Sdas
3112158SdasThe author of this software is David M. Gay.
4112158Sdas
5112158SdasCopyright (C) 1998-2000 by Lucent Technologies
6112158SdasAll Rights Reserved
7112158Sdas
8112158SdasPermission to use, copy, modify, and distribute this software and
9112158Sdasits documentation for any purpose and without fee is hereby
10112158Sdasgranted, provided that the above copyright notice appear in all
11112158Sdascopies and that both that the copyright notice and this
12112158Sdaspermission notice and warranty disclaimer appear in supporting
13112158Sdasdocumentation, and that the name of Lucent or any of its entities
14112158Sdasnot be used in advertising or publicity pertaining to
15112158Sdasdistribution of the software without specific, written prior
16112158Sdaspermission.
17112158Sdas
18112158SdasLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19112158SdasINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20112158SdasIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21112158SdasSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22112158SdasWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23112158SdasIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24112158SdasARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25112158SdasTHIS SOFTWARE.
26112158Sdas
27112158Sdas****************************************************************/
28112158Sdas
29112158Sdas/* This is a variation on dtoa.c that converts arbitary binary
30112158Sdas   floating-point formats to and from decimal notation.  It uses
31112158Sdas   double-precision arithmetic internally, so there are still
32112158Sdas   various #ifdefs that adapt the calculations to the native
33112158Sdas   double-precision arithmetic (any of IEEE, VAX D_floating,
34112158Sdas   or IBM mainframe arithmetic).
35112158Sdas
36112158Sdas   Please send bug reports to
37112158Sdas	David M. Gay
38112158Sdas	Bell Laboratories, Room 2C-463
39112158Sdas	600 Mountain Avenue
40112158Sdas	Murray Hill, NJ 07974-0636
41112158Sdas	U.S.A.
42112158Sdas	dmg@bell-labs.com
43112158Sdas */
44112158Sdas
45112158Sdas/* On a machine with IEEE extended-precision registers, it is
46112158Sdas * necessary to specify double-precision (53-bit) rounding precision
47112158Sdas * before invoking strtod or dtoa.  If the machine uses (the equivalent
48112158Sdas * of) Intel 80x87 arithmetic, the call
49112158Sdas *	_control87(PC_53, MCW_PC);
50112158Sdas * does this with many compilers.  Whether this or another call is
51112158Sdas * appropriate depends on the compiler; for this to work, it may be
52112158Sdas * necessary to #include "float.h" or another system-dependent header
53112158Sdas * file.
54112158Sdas */
55112158Sdas
56112158Sdas/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
57112158Sdas *
58112158Sdas * This strtod returns a nearest machine number to the input decimal
59112158Sdas * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
60112158Sdas * broken by the IEEE round-even rule.  Otherwise ties are broken by
61112158Sdas * biased rounding (add half and chop).
62112158Sdas *
63112158Sdas * Inspired loosely by William D. Clinger's paper "How to Read Floating
64112158Sdas * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
65112158Sdas *
66112158Sdas * Modifications:
67112158Sdas *
68112158Sdas *	1. We only require IEEE, IBM, or VAX double-precision
69112158Sdas *		arithmetic (not IEEE double-extended).
70112158Sdas *	2. We get by with floating-point arithmetic in a case that
71112158Sdas *		Clinger missed -- when we're computing d * 10^n
72112158Sdas *		for a small integer d and the integer n is not too
73112158Sdas *		much larger than 22 (the maximum integer k for which
74112158Sdas *		we can represent 10^k exactly), we may be able to
75112158Sdas *		compute (d*10^k) * 10^(e-k) with just one roundoff.
76112158Sdas *	3. Rather than a bit-at-a-time adjustment of the binary
77112158Sdas *		result in the hard case, we use floating-point
78112158Sdas *		arithmetic to determine the adjustment to within
79112158Sdas *		one bit; only in really hard cases do we need to
80112158Sdas *		compute a second residual.
81112158Sdas *	4. Because of 3., we don't need a large table of powers of 10
82112158Sdas *		for ten-to-e (just some small tables, e.g. of 10^k
83112158Sdas *		for 0 <= k <= 22).
84112158Sdas */
85112158Sdas
86112158Sdas/*
87112158Sdas * #define IEEE_8087 for IEEE-arithmetic machines where the least
88112158Sdas *	significant byte has the lowest address.
89112158Sdas * #define IEEE_MC68k for IEEE-arithmetic machines where the most
90112158Sdas *	significant byte has the lowest address.
91112158Sdas * #define Long int on machines with 32-bit ints and 64-bit longs.
92112158Sdas * #define Sudden_Underflow for IEEE-format machines without gradual
93112158Sdas *	underflow (i.e., that flush to zero on underflow).
94112158Sdas * #define IBM for IBM mainframe-style floating-point arithmetic.
95112158Sdas * #define VAX for VAX-style floating-point arithmetic (D_floating).
96112158Sdas * #define No_leftright to omit left-right logic in fast floating-point
97112158Sdas *	computation of dtoa.
98112158Sdas * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
99112158Sdas * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
100112158Sdas *	that use extended-precision instructions to compute rounded
101112158Sdas *	products and quotients) with IBM.
102112158Sdas * #define ROUND_BIASED for IEEE-format with biased rounding.
103112158Sdas * #define Inaccurate_Divide for IEEE-format with correctly rounded
104112158Sdas *	products but inaccurate quotients, e.g., for Intel i860.
105112158Sdas * #define NO_LONG_LONG on machines that do not have a "long long"
106112158Sdas *	integer type (of >= 64 bits).  On such machines, you can
107112158Sdas *	#define Just_16 to store 16 bits per 32-bit Long when doing
108112158Sdas *	high-precision integer arithmetic.  Whether this speeds things
109112158Sdas *	up or slows things down depends on the machine and the number
110112158Sdas *	being converted.  If long long is available and the name is
111112158Sdas *	something other than "long long", #define Llong to be the name,
112112158Sdas *	and if "unsigned Llong" does not work as an unsigned version of
113112158Sdas *	Llong, #define #ULLong to be the corresponding unsigned type.
114112158Sdas * #define KR_headers for old-style C function headers.
115112158Sdas * #define Bad_float_h if your system lacks a float.h or if it does not
116112158Sdas *	define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
117112158Sdas *	FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
118112158Sdas * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
119112158Sdas *	if memory is available and otherwise does something you deem
120112158Sdas *	appropriate.  If MALLOC is undefined, malloc will be invoked
121112158Sdas *	directly -- and assumed always to succeed.
122112158Sdas * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
123112158Sdas *	memory allocations from a private pool of memory when possible.
124112158Sdas *	When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
125112158Sdas *	unless #defined to be a different length.  This default length
126112158Sdas *	suffices to get rid of MALLOC calls except for unusual cases,
127112158Sdas *	such as decimal-to-binary conversion of a very long string of
128112158Sdas *	digits.  When converting IEEE double precision values, the
129112158Sdas *	longest string gdtoa can return is about 751 bytes long.  For
130112158Sdas *	conversions by strtod of strings of 800 digits and all gdtoa
131112158Sdas *	conversions of IEEE doubles in single-threaded executions with
132112158Sdas *	8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with
133112158Sdas *	4-byte pointers, PRIVATE_MEM >= 7112 appears adequate.
134112158Sdas * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
135112158Sdas *	Infinity and NaN (case insensitively).  On some systems (e.g.,
136112158Sdas *	some HP systems), it may be necessary to #define NAN_WORD0
137112158Sdas *	appropriately -- to the most significant word of a quiet NaN.
138112158Sdas *	(On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
139112158Sdas *	When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
140112158Sdas *	strtodg also accepts (case insensitively) strings of the form
141112158Sdas *	NaN(x), where x is a string of hexadecimal digits and spaces;
142112158Sdas *	if there is only one string of hexadecimal digits, it is taken
143112158Sdas *	for the fraction bits of the resulting NaN; if there are two or
144112158Sdas *	more strings of hexadecimal digits, each string is assigned
145112158Sdas *	to the next available sequence of 32-bit words of fractions
146112158Sdas *	bits (starting with the most significant), right-aligned in
147112158Sdas *	each sequence.
148112158Sdas * #define MULTIPLE_THREADS if the system offers preemptively scheduled
149112158Sdas *	multiple threads.  In this case, you must provide (or suitably
150112158Sdas *	#define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
151112158Sdas *	by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
152112158Sdas *	in pow5mult, ensures lazy evaluation of only one copy of high
153112158Sdas *	powers of 5; omitting this lock would introduce a small
154112158Sdas *	probability of wasting memory, but would otherwise be harmless.)
155112158Sdas *	You must also invoke freedtoa(s) to free the value s returned by
156112158Sdas *	dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
157112158Sdas * #define IMPRECISE_INEXACT if you do not care about the setting of
158112158Sdas *	the STRTOG_Inexact bits in the special case of doing IEEE double
159112158Sdas *	precision conversions (which could also be done by the strtog in
160112158Sdas *	dtoa.c).
161112158Sdas * #define NO_HEX_FP to disable recognition of C9x's hexadecimal
162112158Sdas *	floating-point constants.
163112158Sdas * #define -DNO_ERRNO to suppress setting errno (in strtod.c and
164112158Sdas *	strtodg.c).
165112158Sdas * #define NO_STRING_H to use private versions of memcpy.
166112158Sdas *	On some K&R systems, it may also be necessary to
167112158Sdas *	#define DECLARE_SIZE_T in this case.
168112158Sdas * #define YES_ALIAS to permit aliasing certain double values with
169112158Sdas *	arrays of ULongs.  This leads to slightly better code with
170112158Sdas *	some compilers and was always used prior to 19990916, but it
171112158Sdas *	is not strictly legal and can cause trouble with aggressively
172112158Sdas *	optimizing compilers (e.g., gcc 2.95.1 under -O2).
173112158Sdas * #define USE_LOCALE to use the current locale's decimal_point value.
174112158Sdas */
175112158Sdas
176112158Sdas#ifndef GDTOAIMP_H_INCLUDED
177112158Sdas#define GDTOAIMP_H_INCLUDED
178112158Sdas#include "gdtoa.h"
179112158Sdas
180112158Sdas#ifdef DEBUG
181112158Sdas#include "stdio.h"
182112158Sdas#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
183112158Sdas#endif
184112158Sdas
185112158Sdas#include "stdlib.h"
186112158Sdas#include "string.h"
187112158Sdas
188112158Sdas#ifdef KR_headers
189112158Sdas#define Char char
190112158Sdas#else
191112158Sdas#define Char void
192112158Sdas#endif
193112158Sdas
194112158Sdas#ifdef MALLOC
195112158Sdasextern Char *MALLOC ANSI((size_t));
196112158Sdas#else
197112158Sdas#define MALLOC malloc
198112158Sdas#endif
199112158Sdas
200112158Sdas#undef IEEE_Arith
201112158Sdas#undef Avoid_Underflow
202112158Sdas#ifdef IEEE_MC68k
203112158Sdas#define IEEE_Arith
204112158Sdas#endif
205112158Sdas#ifdef IEEE_8087
206112158Sdas#define IEEE_Arith
207112158Sdas#endif
208112158Sdas
209112158Sdas#include "errno.h"
210112158Sdas#ifdef Bad_float_h
211112158Sdas
212112158Sdas#ifdef IEEE_Arith
213112158Sdas#define DBL_DIG 15
214112158Sdas#define DBL_MAX_10_EXP 308
215112158Sdas#define DBL_MAX_EXP 1024
216112158Sdas#define FLT_RADIX 2
217112158Sdas#define DBL_MAX 1.7976931348623157e+308
218112158Sdas#endif
219112158Sdas
220112158Sdas#ifdef IBM
221112158Sdas#define DBL_DIG 16
222112158Sdas#define DBL_MAX_10_EXP 75
223112158Sdas#define DBL_MAX_EXP 63
224112158Sdas#define FLT_RADIX 16
225112158Sdas#define DBL_MAX 7.2370055773322621e+75
226112158Sdas#endif
227112158Sdas
228112158Sdas#ifdef VAX
229112158Sdas#define DBL_DIG 16
230112158Sdas#define DBL_MAX_10_EXP 38
231112158Sdas#define DBL_MAX_EXP 127
232112158Sdas#define FLT_RADIX 2
233112158Sdas#define DBL_MAX 1.7014118346046923e+38
234112158Sdas#define n_bigtens 2
235112158Sdas#endif
236112158Sdas
237112158Sdas#ifndef LONG_MAX
238112158Sdas#define LONG_MAX 2147483647
239112158Sdas#endif
240112158Sdas
241112158Sdas#else /* ifndef Bad_float_h */
242112158Sdas#include "float.h"
243112158Sdas#endif /* Bad_float_h */
244112158Sdas
245112158Sdas#ifdef IEEE_Arith
246112158Sdas#define Scale_Bit 0x10
247112158Sdas#define n_bigtens 5
248112158Sdas#endif
249112158Sdas
250112158Sdas#ifdef IBM
251112158Sdas#define n_bigtens 3
252112158Sdas#endif
253112158Sdas
254112158Sdas#ifdef VAX
255112158Sdas#define n_bigtens 2
256112158Sdas#endif
257112158Sdas
258112158Sdas#ifndef __MATH_H__
259112158Sdas#include "math.h"
260112158Sdas#endif
261112158Sdas
262112158Sdas#ifdef __cplusplus
263112158Sdasextern "C" {
264112158Sdas#endif
265112158Sdas
266112158Sdas#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
267112158SdasExactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
268112158Sdas#endif
269112158Sdas
270112158Sdastypedef union { double d; ULong L[2]; } U;
271112158Sdas
272112158Sdas#ifdef YES_ALIAS
273112158Sdas#define dval(x) x
274112158Sdas#ifdef IEEE_8087
275112158Sdas#define word0(x) ((ULong *)&x)[1]
276112158Sdas#define word1(x) ((ULong *)&x)[0]
277112158Sdas#else
278112158Sdas#define word0(x) ((ULong *)&x)[0]
279112158Sdas#define word1(x) ((ULong *)&x)[1]
280112158Sdas#endif
281112158Sdas#else /* !YES_ALIAS */
282112158Sdas#ifdef IEEE_8087
283112158Sdas#define word0(x) ((U*)&x)->L[1]
284112158Sdas#define word1(x) ((U*)&x)->L[0]
285112158Sdas#else
286112158Sdas#define word0(x) ((U*)&x)->L[0]
287112158Sdas#define word1(x) ((U*)&x)->L[1]
288112158Sdas#endif
289112158Sdas#define dval(x) ((U*)&x)->d
290112158Sdas#endif /* YES_ALIAS */
291112158Sdas
292112158Sdas/* The following definition of Storeinc is appropriate for MIPS processors.
293112158Sdas * An alternative that might be better on some machines is
294112158Sdas * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
295112158Sdas */
296112158Sdas#if defined(IEEE_8087) + defined(VAX)
297112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
298112158Sdas((unsigned short *)a)[0] = (unsigned short)c, a++)
299112158Sdas#else
300112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
301112158Sdas((unsigned short *)a)[1] = (unsigned short)c, a++)
302112158Sdas#endif
303112158Sdas
304112158Sdas/* #define P DBL_MANT_DIG */
305112158Sdas/* Ten_pmax = floor(P*log(2)/log(5)) */
306112158Sdas/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
307112158Sdas/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
308112158Sdas/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
309112158Sdas
310112158Sdas#ifdef IEEE_Arith
311112158Sdas#define Exp_shift  20
312112158Sdas#define Exp_shift1 20
313112158Sdas#define Exp_msk1    0x100000
314112158Sdas#define Exp_msk11   0x100000
315112158Sdas#define Exp_mask  0x7ff00000
316112158Sdas#define P 53
317112158Sdas#define Bias 1023
318112158Sdas#define Emin (-1022)
319112158Sdas#define Exp_1  0x3ff00000
320112158Sdas#define Exp_11 0x3ff00000
321112158Sdas#define Ebits 11
322112158Sdas#define Frac_mask  0xfffff
323112158Sdas#define Frac_mask1 0xfffff
324112158Sdas#define Ten_pmax 22
325112158Sdas#define Bletch 0x10
326112158Sdas#define Bndry_mask  0xfffff
327112158Sdas#define Bndry_mask1 0xfffff
328112158Sdas#define LSB 1
329112158Sdas#define Sign_bit 0x80000000
330112158Sdas#define Log2P 1
331112158Sdas#define Tiny0 0
332112158Sdas#define Tiny1 1
333112158Sdas#define Quick_max 14
334112158Sdas#define Int_max 14
335112158Sdas
336112158Sdas#ifndef Flt_Rounds
337112158Sdas#ifdef FLT_ROUNDS
338112158Sdas#define Flt_Rounds FLT_ROUNDS
339112158Sdas#else
340112158Sdas#define Flt_Rounds 1
341112158Sdas#endif
342112158Sdas#endif /*Flt_Rounds*/
343112158Sdas
344112158Sdas#else /* ifndef IEEE_Arith */
345112158Sdas#undef  Sudden_Underflow
346112158Sdas#define Sudden_Underflow
347112158Sdas#ifdef IBM
348112158Sdas#undef Flt_Rounds
349112158Sdas#define Flt_Rounds 0
350112158Sdas#define Exp_shift  24
351112158Sdas#define Exp_shift1 24
352112158Sdas#define Exp_msk1   0x1000000
353112158Sdas#define Exp_msk11  0x1000000
354112158Sdas#define Exp_mask  0x7f000000
355112158Sdas#define P 14
356112158Sdas#define Bias 65
357112158Sdas#define Exp_1  0x41000000
358112158Sdas#define Exp_11 0x41000000
359112158Sdas#define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
360112158Sdas#define Frac_mask  0xffffff
361112158Sdas#define Frac_mask1 0xffffff
362112158Sdas#define Bletch 4
363112158Sdas#define Ten_pmax 22
364112158Sdas#define Bndry_mask  0xefffff
365112158Sdas#define Bndry_mask1 0xffffff
366112158Sdas#define LSB 1
367112158Sdas#define Sign_bit 0x80000000
368112158Sdas#define Log2P 4
369112158Sdas#define Tiny0 0x100000
370112158Sdas#define Tiny1 0
371112158Sdas#define Quick_max 14
372112158Sdas#define Int_max 15
373112158Sdas#else /* VAX */
374112158Sdas#undef Flt_Rounds
375112158Sdas#define Flt_Rounds 1
376112158Sdas#define Exp_shift  23
377112158Sdas#define Exp_shift1 7
378112158Sdas#define Exp_msk1    0x80
379112158Sdas#define Exp_msk11   0x800000
380112158Sdas#define Exp_mask  0x7f80
381112158Sdas#define P 56
382112158Sdas#define Bias 129
383112158Sdas#define Exp_1  0x40800000
384112158Sdas#define Exp_11 0x4080
385112158Sdas#define Ebits 8
386112158Sdas#define Frac_mask  0x7fffff
387112158Sdas#define Frac_mask1 0xffff007f
388112158Sdas#define Ten_pmax 24
389112158Sdas#define Bletch 2
390112158Sdas#define Bndry_mask  0xffff007f
391112158Sdas#define Bndry_mask1 0xffff007f
392112158Sdas#define LSB 0x10000
393112158Sdas#define Sign_bit 0x8000
394112158Sdas#define Log2P 1
395112158Sdas#define Tiny0 0x80
396112158Sdas#define Tiny1 0
397112158Sdas#define Quick_max 15
398112158Sdas#define Int_max 15
399112158Sdas#endif /* IBM, VAX */
400112158Sdas#endif /* IEEE_Arith */
401112158Sdas
402112158Sdas#ifndef IEEE_Arith
403112158Sdas#define ROUND_BIASED
404112158Sdas#endif
405112158Sdas
406112158Sdas#ifdef RND_PRODQUOT
407112158Sdas#define rounded_product(a,b) a = rnd_prod(a, b)
408112158Sdas#define rounded_quotient(a,b) a = rnd_quot(a, b)
409112158Sdas#ifdef KR_headers
410112158Sdasextern double rnd_prod(), rnd_quot();
411112158Sdas#else
412112158Sdasextern double rnd_prod(double, double), rnd_quot(double, double);
413112158Sdas#endif
414112158Sdas#else
415112158Sdas#define rounded_product(a,b) a *= b
416112158Sdas#define rounded_quotient(a,b) a /= b
417112158Sdas#endif
418112158Sdas
419112158Sdas#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
420112158Sdas#define Big1 0xffffffff
421112158Sdas
422112158Sdas#undef  Pack_16
423112158Sdas#ifndef Pack_32
424112158Sdas#define Pack_32
425112158Sdas#endif
426112158Sdas
427112158Sdas#ifdef NO_LONG_LONG
428112158Sdas#undef ULLong
429112158Sdas#ifdef Just_16
430112158Sdas#undef Pack_32
431112158Sdas#define Pack_16
432112158Sdas/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
433112158Sdas * This makes some inner loops simpler and sometimes saves work
434112158Sdas * during multiplications, but it often seems to make things slightly
435112158Sdas * slower.  Hence the default is now to store 32 bits per Long.
436112158Sdas */
437112158Sdas#endif
438112158Sdas#else	/* long long available */
439112158Sdas#ifndef Llong
440112158Sdas#define Llong long long
441112158Sdas#endif
442112158Sdas#ifndef ULLong
443112158Sdas#define ULLong unsigned Llong
444112158Sdas#endif
445112158Sdas#endif /* NO_LONG_LONG */
446112158Sdas
447112158Sdas#ifdef Pack_32
448112158Sdas#define ULbits 32
449112158Sdas#define kshift 5
450112158Sdas#define kmask 31
451112158Sdas#define ALL_ON 0xffffffff
452112158Sdas#else
453112158Sdas#define ULbits 16
454112158Sdas#define kshift 4
455112158Sdas#define kmask 15
456112158Sdas#define ALL_ON 0xffff
457112158Sdas#endif
458112158Sdas
459112158Sdas#ifndef MULTIPLE_THREADS
460112158Sdas#define ACQUIRE_DTOA_LOCK(n)	/*nothing*/
461112158Sdas#define FREE_DTOA_LOCK(n)	/*nothing*/
462112158Sdas#endif
463112158Sdas
464112158Sdas#define Kmax 15
465112158Sdas
466112158Sdas struct
467112158SdasBigint {
468112158Sdas	struct Bigint *next;
469112158Sdas	int k, maxwds, sign, wds;
470112158Sdas	ULong x[1];
471112158Sdas	};
472112158Sdas
473112158Sdas typedef struct Bigint Bigint;
474112158Sdas
475112158Sdas#ifdef NO_STRING_H
476112158Sdas#ifdef DECLARE_SIZE_T
477112158Sdastypedef unsigned int size_t;
478112158Sdas#endif
479112158Sdasextern void memcpy_D2A ANSI((void*, const void*, size_t));
480112158Sdas#define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
481112158Sdas#else /* !NO_STRING_H */
482112158Sdas#define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
483112158Sdas#endif /* NO_STRING_H */
484112158Sdas
485112158Sdas#define Balloc Balloc_D2A
486112158Sdas#define Bfree Bfree_D2A
487112158Sdas#define ULtoQ ULtoQ_D2A
488112158Sdas#define ULtof ULtof_D2A
489112158Sdas#define ULtod ULtod_D2A
490112158Sdas#define ULtodd ULtodd_D2A
491112158Sdas#define ULtox ULtox_D2A
492112158Sdas#define ULtoxL ULtoxL_D2A
493112158Sdas#define any_on any_on_D2A
494112158Sdas#define b2d b2d_D2A
495112158Sdas#define bigtens bigtens_D2A
496112158Sdas#define cmp cmp_D2A
497112158Sdas#define copybits copybits_D2A
498112158Sdas#define d2b d2b_D2A
499112158Sdas#define decrement decrement_D2A
500112158Sdas#define diff diff_D2A
501112158Sdas#define dtoa_result dtoa_result_D2A
502112158Sdas#define g__fmt g__fmt_D2A
503112158Sdas#define gethex gethex_D2A
504112158Sdas#define hexdig hexdig_D2A
505112158Sdas#define hexnan hexnan_D2A
506112158Sdas#define hi0bits hi0bits_D2A
507112158Sdas#define i2b i2b_D2A
508112158Sdas#define increment increment_D2A
509112158Sdas#define lo0bits lo0bits_D2A
510112158Sdas#define lshift lshift_D2A
511112158Sdas#define match match_D2A
512112158Sdas#define mult mult_D2A
513112158Sdas#define multadd multadd_D2A
514112158Sdas#define nrv_alloc nrv_alloc_D2A
515112158Sdas#define pow5mult pow5mult_D2A
516112158Sdas#define quorem quorem_D2A
517112158Sdas#define ratio ratio_D2A
518112158Sdas#define rshift rshift_D2A
519112158Sdas#define rv_alloc rv_alloc_D2A
520112158Sdas#define s2b s2b_D2A
521112158Sdas#define set_ones set_ones_D2A
522112158Sdas#define strcp strcp_D2A
523112158Sdas#define strtoIg strtoIg_D2A
524112158Sdas#define sum sum_D2A
525112158Sdas#define tens tens_D2A
526112158Sdas#define tinytens tinytens_D2A
527112158Sdas#define tinytens tinytens_D2A
528112158Sdas#define trailz trailz_D2A
529112158Sdas#define ulp ulp_D2A
530112158Sdas
531112158Sdas extern char *dtoa_result;
532112158Sdas extern CONST double bigtens[], tens[], tinytens[];
533112158Sdas extern unsigned char hexdig[];
534112158Sdas
535112158Sdas extern Bigint *Balloc ANSI((int));
536112158Sdas extern void Bfree ANSI((Bigint*));
537112158Sdas extern void ULtof ANSI((ULong*, ULong*, Long, int));
538112158Sdas extern void ULtod ANSI((ULong*, ULong*, Long, int));
539112158Sdas extern void ULtodd ANSI((ULong*, ULong*, Long, int));
540112158Sdas extern void ULtoQ ANSI((ULong*, ULong*, Long, int));
541112158Sdas extern void ULtox ANSI((UShort*, ULong*, Long, int));
542112158Sdas extern void ULtoxL ANSI((ULong*, ULong*, Long, int));
543112158Sdas extern ULong any_on ANSI((Bigint*, int));
544112158Sdas extern double b2d ANSI((Bigint*, int*));
545112158Sdas extern int cmp ANSI((Bigint*, Bigint*));
546112158Sdas extern void copybits ANSI((ULong*, int, Bigint*));
547112158Sdas extern Bigint *d2b ANSI((double, int*, int*));
548112158Sdas extern int decrement ANSI((Bigint*));
549112158Sdas extern Bigint *diff ANSI((Bigint*, Bigint*));
550112158Sdas extern char *dtoa ANSI((double d, int mode, int ndigits,
551112158Sdas			int *decpt, int *sign, char **rve));
552112158Sdas extern char *g__fmt ANSI((char*, char*, char*, int, ULong));
553112158Sdas extern int gethex ANSI((CONST char**, FPI*, Long*, Bigint**, int));
554112158Sdas extern void hexdig_init_D2A(Void);
555112158Sdas extern int hexnan ANSI((CONST char**, FPI*, ULong*));
556112158Sdas extern int hi0bits ANSI((ULong));
557112158Sdas extern Bigint *i2b ANSI((int));
558112158Sdas extern Bigint *increment ANSI((Bigint*));
559112158Sdas extern int lo0bits ANSI((ULong*));
560112158Sdas extern Bigint *lshift ANSI((Bigint*, int));
561112158Sdas extern int match ANSI((CONST char**, char*));
562112158Sdas extern Bigint *mult ANSI((Bigint*, Bigint*));
563112158Sdas extern Bigint *multadd ANSI((Bigint*, int, int));
564112158Sdas extern char *nrv_alloc ANSI((char*, char **, int));
565112158Sdas extern Bigint *pow5mult ANSI((Bigint*, int));
566112158Sdas extern int quorem ANSI((Bigint*, Bigint*));
567112158Sdas extern double ratio ANSI((Bigint*, Bigint*));
568112158Sdas extern void rshift ANSI((Bigint*, int));
569112158Sdas extern char *rv_alloc ANSI((int));
570112158Sdas extern Bigint *s2b ANSI((CONST char*, int, int, ULong));
571112158Sdas extern Bigint *set_ones ANSI((Bigint*, int));
572112158Sdas extern char *strcp ANSI((char*, const char*));
573112158Sdas extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*));
574112158Sdas extern double strtod ANSI((const char *s00, char **se));
575112158Sdas extern Bigint *sum ANSI((Bigint*, Bigint*));
576112158Sdas extern int trailz ANSI((Bigint*));
577112158Sdas extern double ulp ANSI((double));
578112158Sdas
579112158Sdas#ifdef __cplusplus
580112158Sdas}
581112158Sdas#endif
582112158Sdas
583112158Sdas
584112158Sdas#ifdef IEEE_Arith
585112158Sdas#ifdef IEEE_MC68k
586112158Sdas#define _0 0
587112158Sdas#define _1 1
588112158Sdas#else
589112158Sdas#define _0 1
590112158Sdas#define _1 0
591112158Sdas#endif
592112158Sdas#else
593112158Sdas#undef INFNAN_CHECK
594112158Sdas#endif
595112158Sdas
596112158Sdas#ifdef INFNAN_CHECK
597112158Sdas
598112158Sdas#ifndef NAN_WORD0
599112158Sdas#define NAN_WORD0 0x7ff80000
600112158Sdas#endif
601112158Sdas
602112158Sdas#ifndef NAN_WORD1
603112158Sdas#define NAN_WORD1 0
604112158Sdas#endif
605112158Sdas#endif	/* INFNAN_CHECK */
606112158Sdas
607112158Sdas#undef SI
608112158Sdas#ifdef Sudden_Underflow
609112158Sdas#define SI 1
610112158Sdas#else
611112158Sdas#define SI 0
612112158Sdas#endif
613112158Sdas
614112158Sdas#endif /* GDTOAIMP_H_INCLUDED */
615