PR149: Used wrong class loader.
[cacao.git] / src / fdlibm / mprec.h
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991, 2000 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 #ifndef __CLASSPATH_MPREC_H__
30 #define __CLASSPATH_MPREC_H__
31
32 #include <config.h>
33 /* #include "config-int.h" */
34 #include <stdint.h>
35 #include "ieeefp.h"
36 /* CLASSPATH LOCAL */
37 /* #include "namespace.h" */
38
39 #if defined HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42
43 #if defined HAVE_SYS_CONFIG_H
44 #include <sys/config.h>
45 #endif
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51   /* These typedefs are true for the targets running Java. */
52
53 #ifdef __IEEE_LITTLE_ENDIAN
54 #define IEEE_8087
55 #endif
56
57 #ifdef __IEEE_BIG_ENDIAN
58 #define IEEE_MC68k
59 #endif
60
61 #ifdef __Z8000__
62 #define Just_16
63 #endif
64
65 #ifdef DEBUG
66 #include "stdio.h"
67 #include <stdlib.h>
68 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
69 #endif
70
71
72 #ifdef Unsigned_Shifts
73 #define Sign_Extend(a,b) if (b < 0) a |= (uint32_t)0xffff0000;
74 #else
75 #define Sign_Extend(a,b) /*no-op*/
76 #endif
77
78 #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
79 Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
80 #endif
81
82 /* If we are going to examine or modify specific bits in a double using
83    the word0 and/or word1 macros, then we must wrap the double inside
84    a union.  This is necessary to avoid undefined behavior according to
85    the ANSI C spec.  */
86 union double_union
87 {
88   double d;
89   uint32_t i[2];
90 };
91
92 #ifdef IEEE_8087
93 #define word0(x) (x.i[1])
94 #define word1(x) (x.i[0])
95 #else
96 #define word0(x) (x.i[0])
97 #define word1(x) (x.i[1])
98 #endif
99
100 /* The following definition of Storeinc is appropriate for MIPS processors.
101  * An alternative that might be better on some machines is
102  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
103  */
104 #if defined(__IEEE_BYTES_LITTLE_ENDIAN) + defined(IEEE_8087) + defined(VAX)
105 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
106 ((unsigned short *)a)[0] = (unsigned short)c, a++)
107 #else
108 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
109 ((unsigned short *)a)[1] = (unsigned short)c, a++)
110 #endif
111
112 /* #define P DBL_MANT_DIG */
113 /* Ten_pmax = floor(P*log(2)/log(5)) */
114 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
115 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
116 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
117
118 #if defined(IEEE_8087) + defined(IEEE_MC68k)
119 #if defined (_DOUBLE_IS_32BITS)
120 #define Exp_shift   23
121 #define Exp_shift1  23
122 #define Exp_msk1    ((uint32_t)0x00800000L)
123 #define Exp_msk11   ((uint32_t)0x00800000L)
124 #define Exp_mask    ((uint32_t)0x7f800000L)
125 #define P           24
126 #define Bias        127
127 #if 0
128 #define IEEE_Arith  /* it is, but the code doesn't handle IEEE singles yet */
129 #endif
130 #define Emin        (-126)
131 #define Exp_1       ((uint32_t)0x3f800000L)
132 #define Exp_11      ((uint32_t)0x3f800000L)
133 #define Ebits       8
134 #define Frac_mask   ((uint32_t)0x007fffffL)
135 #define Frac_mask1  ((uint32_t)0x007fffffL)
136 #define Ten_pmax    10
137 #define Sign_bit    ((uint32_t)0x80000000L)
138 #define Ten_pmax    10
139 #define Bletch      2
140 #define Bndry_mask  ((uint32_t)0x007fffffL)
141 #define Bndry_mask1 ((uint32_t)0x007fffffL)
142 #define LSB 1
143 #define Sign_bit    ((uint32_t)0x80000000L)
144 #define Log2P       1
145 #define Tiny0       0
146 #define Tiny1       1
147 #define Quick_max   5
148 #define Int_max     6
149 #define Infinite(x) (word0(x) == ((uint32_t)0x7f800000L))
150 #undef word0
151 #undef word1
152
153 #define word0(x) (x.i[0])
154 #define word1(x) 0
155 #else
156
157 #define Exp_shift  20
158 #define Exp_shift1 20
159 #define Exp_msk1    ((uint32_t)0x100000L)
160 #define Exp_msk11   ((uint32_t)0x100000L)
161 #define Exp_mask  ((uint32_t)0x7ff00000L)
162 #define P 53
163 #define Bias 1023
164 #define IEEE_Arith
165 #define Emin (-1022)
166 #define Exp_1  ((uint32_t)0x3ff00000L)
167 #define Exp_11 ((uint32_t)0x3ff00000L)
168 #define Ebits 11
169 #define Frac_mask  ((uint32_t)0xfffffL)
170 #define Frac_mask1 ((uint32_t)0xfffffL)
171 #define Ten_pmax 22
172 #define Bletch 0x10
173 #define Bndry_mask  ((uint32_t)0xfffffL)
174 #define Bndry_mask1 ((uint32_t)0xfffffL)
175 #define LSB 1
176 #define Sign_bit ((uint32_t)0x80000000L)
177 #define Log2P 1
178 #define Tiny0 0
179 #define Tiny1 1
180 #define Quick_max 14
181 #define Int_max 14
182 #define Infinite(x) (word0(x) == ((uint32_t)0x7ff00000L)) /* sufficient test for here */
183 #endif
184
185 #else
186 #undef  Sudden_Underflow
187 #define Sudden_Underflow
188 #ifdef IBM
189 #define Exp_shift  24
190 #define Exp_shift1 24
191 #define Exp_msk1   ((uint32_t)0x1000000L)
192 #define Exp_msk11  ((uint32_t)0x1000000L)
193 #define Exp_mask  ((uint32_t)0x7f000000L)
194 #define P 14
195 #define Bias 65
196 #define Exp_1  ((uint32_t)0x41000000L)
197 #define Exp_11 ((uint32_t)0x41000000L)
198 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
199 #define Frac_mask  ((uint32_t)0xffffffL)
200 #define Frac_mask1 ((uint32_t)0xffffffL)
201 #define Bletch 4
202 #define Ten_pmax 22
203 #define Bndry_mask  ((uint32_t)0xefffffL)
204 #define Bndry_mask1 ((uint32_t)0xffffffL)
205 #define LSB 1
206 #define Sign_bit ((uint32_t)0x80000000L)
207 #define Log2P 4
208 #define Tiny0 ((uint32_t)0x100000L)
209 #define Tiny1 0
210 #define Quick_max 14
211 #define Int_max 15
212 #else /* VAX */
213 #define Exp_shift  23
214 #define Exp_shift1 7
215 #define Exp_msk1    0x80
216 #define Exp_msk11   ((uint32_t)0x800000L)
217 #define Exp_mask  ((uint32_t)0x7f80L)
218 #define P 56
219 #define Bias 129
220 #define Exp_1  ((uint32_t)0x40800000L)
221 #define Exp_11 ((uint32_t)0x4080L)
222 #define Ebits 8
223 #define Frac_mask  ((uint32_t)0x7fffffL)
224 #define Frac_mask1 ((uint32_t)0xffff007fL)
225 #define Ten_pmax 24
226 #define Bletch 2
227 #define Bndry_mask  ((uint32_t)0xffff007fL)
228 #define Bndry_mask1 ((uint32_t)0xffff007fL)
229 #define LSB ((uint32_t)0x10000L)
230 #define Sign_bit ((uint32_t)0x8000L)
231 #define Log2P 1
232 #define Tiny0 0x80
233 #define Tiny1 0
234 #define Quick_max 15
235 #define Int_max 15
236 #endif
237 #endif
238
239 #ifndef IEEE_Arith
240 #define ROUND_BIASED
241 #endif
242
243 #ifdef RND_PRODQUOT
244 #define rounded_product(a,b) a = rnd_prod(a, b)
245 #define rounded_quotient(a,b) a = rnd_quot(a, b)
246 #ifdef KR_headers
247 extern double rnd_prod(), rnd_quot();
248 #else
249 extern double rnd_prod(double, double), rnd_quot(double, double);
250 #endif
251 #else
252 #define rounded_product(a,b) a *= b
253 #define rounded_quotient(a,b) a /= b
254 #endif
255
256 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
257 #define Big1 ((uint32_t)0xffffffffL)
258
259 #ifndef Just_16
260 /* When Pack_32 is not defined, we store 16 bits per 32-bit long.
261  * This makes some inner loops simpler and sometimes saves work
262  * during multiplications, but it often seems to make things slightly
263  * slower.  Hence the default is now to store 32 bits per long.
264  */
265
266 #ifndef Pack_32
267 #if SIZEOF_VOID_P != 8
268 #define Pack_32
269 #endif
270 #endif
271 #endif
272
273
274 #define MAX_BIGNUMS 16
275 #ifdef Pack_32
276 #define MAX_BIGNUM_WDS 32
277 #else
278   /* Note that this is a workaround for */
279 #define MAX_BIGNUM_WDS 128
280 #endif
281
282 struct _Jv_Bigint
283 {
284   struct _Jv_Bigint *_next;
285   int _k, _maxwds, _sign, _wds;
286   unsigned long _x[1];
287 };
288
289
290 #define _PTR            void *
291 #define _AND            ,
292 #define _NOARGS         void
293 #define _CONST          const
294 #define _VOLATILE       volatile
295 #define _SIGNED         signed
296 #define _DOTS           , ...
297 #define _VOID void
298 #define _EXFUN(name, proto)             name proto
299 #define _DEFUN(name, arglist, args)     name(args)
300 #define _DEFUN_VOID(name)               name(_NOARGS)
301 #define _CAST_VOID (void)
302
303
304 struct _Jv_reent
305 {
306   /* local copy of errno */
307   int _errno;
308
309   /* used by mprec routines */
310   struct _Jv_Bigint *_result;
311   int _result_k;
312   struct _Jv_Bigint *_p5s;
313
314   struct _Jv_Bigint **_freelist;
315   int _max_k;
316 };
317
318
319 typedef struct _Jv_Bigint _Jv_Bigint;
320
321 #define Balloc  _Jv_Balloc
322 #define Bfree   _Jv_Bfree
323 #define multadd _Jv_multadd
324 #define s2b     _Jv_s2b
325 #define lo0bits _Jv_lo0bits
326 #define hi0bits _Jv_hi0bits
327 #define i2b     _Jv_i2b
328 #define mult    _Jv_mult
329 #define pow5mult        _Jv_pow5mult
330 #define lshift  _Jv_lshift
331 #define cmp     _Jv__mcmp
332 #define diff    _Jv__mdiff
333 #define ulp     _Jv_ulp
334 #define b2d     _Jv_b2d
335 #define d2b     _Jv_d2b
336 #define ratio   _Jv_ratio
337
338 #define tens _Jv__mprec_tens
339 #define bigtens _Jv__mprec_bigtens
340 #define tinytens _Jv__mprec_tinytens
341
342 #define _dtoa _Jv_dtoa
343 #define _dtoa_r _Jv_dtoa_r
344 #define _strtod_r _Jv_strtod_r
345
346 extern double _EXFUN(_strtod_r, (struct _Jv_reent *ptr, const char *s00, char **se));
347 extern char* _EXFUN(_dtoa_r, (struct _Jv_reent *ptr, double d,
348                               int mode, int ndigits, int *decpt, int *sign,
349                               char **rve, int float_type));
350 void _EXFUN(_dtoa, (double d, int mode, int ndigits, int *decpt, int *sign,
351                     char **rve, char *buf, int float_type));
352
353 double          _EXFUN(ulp,(double x));
354 double          _EXFUN(b2d,(_Jv_Bigint *a , int *e));
355 _Jv_Bigint *    _EXFUN(Balloc,(struct _Jv_reent *p, int k));
356 void            _EXFUN(Bfree,(struct _Jv_reent *p, _Jv_Bigint *v));
357 _Jv_Bigint *    _EXFUN(multadd,(struct _Jv_reent *p, _Jv_Bigint *, int, int));
358 _Jv_Bigint *    _EXFUN(s2b,(struct _Jv_reent *, const char*, int, int, unsigned long));
359 _Jv_Bigint *    _EXFUN(i2b,(struct _Jv_reent *,int));
360 _Jv_Bigint *    _EXFUN(mult, (struct _Jv_reent *, _Jv_Bigint *, _Jv_Bigint *));
361 _Jv_Bigint *    _EXFUN(pow5mult, (struct _Jv_reent *, _Jv_Bigint *, int k));
362 int             _EXFUN(hi0bits,(unsigned long));
363 int             _EXFUN(lo0bits,(unsigned long *));
364 _Jv_Bigint *    _EXFUN(d2b,(struct _Jv_reent *p, double d, int *e, int *bits));
365 _Jv_Bigint *    _EXFUN(lshift,(struct _Jv_reent *p, _Jv_Bigint *b, int k));
366 _Jv_Bigint *    _EXFUN(diff,(struct _Jv_reent *p, _Jv_Bigint *a, _Jv_Bigint *b));
367 int             _EXFUN(cmp,(_Jv_Bigint *a, _Jv_Bigint *b));
368
369 double          _EXFUN(ratio,(_Jv_Bigint *a, _Jv_Bigint *b));
370 #define Bcopy(x,y) memcpy((char *)&x->_sign, (char *)&y->_sign, y->_wds*sizeof(long) + 2*sizeof(int))
371
372 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
373 #define n_bigtens 2
374 #else
375 #define n_bigtens 5
376 #endif
377
378 extern _CONST double tinytens[];
379 extern _CONST double bigtens[];
380 extern _CONST double tens[];
381
382 #ifdef __cplusplus
383 }
384 #endif
385
386 #endif /* __CLASSPATH_MPREC_H__ */