openlibm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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: src/lib/msun/src/openlibm.h,v 1.82 2011/11/12 19:55:48 theraven Exp $
  14. */
  15. #ifndef _MATH_H_
  16. #define _MATH_H_
  17. #include <complex.h>
  18. #include "cdefs-compat.h"
  19. #include "types-compat.h"
  20. /*
  21. * ANSI/POSIX
  22. */
  23. extern const union __infinity_un {
  24. unsigned char __uc[8];
  25. double __ud;
  26. } __infinity;
  27. extern const union __nan_un {
  28. unsigned char __uc[sizeof(float)];
  29. float __uf;
  30. } __nan;
  31. /* VBS
  32. #if __GNUC_PREREQ__(3, 3) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
  33. #define __MATH_BUILTIN_CONSTANTS
  34. #endif
  35. #if __GNUC_PREREQ__(3, 0) && !defined(__INTEL_COMPILER)
  36. #define __MATH_BUILTIN_RELOPS
  37. #endif
  38. */
  39. //VBS begin
  40. #define __MATH_BUILTIN_CONSTANTS
  41. #define __MATH_BUILTIN_RELOPS
  42. #define __ISO_C_VISIBLE 1999
  43. //VBS end
  44. #ifdef __MATH_BUILTIN_CONSTANTS
  45. #define HUGE_VAL __builtin_huge_val()
  46. #else
  47. #define HUGE_VAL (__infinity.__ud)
  48. #endif
  49. #if __ISO_C_VISIBLE >= 1999
  50. #define FP_ILOGB0 (-INT_MAX)
  51. #define FP_ILOGBNAN INT_MAX
  52. #ifdef __MATH_BUILTIN_CONSTANTS
  53. #define HUGE_VALF __builtin_huge_valf()
  54. #define HUGE_VALL __builtin_huge_vall()
  55. #define INFINITY __builtin_inff()
  56. #define NAN __builtin_nanf("")
  57. #else
  58. #define HUGE_VALF (float)HUGE_VAL
  59. #define HUGE_VALL (long double)HUGE_VAL
  60. #define INFINITY HUGE_VALF
  61. #define NAN (__nan.__uf)
  62. #endif /* __MATH_BUILTIN_CONSTANTS */
  63. #define MATH_ERRNO 1
  64. #define MATH_ERREXCEPT 2
  65. #define math_errhandling MATH_ERREXCEPT
  66. #define FP_FAST_FMAF 1
  67. #ifdef __ia64__
  68. #define FP_FAST_FMA 1
  69. #define FP_FAST_FMAL 1
  70. #endif
  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. #define fpclassify(x) \
  78. ((sizeof (x) == sizeof (float)) ? __fpclassifyf(x) \
  79. : (sizeof (x) == sizeof (double)) ? __fpclassifyd(x) \
  80. : __fpclassifyl(x))
  81. #define isfinite(x) \
  82. ((sizeof (x) == sizeof (float)) ? __isfinitef(x) \
  83. : (sizeof (x) == sizeof (double)) ? __isfinite(x) \
  84. : __isfinitel(x))
  85. #define isinf(x) \
  86. ((sizeof (x) == sizeof (float)) ? __isinff(x) \
  87. : (sizeof (x) == sizeof (double)) ? isinf(x) \
  88. : __isinfl(x))
  89. #define isnan(x) \
  90. ((sizeof (x) == sizeof (float)) ? __isnanf(x) \
  91. : (sizeof (x) == sizeof (double)) ? isnan(x) \
  92. : __isnanl(x))
  93. #define isnormal(x) \
  94. ((sizeof (x) == sizeof (float)) ? __isnormalf(x) \
  95. : (sizeof (x) == sizeof (double)) ? __isnormal(x) \
  96. : __isnormall(x))
  97. #ifdef __MATH_BUILTIN_RELOPS
  98. #define isgreater(x, y) __builtin_isgreater((x), (y))
  99. #define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y))
  100. #define isless(x, y) __builtin_isless((x), (y))
  101. #define islessequal(x, y) __builtin_islessequal((x), (y))
  102. #define islessgreater(x, y) __builtin_islessgreater((x), (y))
  103. #define isunordered(x, y) __builtin_isunordered((x), (y))
  104. #else
  105. #define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y))
  106. #define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y))
  107. #define isless(x, y) (!isunordered((x), (y)) && (x) < (y))
  108. #define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y))
  109. #define islessgreater(x, y) (!isunordered((x), (y)) && \
  110. ((x) > (y) || (y) > (x)))
  111. #define isunordered(x, y) (isnan(x) || isnan(y))
  112. #endif /* __MATH_BUILTIN_RELOPS */
  113. #define signbit(x) \
  114. ((sizeof (x) == sizeof (float)) ? __signbitf(x) \
  115. : (sizeof (x) == sizeof (double)) ? __signbit(x) \
  116. : __signbitl(x))
  117. //VBS
  118. //typedef __double_t double_t;
  119. //typedef __float_t float_t;
  120. #endif /* __ISO_C_VISIBLE >= 1999 */
  121. /*
  122. * XOPEN/SVID
  123. */
  124. #if __BSD_VISIBLE || __XSI_VISIBLE
  125. #define M_E 2.7182818284590452354 /* e */
  126. #define M_LOG2E 1.4426950408889634074 /* log 2e */
  127. #define M_LOG10E 0.43429448190325182765 /* log 10e */
  128. #define M_LN2 0.69314718055994530942 /* log e2 */
  129. #define M_LN10 2.30258509299404568402 /* log e10 */
  130. #define M_PI 3.14159265358979323846 /* pi */
  131. #define M_PI_2 1.57079632679489661923 /* pi/2 */
  132. #define M_PI_4 0.78539816339744830962 /* pi/4 */
  133. #define M_1_PI 0.31830988618379067154 /* 1/pi */
  134. #define M_2_PI 0.63661977236758134308 /* 2/pi */
  135. #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  136. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  137. #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
  138. #define MAXFLOAT ((float)3.40282346638528860e+38)
  139. extern int signgam;
  140. #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
  141. #if __BSD_VISIBLE
  142. #if 0
  143. /* Old value from 4.4BSD-Lite openlibm.h; this is probably better. */
  144. #define HUGE HUGE_VAL
  145. #else
  146. #define HUGE MAXFLOAT
  147. #endif
  148. #endif /* __BSD_VISIBLE */
  149. //VBS
  150. //#ifdef _COMPLEX_H
  151. /*
  152. * C99 specifies that complex numbers have the same representation as
  153. * an array of two elements, where the first element is the real part
  154. * and the second element is the imaginary part.
  155. */
  156. typedef union {
  157. float complex f;
  158. float a[2];
  159. } float_complex;
  160. typedef union {
  161. double complex f;
  162. double a[2];
  163. } double_complex;
  164. typedef union {
  165. long double complex f;
  166. long double a[2];
  167. } long_double_complex;
  168. #define REALPART(z) ((z).a[0])
  169. #define IMAGPART(z) ((z).a[1])
  170. /*
  171. * Inline functions that can be used to construct complex values.
  172. *
  173. * The C99 standard intends x+I*y to be used for this, but x+I*y is
  174. * currently unusable in general since gcc introduces many overflow,
  175. * underflow, sign and efficiency bugs by rewriting I*y as
  176. * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
  177. * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
  178. * to -0.0+I*0.0.
  179. *
  180. * In C11, a CMPLX(x,y) macro was added to circumvent this limitation,
  181. * and gcc 4.7 added a __builtin_complex feature to simplify implementation
  182. * of CMPLX in libc, so we can take advantage of these features if they
  183. * are available.
  184. */
  185. #if defined(CMPLXF) && defined(CMPLX) && defined(CMPLXL) /* C11 */
  186. # define cpackf(x,y) CMPLXF(x,y)
  187. # define cpack(x,y) CMPLX(x,y)
  188. # define cpackl(x,y) CMPLXL(x,y)
  189. #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__INTEL_COMPILER)
  190. # define cpackf(x,y) __builtin_complex ((float) (x), (float) (y))
  191. # define cpack(x,y) __builtin_complex ((double) (x), (double) (y))
  192. # define cpackl(x,y) __builtin_complex ((long double) (x), (long double) (y))
  193. #else /* define our own cpack functions */
  194. static __inline float complex
  195. cpackf(float x, float y)
  196. {
  197. float_complex z;
  198. REALPART(z) = x;
  199. IMAGPART(z) = y;
  200. return (z.f);
  201. }
  202. static __inline double complex
  203. cpack(double x, double y)
  204. {
  205. double_complex z;
  206. REALPART(z) = x;
  207. IMAGPART(z) = y;
  208. return (z.f);
  209. }
  210. static __inline long double complex
  211. cpackl(long double x, long double y)
  212. {
  213. long_double_complex z;
  214. REALPART(z) = x;
  215. IMAGPART(z) = y;
  216. return (z.f);
  217. }
  218. #endif /* define our own cpack functions */
  219. //VBS
  220. //#endif /* _COMPLEX_H */
  221. /*
  222. * Most of these functions depend on the rounding mode and have the side
  223. * effect of raising floating-point exceptions, so they are not declared
  224. * as __pure2. In C99, FENV_ACCESS affects the purity of these functions.
  225. */
  226. __BEGIN_DECLS
  227. /*
  228. * ANSI/POSIX
  229. */
  230. int __fpclassifyd(double) __pure2;
  231. int __fpclassifyf(float) __pure2;
  232. int __fpclassifyl(long double) __pure2;
  233. int __isfinitef(float) __pure2;
  234. int __isfinite(double) __pure2;
  235. int __isfinitel(long double) __pure2;
  236. int __isinff(float) __pure2;
  237. int __isinfl(long double) __pure2;
  238. int __isnanf(float) __pure2;
  239. int __isnanl(long double) __pure2;
  240. int __isnormalf(float) __pure2;
  241. int __isnormal(double) __pure2;
  242. int __isnormall(long double) __pure2;
  243. int __signbit(double) __pure2;
  244. int __signbitf(float) __pure2;
  245. int __signbitl(long double) __pure2;
  246. double acos(double);
  247. double asin(double);
  248. double atan(double);
  249. double atan2(double, double);
  250. double cos(double);
  251. double sin(double);
  252. double tan(double);
  253. double cosh(double);
  254. double sinh(double);
  255. double tanh(double);
  256. double exp(double);
  257. double frexp(double, int *); /* fundamentally !__pure2 */
  258. double ldexp(double, int);
  259. double log(double);
  260. double log10(double);
  261. double modf(double, double *); /* fundamentally !__pure2 */
  262. double pow(double, double);
  263. double sqrt(double);
  264. double ceil(double);
  265. double fabs(double) __pure2;
  266. double floor(double);
  267. double fmod(double, double);
  268. /*
  269. * These functions are not in C90.
  270. */
  271. #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE
  272. double acosh(double);
  273. double asinh(double);
  274. double atanh(double);
  275. double cbrt(double);
  276. double erf(double);
  277. double erfc(double);
  278. double exp2(double);
  279. double expm1(double);
  280. double fma(double, double, double);
  281. double hypot(double, double);
  282. int ilogb(double) __pure2;
  283. int (isinf)(double) __pure2;
  284. int (isnan)(double) __pure2;
  285. double lgamma(double);
  286. long long llrint(double);
  287. long long llround(double);
  288. double log1p(double);
  289. double log2(double);
  290. double logb(double);
  291. long lrint(double);
  292. long lround(double);
  293. double nan(const char *) __pure2;
  294. double nextafter(double, double);
  295. double remainder(double, double);
  296. double remquo(double, double, int *);
  297. double rint(double);
  298. #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */
  299. #if __BSD_VISIBLE || __XSI_VISIBLE
  300. double j0(double);
  301. double j1(double);
  302. double jn(int, double);
  303. double y0(double);
  304. double y1(double);
  305. double yn(int, double);
  306. #if __XSI_VISIBLE <= 500 || __BSD_VISIBLE
  307. double gamma(double);
  308. #endif
  309. #if __XSI_VISIBLE <= 600 || __BSD_VISIBLE
  310. double scalb(double, double);
  311. #endif
  312. #endif /* __BSD_VISIBLE || __XSI_VISIBLE */
  313. #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999
  314. double copysign(double, double) __pure2;
  315. double fdim(double, double);
  316. double fmax(double, double) __pure2;
  317. double fmin(double, double) __pure2;
  318. double nearbyint(double);
  319. double round(double);
  320. double scalbln(double, long);
  321. double scalbn(double, int);
  322. double tgamma(double);
  323. double trunc(double);
  324. #endif
  325. /*
  326. * BSD math library entry points
  327. */
  328. #if __BSD_VISIBLE
  329. double drem(double, double);
  330. int finite(double) __pure2;
  331. int isnanf(float) __pure2;
  332. /*
  333. * Reentrant version of gamma & lgamma; passes signgam back by reference
  334. * as the second argument; user must allocate space for signgam.
  335. */
  336. double gamma_r(double, int *);
  337. double lgamma_r(double, int *);
  338. /*
  339. * IEEE Test Vector
  340. */
  341. double significand(double);
  342. #endif /* __BSD_VISIBLE */
  343. /* float versions of ANSI/POSIX functions */
  344. #if __ISO_C_VISIBLE >= 1999
  345. float acosf(float);
  346. float asinf(float);
  347. float atanf(float);
  348. float atan2f(float, float);
  349. float cosf(float);
  350. float sinf(float);
  351. float tanf(float);
  352. float coshf(float);
  353. float sinhf(float);
  354. float tanhf(float);
  355. float exp2f(float);
  356. float expf(float);
  357. float expm1f(float);
  358. float frexpf(float, int *); /* fundamentally !__pure2 */
  359. int ilogbf(float) __pure2;
  360. float ldexpf(float, int);
  361. float log10f(float);
  362. float log1pf(float);
  363. float log2f(float);
  364. float logf(float);
  365. float modff(float, float *); /* fundamentally !__pure2 */
  366. float powf(float, float);
  367. float sqrtf(float);
  368. float ceilf(float);
  369. float fabsf(float) __pure2;
  370. float floorf(float);
  371. float fmodf(float, float);
  372. float roundf(float);
  373. float erff(float);
  374. float erfcf(float);
  375. float hypotf(float, float);
  376. float lgammaf(float);
  377. float tgammaf(float);
  378. float acoshf(float);
  379. float asinhf(float);
  380. float atanhf(float);
  381. float cbrtf(float);
  382. float logbf(float);
  383. float copysignf(float, float) __pure2;
  384. long long llrintf(float);
  385. long long llroundf(float);
  386. long lrintf(float);
  387. long lroundf(float);
  388. float nanf(const char *) __pure2;
  389. float nearbyintf(float);
  390. float nextafterf(float, float);
  391. float remainderf(float, float);
  392. float remquof(float, float, int *);
  393. float rintf(float);
  394. float scalblnf(float, long);
  395. float scalbnf(float, int);
  396. float truncf(float);
  397. float fdimf(float, float);
  398. float fmaf(float, float, float);
  399. float fmaxf(float, float) __pure2;
  400. float fminf(float, float) __pure2;
  401. #endif
  402. /*
  403. * float versions of BSD math library entry points
  404. */
  405. #if __BSD_VISIBLE
  406. float dremf(float, float);
  407. int finitef(float) __pure2;
  408. float gammaf(float);
  409. float j0f(float);
  410. float j1f(float);
  411. float jnf(int, float);
  412. float scalbf(float, float);
  413. float y0f(float);
  414. float y1f(float);
  415. float ynf(int, float);
  416. /*
  417. * Float versions of reentrant version of gamma & lgamma; passes
  418. * signgam back by reference as the second argument; user must
  419. * allocate space for signgam.
  420. */
  421. float gammaf_r(float, int *);
  422. float lgammaf_r(float, int *);
  423. /*
  424. * float version of IEEE Test Vector
  425. */
  426. float significandf(float);
  427. #endif /* __BSD_VISIBLE */
  428. /*
  429. * long double versions of ISO/POSIX math functions
  430. */
  431. #if __ISO_C_VISIBLE >= 1999
  432. #if _DECLARE_C99_LDBL_MATH
  433. long double acoshl(long double);
  434. #endif
  435. long double acosl(long double);
  436. #if _DECLARE_C99_LDBL_MATH
  437. long double asinhl(long double);
  438. #endif
  439. long double asinl(long double);
  440. long double atan2l(long double, long double);
  441. #if _DECLARE_C99_LDBL_MATH
  442. long double atanhl(long double);
  443. #endif
  444. long double atanl(long double);
  445. long double cbrtl(long double);
  446. long double ceill(long double);
  447. long double copysignl(long double, long double) __pure2;
  448. #if _DECLARE_C99_LDBL_MATH
  449. long double coshl(long double);
  450. #endif
  451. long double cosl(long double);
  452. #if _DECLARE_C99_LDBL_MATH
  453. long double erfcl(long double);
  454. long double erfl(long double);
  455. #endif
  456. long double exp2l(long double);
  457. #if _DECLARE_C99_LDBL_MATH
  458. long double expl(long double);
  459. long double expm1l(long double);
  460. #endif
  461. long double fabsl(long double) __pure2;
  462. long double fdiml(long double, long double);
  463. long double floorl(long double);
  464. long double fmal(long double, long double, long double);
  465. long double fmaxl(long double, long double) __pure2;
  466. long double fminl(long double, long double) __pure2;
  467. long double fmodl(long double, long double);
  468. long double frexpl(long double value, int *); /* fundamentally !__pure2 */
  469. long double hypotl(long double, long double);
  470. int ilogbl(long double) __pure2;
  471. long double ldexpl(long double, int);
  472. #if _DECLARE_C99_LDBL_MATH
  473. long double lgammal(long double);
  474. #endif
  475. long long llrintl(long double);
  476. long long llroundl(long double);
  477. #if _DECLARE_C99_LDBL_MATH
  478. long double log10l(long double);
  479. long double log1pl(long double);
  480. long double log2l(long double);
  481. #endif
  482. long double logbl(long double);
  483. #if _DECLARE_C99_LDBL_MATH
  484. long double logl(long double);
  485. #endif
  486. long lrintl(long double);
  487. long lroundl(long double);
  488. long double modfl(long double, long double *); /* fundamentally !__pure2 */
  489. long double nanl(const char *) __pure2;
  490. long double nearbyintl(long double);
  491. long double nextafterl(long double, long double);
  492. double nexttoward(double, long double);
  493. float nexttowardf(float, long double);
  494. long double nexttowardl(long double, long double);
  495. #if _DECLARE_C99_LDBL_MATH
  496. long double powl(long double, long double);
  497. #endif
  498. long double remainderl(long double, long double);
  499. long double remquol(long double, long double, int *);
  500. long double rintl(long double);
  501. long double roundl(long double);
  502. long double scalblnl(long double, long);
  503. long double scalbnl(long double, int);
  504. #if _DECLARE_C99_LDBL_MATH
  505. long double sinhl(long double);
  506. #endif
  507. long double sinl(long double);
  508. long double sqrtl(long double);
  509. #if _DECLARE_C99_LDBL_MATH
  510. long double tanhl(long double);
  511. #endif
  512. long double tanl(long double);
  513. #if _DECLARE_C99_LDBL_MATH
  514. long double tgammal(long double);
  515. #endif
  516. long double truncl(long double);
  517. #endif /* __ISO_C_VISIBLE >= 1999 */
  518. __END_DECLS
  519. #endif /* !_MATH_H_ */