]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/cmath
Vendor import of stripped libc++ trunk r375505, the last commit before
[FreeBSD/FreeBSD.git] / include / cmath
1 // -*- C++ -*-
2 //===---------------------------- cmath -----------------------------------===//
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_CMATH
11 #define _LIBCPP_CMATH
12
13 /*
14     cmath 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 namespace std
38 {
39
40 Types:
41
42     float_t                 // C99
43     double_t                // C99
44
45 // C90
46
47 floating_point abs(floating_point x);
48
49 floating_point acos (arithmetic x);
50 float          acosf(float x);
51 long double    acosl(long double x);
52
53 floating_point asin (arithmetic x);
54 float          asinf(float x);
55 long double    asinl(long double x);
56
57 floating_point atan (arithmetic x);
58 float          atanf(float x);
59 long double    atanl(long double x);
60
61 floating_point atan2 (arithmetic y, arithmetic x);
62 float          atan2f(float y, float x);
63 long double    atan2l(long double y, long double x);
64
65 floating_point ceil (arithmetic x);
66 float          ceilf(float x);
67 long double    ceill(long double x);
68
69 floating_point cos (arithmetic x);
70 float          cosf(float x);
71 long double    cosl(long double x);
72
73 floating_point cosh (arithmetic x);
74 float          coshf(float x);
75 long double    coshl(long double x);
76
77 floating_point exp (arithmetic x);
78 float          expf(float x);
79 long double    expl(long double x);
80
81 floating_point fabs (arithmetic x);
82 float          fabsf(float x);
83 long double    fabsl(long double x);
84
85 floating_point floor (arithmetic x);
86 float          floorf(float x);
87 long double    floorl(long double x);
88
89 floating_point fmod (arithmetic x, arithmetic y);
90 float          fmodf(float x, float y);
91 long double    fmodl(long double x, long double y);
92
93 floating_point frexp (arithmetic value, int* exp);
94 float          frexpf(float value, int* exp);
95 long double    frexpl(long double value, int* exp);
96
97 floating_point ldexp (arithmetic value, int exp);
98 float          ldexpf(float value, int exp);
99 long double    ldexpl(long double value, int exp);
100
101 floating_point log (arithmetic x);
102 float          logf(float x);
103 long double    logl(long double x);
104
105 floating_point log10 (arithmetic x);
106 float          log10f(float x);
107 long double    log10l(long double x);
108
109 floating_point modf (floating_point value, floating_point* iptr);
110 float          modff(float value, float* iptr);
111 long double    modfl(long double value, long double* iptr);
112
113 floating_point pow (arithmetic x, arithmetic y);
114 float          powf(float x, float y);
115 long double    powl(long double x, long double y);
116
117 floating_point sin (arithmetic x);
118 float          sinf(float x);
119 long double    sinl(long double x);
120
121 floating_point sinh (arithmetic x);
122 float          sinhf(float x);
123 long double    sinhl(long double x);
124
125 floating_point sqrt (arithmetic x);
126 float          sqrtf(float x);
127 long double    sqrtl(long double x);
128
129 floating_point tan (arithmetic x);
130 float          tanf(float x);
131 long double    tanl(long double x);
132
133 floating_point tanh (arithmetic x);
134 float          tanhf(float x);
135 long double    tanhl(long double x);
136
137 //  C99
138
139 bool signbit(arithmetic x);
140
141 int fpclassify(arithmetic x);
142
143 bool isfinite(arithmetic x);
144 bool isinf(arithmetic x);
145 bool isnan(arithmetic x);
146 bool isnormal(arithmetic x);
147
148 bool isgreater(arithmetic x, arithmetic y);
149 bool isgreaterequal(arithmetic x, arithmetic y);
150 bool isless(arithmetic x, arithmetic y);
151 bool islessequal(arithmetic x, arithmetic y);
152 bool islessgreater(arithmetic x, arithmetic y);
153 bool isunordered(arithmetic x, arithmetic y);
154
155 floating_point acosh (arithmetic x);
156 float          acoshf(float x);
157 long double    acoshl(long double x);
158
159 floating_point asinh (arithmetic x);
160 float          asinhf(float x);
161 long double    asinhl(long double x);
162
163 floating_point atanh (arithmetic x);
164 float          atanhf(float x);
165 long double    atanhl(long double x);
166
167 floating_point cbrt (arithmetic x);
168 float          cbrtf(float x);
169 long double    cbrtl(long double x);
170
171 floating_point copysign (arithmetic x, arithmetic y);
172 float          copysignf(float x, float y);
173 long double    copysignl(long double x, long double y);
174
175 floating_point erf (arithmetic x);
176 float          erff(float x);
177 long double    erfl(long double x);
178
179 floating_point erfc (arithmetic x);
180 float          erfcf(float x);
181 long double    erfcl(long double x);
182
183 floating_point exp2 (arithmetic x);
184 float          exp2f(float x);
185 long double    exp2l(long double x);
186
187 floating_point expm1 (arithmetic x);
188 float          expm1f(float x);
189 long double    expm1l(long double x);
190
191 floating_point fdim (arithmetic x, arithmetic y);
192 float          fdimf(float x, float y);
193 long double    fdiml(long double x, long double y);
194
195 floating_point fma (arithmetic x, arithmetic y, arithmetic z);
196 float          fmaf(float x, float y, float z);
197 long double    fmal(long double x, long double y, long double z);
198
199 floating_point fmax (arithmetic x, arithmetic y);
200 float          fmaxf(float x, float y);
201 long double    fmaxl(long double x, long double y);
202
203 floating_point fmin (arithmetic x, arithmetic y);
204 float          fminf(float x, float y);
205 long double    fminl(long double x, long double y);
206
207 floating_point hypot (arithmetic x, arithmetic y);
208 float          hypotf(float x, float y);
209 long double    hypotl(long double x, long double y);
210
211 double       hypot(double x, double y, double z);                // C++17
212 float        hypot(float x, float y, float z);                   // C++17
213 long double  hypot(long double x, long double y, long double z); // C++17
214
215 int ilogb (arithmetic x);
216 int ilogbf(float x);
217 int ilogbl(long double x);
218
219 floating_point lgamma (arithmetic x);
220 float          lgammaf(float x);
221 long double    lgammal(long double x);
222
223 long long llrint (arithmetic x);
224 long long llrintf(float x);
225 long long llrintl(long double x);
226
227 long long llround (arithmetic x);
228 long long llroundf(float x);
229 long long llroundl(long double x);
230
231 floating_point log1p (arithmetic x);
232 float          log1pf(float x);
233 long double    log1pl(long double x);
234
235 floating_point log2 (arithmetic x);
236 float          log2f(float x);
237 long double    log2l(long double x);
238
239 floating_point logb (arithmetic x);
240 float          logbf(float x);
241 long double    logbl(long double x);
242
243 long lrint (arithmetic x);
244 long lrintf(float x);
245 long lrintl(long double x);
246
247 long lround (arithmetic x);
248 long lroundf(float x);
249 long lroundl(long double x);
250
251 double      nan (const char* str);
252 float       nanf(const char* str);
253 long double nanl(const char* str);
254
255 floating_point nearbyint (arithmetic x);
256 float          nearbyintf(float x);
257 long double    nearbyintl(long double x);
258
259 floating_point nextafter (arithmetic x, arithmetic y);
260 float          nextafterf(float x, float y);
261 long double    nextafterl(long double x, long double y);
262
263 floating_point nexttoward (arithmetic x, long double y);
264 float          nexttowardf(float x, long double y);
265 long double    nexttowardl(long double x, long double y);
266
267 floating_point remainder (arithmetic x, arithmetic y);
268 float          remainderf(float x, float y);
269 long double    remainderl(long double x, long double y);
270
271 floating_point remquo (arithmetic x, arithmetic y, int* pquo);
272 float          remquof(float x, float y, int* pquo);
273 long double    remquol(long double x, long double y, int* pquo);
274
275 floating_point rint (arithmetic x);
276 float          rintf(float x);
277 long double    rintl(long double x);
278
279 floating_point round (arithmetic x);
280 float          roundf(float x);
281 long double    roundl(long double x);
282
283 floating_point scalbln (arithmetic x, long ex);
284 float          scalblnf(float x, long ex);
285 long double    scalblnl(long double x, long ex);
286
287 floating_point scalbn (arithmetic x, int ex);
288 float          scalbnf(float x, int ex);
289 long double    scalbnl(long double x, int ex);
290
291 floating_point tgamma (arithmetic x);
292 float          tgammaf(float x);
293 long double    tgammal(long double x);
294
295 floating_point trunc (arithmetic x);
296 float          truncf(float x);
297 long double    truncl(long double x);
298
299 }  // std
300
301 */
302
303 #include <__config>
304 #include <math.h>
305 #include <version>
306 #include <type_traits>
307
308 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
309 #pragma GCC system_header
310 #endif
311
312 _LIBCPP_PUSH_MACROS
313 #include <__undef_macros>
314
315 _LIBCPP_BEGIN_NAMESPACE_STD
316
317 using ::signbit;
318 using ::fpclassify;
319 using ::isfinite;
320 using ::isinf;
321 using ::isnan;
322 using ::isnormal;
323 using ::isgreater;
324 using ::isgreaterequal;
325 using ::isless;
326 using ::islessequal;
327 using ::islessgreater;
328 using ::isunordered;
329 using ::isunordered;
330
331 using ::float_t;
332 using ::double_t;
333
334 #ifndef _AIX
335 using ::abs;
336 #endif
337
338 using ::acos;
339 using ::acosf;
340 using ::asin;
341 using ::asinf;
342 using ::atan;
343 using ::atanf;
344 using ::atan2;
345 using ::atan2f;
346 using ::ceil;
347 using ::ceilf;
348 using ::cos;
349 using ::cosf;
350 using ::cosh;
351 using ::coshf;
352
353 using ::exp;
354 using ::expf;
355
356 using ::fabs;
357 using ::fabsf;
358 using ::floor;
359 using ::floorf;
360
361 using ::fmod;
362 using ::fmodf;
363
364 using ::frexp;
365 using ::frexpf;
366 using ::ldexp;
367 using ::ldexpf;
368
369 using ::log;
370 using ::logf;
371
372 using ::log10;
373 using ::log10f;
374 using ::modf;
375 using ::modff;
376
377 using ::pow;
378 using ::powf;
379
380 using ::sin;
381 using ::sinf;
382 using ::sinh;
383 using ::sinhf;
384
385 using ::sqrt;
386 using ::sqrtf;
387 using ::tan;
388 using ::tanf;
389
390 using ::tanh;
391 using ::tanhf;
392
393 using ::acosh;
394 using ::acoshf;
395 using ::asinh;
396 using ::asinhf;
397 using ::atanh;
398 using ::atanhf;
399 using ::cbrt;
400 using ::cbrtf;
401
402 using ::copysign;
403 using ::copysignf;
404
405 using ::erf;
406 using ::erff;
407 using ::erfc;
408 using ::erfcf;
409 using ::exp2;
410 using ::exp2f;
411 using ::expm1;
412 using ::expm1f;
413 using ::fdim;
414 using ::fdimf;
415 using ::fmaf;
416 using ::fma;
417 using ::fmax;
418 using ::fmaxf;
419 using ::fmin;
420 using ::fminf;
421 using ::hypot;
422 using ::hypotf;
423 using ::ilogb;
424 using ::ilogbf;
425 using ::lgamma;
426 using ::lgammaf;
427 using ::llrint;
428 using ::llrintf;
429 using ::llround;
430 using ::llroundf;
431 using ::log1p;
432 using ::log1pf;
433 using ::log2;
434 using ::log2f;
435 using ::logb;
436 using ::logbf;
437 using ::lrint;
438 using ::lrintf;
439 using ::lround;
440 using ::lroundf;
441
442 using ::nan;
443 using ::nanf;
444
445 using ::nearbyint;
446 using ::nearbyintf;
447 using ::nextafter;
448 using ::nextafterf;
449 using ::nexttoward;
450 using ::nexttowardf;
451 using ::remainder;
452 using ::remainderf;
453 using ::remquo;
454 using ::remquof;
455 using ::rint;
456 using ::rintf;
457 using ::round;
458 using ::roundf;
459 using ::scalbln;
460 using ::scalblnf;
461 using ::scalbn;
462 using ::scalbnf;
463 using ::tgamma;
464 using ::tgammaf;
465 using ::trunc;
466 using ::truncf;
467
468 using ::acosl;
469 using ::asinl;
470 using ::atanl;
471 using ::atan2l;
472 using ::ceill;
473 using ::cosl;
474 using ::coshl;
475 using ::expl;
476 using ::fabsl;
477 using ::floorl;
478 using ::fmodl;
479 using ::frexpl;
480 using ::ldexpl;
481 using ::logl;
482 using ::log10l;
483 using ::modfl;
484 using ::powl;
485 using ::sinl;
486 using ::sinhl;
487 using ::sqrtl;
488 using ::tanl;
489
490 using ::tanhl;
491 using ::acoshl;
492 using ::asinhl;
493 using ::atanhl;
494 using ::cbrtl;
495
496 using ::copysignl;
497
498 using ::erfl;
499 using ::erfcl;
500 using ::exp2l;
501 using ::expm1l;
502 using ::fdiml;
503 using ::fmal;
504 using ::fmaxl;
505 using ::fminl;
506 using ::hypotl;
507 using ::ilogbl;
508 using ::lgammal;
509 using ::llrintl;
510 using ::llroundl;
511 using ::log1pl;
512 using ::log2l;
513 using ::logbl;
514 using ::lrintl;
515 using ::lroundl;
516 using ::nanl;
517 using ::nearbyintl;
518 using ::nextafterl;
519 using ::nexttowardl;
520 using ::remainderl;
521 using ::remquol;
522 using ::rintl;
523 using ::roundl;
524 using ::scalblnl;
525 using ::scalbnl;
526 using ::tgammal;
527 using ::truncl;
528
529 #if _LIBCPP_STD_VER > 14
530 inline _LIBCPP_INLINE_VISIBILITY float       hypot(       float x,       float y,       float z ) { return sqrt(x*x + y*y + z*z); }
531 inline _LIBCPP_INLINE_VISIBILITY double      hypot(      double x,      double y,      double z ) { return sqrt(x*x + y*y + z*z); }
532 inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); }
533
534 template <class _A1, class _A2, class _A3>
535 inline _LIBCPP_INLINE_VISIBILITY
536 typename _EnableIf
537 <
538     is_arithmetic<_A1>::value &&
539     is_arithmetic<_A2>::value &&
540     is_arithmetic<_A3>::value,
541     __promote<_A1, _A2, _A3>
542 >::type
543 hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT
544 {
545     typedef typename __promote<_A1, _A2, _A3>::type __result_type;
546     static_assert((!(is_same<_A1, __result_type>::value &&
547                      is_same<_A2, __result_type>::value &&
548                      is_same<_A3, __result_type>::value)), "");
549     return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
550 }
551 #endif
552
553 template <class _A1>
554 _LIBCPP_INLINE_VISIBILITY
555 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
556 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
557 {
558 #if __has_builtin(__builtin_isnan)
559     return __builtin_isnan(__lcpp_x);
560 #else
561     return isnan(__lcpp_x);
562 #endif
563 }
564
565 template <class _A1>
566 _LIBCPP_INLINE_VISIBILITY
567 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
568 __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
569 {
570     return isnan(__lcpp_x);
571 }
572
573 template <class _A1>
574 _LIBCPP_INLINE_VISIBILITY
575 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
576 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
577 {
578 #if __has_builtin(__builtin_isinf)
579     return __builtin_isinf(__lcpp_x);
580 #else
581     return isinf(__lcpp_x);
582 #endif
583 }
584
585 template <class _A1>
586 _LIBCPP_INLINE_VISIBILITY
587 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
588 __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
589 {
590     return isinf(__lcpp_x);
591 }
592
593 template <class _A1>
594 _LIBCPP_INLINE_VISIBILITY
595 _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
596 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
597 {
598 #if __has_builtin(__builtin_isfinite)
599     return __builtin_isfinite(__lcpp_x);
600 #else
601     return isfinite(__lcpp_x);
602 #endif
603 }
604
605 template <class _A1>
606 _LIBCPP_INLINE_VISIBILITY
607 _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
608 __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
609 {
610     return isfinite(__lcpp_x);
611 }
612
613 #if _LIBCPP_STD_VER > 17
614 template <typename _Fp>
615 constexpr
616 _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
617     if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
618         return __t * __b + (1 - __t) * __a;
619
620     if (__t == 1) return __b;
621     const _Fp __x = __a + __t * (__b - __a);
622     if (__t > 1 == __b > __a)
623         return __b < __x ? __x : __b;
624     else
625         return __x < __b ? __x : __b;
626 }
627
628 constexpr float
629 lerp(float __a, float __b, float __t)                   _NOEXCEPT { return __lerp(__a, __b, __t); }
630
631 constexpr double
632 lerp(double __a, double __b, double __t)                _NOEXCEPT { return __lerp(__a, __b, __t); }
633
634 constexpr long double
635 lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
636
637 #endif // _LIBCPP_STD_VER > 17
638
639 template <class _IntT, class _FloatT,
640     bool _FloatBigger = (numeric_limits<_FloatT>::digits > numeric_limits<_IntT>::digits),
641     int _Bits = (numeric_limits<_IntT>::digits - numeric_limits<_FloatT>::digits)>
642 _LIBCPP_INLINE_VISIBILITY
643 _LIBCPP_CONSTEXPR _IntT __max_representable_int_for_float() _NOEXCEPT {
644   static_assert(is_floating_point<_FloatT>::value, "must be a floating point type");
645   static_assert(is_integral<_IntT>::value, "must be an integral type");
646   static_assert(numeric_limits<_FloatT>::radix == 2, "FloatT has incorrect radix");
647   static_assert((_IsSame<_FloatT, float>::value || _IsSame<_FloatT, double>::value
648                  || _IsSame<_FloatT,long double>::value), "unsupported floating point type");
649   return _FloatBigger ? numeric_limits<_IntT>::max() :  (numeric_limits<_IntT>::max() >> _Bits << _Bits);
650 }
651
652 // Convert a floating point number to the specified integral type after
653 // clamping to the integral types representable range.
654 //
655 // The behavior is undefined if `__r` is NaN.
656 template <class _IntT, class _RealT>
657 _LIBCPP_INLINE_VISIBILITY
658 _IntT __clamp_to_integral(_RealT __r) _NOEXCEPT {
659   using _Lim = std::numeric_limits<_IntT>;
660   const _IntT _MaxVal = std::__max_representable_int_for_float<_IntT, _RealT>();
661   if (__r >= ::nextafter(static_cast<_RealT>(_MaxVal), INFINITY)) {
662     return _Lim::max();
663   } else if (__r <= _Lim::lowest()) {
664     return _Lim::min();
665   }
666   return static_cast<_IntT>(__r);
667 }
668
669 _LIBCPP_END_NAMESPACE_STD
670
671 _LIBCPP_POP_MACROS
672
673 #endif  // _LIBCPP_CMATH