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