521 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___CXX03_MATH_H11# define _LIBCPP___CXX03_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# include <__cxx03/__config>295 296# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)297# pragma GCC system_header298# endif299 300# if __has_include_next(<math.h>)301# include_next <math.h>302# endif303 304# ifdef __cplusplus305 306// We support including .h headers inside 'extern "C"' contexts, so switch307// back to C++ linkage before including these C++ headers.308extern "C++" {309 310# ifdef fpclassify311# undef fpclassify312# endif313 314# ifdef signbit315# undef signbit316# endif317 318# ifdef isfinite319# undef isfinite320# endif321 322# ifdef isinf323# undef isinf324# endif325 326# ifdef isnan327# undef isnan328# endif329 330# ifdef isnormal331# undef isnormal332# endif333 334# ifdef isgreater335# undef isgreater336# endif337 338# ifdef isgreaterequal339# undef isgreaterequal340# endif341 342# ifdef isless343# undef isless344# endif345 346# ifdef islessequal347# undef islessequal348# endif349 350# ifdef islessgreater351# undef islessgreater352# endif353 354# ifdef isunordered355# undef isunordered356# endif357 358# include <__cxx03/__math/abs.h>359# include <__cxx03/__math/copysign.h>360# include <__cxx03/__math/error_functions.h>361# include <__cxx03/__math/exponential_functions.h>362# include <__cxx03/__math/fdim.h>363# include <__cxx03/__math/fma.h>364# include <__cxx03/__math/gamma.h>365# include <__cxx03/__math/hyperbolic_functions.h>366# include <__cxx03/__math/hypot.h>367# include <__cxx03/__math/inverse_hyperbolic_functions.h>368# include <__cxx03/__math/inverse_trigonometric_functions.h>369# include <__cxx03/__math/logarithms.h>370# include <__cxx03/__math/min_max.h>371# include <__cxx03/__math/modulo.h>372# include <__cxx03/__math/remainder.h>373# include <__cxx03/__math/roots.h>374# include <__cxx03/__math/rounding_functions.h>375# include <__cxx03/__math/traits.h>376# include <__cxx03/__math/trigonometric_functions.h>377# include <__cxx03/__type_traits/enable_if.h>378# include <__cxx03/__type_traits/is_floating_point.h>379# include <__cxx03/__type_traits/is_integral.h>380# include <__cxx03/stdlib.h>381 382// fpclassify relies on implementation-defined constants, so we can't move it to a detail header383_LIBCPP_BEGIN_NAMESPACE_STD384 385namespace __math {386 387// fpclassify388 389// template on non-double overloads to make them weaker than same overloads from MSVC runtime390template <class = int>391_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int fpclassify(float __x) _NOEXCEPT {392 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);393}394 395template <class = int>396_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int fpclassify(double __x) _NOEXCEPT {397 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);398}399 400template <class = int>401_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int fpclassify(long double __x) _NOEXCEPT {402 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);403}404 405template <class _A1, std::__enable_if_t<std::is_integral<_A1>::value, int> = 0>406_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {407 return __x == 0 ? FP_ZERO : FP_NORMAL;408}409 410} // namespace __math411 412_LIBCPP_END_NAMESPACE_STD413 414using std::__math::fpclassify;415using std::__math::signbit;416 417// The MSVC runtime already provides these functions as templates418# ifndef _LIBCPP_MSVCRT419using std::__math::isfinite;420using std::__math::isgreater;421using std::__math::isgreaterequal;422using std::__math::isinf;423using std::__math::isless;424using std::__math::islessequal;425using std::__math::islessgreater;426using std::__math::isnan;427using std::__math::isnormal;428using std::__math::isunordered;429# endif // _LIBCPP_MSVCRT430 431// abs432//433// handled in stdlib.h434 435// div436//437// handled in stdlib.h438 439// We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math440// functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads441// templates. Functions are preferred over function templates during overload resolution, which means that our overload442// will only be selected when the C library doesn't provide one.443 444using std::__math::acos;445using std::__math::acosh;446using std::__math::asin;447using std::__math::asinh;448using std::__math::atan;449using std::__math::atan2;450using std::__math::atanh;451using std::__math::cbrt;452using std::__math::ceil;453using std::__math::copysign;454using std::__math::cos;455using std::__math::cosh;456using std::__math::erf;457using std::__math::erfc;458using std::__math::exp;459using std::__math::exp2;460using std::__math::expm1;461using std::__math::fabs;462using std::__math::fdim;463using std::__math::floor;464using std::__math::fma;465using std::__math::fmax;466using std::__math::fmin;467using std::__math::fmod;468using std::__math::frexp;469using std::__math::hypot;470using std::__math::ilogb;471using std::__math::ldexp;472using std::__math::lgamma;473using std::__math::llrint;474using std::__math::llround;475using std::__math::log;476using std::__math::log10;477using std::__math::log1p;478using std::__math::log2;479using std::__math::logb;480using std::__math::lrint;481using std::__math::lround;482using std::__math::modf;483using std::__math::nearbyint;484using std::__math::nextafter;485using std::__math::nexttoward;486using std::__math::pow;487using std::__math::remainder;488using std::__math::remquo;489using std::__math::rint;490using std::__math::round;491using std::__math::scalbln;492using std::__math::scalbn;493using std::__math::signbit;494using std::__math::sin;495using std::__math::sinh;496using std::__math::sqrt;497using std::__math::tan;498using std::__math::tanh;499using std::__math::tgamma;500using std::__math::trunc;501 502} // extern "C++"503 504# endif // __cplusplus505 506#else // _LIBCPP___CXX03_MATH_H507 508// This include lives outside the header guard in order to support an MSVC509// extension which allows users to do:510//511// #define _USE_MATH_DEFINES512// #include <__cxx03/math.h>513//514// and receive the definitions of mathematical constants, even if <math.h>515// has previously been included.516# if defined(_LIBCPP_MSVCRT) && defined(_USE_MATH_DEFINES)517# include_next <math.h>518# endif519 520#endif // _LIBCPP___CXX03_MATH_H521