xref: /freebsd/contrib/llvm-project/libcxx/include/math.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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 #  include <__config>
295 
296 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
297 #    pragma GCC system_header
298 #  endif
299 
300 #  if __has_include_next(<math.h>)
301 #    include_next <math.h>
302 #  endif
303 
304 #  ifdef __cplusplus
305 
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++" {
309 
310 #    ifdef fpclassify
311 #      undef fpclassify
312 #    endif
313 
314 #    ifdef signbit
315 #      undef signbit
316 #    endif
317 
318 #    ifdef isfinite
319 #      undef isfinite
320 #    endif
321 
322 #    ifdef isinf
323 #      undef isinf
324 #    endif
325 
326 #    ifdef isnan
327 #      undef isnan
328 #    endif
329 
330 #    ifdef isnormal
331 #      undef isnormal
332 #    endif
333 
334 #    ifdef isgreater
335 #      undef isgreater
336 #    endif
337 
338 #    ifdef isgreaterequal
339 #      undef isgreaterequal
340 #    endif
341 
342 #    ifdef isless
343 #      undef isless
344 #    endif
345 
346 #    ifdef islessequal
347 #      undef islessequal
348 #    endif
349 
350 #    ifdef islessgreater
351 #      undef islessgreater
352 #    endif
353 
354 #    ifdef isunordered
355 #      undef isunordered
356 #    endif
357 
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>
381 
382 // fpclassify relies on implementation-defined constants, so we can't move it to a detail header
383 _LIBCPP_BEGIN_NAMESPACE_STD
384 
385 namespace __math {
386 
387 // fpclassify
388 
389 // template on non-double overloads to make them weaker than same overloads from MSVC runtime
390 template <class = int>
391 _LIBCPP_NODISCARD_EXT 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 
395 template <class = int>
396 _LIBCPP_NODISCARD_EXT 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 
400 template <class = int>
401 _LIBCPP_NODISCARD_EXT 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 
405 template <class _A1, std::__enable_if_t<std::is_integral<_A1>::value, int> = 0>
406 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {
407   return __x == 0 ? FP_ZERO : FP_NORMAL;
408 }
409 
410 } // namespace __math
411 
412 _LIBCPP_END_NAMESPACE_STD
413 
414 using std::__math::fpclassify;
415 using std::__math::signbit;
416 
417 // The MSVC runtime already provides these functions as templates
418 #    ifndef _LIBCPP_MSVCRT
419 using std::__math::isfinite;
420 using std::__math::isgreater;
421 using std::__math::isgreaterequal;
422 using std::__math::isinf;
423 using std::__math::isless;
424 using std::__math::islessequal;
425 using std::__math::islessgreater;
426 using std::__math::isnan;
427 using std::__math::isnormal;
428 using std::__math::isunordered;
429 #    endif // _LIBCPP_MSVCRT
430 
431 // abs
432 //
433 // handled in stdlib.h
434 
435 // div
436 //
437 // handled in stdlib.h
438 
439 // We have to provide double overloads for <math.h> to work on platforms that don't provide the full set of math
440 // functions. To make the overload set work with multiple functions that take the same arguments, we make our overloads
441 // templates. Functions are preferred over function templates during overload resolution, which means that our overload
442 // will only be selected when the C library doesn't provide one.
443 
444 using std::__math::acos;
445 using std::__math::acosh;
446 using std::__math::asin;
447 using std::__math::asinh;
448 using std::__math::atan;
449 using std::__math::atan2;
450 using std::__math::atanh;
451 using std::__math::cbrt;
452 using std::__math::ceil;
453 using std::__math::copysign;
454 using std::__math::cos;
455 using std::__math::cosh;
456 using std::__math::erf;
457 using std::__math::erfc;
458 using std::__math::exp;
459 using std::__math::exp2;
460 using std::__math::expm1;
461 using std::__math::fabs;
462 using std::__math::fdim;
463 using std::__math::floor;
464 using std::__math::fma;
465 using std::__math::fmax;
466 using std::__math::fmin;
467 using std::__math::fmod;
468 using std::__math::frexp;
469 using std::__math::hypot;
470 using std::__math::ilogb;
471 using std::__math::ldexp;
472 using std::__math::lgamma;
473 using std::__math::llrint;
474 using std::__math::llround;
475 using std::__math::log;
476 using std::__math::log10;
477 using std::__math::log1p;
478 using std::__math::log2;
479 using std::__math::logb;
480 using std::__math::lrint;
481 using std::__math::lround;
482 using std::__math::modf;
483 using std::__math::nearbyint;
484 using std::__math::nextafter;
485 using std::__math::nexttoward;
486 using std::__math::pow;
487 using std::__math::remainder;
488 using std::__math::remquo;
489 using std::__math::rint;
490 using std::__math::round;
491 using std::__math::scalbln;
492 using std::__math::scalbn;
493 using std::__math::signbit;
494 using std::__math::sin;
495 using std::__math::sinh;
496 using std::__math::sqrt;
497 using std::__math::tan;
498 using std::__math::tanh;
499 using std::__math::tgamma;
500 using std::__math::trunc;
501 
502 } // extern "C++"
503 
504 #  endif // __cplusplus
505 
506 #else // _LIBCPP_MATH_H
507 
508 // This include lives outside the header guard in order to support an MSVC
509 // extension which allows users to do:
510 //
511 // #define _USE_MATH_DEFINES
512 // #include <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 #  endif
519 
520 #endif // _LIBCPP_MATH_H
521