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
29 #ifndef __CLASSPATH_MPREC_H__
30 #define __CLASSPATH_MPREC_H__
35 #if defined HAVE_STDINT_H
37 #elif defined HAVE_INTTYPES_H
41 #if defined HAVE_SYS_TYPES_H
42 #include <sys/types.h>
45 #if defined HAVE_SYS_CONFIG_H
46 #include <sys/config.h>
53 /* ISO C99 int type declarations */
55 #if !defined HAVE_INT32_DEFINED && defined HAVE_BSD_INT32_DEFINED
56 typedef u_int32_t uint32_t;
59 #if !defined HAVE_BSD_INT32_DEFINED && !defined HAVE_INT32_DEFINED
60 /* FIXME this could have problems with systems that don't define SI to be 4 */
61 typedef int int32_t __attribute__((mode(SI)));
63 /* This is a blatant hack: on Solaris 2.5, pthread.h defines uint32_t
64 in pthread.h, which we sometimes include. We protect our
65 definition the same way Solaris 2.5 does, to avoid redefining it. */
67 typedef unsigned int uint32_t __attribute__((mode(SI)));
71 /* These typedefs are true for the targets running Java. */
73 #ifdef __IEEE_LITTLE_ENDIAN
77 #ifdef __IEEE_BIG_ENDIAN
87 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
91 #ifdef Unsigned_Shifts
92 #define Sign_Extend(a,b) if (b < 0) a |= (uint32_t)0xffff0000;
94 #define Sign_Extend(a,b) /*no-op*/
97 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
98 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
101 /* If we are going to examine or modify specific bits in a double using
102 the word0 and/or word1 macros, then we must wrap the double inside
103 a union. This is necessary to avoid undefined behavior according to
112 #define word0(x) (x.i[1])
113 #define word1(x) (x.i[0])
115 #define word0(x) (x.i[0])
116 #define word1(x) (x.i[1])
119 /* The following definition of Storeinc is appropriate for MIPS processors.
120 * An alternative that might be better on some machines is
121 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
123 #if defined(IEEE_8087) + defined(VAX)
124 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
125 ((unsigned short *)a)[0] = (unsigned short)c, a++)
127 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
128 ((unsigned short *)a)[1] = (unsigned short)c, a++)
131 /* #define P DBL_MANT_DIG */
132 /* Ten_pmax = floor(P*log(2)/log(5)) */
133 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
134 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
135 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
137 #if defined(IEEE_8087) + defined(IEEE_MC68k)
138 #if defined (_DOUBLE_IS_32BITS)
140 #define Exp_shift1 23
141 #define Exp_msk1 ((uint32_t)0x00800000L)
142 #define Exp_msk11 ((uint32_t)0x00800000L)
143 #define Exp_mask ((uint32_t)0x7f800000L)
147 #define IEEE_Arith /* it is, but the code doesn't handle IEEE singles yet */
150 #define Exp_1 ((uint32_t)0x3f800000L)
151 #define Exp_11 ((uint32_t)0x3f800000L)
153 #define Frac_mask ((uint32_t)0x007fffffL)
154 #define Frac_mask1 ((uint32_t)0x007fffffL)
156 #define Sign_bit ((uint32_t)0x80000000L)
159 #define Bndry_mask ((uint32_t)0x007fffffL)
160 #define Bndry_mask1 ((uint32_t)0x007fffffL)
162 #define Sign_bit ((uint32_t)0x80000000L)
168 #define Infinite(x) (word0(x) == ((uint32_t)0x7f800000L))
172 #define word0(x) (x.i[0])
177 #define Exp_shift1 20
178 #define Exp_msk1 ((uint32_t)0x100000L)
179 #define Exp_msk11 ((uint32_t)0x100000L)
180 #define Exp_mask ((uint32_t)0x7ff00000L)
185 #define Exp_1 ((uint32_t)0x3ff00000L)
186 #define Exp_11 ((uint32_t)0x3ff00000L)
188 #define Frac_mask ((uint32_t)0xfffffL)
189 #define Frac_mask1 ((uint32_t)0xfffffL)
192 #define Bndry_mask ((uint32_t)0xfffffL)
193 #define Bndry_mask1 ((uint32_t)0xfffffL)
195 #define Sign_bit ((uint32_t)0x80000000L)
201 #define Infinite(x) (word0(x) == ((uint32_t)0x7ff00000L)) /* sufficient test for here */
205 #undef Sudden_Underflow
206 #define Sudden_Underflow
209 #define Exp_shift1 24
210 #define Exp_msk1 ((uint32_t)0x1000000L)
211 #define Exp_msk11 ((uint32_t)0x1000000L)
212 #define Exp_mask ((uint32_t)0x7f000000L)
215 #define Exp_1 ((uint32_t)0x41000000L)
216 #define Exp_11 ((uint32_t)0x41000000L)
217 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
218 #define Frac_mask ((uint32_t)0xffffffL)
219 #define Frac_mask1 ((uint32_t)0xffffffL)
222 #define Bndry_mask ((uint32_t)0xefffffL)
223 #define Bndry_mask1 ((uint32_t)0xffffffL)
225 #define Sign_bit ((uint32_t)0x80000000L)
227 #define Tiny0 ((uint32_t)0x100000L)
234 #define Exp_msk1 0x80
235 #define Exp_msk11 ((uint32_t)0x800000L)
236 #define Exp_mask ((uint32_t)0x7f80L)
239 #define Exp_1 ((uint32_t)0x40800000L)
240 #define Exp_11 ((uint32_t)0x4080L)
242 #define Frac_mask ((uint32_t)0x7fffffL)
243 #define Frac_mask1 ((uint32_t)0xffff007fL)
246 #define Bndry_mask ((uint32_t)0xffff007fL)
247 #define Bndry_mask1 ((uint32_t)0xffff007fL)
248 #define LSB ((uint32_t)0x10000L)
249 #define Sign_bit ((uint32_t)0x8000L)
263 #define rounded_product(a,b) a = rnd_prod(a, b)
264 #define rounded_quotient(a,b) a = rnd_quot(a, b)
266 extern double rnd_prod(), rnd_quot();
268 extern double rnd_prod(double, double), rnd_quot(double, double);
271 #define rounded_product(a,b) a *= b
272 #define rounded_quotient(a,b) a /= b
275 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
276 #define Big1 ((uint32_t)0xffffffffL)
278 /* TWISTI to avoid bugs, we should use Just_16 on every platform */
282 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
283 * This makes some inner loops simpler and sometimes saves work
284 * during multiplications, but it often seems to make things slightly
285 * slower. Hence the default is now to store 32 bits per long.
289 #if SIZEOF_VOID_P != 8
296 #define MAX_BIGNUMS 16
297 /* TWISTI this does not work at least on alpha */
298 /* #define MAX_BIGNUM_WDS 32 */
299 #define MAX_BIGNUM_WDS 64
303 struct _Jv_Bigint *_next;
304 int _k, _maxwds, _sign, _wds;
305 unsigned long _x[MAX_BIGNUM_WDS];
313 #define _VOLATILE volatile
314 #define _SIGNED signed
317 #define _EXFUN(name, proto) name proto
318 #define _DEFUN(name, arglist, args) name(args)
319 #define _DEFUN_VOID(name) name(_NOARGS)
320 #define _CAST_VOID (void)
325 /* local copy of errno */
328 /* used by mprec routines */
329 struct _Jv_Bigint *_result;
331 struct _Jv_Bigint *_p5s;
333 struct _Jv_Bigint _freelist[MAX_BIGNUMS];
340 typedef struct _Jv_Bigint _Jv_Bigint;
342 #define Balloc _Jv_Balloc
343 #define Bfree _Jv_Bfree
344 #define multadd _Jv_multadd
346 #define lo0bits _Jv_lo0bits
347 #define hi0bits _Jv_hi0bits
349 #define mult _Jv_mult
350 #define pow5mult _Jv_pow5mult
351 #define lshift _Jv_lshift
352 #define cmp _Jv__mcmp
353 #define diff _Jv__mdiff
357 #define ratio _Jv_ratio
359 #define tens _Jv__mprec_tens
360 #define bigtens _Jv__mprec_bigtens
361 #define tinytens _Jv__mprec_tinytens
363 #define _dtoa _Jv_dtoa
364 #define _dtoa_r _Jv_dtoa_r
365 #define _strtod_r _Jv_strtod_r
367 extern double _EXFUN(_strtod_r, (struct _Jv_reent *ptr, const char *s00, char **se));
368 extern char* _EXFUN(_dtoa_r, (struct _Jv_reent *ptr, double d,
369 int mode, int ndigits, int *decpt, int *sign,
370 char **rve, int float_type));
371 void _EXFUN(_dtoa, (double d, int mode, int ndigits, int *decpt, int *sign,
372 char **rve, char *buf, int float_type));
374 double _EXFUN(ulp,(double x));
375 double _EXFUN(b2d,(_Jv_Bigint *a , int *e));
376 _Jv_Bigint * _EXFUN(Balloc,(struct _Jv_reent *p, int k));
377 void _EXFUN(Bfree,(struct _Jv_reent *p, _Jv_Bigint *v));
378 _Jv_Bigint * _EXFUN(multadd,(struct _Jv_reent *p, _Jv_Bigint *, int, int));
379 _Jv_Bigint * _EXFUN(s2b,(struct _Jv_reent *, const char*, int, int, unsigned long));
380 _Jv_Bigint * _EXFUN(i2b,(struct _Jv_reent *,int));
381 _Jv_Bigint * _EXFUN(mult, (struct _Jv_reent *, _Jv_Bigint *, _Jv_Bigint *));
382 _Jv_Bigint * _EXFUN(pow5mult, (struct _Jv_reent *, _Jv_Bigint *, int k));
383 int _EXFUN(hi0bits,(unsigned long));
384 int _EXFUN(lo0bits,(unsigned long *));
385 _Jv_Bigint * _EXFUN(d2b,(struct _Jv_reent *p, double d, int *e, int *bits));
386 _Jv_Bigint * _EXFUN(lshift,(struct _Jv_reent *p, _Jv_Bigint *b, int k));
387 _Jv_Bigint * _EXFUN(diff,(struct _Jv_reent *p, _Jv_Bigint *a, _Jv_Bigint *b));
388 int _EXFUN(cmp,(_Jv_Bigint *a, _Jv_Bigint *b));
390 double _EXFUN(ratio,(_Jv_Bigint *a, _Jv_Bigint *b));
391 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(long) + 2*sizeof(int))
393 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
399 extern _CONST double tinytens[];
400 extern _CONST double bigtens[];
401 extern _CONST double tens[];
407 #endif /* __CLASSPATH_MPREC_H__ */