openlibm_math.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * from: @(#)fdlibm.h 5.1 93/09/24
  13. * $FreeBSD$
  14. */
  15. #ifndef _OPENLIBM_MATH_H_
  16. #define _OPENLIBM_MATH_H_
  17. #include <sys/cdefs.h>
  18. //#include <sys/_types.h>
  19. //#include <machine/_limits.h>
  20. #include <limits.h>
  21. #include <openlibm_compat.h>
  22. /*
  23. * ANSI/POSIX
  24. */
  25. extern const union __infinity_un {
  26. unsigned char __uc[8];
  27. double __ud;
  28. } __infinity;
  29. extern const union __nan_un {
  30. unsigned char __uc[sizeof(float)];
  31. float __uf;
  32. } __nan;
  33. /* VBS
  34. #if __GNUC_PREREQ__(3, 3) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
  35. #define __MATH_BUILTIN_CONSTANTS
  36. #endif
  37. #if __GNUC_PREREQ__(3, 0) && !defined(__INTEL_COMPILER)
  38. #define __MATH_BUILTIN_RELOPS
  39. #endif
  40. */
  41. //VBS begin
  42. #define __MATH_BUILTIN_CONSTANTS
  43. #define __MATH_BUILTIN_RELOPS
  44. #define __ISO_C_VISIBLE 1999
  45. //VBS end
  46. #ifdef __MATH_BUILTIN_CONSTANTS
  47. #define HUGE_VAL __builtin_huge_val()
  48. #else
  49. #define HUGE_VAL (__infinity.__ud)
  50. #endif
  51. #if __ISO_C_VISIBLE >= 1999
  52. //#define FP_ILOGB0 (-__INT_MAX)
  53. //#define FP_ILOGBNAN __INT_MAX
  54. #define FP_ILOGB0 (-INT_MAX)
  55. #define FP_ILOGBNAN INT_MAX
  56. #ifdef __MATH_BUILTIN_CONSTANTS
  57. #define HUGE_VALF __builtin_huge_valf()
  58. #define HUGE_VALL __builtin_huge_vall()
  59. #define INFINITY __builtin_inff()
  60. #define NAN __builtin_nanf("")
  61. #else
  62. #define HUGE_VALF (float)HUGE_VAL
  63. #define HUGE_VALL (long double)HUGE_VAL
  64. #define INFINITY HUGE_VALF
  65. #define NAN (__nan.__uf)
  66. #endif /* __MATH_BUILTIN_CONSTANTS */
  67. #define MATH_ERRNO 1
  68. #define MATH_ERREXCEPT 2
  69. #define math_errhandling MATH_ERREXCEPT
  70. #define FP_FAST_FMAF 1
  71. /* Symbolic constants to classify floating point numbers. */
  72. #define FP_INFINITE 0x01
  73. #define FP_NAN 0x02
  74. #define FP_NORMAL 0x04
  75. #define FP_SUBNORMAL 0x08
  76. #define FP_ZERO 0x10
  77. #if (__STDC_VERSION__ >= 201112L && defined(__clang__))
  78. #define __fp_type_select(x, f, d, ld) _Generic((x), \
  79. float: f(x), \
  80. double: d(x), \
  81. long double: ld(x), \
  82. volatile float: f(x), \
  83. volatile double: d(x), \
  84. volatile long double: ld(x), \
  85. volatile const float: f(x), \
  86. volatile const double: d(x), \
  87. volatile const long double: ld(x), \
  88. const float: f(x), \
  89. const double: d(x), \
  90. const long double: ld(x))
  91. /*
  92. #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
  93. #define __fp_type_select(x, f, d, ld) __builtin_choose_expr( \
  94. __builtin_types_compatible_p(__typeof(x), long double), ld(x), \
  95. __builtin_choose_expr( \
  96. __builtin_types_compatible_p(__typeof(x), double), d(x), \
  97. __builtin_choose_expr( \
  98. __builtin_types_compatible_p(__typeof(x), float), f(x), (void)0)))
  99. #else
  100. */
  101. #else
  102. #define __fp_type_select(x, f, d, ld) \
  103. ((sizeof(x) == sizeof(float)) ? f(x) \
  104. : (sizeof(x) == sizeof(double)) ? d(x) \
  105. : ld(x))
  106. #endif
  107. #define fpclassify(x) \
  108. __fp_type_select(x, __fpclassifyf, __fpclassifyd, __fpclassifyl)
  109. #define isfinite(x) __fp_type_select(x, __isfinitef, __isfinite, __isfinitel)
  110. #define isinf(x) __fp_type_select(x, __isinff, __isinf, __isinfl)
  111. #define isnan(x) \
  112. __fp_type_select(x, __inline_isnanf, __inline_isnan, __inline_isnanl)
  113. #define isnormal(x) __fp_type_select(x, __isnormalf, __isnormal, __isnormall)
  114. #ifdef __MATH_BUILTIN_RELOPS
  115. #define isgreater(x, y) __builtin_isgreater((x), (y))
  116. #define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y))
  117. #define isless(x, y) __builtin_isless((x), (y))
  118. #define islessequal(x, y) __builtin_islessequal((x), (y))
  119. #define islessgreater(x, y) __builtin_islessgreater((x), (y))
  120. #define isunordered(x, y) __builtin_isunordered((x), (y))
  121. #else
  122. #define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
  123. #define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
  124. #define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
  125. #define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
  126. #define islessgreater(x, y) (!isunordered((x), (y)) && \
  127. ((x) > (y) || (y) > (x)))
  128. #define isunordered(x, y) (isnan(x) || isnan(y))
  129. #endif /* __MATH_BUILTIN_RELOPS */
  130. #define signbit(x) __fp_type_select(x, __signbitf, __signbit, __signbitl)
  131. //VBS
  132. //typedef __double_t double_t;
  133. //typedef __float_t float_t;
  134. #endif /* __ISO_C_VISIBLE >= 1999 */
  135. /*
  136. * XOPEN/SVID
  137. */
  138. #if __BSD_VISIBLE || __XSI_VISIBLE
  139. #define M_E 2.7182818284590452354 /* e */
  140. #define M_LOG2E 1.4426950408889634074 /* log 2e */
  141. #define M_LOG10E 0.43429448190325182765 /* log 10e */
  142. #define M_LN2 0.69314718055994530942 /* log e2 */
  143. #define M_LN10 2.30258509299404568402 /* log e10 */
  144. #define M_PI 3.14159265358979323846 /* pi */
  145. #define M_PI_2 1.57079632679489661923 /* pi/2 */
  146. #define M_PI_4 0.78539816339744830962 /* pi/4 */
  147. #define M_1_PI 0.31830988618379067154 /* 1/pi */
  148. #define M_2_PI 0.63661977236758134308 /* 2/pi */
  149. #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  150. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  151. #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
  152. #define MAXFLOAT ((float)3.40282346638528860e+38)
  153. extern int signgam;
  154. #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
  155. #if __BSD_VISIBLE
  156. #if 0
  157. /* Old value from 4.4BSD-Lite math.h; this is probably better. */
  158. #define HUGE HUGE_VAL
  159. #else
  160. #define HUGE MAXFLOAT
  161. #endif
  162. #endif /* __BSD_VISIBLE */
  163. /*
  164. * Most of these functions depend on the rounding mode and have the side
  165. * effect of raising floating-point exceptions, so they are not declared
  166. * as __pure2. In C99, FENV_ACCESS affects the purity of these functions.
  167. */
  168. __BEGIN_DECLS
  169. /*
  170. * ANSI/POSIX
  171. */
  172. int __fpclassifyd(double) __pure2;
  173. int __fpclassifyf(float) __pure2;
  174. int __fpclassifyl(long double) __pure2;
  175. int __isfinitef(float) __pure2;
  176. int __isfinite(double) __pure2;
  177. int __isfinitel(long double) __pure2;
  178. int __isinff(float) __pure2;
  179. int __isinf(double) __pure2;
  180. int __isinfl(long double) __pure2;
  181. int __isnormalf(float) __pure2;
  182. int __isnormal(double) __pure2;
  183. int __isnormall(long double) __pure2;
  184. int __signbit(double) __pure2;
  185. int __signbitf(float) __pure2;
  186. int __signbitl(long double) __pure2;
  187. static __inline int
  188. __inline_isnan(__const double __x)
  189. {
  190. return (__x != __x);
  191. }
  192. static __inline int
  193. __inline_isnanf(__const float __x)
  194. {
  195. return (__x != __x);
  196. }
  197. static __inline int
  198. __inline_isnanl(__const long double __x)
  199. {
  200. return (__x != __x);
  201. }
  202. /*
  203. * Version 2 of the Single UNIX Specification (UNIX98) defined isnan() and
  204. * isinf() as functions taking double. C99, and the subsequent POSIX revisions
  205. * (SUSv3, POSIX.1-2001, define it as a macro that accepts any real floating
  206. * point type. If we are targeting SUSv2 and C99 or C11 (or C++11) then we
  207. * expose the newer definition, assuming that the language spec takes
  208. * precedence over the operating system interface spec.
  209. */
  210. #if __XSI_VISIBLE > 0 && __XSI_VISIBLE < 600 && __ISO_C_VISIBLE < 1999
  211. #undef isinf
  212. #undef isnan
  213. int isinf(double);
  214. int isnan(double);
  215. #endif
  216. double acos(double);
  217. double asin(double);
  218. double atan(double);
  219. double atan2(double, double);
  220. double cos(double);
  221. double sin(double);
  222. double tan(double);
  223. double cosh(double);
  224. double sinh(double);
  225. double tanh(double);
  226. double exp(double);
  227. double frexp(double, int *); /* fundamentally !__pure2 */
  228. double ldexp(double, int);
  229. double log(double);
  230. double log10(double);
  231. double modf(double, double *); /* fundamentally !__pure2 */
  232. double pow(double, double);
  233. double sqrt(double);
  234. double ceil(double);
  235. double fabs(double) __pure2;
  236. double floor(double);
  237. double fmod(double, double);
  238. /*
  239. * These functions are not in C90.
  240. */
  241. #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE
  242. double acosh(double);
  243. double asinh(double);
  244. double atanh(double);
  245. double cbrt(double);
  246. double erf(double);
  247. double erfc(double);
  248. double exp2(double);
  249. double expm1(double);
  250. double fma(double, double, double);
  251. double hypot(double, double);
  252. int ilogb(double) __pure2;
  253. double lgamma(double);
  254. long long llrint(double);
  255. long long llround(double);
  256. double log1p(double);
  257. double log2(double);
  258. double logb(double);
  259. long lrint(double);
  260. long lround(double);
  261. double nan(const char *) __pure2;
  262. double nextafter(double, double);
  263. double remainder(double, double);
  264. double remquo(double, double, int *);
  265. double rint(double);
  266. #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */
  267. #if __BSD_VISIBLE || __XSI_VISIBLE
  268. double j0(double);
  269. double j1(double);
  270. double jn(int, double);
  271. double y0(double);
  272. double y1(double);
  273. double yn(int, double);
  274. #if __XSI_VISIBLE <= 500 || __BSD_VISIBLE
  275. double gamma(double);
  276. #endif
  277. #if __XSI_VISIBLE <= 600 || __BSD_VISIBLE
  278. double scalb(double, double);
  279. #endif
  280. #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
  281. #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999
  282. double copysign(double, double) __pure2;
  283. double fdim(double, double);
  284. double fmax(double, double) __pure2;
  285. double fmin(double, double) __pure2;
  286. double nearbyint(double);
  287. double round(double);
  288. double scalbln(double, long);
  289. double scalbn(double, int);
  290. double tgamma(double);
  291. double trunc(double);
  292. #endif
  293. /*
  294. * BSD math library entry points
  295. */
  296. #if __BSD_VISIBLE
  297. double drem(double, double);
  298. int finite(double) __pure2;
  299. int isnanf(float) __pure2;
  300. /*
  301. * Reentrant version of gamma & lgamma; passes signgam back by reference
  302. * as the second argument; user must allocate space for signgam.
  303. */
  304. double gamma_r(double, int *);
  305. double lgamma_r(double, int *);
  306. /*
  307. * IEEE Test Vector
  308. */
  309. double significand(double);
  310. #endif /* __BSD_VISIBLE */
  311. /* float versions of ANSI/POSIX functions */
  312. #if __ISO_C_VISIBLE >= 1999
  313. float acosf(float);
  314. float asinf(float);
  315. float atanf(float);
  316. float atan2f(float, float);
  317. float cosf(float);
  318. float sinf(float);
  319. float tanf(float);
  320. float coshf(float);
  321. float sinhf(float);
  322. float tanhf(float);
  323. float exp2f(float);
  324. float expf(float);
  325. float expm1f(float);
  326. float frexpf(float, int *); /* fundamentally !__pure2 */
  327. int ilogbf(float) __pure2;
  328. float ldexpf(float, int);
  329. float log10f(float);
  330. float log1pf(float);
  331. float log2f(float);
  332. float logf(float);
  333. float modff(float, float *); /* fundamentally !__pure2 */
  334. float powf(float, float);
  335. float sqrtf(float);
  336. float ceilf(float);
  337. float fabsf(float) __pure2;
  338. float floorf(float);
  339. float fmodf(float, float);
  340. float roundf(float);
  341. float erff(float);
  342. float erfcf(float);
  343. float hypotf(float, float);
  344. float lgammaf(float);
  345. float tgammaf(float);
  346. float acoshf(float);
  347. float asinhf(float);
  348. float atanhf(float);
  349. float cbrtf(float);
  350. float logbf(float);
  351. float copysignf(float, float) __pure2;
  352. long long llrintf(float);
  353. long long llroundf(float);
  354. long lrintf(float);
  355. long lroundf(float);
  356. float nanf(const char *) __pure2;
  357. float nearbyintf(float);
  358. float nextafterf(float, float);
  359. float remainderf(float, float);
  360. float remquof(float, float, int *);
  361. float rintf(float);
  362. float scalblnf(float, long);
  363. float scalbnf(float, int);
  364. float truncf(float);
  365. float fdimf(float, float);
  366. float fmaf(float, float, float);
  367. float fmaxf(float, float) __pure2;
  368. float fminf(float, float) __pure2;
  369. #endif
  370. /*
  371. * float versions of BSD math library entry points
  372. */
  373. #if __BSD_VISIBLE
  374. float dremf(float, float);
  375. int finitef(float) __pure2;
  376. float gammaf(float);
  377. float j0f(float);
  378. float j1f(float);
  379. float jnf(int, float);
  380. float scalbf(float, float);
  381. float y0f(float);
  382. float y1f(float);
  383. float ynf(int, float);
  384. /*
  385. * Float versions of reentrant version of gamma & lgamma; passes
  386. * signgam back by reference as the second argument; user must
  387. * allocate space for signgam.
  388. */
  389. float gammaf_r(float, int *);
  390. float lgammaf_r(float, int *);
  391. /*
  392. * float version of IEEE Test Vector
  393. */
  394. float significandf(float);
  395. #endif /* __BSD_VISIBLE */
  396. /*
  397. * long double versions of ISO/POSIX math functions
  398. */
  399. #if __ISO_C_VISIBLE >= 1999
  400. long double acoshl(long double);
  401. long double acosl(long double);
  402. long double asinhl(long double);
  403. long double asinl(long double);
  404. long double atan2l(long double, long double);
  405. long double atanhl(long double);
  406. long double atanl(long double);
  407. long double cbrtl(long double);
  408. long double ceill(long double);
  409. long double copysignl(long double, long double) __pure2;
  410. long double coshl(long double);
  411. long double cosl(long double);
  412. long double erfcl(long double);
  413. long double erfl(long double);
  414. long double exp2l(long double);
  415. long double expl(long double);
  416. long double expm1l(long double);
  417. long double fabsl(long double) __pure2;
  418. long double fdiml(long double, long double);
  419. long double floorl(long double);
  420. long double fmal(long double, long double, long double);
  421. long double fmaxl(long double, long double) __pure2;
  422. long double fminl(long double, long double) __pure2;
  423. long double fmodl(long double, long double);
  424. long double frexpl(long double value, int *); /* fundamentally !__pure2 */
  425. long double hypotl(long double, long double);
  426. int ilogbl(long double) __pure2;
  427. long double ldexpl(long double, int);
  428. long double lgammal(long double);
  429. long long llrintl(long double);
  430. long long llroundl(long double);
  431. long double log10l(long double);
  432. long double log1pl(long double);
  433. long double log2l(long double);
  434. long double logbl(long double);
  435. long double logl(long double);
  436. long lrintl(long double);
  437. long lroundl(long double);
  438. long double modfl(long double, long double *); /* fundamentally !__pure2 */
  439. long double nanl(const char *) __pure2;
  440. long double nearbyintl(long double);
  441. long double nextafterl(long double, long double);
  442. double nexttoward(double, long double);
  443. float nexttowardf(float, long double);
  444. long double nexttowardl(long double, long double);
  445. long double powl(long double, long double);
  446. long double remainderl(long double, long double);
  447. long double remquol(long double, long double, int *);
  448. long double rintl(long double);
  449. long double roundl(long double);
  450. long double scalblnl(long double, long);
  451. long double scalbnl(long double, int);
  452. long double sinhl(long double);
  453. long double sinl(long double);
  454. long double sqrtl(long double);
  455. long double tanhl(long double);
  456. long double tanl(long double);
  457. long double tgammal(long double);
  458. long double truncl(long double);
  459. #endif /* __ISO_C_VISIBLE >= 1999 */
  460. #if __BSD_VISIBLE
  461. long double lgammal_r(long double, int *);
  462. #endif
  463. __END_DECLS
  464. #endif /* !_OPENLIBM_MATH_H_ */