[libc] Fix improper initialization of `StorageType` (#75610)
[llvm-project.git] / libcxx / include / math.h
blobb068ea388f095b5b02a263bfca08d20504de0edd
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_MATH_H
11 #define _LIBCPP_MATH_H
14 math.h synopsis
16 Macros:
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
37 Types:
39 float_t // C99
40 double_t // C99
42 // C90
44 floating_point abs(floating_point x);
46 floating_point acos (arithmetic x);
47 float acosf(float x);
48 long double acosl(long double x);
50 floating_point asin (arithmetic x);
51 float asinf(float x);
52 long double asinl(long double x);
54 floating_point atan (arithmetic x);
55 float atanf(float x);
56 long double atanl(long double x);
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);
62 floating_point ceil (arithmetic x);
63 float ceilf(float x);
64 long double ceill(long double x);
66 floating_point cos (arithmetic x);
67 float cosf(float x);
68 long double cosl(long double x);
70 floating_point cosh (arithmetic x);
71 float coshf(float x);
72 long double coshl(long double x);
74 floating_point exp (arithmetic x);
75 float expf(float x);
76 long double expl(long double x);
78 floating_point fabs (arithmetic x);
79 float fabsf(float x);
80 long double fabsl(long double x);
82 floating_point floor (arithmetic x);
83 float floorf(float x);
84 long double floorl(long double x);
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);
90 floating_point frexp (arithmetic value, int* exp);
91 float frexpf(float value, int* exp);
92 long double frexpl(long double value, int* exp);
94 floating_point ldexp (arithmetic value, int exp);
95 float ldexpf(float value, int exp);
96 long double ldexpl(long double value, int exp);
98 floating_point log (arithmetic x);
99 float logf(float x);
100 long double logl(long double x);
102 floating_point log10 (arithmetic x);
103 float log10f(float x);
104 long double log10l(long double x);
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);
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);
114 floating_point sin (arithmetic x);
115 float sinf(float x);
116 long double sinl(long double x);
118 floating_point sinh (arithmetic x);
119 float sinhf(float x);
120 long double sinhl(long double x);
122 floating_point sqrt (arithmetic x);
123 float sqrtf(float x);
124 long double sqrtl(long double x);
126 floating_point tan (arithmetic x);
127 float tanf(float x);
128 long double tanl(long double x);
130 floating_point tanh (arithmetic x);
131 float tanhf(float x);
132 long double tanhl(long double x);
134 // C99
136 bool signbit(arithmetic x);
138 int fpclassify(arithmetic x);
140 bool isfinite(arithmetic x);
141 bool isinf(arithmetic x);
142 bool isnan(arithmetic x);
143 bool isnormal(arithmetic x);
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);
152 floating_point acosh (arithmetic x);
153 float acoshf(float x);
154 long double acoshl(long double x);
156 floating_point asinh (arithmetic x);
157 float asinhf(float x);
158 long double asinhl(long double x);
160 floating_point atanh (arithmetic x);
161 float atanhf(float x);
162 long double atanhl(long double x);
164 floating_point cbrt (arithmetic x);
165 float cbrtf(float x);
166 long double cbrtl(long double x);
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);
172 floating_point erf (arithmetic x);
173 float erff(float x);
174 long double erfl(long double x);
176 floating_point erfc (arithmetic x);
177 float erfcf(float x);
178 long double erfcl(long double x);
180 floating_point exp2 (arithmetic x);
181 float exp2f(float x);
182 long double exp2l(long double x);
184 floating_point expm1 (arithmetic x);
185 float expm1f(float x);
186 long double expm1l(long double x);
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);
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);
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);
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);
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);
208 int ilogb (arithmetic x);
209 int ilogbf(float x);
210 int ilogbl(long double x);
212 floating_point lgamma (arithmetic x);
213 float lgammaf(float x);
214 long double lgammal(long double x);
216 long long llrint (arithmetic x);
217 long long llrintf(float x);
218 long long llrintl(long double x);
220 long long llround (arithmetic x);
221 long long llroundf(float x);
222 long long llroundl(long double x);
224 floating_point log1p (arithmetic x);
225 float log1pf(float x);
226 long double log1pl(long double x);
228 floating_point log2 (arithmetic x);
229 float log2f(float x);
230 long double log2l(long double x);
232 floating_point logb (arithmetic x);
233 float logbf(float x);
234 long double logbl(long double x);
236 long lrint (arithmetic x);
237 long lrintf(float x);
238 long lrintl(long double x);
240 long lround (arithmetic x);
241 long lroundf(float x);
242 long lroundl(long double x);
244 double nan (const char* str);
245 float nanf(const char* str);
246 long double nanl(const char* str);
248 floating_point nearbyint (arithmetic x);
249 float nearbyintf(float x);
250 long double nearbyintl(long double x);
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);
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);
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);
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);
268 floating_point rint (arithmetic x);
269 float rintf(float x);
270 long double rintl(long double x);
272 floating_point round (arithmetic x);
273 float roundf(float x);
274 long double roundl(long double x);
276 floating_point scalbln (arithmetic x, long ex);
277 float scalblnf(float x, long ex);
278 long double scalblnl(long double x, long ex);
280 floating_point scalbn (arithmetic x, int ex);
281 float scalbnf(float x, int ex);
282 long double scalbnl(long double x, int ex);
284 floating_point tgamma (arithmetic x);
285 float tgammaf(float x);
286 long double tgammal(long double x);
288 floating_point trunc (arithmetic x);
289 float truncf(float x);
290 long double truncl(long double x);
294 #include <__config>
296 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
297 # pragma GCC system_header
298 #endif
300 # if __has_include_next(<math.h>)
301 # include_next <math.h>
302 # endif
304 #ifdef __cplusplus
306 // We support including .h headers inside 'extern "C"' contexts, so switch
307 // back to C++ linkage before including these C++ headers.
308 extern "C++" {
310 # ifdef fpclassify
311 # undef fpclassify
312 # endif
314 # ifdef signbit
315 # undef signbit
316 # endif
318 # ifdef isfinite
319 # undef isfinite
320 # endif
322 # ifdef isinf
323 # undef isinf
324 # endif
326 # ifdef isnan
327 # undef isnan
328 # endif
330 # ifdef isnormal
331 # undef isnormal
332 # endif
334 # ifdef isgreater
335 # undef isgreater
336 # endif
338 # ifdef isgreaterequal
339 # undef isgreaterequal
340 # endif
342 # ifdef isless
343 # undef isless
344 # endif
346 # ifdef islessequal
347 # undef islessequal
348 # endif
350 # ifdef islessgreater
351 # undef islessgreater
352 # endif
354 # ifdef isunordered
355 # undef isunordered
356 # endif
358 #include <__math/abs.h>
359 #include <__math/copysign.h>
360 #include <__math/error_functions.h>
361 #include <__math/exponential_functions.h>
362 #include <__math/fdim.h>
363 #include <__math/fma.h>
364 #include <__math/gamma.h>
365 #include <__math/hyperbolic_functions.h>
366 #include <__math/hypot.h>
367 #include <__math/inverse_hyperbolic_functions.h>
368 #include <__math/inverse_trigonometric_functions.h>
369 #include <__math/logarithms.h>
370 #include <__math/min_max.h>
371 #include <__math/modulo.h>
372 #include <__math/remainder.h>
373 #include <__math/roots.h>
374 #include <__math/rounding_functions.h>
375 #include <__math/traits.h>
376 #include <__math/trigonometric_functions.h>
377 #include <__type_traits/enable_if.h>
378 #include <__type_traits/is_floating_point.h>
379 #include <__type_traits/is_integral.h>
380 #include <stdlib.h>
383 // fpclassify relies on implementation-defined constants, so we can't move it to a detail header
384 _LIBCPP_BEGIN_NAMESPACE_STD
386 namespace __math {
388 // fpclassify
390 // template on non-double overloads to make them weaker than same overloads from MSVC runtime
391 template <class = int>
392 _LIBCPP_NODISCARD_EXT 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);
396 template <class = int>
397 _LIBCPP_NODISCARD_EXT 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);
401 template <class = int>
402 _LIBCPP_NODISCARD_EXT 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);
406 template <class _A1, std::__enable_if_t<std::is_integral<_A1>::value, int> = 0>
407 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {
408 return __x == 0 ? FP_ZERO : FP_NORMAL;
411 } // namespace __math
413 _LIBCPP_END_NAMESPACE_STD
415 using std::__math::fpclassify;
416 using std::__math::signbit;
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
432 // abs
434 // handled in stdlib.h
436 // div
438 // handled in stdlib.h
440 // We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math
441 // functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads
442 // templates. Functions are preferred over function templates during overload resolution, which means that our overload
443 // will only be selected when the C library doesn't provide one.
445 using std::__math::acosh;
446 using std::__math::acos;
447 using std::__math::asinh;
448 using std::__math::asin;
449 using std::__math::atanh;
450 using std::__math::atan;
451 using std::__math::atan2;
452 using std::__math::cbrt;
453 using std::__math::ceil;
454 using std::__math::copysign;
455 using std::__math::cos;
456 using std::__math::cosh;
457 using std::__math::erf;
458 using std::__math::erfc;
459 using std::__math::exp;
460 using std::__math::exp2;
461 using std::__math::expm1;
462 using std::__math::fabs;
463 using std::__math::fdim;
464 using std::__math::floor;
465 using std::__math::fma;
466 using std::__math::fmax;
467 using std::__math::fmin;
468 using std::__math::fmod;
469 using std::__math::frexp;
470 using std::__math::hypot;
471 using std::__math::ilogb;
472 using std::__math::ldexp;
473 using std::__math::lgamma;
474 using std::__math::llrint;
475 using std::__math::llround;
476 using std::__math::lrint;
477 using std::__math::lround;
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::modf;
484 using std::__math::nearbyint;
485 using std::__math::nextafter;
486 using std::__math::nexttoward;
487 using std::__math::pow;
488 using std::__math::remainder;
489 using std::__math::remquo;
490 using std::__math::rint;
491 using std::__math::round;
492 using std::__math::scalbln;
493 using std::__math::scalbn;
494 using std::__math::signbit;
495 using std::__math::sin;
496 using std::__math::sinh;
497 using std::__math::sqrt;
498 using std::__math::tan;
499 using std::__math::tanh;
500 using std::__math::tgamma;
501 using std::__math::trunc;
503 } // extern "C++"
505 #endif // __cplusplus
507 #else // _LIBCPP_MATH_H
509 // This include lives outside the header guard in order to support an MSVC
510 // extension which allows users to do:
512 // #define _USE_MATH_DEFINES
513 // #include <math.h>
515 // and receive the definitions of mathematical constants, even if <math.h>
516 // has previously been included.
517 #if defined(_LIBCPP_MSVCRT) && defined(_USE_MATH_DEFINES)
518 #include_next <math.h>
519 #endif
521 #endif // _LIBCPP_MATH_H