You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

630 lines
19KB

  1. #ifndef _MATH_H_
  2. #define _MATH_H_
  3. #include <sys/reent.h>
  4. #include <sys/cdefs.h>
  5. #include <machine/ieeefp.h>
  6. #include "_ansi.h"
  7. _BEGIN_STD_C
  8. /* Natural log of 2 */
  9. #define _M_LN2 0.693147180559945309417
  10. #if __GNUC_PREREQ (3, 3)
  11. /* gcc >= 3.3 implicitly defines builtins for HUGE_VALx values. */
  12. # ifndef HUGE_VAL
  13. # define HUGE_VAL (__builtin_huge_val())
  14. # endif
  15. # ifndef HUGE_VALF
  16. # define HUGE_VALF (__builtin_huge_valf())
  17. # endif
  18. # ifndef HUGE_VALL
  19. # define HUGE_VALL (__builtin_huge_vall())
  20. # endif
  21. # ifndef INFINITY
  22. # define INFINITY (__builtin_inff())
  23. # endif
  24. # ifndef NAN
  25. # define NAN (__builtin_nanf(""))
  26. # endif
  27. #else /* !gcc >= 3.3 */
  28. /* No builtins. Use fixed defines instead. (All 3 HUGE plus the INFINITY
  29. * and NAN macros are required to be constant expressions. Using a variable--
  30. * even a static const--does not meet this requirement, as it cannot be
  31. * evaluated at translation time.)
  32. * The infinities are done using numbers that are far in excess of
  33. * something that would be expected to be encountered in a floating-point
  34. * implementation. (A more certain way uses values from float.h, but that is
  35. * avoided because system includes are not supposed to include each other.)
  36. * This method might produce warnings from some compilers. (It does in
  37. * newer GCCs, but not for ones that would hit this #else.) If this happens,
  38. * please report details to the Newlib mailing list. */
  39. #ifndef HUGE_VAL
  40. #define HUGE_VAL (1.0e999999999)
  41. #endif
  42. #ifndef HUGE_VALF
  43. #define HUGE_VALF (1.0e999999999F)
  44. #endif
  45. #if !defined(HUGE_VALL) && defined(_HAVE_LONG_DOUBLE)
  46. #define HUGE_VALL (1.0e999999999L)
  47. #endif
  48. #if !defined(INFINITY)
  49. #define INFINITY (HUGE_VALF)
  50. #endif
  51. #if !defined(NAN)
  52. #if defined(__GNUC__) && defined(__cplusplus)
  53. /* Exception: older g++ versions warn about the divide by 0 used in the
  54. * normal case (even though older gccs do not). This trick suppresses the
  55. * warning, but causes errors for plain gcc, so is only used in the one
  56. * special case. */
  57. static const union { __ULong __i[1]; float __d; } __Nanf = {0x7FC00000};
  58. #define NAN (__Nanf.__d)
  59. #else
  60. #define NAN (0.0F/0.0F)
  61. #endif
  62. #endif
  63. #endif /* !gcc >= 3.3 */
  64. /* Reentrant ANSI C functions. */
  65. #ifndef __math_68881
  66. extern double atan (double);
  67. extern double cos (double);
  68. extern double sin (double);
  69. extern double tan (double);
  70. extern double tanh (double);
  71. extern double frexp (double, int *);
  72. extern double modf (double, double *);
  73. extern double ceil (double);
  74. extern double fabs (double);
  75. extern double floor (double);
  76. #endif /* ! defined (__math_68881) */
  77. /* Non reentrant ANSI C functions. */
  78. #ifndef _REENT_ONLY
  79. #ifndef __math_68881
  80. extern double acos (double);
  81. extern double asin (double);
  82. extern double atan2 (double, double);
  83. extern double cosh (double);
  84. extern double sinh (double);
  85. extern double exp (double);
  86. extern double ldexp (double, int);
  87. extern double log (double);
  88. extern double log10 (double);
  89. extern double pow (double, double);
  90. extern double sqrt (double);
  91. extern double fmod (double, double);
  92. #endif /* ! defined (__math_68881) */
  93. #endif /* ! defined (_REENT_ONLY) */
  94. #if __MISC_VISIBLE
  95. extern int finite (double);
  96. extern int finitef (float);
  97. extern int finitel (long double);
  98. extern int isinff (float);
  99. extern int isnanf (float);
  100. #ifdef __CYGWIN__ /* not implemented in newlib yet */
  101. extern int isinfl (long double);
  102. extern int isnanl (long double);
  103. #endif
  104. #if !defined(__cplusplus) || __cplusplus < 201103L
  105. extern int isinf (double);
  106. #endif
  107. #endif /* __MISC_VISIBLE */
  108. #if (__MISC_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)) \
  109. && (!defined(__cplusplus) || __cplusplus < 201103L)
  110. extern int isnan (double);
  111. #endif
  112. #if __ISO_C_VISIBLE >= 1999
  113. /* ISO C99 types and macros. */
  114. /* FIXME: FLT_EVAL_METHOD should somehow be gotten from float.h (which is hard,
  115. * considering that the standard says the includes it defines should not
  116. * include other includes that it defines) and that value used. (This can be
  117. * solved, but autoconf has a bug which makes the solution more difficult, so
  118. * it has been skipped for now.) */
  119. #if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__)
  120. #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
  121. #define __TMP_FLT_EVAL_METHOD
  122. #endif /* FLT_EVAL_METHOD */
  123. #if defined FLT_EVAL_METHOD
  124. #if FLT_EVAL_METHOD == 0
  125. typedef float float_t;
  126. typedef double double_t;
  127. #elif FLT_EVAL_METHOD == 1
  128. typedef double float_t;
  129. typedef double double_t;
  130. #elif FLT_EVAL_METHOD == 2
  131. typedef long double float_t;
  132. typedef long double double_t;
  133. #else
  134. /* Implementation-defined. Assume float_t and double_t have been
  135. * defined previously for this configuration (e.g. config.h). */
  136. #endif
  137. #else
  138. /* Assume basic definitions. */
  139. typedef float float_t;
  140. typedef double double_t;
  141. #endif
  142. #if defined(__TMP_FLT_EVAL_METHOD)
  143. #undef FLT_EVAL_METHOD
  144. #endif
  145. #define FP_NAN 0
  146. #define FP_INFINITE 1
  147. #define FP_ZERO 2
  148. #define FP_SUBNORMAL 3
  149. #define FP_NORMAL 4
  150. #ifndef FP_ILOGB0
  151. # define FP_ILOGB0 (-__INT_MAX__)
  152. #endif
  153. #ifndef FP_ILOGBNAN
  154. # define FP_ILOGBNAN __INT_MAX__
  155. #endif
  156. #ifndef MATH_ERRNO
  157. # define MATH_ERRNO 1
  158. #endif
  159. #ifndef MATH_ERREXCEPT
  160. # define MATH_ERREXCEPT 2
  161. #endif
  162. #ifndef math_errhandling
  163. # define math_errhandling MATH_ERRNO
  164. #endif
  165. extern int __isinff (float x);
  166. extern int __isinfd (double x);
  167. extern int __isnanf (float x);
  168. extern int __isnand (double x);
  169. extern int __fpclassifyf (float x);
  170. extern int __fpclassifyd (double x);
  171. extern int __signbitf (float x);
  172. extern int __signbitd (double x);
  173. /* Note: isinf and isnan were once functions in newlib that took double
  174. * arguments. C99 specifies that these names are reserved for macros
  175. * supporting multiple floating point types. Thus, they are
  176. * now defined as macros. Implementations of the old functions
  177. * taking double arguments still exist for compatibility purposes
  178. * (prototypes for them are earlier in this header). */
  179. #if __GNUC_PREREQ (4, 4)
  180. #define fpclassify(__x) (__builtin_fpclassify (FP_NAN, FP_INFINITE, \
  181. FP_NORMAL, FP_SUBNORMAL, \
  182. FP_ZERO, __x))
  183. #ifndef isfinite
  184. #define isfinite(__x) (__builtin_isfinite (__x))
  185. #endif
  186. #ifndef isinf
  187. #define isinf(__x) (__builtin_isinf_sign (__x))
  188. #endif
  189. #ifndef isnan
  190. #define isnan(__x) (__builtin_isnan (__x))
  191. #endif
  192. #define isnormal(__x) (__builtin_isnormal (__x))
  193. #else
  194. #define fpclassify(__x) \
  195. ((sizeof(__x) == sizeof(float)) ? __fpclassifyf(__x) : \
  196. __fpclassifyd(__x))
  197. #ifndef isfinite
  198. #define isfinite(__y) \
  199. (__extension__ ({int __cy = fpclassify(__y); \
  200. __cy != FP_INFINITE && __cy != FP_NAN;}))
  201. #endif
  202. #ifndef isinf
  203. #define isinf(__x) (fpclassify(__x) == FP_INFINITE)
  204. #endif
  205. #ifndef isnan
  206. #define isnan(__x) (fpclassify(__x) == FP_NAN)
  207. #endif
  208. #define isnormal(__x) (fpclassify(__x) == FP_NORMAL)
  209. #endif
  210. #if __GNUC_PREREQ (4, 0)
  211. #if defined(_HAVE_LONG_DOUBLE)
  212. #define signbit(__x) \
  213. ((sizeof(__x) == sizeof(float)) ? __builtin_signbitf(__x) : \
  214. (sizeof(__x) == sizeof(double)) ? __builtin_signbit (__x) : \
  215. __builtin_signbitl(__x))
  216. #else
  217. #define signbit(__x) \
  218. ((sizeof(__x) == sizeof(float)) ? __builtin_signbitf(__x) : \
  219. __builtin_signbit (__x))
  220. #endif
  221. #else
  222. #define signbit(__x) \
  223. ((sizeof(__x) == sizeof(float)) ? __signbitf(__x) : \
  224. __signbitd(__x))
  225. #endif
  226. #if __GNUC_PREREQ (2, 97)
  227. #define isgreater(__x,__y) (__builtin_isgreater (__x, __y))
  228. #define isgreaterequal(__x,__y) (__builtin_isgreaterequal (__x, __y))
  229. #define isless(__x,__y) (__builtin_isless (__x, __y))
  230. #define islessequal(__x,__y) (__builtin_islessequal (__x, __y))
  231. #define islessgreater(__x,__y) (__builtin_islessgreater (__x, __y))
  232. #define isunordered(__x,__y) (__builtin_isunordered (__x, __y))
  233. #else
  234. #define isgreater(x,y) \
  235. (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  236. !isunordered(__x,__y) && (__x > __y);}))
  237. #define isgreaterequal(x,y) \
  238. (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  239. !isunordered(__x,__y) && (__x >= __y);}))
  240. #define isless(x,y) \
  241. (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  242. !isunordered(__x,__y) && (__x < __y);}))
  243. #define islessequal(x,y) \
  244. (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  245. !isunordered(__x,__y) && (__x <= __y);}))
  246. #define islessgreater(x,y) \
  247. (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  248. !isunordered(__x,__y) && (__x < __y || __x > __y);}))
  249. #define isunordered(a,b) \
  250. (__extension__ ({__typeof__(a) __a = (a); __typeof__(b) __b = (b); \
  251. fpclassify(__a) == FP_NAN || fpclassify(__b) == FP_NAN;}))
  252. #endif
  253. /* Non ANSI double precision functions. */
  254. extern double infinity (void);
  255. extern double nan (const char *);
  256. extern double copysign (double, double);
  257. extern double logb (double);
  258. extern int ilogb (double);
  259. extern double asinh (double);
  260. extern double cbrt (double);
  261. extern double nextafter (double, double);
  262. extern double rint (double);
  263. extern double scalbn (double, int);
  264. extern double exp2 (double);
  265. extern double scalbln (double, long int);
  266. extern double tgamma (double);
  267. extern double nearbyint (double);
  268. extern long int lrint (double);
  269. extern long long int llrint (double);
  270. extern double round (double);
  271. extern long int lround (double);
  272. extern long long int llround (double);
  273. extern double trunc (double);
  274. extern double remquo (double, double, int *);
  275. extern double fdim (double, double);
  276. extern double fmax (double, double);
  277. extern double fmin (double, double);
  278. extern double fma (double, double, double);
  279. #ifndef __math_68881
  280. extern double log1p (double);
  281. extern double expm1 (double);
  282. #endif /* ! defined (__math_68881) */
  283. #ifndef _REENT_ONLY
  284. extern double acosh (double);
  285. extern double atanh (double);
  286. extern double remainder (double, double);
  287. extern double gamma (double);
  288. extern double lgamma (double);
  289. extern double erf (double);
  290. extern double erfc (double);
  291. extern double log2 (double);
  292. #if !defined(__cplusplus)
  293. #define log2(x) (log (x) / _M_LN2)
  294. #endif
  295. #ifndef __math_68881
  296. extern double hypot (double, double);
  297. #endif
  298. #endif /* ! defined (_REENT_ONLY) */
  299. /* Single precision versions of ANSI functions. */
  300. extern float atanf (float);
  301. extern float cosf (float);
  302. extern float sinf (float);
  303. extern float tanf (float);
  304. extern float tanhf (float);
  305. extern float frexpf (float, int *);
  306. extern float modff (float, float *);
  307. extern float ceilf (float);
  308. extern float fabsf (float);
  309. extern float floorf (float);
  310. #ifndef _REENT_ONLY
  311. extern float acosf (float);
  312. extern float asinf (float);
  313. extern float atan2f (float, float);
  314. extern float coshf (float);
  315. extern float sinhf (float);
  316. extern float expf (float);
  317. extern float ldexpf (float, int);
  318. extern float logf (float);
  319. extern float log10f (float);
  320. extern float powf (float, float);
  321. extern float sqrtf (float);
  322. extern float fmodf (float, float);
  323. #endif /* ! defined (_REENT_ONLY) */
  324. /* Other single precision functions. */
  325. extern float exp2f (float);
  326. extern float scalblnf (float, long int);
  327. extern float tgammaf (float);
  328. extern float nearbyintf (float);
  329. extern long int lrintf (float);
  330. extern long long int llrintf (float);
  331. extern float roundf (float);
  332. extern long int lroundf (float);
  333. extern long long int llroundf (float);
  334. extern float truncf (float);
  335. extern float remquof (float, float, int *);
  336. extern float fdimf (float, float);
  337. extern float fmaxf (float, float);
  338. extern float fminf (float, float);
  339. extern float fmaf (float, float, float);
  340. extern float infinityf (void);
  341. extern float nanf (const char *);
  342. extern float copysignf (float, float);
  343. extern float logbf (float);
  344. extern int ilogbf (float);
  345. extern float asinhf (float);
  346. extern float cbrtf (float);
  347. extern float nextafterf (float, float);
  348. extern float rintf (float);
  349. extern float scalbnf (float, int);
  350. extern float log1pf (float);
  351. extern float expm1f (float);
  352. #ifndef _REENT_ONLY
  353. extern float acoshf (float);
  354. extern float atanhf (float);
  355. extern float remainderf (float, float);
  356. extern float gammaf (float);
  357. extern float lgammaf (float);
  358. extern float erff (float);
  359. extern float erfcf (float);
  360. extern float log2f (float);
  361. extern float hypotf (float, float);
  362. #endif /* ! defined (_REENT_ONLY) */
  363. /* Newlib doesn't fully support long double math functions so far.
  364. On platforms where long double equals double the long double functions
  365. simply call the double functions. On Cygwin the long double functions
  366. are implemented independently from newlib to be able to use optimized
  367. assembler functions despite using the Microsoft x86_64 ABI. */
  368. #if defined (_LDBL_EQ_DBL) || defined (__CYGWIN__)
  369. /* Reentrant ANSI C functions. */
  370. #ifndef __math_68881
  371. extern long double atanl (long double);
  372. extern long double cosl (long double);
  373. extern long double sinl (long double);
  374. extern long double tanl (long double);
  375. extern long double tanhl (long double);
  376. extern long double frexpl (long double, int *);
  377. extern long double modfl (long double, long double *);
  378. extern long double ceill (long double);
  379. extern long double fabsl (long double);
  380. extern long double floorl (long double);
  381. extern long double log1pl (long double);
  382. extern long double expm1l (long double);
  383. #endif /* ! defined (__math_68881) */
  384. /* Non reentrant ANSI C functions. */
  385. #ifndef _REENT_ONLY
  386. #ifndef __math_68881
  387. extern long double acosl (long double);
  388. extern long double asinl (long double);
  389. extern long double atan2l (long double, long double);
  390. extern long double coshl (long double);
  391. extern long double sinhl (long double);
  392. extern long double expl (long double);
  393. extern long double ldexpl (long double, int);
  394. extern long double logl (long double);
  395. extern long double log10l (long double);
  396. extern long double powl (long double, long double);
  397. extern long double sqrtl (long double);
  398. extern long double fmodl (long double, long double);
  399. extern long double hypotl (long double, long double);
  400. #endif /* ! defined (__math_68881) */
  401. #endif /* ! defined (_REENT_ONLY) */
  402. extern long double copysignl (long double, long double);
  403. extern long double nanl (const char *);
  404. extern int ilogbl (long double);
  405. extern long double asinhl (long double);
  406. extern long double cbrtl (long double);
  407. extern long double nextafterl (long double, long double);
  408. extern float nexttowardf (float, long double);
  409. extern double nexttoward (double, long double);
  410. extern long double nexttowardl (long double, long double);
  411. extern long double logbl (long double);
  412. extern long double log2l (long double);
  413. extern long double rintl (long double);
  414. extern long double scalbnl (long double, int);
  415. extern long double exp2l (long double);
  416. extern long double scalblnl (long double, long);
  417. extern long double tgammal (long double);
  418. extern long double nearbyintl (long double);
  419. extern long int lrintl (long double);
  420. extern long long int llrintl (long double);
  421. extern long double roundl (long double);
  422. extern long lroundl (long double);
  423. extern long long int llroundl (long double);
  424. extern long double truncl (long double);
  425. extern long double remquol (long double, long double, int *);
  426. extern long double fdiml (long double, long double);
  427. extern long double fmaxl (long double, long double);
  428. extern long double fminl (long double, long double);
  429. extern long double fmal (long double, long double, long double);
  430. #ifndef _REENT_ONLY
  431. extern long double acoshl (long double);
  432. extern long double atanhl (long double);
  433. extern long double remainderl (long double, long double);
  434. extern long double lgammal (long double);
  435. extern long double erfl (long double);
  436. extern long double erfcl (long double);
  437. #endif /* ! defined (_REENT_ONLY) */
  438. #else /* !_LDBL_EQ_DBL && !__CYGWIN__ */
  439. extern long double hypotl (long double, long double);
  440. extern long double sqrtl (long double);
  441. #ifdef __i386__
  442. /* Other long double precision functions. */
  443. extern _LONG_DOUBLE rintl (_LONG_DOUBLE);
  444. extern long int lrintl (_LONG_DOUBLE);
  445. extern long long int llrintl (_LONG_DOUBLE);
  446. #endif /* __i386__ */
  447. #endif /* !_LDBL_EQ_DBL && !__CYGWIN__ */
  448. #endif /* __ISO_C_VISIBLE >= 1999 */
  449. #if __MISC_VISIBLE
  450. extern double drem (double, double);
  451. extern float dremf (float, float);
  452. #ifdef __CYGWIN__
  453. extern float dreml (long double, long double);
  454. #endif /* __CYGWIN__ */
  455. extern double gamma_r (double, int *);
  456. extern double lgamma_r (double, int *);
  457. extern float gammaf_r (float, int *);
  458. extern float lgammaf_r (float, int *);
  459. #endif
  460. #if __MISC_VISIBLE || __XSI_VISIBLE
  461. extern double y0 (double);
  462. extern double y1 (double);
  463. extern double yn (int, double);
  464. extern double j0 (double);
  465. extern double j1 (double);
  466. extern double jn (int, double);
  467. #endif
  468. #if __MISC_VISIBLE || __XSI_VISIBLE >= 600
  469. extern float y0f (float);
  470. extern float y1f (float);
  471. extern float ynf (int, float);
  472. extern float j0f (float);
  473. extern float j1f (float);
  474. extern float jnf (int, float);
  475. #endif
  476. /* GNU extensions */
  477. #if __GNU_VISIBLE
  478. extern void sincos (double, double *, double *);
  479. extern void sincosf (float, float *, float *);
  480. #ifdef __CYGWIN__
  481. extern void sincosl (long double, long double *, long double *);
  482. #endif /* __CYGWIN__ */
  483. # ifndef exp10
  484. extern double exp10 (double);
  485. # endif
  486. # ifndef pow10
  487. extern double pow10 (double);
  488. # endif
  489. # ifndef exp10f
  490. extern float exp10f (float);
  491. # endif
  492. # ifndef pow10f
  493. extern float pow10f (float);
  494. # endif
  495. #ifdef __CYGWIN__
  496. # ifndef exp10l
  497. extern float exp10l (float);
  498. # endif
  499. # ifndef pow10l
  500. extern float pow10l (float);
  501. # endif
  502. #endif /* __CYGWIN__ */
  503. #endif /* __GNU_VISIBLE */
  504. #if __MISC_VISIBLE || __XSI_VISIBLE
  505. /* The gamma functions use a global variable, signgam. */
  506. #ifndef _REENT_ONLY
  507. #define signgam (*__signgam())
  508. extern int *__signgam (void);
  509. #endif /* ! defined (_REENT_ONLY) */
  510. #define __signgam_r(ptr) _REENT_SIGNGAM(ptr)
  511. #endif /* __MISC_VISIBLE || __XSI_VISIBLE */
  512. /* Useful constants. */
  513. #if __BSD_VISIBLE || __XSI_VISIBLE
  514. #define MAXFLOAT 3.40282347e+38F
  515. #define M_E 2.7182818284590452354
  516. #define M_LOG2E 1.4426950408889634074
  517. #define M_LOG10E 0.43429448190325182765
  518. #define M_LN2 _M_LN2
  519. #define M_LN10 2.30258509299404568402
  520. #define M_PI 3.14159265358979323846
  521. #define M_PI_2 1.57079632679489661923
  522. #define M_PI_4 0.78539816339744830962
  523. #define M_1_PI 0.31830988618379067154
  524. #define M_2_PI 0.63661977236758134308
  525. #define M_2_SQRTPI 1.12837916709551257390
  526. #define M_SQRT2 1.41421356237309504880
  527. #define M_SQRT1_2 0.70710678118654752440
  528. #endif
  529. #if __BSD_VISIBLE
  530. #define M_TWOPI (M_PI * 2.0)
  531. #define M_3PI_4 2.3561944901923448370E0
  532. #define M_SQRTPI 1.77245385090551602792981
  533. #define M_LN2LO 1.9082149292705877000E-10
  534. #define M_LN2HI 6.9314718036912381649E-1
  535. #define M_SQRT3 1.73205080756887719000
  536. #define M_IVLN10 0.43429448190325182765 /* 1 / log(10) */
  537. #define M_LOG2_E _M_LN2
  538. #define M_INVLN2 1.4426950408889633870E0 /* 1 / log(2) */
  539. /* Global control over fdlibm error handling. */
  540. enum __fdlibm_version
  541. {
  542. __fdlibm_ieee = -1,
  543. __fdlibm_posix
  544. };
  545. #define _LIB_VERSION_TYPE enum __fdlibm_version
  546. #define _LIB_VERSION __fdlib_version
  547. extern __IMPORT _LIB_VERSION_TYPE _LIB_VERSION;
  548. #define _IEEE_ __fdlibm_ieee
  549. #define _POSIX_ __fdlibm_posix
  550. #endif /* __BSD_VISIBLE */
  551. _END_STD_C
  552. #ifdef __FAST_MATH__
  553. #include <machine/fastmath.h>
  554. #endif
  555. #endif /* _MATH_H_ */