535 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_MATH_H11# define _LIBCPP_MATH_H12 13/*14 math.h synopsis15 16Macros:17 18 HUGE_VAL19 HUGE_VALF // C9920 HUGE_VALL // C9921 INFINITY // C9922 NAN // C9923 FP_INFINITE // C9924 FP_NAN // C9925 FP_NORMAL // C9926 FP_SUBNORMAL // C9927 FP_ZERO // C9928 FP_FAST_FMA // C9929 FP_FAST_FMAF // C9930 FP_FAST_FMAL // C9931 FP_ILOGB0 // C9932 FP_ILOGBNAN // C9933 MATH_ERRNO // C9934 MATH_ERREXCEPT // C9935 math_errhandling // C9936 37Types:38 39 float_t // C9940 double_t // C9941 42// C9043 44floating_point abs(floating_point x);45 46floating_point acos (arithmetic x);47float acosf(float x);48long double acosl(long double x);49 50floating_point asin (arithmetic x);51float asinf(float x);52long double asinl(long double x);53 54floating_point atan (arithmetic x);55float atanf(float x);56long double atanl(long double x);57 58floating_point atan2 (arithmetic y, arithmetic x);59float atan2f(float y, float x);60long double atan2l(long double y, long double x);61 62floating_point ceil (arithmetic x);63float ceilf(float x);64long double ceill(long double x);65 66floating_point cos (arithmetic x);67float cosf(float x);68long double cosl(long double x);69 70floating_point cosh (arithmetic x);71float coshf(float x);72long double coshl(long double x);73 74floating_point exp (arithmetic x);75float expf(float x);76long double expl(long double x);77 78floating_point fabs (arithmetic x);79float fabsf(float x);80long double fabsl(long double x);81 82floating_point floor (arithmetic x);83float floorf(float x);84long double floorl(long double x);85 86floating_point fmod (arithmetic x, arithmetic y);87float fmodf(float x, float y);88long double fmodl(long double x, long double y);89 90floating_point frexp (arithmetic value, int* exp);91float frexpf(float value, int* exp);92long double frexpl(long double value, int* exp);93 94floating_point ldexp (arithmetic value, int exp);95float ldexpf(float value, int exp);96long double ldexpl(long double value, int exp);97 98floating_point log (arithmetic x);99float logf(float x);100long double logl(long double x);101 102floating_point log10 (arithmetic x);103float log10f(float x);104long double log10l(long double x);105 106floating_point modf (floating_point value, floating_point* iptr);107float modff(float value, float* iptr);108long double modfl(long double value, long double* iptr);109 110floating_point pow (arithmetic x, arithmetic y);111float powf(float x, float y);112long double powl(long double x, long double y);113 114floating_point sin (arithmetic x);115float sinf(float x);116long double sinl(long double x);117 118floating_point sinh (arithmetic x);119float sinhf(float x);120long double sinhl(long double x);121 122floating_point sqrt (arithmetic x);123float sqrtf(float x);124long double sqrtl(long double x);125 126floating_point tan (arithmetic x);127float tanf(float x);128long double tanl(long double x);129 130floating_point tanh (arithmetic x);131float tanhf(float x);132long double tanhl(long double x);133 134// C99135 136bool signbit(arithmetic x);137 138int fpclassify(arithmetic x);139 140bool isfinite(arithmetic x);141bool isinf(arithmetic x);142bool isnan(arithmetic x);143bool isnormal(arithmetic x);144 145bool isgreater(arithmetic x, arithmetic y);146bool isgreaterequal(arithmetic x, arithmetic y);147bool isless(arithmetic x, arithmetic y);148bool islessequal(arithmetic x, arithmetic y);149bool islessgreater(arithmetic x, arithmetic y);150bool isunordered(arithmetic x, arithmetic y);151 152floating_point acosh (arithmetic x);153float acoshf(float x);154long double acoshl(long double x);155 156floating_point asinh (arithmetic x);157float asinhf(float x);158long double asinhl(long double x);159 160floating_point atanh (arithmetic x);161float atanhf(float x);162long double atanhl(long double x);163 164floating_point cbrt (arithmetic x);165float cbrtf(float x);166long double cbrtl(long double x);167 168floating_point copysign (arithmetic x, arithmetic y);169float copysignf(float x, float y);170long double copysignl(long double x, long double y);171 172floating_point erf (arithmetic x);173float erff(float x);174long double erfl(long double x);175 176floating_point erfc (arithmetic x);177float erfcf(float x);178long double erfcl(long double x);179 180floating_point exp2 (arithmetic x);181float exp2f(float x);182long double exp2l(long double x);183 184floating_point expm1 (arithmetic x);185float expm1f(float x);186long double expm1l(long double x);187 188floating_point fdim (arithmetic x, arithmetic y);189float fdimf(float x, float y);190long double fdiml(long double x, long double y);191 192floating_point fma (arithmetic x, arithmetic y, arithmetic z);193float fmaf(float x, float y, float z);194long double fmal(long double x, long double y, long double z);195 196floating_point fmax (arithmetic x, arithmetic y);197float fmaxf(float x, float y);198long double fmaxl(long double x, long double y);199 200floating_point fmin (arithmetic x, arithmetic y);201float fminf(float x, float y);202long double fminl(long double x, long double y);203 204floating_point hypot (arithmetic x, arithmetic y);205float hypotf(float x, float y);206long double hypotl(long double x, long double y);207 208int ilogb (arithmetic x);209int ilogbf(float x);210int ilogbl(long double x);211 212floating_point lgamma (arithmetic x);213float lgammaf(float x);214long double lgammal(long double x);215 216long long llrint (arithmetic x);217long long llrintf(float x);218long long llrintl(long double x);219 220long long llround (arithmetic x);221long long llroundf(float x);222long long llroundl(long double x);223 224floating_point log1p (arithmetic x);225float log1pf(float x);226long double log1pl(long double x);227 228floating_point log2 (arithmetic x);229float log2f(float x);230long double log2l(long double x);231 232floating_point logb (arithmetic x);233float logbf(float x);234long double logbl(long double x);235 236long lrint (arithmetic x);237long lrintf(float x);238long lrintl(long double x);239 240long lround (arithmetic x);241long lroundf(float x);242long lroundl(long double x);243 244double nan (const char* str);245float nanf(const char* str);246long double nanl(const char* str);247 248floating_point nearbyint (arithmetic x);249float nearbyintf(float x);250long double nearbyintl(long double x);251 252floating_point nextafter (arithmetic x, arithmetic y);253float nextafterf(float x, float y);254long double nextafterl(long double x, long double y);255 256floating_point nexttoward (arithmetic x, long double y);257float nexttowardf(float x, long double y);258long double nexttowardl(long double x, long double y);259 260floating_point remainder (arithmetic x, arithmetic y);261float remainderf(float x, float y);262long double remainderl(long double x, long double y);263 264floating_point remquo (arithmetic x, arithmetic y, int* pquo);265float remquof(float x, float y, int* pquo);266long double remquol(long double x, long double y, int* pquo);267 268floating_point rint (arithmetic x);269float rintf(float x);270long double rintl(long double x);271 272floating_point round (arithmetic x);273float roundf(float x);274long double roundl(long double x);275 276floating_point scalbln (arithmetic x, long ex);277float scalblnf(float x, long ex);278long double scalblnl(long double x, long ex);279 280floating_point scalbn (arithmetic x, int ex);281float scalbnf(float x, int ex);282long double scalbnl(long double x, int ex);283 284floating_point tgamma (arithmetic x);285float tgammaf(float x);286long double tgammal(long double x);287 288floating_point trunc (arithmetic x);289float truncf(float x);290long 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# else297# include <__config>298 299# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)300# pragma GCC system_header301# endif302 303# if __has_include_next(<math.h>)304# include_next <math.h>305# endif306 307# ifdef __cplusplus308 309// We support including .h headers inside 'extern "C"' contexts, so switch310// back to C++ linkage before including these C++ headers.311extern "C++" {312 313# ifdef fpclassify314# undef fpclassify315# endif316 317# ifdef signbit318# undef signbit319# endif320 321# ifdef isfinite322# undef isfinite323# endif324 325# ifdef isinf326# undef isinf327# endif328 329# ifdef isnan330# undef isnan331# endif332 333# ifdef isnormal334# undef isnormal335# endif336 337# ifdef isgreater338# undef isgreater339# endif340 341# ifdef isgreaterequal342# undef isgreaterequal343# endif344 345# ifdef isless346# undef isless347# endif348 349# ifdef islessequal350# undef islessequal351# endif352 353# ifdef islessgreater354# undef islessgreater355# endif356 357# ifdef isunordered358# undef isunordered359# endif360 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 header384_LIBCPP_BEGIN_NAMESPACE_STD385 386namespace __math {387 388// fpclassify389 390// template on non-double overloads to make them weaker than same overloads from MSVC runtime391template <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 396template <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 401template <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 406template <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 __math412 413_LIBCPP_END_NAMESPACE_STD414 415using std::__math::fpclassify;416using std::__math::signbit;417 418// The MSVC runtime already provides these functions as templates419# ifndef _LIBCPP_MSVCRT420using std::__math::isfinite;421using std::__math::isgreater;422using std::__math::isgreaterequal;423using std::__math::isinf;424using std::__math::isless;425using std::__math::islessequal;426using std::__math::islessgreater;427using std::__math::isnan;428using std::__math::isnormal;429using std::__math::isunordered;430# endif // _LIBCPP_MSVCRT431 432# if defined(_LIBCPP_MSVCRT) && _LIBCPP_STD_VER >= 20433// MS UCRT incorrectly defines some functions in a way not working with integer types. Until C++20, this was worked434// around by -fdelayed-template-parsing. Since C++20, we can use standard feature "requires" instead.435 436// TODO: Remove the workaround once UCRT fixes these functions. Note that this doesn't seem planned as of 2025-07 per437// https://developercommunity.visualstudio.com/t/10294165.438 439using std::__math::__ucrt::isfinite;440using std::__math::__ucrt::isgreater;441using std::__math::__ucrt::isgreaterequal;442using std::__math::__ucrt::isinf;443using std::__math::__ucrt::isless;444using std::__math::__ucrt::islessequal;445using std::__math::__ucrt::islessgreater;446using std::__math::__ucrt::isnan;447using std::__math::__ucrt::isnormal;448using std::__math::__ucrt::isunordered;449# endif // defined(_LIBCPP_MSVCRT) && _LIBCPP_STD_VER >= 20450 451// We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math452// functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads453// templates. Functions are preferred over function templates during overload resolution, which means that our overload454// will only be selected when the C library doesn't provide one.455 456using std::__math::abs;457using std::__math::acos;458using std::__math::acosh;459using std::__math::asin;460using std::__math::asinh;461using std::__math::atan;462using std::__math::atan2;463using std::__math::atanh;464using std::__math::cbrt;465using std::__math::ceil;466using std::__math::copysign;467using std::__math::cos;468using std::__math::cosh;469using std::__math::erf;470using std::__math::erfc;471using std::__math::exp;472using std::__math::exp2;473using std::__math::expm1;474using std::__math::fabs;475using std::__math::fdim;476using std::__math::floor;477using std::__math::fma;478using std::__math::fmax;479using std::__math::fmin;480using std::__math::fmod;481using std::__math::frexp;482using std::__math::hypot;483using std::__math::ilogb;484using std::__math::ldexp;485using std::__math::lgamma;486using std::__math::llrint;487using std::__math::llround;488using std::__math::log;489using std::__math::log10;490using std::__math::log1p;491using std::__math::log2;492using std::__math::logb;493using std::__math::lrint;494using std::__math::lround;495using std::__math::modf;496using std::__math::nearbyint;497using std::__math::nextafter;498using std::__math::nexttoward;499using std::__math::pow;500using std::__math::remainder;501using std::__math::remquo;502using std::__math::rint;503using std::__math::round;504using std::__math::scalbln;505using std::__math::scalbn;506using std::__math::signbit;507using std::__math::sin;508using std::__math::sinh;509using std::__math::sqrt;510using std::__math::tan;511using std::__math::tanh;512using std::__math::tgamma;513using std::__math::trunc;514 515} // extern "C++"516 517# endif // __cplusplus518# endif // defined(__cplusplus) && __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)519 520#else // _LIBCPP_MATH_H521 522// This include lives outside the header guard in order to support an MSVC523// extension which allows users to do:524//525// #define _USE_MATH_DEFINES526// #include <math.h>527//528// and receive the definitions of mathematical constants, even if <math.h>529// has previously been included.530# if defined(_LIBCPP_MSVCRT) && defined(_USE_MATH_DEFINES)531# include_next <math.h>532# endif533 534#endif // _LIBCPP_MATH_H535