mprec.h 10 KB

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