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_floating_point.h> 382 # include <__type_traits/is_integral.h> 383 # include <stdlib.h> 384 385 // fpclassify relies on implementation-defined constants, so we can't move it to a detail header 386 _LIBCPP_BEGIN_NAMESPACE_STD 387 388 namespace __math { 389 390 // fpclassify 391 392 // template on non-double overloads to make them weaker than same overloads from MSVC runtime 393 template <class = int> 394 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI int fpclassify(float __x) _NOEXCEPT { 395 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x); 396 } 397 398 template <class = int> 399 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI int fpclassify(double __x) _NOEXCEPT { 400 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x); 401 } 402 403 template <class = int> 404 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI int fpclassify(long double __x) _NOEXCEPT { 405 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x); 406 } 407 408 template <class _A1, std::__enable_if_t<std::is_integral<_A1>::value, int> = 0> 409 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT { 410 return __x == 0 ? FP_ZERO : FP_NORMAL; 411 } 412 413 } // namespace __math 414 415 _LIBCPP_END_NAMESPACE_STD 416 417 using std::__math::fpclassify; 418 using std::__math::signbit; 419 420 // The MSVC runtime already provides these functions as templates 421 # ifndef _LIBCPP_MSVCRT 422 using std::__math::isfinite; 423 using std::__math::isgreater; 424 using std::__math::isgreaterequal; 425 using std::__math::isinf; 426 using std::__math::isless; 427 using std::__math::islessequal; 428 using std::__math::islessgreater; 429 using std::__math::isnan; 430 using std::__math::isnormal; 431 using std::__math::isunordered; 432 # endif // _LIBCPP_MSVCRT 433 434 // abs 435 // 436 // handled in stdlib.h 437 438 // div 439 // 440 // handled in stdlib.h 441 442 // We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math 443 // functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads 444 // templates. Functions are preferred over function templates during overload resolution, which means that our overload 445 // will only be selected when the C library doesn't provide one. 446 447 using std::__math::acos; 448 using std::__math::acosh; 449 using std::__math::asin; 450 using std::__math::asinh; 451 using std::__math::atan; 452 using std::__math::atan2; 453 using std::__math::atanh; 454 using std::__math::cbrt; 455 using std::__math::ceil; 456 using std::__math::copysign; 457 using std::__math::cos; 458 using std::__math::cosh; 459 using std::__math::erf; 460 using std::__math::erfc; 461 using std::__math::exp; 462 using std::__math::exp2; 463 using std::__math::expm1; 464 using std::__math::fabs; 465 using std::__math::fdim; 466 using std::__math::floor; 467 using std::__math::fma; 468 using std::__math::fmax; 469 using std::__math::fmin; 470 using std::__math::fmod; 471 using std::__math::frexp; 472 using std::__math::hypot; 473 using std::__math::ilogb; 474 using std::__math::ldexp; 475 using std::__math::lgamma; 476 using std::__math::llrint; 477 using std::__math::llround; 478 using std::__math::log; 479 using std::__math::log10; 480 using std::__math::log1p; 481 using std::__math::log2; 482 using std::__math::logb; 483 using std::__math::lrint; 484 using std::__math::lround; 485 using std::__math::modf; 486 using std::__math::nearbyint; 487 using std::__math::nextafter; 488 using std::__math::nexttoward; 489 using std::__math::pow; 490 using std::__math::remainder; 491 using std::__math::remquo; 492 using std::__math::rint; 493 using std::__math::round; 494 using std::__math::scalbln; 495 using std::__math::scalbn; 496 using std::__math::signbit; 497 using std::__math::sin; 498 using std::__math::sinh; 499 using std::__math::sqrt; 500 using std::__math::tan; 501 using std::__math::tanh; 502 using std::__math::tgamma; 503 using std::__math::trunc; 504 505 } // extern "C++" 506 507 # endif // __cplusplus 508 # endif // defined(__cplusplus) && __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) 509 510 #else // _LIBCPP_MATH_H 511 512 // This include lives outside the header guard in order to support an MSVC 513 // extension which allows users to do: 514 // 515 // #define _USE_MATH_DEFINES 516 // #include <math.h> 517 // 518 // and receive the definitions of mathematical constants, even if <math.h> 519 // has previously been included. 520 # if defined(_LIBCPP_MSVCRT) && defined(_USE_MATH_DEFINES) 521 # include_next <math.h> 522 # endif 523 524 #endif // _LIBCPP_MATH_H 525