gdtoaimp.h revision 113279
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 113279 2003-04-09 06:04:35Z 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
206112418Sdas#define USE_LOCALE
207112161Sdas
208112158Sdas#undef IEEE_Arith
209112158Sdas#undef Avoid_Underflow
210112158Sdas#ifdef IEEE_MC68k
211112158Sdas#define IEEE_Arith
212112158Sdas#endif
213112158Sdas#ifdef IEEE_8087
214112158Sdas#define IEEE_Arith
215112158Sdas#endif
216112158Sdas
217112158Sdas#include "errno.h"
218112158Sdas#ifdef Bad_float_h
219112158Sdas
220112158Sdas#ifdef IEEE_Arith
221112158Sdas#define DBL_DIG 15
222112158Sdas#define DBL_MAX_10_EXP 308
223112158Sdas#define DBL_MAX_EXP 1024
224112158Sdas#define FLT_RADIX 2
225112158Sdas#define DBL_MAX 1.7976931348623157e+308
226112158Sdas#endif
227112158Sdas
228112158Sdas#ifdef IBM
229112158Sdas#define DBL_DIG 16
230112158Sdas#define DBL_MAX_10_EXP 75
231112158Sdas#define DBL_MAX_EXP 63
232112158Sdas#define FLT_RADIX 16
233112158Sdas#define DBL_MAX 7.2370055773322621e+75
234112158Sdas#endif
235112158Sdas
236112158Sdas#ifdef VAX
237112158Sdas#define DBL_DIG 16
238112158Sdas#define DBL_MAX_10_EXP 38
239112158Sdas#define DBL_MAX_EXP 127
240112158Sdas#define FLT_RADIX 2
241112158Sdas#define DBL_MAX 1.7014118346046923e+38
242112158Sdas#define n_bigtens 2
243112158Sdas#endif
244112158Sdas
245112158Sdas#ifndef LONG_MAX
246112158Sdas#define LONG_MAX 2147483647
247112158Sdas#endif
248112158Sdas
249112158Sdas#else /* ifndef Bad_float_h */
250112158Sdas#include "float.h"
251112158Sdas#endif /* Bad_float_h */
252112158Sdas
253112158Sdas#ifdef IEEE_Arith
254112158Sdas#define Scale_Bit 0x10
255112158Sdas#define n_bigtens 5
256112158Sdas#endif
257112158Sdas
258112158Sdas#ifdef IBM
259112158Sdas#define n_bigtens 3
260112158Sdas#endif
261112158Sdas
262112158Sdas#ifdef VAX
263112158Sdas#define n_bigtens 2
264112158Sdas#endif
265112158Sdas
266112158Sdas#ifndef __MATH_H__
267112158Sdas#include "math.h"
268112158Sdas#endif
269112158Sdas
270112158Sdas#ifdef __cplusplus
271112158Sdasextern "C" {
272112158Sdas#endif
273112158Sdas
274112158Sdas#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
275112158SdasExactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
276112158Sdas#endif
277112158Sdas
278112158Sdastypedef union { double d; ULong L[2]; } U;
279112158Sdas
280112158Sdas#ifdef YES_ALIAS
281112158Sdas#define dval(x) x
282112158Sdas#ifdef IEEE_8087
283112158Sdas#define word0(x) ((ULong *)&x)[1]
284112158Sdas#define word1(x) ((ULong *)&x)[0]
285112158Sdas#else
286112158Sdas#define word0(x) ((ULong *)&x)[0]
287112158Sdas#define word1(x) ((ULong *)&x)[1]
288112158Sdas#endif
289112158Sdas#else /* !YES_ALIAS */
290112158Sdas#ifdef IEEE_8087
291112158Sdas#define word0(x) ((U*)&x)->L[1]
292112158Sdas#define word1(x) ((U*)&x)->L[0]
293112158Sdas#else
294112158Sdas#define word0(x) ((U*)&x)->L[0]
295112158Sdas#define word1(x) ((U*)&x)->L[1]
296112158Sdas#endif
297112158Sdas#define dval(x) ((U*)&x)->d
298112158Sdas#endif /* YES_ALIAS */
299112158Sdas
300112158Sdas/* The following definition of Storeinc is appropriate for MIPS processors.
301112158Sdas * An alternative that might be better on some machines is
302112158Sdas * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
303112158Sdas */
304112158Sdas#if defined(IEEE_8087) + defined(VAX)
305112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
306112158Sdas((unsigned short *)a)[0] = (unsigned short)c, a++)
307112158Sdas#else
308112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
309112158Sdas((unsigned short *)a)[1] = (unsigned short)c, a++)
310112158Sdas#endif
311112158Sdas
312112158Sdas/* #define P DBL_MANT_DIG */
313112158Sdas/* Ten_pmax = floor(P*log(2)/log(5)) */
314112158Sdas/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
315112158Sdas/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
316112158Sdas/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
317112158Sdas
318112158Sdas#ifdef IEEE_Arith
319112158Sdas#define Exp_shift  20
320112158Sdas#define Exp_shift1 20
321112158Sdas#define Exp_msk1    0x100000
322112158Sdas#define Exp_msk11   0x100000
323112158Sdas#define Exp_mask  0x7ff00000
324112158Sdas#define P 53
325112158Sdas#define Bias 1023
326112158Sdas#define Emin (-1022)
327112158Sdas#define Exp_1  0x3ff00000
328112158Sdas#define Exp_11 0x3ff00000
329112158Sdas#define Ebits 11
330112158Sdas#define Frac_mask  0xfffff
331112158Sdas#define Frac_mask1 0xfffff
332112158Sdas#define Ten_pmax 22
333112158Sdas#define Bletch 0x10
334112158Sdas#define Bndry_mask  0xfffff
335112158Sdas#define Bndry_mask1 0xfffff
336112158Sdas#define LSB 1
337112158Sdas#define Sign_bit 0x80000000
338112158Sdas#define Log2P 1
339112158Sdas#define Tiny0 0
340112158Sdas#define Tiny1 1
341112158Sdas#define Quick_max 14
342112158Sdas#define Int_max 14
343112158Sdas
344112158Sdas#ifndef Flt_Rounds
345112158Sdas#ifdef FLT_ROUNDS
346112158Sdas#define Flt_Rounds FLT_ROUNDS
347112158Sdas#else
348112158Sdas#define Flt_Rounds 1
349112158Sdas#endif
350112158Sdas#endif /*Flt_Rounds*/
351112158Sdas
352112158Sdas#else /* ifndef IEEE_Arith */
353112158Sdas#undef  Sudden_Underflow
354112158Sdas#define Sudden_Underflow
355112158Sdas#ifdef IBM
356112158Sdas#undef Flt_Rounds
357112158Sdas#define Flt_Rounds 0
358112158Sdas#define Exp_shift  24
359112158Sdas#define Exp_shift1 24
360112158Sdas#define Exp_msk1   0x1000000
361112158Sdas#define Exp_msk11  0x1000000
362112158Sdas#define Exp_mask  0x7f000000
363112158Sdas#define P 14
364112158Sdas#define Bias 65
365112158Sdas#define Exp_1  0x41000000
366112158Sdas#define Exp_11 0x41000000
367112158Sdas#define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
368112158Sdas#define Frac_mask  0xffffff
369112158Sdas#define Frac_mask1 0xffffff
370112158Sdas#define Bletch 4
371112158Sdas#define Ten_pmax 22
372112158Sdas#define Bndry_mask  0xefffff
373112158Sdas#define Bndry_mask1 0xffffff
374112158Sdas#define LSB 1
375112158Sdas#define Sign_bit 0x80000000
376112158Sdas#define Log2P 4
377112158Sdas#define Tiny0 0x100000
378112158Sdas#define Tiny1 0
379112158Sdas#define Quick_max 14
380112158Sdas#define Int_max 15
381112158Sdas#else /* VAX */
382112158Sdas#undef Flt_Rounds
383112158Sdas#define Flt_Rounds 1
384112158Sdas#define Exp_shift  23
385112158Sdas#define Exp_shift1 7
386112158Sdas#define Exp_msk1    0x80
387112158Sdas#define Exp_msk11   0x800000
388112158Sdas#define Exp_mask  0x7f80
389112158Sdas#define P 56
390112158Sdas#define Bias 129
391112158Sdas#define Exp_1  0x40800000
392112158Sdas#define Exp_11 0x4080
393112158Sdas#define Ebits 8
394112158Sdas#define Frac_mask  0x7fffff
395112158Sdas#define Frac_mask1 0xffff007f
396112158Sdas#define Ten_pmax 24
397112158Sdas#define Bletch 2
398112158Sdas#define Bndry_mask  0xffff007f
399112158Sdas#define Bndry_mask1 0xffff007f
400112158Sdas#define LSB 0x10000
401112158Sdas#define Sign_bit 0x8000
402112158Sdas#define Log2P 1
403112158Sdas#define Tiny0 0x80
404112158Sdas#define Tiny1 0
405112158Sdas#define Quick_max 15
406112158Sdas#define Int_max 15
407112158Sdas#endif /* IBM, VAX */
408112158Sdas#endif /* IEEE_Arith */
409112158Sdas
410112158Sdas#ifndef IEEE_Arith
411112158Sdas#define ROUND_BIASED
412112158Sdas#endif
413112158Sdas
414112158Sdas#ifdef RND_PRODQUOT
415112158Sdas#define rounded_product(a,b) a = rnd_prod(a, b)
416112158Sdas#define rounded_quotient(a,b) a = rnd_quot(a, b)
417112158Sdas#ifdef KR_headers
418112158Sdasextern double rnd_prod(), rnd_quot();
419112158Sdas#else
420112158Sdasextern double rnd_prod(double, double), rnd_quot(double, double);
421112158Sdas#endif
422112158Sdas#else
423112158Sdas#define rounded_product(a,b) a *= b
424112158Sdas#define rounded_quotient(a,b) a /= b
425112158Sdas#endif
426112158Sdas
427112158Sdas#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
428112158Sdas#define Big1 0xffffffff
429112158Sdas
430112158Sdas#undef  Pack_16
431112158Sdas#ifndef Pack_32
432112158Sdas#define Pack_32
433112158Sdas#endif
434112158Sdas
435112158Sdas#ifdef NO_LONG_LONG
436112158Sdas#undef ULLong
437112158Sdas#ifdef Just_16
438112158Sdas#undef Pack_32
439112158Sdas#define Pack_16
440112158Sdas/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
441112158Sdas * This makes some inner loops simpler and sometimes saves work
442112158Sdas * during multiplications, but it often seems to make things slightly
443112158Sdas * slower.  Hence the default is now to store 32 bits per Long.
444112158Sdas */
445112158Sdas#endif
446112158Sdas#else	/* long long available */
447112158Sdas#ifndef Llong
448112158Sdas#define Llong long long
449112158Sdas#endif
450112158Sdas#ifndef ULLong
451112158Sdas#define ULLong unsigned Llong
452112158Sdas#endif
453112158Sdas#endif /* NO_LONG_LONG */
454112158Sdas
455112158Sdas#ifdef Pack_32
456112158Sdas#define ULbits 32
457112158Sdas#define kshift 5
458112158Sdas#define kmask 31
459112158Sdas#define ALL_ON 0xffffffff
460112158Sdas#else
461112158Sdas#define ULbits 16
462112158Sdas#define kshift 4
463112158Sdas#define kmask 15
464112158Sdas#define ALL_ON 0xffff
465112158Sdas#endif
466112158Sdas
467112161Sdas#define MULTIPLE_THREADS
468112161Sdasextern spinlock_t __gdtoa_locks[2];
469112161Sdas#define ACQUIRE_DTOA_LOCK(n)	do {		\
470112161Sdas	if (__isthreaded)			\
471112161Sdas		_SPINLOCK(&__gdtoa_locks[n]);	\
472112161Sdas} while(0)
473112161Sdas#define FREE_DTOA_LOCK(n)	do {		\
474112161Sdas	if (__isthreaded)			\
475112161Sdas		_SPINUNLOCK(&__gdtoa_locks[n]);	\
476112161Sdas} while(0)
477112158Sdas
478112158Sdas#define Kmax 15
479112158Sdas
480112158Sdas struct
481112158SdasBigint {
482112158Sdas	struct Bigint *next;
483112158Sdas	int k, maxwds, sign, wds;
484112158Sdas	ULong x[1];
485112158Sdas	};
486112158Sdas
487112158Sdas typedef struct Bigint Bigint;
488112158Sdas
489112158Sdas#ifdef NO_STRING_H
490112158Sdas#ifdef DECLARE_SIZE_T
491112158Sdastypedef unsigned int size_t;
492112158Sdas#endif
493112158Sdasextern void memcpy_D2A ANSI((void*, const void*, size_t));
494112158Sdas#define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
495112158Sdas#else /* !NO_STRING_H */
496112158Sdas#define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
497112158Sdas#endif /* NO_STRING_H */
498112158Sdas
499112161Sdas/*
500112161Sdas * Paranoia: Protect exported symbols, including ones in files we don't
501112161Sdas * compile right now.  The standard strtof and strtod survive.
502112161Sdas */
503112161Sdas#define	dtoa		__dtoa
504112161Sdas#define	gdtoa		__gdtoa
505112161Sdas#define	freedtoa	__freedtoa
506112161Sdas#define	strtodg		__strtodg
507112161Sdas#define	g_ddfmt		__g_ddfmt
508112161Sdas#define	g_dfmt		__g_dfmt
509112161Sdas#define	g_ffmt		__g_ffmt
510112161Sdas#define	g_Qfmt		__g_Qfmt
511112161Sdas#define	g_xfmt		__g_xfmt
512112161Sdas#define	g_xLfmt		__g_xLfmt
513112161Sdas#define	strtoId		__strtoId
514112161Sdas#define	strtoIdd	__strtoIdd
515112161Sdas#define	strtoIf		__strtoIf
516112161Sdas#define	strtoIQ		__strtoIQ
517112161Sdas#define	strtoIx		__strtoIx
518112161Sdas#define	strtoIxL	__strtoIxL
519112161Sdas#define	strtord		__strtord
520112161Sdas#define	strtordd	__strtordd
521112161Sdas#define	strtorf		__strtorf
522112161Sdas#define	strtorQ		__strtorQ
523112161Sdas#define	strtorx		__strtorx
524112161Sdas#define	strtorxL	__strtorxL
525112161Sdas#define	strtodI		__strtodI
526112161Sdas#define	strtopd		__strtopd
527112161Sdas#define	strtopdd	__strtopdd
528112161Sdas#define	strtopf		__strtopf
529112161Sdas#define	strtopQ		__strtopQ
530112161Sdas#define	strtopx		__strtopx
531112161Sdas#define	strtopxL	__strtopxL
532112158Sdas
533112161Sdas/* Protect gdtoa-internal symbols */
534112161Sdas#define	Balloc		__Balloc_D2A
535112161Sdas#define	Bfree		__Bfree_D2A
536112161Sdas#define	ULtoQ		__ULtoQ_D2A
537112161Sdas#define	ULtof		__ULtof_D2A
538112161Sdas#define	ULtod		__ULtod_D2A
539112161Sdas#define	ULtodd		__ULtodd_D2A
540112161Sdas#define	ULtox		__ULtox_D2A
541112161Sdas#define	ULtoxL		__ULtoxL_D2A
542112161Sdas#define	any_on		__any_on_D2A
543112161Sdas#define	b2d		__b2d_D2A
544112161Sdas#define	bigtens		__bigtens_D2A
545112161Sdas#define	cmp		__cmp_D2A
546112161Sdas#define	copybits	__copybits_D2A
547112161Sdas#define	d2b		__d2b_D2A
548112161Sdas#define	decrement	__decrement_D2A
549112161Sdas#define	diff		__diff_D2A
550112161Sdas#define	dtoa_result	__dtoa_result_D2A
551112161Sdas#define	g__fmt		__g__fmt_D2A
552112161Sdas#define	gethex		__gethex_D2A
553112161Sdas#define	hexdig		__hexdig_D2A
554112161Sdas#define	hexdig_init_D2A	__hexdig_init_D2A
555112161Sdas#define	hexnan		__hexnan_D2A
556112161Sdas#define	hi0bits		__hi0bits_D2A
557112161Sdas#define	i2b		__i2b_D2A
558112161Sdas#define	increment	__increment_D2A
559112161Sdas#define	lo0bits		__lo0bits_D2A
560112161Sdas#define	lshift		__lshift_D2A
561112161Sdas#define	match		__match_D2A
562112161Sdas#define	mult		__mult_D2A
563112161Sdas#define	multadd		__multadd_D2A
564112161Sdas#define	nrv_alloc	__nrv_alloc_D2A
565112161Sdas#define	pow5mult	__pow5mult_D2A
566112161Sdas#define	quorem		__quorem_D2A
567112161Sdas#define	ratio		__ratio_D2A
568112161Sdas#define	rshift		__rshift_D2A
569112161Sdas#define	rv_alloc	__rv_alloc_D2A
570112161Sdas#define	s2b		__s2b_D2A
571112161Sdas#define	set_ones	__set_ones_D2A
572112161Sdas#define	strcp		__strcp_D2A
573112161Sdas#define	strcp_D2A      	__strcp_D2A
574112161Sdas#define	strtoIg		__strtoIg_D2A
575112161Sdas#define	sum		__sum_D2A
576112161Sdas#define	tens		__tens_D2A
577112161Sdas#define	tinytens	__tinytens_D2A
578112161Sdas#define	tinytens	__tinytens_D2A
579112161Sdas#define	trailz		__trailz_D2A
580112161Sdas#define	ulp		__ulp_D2A
581112161Sdas
582112158Sdas extern char *dtoa_result;
583112158Sdas extern CONST double bigtens[], tens[], tinytens[];
584112158Sdas extern unsigned char hexdig[];
585112158Sdas
586112158Sdas extern Bigint *Balloc ANSI((int));
587112158Sdas extern void Bfree ANSI((Bigint*));
588112158Sdas extern void ULtof ANSI((ULong*, ULong*, Long, int));
589112158Sdas extern void ULtod ANSI((ULong*, ULong*, Long, int));
590112158Sdas extern void ULtodd ANSI((ULong*, ULong*, Long, int));
591112158Sdas extern void ULtoQ ANSI((ULong*, ULong*, Long, int));
592112158Sdas extern void ULtox ANSI((UShort*, ULong*, Long, int));
593112158Sdas extern void ULtoxL ANSI((ULong*, ULong*, Long, int));
594112158Sdas extern ULong any_on ANSI((Bigint*, int));
595112158Sdas extern double b2d ANSI((Bigint*, int*));
596112158Sdas extern int cmp ANSI((Bigint*, Bigint*));
597112158Sdas extern void copybits ANSI((ULong*, int, Bigint*));
598112158Sdas extern Bigint *d2b ANSI((double, int*, int*));
599112158Sdas extern int decrement ANSI((Bigint*));
600112158Sdas extern Bigint *diff ANSI((Bigint*, Bigint*));
601112158Sdas extern char *dtoa ANSI((double d, int mode, int ndigits,
602112158Sdas			int *decpt, int *sign, char **rve));
603113279Sdas extern void freedtoa ANSI((char*));
604113144Sdas extern char *gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp,
605113144Sdas			  int mode, int ndigits, int *decpt, char **rve));
606112158Sdas extern char *g__fmt ANSI((char*, char*, char*, int, ULong));
607112158Sdas extern int gethex ANSI((CONST char**, FPI*, Long*, Bigint**, int));
608112158Sdas extern void hexdig_init_D2A(Void);
609112158Sdas extern int hexnan ANSI((CONST char**, FPI*, ULong*));
610112158Sdas extern int hi0bits ANSI((ULong));
611112158Sdas extern Bigint *i2b ANSI((int));
612112158Sdas extern Bigint *increment ANSI((Bigint*));
613112158Sdas extern int lo0bits ANSI((ULong*));
614112158Sdas extern Bigint *lshift ANSI((Bigint*, int));
615112158Sdas extern int match ANSI((CONST char**, char*));
616112158Sdas extern Bigint *mult ANSI((Bigint*, Bigint*));
617112158Sdas extern Bigint *multadd ANSI((Bigint*, int, int));
618112158Sdas extern char *nrv_alloc ANSI((char*, char **, int));
619112158Sdas extern Bigint *pow5mult ANSI((Bigint*, int));
620112158Sdas extern int quorem ANSI((Bigint*, Bigint*));
621112158Sdas extern double ratio ANSI((Bigint*, Bigint*));
622112158Sdas extern void rshift ANSI((Bigint*, int));
623112158Sdas extern char *rv_alloc ANSI((int));
624112158Sdas extern Bigint *s2b ANSI((CONST char*, int, int, ULong));
625112158Sdas extern Bigint *set_ones ANSI((Bigint*, int));
626112158Sdas extern char *strcp ANSI((char*, const char*));
627113279Sdas extern int strtodg ANSI((CONST char*, char**, FPI*, Long*, ULong*));
628113279Sdas
629113279Sdas extern int strtoId ANSI((CONST char *, char **, double *, double *));
630113279Sdas extern int strtoIdd ANSI((CONST char *, char **, double *, double *));
631113279Sdas extern int strtoIf ANSI((CONST char *, char **, float *, float *));
632112158Sdas extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*));
633113279Sdas extern int strtoIQ ANSI((CONST char *, char **, void *, void *));
634113279Sdas extern int strtoIx ANSI((CONST char *, char **, void *, void *));
635113279Sdas extern int strtoIxL ANSI((CONST char *, char **, void *, void *));
636112158Sdas extern double strtod ANSI((const char *s00, char **se));
637113279Sdas extern int strtopQ ANSI((CONST char *, char **, Void *));
638113279Sdas extern int strtopf ANSI((CONST char *, char **, float *));
639113279Sdas extern int strtopd ANSI((CONST char *, char **, double *));
640113279Sdas extern int strtopdd ANSI((CONST char *, char **, double *));
641113279Sdas extern int strtopx ANSI((CONST char *, char **, Void *));
642113279Sdas extern int strtopxL ANSI((CONST char *, char **, Void *));
643113279Sdas extern int strtord ANSI((CONST char *, char **, int, double *));
644113279Sdas extern int strtordd ANSI((CONST char *, char **, int, double *));
645113279Sdas extern int strtorf ANSI((CONST char *, char **, int, float *));
646113279Sdas extern int strtorQ ANSI((CONST char *, char **, int, void *));
647113279Sdas extern int strtorx ANSI((CONST char *, char **, int, void *));
648113279Sdas extern int strtorxL ANSI((CONST char *, char **, int, void *));
649112158Sdas extern Bigint *sum ANSI((Bigint*, Bigint*));
650112158Sdas extern int trailz ANSI((Bigint*));
651112158Sdas extern double ulp ANSI((double));
652112158Sdas
653112158Sdas#ifdef __cplusplus
654112158Sdas}
655112158Sdas#endif
656112158Sdas
657112158Sdas
658112158Sdas#ifdef IEEE_Arith
659112158Sdas#ifdef IEEE_MC68k
660112158Sdas#define _0 0
661112158Sdas#define _1 1
662112158Sdas#else
663112158Sdas#define _0 1
664112158Sdas#define _1 0
665112158Sdas#endif
666112158Sdas#else
667112158Sdas#undef INFNAN_CHECK
668112158Sdas#endif
669112158Sdas
670112158Sdas#ifdef INFNAN_CHECK
671112158Sdas
672112158Sdas#ifndef NAN_WORD0
673112158Sdas#define NAN_WORD0 0x7ff80000
674112158Sdas#endif
675112158Sdas
676112158Sdas#ifndef NAN_WORD1
677112158Sdas#define NAN_WORD1 0
678112158Sdas#endif
679112158Sdas#endif	/* INFNAN_CHECK */
680112158Sdas
681112158Sdas#undef SI
682112158Sdas#ifdef Sudden_Underflow
683112158Sdas#define SI 1
684112158Sdas#else
685112158Sdas#define SI 0
686112158Sdas#endif
687112158Sdas
688112158Sdas#endif /* GDTOAIMP_H_INCLUDED */
689