math.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_MATH_H
  10. #define _LIBCPP_MATH_H
  11. /*
  12. math.h synopsis
  13. Macros:
  14. HUGE_VAL
  15. HUGE_VALF // C99
  16. HUGE_VALL // C99
  17. INFINITY // C99
  18. NAN // C99
  19. FP_INFINITE // C99
  20. FP_NAN // C99
  21. FP_NORMAL // C99
  22. FP_SUBNORMAL // C99
  23. FP_ZERO // C99
  24. FP_FAST_FMA // C99
  25. FP_FAST_FMAF // C99
  26. FP_FAST_FMAL // C99
  27. FP_ILOGB0 // C99
  28. FP_ILOGBNAN // C99
  29. MATH_ERRNO // C99
  30. MATH_ERREXCEPT // C99
  31. math_errhandling // C99
  32. Types:
  33. float_t // C99
  34. double_t // C99
  35. // C90
  36. floating_point abs(floating_point x);
  37. floating_point acos (arithmetic x);
  38. float acosf(float x);
  39. long double acosl(long double x);
  40. floating_point asin (arithmetic x);
  41. float asinf(float x);
  42. long double asinl(long double x);
  43. floating_point atan (arithmetic x);
  44. float atanf(float x);
  45. long double atanl(long double x);
  46. floating_point atan2 (arithmetic y, arithmetic x);
  47. float atan2f(float y, float x);
  48. long double atan2l(long double y, long double x);
  49. floating_point ceil (arithmetic x);
  50. float ceilf(float x);
  51. long double ceill(long double x);
  52. floating_point cos (arithmetic x);
  53. float cosf(float x);
  54. long double cosl(long double x);
  55. floating_point cosh (arithmetic x);
  56. float coshf(float x);
  57. long double coshl(long double x);
  58. floating_point exp (arithmetic x);
  59. float expf(float x);
  60. long double expl(long double x);
  61. floating_point fabs (arithmetic x);
  62. float fabsf(float x);
  63. long double fabsl(long double x);
  64. floating_point floor (arithmetic x);
  65. float floorf(float x);
  66. long double floorl(long double x);
  67. floating_point fmod (arithmetic x, arithmetic y);
  68. float fmodf(float x, float y);
  69. long double fmodl(long double x, long double y);
  70. floating_point frexp (arithmetic value, int* exp);
  71. float frexpf(float value, int* exp);
  72. long double frexpl(long double value, int* exp);
  73. floating_point ldexp (arithmetic value, int exp);
  74. float ldexpf(float value, int exp);
  75. long double ldexpl(long double value, int exp);
  76. floating_point log (arithmetic x);
  77. float logf(float x);
  78. long double logl(long double x);
  79. floating_point log10 (arithmetic x);
  80. float log10f(float x);
  81. long double log10l(long double x);
  82. floating_point modf (floating_point value, floating_point* iptr);
  83. float modff(float value, float* iptr);
  84. long double modfl(long double value, long double* iptr);
  85. floating_point pow (arithmetic x, arithmetic y);
  86. float powf(float x, float y);
  87. long double powl(long double x, long double y);
  88. floating_point sin (arithmetic x);
  89. float sinf(float x);
  90. long double sinl(long double x);
  91. floating_point sinh (arithmetic x);
  92. float sinhf(float x);
  93. long double sinhl(long double x);
  94. floating_point sqrt (arithmetic x);
  95. float sqrtf(float x);
  96. long double sqrtl(long double x);
  97. floating_point tan (arithmetic x);
  98. float tanf(float x);
  99. long double tanl(long double x);
  100. floating_point tanh (arithmetic x);
  101. float tanhf(float x);
  102. long double tanhl(long double x);
  103. // C99
  104. bool signbit(arithmetic x);
  105. int fpclassify(arithmetic x);
  106. bool isfinite(arithmetic x);
  107. bool isinf(arithmetic x);
  108. bool isnan(arithmetic x);
  109. bool isnormal(arithmetic x);
  110. bool isgreater(arithmetic x, arithmetic y);
  111. bool isgreaterequal(arithmetic x, arithmetic y);
  112. bool isless(arithmetic x, arithmetic y);
  113. bool islessequal(arithmetic x, arithmetic y);
  114. bool islessgreater(arithmetic x, arithmetic y);
  115. bool isunordered(arithmetic x, arithmetic y);
  116. floating_point acosh (arithmetic x);
  117. float acoshf(float x);
  118. long double acoshl(long double x);
  119. floating_point asinh (arithmetic x);
  120. float asinhf(float x);
  121. long double asinhl(long double x);
  122. floating_point atanh (arithmetic x);
  123. float atanhf(float x);
  124. long double atanhl(long double x);
  125. floating_point cbrt (arithmetic x);
  126. float cbrtf(float x);
  127. long double cbrtl(long double x);
  128. floating_point copysign (arithmetic x, arithmetic y);
  129. float copysignf(float x, float y);
  130. long double copysignl(long double x, long double y);
  131. floating_point erf (arithmetic x);
  132. float erff(float x);
  133. long double erfl(long double x);
  134. floating_point erfc (arithmetic x);
  135. float erfcf(float x);
  136. long double erfcl(long double x);
  137. floating_point exp2 (arithmetic x);
  138. float exp2f(float x);
  139. long double exp2l(long double x);
  140. floating_point expm1 (arithmetic x);
  141. float expm1f(float x);
  142. long double expm1l(long double x);
  143. floating_point fdim (arithmetic x, arithmetic y);
  144. float fdimf(float x, float y);
  145. long double fdiml(long double x, long double y);
  146. floating_point fma (arithmetic x, arithmetic y, arithmetic z);
  147. float fmaf(float x, float y, float z);
  148. long double fmal(long double x, long double y, long double z);
  149. floating_point fmax (arithmetic x, arithmetic y);
  150. float fmaxf(float x, float y);
  151. long double fmaxl(long double x, long double y);
  152. floating_point fmin (arithmetic x, arithmetic y);
  153. float fminf(float x, float y);
  154. long double fminl(long double x, long double y);
  155. floating_point hypot (arithmetic x, arithmetic y);
  156. float hypotf(float x, float y);
  157. long double hypotl(long double x, long double y);
  158. int ilogb (arithmetic x);
  159. int ilogbf(float x);
  160. int ilogbl(long double x);
  161. floating_point lgamma (arithmetic x);
  162. float lgammaf(float x);
  163. long double lgammal(long double x);
  164. long long llrint (arithmetic x);
  165. long long llrintf(float x);
  166. long long llrintl(long double x);
  167. long long llround (arithmetic x);
  168. long long llroundf(float x);
  169. long long llroundl(long double x);
  170. floating_point log1p (arithmetic x);
  171. float log1pf(float x);
  172. long double log1pl(long double x);
  173. floating_point log2 (arithmetic x);
  174. float log2f(float x);
  175. long double log2l(long double x);
  176. floating_point logb (arithmetic x);
  177. float logbf(float x);
  178. long double logbl(long double x);
  179. long lrint (arithmetic x);
  180. long lrintf(float x);
  181. long lrintl(long double x);
  182. long lround (arithmetic x);
  183. long lroundf(float x);
  184. long lroundl(long double x);
  185. double nan (const char* str);
  186. float nanf(const char* str);
  187. long double nanl(const char* str);
  188. floating_point nearbyint (arithmetic x);
  189. float nearbyintf(float x);
  190. long double nearbyintl(long double x);
  191. floating_point nextafter (arithmetic x, arithmetic y);
  192. float nextafterf(float x, float y);
  193. long double nextafterl(long double x, long double y);
  194. floating_point nexttoward (arithmetic x, long double y);
  195. float nexttowardf(float x, long double y);
  196. long double nexttowardl(long double x, long double y);
  197. floating_point remainder (arithmetic x, arithmetic y);
  198. float remainderf(float x, float y);
  199. long double remainderl(long double x, long double y);
  200. floating_point remquo (arithmetic x, arithmetic y, int* pquo);
  201. float remquof(float x, float y, int* pquo);
  202. long double remquol(long double x, long double y, int* pquo);
  203. floating_point rint (arithmetic x);
  204. float rintf(float x);
  205. long double rintl(long double x);
  206. floating_point round (arithmetic x);
  207. float roundf(float x);
  208. long double roundl(long double x);
  209. floating_point scalbln (arithmetic x, long ex);
  210. float scalblnf(float x, long ex);
  211. long double scalblnl(long double x, long ex);
  212. floating_point scalbn (arithmetic x, int ex);
  213. float scalbnf(float x, int ex);
  214. long double scalbnl(long double x, int ex);
  215. floating_point tgamma (arithmetic x);
  216. float tgammaf(float x);
  217. long double tgammal(long double x);
  218. floating_point trunc (arithmetic x);
  219. float truncf(float x);
  220. long double truncl(long double x);
  221. */
  222. #ifdef __CUDACC__
  223. #include <math_cuda.h>
  224. #else
  225. #include <__config>
  226. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  227. # pragma GCC system_header
  228. #endif
  229. # if __has_include_next(<math.h>)
  230. # include_next <math.h>
  231. # endif
  232. #ifdef __cplusplus
  233. // We support including .h headers inside 'extern "C"' contexts, so switch
  234. // back to C++ linkage before including these C++ headers.
  235. extern "C++" {
  236. # ifdef fpclassify
  237. # undef fpclassify
  238. # endif
  239. # ifdef signbit
  240. # undef signbit
  241. # endif
  242. # ifdef isfinite
  243. # undef isfinite
  244. # endif
  245. # ifdef isinf
  246. # undef isinf
  247. # endif
  248. # if defined(isnan) && !defined(_LIBCPP_MSVCRT)
  249. # undef isnan
  250. # endif
  251. # ifdef isnormal
  252. # undef isnormal
  253. # endif
  254. # ifdef isgreater
  255. # undef isgreater
  256. # endif
  257. # ifdef isgreaterequal
  258. # undef isgreaterequal
  259. # endif
  260. # ifdef isless
  261. # undef isless
  262. # endif
  263. # ifdef islessequal
  264. # undef islessequal
  265. # endif
  266. # ifdef islessgreater
  267. # undef islessgreater
  268. # endif
  269. # ifdef isunordered
  270. # undef isunordered
  271. # endif
  272. #include <__math/abs.h>
  273. #include <__math/copysign.h>
  274. #include <__math/error_functions.h>
  275. #include <__math/exponential_functions.h>
  276. #include <__math/fdim.h>
  277. #include <__math/fma.h>
  278. #include <__math/gamma.h>
  279. #include <__math/hyperbolic_functions.h>
  280. #include <__math/hypot.h>
  281. #include <__math/inverse_hyperbolic_functions.h>
  282. #include <__math/inverse_trigonometric_functions.h>
  283. #include <__math/logarithms.h>
  284. #include <__math/min_max.h>
  285. #include <__math/modulo.h>
  286. #include <__math/remainder.h>
  287. #include <__math/roots.h>
  288. #include <__math/rounding_functions.h>
  289. #include <__math/traits.h>
  290. #include <__math/trigonometric_functions.h>
  291. #include <__type_traits/enable_if.h>
  292. #include <__type_traits/is_floating_point.h>
  293. #include <__type_traits/is_integral.h>
  294. #include <stdlib.h>
  295. // fpclassify relies on implementation-defined constants, so we can't move it to a detail header
  296. _LIBCPP_BEGIN_NAMESPACE_STD
  297. namespace __math {
  298. // fpclassify
  299. // template on non-double overloads to make them weaker than same overloads from MSVC runtime
  300. template <class = int>
  301. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(float __x) _NOEXCEPT {
  302. return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
  303. }
  304. template <class = int>
  305. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(double __x) _NOEXCEPT {
  306. return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
  307. }
  308. template <class = int>
  309. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(long double __x) _NOEXCEPT {
  310. return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
  311. }
  312. template <class _A1, std::__enable_if_t<std::is_integral<_A1>::value, int> = 0>
  313. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {
  314. return __x == 0 ? FP_ZERO : FP_NORMAL;
  315. }
  316. } // namespace __math
  317. _LIBCPP_END_NAMESPACE_STD
  318. using std::__math::fpclassify;
  319. using std::__math::signbit;
  320. // The MSVC runtime already provides these functions as templates
  321. #ifndef _LIBCPP_MSVCRT
  322. using std::__math::isfinite;
  323. using std::__math::isgreater;
  324. using std::__math::isgreaterequal;
  325. using std::__math::isinf;
  326. using std::__math::isless;
  327. using std::__math::islessequal;
  328. using std::__math::islessgreater;
  329. using std::__math::isnan;
  330. using std::__math::isnormal;
  331. using std::__math::isunordered;
  332. #endif // _LIBCPP_MSVCRT
  333. // abs
  334. //
  335. // handled in stdlib.h
  336. // div
  337. //
  338. // handled in stdlib.h
  339. // We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math
  340. // functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads
  341. // templates. Functions are preferred over function templates during overload resolution, which means that our overload
  342. // will only be selected when the C library doesn't provide one.
  343. using std::__math::acosh;
  344. using std::__math::acos;
  345. using std::__math::asinh;
  346. using std::__math::asin;
  347. using std::__math::atanh;
  348. using std::__math::atan;
  349. using std::__math::atan2;
  350. using std::__math::cbrt;
  351. using std::__math::ceil;
  352. using std::__math::copysign;
  353. using std::__math::cos;
  354. using std::__math::cosh;
  355. using std::__math::erf;
  356. using std::__math::erfc;
  357. using std::__math::exp;
  358. using std::__math::exp2;
  359. using std::__math::expm1;
  360. using std::__math::fabs;
  361. using std::__math::fdim;
  362. using std::__math::floor;
  363. using std::__math::fma;
  364. using std::__math::fmax;
  365. using std::__math::fmin;
  366. using std::__math::fmod;
  367. using std::__math::frexp;
  368. using std::__math::hypot;
  369. using std::__math::ilogb;
  370. using std::__math::ldexp;
  371. using std::__math::lgamma;
  372. using std::__math::llrint;
  373. using std::__math::llround;
  374. using std::__math::lrint;
  375. using std::__math::lround;
  376. using std::__math::log;
  377. using std::__math::log10;
  378. using std::__math::log1p;
  379. using std::__math::log2;
  380. using std::__math::logb;
  381. using std::__math::modf;
  382. using std::__math::nearbyint;
  383. using std::__math::nextafter;
  384. using std::__math::nexttoward;
  385. using std::__math::pow;
  386. using std::__math::remainder;
  387. using std::__math::remquo;
  388. using std::__math::rint;
  389. using std::__math::round;
  390. using std::__math::scalbln;
  391. using std::__math::scalbn;
  392. using std::__math::signbit;
  393. using std::__math::sin;
  394. using std::__math::sinh;
  395. using std::__math::sqrt;
  396. using std::__math::tan;
  397. using std::__math::tanh;
  398. using std::__math::tgamma;
  399. using std::__math::trunc;
  400. } // extern "C++"
  401. #endif // __cplusplus
  402. #endif // __CUDACC__
  403. #else // _LIBCPP_MATH_H
  404. // This include lives outside the header guard in order to support an MSVC
  405. // extension which allows users to do:
  406. //
  407. // #define _USE_MATH_DEFINES
  408. // #include <math.h>
  409. //
  410. // and receive the definitions of mathematical constants, even if <math.h>
  411. // has previously been included.
  412. #if defined(_LIBCPP_MSVCRT) && defined(_USE_MATH_DEFINES)
  413. #include_next <math.h>
  414. #endif
  415. #endif // _LIBCPP_MATH_H