* Slim fdlibm version from GNU classpath, we need these functions (finite,
[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 "ieeefp.h"
34
35 #if defined HAVE_STDINT_H
36 #include <stdint.h>
37 #elif defined HAVE_INTTYPES_H
38 #include <inttypes.h>
39 #endif
40
41 #if defined HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44
45 #if defined HAVE_SYS_CONFIG_H
46 #include <sys/config.h>
47 #endif
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /* ISO C99 int type declarations */
54
55 #if !defined HAVE_INT32_DEFINED && defined HAVE_BSD_INT32_DEFINED
56 typedef u_int32_t uint32_t;
57 #endif
58
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)));
62
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.  */
66 #  ifndef _UINT32_T
67 typedef unsigned int uint32_t __attribute__((mode(SI)));
68 #  endif
69 #endif
70
71   /* These typedefs are true for the targets running Java. */
72
73 #ifdef __IEEE_LITTLE_ENDIAN
74 #define IEEE_8087
75 #endif
76
77 #ifdef __IEEE_BIG_ENDIAN
78 #define IEEE_MC68k
79 #endif
80
81 #ifdef __Z8000__
82 #define Just_16
83 #endif
84
85 #ifdef DEBUG
86 #include "stdio.h"
87 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
88 #endif
89
90
91 #ifdef Unsigned_Shifts
92 #define Sign_Extend(a,b) if (b < 0) a |= (uint32_t)0xffff0000;
93 #else
94 #define Sign_Extend(a,b) /*no-op*/
95 #endif
96
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.
99 #endif
100
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
104    the ANSI C spec.  */
105 union double_union
106 {
107   double d;
108   uint32_t i[2];
109 };
110
111 #ifdef IEEE_8087
112 #define word0(x) (x.i[1])
113 #define word1(x) (x.i[0])
114 #else
115 #define word0(x) (x.i[0])
116 #define word1(x) (x.i[1])
117 #endif
118
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)
122  */
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++)
126 #else
127 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
128 ((unsigned short *)a)[1] = (unsigned short)c, a++)
129 #endif
130
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) */
136
137 #if defined(IEEE_8087) + defined(IEEE_MC68k)
138 #if defined (_DOUBLE_IS_32BITS)
139 #define Exp_shift   23
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)
144 #define P           24
145 #define Bias        127
146 #if 0
147 #define IEEE_Arith  /* it is, but the code doesn't handle IEEE singles yet */
148 #endif
149 #define Emin        (-126)
150 #define Exp_1       ((uint32_t)0x3f800000L)
151 #define Exp_11      ((uint32_t)0x3f800000L)
152 #define Ebits       8
153 #define Frac_mask   ((uint32_t)0x007fffffL)
154 #define Frac_mask1  ((uint32_t)0x007fffffL)
155 #define Ten_pmax    10
156 #define Sign_bit    ((uint32_t)0x80000000L)
157 #define Ten_pmax    10
158 #define Bletch      2
159 #define Bndry_mask  ((uint32_t)0x007fffffL)
160 #define Bndry_mask1 ((uint32_t)0x007fffffL)
161 #define LSB 1
162 #define Sign_bit    ((uint32_t)0x80000000L)
163 #define Log2P       1
164 #define Tiny0       0
165 #define Tiny1       1
166 #define Quick_max   5
167 #define Int_max     6
168 #define Infinite(x) (word0(x) == ((uint32_t)0x7f800000L))
169 #undef word0
170 #undef word1
171
172 #define word0(x) (x.i[0])
173 #define word1(x) 0
174 #else
175
176 #define Exp_shift  20
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)
181 #define P 53
182 #define Bias 1023
183 #define IEEE_Arith
184 #define Emin (-1022)
185 #define Exp_1  ((uint32_t)0x3ff00000L)
186 #define Exp_11 ((uint32_t)0x3ff00000L)
187 #define Ebits 11
188 #define Frac_mask  ((uint32_t)0xfffffL)
189 #define Frac_mask1 ((uint32_t)0xfffffL)
190 #define Ten_pmax 22
191 #define Bletch 0x10
192 #define Bndry_mask  ((uint32_t)0xfffffL)
193 #define Bndry_mask1 ((uint32_t)0xfffffL)
194 #define LSB 1
195 #define Sign_bit ((uint32_t)0x80000000L)
196 #define Log2P 1
197 #define Tiny0 0
198 #define Tiny1 1
199 #define Quick_max 14
200 #define Int_max 14
201 #define Infinite(x) (word0(x) == ((uint32_t)0x7ff00000L)) /* sufficient test for here */
202 #endif
203
204 #else
205 #undef  Sudden_Underflow
206 #define Sudden_Underflow
207 #ifdef IBM
208 #define Exp_shift  24
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)
213 #define P 14
214 #define Bias 65
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)
220 #define Bletch 4
221 #define Ten_pmax 22
222 #define Bndry_mask  ((uint32_t)0xefffffL)
223 #define Bndry_mask1 ((uint32_t)0xffffffL)
224 #define LSB 1
225 #define Sign_bit ((uint32_t)0x80000000L)
226 #define Log2P 4
227 #define Tiny0 ((uint32_t)0x100000L)
228 #define Tiny1 0
229 #define Quick_max 14
230 #define Int_max 15
231 #else /* VAX */
232 #define Exp_shift  23
233 #define Exp_shift1 7
234 #define Exp_msk1    0x80
235 #define Exp_msk11   ((uint32_t)0x800000L)
236 #define Exp_mask  ((uint32_t)0x7f80L)
237 #define P 56
238 #define Bias 129
239 #define Exp_1  ((uint32_t)0x40800000L)
240 #define Exp_11 ((uint32_t)0x4080L)
241 #define Ebits 8
242 #define Frac_mask  ((uint32_t)0x7fffffL)
243 #define Frac_mask1 ((uint32_t)0xffff007fL)
244 #define Ten_pmax 24
245 #define Bletch 2
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)
250 #define Log2P 1
251 #define Tiny0 0x80
252 #define Tiny1 0
253 #define Quick_max 15
254 #define Int_max 15
255 #endif
256 #endif
257
258 #ifndef IEEE_Arith
259 #define ROUND_BIASED
260 #endif
261
262 #ifdef RND_PRODQUOT
263 #define rounded_product(a,b) a = rnd_prod(a, b)
264 #define rounded_quotient(a,b) a = rnd_quot(a, b)
265 #ifdef KR_headers
266 extern double rnd_prod(), rnd_quot();
267 #else
268 extern double rnd_prod(double, double), rnd_quot(double, double);
269 #endif
270 #else
271 #define rounded_product(a,b) a *= b
272 #define rounded_quotient(a,b) a /= b
273 #endif
274
275 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
276 #define Big1 ((uint32_t)0xffffffffL)
277
278 /* TWISTI to avoid bugs, we should use Just_16 on every platform */
279 #define Just_16
280
281 #ifndef Just_16
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.
286  */
287
288 #ifndef Pack_32
289 #if SIZEOF_VOID_P != 8
290 #define Pack_32
291 #endif
292 #endif
293 #endif
294
295
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
300
301 struct _Jv_Bigint
302 {
303   struct _Jv_Bigint *_next;
304   int _k, _maxwds, _sign, _wds;
305   unsigned long _x[MAX_BIGNUM_WDS];
306 };
307
308
309 #define _PTR            void *
310 #define _AND            ,
311 #define _NOARGS         void
312 #define _CONST          const
313 #define _VOLATILE       volatile
314 #define _SIGNED         signed
315 #define _DOTS           , ...
316 #define _VOID void
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)
321
322
323 struct _Jv_reent
324 {
325   /* local copy of errno */
326   int _errno;
327
328   /* used by mprec routines */
329   struct _Jv_Bigint *_result;
330   int _result_k;
331   struct _Jv_Bigint *_p5s;
332
333   struct _Jv_Bigint _freelist[MAX_BIGNUMS];
334   int _allocation_map;
335
336   int num;
337 };
338
339
340 typedef struct _Jv_Bigint _Jv_Bigint;
341
342 #define Balloc  _Jv_Balloc
343 #define Bfree   _Jv_Bfree
344 #define multadd _Jv_multadd
345 #define s2b     _Jv_s2b
346 #define lo0bits _Jv_lo0bits
347 #define hi0bits _Jv_hi0bits
348 #define i2b     _Jv_i2b
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
354 #define ulp     _Jv_ulp
355 #define b2d     _Jv_b2d
356 #define d2b     _Jv_d2b
357 #define ratio   _Jv_ratio
358
359 #define tens _Jv__mprec_tens
360 #define bigtens _Jv__mprec_bigtens
361 #define tinytens _Jv__mprec_tinytens
362
363 #define _dtoa _Jv_dtoa
364 #define _dtoa_r _Jv_dtoa_r
365 #define _strtod_r _Jv_strtod_r
366
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));
373
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));
389
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))
392
393 #if defined(_DOUBLE_IS_32BITS) && defined(__v800)
394 #define n_bigtens 2
395 #else
396 #define n_bigtens 5
397 #endif
398
399 extern _CONST double tinytens[];
400 extern _CONST double bigtens[];
401 extern _CONST double tens[];
402
403 #ifdef __cplusplus
404 }
405 #endif
406
407 #endif /* __CLASSPATH_MPREC_H__ */