1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2000 by AT&T.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* Please send bug reports to
22 AT&T Bell Laboratories, Room 2C-463
24 Murray Hill, NJ 07974-2070
26 dmg@research.att.com or research!dmg
36 #if defined HAVE_STDINT_H
38 #elif defined HAVE_INTTYPES_H
42 #if defined HAVE_SYS_TYPES_H
43 #include <sys/types.h>
46 #if defined HAVE_SYS_CONFIG_H
47 #include <sys/config.h>
54 /* ISO C99 int type declarations */
56 #if !defined HAVE_INT32_DEFINED && defined HAVE_BSD_INT32_DEFINED
57 typedef u_int32_t
uint32_t;
60 #if !defined HAVE_BSD_INT32_DEFINED && !defined HAVE_INT32_DEFINED
61 // FIXME -- this could have problems with systems that don't define SI to be 4
62 typedef int int32_t __attribute__((mode(SI
)));
64 /* This is a blatant hack: on Solaris 2.5, pthread.h defines uint32_t
65 in pthread.h, which we sometimes include. We protect our
66 definition the same way Solaris 2.5 does, to avoid redefining it. */
68 typedef unsigned int uint32_t __attribute__((mode(SI
)));
72 /* These typedefs are true for the targets running Java. */
74 #ifdef __IEEE_LITTLE_ENDIAN
78 #ifdef __IEEE_BIG_ENDIAN
88 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
92 #ifdef Unsigned_Shifts
93 #define Sign_Extend(a,b) if (b < 0) a |= (uint32_t)0xffff0000;
95 #define Sign_Extend(a,b) /*no-op*/
98 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
99 Exactly one of IEEE_8087
, IEEE_MC68k
, VAX
, or IBM should be defined
.
102 /* If we are going to examine or modify specific bits in a double using
103 the word0 and/or word1 macros, then we must wrap the double inside
104 a union. This is necessary to avoid undefined behavior according to
113 #define word0(x) (x.i[1])
114 #define word1(x) (x.i[0])
116 #define word0(x) (x.i[0])
117 #define word1(x) (x.i[1])
120 /* The following definition of Storeinc is appropriate for MIPS processors.
121 * An alternative that might be better on some machines is
122 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
124 #if defined(IEEE_8087) + defined(VAX)
125 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
126 ((unsigned short *)a)[0] = (unsigned short)c, a++)
128 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
129 ((unsigned short *)a)[1] = (unsigned short)c, a++)
132 /* #define P DBL_MANT_DIG */
133 /* Ten_pmax = floor(P*log(2)/log(5)) */
134 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
135 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
136 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
138 #if defined(IEEE_8087) + defined(IEEE_MC68k)
139 #if defined (_DOUBLE_IS_32BITS)
141 #define Exp_shift1 23
142 #define Exp_msk1 ((uint32_t)0x00800000L)
143 #define Exp_msk11 ((uint32_t)0x00800000L)
144 #define Exp_mask ((uint32_t)0x7f800000L)
148 #define IEEE_Arith /* it is, but the code doesn't handle IEEE singles yet */
151 #define Exp_1 ((uint32_t)0x3f800000L)
152 #define Exp_11 ((uint32_t)0x3f800000L)
154 #define Frac_mask ((uint32_t)0x007fffffL)
155 #define Frac_mask1 ((uint32_t)0x007fffffL)
157 #define Sign_bit ((uint32_t)0x80000000L)
160 #define Bndry_mask ((uint32_t)0x007fffffL)
161 #define Bndry_mask1 ((uint32_t)0x007fffffL)
163 #define Sign_bit ((uint32_t)0x80000000L)
169 #define Infinite(x) (word0(x) == ((uint32_t)0x7f800000L))
173 #define word0(x) (x.i[0])
178 #define Exp_shift1 20
179 #define Exp_msk1 ((uint32_t)0x100000L)
180 #define Exp_msk11 ((uint32_t)0x100000L)
181 #define Exp_mask ((uint32_t)0x7ff00000L)
186 #define Exp_1 ((uint32_t)0x3ff00000L)
187 #define Exp_11 ((uint32_t)0x3ff00000L)
189 #define Frac_mask ((uint32_t)0xfffffL)
190 #define Frac_mask1 ((uint32_t)0xfffffL)
193 #define Bndry_mask ((uint32_t)0xfffffL)
194 #define Bndry_mask1 ((uint32_t)0xfffffL)
196 #define Sign_bit ((uint32_t)0x80000000L)
202 #define Infinite(x) (word0(x) == ((uint32_t)0x7ff00000L)) /* sufficient test for here */
206 #undef Sudden_Underflow
207 #define Sudden_Underflow
210 #define Exp_shift1 24
211 #define Exp_msk1 ((uint32_t)0x1000000L)
212 #define Exp_msk11 ((uint32_t)0x1000000L)
213 #define Exp_mask ((uint32_t)0x7f000000L)
216 #define Exp_1 ((uint32_t)0x41000000L)
217 #define Exp_11 ((uint32_t)0x41000000L)
218 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
219 #define Frac_mask ((uint32_t)0xffffffL)
220 #define Frac_mask1 ((uint32_t)0xffffffL)
223 #define Bndry_mask ((uint32_t)0xefffffL)
224 #define Bndry_mask1 ((uint32_t)0xffffffL)
226 #define Sign_bit ((uint32_t)0x80000000L)
228 #define Tiny0 ((uint32_t)0x100000L)
235 #define Exp_msk1 0x80
236 #define Exp_msk11 ((uint32_t)0x800000L)
237 #define Exp_mask ((uint32_t)0x7f80L)
240 #define Exp_1 ((uint32_t)0x40800000L)
241 #define Exp_11 ((uint32_t)0x4080L)
243 #define Frac_mask ((uint32_t)0x7fffffL)
244 #define Frac_mask1 ((uint32_t)0xffff007fL)
247 #define Bndry_mask ((uint32_t)0xffff007fL)
248 #define Bndry_mask1 ((uint32_t)0xffff007fL)
249 #define LSB ((uint32_t)0x10000L)
250 #define Sign_bit ((uint32_t)0x8000L)
264 #define rounded_product(a,b) a = rnd_prod(a, b)
265 #define rounded_quotient(a,b) a = rnd_quot(a, b)
267 extern double rnd_prod(), rnd_quot();
269 extern double rnd_prod(double, double), rnd_quot(double, double);
272 #define rounded_product(a,b) a *= b
273 #define rounded_quotient(a,b) a /= b
276 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
277 #define Big1 ((uint32_t)0xffffffffL)
280 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
281 * This makes some inner loops simpler and sometimes saves work
282 * during multiplications, but it often seems to make things slightly
283 * slower. Hence the default is now to store 32 bits per long.
287 #if SIZEOF_VOID_P != 8
294 #define MAX_BIGNUMS 16
295 #define MAX_BIGNUM_WDS 32
299 struct _Jv_Bigint
*_next
;
300 int _k
, _maxwds
, _sign
, _wds
;
301 unsigned long _x
[MAX_BIGNUM_WDS
];
309 #define _VOLATILE volatile
310 #define _SIGNED signed
313 #define _EXFUN(name, proto) name proto
314 #define _DEFUN(name, arglist, args) name(args)
315 #define _DEFUN_VOID(name) name(_NOARGS)
316 #define _CAST_VOID (void)
321 /* local copy of errno */
324 /* used by mprec routines */
325 struct _Jv_Bigint
*_result
;
327 struct _Jv_Bigint
*_p5s
;
329 struct _Jv_Bigint _freelist
[MAX_BIGNUMS
];
336 typedef struct _Jv_Bigint _Jv_Bigint
;
338 #define Balloc _Jv_Balloc
339 #define Bfree _Jv_Bfree
340 #define multadd _Jv_multadd
342 #define lo0bits _Jv_lo0bits
343 #define hi0bits _Jv_hi0bits
345 #define mult _Jv_mult
346 #define pow5mult _Jv_pow5mult
347 #define lshift _Jv_lshift
348 #define cmp _Jv__mcmp
349 #define diff _Jv__mdiff
353 #define ratio _Jv_ratio
355 #define tens _Jv__mprec_tens
356 #define bigtens _Jv__mprec_bigtens
357 #define tinytens _Jv__mprec_tinytens
359 #define _dtoa _Jv_dtoa
360 #define _dtoa_r _Jv_dtoa_r
361 #define _strtod_r _Jv_strtod_r
363 extern double _EXFUN(_strtod_r
, (struct _Jv_reent
*ptr
, const char *s00
, char **se
));
364 extern char* _EXFUN(_dtoa_r
, (struct _Jv_reent
*ptr
, double d
,
365 int mode
, int ndigits
, int *decpt
, int *sign
,
366 char **rve
, int float_type
));
367 void _EXFUN(_dtoa
, (double d
, int mode
, int ndigits
, int *decpt
, int *sign
,
368 char **rve
, char *buf
, int float_type
));
370 double _EXFUN(ulp
,(double x
));
371 double _EXFUN(b2d
,(_Jv_Bigint
*a
, int *e
));
372 _Jv_Bigint
* _EXFUN(Balloc
,(struct _Jv_reent
*p
, int k
));
373 void _EXFUN(Bfree
,(struct _Jv_reent
*p
, _Jv_Bigint
*v
));
374 _Jv_Bigint
* _EXFUN(multadd
,(struct _Jv_reent
*p
, _Jv_Bigint
*, int, int));
375 _Jv_Bigint
* _EXFUN(s2b
,(struct _Jv_reent
*, const char*, int, int, unsigned long));
376 _Jv_Bigint
* _EXFUN(i2b
,(struct _Jv_reent
*,int));
377 _Jv_Bigint
* _EXFUN(mult
, (struct _Jv_reent
*, _Jv_Bigint
*, _Jv_Bigint
*));
378 _Jv_Bigint
* _EXFUN(pow5mult
, (struct _Jv_reent
*, _Jv_Bigint
*, int k
));
379 int _EXFUN(hi0bits
,(unsigned long));
380 int _EXFUN(lo0bits
,(unsigned long *));
381 _Jv_Bigint
* _EXFUN(d2b
,(struct _Jv_reent
*p
, double d
, int *e
, int *bits
));
382 _Jv_Bigint
* _EXFUN(lshift
,(struct _Jv_reent
*p
, _Jv_Bigint
*b
, int k
));
383 _Jv_Bigint
* _EXFUN(diff
,(struct _Jv_reent
*p
, _Jv_Bigint
*a
, _Jv_Bigint
*b
));
384 int _EXFUN(cmp
,(_Jv_Bigint
*a
, _Jv_Bigint
*b
));
386 double _EXFUN(ratio
,(_Jv_Bigint
*a
, _Jv_Bigint
*b
));
387 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(long) + 2*sizeof(int))
389 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
395 extern _CONST
double tinytens
[];
396 extern _CONST
double bigtens
[];
397 extern _CONST
double tens
[];