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
16Macros:
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
37Types:
38
39 float_t // C99
40 double_t // C99
41
42// C90
43
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// C99
135
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# 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.
311extern "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
386namespace __math {
387
388// fpclassify
389
390// template on non-double overloads to make them weaker than same overloads from MSVC runtime
391template <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 __math
412
413_LIBCPP_END_NAMESPACE_STD
414
415using std::__math::fpclassify;
416using std::__math::signbit;
417
418// The MSVC runtime already provides these functions as templates
419# ifndef _LIBCPP_MSVCRT
420using 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_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
437using std::__math::abs;
438using std::__math::acos;
439using std::__math::acosh;
440using std::__math::asin;
441using std::__math::asinh;
442using std::__math::atan;
443using std::__math::atan2;
444using std::__math::atanh;
445using std::__math::cbrt;
446using std::__math::ceil;
447using std::__math::copysign;
448using std::__math::cos;
449using std::__math::cosh;
450using std::__math::erf;
451using std::__math::erfc;
452using std::__math::exp;
453using std::__math::exp2;
454using std::__math::expm1;
455using std::__math::fabs;
456using std::__math::fdim;
457using std::__math::floor;
458using std::__math::fma;
459using std::__math::fmax;
460using std::__math::fmin;
461using std::__math::fmod;
462using std::__math::frexp;
463using std::__math::hypot;
464using std::__math::ilogb;
465using std::__math::ldexp;
466using std::__math::lgamma;
467using std::__math::llrint;
468using std::__math::llround;
469using std::__math::log;
470using std::__math::log10;
471using std::__math::log1p;
472using std::__math::log2;
473using std::__math::logb;
474using std::__math::lrint;
475using std::__math::lround;
476using std::__math::modf;
477using std::__math::nearbyint;
478using std::__math::nextafter;
479using std::__math::nexttoward;
480using std::__math::pow;
481using std::__math::remainder;
482using std::__math::remquo;
483using std::__math::rint;
484using std::__math::round;
485using std::__math::scalbln;
486using std::__math::scalbn;
487using std::__math::signbit;
488using std::__math::sin;
489using std::__math::sinh;
490using std::__math::sqrt;
491using std::__math::tan;
492using std::__math::tanh;
493using std::__math::tgamma;
494using 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