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$ */
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
38165746Sdas   Please send bug reports to David M. Gay (dmg at acm dot org,
39165746Sdas   with " at " changed at "@" and " dot " changed to ".").
40112158Sdas */
41112158Sdas
42112158Sdas/* On a machine with IEEE extended-precision registers, it is
43112158Sdas * necessary to specify double-precision (53-bit) rounding precision
44112158Sdas * before invoking strtod or dtoa.  If the machine uses (the equivalent
45112158Sdas * of) Intel 80x87 arithmetic, the call
46112158Sdas *	_control87(PC_53, MCW_PC);
47112158Sdas * does this with many compilers.  Whether this or another call is
48112158Sdas * appropriate depends on the compiler; for this to work, it may be
49112158Sdas * necessary to #include "float.h" or another system-dependent header
50112158Sdas * file.
51112158Sdas */
52112158Sdas
53112158Sdas/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
54112158Sdas *
55112158Sdas * This strtod returns a nearest machine number to the input decimal
56112158Sdas * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
57112158Sdas * broken by the IEEE round-even rule.  Otherwise ties are broken by
58112158Sdas * biased rounding (add half and chop).
59112158Sdas *
60112158Sdas * Inspired loosely by William D. Clinger's paper "How to Read Floating
61165746Sdas * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 112-126].
62112158Sdas *
63112158Sdas * Modifications:
64112158Sdas *
65112158Sdas *	1. We only require IEEE, IBM, or VAX double-precision
66112158Sdas *		arithmetic (not IEEE double-extended).
67112158Sdas *	2. We get by with floating-point arithmetic in a case that
68112158Sdas *		Clinger missed -- when we're computing d * 10^n
69112158Sdas *		for a small integer d and the integer n is not too
70112158Sdas *		much larger than 22 (the maximum integer k for which
71112158Sdas *		we can represent 10^k exactly), we may be able to
72112158Sdas *		compute (d*10^k) * 10^(e-k) with just one roundoff.
73112158Sdas *	3. Rather than a bit-at-a-time adjustment of the binary
74112158Sdas *		result in the hard case, we use floating-point
75112158Sdas *		arithmetic to determine the adjustment to within
76112158Sdas *		one bit; only in really hard cases do we need to
77112158Sdas *		compute a second residual.
78112158Sdas *	4. Because of 3., we don't need a large table of powers of 10
79112158Sdas *		for ten-to-e (just some small tables, e.g. of 10^k
80112158Sdas *		for 0 <= k <= 22).
81112158Sdas */
82112158Sdas
83112158Sdas/*
84112158Sdas * #define IEEE_8087 for IEEE-arithmetic machines where the least
85112158Sdas *	significant byte has the lowest address.
86112158Sdas * #define IEEE_MC68k for IEEE-arithmetic machines where the most
87112158Sdas *	significant byte has the lowest address.
88112158Sdas * #define Long int on machines with 32-bit ints and 64-bit longs.
89112158Sdas * #define Sudden_Underflow for IEEE-format machines without gradual
90112158Sdas *	underflow (i.e., that flush to zero on underflow).
91112158Sdas * #define IBM for IBM mainframe-style floating-point arithmetic.
92112158Sdas * #define VAX for VAX-style floating-point arithmetic (D_floating).
93112158Sdas * #define No_leftright to omit left-right logic in fast floating-point
94112158Sdas *	computation of dtoa.
95112158Sdas * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
96112158Sdas * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
97112158Sdas *	that use extended-precision instructions to compute rounded
98112158Sdas *	products and quotients) with IBM.
99219557Sdas * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
100219557Sdas *	that rounds toward +Infinity.
101219557Sdas * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
102219557Sdas *	rounding when the underlying floating-point arithmetic uses
103219557Sdas *	unbiased rounding.  This prevent using ordinary floating-point
104219557Sdas *	arithmetic when the result could be computed with one rounding error.
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
123219557Sdas *	directly -- and assumed always to succeed.  Similarly, if you
124219557Sdas *	want something other than the system's free() to be called to
125219557Sdas *	recycle memory acquired from MALLOC, #define FREE to be the
126219557Sdas *	name of the alternate routine.  (FREE or free is only called in
127219557Sdas *	pathological cases, e.g., in a gdtoa call after a gdtoa return in
128219557Sdas *	mode 3 with thousands of digits requested.)
129112158Sdas * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
130112158Sdas *	memory allocations from a private pool of memory when possible.
131112158Sdas *	When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
132112158Sdas *	unless #defined to be a different length.  This default length
133112158Sdas *	suffices to get rid of MALLOC calls except for unusual cases,
134112158Sdas *	such as decimal-to-binary conversion of a very long string of
135112158Sdas *	digits.  When converting IEEE double precision values, the
136112158Sdas *	longest string gdtoa can return is about 751 bytes long.  For
137112158Sdas *	conversions by strtod of strings of 800 digits and all gdtoa
138112158Sdas *	conversions of IEEE doubles in single-threaded executions with
139112158Sdas *	8-byte pointers, PRIVATE_MEM >= 7400 appears to suffice; with
140112158Sdas *	4-byte pointers, PRIVATE_MEM >= 7112 appears adequate.
141187808Sdas * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
142187808Sdas *	#defined automatically on IEEE systems.  On such systems,
143187808Sdas *	when INFNAN_CHECK is #defined, strtod checks
144187808Sdas *	for Infinity and NaN (case insensitively).
145112158Sdas *	When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
146112158Sdas *	strtodg also accepts (case insensitively) strings of the form
147182709Sdas *	NaN(x), where x is a string of hexadecimal digits (optionally
148182709Sdas *	preceded by 0x or 0X) and spaces; if there is only one string
149182709Sdas *	of hexadecimal digits, it is taken for the fraction bits of the
150182709Sdas *	resulting NaN; if there are two or more strings of hexadecimal
151182709Sdas *	digits, each string is assigned to the next available sequence
152182709Sdas *	of 32-bit words of fractions bits (starting with the most
153182709Sdas *	significant), right-aligned in each sequence.
154182709Sdas *	Unless GDTOA_NON_PEDANTIC_NANCHECK is #defined, input "NaN(...)"
155182709Sdas *	is consumed even when ... has the wrong form (in which case the
156182709Sdas *	"(...)" is consumed but ignored).
157112158Sdas * #define MULTIPLE_THREADS if the system offers preemptively scheduled
158112158Sdas *	multiple threads.  In this case, you must provide (or suitably
159112158Sdas *	#define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
160112158Sdas *	by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
161112158Sdas *	in pow5mult, ensures lazy evaluation of only one copy of high
162112158Sdas *	powers of 5; omitting this lock would introduce a small
163112158Sdas *	probability of wasting memory, but would otherwise be harmless.)
164112158Sdas *	You must also invoke freedtoa(s) to free the value s returned by
165112158Sdas *	dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
166112158Sdas * #define IMPRECISE_INEXACT if you do not care about the setting of
167112158Sdas *	the STRTOG_Inexact bits in the special case of doing IEEE double
168182709Sdas *	precision conversions (which could also be done by the strtod in
169112158Sdas *	dtoa.c).
170112158Sdas * #define NO_HEX_FP to disable recognition of C9x's hexadecimal
171112158Sdas *	floating-point constants.
172112158Sdas * #define -DNO_ERRNO to suppress setting errno (in strtod.c and
173112158Sdas *	strtodg.c).
174112158Sdas * #define NO_STRING_H to use private versions of memcpy.
175112158Sdas *	On some K&R systems, it may also be necessary to
176112158Sdas *	#define DECLARE_SIZE_T in this case.
177112158Sdas * #define USE_LOCALE to use the current locale's decimal_point value.
178112158Sdas */
179112158Sdas
180112158Sdas#ifndef GDTOAIMP_H_INCLUDED
181112158Sdas#define GDTOAIMP_H_INCLUDED
182174693Sdas
183174693Sdas#define	Long	int
184174693Sdas
185112158Sdas#include "gdtoa.h"
186165746Sdas#include "gd_qnan.h"
187187808Sdas#ifdef Honor_FLT_ROUNDS
188187808Sdas#include <fenv.h>
189187808Sdas#endif
190112158Sdas
191112158Sdas#ifdef DEBUG
192112158Sdas#include "stdio.h"
193112158Sdas#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
194112158Sdas#endif
195112158Sdas
196112161Sdas#include "limits.h"
197112158Sdas#include "stdlib.h"
198112158Sdas#include "string.h"
199112161Sdas#include "libc_private.h"
200112158Sdas
201116652Sdas#include "namespace.h"
202116652Sdas#include <pthread.h>
203116652Sdas#include "un-namespace.h"
204235785Stheraven#include "xlocale_private.h"
205116652Sdas
206112158Sdas#ifdef KR_headers
207112158Sdas#define Char char
208112158Sdas#else
209112158Sdas#define Char void
210112158Sdas#endif
211112158Sdas
212112158Sdas#ifdef MALLOC
213112158Sdasextern Char *MALLOC ANSI((size_t));
214112158Sdas#else
215112158Sdas#define MALLOC malloc
216112158Sdas#endif
217112158Sdas
218112161Sdas#define INFNAN_CHECK
219112418Sdas#define USE_LOCALE
220187808Sdas#define NO_LOCALE_CACHE
221140431Sdas#define Honor_FLT_ROUNDS
222182709Sdas#define Trust_FLT_ROUNDS
223112161Sdas
224112158Sdas#undef IEEE_Arith
225112158Sdas#undef Avoid_Underflow
226112158Sdas#ifdef IEEE_MC68k
227112158Sdas#define IEEE_Arith
228112158Sdas#endif
229112158Sdas#ifdef IEEE_8087
230112158Sdas#define IEEE_Arith
231112158Sdas#endif
232112158Sdas
233112158Sdas#include "errno.h"
234112158Sdas#ifdef Bad_float_h
235112158Sdas
236112158Sdas#ifdef IEEE_Arith
237112158Sdas#define DBL_DIG 15
238112158Sdas#define DBL_MAX_10_EXP 308
239112158Sdas#define DBL_MAX_EXP 1024
240112158Sdas#define FLT_RADIX 2
241112158Sdas#define DBL_MAX 1.7976931348623157e+308
242112158Sdas#endif
243112158Sdas
244112158Sdas#ifdef IBM
245112158Sdas#define DBL_DIG 16
246112158Sdas#define DBL_MAX_10_EXP 75
247112158Sdas#define DBL_MAX_EXP 63
248112158Sdas#define FLT_RADIX 16
249112158Sdas#define DBL_MAX 7.2370055773322621e+75
250112158Sdas#endif
251112158Sdas
252112158Sdas#ifdef VAX
253112158Sdas#define DBL_DIG 16
254112158Sdas#define DBL_MAX_10_EXP 38
255112158Sdas#define DBL_MAX_EXP 127
256112158Sdas#define FLT_RADIX 2
257112158Sdas#define DBL_MAX 1.7014118346046923e+38
258112158Sdas#define n_bigtens 2
259112158Sdas#endif
260112158Sdas
261112158Sdas#ifndef LONG_MAX
262112158Sdas#define LONG_MAX 2147483647
263112158Sdas#endif
264112158Sdas
265112158Sdas#else /* ifndef Bad_float_h */
266112158Sdas#include "float.h"
267112158Sdas#endif /* Bad_float_h */
268112158Sdas
269112158Sdas#ifdef IEEE_Arith
270112158Sdas#define Scale_Bit 0x10
271112158Sdas#define n_bigtens 5
272112158Sdas#endif
273112158Sdas
274112158Sdas#ifdef IBM
275112158Sdas#define n_bigtens 3
276112158Sdas#endif
277112158Sdas
278112158Sdas#ifdef VAX
279112158Sdas#define n_bigtens 2
280112158Sdas#endif
281112158Sdas
282112158Sdas#ifndef __MATH_H__
283112158Sdas#include "math.h"
284112158Sdas#endif
285112158Sdas
286112158Sdas#ifdef __cplusplus
287112158Sdasextern "C" {
288112158Sdas#endif
289112158Sdas
290112158Sdas#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
291112158SdasExactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
292112158Sdas#endif
293112158Sdas
294112158Sdastypedef union { double d; ULong L[2]; } U;
295112158Sdas
296112158Sdas#ifdef IEEE_8087
297219557Sdas#define word0(x) (x)->L[1]
298219557Sdas#define word1(x) (x)->L[0]
299112158Sdas#else
300219557Sdas#define word0(x) (x)->L[0]
301219557Sdas#define word1(x) (x)->L[1]
302112158Sdas#endif
303219557Sdas#define dval(x) (x)->d
304112158Sdas
305112158Sdas/* The following definition of Storeinc is appropriate for MIPS processors.
306112158Sdas * An alternative that might be better on some machines is
307112158Sdas * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
308112158Sdas */
309112158Sdas#if defined(IEEE_8087) + defined(VAX)
310112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
311112158Sdas((unsigned short *)a)[0] = (unsigned short)c, a++)
312112158Sdas#else
313112158Sdas#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
314112158Sdas((unsigned short *)a)[1] = (unsigned short)c, a++)
315112158Sdas#endif
316112158Sdas
317112158Sdas/* #define P DBL_MANT_DIG */
318112158Sdas/* Ten_pmax = floor(P*log(2)/log(5)) */
319112158Sdas/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
320112158Sdas/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
321112158Sdas/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
322112158Sdas
323112158Sdas#ifdef IEEE_Arith
324112158Sdas#define Exp_shift  20
325112158Sdas#define Exp_shift1 20
326112158Sdas#define Exp_msk1    0x100000
327112158Sdas#define Exp_msk11   0x100000
328112158Sdas#define Exp_mask  0x7ff00000
329112158Sdas#define P 53
330112158Sdas#define Bias 1023
331112158Sdas#define Emin (-1022)
332112158Sdas#define Exp_1  0x3ff00000
333112158Sdas#define Exp_11 0x3ff00000
334112158Sdas#define Ebits 11
335112158Sdas#define Frac_mask  0xfffff
336112158Sdas#define Frac_mask1 0xfffff
337112158Sdas#define Ten_pmax 22
338112158Sdas#define Bletch 0x10
339112158Sdas#define Bndry_mask  0xfffff
340112158Sdas#define Bndry_mask1 0xfffff
341112158Sdas#define LSB 1
342112158Sdas#define Sign_bit 0x80000000
343112158Sdas#define Log2P 1
344112158Sdas#define Tiny0 0
345112158Sdas#define Tiny1 1
346112158Sdas#define Quick_max 14
347112158Sdas#define Int_max 14
348112158Sdas
349112158Sdas#ifndef Flt_Rounds
350112158Sdas#ifdef FLT_ROUNDS
351112158Sdas#define Flt_Rounds FLT_ROUNDS
352112158Sdas#else
353112158Sdas#define Flt_Rounds 1
354112158Sdas#endif
355112158Sdas#endif /*Flt_Rounds*/
356112158Sdas
357112158Sdas#else /* ifndef IEEE_Arith */
358112158Sdas#undef  Sudden_Underflow
359112158Sdas#define Sudden_Underflow
360112158Sdas#ifdef IBM
361112158Sdas#undef Flt_Rounds
362112158Sdas#define Flt_Rounds 0
363112158Sdas#define Exp_shift  24
364112158Sdas#define Exp_shift1 24
365112158Sdas#define Exp_msk1   0x1000000
366112158Sdas#define Exp_msk11  0x1000000
367112158Sdas#define Exp_mask  0x7f000000
368112158Sdas#define P 14
369112158Sdas#define Bias 65
370112158Sdas#define Exp_1  0x41000000
371112158Sdas#define Exp_11 0x41000000
372112158Sdas#define Ebits 8	/* exponent has 7 bits, but 8 is the right value in b2d */
373112158Sdas#define Frac_mask  0xffffff
374112158Sdas#define Frac_mask1 0xffffff
375112158Sdas#define Bletch 4
376112158Sdas#define Ten_pmax 22
377112158Sdas#define Bndry_mask  0xefffff
378112158Sdas#define Bndry_mask1 0xffffff
379112158Sdas#define LSB 1
380112158Sdas#define Sign_bit 0x80000000
381112158Sdas#define Log2P 4
382112158Sdas#define Tiny0 0x100000
383112158Sdas#define Tiny1 0
384112158Sdas#define Quick_max 14
385112158Sdas#define Int_max 15
386112158Sdas#else /* VAX */
387112158Sdas#undef Flt_Rounds
388112158Sdas#define Flt_Rounds 1
389112158Sdas#define Exp_shift  23
390112158Sdas#define Exp_shift1 7
391112158Sdas#define Exp_msk1    0x80
392112158Sdas#define Exp_msk11   0x800000
393112158Sdas#define Exp_mask  0x7f80
394112158Sdas#define P 56
395112158Sdas#define Bias 129
396112158Sdas#define Exp_1  0x40800000
397112158Sdas#define Exp_11 0x4080
398112158Sdas#define Ebits 8
399112158Sdas#define Frac_mask  0x7fffff
400112158Sdas#define Frac_mask1 0xffff007f
401112158Sdas#define Ten_pmax 24
402112158Sdas#define Bletch 2
403112158Sdas#define Bndry_mask  0xffff007f
404112158Sdas#define Bndry_mask1 0xffff007f
405112158Sdas#define LSB 0x10000
406112158Sdas#define Sign_bit 0x8000
407112158Sdas#define Log2P 1
408112158Sdas#define Tiny0 0x80
409112158Sdas#define Tiny1 0
410112158Sdas#define Quick_max 15
411112158Sdas#define Int_max 15
412112158Sdas#endif /* IBM, VAX */
413112158Sdas#endif /* IEEE_Arith */
414112158Sdas
415112158Sdas#ifndef IEEE_Arith
416112158Sdas#define ROUND_BIASED
417219557Sdas#else
418219557Sdas#ifdef ROUND_BIASED_without_Round_Up
419219557Sdas#undef  ROUND_BIASED
420219557Sdas#define ROUND_BIASED
421112158Sdas#endif
422219557Sdas#endif
423112158Sdas
424112158Sdas#ifdef RND_PRODQUOT
425112158Sdas#define rounded_product(a,b) a = rnd_prod(a, b)
426112158Sdas#define rounded_quotient(a,b) a = rnd_quot(a, b)
427112158Sdas#ifdef KR_headers
428112158Sdasextern double rnd_prod(), rnd_quot();
429112158Sdas#else
430112158Sdasextern double rnd_prod(double, double), rnd_quot(double, double);
431112158Sdas#endif
432112158Sdas#else
433112158Sdas#define rounded_product(a,b) a *= b
434112158Sdas#define rounded_quotient(a,b) a /= b
435112158Sdas#endif
436112158Sdas
437112158Sdas#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
438112158Sdas#define Big1 0xffffffff
439112158Sdas
440112158Sdas#undef  Pack_16
441112158Sdas#ifndef Pack_32
442112158Sdas#define Pack_32
443112158Sdas#endif
444112158Sdas
445112158Sdas#ifdef NO_LONG_LONG
446112158Sdas#undef ULLong
447112158Sdas#ifdef Just_16
448112158Sdas#undef Pack_32
449112158Sdas#define Pack_16
450112158Sdas/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
451112158Sdas * This makes some inner loops simpler and sometimes saves work
452112158Sdas * during multiplications, but it often seems to make things slightly
453112158Sdas * slower.  Hence the default is now to store 32 bits per Long.
454112158Sdas */
455112158Sdas#endif
456112158Sdas#else	/* long long available */
457112158Sdas#ifndef Llong
458112158Sdas#define Llong long long
459112158Sdas#endif
460112158Sdas#ifndef ULLong
461112158Sdas#define ULLong unsigned Llong
462112158Sdas#endif
463112158Sdas#endif /* NO_LONG_LONG */
464112158Sdas
465112158Sdas#ifdef Pack_32
466112158Sdas#define ULbits 32
467112158Sdas#define kshift 5
468112158Sdas#define kmask 31
469112158Sdas#define ALL_ON 0xffffffff
470112158Sdas#else
471112158Sdas#define ULbits 16
472112158Sdas#define kshift 4
473112158Sdas#define kmask 15
474112158Sdas#define ALL_ON 0xffff
475112158Sdas#endif
476112158Sdas
477112161Sdas#define MULTIPLE_THREADS
478116652Sdasextern pthread_mutex_t __gdtoa_locks[2];
479116652Sdas#define ACQUIRE_DTOA_LOCK(n)	do {				\
480116652Sdas	if (__isthreaded)					\
481116652Sdas		_pthread_mutex_lock(&__gdtoa_locks[n]);		\
482112161Sdas} while(0)
483116652Sdas#define FREE_DTOA_LOCK(n)	do {				\
484116652Sdas	if (__isthreaded)					\
485116652Sdas		_pthread_mutex_unlock(&__gdtoa_locks[n]);	\
486112161Sdas} while(0)
487112158Sdas
488196916Sattilio#define Kmax 9
489112158Sdas
490112158Sdas struct
491112158SdasBigint {
492112158Sdas	struct Bigint *next;
493112158Sdas	int k, maxwds, sign, wds;
494112158Sdas	ULong x[1];
495112158Sdas	};
496112158Sdas
497112158Sdas typedef struct Bigint Bigint;
498112158Sdas
499112158Sdas#ifdef NO_STRING_H
500112158Sdas#ifdef DECLARE_SIZE_T
501112158Sdastypedef unsigned int size_t;
502112158Sdas#endif
503112158Sdasextern void memcpy_D2A ANSI((void*, const void*, size_t));
504112158Sdas#define Bcopy(x,y) memcpy_D2A(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
505112158Sdas#else /* !NO_STRING_H */
506112158Sdas#define Bcopy(x,y) memcpy(&x->sign,&y->sign,y->wds*sizeof(ULong) + 2*sizeof(int))
507112158Sdas#endif /* NO_STRING_H */
508112158Sdas
509112161Sdas/*
510112161Sdas * Paranoia: Protect exported symbols, including ones in files we don't
511112161Sdas * compile right now.  The standard strtof and strtod survive.
512112161Sdas */
513112161Sdas#define	dtoa		__dtoa
514112161Sdas#define	gdtoa		__gdtoa
515112161Sdas#define	freedtoa	__freedtoa
516112161Sdas#define	strtodg		__strtodg
517112161Sdas#define	g_ddfmt		__g_ddfmt
518112161Sdas#define	g_dfmt		__g_dfmt
519112161Sdas#define	g_ffmt		__g_ffmt
520112161Sdas#define	g_Qfmt		__g_Qfmt
521112161Sdas#define	g_xfmt		__g_xfmt
522112161Sdas#define	g_xLfmt		__g_xLfmt
523112161Sdas#define	strtoId		__strtoId
524112161Sdas#define	strtoIdd	__strtoIdd
525112161Sdas#define	strtoIf		__strtoIf
526112161Sdas#define	strtoIQ		__strtoIQ
527112161Sdas#define	strtoIx		__strtoIx
528112161Sdas#define	strtoIxL	__strtoIxL
529235785Stheraven#define	strtord_l		__strtord_l
530112161Sdas#define	strtordd	__strtordd
531112161Sdas#define	strtorf		__strtorf
532235785Stheraven#define	strtorQ_l		__strtorQ_l
533235785Stheraven#define	strtorx_l		__strtorx_l
534112161Sdas#define	strtorxL	__strtorxL
535112161Sdas#define	strtodI		__strtodI
536112161Sdas#define	strtopd		__strtopd
537112161Sdas#define	strtopdd	__strtopdd
538112161Sdas#define	strtopf		__strtopf
539112161Sdas#define	strtopQ		__strtopQ
540112161Sdas#define	strtopx		__strtopx
541112161Sdas#define	strtopxL	__strtopxL
542112158Sdas
543112161Sdas/* Protect gdtoa-internal symbols */
544112161Sdas#define	Balloc		__Balloc_D2A
545112161Sdas#define	Bfree		__Bfree_D2A
546112161Sdas#define	ULtoQ		__ULtoQ_D2A
547112161Sdas#define	ULtof		__ULtof_D2A
548112161Sdas#define	ULtod		__ULtod_D2A
549112161Sdas#define	ULtodd		__ULtodd_D2A
550112161Sdas#define	ULtox		__ULtox_D2A
551112161Sdas#define	ULtoxL		__ULtoxL_D2A
552112161Sdas#define	any_on		__any_on_D2A
553112161Sdas#define	b2d		__b2d_D2A
554112161Sdas#define	bigtens		__bigtens_D2A
555112161Sdas#define	cmp		__cmp_D2A
556112161Sdas#define	copybits	__copybits_D2A
557112161Sdas#define	d2b		__d2b_D2A
558112161Sdas#define	decrement	__decrement_D2A
559112161Sdas#define	diff		__diff_D2A
560112161Sdas#define	dtoa_result	__dtoa_result_D2A
561112161Sdas#define	g__fmt		__g__fmt_D2A
562112161Sdas#define	gethex		__gethex_D2A
563112161Sdas#define	hexdig		__hexdig_D2A
564112161Sdas#define	hexdig_init_D2A	__hexdig_init_D2A
565112161Sdas#define	hexnan		__hexnan_D2A
566112161Sdas#define	hi0bits		__hi0bits_D2A
567165746Sdas#define	hi0bits_D2A	__hi0bits_D2A
568112161Sdas#define	i2b		__i2b_D2A
569112161Sdas#define	increment	__increment_D2A
570112161Sdas#define	lo0bits		__lo0bits_D2A
571112161Sdas#define	lshift		__lshift_D2A
572112161Sdas#define	match		__match_D2A
573112161Sdas#define	mult		__mult_D2A
574112161Sdas#define	multadd		__multadd_D2A
575112161Sdas#define	nrv_alloc	__nrv_alloc_D2A
576112161Sdas#define	pow5mult	__pow5mult_D2A
577112161Sdas#define	quorem		__quorem_D2A
578112161Sdas#define	ratio		__ratio_D2A
579112161Sdas#define	rshift		__rshift_D2A
580112161Sdas#define	rv_alloc	__rv_alloc_D2A
581112161Sdas#define	s2b		__s2b_D2A
582112161Sdas#define	set_ones	__set_ones_D2A
583112161Sdas#define	strcp		__strcp_D2A
584112161Sdas#define	strcp_D2A      	__strcp_D2A
585112161Sdas#define	strtoIg		__strtoIg_D2A
586112161Sdas#define	sum		__sum_D2A
587112161Sdas#define	tens		__tens_D2A
588112161Sdas#define	tinytens	__tinytens_D2A
589112161Sdas#define	tinytens	__tinytens_D2A
590112161Sdas#define	trailz		__trailz_D2A
591112161Sdas#define	ulp		__ulp_D2A
592112161Sdas
593112158Sdas extern char *dtoa_result;
594112158Sdas extern CONST double bigtens[], tens[], tinytens[];
595112158Sdas extern unsigned char hexdig[];
596112158Sdas
597112158Sdas extern Bigint *Balloc ANSI((int));
598112158Sdas extern void Bfree ANSI((Bigint*));
599112158Sdas extern void ULtof ANSI((ULong*, ULong*, Long, int));
600112158Sdas extern void ULtod ANSI((ULong*, ULong*, Long, int));
601112158Sdas extern void ULtodd ANSI((ULong*, ULong*, Long, int));
602112158Sdas extern void ULtoQ ANSI((ULong*, ULong*, Long, int));
603112158Sdas extern void ULtox ANSI((UShort*, ULong*, Long, int));
604112158Sdas extern void ULtoxL ANSI((ULong*, ULong*, Long, int));
605112158Sdas extern ULong any_on ANSI((Bigint*, int));
606112158Sdas extern double b2d ANSI((Bigint*, int*));
607112158Sdas extern int cmp ANSI((Bigint*, Bigint*));
608112158Sdas extern void copybits ANSI((ULong*, int, Bigint*));
609112158Sdas extern Bigint *d2b ANSI((double, int*, int*));
610182709Sdas extern void decrement ANSI((Bigint*));
611112158Sdas extern Bigint *diff ANSI((Bigint*, Bigint*));
612112158Sdas extern char *dtoa ANSI((double d, int mode, int ndigits,
613112158Sdas			int *decpt, int *sign, char **rve));
614113279Sdas extern void freedtoa ANSI((char*));
615113144Sdas extern char *gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp,
616113144Sdas			  int mode, int ndigits, int *decpt, char **rve));
617187808Sdas extern char *g__fmt ANSI((char*, char*, char*, int, ULong, size_t));
618112158Sdas extern int gethex ANSI((CONST char**, FPI*, Long*, Bigint**, int));
619112158Sdas extern void hexdig_init_D2A(Void);
620112158Sdas extern int hexnan ANSI((CONST char**, FPI*, ULong*));
621112158Sdas extern int hi0bits ANSI((ULong));
622112158Sdas extern Bigint *i2b ANSI((int));
623112158Sdas extern Bigint *increment ANSI((Bigint*));
624112158Sdas extern int lo0bits ANSI((ULong*));
625112158Sdas extern Bigint *lshift ANSI((Bigint*, int));
626112158Sdas extern int match ANSI((CONST char**, char*));
627112158Sdas extern Bigint *mult ANSI((Bigint*, Bigint*));
628112158Sdas extern Bigint *multadd ANSI((Bigint*, int, int));
629112158Sdas extern char *nrv_alloc ANSI((char*, char **, int));
630112158Sdas extern Bigint *pow5mult ANSI((Bigint*, int));
631112158Sdas extern int quorem ANSI((Bigint*, Bigint*));
632112158Sdas extern double ratio ANSI((Bigint*, Bigint*));
633112158Sdas extern void rshift ANSI((Bigint*, int));
634112158Sdas extern char *rv_alloc ANSI((int));
635187808Sdas extern Bigint *s2b ANSI((CONST char*, int, int, ULong, int));
636112158Sdas extern Bigint *set_ones ANSI((Bigint*, int));
637112158Sdas extern char *strcp ANSI((char*, const char*));
638235785Stheraven extern int strtodg_l ANSI((CONST char*, char**, FPI*, Long*, ULong*, locale_t));
639113279Sdas
640113279Sdas extern int strtoId ANSI((CONST char *, char **, double *, double *));
641113279Sdas extern int strtoIdd ANSI((CONST char *, char **, double *, double *));
642113279Sdas extern int strtoIf ANSI((CONST char *, char **, float *, float *));
643112158Sdas extern int strtoIg ANSI((CONST char*, char**, FPI*, Long*, Bigint**, int*));
644113279Sdas extern int strtoIQ ANSI((CONST char *, char **, void *, void *));
645113279Sdas extern int strtoIx ANSI((CONST char *, char **, void *, void *));
646113279Sdas extern int strtoIxL ANSI((CONST char *, char **, void *, void *));
647112158Sdas extern double strtod ANSI((const char *s00, char **se));
648235785Stheraven extern double strtod_l ANSI((const char *s00, char **se, locale_t));
649113279Sdas extern int strtopQ ANSI((CONST char *, char **, Void *));
650113279Sdas extern int strtopf ANSI((CONST char *, char **, float *));
651113279Sdas extern int strtopd ANSI((CONST char *, char **, double *));
652113279Sdas extern int strtopdd ANSI((CONST char *, char **, double *));
653113279Sdas extern int strtopx ANSI((CONST char *, char **, Void *));
654113279Sdas extern int strtopxL ANSI((CONST char *, char **, Void *));
655235785Stheraven extern int strtord_l ANSI((CONST char *, char **, int, double *, locale_t));
656113279Sdas extern int strtordd ANSI((CONST char *, char **, int, double *));
657113279Sdas extern int strtorf ANSI((CONST char *, char **, int, float *));
658235785Stheraven extern int strtorQ_l ANSI((CONST char *, char **, int, void *, locale_t));
659235785Stheraven extern int strtorx_l ANSI((CONST char *, char **, int, void *, locale_t));
660113279Sdas extern int strtorxL ANSI((CONST char *, char **, int, void *));
661112158Sdas extern Bigint *sum ANSI((Bigint*, Bigint*));
662112158Sdas extern int trailz ANSI((Bigint*));
663219557Sdas extern double ulp ANSI((U*));
664112158Sdas
665112158Sdas#ifdef __cplusplus
666112158Sdas}
667112158Sdas#endif
668165746Sdas/*
669165746Sdas * NAN_WORD0 and NAN_WORD1 are only referenced in strtod.c.  Prior to
670165746Sdas * 20050115, they used to be hard-wired here (to 0x7ff80000 and 0,
671165746Sdas * respectively), but now are determined by compiling and running
672165746Sdas * qnan.c to generate gd_qnan.h, which specifies d_QNAN0 and d_QNAN1.
673165746Sdas * Formerly gdtoaimp.h recommended supplying suitable -DNAN_WORD0=...
674165746Sdas * and -DNAN_WORD1=...  values if necessary.  This should still work.
675165746Sdas * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
676165746Sdas */
677112158Sdas#ifdef IEEE_Arith
678187808Sdas#ifndef NO_INFNAN_CHECK
679187808Sdas#undef INFNAN_CHECK
680187808Sdas#define INFNAN_CHECK
681187808Sdas#endif
682112158Sdas#ifdef IEEE_MC68k
683112158Sdas#define _0 0
684112158Sdas#define _1 1
685165746Sdas#ifndef NAN_WORD0
686165746Sdas#define NAN_WORD0 d_QNAN0
687165746Sdas#endif
688165746Sdas#ifndef NAN_WORD1
689165746Sdas#define NAN_WORD1 d_QNAN1
690165746Sdas#endif
691112158Sdas#else
692112158Sdas#define _0 1
693112158Sdas#define _1 0
694165746Sdas#ifndef NAN_WORD0
695165746Sdas#define NAN_WORD0 d_QNAN1
696112158Sdas#endif
697165746Sdas#ifndef NAN_WORD1
698165746Sdas#define NAN_WORD1 d_QNAN0
699165746Sdas#endif
700165746Sdas#endif
701112158Sdas#else
702112158Sdas#undef INFNAN_CHECK
703112158Sdas#endif
704112158Sdas
705112158Sdas#undef SI
706112158Sdas#ifdef Sudden_Underflow
707112158Sdas#define SI 1
708112158Sdas#else
709112158Sdas#define SI 0
710112158Sdas#endif
711112158Sdas
712112158Sdas#endif /* GDTOAIMP_H_INCLUDED */
713