gdtoaimp.h revision 112161
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
29112161Sdas/* $FreeBSD: head/contrib/gdtoa/gdtoaimp.h 112161 2003-03-12 20:20:22Z das $ */
30112161Sdas
31112158Sdas/* This is a variation on dtoa.c that converts arbitary binary
32112158Sdas   floating-point formats to and from decimal notation.  It uses
33112158Sdas   double-precision arithmetic internally, so there are still
34112158Sdas   various #ifdefs that adapt the calculations to the native
35112158Sdas   double-precision arithmetic (any of IEEE, VAX D_floating,
36112158Sdas   or IBM mainframe arithmetic).
37112158Sdas
38112158Sdas   Please send bug reports to
39112158Sdas	David M. Gay
40112158Sdas	Bell Laboratories, Room 2C-463
41112158Sdas	600 Mountain Avenue
42112158Sdas	Murray Hill, NJ 07974-0636
43112158Sdas	U.S.A.
44112158Sdas	dmg@bell-labs.com
45112158Sdas */
46112158Sdas
47112158Sdas/* On a machine with IEEE extended-precision registers, it is
48112158Sdas * necessary to specify double-precision (53-bit) rounding precision
49112158Sdas * before invoking strtod or dtoa.  If the machine uses (the equivalent
50112158Sdas * of) Intel 80x87 arithmetic, the call
51112158Sdas *	_control87(PC_53, MCW_PC);
52112158Sdas * does this with many compilers.  Whether this or another call is
53112158Sdas * appropriate depends on the compiler; for this to work, it may be
54112158Sdas * necessary to #include "float.h" or another system-dependent header
55112158Sdas * file.
56112158Sdas */
57112158Sdas
58112158Sdas/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
59112158Sdas *
60112158Sdas * This strtod returns a nearest machine number to the input decimal
61112158Sdas * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
62112158Sdas * broken by the IEEE round-even rule.  Otherwise ties are broken by
63112158Sdas * biased rounding (add half and chop).
64112158Sdas *
65112158Sdas * Inspired loosely by William D. Clinger's paper "How to Read Floating
66112158Sdas * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
67112158Sdas *
68112158Sdas * Modifications:
69112158Sdas *
70112158Sdas *	1. We only require IEEE, IBM, or VAX double-precision
71112158Sdas *		arithmetic (not IEEE double-extended).
72112158Sdas *	2. We get by with floating-point arithmetic in a case that
73112158Sdas *		Clinger missed -- when we're computing d * 10^n
74112158Sdas *		for a small integer d and the integer n is not too
75112158Sdas *		much larger than 22 (the maximum integer k for which
76112158Sdas *		we can represent 10^k exactly), we may be able to
77112158Sdas *		compute (d*10^k) * 10^(e-k) with just one roundoff.
78112158Sdas *	3. Rather than a bit-at-a-time adjustment of the binary
79112158Sdas *		result in the hard case, we use floating-point
80112158Sdas *		arithmetic to determine the adjustment to within
81112158Sdas *		one bit; only in really hard cases do we need to
82112158Sdas *		compute a second residual.
83112158Sdas *	4. Because of 3., we don't need a large table of powers of 10
84112158Sdas *		for ten-to-e (just some small tables, e.g. of 10^k
85112158Sdas *		for 0 <= k <= 22).
86112158Sdas */
87112158Sdas
88112158Sdas/*
89112158Sdas * #define IEEE_8087 for IEEE-arithmetic machines where the least
90112158Sdas *	significant byte has the lowest address.
91112158Sdas * #define IEEE_MC68k for IEEE-arithmetic machines where the most
92112158Sdas *	significant byte has the lowest address.
93112158Sdas * #define Long int on machines with 32-bit ints and 64-bit longs.
94112158Sdas * #define Sudden_Underflow for IEEE-format machines without gradual
95112158Sdas *	underflow (i.e., that flush to zero on underflow).
96112158Sdas * #define IBM for IBM mainframe-style floating-point arithmetic.
97112158Sdas * #define VAX for VAX-style floating-point arithmetic (D_floating).
98112158Sdas * #define No_leftright to omit left-right logic in fast floating-point
99112158Sdas *	computation of dtoa.
100112158Sdas * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
101112158Sdas * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
102112158Sdas *	that use extended-precision instructions to compute rounded
103112158Sdas *	products and quotients) with IBM.
104112158Sdas * #define ROUND_BIASED for IEEE-format with biased rounding.
105112158Sdas * #define Inaccurate_Divide for IEEE-format with correctly rounded
106112158Sdas *	products but inaccurate quotients, e.g., for Intel i860.
107112158Sdas * #define NO_LONG_LONG on machines that do not have a "long long"
108112158Sdas *	integer type (of >= 64 bits).  On such machines, you can
109112158Sdas *	#define Just_16 to store 16 bits per 32-bit Long when doing
110112158Sdas *	high-precision integer arithmetic.  Whether this speeds things
111112158Sdas *	up or slows things down depends on the machine and the number
112112158Sdas *	being converted.  If long long is available and the name is
113112158Sdas *	something other than "long long", #define Llong to be the name,
114112158Sdas *	and if "unsigned Llong" does not work as an unsigned version of
115112158Sdas *	Llong, #define #ULLong to be the corresponding unsigned type.
116112158Sdas * #define KR_headers for old-style C function headers.
117112158Sdas * #define Bad_float_h if your system lacks a float.h or if it does not
118112158Sdas *	define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
119112158Sdas *	FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
120112158Sdas * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
121112158Sdas *	if memory is available and otherwise does something you deem
122112158Sdas *	appropriate.  If MALLOC is undefined, malloc will be invoked
123112158Sdas *	directly -- and assumed always to succeed.
124112158Sdas * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
125112158Sdas *	memory allocations from a private pool of memory when possible.
126112158Sdas *	When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
127112158Sdas *	unless #defined to be a different length.  This default length
128112158Sdas *	suffices to get rid of MALLOC calls except for unusual cases,
129112158Sdas *	such as decimal-to-binary conversion of a very long string of
130112158Sdas *	digits.  When converting IEEE double precision values, the
131112158Sdas *	longest string gdtoa can return is about 751 bytes long.  For
132112158Sdas *	conversions by strtod of strings of 800 digits and all gdtoa
133112158Sdas *	conversions of IEEE doubles in single-threaded executions with
134112158Sdas *	8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with
135112158Sdas *	4-byte pointers, PRIVATE_MEM >= 7112 appears adequate.
136112158Sdas * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
137112158Sdas *	Infinity and NaN (case insensitively).  On some systems (e.g.,
138112158Sdas *	some HP systems), it may be necessary to #define NAN_WORD0
139112158Sdas *	appropriately -- to the most significant word of a quiet NaN.
140112158Sdas *	(On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
141112158Sdas *	When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
142112158Sdas *	strtodg also accepts (case insensitively) strings of the form
143112158Sdas *	NaN(x), where x is a string of hexadecimal digits and spaces;
144112158Sdas *	if there is only one string of hexadecimal digits, it is taken
145112158Sdas *	for the fraction bits of the resulting NaN; if there are two or
146112158Sdas *	more strings of hexadecimal digits, each string is assigned
147112158Sdas *	to the next available sequence of 32-bit words of fractions
148112158Sdas *	bits (starting with the most significant), right-aligned in
149112158Sdas *	each sequence.
150112158Sdas * #define MULTIPLE_THREADS if the system offers preemptively scheduled
151112158Sdas *	multiple threads.  In this case, you must provide (or suitably
152112158Sdas *	#define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
153112158Sdas *	by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
154112158Sdas *	in pow5mult, ensures lazy evaluation of only one copy of high
155112158Sdas *	powers of 5; omitting this lock would introduce a small
156112158Sdas *	probability of wasting memory, but would otherwise be harmless.)
157112158Sdas *	You must also invoke freedtoa(s) to free the value s returned by
158112158Sdas *	dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
159112158Sdas * #define IMPRECISE_INEXACT if you do not care about the setting of
160112158Sdas *	the STRTOG_Inexact bits in the special case of doing IEEE double
161112158Sdas *	precision conversions (which could also be done by the strtog in
162112158Sdas *	dtoa.c).
163112158Sdas * #define NO_HEX_FP to disable recognition of C9x's hexadecimal
164112158Sdas *	floating-point constants.
165112158Sdas * #define -DNO_ERRNO to suppress setting errno (in strtod.c and
166112158Sdas *	strtodg.c).
167112158Sdas * #define NO_STRING_H to use private versions of memcpy.
168112158Sdas *	On some K&R systems, it may also be necessary to
169112158Sdas *	#define DECLARE_SIZE_T in this case.
170112158Sdas * #define YES_ALIAS to permit aliasing certain double values with
171112158Sdas *	arrays of ULongs.  This leads to slightly better code with
172112158Sdas *	some compilers and was always used prior to 19990916, but it
173112158Sdas *	is not strictly legal and can cause trouble with aggressively
174112158Sdas *	optimizing compilers (e.g., gcc 2.95.1 under -O2).
175112158Sdas * #define USE_LOCALE to use the current locale's decimal_point value.
176112158Sdas */
177112158Sdas
178112158Sdas#ifndef GDTOAIMP_H_INCLUDED
179112158Sdas#define GDTOAIMP_H_INCLUDED
180112158Sdas#include "gdtoa.h"
181112158Sdas
182112158Sdas#ifdef DEBUG
183112158Sdas#include "stdio.h"
184112158Sdas#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
185112158Sdas#endif
186112158Sdas
187112161Sdas#include "limits.h"
188112158Sdas#include "stdlib.h"
189112158Sdas#include "string.h"
190112161Sdas#include "libc_private.h"
191112161Sdas#include "spinlock.h"
192112158Sdas
193112158Sdas#ifdef KR_headers
194112158Sdas#define Char char
195112158Sdas#else
196112158Sdas#define Char void
197112158Sdas#endif
198112158Sdas
199112158Sdas#ifdef MALLOC
200112158Sdasextern Char *MALLOC ANSI((size_t));
201112158Sdas#else
202112158Sdas#define MALLOC malloc
203112158Sdas#endif
204112158Sdas
205112161Sdas#define INFNAN_CHECK
206112161Sdas
207112158Sdas#undef IEEE_Arith
208112158Sdas#undef Avoid_Underflow
209112158Sdas#ifdef IEEE_MC68k
210112158Sdas#define IEEE_Arith
211112158Sdas#endif
212112158Sdas#ifdef IEEE_8087
213112158Sdas#define IEEE_Arith
214112158Sdas#endif
215112158Sdas
216112158Sdas#include "errno.h"
217112158Sdas#ifdef Bad_float_h
218112158Sdas
219112158Sdas#ifdef IEEE_Arith
220112158Sdas#define DBL_DIG 15
221112158Sdas#define DBL_MAX_10_EXP 308
222112158Sdas#define DBL_MAX_EXP 1024
223112158Sdas#define FLT_RADIX 2
224112158Sdas#define DBL_MAX 1.7976931348623157e+308
225112158Sdas#endif
226112158Sdas
227112158Sdas#ifdef IBM
228112158Sdas#define DBL_DIG 16
229112158Sdas#define DBL_MAX_10_EXP 75
230112158Sdas#define DBL_MAX_EXP 63
231112158Sdas#define FLT_RADIX 16
232112158Sdas#define DBL_MAX 7.2370055773322621e+75
233112158Sdas#endif
234112158Sdas
235112158Sdas#ifdef VAX
236112158Sdas#define DBL_DIG 16
237112158Sdas#define DBL_MAX_10_EXP 38
238112158Sdas#define DBL_MAX_EXP 127
239112158Sdas#define FLT_RADIX 2
240112158Sdas#define DBL_MAX 1.7014118346046923e+38
241112158Sdas#define n_bigtens 2
242112158Sdas#endif
243112158Sdas
244112158Sdas#ifndef LONG_MAX
245112158Sdas#define LONG_MAX 2147483647
246112158Sdas#endif
247112158Sdas
248112158Sdas#else /* ifndef Bad_float_h */
249112158Sdas#include "float.h"
250112158Sdas#endif /* Bad_float_h */
251112158Sdas
252112158Sdas#ifdef IEEE_Arith
253112158Sdas#define Scale_Bit 0x10
254112158Sdas#define n_bigtens 5
255112158Sdas#endif
256112158Sdas
257112158Sdas#ifdef IBM
258112158Sdas#define n_bigtens 3
259112158Sdas#endif
260112158Sdas
261112158Sdas#ifdef VAX
262112158Sdas#define n_bigtens 2
263112158Sdas#endif
264112158Sdas
265112158Sdas#ifndef __MATH_H__
266112158Sdas#include "math.h"
267112158Sdas#endif
268112158Sdas
269112158Sdas#ifdef __cplusplus
270112158Sdasextern "C" {
271112158Sdas#endif
272112158Sdas
273112158Sdas#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
274112158SdasExactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
275112158Sdas#endif
276112158Sdas
277112158Sdastypedef union { double d; ULong L[2]; } U;
278112158Sdas
279112158Sdas#ifdef YES_ALIAS
280112158Sdas#define dval(x) x
281112158Sdas#ifdef IEEE_8087
282112158Sdas#define word0(x) ((ULong *)&x)[1]
283112158Sdas#define word1(x) ((ULong *)&x)[0]
284112158Sdas#else
285112158Sdas#define word0(x) ((ULong *)&x)[0]
286112158Sdas#define word1(x) ((ULong *)&x)[1]
287112158Sdas#endif
288112158Sdas#else /* !YES_ALIAS */
289112158Sdas#ifdef IEEE_8087
290112158Sdas#define word0(x) ((U*)&x)->L[1]
291112158Sdas#define word1(x) ((U*)&x)->L[0]
292112158Sdas#else
293112158Sdas#define word0(x) ((U*)&x)->L[0]
294112158Sdas#define word1(x) ((U*)&x)->L[1]
295112158Sdas#endif
296112158Sdas#define dval(x) ((U*)&x)->d
297112158Sdas#endif /* YES_ALIAS */
298112158Sdas
299112158Sdas/* The following definition of Storeinc is appropriate for MIPS processors.
300112158Sdas * An alternative that might be better on some machines is
301112158Sdas * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
302112158Sdas */
303112158Sdas#if defined(IEEE_8087) + defined(VAX)
304112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
305112158Sdas((unsigned short *)a)[0] = (unsigned short)c, a++)
306112158Sdas#else
307112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
308112158Sdas((unsigned short *)a)[1] = (unsigned short)c, a++)
309112158Sdas#endif
310112158Sdas
311112158Sdas/* #define P DBL_MANT_DIG */
312112158Sdas/* Ten_pmax = floor(P*log(2)/log(5)) */
313112158Sdas/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
314112158Sdas/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
315112158Sdas/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
316112158Sdas
317112158Sdas#ifdef IEEE_Arith
318112158Sdas#define Exp_shift  20
319112158Sdas#define Exp_shift1 20
320112158Sdas#define Exp_msk1    0x100000
321112158Sdas#define Exp_msk11   0x100000
322112158Sdas#define Exp_mask  0x7ff00000
323112158Sdas#define P 53
324112158Sdas#define Bias 1023
325112158Sdas#define Emin (-1022)
326112158Sdas#define Exp_1  0x3ff00000
327112158Sdas#define Exp_11 0x3ff00000
328112158Sdas#define Ebits 11
329112158Sdas#define Frac_mask  0xfffff
330112158Sdas#define Frac_mask1 0xfffff
331112158Sdas#define Ten_pmax 22
332112158Sdas#define Bletch 0x10
333112158Sdas#define Bndry_mask  0xfffff
334112158Sdas#define Bndry_mask1 0xfffff
335112158Sdas#define LSB 1
336112158Sdas#define Sign_bit 0x80000000
337112158Sdas#define Log2P 1
338112158Sdas#define Tiny0 0
339112158Sdas#define Tiny1 1
340112158Sdas#define Quick_max 14
341112158Sdas#define Int_max 14
342112158Sdas
343112158Sdas#ifndef Flt_Rounds
344112158Sdas#ifdef FLT_ROUNDS
345112158Sdas#define Flt_Rounds FLT_ROUNDS
346112158Sdas#else
347112158Sdas#define Flt_Rounds 1
348112158Sdas#endif
349112158Sdas#endif /*Flt_Rounds*/
350112158Sdas
351112158Sdas#else /* ifndef IEEE_Arith */
352112158Sdas#undef  Sudden_Underflow
353112158Sdas#define Sudden_Underflow
354112158Sdas#ifdef IBM
355112158Sdas#undef Flt_Rounds
356112158Sdas#define Flt_Rounds 0
357112158Sdas#define Exp_shift  24
358112158Sdas#define Exp_shift1 24
359112158Sdas#define Exp_msk1   0x1000000
360112158Sdas#define Exp_msk11  0x1000000
361112158Sdas#define Exp_mask  0x7f000000
362112158Sdas#define P 14
363112158Sdas#define Bias 65
364112158Sdas#define Exp_1  0x41000000
365112158Sdas#define Exp_11 0x41000000
366112158Sdas#define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
367112158Sdas#define Frac_mask  0xffffff
368112158Sdas#define Frac_mask1 0xffffff
369112158Sdas#define Bletch 4
370112158Sdas#define Ten_pmax 22
371112158Sdas#define Bndry_mask  0xefffff
372112158Sdas#define Bndry_mask1 0xffffff
373112158Sdas#define LSB 1
374112158Sdas#define Sign_bit 0x80000000
375112158Sdas#define Log2P 4
376112158Sdas#define Tiny0 0x100000
377112158Sdas#define Tiny1 0
378112158Sdas#define Quick_max 14
379112158Sdas#define Int_max 15
380112158Sdas#else /* VAX */
381112158Sdas#undef Flt_Rounds
382112158Sdas#define Flt_Rounds 1
383112158Sdas#define Exp_shift  23
384112158Sdas#define Exp_shift1 7
385112158Sdas#define Exp_msk1    0x80
386112158Sdas#define Exp_msk11   0x800000
387112158Sdas#define Exp_mask  0x7f80
388112158Sdas#define P 56
389112158Sdas#define Bias 129
390112158Sdas#define Exp_1  0x40800000
391112158Sdas#define Exp_11 0x4080
392112158Sdas#define Ebits 8
393112158Sdas#define Frac_mask  0x7fffff
394112158Sdas#define Frac_mask1 0xffff007f
395112158Sdas#define Ten_pmax 24
396112158Sdas#define Bletch 2
397112158Sdas#define Bndry_mask  0xffff007f
398112158Sdas#define Bndry_mask1 0xffff007f
399112158Sdas#define LSB 0x10000
400112158Sdas#define Sign_bit 0x8000
401112158Sdas#define Log2P 1
402112158Sdas#define Tiny0 0x80
403112158Sdas#define Tiny1 0
404112158Sdas#define Quick_max 15
405112158Sdas#define Int_max 15
406112158Sdas#endif /* IBM, VAX */
407112158Sdas#endif /* IEEE_Arith */
408112158Sdas
409112158Sdas#ifndef IEEE_Arith
410112158Sdas#define ROUND_BIASED
411112158Sdas#endif
412112158Sdas
413112158Sdas#ifdef RND_PRODQUOT
414112158Sdas#define rounded_product(a,b) a = rnd_prod(a, b)
415112158Sdas#define rounded_quotient(a,b) a = rnd_quot(a, b)
416112158Sdas#ifdef KR_headers
417112158Sdasextern double rnd_prod(), rnd_quot();
418112158Sdas#else
419112158Sdasextern double rnd_prod(double, double), rnd_quot(double, double);
420112158Sdas#endif
421112158Sdas#else
422112158Sdas#define rounded_product(a,b) a *= b
423112158Sdas#define rounded_quotient(a,b) a /= b
424112158Sdas#endif
425112158Sdas
426112158Sdas#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
427112158Sdas#define Big1 0xffffffff
428112158Sdas
429112158Sdas#undef  Pack_16
430112158Sdas#ifndef Pack_32
431112158Sdas#define Pack_32
432112158Sdas#endif
433112158Sdas
434112158Sdas#ifdef NO_LONG_LONG
435112158Sdas#undef ULLong
436112158Sdas#ifdef Just_16
437112158Sdas#undef Pack_32
438112158Sdas#define Pack_16
439112158Sdas/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
440112158Sdas * This makes some inner loops simpler and sometimes saves work
441112158Sdas * during multiplications, but it often seems to make things slightly
442112158Sdas * slower.  Hence the default is now to store 32 bits per Long.
443112158Sdas */
444112158Sdas#endif
445112158Sdas#else	/* long long available */
446112158Sdas#ifndef Llong
447112158Sdas#define Llong long long
448112158Sdas#endif
449112158Sdas#ifndef ULLong
450112158Sdas#define ULLong unsigned Llong
451112158Sdas#endif
452112158Sdas#endif /* NO_LONG_LONG */
453112158Sdas
454112158Sdas#ifdef Pack_32
455112158Sdas#define ULbits 32
456112158Sdas#define kshift 5
457112158Sdas#define kmask 31
458112158Sdas#define ALL_ON 0xffffffff
459112158Sdas#else
460112158Sdas#define ULbits 16
461112158Sdas#define kshift 4
462112158Sdas#define kmask 15
463112158Sdas#define ALL_ON 0xffff
464112158Sdas#endif
465112158Sdas
466112161Sdas#define MULTIPLE_THREADS
467112161Sdasextern spinlock_t __gdtoa_locks[2];
468112161Sdas#define ACQUIRE_DTOA_LOCK(n)	do {		\
469112161Sdas	if (__isthreaded)			\
470112161Sdas		_SPINLOCK(&__gdtoa_locks[n]);	\
471112161Sdas} while(0)
472112161Sdas#define FREE_DTOA_LOCK(n)	do {		\
473112161Sdas	if (__isthreaded)			\
474112161Sdas		_SPINUNLOCK(&__gdtoa_locks[n]);	\
475112161Sdas} while(0)
476112158Sdas
477112158Sdas#define Kmax 15
478112158Sdas
479112158Sdas struct
480112158SdasBigint {
481112158Sdas	struct Bigint *next;
482112158Sdas	int k, maxwds, sign, wds;
483112158Sdas	ULong x[1];
484112158Sdas	};
485112158Sdas
486112158Sdas typedef struct Bigint Bigint;
487112158Sdas
488112158Sdas#ifdef NO_STRING_H
489112158Sdas#ifdef DECLARE_SIZE_T
490112158Sdastypedef unsigned int size_t;
491112158Sdas#endif
492112158Sdasextern void memcpy_D2A ANSI((void*, const void*, size_t));
493112158Sdas#define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
494112158Sdas#else /* !NO_STRING_H */
495112158Sdas#define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
496112158Sdas#endif /* NO_STRING_H */
497112158Sdas
498112161Sdas/*
499112161Sdas * Paranoia: Protect exported symbols, including ones in files we don't
500112161Sdas * compile right now.  The standard strtof and strtod survive.
501112161Sdas */
502112161Sdas#define	dtoa		__dtoa
503112161Sdas#define	gdtoa		__gdtoa
504112161Sdas#define	freedtoa	__freedtoa
505112161Sdas#define	strtodg		__strtodg
506112161Sdas#define	g_ddfmt		__g_ddfmt
507112161Sdas#define	g_dfmt		__g_dfmt
508112161Sdas#define	g_ffmt		__g_ffmt
509112161Sdas#define	g_Qfmt		__g_Qfmt
510112161Sdas#define	g_xfmt		__g_xfmt
511112161Sdas#define	g_xLfmt		__g_xLfmt
512112161Sdas#define	strtoId		__strtoId
513112161Sdas#define	strtoIdd	__strtoIdd
514112161Sdas#define	strtoIf		__strtoIf
515112161Sdas#define	strtoIQ		__strtoIQ
516112161Sdas#define	strtoIx		__strtoIx
517112161Sdas#define	strtoIxL	__strtoIxL
518112161Sdas#define	strtord		__strtord
519112161Sdas#define	strtordd	__strtordd
520112161Sdas#define	strtorf		__strtorf
521112161Sdas#define	strtorQ		__strtorQ
522112161Sdas#define	strtorx		__strtorx
523112161Sdas#define	strtorxL	__strtorxL
524112161Sdas#define	strtodI		__strtodI
525112161Sdas#define	strtopd		__strtopd
526112161Sdas#define	strtopdd	__strtopdd
527112161Sdas#define	strtopf		__strtopf
528112161Sdas#define	strtopQ		__strtopQ
529112161Sdas#define	strtopx		__strtopx
530112161Sdas#define	strtopxL	__strtopxL
531112158Sdas
532112161Sdas/* Protect gdtoa-internal symbols */
533112161Sdas#define	Balloc		__Balloc_D2A
534112161Sdas#define	Bfree		__Bfree_D2A
535112161Sdas#define	ULtoQ		__ULtoQ_D2A
536112161Sdas#define	ULtof		__ULtof_D2A
537112161Sdas#define	ULtod		__ULtod_D2A
538112161Sdas#define	ULtodd		__ULtodd_D2A
539112161Sdas#define	ULtox		__ULtox_D2A
540112161Sdas#define	ULtoxL		__ULtoxL_D2A
541112161Sdas#define	any_on		__any_on_D2A
542112161Sdas#define	b2d		__b2d_D2A
543112161Sdas#define	bigtens		__bigtens_D2A
544112161Sdas#define	cmp		__cmp_D2A
545112161Sdas#define	copybits	__copybits_D2A
546112161Sdas#define	d2b		__d2b_D2A
547112161Sdas#define	decrement	__decrement_D2A
548112161Sdas#define	diff		__diff_D2A
549112161Sdas#define	dtoa_result	__dtoa_result_D2A
550112161Sdas#define	g__fmt		__g__fmt_D2A
551112161Sdas#define	gethex		__gethex_D2A
552112161Sdas#define	hexdig		__hexdig_D2A
553112161Sdas#define	hexdig_init_D2A	__hexdig_init_D2A
554112161Sdas#define	hexnan		__hexnan_D2A
555112161Sdas#define	hi0bits		__hi0bits_D2A
556112161Sdas#define	i2b		__i2b_D2A
557112161Sdas#define	increment	__increment_D2A
558112161Sdas#define	lo0bits		__lo0bits_D2A
559112161Sdas#define	lshift		__lshift_D2A
560112161Sdas#define	match		__match_D2A
561112161Sdas#define	mult		__mult_D2A
562112161Sdas#define	multadd		__multadd_D2A
563112161Sdas#define	nrv_alloc	__nrv_alloc_D2A
564112161Sdas#define	pow5mult	__pow5mult_D2A
565112161Sdas#define	quorem		__quorem_D2A
566112161Sdas#define	ratio		__ratio_D2A
567112161Sdas#define	rshift		__rshift_D2A
568112161Sdas#define	rv_alloc	__rv_alloc_D2A
569112161Sdas#define	s2b		__s2b_D2A
570112161Sdas#define	set_ones	__set_ones_D2A
571112161Sdas#define	strcp		__strcp_D2A
572112161Sdas#define	strcp_D2A      	__strcp_D2A
573112161Sdas#define	strtoIg		__strtoIg_D2A
574112161Sdas#define	sum		__sum_D2A
575112161Sdas#define	tens		__tens_D2A
576112161Sdas#define	tinytens	__tinytens_D2A
577112161Sdas#define	tinytens	__tinytens_D2A
578112161Sdas#define	trailz		__trailz_D2A
579112161Sdas#define	ulp		__ulp_D2A
580112161Sdas
581112158Sdas extern char *dtoa_result;
582112158Sdas extern CONST double bigtens[], tens[], tinytens[];
583112158Sdas extern unsigned char hexdig[];
584112158Sdas
585112158Sdas extern Bigint *Balloc ANSI((int));
586112158Sdas extern void Bfree ANSI((Bigint*));
587112158Sdas extern void ULtof ANSI((ULong*, ULong*, Long, int));
588112158Sdas extern void ULtod ANSI((ULong*, ULong*, Long, int));
589112158Sdas extern void ULtodd ANSI((ULong*, ULong*, Long, int));
590112158Sdas extern void ULtoQ ANSI((ULong*, ULong*, Long, int));
591112158Sdas extern void ULtox ANSI((UShort*, ULong*, Long, int));
592112158Sdas extern void ULtoxL ANSI((ULong*, ULong*, Long, int));
593112158Sdas extern ULong any_on ANSI((Bigint*, int));
594112158Sdas extern double b2d ANSI((Bigint*, int*));
595112158Sdas extern int cmp ANSI((Bigint*, Bigint*));
596112158Sdas extern void copybits ANSI((ULong*, int, Bigint*));
597112158Sdas extern Bigint *d2b ANSI((double, int*, int*));
598112158Sdas extern int decrement ANSI((Bigint*));
599112158Sdas extern Bigint *diff ANSI((Bigint*, Bigint*));
600112158Sdas extern char *dtoa ANSI((double d, int mode, int ndigits,
601112158Sdas			int *decpt, int *sign, char **rve));
602112158Sdas extern char *g__fmt ANSI((char*, char*, char*, int, ULong));
603112158Sdas extern int gethex ANSI((CONST char**, FPI*, Long*, Bigint**, int));
604112158Sdas extern void hexdig_init_D2A(Void);
605112158Sdas extern int hexnan ANSI((CONST char**, FPI*, ULong*));
606112158Sdas extern int hi0bits ANSI((ULong));
607112158Sdas extern Bigint *i2b ANSI((int));
608112158Sdas extern Bigint *increment ANSI((Bigint*));
609112158Sdas extern int lo0bits ANSI((ULong*));
610112158Sdas extern Bigint *lshift ANSI((Bigint*, int));
611112158Sdas extern int match ANSI((CONST char**, char*));
612112158Sdas extern Bigint *mult ANSI((Bigint*, Bigint*));
613112158Sdas extern Bigint *multadd ANSI((Bigint*, int, int));
614112158Sdas extern char *nrv_alloc ANSI((char*, char **, int));
615112158Sdas extern Bigint *pow5mult ANSI((Bigint*, int));
616112158Sdas extern int quorem ANSI((Bigint*, Bigint*));
617112158Sdas extern double ratio ANSI((Bigint*, Bigint*));
618112158Sdas extern void rshift ANSI((Bigint*, int));
619112158Sdas extern char *rv_alloc ANSI((int));
620112158Sdas extern Bigint *s2b ANSI((CONST char*, int, int, ULong));
621112158Sdas extern Bigint *set_ones ANSI((Bigint*, int));
622112158Sdas extern char *strcp ANSI((char*, const char*));
623112158Sdas extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*));
624112158Sdas extern double strtod ANSI((const char *s00, char **se));
625112158Sdas extern Bigint *sum ANSI((Bigint*, Bigint*));
626112158Sdas extern int trailz ANSI((Bigint*));
627112158Sdas extern double ulp ANSI((double));
628112158Sdas
629112158Sdas#ifdef __cplusplus
630112158Sdas}
631112158Sdas#endif
632112158Sdas
633112158Sdas
634112158Sdas#ifdef IEEE_Arith
635112158Sdas#ifdef IEEE_MC68k
636112158Sdas#define _0 0
637112158Sdas#define _1 1
638112158Sdas#else
639112158Sdas#define _0 1
640112158Sdas#define _1 0
641112158Sdas#endif
642112158Sdas#else
643112158Sdas#undef INFNAN_CHECK
644112158Sdas#endif
645112158Sdas
646112158Sdas#ifdef INFNAN_CHECK
647112158Sdas
648112158Sdas#ifndef NAN_WORD0
649112158Sdas#define NAN_WORD0 0x7ff80000
650112158Sdas#endif
651112158Sdas
652112158Sdas#ifndef NAN_WORD1
653112158Sdas#define NAN_WORD1 0
654112158Sdas#endif
655112158Sdas#endif	/* INFNAN_CHECK */
656112158Sdas
657112158Sdas#undef SI
658112158Sdas#ifdef Sudden_Underflow
659112158Sdas#define SI 1
660112158Sdas#else
661112158Sdas#define SI 0
662112158Sdas#endif
663112158Sdas
664112158Sdas#endif /* GDTOAIMP_H_INCLUDED */
665