OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / mprec.h
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991 by AT&T.
6  *
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.
12  *
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.
17  *
18  ***************************************************************/
19
20 /* Please send bug reports to
21         David M. Gay
22         AT&T Bell Laboratories, Room 2C-463
23         600 Mountain Avenue
24         Murray Hill, NJ 07974-2070
25         U.S.A.
26         dmg@research.att.com or research!dmg
27  */
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include <config.h>
34 #include "ieeefp.h"
35
36 #include <math.h>
37 // #include <float.h>
38 // #include <errno.h>
39
40 /* These typedefs are true for the targets running Java. */
41
42 #ifndef HAVE_INT32_DEFINED
43 typedef int __int32_t;
44 typedef unsigned int __uint32_t;
45 #endif
46
47 #ifdef __IEEE_LITTLE_ENDIAN
48 #define IEEE_8087
49 #endif
50
51 #ifdef __IEEE_BIG_ENDIAN
52 #define IEEE_MC68k
53 #endif
54
55 #ifdef __Z8000__
56 #define Just_16
57 #endif
58
59 #ifdef DEBUG
60 #include "stdio.h"
61 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
62 #endif
63
64
65 #ifdef Unsigned_Shifts
66 #define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
67 #else
68 #define Sign_Extend(a,b) /*no-op*/
69 #endif
70
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.
73 #endif
74
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
78    the ANSI C spec.  */
79 union double_union
80 {
81   double d;
82   // FIXME: This should be some well-defined 32 bit type.
83   __uint32_t i[2];
84 };
85
86 #ifdef IEEE_8087
87 #define word0(x) (x.i[1])
88 #define word1(x) (x.i[0])
89 #else
90 #define word0(x) (x.i[0])
91 #define word1(x) (x.i[1])
92 #endif
93
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)
97  */
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++)
101 #else
102 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
103 ((unsigned short *)a)[1] = (unsigned short)c, a++)
104 #endif
105
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) */
111
112 #if defined(IEEE_8087) + defined(IEEE_MC68k)
113 #if defined (_DOUBLE_IS_32BITS) 
114 #define Exp_shift   23
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)
119 #define P           24
120 #define Bias        127
121 #if 0
122 #define IEEE_Arith  /* it is, but the code doesn't handle IEEE singles yet */
123 #endif
124 #define Emin        (-126)
125 #define Exp_1       ((__uint32_t)0x3f800000L)
126 #define Exp_11      ((__uint32_t)0x3f800000L)
127 #define Ebits       8
128 #define Frac_mask   ((__uint32_t)0x007fffffL)
129 #define Frac_mask1  ((__uint32_t)0x007fffffL)
130 #define Ten_pmax    10
131 #define Sign_bit    ((__uint32_t)0x80000000L)
132 #define Ten_pmax    10
133 #define Bletch      2
134 #define Bndry_mask  ((__uint32_t)0x007fffffL)
135 #define Bndry_mask1 ((__uint32_t)0x007fffffL)
136 #define LSB 1
137 #define Sign_bit    ((__uint32_t)0x80000000L)
138 #define Log2P       1
139 #define Tiny0       0
140 #define Tiny1       1
141 #define Quick_max   5
142 #define Int_max     6
143 #define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
144 #undef word0
145 #undef word1
146
147 #define word0(x) (x.i[0])
148 #define word1(x) 0
149 #else
150
151 #define Exp_shift  20
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)
156 #define P 53
157 #define Bias 1023
158 #define IEEE_Arith
159 #define Emin (-1022)
160 #define Exp_1  ((__uint32_t)0x3ff00000L)
161 #define Exp_11 ((__uint32_t)0x3ff00000L)
162 #define Ebits 11
163 #define Frac_mask  ((__uint32_t)0xfffffL)
164 #define Frac_mask1 ((__uint32_t)0xfffffL)
165 #define Ten_pmax 22
166 #define Bletch 0x10
167 #define Bndry_mask  ((__uint32_t)0xfffffL)
168 #define Bndry_mask1 ((__uint32_t)0xfffffL)
169 #define LSB 1
170 #define Sign_bit ((__uint32_t)0x80000000L)
171 #define Log2P 1
172 #define Tiny0 0
173 #define Tiny1 1
174 #define Quick_max 14
175 #define Int_max 14
176 #define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
177 #endif
178
179 #else
180 #undef  Sudden_Underflow
181 #define Sudden_Underflow
182 #ifdef IBM
183 #define Exp_shift  24
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)
188 #define P 14
189 #define Bias 65
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)
195 #define Bletch 4
196 #define Ten_pmax 22
197 #define Bndry_mask  ((__uint32_t)0xefffffL)
198 #define Bndry_mask1 ((__uint32_t)0xffffffL)
199 #define LSB 1
200 #define Sign_bit ((__uint32_t)0x80000000L)
201 #define Log2P 4
202 #define Tiny0 ((__uint32_t)0x100000L)
203 #define Tiny1 0
204 #define Quick_max 14
205 #define Int_max 15
206 #else /* VAX */
207 #define Exp_shift  23
208 #define Exp_shift1 7
209 #define Exp_msk1    0x80
210 #define Exp_msk11   ((__uint32_t)0x800000L)
211 #define Exp_mask  ((__uint32_t)0x7f80L)
212 #define P 56
213 #define Bias 129
214 #define Exp_1  ((__uint32_t)0x40800000L)
215 #define Exp_11 ((__uint32_t)0x4080L)
216 #define Ebits 8
217 #define Frac_mask  ((__uint32_t)0x7fffffL)
218 #define Frac_mask1 ((__uint32_t)0xffff007fL)
219 #define Ten_pmax 24
220 #define Bletch 2
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)
225 #define Log2P 1
226 #define Tiny0 0x80
227 #define Tiny1 0
228 #define Quick_max 15
229 #define Int_max 15
230 #endif
231 #endif
232
233 #ifndef IEEE_Arith
234 #define ROUND_BIASED
235 #endif
236
237 #ifdef RND_PRODQUOT
238 #define rounded_product(a,b) a = rnd_prod(a, b)
239 #define rounded_quotient(a,b) a = rnd_quot(a, b)
240 #ifdef KR_headers
241 extern double rnd_prod(), rnd_quot();
242 #else
243 extern double rnd_prod(double, double), rnd_quot(double, double);
244 #endif
245 #else
246 #define rounded_product(a,b) a *= b
247 #define rounded_quotient(a,b) a /= b
248 #endif
249
250 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
251 #define Big1 ((__uint32_t)0xffffffffL)
252
253 #ifndef Just_16
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.
258  */
259
260 #ifndef Pack_32
261 #define Pack_32
262 #endif
263 #endif
264
265
266 #define MAX_BIGNUMS 16
267 #define MAX_BIGNUM_WDS 32
268
269 struct _Jv_Bigint 
270 {
271   struct _Jv_Bigint *_next;
272   int _k, _maxwds, _sign, _wds;
273   unsigned long _x[MAX_BIGNUM_WDS];
274 };
275
276
277 #define _PTR            void *
278 #define _AND            ,
279 #define _NOARGS         void
280 #define _CONST          const
281 #define _VOLATILE       volatile
282 #define _SIGNED         signed
283 #define _DOTS           , ...
284 #define _VOID void
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)
289
290
291 struct _Jv_reent
292 {
293   /* local copy of errno */
294   int _errno;
295
296   /* used by mprec routines */
297   struct _Jv_Bigint *_result;
298   int _result_k;
299   struct _Jv_Bigint *_p5s;
300
301   struct _Jv_Bigint _freelist[MAX_BIGNUMS];
302   int _allocation_map;
303
304   int num;
305 };
306
307
308 typedef struct _Jv_Bigint _Jv_Bigint;
309
310 #define Balloc  _Jv_Balloc
311 #define Bfree   _Jv_Bfree
312 #define multadd _Jv_multadd
313 #define s2b     _Jv_s2b
314 #define lo0bits _Jv_lo0bits
315 #define hi0bits _Jv_hi0bits
316 #define i2b     _Jv_i2b
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
322 #define ulp     _Jv_ulp
323 #define b2d     _Jv_b2d
324 #define d2b     _Jv_d2b
325 #define ratio   _Jv_ratio
326
327 #define tens _Jv__mprec_tens
328 #define bigtens _Jv__mprec_bigtens
329 #define tinytens _Jv__mprec_tinytens
330
331 #define _dtoa _Jv_dtoa
332 #define _dtoa_r _Jv_dtoa_r
333 #define _strtod_r _Jv_strtod_r
334
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));
341
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));
357
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))
360
361 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
362 #define n_bigtens 2
363 #else
364 #define n_bigtens 5
365 #endif
366
367 extern _CONST double tinytens[];
368 extern _CONST double bigtens[];
369 extern _CONST double tens[];
370
371 #ifdef __cplusplus
372 }
373 #endif
374