1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991 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
40 /* These typedefs are true for the targets running Java. */
42 #ifndef HAVE_INT32_DEFINED
43 typedef int __int32_t;
44 typedef unsigned int __uint32_t;
47 #ifdef __IEEE_LITTLE_ENDIAN
51 #ifdef __IEEE_BIG_ENDIAN
61 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
65 #ifdef Unsigned_Shifts
66 #define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
68 #define Sign_Extend(a,b) /*no-op*/
71 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
72 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
75 /* If we are going to examine or modify specific bits in a double using
76 the word0 and/or word1 macros, then we must wrap the double inside
77 a union. This is necessary to avoid undefined behavior according to
82 // FIXME: This should be some well-defined 32 bit type.
87 #define word0(x) (x.i[1])
88 #define word1(x) (x.i[0])
90 #define word0(x) (x.i[0])
91 #define word1(x) (x.i[1])
94 /* The following definition of Storeinc is appropriate for MIPS processors.
95 * An alternative that might be better on some machines is
96 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
98 #if defined(IEEE_8087) + defined(VAX)
99 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
100 ((unsigned short *)a)[0] = (unsigned short)c, a++)
102 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
103 ((unsigned short *)a)[1] = (unsigned short)c, a++)
106 /* #define P DBL_MANT_DIG */
107 /* Ten_pmax = floor(P*log(2)/log(5)) */
108 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
109 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
110 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
112 #if defined(IEEE_8087) + defined(IEEE_MC68k)
113 #if defined (_DOUBLE_IS_32BITS)
115 #define Exp_shift1 23
116 #define Exp_msk1 ((__uint32_t)0x00800000L)
117 #define Exp_msk11 ((__uint32_t)0x00800000L)
118 #define Exp_mask ((__uint32_t)0x7f800000L)
122 #define IEEE_Arith /* it is, but the code doesn't handle IEEE singles yet */
125 #define Exp_1 ((__uint32_t)0x3f800000L)
126 #define Exp_11 ((__uint32_t)0x3f800000L)
128 #define Frac_mask ((__uint32_t)0x007fffffL)
129 #define Frac_mask1 ((__uint32_t)0x007fffffL)
131 #define Sign_bit ((__uint32_t)0x80000000L)
134 #define Bndry_mask ((__uint32_t)0x007fffffL)
135 #define Bndry_mask1 ((__uint32_t)0x007fffffL)
137 #define Sign_bit ((__uint32_t)0x80000000L)
143 #define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
147 #define word0(x) (x.i[0])
152 #define Exp_shift1 20
153 #define Exp_msk1 ((__uint32_t)0x100000L)
154 #define Exp_msk11 ((__uint32_t)0x100000L)
155 #define Exp_mask ((__uint32_t)0x7ff00000L)
160 #define Exp_1 ((__uint32_t)0x3ff00000L)
161 #define Exp_11 ((__uint32_t)0x3ff00000L)
163 #define Frac_mask ((__uint32_t)0xfffffL)
164 #define Frac_mask1 ((__uint32_t)0xfffffL)
167 #define Bndry_mask ((__uint32_t)0xfffffL)
168 #define Bndry_mask1 ((__uint32_t)0xfffffL)
170 #define Sign_bit ((__uint32_t)0x80000000L)
176 #define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
180 #undef Sudden_Underflow
181 #define Sudden_Underflow
184 #define Exp_shift1 24
185 #define Exp_msk1 ((__uint32_t)0x1000000L)
186 #define Exp_msk11 ((__uint32_t)0x1000000L)
187 #define Exp_mask ((__uint32_t)0x7f000000L)
190 #define Exp_1 ((__uint32_t)0x41000000L)
191 #define Exp_11 ((__uint32_t)0x41000000L)
192 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
193 #define Frac_mask ((__uint32_t)0xffffffL)
194 #define Frac_mask1 ((__uint32_t)0xffffffL)
197 #define Bndry_mask ((__uint32_t)0xefffffL)
198 #define Bndry_mask1 ((__uint32_t)0xffffffL)
200 #define Sign_bit ((__uint32_t)0x80000000L)
202 #define Tiny0 ((__uint32_t)0x100000L)
209 #define Exp_msk1 0x80
210 #define Exp_msk11 ((__uint32_t)0x800000L)
211 #define Exp_mask ((__uint32_t)0x7f80L)
214 #define Exp_1 ((__uint32_t)0x40800000L)
215 #define Exp_11 ((__uint32_t)0x4080L)
217 #define Frac_mask ((__uint32_t)0x7fffffL)
218 #define Frac_mask1 ((__uint32_t)0xffff007fL)
221 #define Bndry_mask ((__uint32_t)0xffff007fL)
222 #define Bndry_mask1 ((__uint32_t)0xffff007fL)
223 #define LSB ((__uint32_t)0x10000L)
224 #define Sign_bit ((__uint32_t)0x8000L)
238 #define rounded_product(a,b) a = rnd_prod(a, b)
239 #define rounded_quotient(a,b) a = rnd_quot(a, b)
241 extern double rnd_prod(), rnd_quot();
243 extern double rnd_prod(double, double), rnd_quot(double, double);
246 #define rounded_product(a,b) a *= b
247 #define rounded_quotient(a,b) a /= b
250 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
251 #define Big1 ((__uint32_t)0xffffffffL)
254 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
255 * This makes some inner loops simpler and sometimes saves work
256 * during multiplications, but it often seems to make things slightly
257 * slower. Hence the default is now to store 32 bits per long.
266 #define MAX_BIGNUMS 16
267 #define MAX_BIGNUM_WDS 32
271 struct _Jv_Bigint *_next;
272 int _k, _maxwds, _sign, _wds;
273 unsigned long _x[MAX_BIGNUM_WDS];
281 #define _VOLATILE volatile
282 #define _SIGNED signed
285 #define _EXFUN(name, proto) name proto
286 #define _DEFUN(name, arglist, args) name(args)
287 #define _DEFUN_VOID(name) name(_NOARGS)
288 #define _CAST_VOID (void)
293 /* local copy of errno */
296 /* used by mprec routines */
297 struct _Jv_Bigint *_result;
299 struct _Jv_Bigint *_p5s;
301 struct _Jv_Bigint _freelist[MAX_BIGNUMS];
308 typedef struct _Jv_Bigint _Jv_Bigint;
310 #define Balloc _Jv_Balloc
311 #define Bfree _Jv_Bfree
312 #define multadd _Jv_multadd
314 #define lo0bits _Jv_lo0bits
315 #define hi0bits _Jv_hi0bits
317 #define mult _Jv_mult
318 #define pow5mult _Jv_pow5mult
319 #define lshift _Jv_lshift
320 #define cmp _Jv__mcmp
321 #define diff _Jv__mdiff
325 #define ratio _Jv_ratio
327 #define tens _Jv__mprec_tens
328 #define bigtens _Jv__mprec_bigtens
329 #define tinytens _Jv__mprec_tinytens
331 #define _dtoa _Jv_dtoa
332 #define _dtoa_r _Jv_dtoa_r
333 #define _strtod_r _Jv_strtod_r
335 extern double _EXFUN(_strtod_r, (struct _Jv_reent *ptr, const char *s00, char **se));
336 extern char* _EXFUN(_dtoa_r, (struct _Jv_reent *ptr, double d,
337 int mode, int ndigits, int *decpt, int *sign,
338 char **rve, int float_type));
339 void _EXFUN(_dtoa, (double d, int mode, int ndigits, int *decpt, int *sign,
340 char **rve, char *buf, int float_type));
342 double _EXFUN(ulp,(double x));
343 double _EXFUN(b2d,(_Jv_Bigint *a , int *e));
344 _Jv_Bigint * _EXFUN(Balloc,(struct _Jv_reent *p, int k));
345 void _EXFUN(Bfree,(struct _Jv_reent *p, _Jv_Bigint *v));
346 _Jv_Bigint * _EXFUN(multadd,(struct _Jv_reent *p, _Jv_Bigint *, int, int));
347 _Jv_Bigint * _EXFUN(s2b,(struct _Jv_reent *, const char*, int, int, unsigned long));
348 _Jv_Bigint * _EXFUN(i2b,(struct _Jv_reent *,int));
349 _Jv_Bigint * _EXFUN(mult, (struct _Jv_reent *, _Jv_Bigint *, _Jv_Bigint *));
350 _Jv_Bigint * _EXFUN(pow5mult, (struct _Jv_reent *, _Jv_Bigint *, int k));
351 int _EXFUN(hi0bits,(unsigned long));
352 int _EXFUN(lo0bits,(unsigned long *));
353 _Jv_Bigint * _EXFUN(d2b,(struct _Jv_reent *p, double d, int *e, int *bits));
354 _Jv_Bigint * _EXFUN(lshift,(struct _Jv_reent *p, _Jv_Bigint *b, int k));
355 _Jv_Bigint * _EXFUN(diff,(struct _Jv_reent *p, _Jv_Bigint *a, _Jv_Bigint *b));
356 int _EXFUN(cmp,(_Jv_Bigint *a, _Jv_Bigint *b));
358 double _EXFUN(ratio,(_Jv_Bigint *a, _Jv_Bigint *b));
359 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(long) + 2*sizeof(int))
361 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
367 extern _CONST double tinytens[];
368 extern _CONST double bigtens[];
369 extern _CONST double tens[];