fdlibm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* @(#)fdlibm.h 1.5 04/04/22 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. #ifndef __CLASSPATH_FDLIBM_H__
  12. #define __CLASSPATH_FDLIBM_H__
  13. /*
  14. * On AIX we need _ALL_SOURCE defined to compile/configure native-lib, but can't
  15. * have it defined to compile fdlibm. UGH.
  16. */
  17. #ifdef _AIX
  18. #undef _ALL_SOURCE
  19. #endif
  20. #include <config.h>
  21. #include <stdlib.h>
  22. /*
  23. * AIX includes a header that defines hz,
  24. * which conflicts with an fdlibm variable in some functions.
  25. */
  26. #ifdef _AIX
  27. #undef hz
  28. #endif
  29. /* GCJ LOCAL: Include files. */
  30. #include "ieeefp.h"
  31. /* CLASSPATH LOCAL: */
  32. #include "namespace.h"
  33. #include "mprec.h"
  34. /* CYGNUS LOCAL: Default to XOPEN_MODE. */
  35. #define _XOPEN_MODE
  36. #ifdef __P
  37. #undef __P
  38. #endif
  39. /* Sometimes it's necessary to define __LITTLE_ENDIAN explicitly
  40. but these catch some common cases. */
  41. #if 0
  42. #if defined(i386) || defined(i486) || \
  43. defined(intel) || defined(x86) || defined(i86pc) || \
  44. defined(__alpha) || defined(__osf__)
  45. #define __LITTLE_ENDIAN
  46. #endif
  47. #ifdef __LITTLE_ENDIAN
  48. #define __HI(x) *(1+(int*)&x)
  49. #define __LO(x) *(int*)&x
  50. #define __HIp(x) *(1+(int*)x)
  51. #define __LOp(x) *(int*)x
  52. #else
  53. #define __HI(x) *(int*)&x
  54. #define __LO(x) *(1+(int*)&x)
  55. #define __HIp(x) *(int*)x
  56. #define __LOp(x) *(1+(int*)x)
  57. #endif
  58. #endif
  59. #ifdef __STDC__
  60. #define __P(p) p
  61. #else
  62. #define __P(p) ()
  63. #endif
  64. /*
  65. * ANSI/POSIX
  66. */
  67. extern int signgam;
  68. #define MAXFLOAT ((float)3.40282346638528860e+38)
  69. enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
  70. #define _LIB_VERSION_TYPE enum fdversion
  71. #define _LIB_VERSION _fdlib_version
  72. /* if global variable _LIB_VERSION is not desirable, one may
  73. * change the following to be a constant by:
  74. * #define _LIB_VERSION_TYPE const enum version
  75. * In that case, after one initializes the value _LIB_VERSION (see
  76. * s_lib_version.c) during compile time, it cannot be modified
  77. * in the middle of a program
  78. */
  79. extern _LIB_VERSION_TYPE _LIB_VERSION;
  80. #define _IEEE_ fdlibm_ieee
  81. #define _SVID_ fdlibm_svid
  82. #define _XOPEN_ fdlibm_xopen
  83. #define _POSIX_ fdlibm_posix
  84. struct exception {
  85. int type;
  86. char *name;
  87. double arg1;
  88. double arg2;
  89. double retval;
  90. };
  91. #define HUGE MAXFLOAT
  92. /*
  93. * set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
  94. * (one may replace the following line by "#include <values.h>")
  95. */
  96. #define X_TLOSS 1.41484755040568800000e+16
  97. #define DOMAIN 1
  98. #define SING 2
  99. #define OVERFLOW 3
  100. #define UNDERFLOW 4
  101. #define TLOSS 5
  102. #define PLOSS 6
  103. /* These typedefs are true for the targets running Java. */
  104. #define _IEEE_LIBM
  105. #ifdef __cplusplus
  106. extern "C" {
  107. #endif
  108. /*
  109. * ANSI/POSIX
  110. */
  111. extern double acos __P((double));
  112. extern double asin __P((double));
  113. extern double atan __P((double));
  114. extern double atan2 __P((double, double));
  115. extern double cos __P((double));
  116. extern double sin __P((double));
  117. extern double tan __P((double));
  118. extern double cosh __P((double));
  119. extern double sinh __P((double));
  120. extern double tanh __P((double));
  121. extern double exp __P((double));
  122. extern double frexp __P((double, int *));
  123. extern double ldexp __P((double, int));
  124. extern double log __P((double));
  125. extern double log10 __P((double));
  126. extern double modf __P((double, double *));
  127. extern double pow __P((double, double));
  128. extern double sqrt __P((double));
  129. extern double ceil __P((double));
  130. extern double fabs __P((double));
  131. extern double floor __P((double));
  132. extern double fmod __P((double, double));
  133. extern double erf __P((double));
  134. extern double erfc __P((double));
  135. extern double gamma __P((double));
  136. extern double hypot __P((double, double));
  137. #if !defined(isnan)
  138. #define isnan(x) ((x) != (x))
  139. #endif
  140. extern int finite __P((double));
  141. extern double j0 __P((double));
  142. extern double j1 __P((double));
  143. extern double jn __P((int, double));
  144. extern double lgamma __P((double));
  145. extern double y0 __P((double));
  146. extern double y1 __P((double));
  147. extern double yn __P((int, double));
  148. extern double acosh __P((double));
  149. extern double asinh __P((double));
  150. extern double atanh __P((double));
  151. extern double cbrt __P((double));
  152. extern double logb __P((double));
  153. extern double nextafter __P((double, double));
  154. extern double remainder __P((double, double));
  155. #ifdef _SCALB_INT
  156. extern double scalb __P((double, int));
  157. #else
  158. extern double scalb __P((double, double));
  159. #endif
  160. extern int matherr __P((struct exception *));
  161. /*
  162. * IEEE Test Vector
  163. */
  164. extern double significand __P((double));
  165. /*
  166. * Functions callable from C, intended to support IEEE arithmetic.
  167. */
  168. extern double copysign __P((double, double));
  169. extern int ilogb __P((double));
  170. extern double rint __P((double));
  171. extern double scalbn __P((double, int));
  172. /*
  173. * BSD math library entry points
  174. */
  175. extern double expm1 __P((double));
  176. extern double log1p __P((double));
  177. /*
  178. * Reentrant version of gamma & lgamma; passes signgam back by reference
  179. * as the second argument; user must allocate space for signgam.
  180. */
  181. #ifdef _REENTRANT
  182. extern double gamma_r __P((double, int *));
  183. extern double lgamma_r __P((double, int *));
  184. #endif /* _REENTRANT */
  185. /* ieee style elementary functions */
  186. extern double __ieee754_sqrt __P((double));
  187. extern double __ieee754_acos __P((double));
  188. extern double __ieee754_acosh __P((double));
  189. extern double __ieee754_log __P((double));
  190. extern double __ieee754_atanh __P((double));
  191. extern double __ieee754_asin __P((double));
  192. extern double __ieee754_atan2 __P((double,double));
  193. extern double __ieee754_exp __P((double));
  194. extern double __ieee754_cosh __P((double));
  195. extern double __ieee754_fmod __P((double,double));
  196. extern double __ieee754_pow __P((double,double));
  197. extern double __ieee754_lgamma_r __P((double,int *));
  198. extern double __ieee754_gamma_r __P((double,int *));
  199. extern double __ieee754_lgamma __P((double));
  200. extern double __ieee754_gamma __P((double));
  201. extern double __ieee754_log10 __P((double));
  202. extern double __ieee754_sinh __P((double));
  203. extern double __ieee754_hypot __P((double,double));
  204. extern double __ieee754_j0 __P((double));
  205. extern double __ieee754_j1 __P((double));
  206. extern double __ieee754_y0 __P((double));
  207. extern double __ieee754_y1 __P((double));
  208. extern double __ieee754_jn __P((int,double));
  209. extern double __ieee754_yn __P((int,double));
  210. extern double __ieee754_remainder __P((double,double));
  211. extern int32_t __ieee754_rem_pio2 __P((double,double*));
  212. #ifdef _SCALB_INT
  213. extern double __ieee754_scalb __P((double,int));
  214. #else
  215. extern double __ieee754_scalb __P((double,double));
  216. #endif
  217. /* fdlibm kernel function */
  218. extern double __kernel_standard __P((double,double,int));
  219. extern double __kernel_sin __P((double,double,int));
  220. extern double __kernel_cos __P((double,double));
  221. extern double __kernel_tan __P((double,double,int));
  222. extern int __kernel_rem_pio2 __P((double*,double*,int,int,int,const int*));
  223. /* Classpath extensions */
  224. /* The original code used statements like
  225. n0 = ((*(int*)&one)>>29)^1; * index of high word *
  226. ix0 = *(n0+(int*)&x); * high word of x *
  227. ix1 = *((1-n0)+(int*)&x); * low word of x *
  228. to dig two 32 bit words out of the 64 bit IEEE floating point
  229. value. That is non-ANSI, and, moreover, the gcc instruction
  230. scheduler gets it wrong. We instead use the following macros.
  231. Unlike the original code, we determine the endianness at compile
  232. time, not at run time; I don't see much benefit to selecting
  233. endianness at run time. */
  234. #ifndef __IEEE_BIG_ENDIAN
  235. #ifndef __IEEE_LITTLE_ENDIAN
  236. #error Must define endianness
  237. #endif
  238. #endif
  239. /* A union which permits us to convert between a double and two 32 bit
  240. ints. */
  241. #ifdef __IEEE_BIG_ENDIAN
  242. typedef union
  243. {
  244. double value;
  245. struct
  246. {
  247. uint32_t msw;
  248. uint32_t lsw;
  249. } parts;
  250. } ieee_double_shape_type;
  251. #endif
  252. #ifdef __IEEE_LITTLE_ENDIAN
  253. typedef union
  254. {
  255. double value;
  256. struct
  257. {
  258. uint32_t lsw;
  259. uint32_t msw;
  260. } parts;
  261. } ieee_double_shape_type;
  262. #endif
  263. /* Get two 32 bit ints from a double. */
  264. #define EXTRACT_WORDS(ix0,ix1,d) \
  265. do { \
  266. ieee_double_shape_type ew_u; \
  267. ew_u.value = (d); \
  268. (ix0) = ew_u.parts.msw; \
  269. (ix1) = ew_u.parts.lsw; \
  270. } while (0)
  271. /* Get the more significant 32 bit int from a double. */
  272. #define GET_HIGH_WORD(i,d) \
  273. do { \
  274. ieee_double_shape_type gh_u; \
  275. gh_u.value = (d); \
  276. (i) = gh_u.parts.msw; \
  277. } while (0)
  278. /* Get the less significant 32 bit int from a double. */
  279. #define GET_LOW_WORD(i,d) \
  280. do { \
  281. ieee_double_shape_type gl_u; \
  282. gl_u.value = (d); \
  283. (i) = gl_u.parts.lsw; \
  284. } while (0)
  285. /* Set a double from two 32 bit ints. */
  286. #define INSERT_WORDS(d,ix0,ix1) \
  287. do { \
  288. ieee_double_shape_type iw_u; \
  289. iw_u.parts.msw = (ix0); \
  290. iw_u.parts.lsw = (ix1); \
  291. (d) = iw_u.value; \
  292. } while (0)
  293. /* Set the more significant 32 bits of a double from an int. */
  294. #define SET_HIGH_WORD(d,v) \
  295. do { \
  296. ieee_double_shape_type sh_u; \
  297. sh_u.value = (d); \
  298. sh_u.parts.msw = (v); \
  299. (d) = sh_u.value; \
  300. } while (0)
  301. /* Set the less significant 32 bits of a double from an int. */
  302. #define SET_LOW_WORD(d,v) \
  303. do { \
  304. ieee_double_shape_type sl_u; \
  305. sl_u.value = (d); \
  306. sl_u.parts.lsw = (v); \
  307. (d) = sl_u.value; \
  308. } while (0)
  309. /* A union which permits us to convert between a float and a 32 bit
  310. int. */
  311. typedef union
  312. {
  313. float value;
  314. uint32_t word;
  315. } ieee_float_shape_type;
  316. /* Get a 32 bit int from a float. */
  317. #define GET_FLOAT_WORD(i,d) \
  318. do { \
  319. ieee_float_shape_type gf_u; \
  320. gf_u.value = (d); \
  321. (i) = gf_u.word; \
  322. } while (0)
  323. /* Set a float from a 32 bit int. */
  324. #define SET_FLOAT_WORD(d,i) \
  325. do { \
  326. ieee_float_shape_type sf_u; \
  327. sf_u.word = (i); \
  328. (d) = sf_u.value; \
  329. } while (0)
  330. #ifdef __cplusplus
  331. }
  332. #endif
  333. #endif /* __CLASSPATH_FDLIBM_H__ */