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_CMATH
11#define _LIBCPP_CMATH
12
13/*
14    cmath 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
37namespace std
38{
39
40Types:
41
42    float_t                 // C99
43    double_t                // C99
44
45// C90
46
47floating_point abs(floating_point x);
48
49floating_point acos (arithmetic x);
50float          acosf(float x);
51long double    acosl(long double x);
52
53floating_point asin (arithmetic x);
54float          asinf(float x);
55long double    asinl(long double x);
56
57floating_point atan (arithmetic x);
58float          atanf(float x);
59long double    atanl(long double x);
60
61floating_point atan2 (arithmetic y, arithmetic x);
62float          atan2f(float y, float x);
63long double    atan2l(long double y, long double x);
64
65floating_point ceil (arithmetic x);
66float          ceilf(float x);
67long double    ceill(long double x);
68
69floating_point cos (arithmetic x);
70float          cosf(float x);
71long double    cosl(long double x);
72
73floating_point cosh (arithmetic x);
74float          coshf(float x);
75long double    coshl(long double x);
76
77floating_point exp (arithmetic x);
78float          expf(float x);
79long double    expl(long double x);
80
81floating_point fabs (arithmetic x);
82float          fabsf(float x);
83long double    fabsl(long double x);
84
85floating_point floor (arithmetic x);
86float          floorf(float x);
87long double    floorl(long double x);
88
89floating_point fmod (arithmetic x, arithmetic y);
90float          fmodf(float x, float y);
91long double    fmodl(long double x, long double y);
92
93floating_point frexp (arithmetic value, int* exp);
94float          frexpf(float value, int* exp);
95long double    frexpl(long double value, int* exp);
96
97floating_point ldexp (arithmetic value, int exp);
98float          ldexpf(float value, int exp);
99long double    ldexpl(long double value, int exp);
100
101floating_point log (arithmetic x);
102float          logf(float x);
103long double    logl(long double x);
104
105floating_point log10 (arithmetic x);
106float          log10f(float x);
107long double    log10l(long double x);
108
109floating_point modf (floating_point value, floating_point* iptr);
110float          modff(float value, float* iptr);
111long double    modfl(long double value, long double* iptr);
112
113floating_point pow (arithmetic x, arithmetic y);
114float          powf(float x, float y);
115long double    powl(long double x, long double y);
116
117floating_point sin (arithmetic x);
118float          sinf(float x);
119long double    sinl(long double x);
120
121floating_point sinh (arithmetic x);
122float          sinhf(float x);
123long double    sinhl(long double x);
124
125floating_point sqrt (arithmetic x);
126float          sqrtf(float x);
127long double    sqrtl(long double x);
128
129floating_point tan (arithmetic x);
130float          tanf(float x);
131long double    tanl(long double x);
132
133floating_point tanh (arithmetic x);
134float          tanhf(float x);
135long double    tanhl(long double x);
136
137//  C99
138
139bool signbit(arithmetic x);
140
141int fpclassify(arithmetic x);
142
143bool isfinite(arithmetic x);
144bool isinf(arithmetic x);
145bool isnan(arithmetic x);
146bool isnormal(arithmetic x);
147
148bool isgreater(arithmetic x, arithmetic y);
149bool isgreaterequal(arithmetic x, arithmetic y);
150bool isless(arithmetic x, arithmetic y);
151bool islessequal(arithmetic x, arithmetic y);
152bool islessgreater(arithmetic x, arithmetic y);
153bool isunordered(arithmetic x, arithmetic y);
154
155floating_point acosh (arithmetic x);
156float          acoshf(float x);
157long double    acoshl(long double x);
158
159floating_point asinh (arithmetic x);
160float          asinhf(float x);
161long double    asinhl(long double x);
162
163floating_point atanh (arithmetic x);
164float          atanhf(float x);
165long double    atanhl(long double x);
166
167floating_point cbrt (arithmetic x);
168float          cbrtf(float x);
169long double    cbrtl(long double x);
170
171floating_point copysign (arithmetic x, arithmetic y);
172float          copysignf(float x, float y);
173long double    copysignl(long double x, long double y);
174
175floating_point erf (arithmetic x);
176float          erff(float x);
177long double    erfl(long double x);
178
179floating_point erfc (arithmetic x);
180float          erfcf(float x);
181long double    erfcl(long double x);
182
183floating_point exp2 (arithmetic x);
184float          exp2f(float x);
185long double    exp2l(long double x);
186
187floating_point expm1 (arithmetic x);
188float          expm1f(float x);
189long double    expm1l(long double x);
190
191floating_point fdim (arithmetic x, arithmetic y);
192float          fdimf(float x, float y);
193long double    fdiml(long double x, long double y);
194
195floating_point fma (arithmetic x, arithmetic y, arithmetic z);
196float          fmaf(float x, float y, float z);
197long double    fmal(long double x, long double y, long double z);
198
199floating_point fmax (arithmetic x, arithmetic y);
200float          fmaxf(float x, float y);
201long double    fmaxl(long double x, long double y);
202
203floating_point fmin (arithmetic x, arithmetic y);
204float          fminf(float x, float y);
205long double    fminl(long double x, long double y);
206
207floating_point hypot (arithmetic x, arithmetic y);
208float          hypotf(float x, float y);
209long double    hypotl(long double x, long double y);
210
211double       hypot(double x, double y, double z);                // C++17
212float        hypot(float x, float y, float z);                   // C++17
213long double  hypot(long double x, long double y, long double z); // C++17
214
215int ilogb (arithmetic x);
216int ilogbf(float x);
217int ilogbl(long double x);
218
219floating_point lgamma (arithmetic x);
220float          lgammaf(float x);
221long double    lgammal(long double x);
222
223long long llrint (arithmetic x);
224long long llrintf(float x);
225long long llrintl(long double x);
226
227long long llround (arithmetic x);
228long long llroundf(float x);
229long long llroundl(long double x);
230
231floating_point log1p (arithmetic x);
232float          log1pf(float x);
233long double    log1pl(long double x);
234
235floating_point log2 (arithmetic x);
236float          log2f(float x);
237long double    log2l(long double x);
238
239floating_point logb (arithmetic x);
240float          logbf(float x);
241long double    logbl(long double x);
242
243long lrint (arithmetic x);
244long lrintf(float x);
245long lrintl(long double x);
246
247long lround (arithmetic x);
248long lroundf(float x);
249long lroundl(long double x);
250
251double      nan (const char* str);
252float       nanf(const char* str);
253long double nanl(const char* str);
254
255floating_point nearbyint (arithmetic x);
256float          nearbyintf(float x);
257long double    nearbyintl(long double x);
258
259floating_point nextafter (arithmetic x, arithmetic y);
260float          nextafterf(float x, float y);
261long double    nextafterl(long double x, long double y);
262
263floating_point nexttoward (arithmetic x, long double y);
264float          nexttowardf(float x, long double y);
265long double    nexttowardl(long double x, long double y);
266
267floating_point remainder (arithmetic x, arithmetic y);
268float          remainderf(float x, float y);
269long double    remainderl(long double x, long double y);
270
271floating_point remquo (arithmetic x, arithmetic y, int* pquo);
272float          remquof(float x, float y, int* pquo);
273long double    remquol(long double x, long double y, int* pquo);
274
275floating_point rint (arithmetic x);
276float          rintf(float x);
277long double    rintl(long double x);
278
279floating_point round (arithmetic x);
280float          roundf(float x);
281long double    roundl(long double x);
282
283floating_point scalbln (arithmetic x, long ex);
284float          scalblnf(float x, long ex);
285long double    scalblnl(long double x, long ex);
286
287floating_point scalbn (arithmetic x, int ex);
288float          scalbnf(float x, int ex);
289long double    scalbnl(long double x, int ex);
290
291floating_point tgamma (arithmetic x);
292float          tgammaf(float x);
293long double    tgammal(long double x);
294
295floating_point trunc (arithmetic x);
296float          truncf(float x);
297long double    truncl(long double x);
298
299constexpr float       lerp(float a, float b, float t) noexcept;                   // C++20
300constexpr double      lerp(double a, double b, double t) noexcept;                // C++20
301constexpr long double lerp(long double a, long double b, long double t) noexcept; // C++20
302
303}  // std
304
305*/
306
307#include <__assert> // all public C++ headers provide the assertion handler
308#include <__config>
309#include <__type_traits/enable_if.h>
310#include <__type_traits/is_arithmetic.h>
311#include <__type_traits/is_constant_evaluated.h>
312#include <__type_traits/is_floating_point.h>
313#include <__type_traits/is_same.h>
314#include <__type_traits/promote.h>
315#include <__type_traits/remove_cv.h>
316#include <version>
317
318#include <math.h>
319
320#ifndef _LIBCPP_MATH_H
321#   error <cmath> tried including <math.h> but didn't find libc++'s <math.h> header. \
322          This usually means that your header search paths are not configured properly. \
323          The header search paths should contain the C++ Standard Library headers before \
324          any C Standard Library, and you are probably using compiler flags that make that \
325          not be the case.
326#endif
327
328#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
329#  pragma GCC system_header
330#endif
331
332_LIBCPP_PUSH_MACROS
333#include <__undef_macros>
334
335_LIBCPP_BEGIN_NAMESPACE_STD
336
337using ::signbit _LIBCPP_USING_IF_EXISTS;
338using ::fpclassify _LIBCPP_USING_IF_EXISTS;
339using ::isfinite _LIBCPP_USING_IF_EXISTS;
340using ::isinf _LIBCPP_USING_IF_EXISTS;
341using ::isnan _LIBCPP_USING_IF_EXISTS;
342using ::isnormal _LIBCPP_USING_IF_EXISTS;
343using ::isgreater _LIBCPP_USING_IF_EXISTS;
344using ::isgreaterequal _LIBCPP_USING_IF_EXISTS;
345using ::isless _LIBCPP_USING_IF_EXISTS;
346using ::islessequal _LIBCPP_USING_IF_EXISTS;
347using ::islessgreater _LIBCPP_USING_IF_EXISTS;
348using ::isunordered _LIBCPP_USING_IF_EXISTS;
349using ::isunordered _LIBCPP_USING_IF_EXISTS;
350
351using ::float_t _LIBCPP_USING_IF_EXISTS;
352using ::double_t _LIBCPP_USING_IF_EXISTS;
353
354using ::abs _LIBCPP_USING_IF_EXISTS;
355
356using ::acos _LIBCPP_USING_IF_EXISTS;
357using ::acosf _LIBCPP_USING_IF_EXISTS;
358using ::asin _LIBCPP_USING_IF_EXISTS;
359using ::asinf _LIBCPP_USING_IF_EXISTS;
360using ::atan _LIBCPP_USING_IF_EXISTS;
361using ::atanf _LIBCPP_USING_IF_EXISTS;
362using ::atan2 _LIBCPP_USING_IF_EXISTS;
363using ::atan2f _LIBCPP_USING_IF_EXISTS;
364using ::ceil _LIBCPP_USING_IF_EXISTS;
365using ::ceilf _LIBCPP_USING_IF_EXISTS;
366using ::cos _LIBCPP_USING_IF_EXISTS;
367using ::cosf _LIBCPP_USING_IF_EXISTS;
368using ::cosh _LIBCPP_USING_IF_EXISTS;
369using ::coshf _LIBCPP_USING_IF_EXISTS;
370
371using ::exp _LIBCPP_USING_IF_EXISTS;
372using ::expf _LIBCPP_USING_IF_EXISTS;
373
374using ::fabs _LIBCPP_USING_IF_EXISTS;
375using ::fabsf _LIBCPP_USING_IF_EXISTS;
376using ::floor _LIBCPP_USING_IF_EXISTS;
377using ::floorf _LIBCPP_USING_IF_EXISTS;
378
379using ::fmod _LIBCPP_USING_IF_EXISTS;
380using ::fmodf _LIBCPP_USING_IF_EXISTS;
381
382using ::frexp _LIBCPP_USING_IF_EXISTS;
383using ::frexpf _LIBCPP_USING_IF_EXISTS;
384using ::ldexp _LIBCPP_USING_IF_EXISTS;
385using ::ldexpf _LIBCPP_USING_IF_EXISTS;
386
387using ::log _LIBCPP_USING_IF_EXISTS;
388using ::logf _LIBCPP_USING_IF_EXISTS;
389
390using ::log10 _LIBCPP_USING_IF_EXISTS;
391using ::log10f _LIBCPP_USING_IF_EXISTS;
392using ::modf _LIBCPP_USING_IF_EXISTS;
393using ::modff _LIBCPP_USING_IF_EXISTS;
394
395using ::pow _LIBCPP_USING_IF_EXISTS;
396using ::powf _LIBCPP_USING_IF_EXISTS;
397
398using ::sin _LIBCPP_USING_IF_EXISTS;
399using ::sinf _LIBCPP_USING_IF_EXISTS;
400using ::sinh _LIBCPP_USING_IF_EXISTS;
401using ::sinhf _LIBCPP_USING_IF_EXISTS;
402
403using ::sqrt _LIBCPP_USING_IF_EXISTS;
404using ::sqrtf _LIBCPP_USING_IF_EXISTS;
405using ::tan _LIBCPP_USING_IF_EXISTS;
406using ::tanf _LIBCPP_USING_IF_EXISTS;
407
408using ::tanh _LIBCPP_USING_IF_EXISTS;
409using ::tanhf _LIBCPP_USING_IF_EXISTS;
410
411using ::acosh _LIBCPP_USING_IF_EXISTS;
412using ::acoshf _LIBCPP_USING_IF_EXISTS;
413using ::asinh _LIBCPP_USING_IF_EXISTS;
414using ::asinhf _LIBCPP_USING_IF_EXISTS;
415using ::atanh _LIBCPP_USING_IF_EXISTS;
416using ::atanhf _LIBCPP_USING_IF_EXISTS;
417using ::cbrt _LIBCPP_USING_IF_EXISTS;
418using ::cbrtf _LIBCPP_USING_IF_EXISTS;
419
420using ::copysign _LIBCPP_USING_IF_EXISTS;
421using ::copysignf _LIBCPP_USING_IF_EXISTS;
422
423using ::erf _LIBCPP_USING_IF_EXISTS;
424using ::erff _LIBCPP_USING_IF_EXISTS;
425using ::erfc _LIBCPP_USING_IF_EXISTS;
426using ::erfcf _LIBCPP_USING_IF_EXISTS;
427using ::exp2 _LIBCPP_USING_IF_EXISTS;
428using ::exp2f _LIBCPP_USING_IF_EXISTS;
429using ::expm1 _LIBCPP_USING_IF_EXISTS;
430using ::expm1f _LIBCPP_USING_IF_EXISTS;
431using ::fdim _LIBCPP_USING_IF_EXISTS;
432using ::fdimf _LIBCPP_USING_IF_EXISTS;
433using ::fmaf _LIBCPP_USING_IF_EXISTS;
434using ::fma _LIBCPP_USING_IF_EXISTS;
435using ::fmax _LIBCPP_USING_IF_EXISTS;
436using ::fmaxf _LIBCPP_USING_IF_EXISTS;
437using ::fmin _LIBCPP_USING_IF_EXISTS;
438using ::fminf _LIBCPP_USING_IF_EXISTS;
439using ::hypot _LIBCPP_USING_IF_EXISTS;
440using ::hypotf _LIBCPP_USING_IF_EXISTS;
441using ::ilogb _LIBCPP_USING_IF_EXISTS;
442using ::ilogbf _LIBCPP_USING_IF_EXISTS;
443using ::lgamma _LIBCPP_USING_IF_EXISTS;
444using ::lgammaf _LIBCPP_USING_IF_EXISTS;
445using ::llrint _LIBCPP_USING_IF_EXISTS;
446using ::llrintf _LIBCPP_USING_IF_EXISTS;
447using ::llround _LIBCPP_USING_IF_EXISTS;
448using ::llroundf _LIBCPP_USING_IF_EXISTS;
449using ::log1p _LIBCPP_USING_IF_EXISTS;
450using ::log1pf _LIBCPP_USING_IF_EXISTS;
451using ::log2 _LIBCPP_USING_IF_EXISTS;
452using ::log2f _LIBCPP_USING_IF_EXISTS;
453using ::logb _LIBCPP_USING_IF_EXISTS;
454using ::logbf _LIBCPP_USING_IF_EXISTS;
455using ::lrint _LIBCPP_USING_IF_EXISTS;
456using ::lrintf _LIBCPP_USING_IF_EXISTS;
457using ::lround _LIBCPP_USING_IF_EXISTS;
458using ::lroundf _LIBCPP_USING_IF_EXISTS;
459
460using ::nan _LIBCPP_USING_IF_EXISTS;
461using ::nanf _LIBCPP_USING_IF_EXISTS;
462
463using ::nearbyint _LIBCPP_USING_IF_EXISTS;
464using ::nearbyintf _LIBCPP_USING_IF_EXISTS;
465using ::nextafter _LIBCPP_USING_IF_EXISTS;
466using ::nextafterf _LIBCPP_USING_IF_EXISTS;
467using ::nexttoward _LIBCPP_USING_IF_EXISTS;
468using ::nexttowardf _LIBCPP_USING_IF_EXISTS;
469using ::remainder _LIBCPP_USING_IF_EXISTS;
470using ::remainderf _LIBCPP_USING_IF_EXISTS;
471using ::remquo _LIBCPP_USING_IF_EXISTS;
472using ::remquof _LIBCPP_USING_IF_EXISTS;
473using ::rint _LIBCPP_USING_IF_EXISTS;
474using ::rintf _LIBCPP_USING_IF_EXISTS;
475using ::round _LIBCPP_USING_IF_EXISTS;
476using ::roundf _LIBCPP_USING_IF_EXISTS;
477using ::scalbln _LIBCPP_USING_IF_EXISTS;
478using ::scalblnf _LIBCPP_USING_IF_EXISTS;
479using ::scalbn _LIBCPP_USING_IF_EXISTS;
480using ::scalbnf _LIBCPP_USING_IF_EXISTS;
481using ::tgamma _LIBCPP_USING_IF_EXISTS;
482using ::tgammaf _LIBCPP_USING_IF_EXISTS;
483using ::trunc _LIBCPP_USING_IF_EXISTS;
484using ::truncf _LIBCPP_USING_IF_EXISTS;
485
486using ::acosl _LIBCPP_USING_IF_EXISTS;
487using ::asinl _LIBCPP_USING_IF_EXISTS;
488using ::atanl _LIBCPP_USING_IF_EXISTS;
489using ::atan2l _LIBCPP_USING_IF_EXISTS;
490using ::ceill _LIBCPP_USING_IF_EXISTS;
491using ::cosl _LIBCPP_USING_IF_EXISTS;
492using ::coshl _LIBCPP_USING_IF_EXISTS;
493using ::expl _LIBCPP_USING_IF_EXISTS;
494using ::fabsl _LIBCPP_USING_IF_EXISTS;
495using ::floorl _LIBCPP_USING_IF_EXISTS;
496using ::fmodl _LIBCPP_USING_IF_EXISTS;
497using ::frexpl _LIBCPP_USING_IF_EXISTS;
498using ::ldexpl _LIBCPP_USING_IF_EXISTS;
499using ::logl _LIBCPP_USING_IF_EXISTS;
500using ::log10l _LIBCPP_USING_IF_EXISTS;
501using ::modfl _LIBCPP_USING_IF_EXISTS;
502using ::powl _LIBCPP_USING_IF_EXISTS;
503using ::sinl _LIBCPP_USING_IF_EXISTS;
504using ::sinhl _LIBCPP_USING_IF_EXISTS;
505using ::sqrtl _LIBCPP_USING_IF_EXISTS;
506using ::tanl _LIBCPP_USING_IF_EXISTS;
507
508using ::tanhl _LIBCPP_USING_IF_EXISTS;
509using ::acoshl _LIBCPP_USING_IF_EXISTS;
510using ::asinhl _LIBCPP_USING_IF_EXISTS;
511using ::atanhl _LIBCPP_USING_IF_EXISTS;
512using ::cbrtl _LIBCPP_USING_IF_EXISTS;
513
514using ::copysignl _LIBCPP_USING_IF_EXISTS;
515
516using ::erfl _LIBCPP_USING_IF_EXISTS;
517using ::erfcl _LIBCPP_USING_IF_EXISTS;
518using ::exp2l _LIBCPP_USING_IF_EXISTS;
519using ::expm1l _LIBCPP_USING_IF_EXISTS;
520using ::fdiml _LIBCPP_USING_IF_EXISTS;
521using ::fmal _LIBCPP_USING_IF_EXISTS;
522using ::fmaxl _LIBCPP_USING_IF_EXISTS;
523using ::fminl _LIBCPP_USING_IF_EXISTS;
524using ::hypotl _LIBCPP_USING_IF_EXISTS;
525using ::ilogbl _LIBCPP_USING_IF_EXISTS;
526using ::lgammal _LIBCPP_USING_IF_EXISTS;
527using ::llrintl _LIBCPP_USING_IF_EXISTS;
528using ::llroundl _LIBCPP_USING_IF_EXISTS;
529using ::log1pl _LIBCPP_USING_IF_EXISTS;
530using ::log2l _LIBCPP_USING_IF_EXISTS;
531using ::logbl _LIBCPP_USING_IF_EXISTS;
532using ::lrintl _LIBCPP_USING_IF_EXISTS;
533using ::lroundl _LIBCPP_USING_IF_EXISTS;
534using ::nanl _LIBCPP_USING_IF_EXISTS;
535using ::nearbyintl _LIBCPP_USING_IF_EXISTS;
536using ::nextafterl _LIBCPP_USING_IF_EXISTS;
537using ::nexttowardl _LIBCPP_USING_IF_EXISTS;
538using ::remainderl _LIBCPP_USING_IF_EXISTS;
539using ::remquol _LIBCPP_USING_IF_EXISTS;
540using ::rintl _LIBCPP_USING_IF_EXISTS;
541using ::roundl _LIBCPP_USING_IF_EXISTS;
542using ::scalblnl _LIBCPP_USING_IF_EXISTS;
543using ::scalbnl _LIBCPP_USING_IF_EXISTS;
544using ::tgammal _LIBCPP_USING_IF_EXISTS;
545using ::truncl _LIBCPP_USING_IF_EXISTS;
546
547#if _LIBCPP_STD_VER >= 17
548inline _LIBCPP_INLINE_VISIBILITY float       hypot(       float __x,       float __y,       float __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
549inline _LIBCPP_INLINE_VISIBILITY double      hypot(      double __x,      double __y,      double __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
550inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double __x, long double __y, long double __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
551
552template <class _A1, class _A2, class _A3>
553inline _LIBCPP_INLINE_VISIBILITY
554typename enable_if_t
555<
556    is_arithmetic<_A1>::value &&
557    is_arithmetic<_A2>::value &&
558    is_arithmetic<_A3>::value,
559    __promote<_A1, _A2, _A3>
560>::type
561hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT
562{
563    typedef typename __promote<_A1, _A2, _A3>::type __result_type;
564    static_assert((!(is_same<_A1, __result_type>::value &&
565                     is_same<_A2, __result_type>::value &&
566                     is_same<_A3, __result_type>::value)), "");
567    return std::hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
568}
569#endif
570
571template <class _A1>
572_LIBCPP_INLINE_VISIBILITY
573_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
574__constexpr_isnan(_A1 __lcpp_x) _NOEXCEPT
575{
576#if __has_builtin(__builtin_isnan)
577    return __builtin_isnan(__lcpp_x);
578#else
579    return isnan(__lcpp_x);
580#endif
581}
582
583template <class _A1>
584_LIBCPP_INLINE_VISIBILITY
585_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
586__constexpr_isnan(_A1 __lcpp_x) _NOEXCEPT
587{
588    return std::isnan(__lcpp_x);
589}
590
591template <class _A1>
592_LIBCPP_INLINE_VISIBILITY
593_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
594__constexpr_isinf(_A1 __lcpp_x) _NOEXCEPT
595{
596#if __has_builtin(__builtin_isinf)
597    return __builtin_isinf(__lcpp_x);
598#else
599    return isinf(__lcpp_x);
600#endif
601}
602
603template <class _A1>
604_LIBCPP_INLINE_VISIBILITY
605_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
606__constexpr_isinf(_A1 __lcpp_x) _NOEXCEPT
607{
608    return std::isinf(__lcpp_x);
609}
610
611template <class _A1>
612_LIBCPP_INLINE_VISIBILITY
613_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
614__constexpr_isfinite(_A1 __lcpp_x) _NOEXCEPT
615{
616#if __has_builtin(__builtin_isfinite)
617    return __builtin_isfinite(__lcpp_x);
618#else
619    return isfinite(__lcpp_x);
620#endif
621}
622
623template <class _A1>
624_LIBCPP_INLINE_VISIBILITY
625_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
626__constexpr_isfinite(_A1 __lcpp_x) _NOEXCEPT
627{
628    return __builtin_isfinite(__lcpp_x);
629}
630
631_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI float __constexpr_copysign(float __x, float __y) _NOEXCEPT {
632    return __builtin_copysignf(__x, __y);
633}
634
635_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI double __constexpr_copysign(double __x, double __y) _NOEXCEPT {
636    return __builtin_copysign(__x, __y);
637}
638
639_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI long double
640__constexpr_copysign(long double __x, long double __y) _NOEXCEPT {
641    return __builtin_copysignl(__x, __y);
642}
643
644template <class _A1, class _A2>
645_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI
646    typename std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value,
647                                std::__promote<_A1, _A2> >::type
648    __constexpr_copysign(_A1 __x, _A2 __y) _NOEXCEPT {
649    typedef typename std::__promote<_A1, _A2>::type __result_type;
650    static_assert((!(std::_IsSame<_A1, __result_type>::value && std::_IsSame<_A2, __result_type>::value)), "");
651    return __builtin_copysign((__result_type)__x, (__result_type)__y);
652}
653
654inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float __constexpr_fabs(float __x) _NOEXCEPT {
655    return __builtin_fabsf(__x);
656}
657
658inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double __constexpr_fabs(double __x) _NOEXCEPT {
659    return __builtin_fabs(__x);
660}
661
662inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double __constexpr_fabs(long double __x) _NOEXCEPT {
663    return __builtin_fabsl(__x);
664}
665
666template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
667_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double __constexpr_fabs(_Tp __x) _NOEXCEPT {
668    return __builtin_fabs(static_cast<double>(__x));
669}
670
671inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 float __constexpr_fmax(float __x, float __y) _NOEXCEPT {
672#if !__has_constexpr_builtin(__builtin_fmaxf)
673  if (__libcpp_is_constant_evaluated()) {
674    if (std::__constexpr_isnan(__x))
675      return __y;
676    if (std::__constexpr_isnan(__y))
677      return __x;
678    return __x < __y ? __y : __x;
679  }
680#endif
681  return __builtin_fmaxf(__x, __y);
682}
683
684inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 double __constexpr_fmax(double __x, double __y) _NOEXCEPT {
685#if !__has_constexpr_builtin(__builtin_fmax)
686  if (__libcpp_is_constant_evaluated()) {
687    if (std::__constexpr_isnan(__x))
688      return __y;
689    if (std::__constexpr_isnan(__y))
690      return __x;
691    return __x < __y ? __y : __x;
692  }
693#endif
694  return __builtin_fmax(__x, __y);
695}
696
697inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 long double
698__constexpr_fmax(long double __x, long double __y) _NOEXCEPT {
699#if !__has_constexpr_builtin(__builtin_fmaxl)
700  if (__libcpp_is_constant_evaluated()) {
701    if (std::__constexpr_isnan(__x))
702      return __y;
703    if (std::__constexpr_isnan(__y))
704      return __x;
705    return __x < __y ? __y : __x;
706  }
707#endif
708  return __builtin_fmaxl(__x, __y);
709}
710
711template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
712_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __promote<_Tp, _Up>::type
713__constexpr_fmax(_Tp __x, _Up __y) _NOEXCEPT {
714  using __result_type = typename __promote<_Tp, _Up>::type;
715  return std::__constexpr_fmax(static_cast<__result_type>(__x), static_cast<__result_type>(__y));
716}
717
718template <class _Tp>
719_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __constexpr_logb(_Tp __x) {
720#if !__has_constexpr_builtin(__builtin_logb)
721  if (__libcpp_is_constant_evaluated()) {
722    if (__x == _Tp(0)) {
723      // raise FE_DIVBYZERO
724      return -numeric_limits<_Tp>::infinity();
725    }
726
727    if (std::__constexpr_isinf(__x))
728      return numeric_limits<_Tp>::infinity();
729
730    if (std::__constexpr_isnan(__x))
731      return numeric_limits<_Tp>::quiet_NaN();
732
733    __x = std::__constexpr_fabs(__x);
734    unsigned long long __exp = 0;
735    while (__x >= numeric_limits<_Tp>::radix) {
736      __x /= numeric_limits<_Tp>::radix;
737      __exp += 1;
738    }
739    return _Tp(__exp);
740  }
741#endif // !__has_constexpr_builtin(__builtin_logb)
742  return __builtin_logb(__x);
743}
744
745template <class _Tp>
746_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp __constexpr_scalbn(_Tp __x, int __exp) {
747#if !__has_constexpr_builtin(__builtin_scalbln)
748  if (__libcpp_is_constant_evaluated()) {
749    if (__x == _Tp(0))
750      return __x;
751
752    if (std::__constexpr_isinf(__x))
753      return __x;
754
755    if (__exp == _Tp(0))
756      return __x;
757
758    if (std::__constexpr_isnan(__x))
759      return numeric_limits<_Tp>::quiet_NaN();
760
761    _Tp __mult(1);
762    if (__exp > 0) {
763      __mult = numeric_limits<_Tp>::radix;
764      --__exp;
765    } else {
766      ++__exp;
767      __exp = -__exp;
768      __mult /= numeric_limits<_Tp>::radix;
769    }
770
771    while (__exp > 0) {
772      if (!(__exp & 1)) {
773        __mult *= __mult;
774        __exp >>= 1;
775      } else {
776        __x *= __mult;
777        --__exp;
778      }
779    }
780    return __x;
781  }
782#endif // !__has_constexpr_builtin(__builtin_scalbln)
783  return __builtin_scalbn(__x, __exp);
784}
785
786#if _LIBCPP_STD_VER >= 20
787template <typename _Fp>
788_LIBCPP_HIDE_FROM_ABI constexpr
789_Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
790    if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
791        return __t * __b + (1 - __t) * __a;
792
793    if (__t == 1) return __b;
794    const _Fp __x = __a + __t * (__b - __a);
795    if ((__t > 1) == (__b > __a))
796        return __b < __x ? __x : __b;
797    else
798        return __x < __b ? __x : __b;
799}
800
801_LIBCPP_HIDE_FROM_ABI constexpr float
802lerp(float __a, float __b, float __t)                   _NOEXCEPT { return __lerp(__a, __b, __t); }
803
804_LIBCPP_HIDE_FROM_ABI constexpr double
805lerp(double __a, double __b, double __t)                _NOEXCEPT { return __lerp(__a, __b, __t); }
806
807_LIBCPP_HIDE_FROM_ABI constexpr long double
808lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
809
810template <class _A1, class _A2, class _A3>
811inline _LIBCPP_HIDE_FROM_ABI
812constexpr typename enable_if_t
813<
814    is_arithmetic<_A1>::value &&
815    is_arithmetic<_A2>::value &&
816    is_arithmetic<_A3>::value,
817    __promote<_A1, _A2, _A3>
818>::type
819lerp(_A1 __a, _A2 __b, _A3 __t) noexcept
820{
821    typedef typename __promote<_A1, _A2, _A3>::type __result_type;
822    static_assert(!(_IsSame<_A1, __result_type>::value &&
823                    _IsSame<_A2, __result_type>::value &&
824                    _IsSame<_A3, __result_type>::value));
825    return std::__lerp((__result_type)__a, (__result_type)__b, (__result_type)__t);
826}
827#endif // _LIBCPP_STD_VER >= 20
828
829_LIBCPP_END_NAMESPACE_STD
830
831_LIBCPP_POP_MACROS
832
833#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
834#  include <type_traits>
835#endif
836
837#endif // _LIBCPP_CMATH
838