xref: /freebsd/contrib/llvm-project/libcxx/include/cmath (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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
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 <__config>
308#include <math.h>
309#include <version>
310#include <type_traits>
311
312#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
313#pragma GCC system_header
314#endif
315
316_LIBCPP_PUSH_MACROS
317#include <__undef_macros>
318
319_LIBCPP_BEGIN_NAMESPACE_STD
320
321using ::signbit _LIBCPP_USING_IF_EXISTS;
322using ::fpclassify _LIBCPP_USING_IF_EXISTS;
323using ::isfinite _LIBCPP_USING_IF_EXISTS;
324using ::isinf _LIBCPP_USING_IF_EXISTS;
325using ::isnan _LIBCPP_USING_IF_EXISTS;
326using ::isnormal _LIBCPP_USING_IF_EXISTS;
327using ::isgreater _LIBCPP_USING_IF_EXISTS;
328using ::isgreaterequal _LIBCPP_USING_IF_EXISTS;
329using ::isless _LIBCPP_USING_IF_EXISTS;
330using ::islessequal _LIBCPP_USING_IF_EXISTS;
331using ::islessgreater _LIBCPP_USING_IF_EXISTS;
332using ::isunordered _LIBCPP_USING_IF_EXISTS;
333using ::isunordered _LIBCPP_USING_IF_EXISTS;
334
335using ::float_t _LIBCPP_USING_IF_EXISTS;
336using ::double_t _LIBCPP_USING_IF_EXISTS;
337
338using ::abs _LIBCPP_USING_IF_EXISTS;
339
340using ::acos _LIBCPP_USING_IF_EXISTS;
341using ::acosf _LIBCPP_USING_IF_EXISTS;
342using ::asin _LIBCPP_USING_IF_EXISTS;
343using ::asinf _LIBCPP_USING_IF_EXISTS;
344using ::atan _LIBCPP_USING_IF_EXISTS;
345using ::atanf _LIBCPP_USING_IF_EXISTS;
346using ::atan2 _LIBCPP_USING_IF_EXISTS;
347using ::atan2f _LIBCPP_USING_IF_EXISTS;
348using ::ceil _LIBCPP_USING_IF_EXISTS;
349using ::ceilf _LIBCPP_USING_IF_EXISTS;
350using ::cos _LIBCPP_USING_IF_EXISTS;
351using ::cosf _LIBCPP_USING_IF_EXISTS;
352using ::cosh _LIBCPP_USING_IF_EXISTS;
353using ::coshf _LIBCPP_USING_IF_EXISTS;
354
355using ::exp _LIBCPP_USING_IF_EXISTS;
356using ::expf _LIBCPP_USING_IF_EXISTS;
357
358using ::fabs _LIBCPP_USING_IF_EXISTS;
359using ::fabsf _LIBCPP_USING_IF_EXISTS;
360using ::floor _LIBCPP_USING_IF_EXISTS;
361using ::floorf _LIBCPP_USING_IF_EXISTS;
362
363using ::fmod _LIBCPP_USING_IF_EXISTS;
364using ::fmodf _LIBCPP_USING_IF_EXISTS;
365
366using ::frexp _LIBCPP_USING_IF_EXISTS;
367using ::frexpf _LIBCPP_USING_IF_EXISTS;
368using ::ldexp _LIBCPP_USING_IF_EXISTS;
369using ::ldexpf _LIBCPP_USING_IF_EXISTS;
370
371using ::log _LIBCPP_USING_IF_EXISTS;
372using ::logf _LIBCPP_USING_IF_EXISTS;
373
374using ::log10 _LIBCPP_USING_IF_EXISTS;
375using ::log10f _LIBCPP_USING_IF_EXISTS;
376using ::modf _LIBCPP_USING_IF_EXISTS;
377using ::modff _LIBCPP_USING_IF_EXISTS;
378
379using ::pow _LIBCPP_USING_IF_EXISTS;
380using ::powf _LIBCPP_USING_IF_EXISTS;
381
382using ::sin _LIBCPP_USING_IF_EXISTS;
383using ::sinf _LIBCPP_USING_IF_EXISTS;
384using ::sinh _LIBCPP_USING_IF_EXISTS;
385using ::sinhf _LIBCPP_USING_IF_EXISTS;
386
387using ::sqrt _LIBCPP_USING_IF_EXISTS;
388using ::sqrtf _LIBCPP_USING_IF_EXISTS;
389using ::tan _LIBCPP_USING_IF_EXISTS;
390using ::tanf _LIBCPP_USING_IF_EXISTS;
391
392using ::tanh _LIBCPP_USING_IF_EXISTS;
393using ::tanhf _LIBCPP_USING_IF_EXISTS;
394
395using ::acosh _LIBCPP_USING_IF_EXISTS;
396using ::acoshf _LIBCPP_USING_IF_EXISTS;
397using ::asinh _LIBCPP_USING_IF_EXISTS;
398using ::asinhf _LIBCPP_USING_IF_EXISTS;
399using ::atanh _LIBCPP_USING_IF_EXISTS;
400using ::atanhf _LIBCPP_USING_IF_EXISTS;
401using ::cbrt _LIBCPP_USING_IF_EXISTS;
402using ::cbrtf _LIBCPP_USING_IF_EXISTS;
403
404using ::copysign _LIBCPP_USING_IF_EXISTS;
405using ::copysignf _LIBCPP_USING_IF_EXISTS;
406
407using ::erf _LIBCPP_USING_IF_EXISTS;
408using ::erff _LIBCPP_USING_IF_EXISTS;
409using ::erfc _LIBCPP_USING_IF_EXISTS;
410using ::erfcf _LIBCPP_USING_IF_EXISTS;
411using ::exp2 _LIBCPP_USING_IF_EXISTS;
412using ::exp2f _LIBCPP_USING_IF_EXISTS;
413using ::expm1 _LIBCPP_USING_IF_EXISTS;
414using ::expm1f _LIBCPP_USING_IF_EXISTS;
415using ::fdim _LIBCPP_USING_IF_EXISTS;
416using ::fdimf _LIBCPP_USING_IF_EXISTS;
417using ::fmaf _LIBCPP_USING_IF_EXISTS;
418using ::fma _LIBCPP_USING_IF_EXISTS;
419using ::fmax _LIBCPP_USING_IF_EXISTS;
420using ::fmaxf _LIBCPP_USING_IF_EXISTS;
421using ::fmin _LIBCPP_USING_IF_EXISTS;
422using ::fminf _LIBCPP_USING_IF_EXISTS;
423using ::hypot _LIBCPP_USING_IF_EXISTS;
424using ::hypotf _LIBCPP_USING_IF_EXISTS;
425using ::ilogb _LIBCPP_USING_IF_EXISTS;
426using ::ilogbf _LIBCPP_USING_IF_EXISTS;
427using ::lgamma _LIBCPP_USING_IF_EXISTS;
428using ::lgammaf _LIBCPP_USING_IF_EXISTS;
429using ::llrint _LIBCPP_USING_IF_EXISTS;
430using ::llrintf _LIBCPP_USING_IF_EXISTS;
431using ::llround _LIBCPP_USING_IF_EXISTS;
432using ::llroundf _LIBCPP_USING_IF_EXISTS;
433using ::log1p _LIBCPP_USING_IF_EXISTS;
434using ::log1pf _LIBCPP_USING_IF_EXISTS;
435using ::log2 _LIBCPP_USING_IF_EXISTS;
436using ::log2f _LIBCPP_USING_IF_EXISTS;
437using ::logb _LIBCPP_USING_IF_EXISTS;
438using ::logbf _LIBCPP_USING_IF_EXISTS;
439using ::lrint _LIBCPP_USING_IF_EXISTS;
440using ::lrintf _LIBCPP_USING_IF_EXISTS;
441using ::lround _LIBCPP_USING_IF_EXISTS;
442using ::lroundf _LIBCPP_USING_IF_EXISTS;
443
444using ::nan _LIBCPP_USING_IF_EXISTS;
445using ::nanf _LIBCPP_USING_IF_EXISTS;
446
447using ::nearbyint _LIBCPP_USING_IF_EXISTS;
448using ::nearbyintf _LIBCPP_USING_IF_EXISTS;
449using ::nextafter _LIBCPP_USING_IF_EXISTS;
450using ::nextafterf _LIBCPP_USING_IF_EXISTS;
451using ::nexttoward _LIBCPP_USING_IF_EXISTS;
452using ::nexttowardf _LIBCPP_USING_IF_EXISTS;
453using ::remainder _LIBCPP_USING_IF_EXISTS;
454using ::remainderf _LIBCPP_USING_IF_EXISTS;
455using ::remquo _LIBCPP_USING_IF_EXISTS;
456using ::remquof _LIBCPP_USING_IF_EXISTS;
457using ::rint _LIBCPP_USING_IF_EXISTS;
458using ::rintf _LIBCPP_USING_IF_EXISTS;
459using ::round _LIBCPP_USING_IF_EXISTS;
460using ::roundf _LIBCPP_USING_IF_EXISTS;
461using ::scalbln _LIBCPP_USING_IF_EXISTS;
462using ::scalblnf _LIBCPP_USING_IF_EXISTS;
463using ::scalbn _LIBCPP_USING_IF_EXISTS;
464using ::scalbnf _LIBCPP_USING_IF_EXISTS;
465using ::tgamma _LIBCPP_USING_IF_EXISTS;
466using ::tgammaf _LIBCPP_USING_IF_EXISTS;
467using ::trunc _LIBCPP_USING_IF_EXISTS;
468using ::truncf _LIBCPP_USING_IF_EXISTS;
469
470using ::acosl _LIBCPP_USING_IF_EXISTS;
471using ::asinl _LIBCPP_USING_IF_EXISTS;
472using ::atanl _LIBCPP_USING_IF_EXISTS;
473using ::atan2l _LIBCPP_USING_IF_EXISTS;
474using ::ceill _LIBCPP_USING_IF_EXISTS;
475using ::cosl _LIBCPP_USING_IF_EXISTS;
476using ::coshl _LIBCPP_USING_IF_EXISTS;
477using ::expl _LIBCPP_USING_IF_EXISTS;
478using ::fabsl _LIBCPP_USING_IF_EXISTS;
479using ::floorl _LIBCPP_USING_IF_EXISTS;
480using ::fmodl _LIBCPP_USING_IF_EXISTS;
481using ::frexpl _LIBCPP_USING_IF_EXISTS;
482using ::ldexpl _LIBCPP_USING_IF_EXISTS;
483using ::logl _LIBCPP_USING_IF_EXISTS;
484using ::log10l _LIBCPP_USING_IF_EXISTS;
485using ::modfl _LIBCPP_USING_IF_EXISTS;
486using ::powl _LIBCPP_USING_IF_EXISTS;
487using ::sinl _LIBCPP_USING_IF_EXISTS;
488using ::sinhl _LIBCPP_USING_IF_EXISTS;
489using ::sqrtl _LIBCPP_USING_IF_EXISTS;
490using ::tanl _LIBCPP_USING_IF_EXISTS;
491
492using ::tanhl _LIBCPP_USING_IF_EXISTS;
493using ::acoshl _LIBCPP_USING_IF_EXISTS;
494using ::asinhl _LIBCPP_USING_IF_EXISTS;
495using ::atanhl _LIBCPP_USING_IF_EXISTS;
496using ::cbrtl _LIBCPP_USING_IF_EXISTS;
497
498using ::copysignl _LIBCPP_USING_IF_EXISTS;
499
500using ::erfl _LIBCPP_USING_IF_EXISTS;
501using ::erfcl _LIBCPP_USING_IF_EXISTS;
502using ::exp2l _LIBCPP_USING_IF_EXISTS;
503using ::expm1l _LIBCPP_USING_IF_EXISTS;
504using ::fdiml _LIBCPP_USING_IF_EXISTS;
505using ::fmal _LIBCPP_USING_IF_EXISTS;
506using ::fmaxl _LIBCPP_USING_IF_EXISTS;
507using ::fminl _LIBCPP_USING_IF_EXISTS;
508using ::hypotl _LIBCPP_USING_IF_EXISTS;
509using ::ilogbl _LIBCPP_USING_IF_EXISTS;
510using ::lgammal _LIBCPP_USING_IF_EXISTS;
511using ::llrintl _LIBCPP_USING_IF_EXISTS;
512using ::llroundl _LIBCPP_USING_IF_EXISTS;
513using ::log1pl _LIBCPP_USING_IF_EXISTS;
514using ::log2l _LIBCPP_USING_IF_EXISTS;
515using ::logbl _LIBCPP_USING_IF_EXISTS;
516using ::lrintl _LIBCPP_USING_IF_EXISTS;
517using ::lroundl _LIBCPP_USING_IF_EXISTS;
518using ::nanl _LIBCPP_USING_IF_EXISTS;
519using ::nearbyintl _LIBCPP_USING_IF_EXISTS;
520using ::nextafterl _LIBCPP_USING_IF_EXISTS;
521using ::nexttowardl _LIBCPP_USING_IF_EXISTS;
522using ::remainderl _LIBCPP_USING_IF_EXISTS;
523using ::remquol _LIBCPP_USING_IF_EXISTS;
524using ::rintl _LIBCPP_USING_IF_EXISTS;
525using ::roundl _LIBCPP_USING_IF_EXISTS;
526using ::scalblnl _LIBCPP_USING_IF_EXISTS;
527using ::scalbnl _LIBCPP_USING_IF_EXISTS;
528using ::tgammal _LIBCPP_USING_IF_EXISTS;
529using ::truncl _LIBCPP_USING_IF_EXISTS;
530
531#if _LIBCPP_STD_VER > 14
532inline _LIBCPP_INLINE_VISIBILITY float       hypot(       float x,       float y,       float z ) { return sqrt(x*x + y*y + z*z); }
533inline _LIBCPP_INLINE_VISIBILITY double      hypot(      double x,      double y,      double z ) { return sqrt(x*x + y*y + z*z); }
534inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); }
535
536template <class _A1, class _A2, class _A3>
537inline _LIBCPP_INLINE_VISIBILITY
538typename _EnableIf
539<
540    is_arithmetic<_A1>::value &&
541    is_arithmetic<_A2>::value &&
542    is_arithmetic<_A3>::value,
543    __promote<_A1, _A2, _A3>
544>::type
545hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT
546{
547    typedef typename __promote<_A1, _A2, _A3>::type __result_type;
548    static_assert((!(is_same<_A1, __result_type>::value &&
549                     is_same<_A2, __result_type>::value &&
550                     is_same<_A3, __result_type>::value)), "");
551    return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
552}
553#endif
554
555template <class _A1>
556_LIBCPP_INLINE_VISIBILITY
557_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
558__libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
559{
560#if __has_builtin(__builtin_isnan)
561    return __builtin_isnan(__lcpp_x);
562#else
563    return isnan(__lcpp_x);
564#endif
565}
566
567template <class _A1>
568_LIBCPP_INLINE_VISIBILITY
569_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
570__libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
571{
572    return isnan(__lcpp_x);
573}
574
575template <class _A1>
576_LIBCPP_INLINE_VISIBILITY
577_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
578__libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
579{
580#if __has_builtin(__builtin_isinf)
581    return __builtin_isinf(__lcpp_x);
582#else
583    return isinf(__lcpp_x);
584#endif
585}
586
587template <class _A1>
588_LIBCPP_INLINE_VISIBILITY
589_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
590__libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
591{
592    return isinf(__lcpp_x);
593}
594
595template <class _A1>
596_LIBCPP_INLINE_VISIBILITY
597_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
598__libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
599{
600#if __has_builtin(__builtin_isfinite)
601    return __builtin_isfinite(__lcpp_x);
602#else
603    return isfinite(__lcpp_x);
604#endif
605}
606
607template <class _A1>
608_LIBCPP_INLINE_VISIBILITY
609_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
610__libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
611{
612    return isfinite(__lcpp_x);
613}
614
615#if _LIBCPP_STD_VER > 17
616template <typename _Fp>
617constexpr
618_Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
619    if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
620        return __t * __b + (1 - __t) * __a;
621
622    if (__t == 1) return __b;
623    const _Fp __x = __a + __t * (__b - __a);
624    if ((__t > 1) == (__b > __a))
625        return __b < __x ? __x : __b;
626    else
627        return __x < __b ? __x : __b;
628}
629
630constexpr float
631lerp(float __a, float __b, float __t)                   _NOEXCEPT { return __lerp(__a, __b, __t); }
632
633constexpr double
634lerp(double __a, double __b, double __t)                _NOEXCEPT { return __lerp(__a, __b, __t); }
635
636constexpr long double
637lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
638
639#endif // _LIBCPP_STD_VER > 17
640
641template <class _IntT, class _FloatT,
642    bool _FloatBigger = (numeric_limits<_FloatT>::digits > numeric_limits<_IntT>::digits),
643    int _Bits = (numeric_limits<_IntT>::digits - numeric_limits<_FloatT>::digits)>
644_LIBCPP_INLINE_VISIBILITY
645_LIBCPP_CONSTEXPR _IntT __max_representable_int_for_float() _NOEXCEPT {
646  static_assert(is_floating_point<_FloatT>::value, "must be a floating point type");
647  static_assert(is_integral<_IntT>::value, "must be an integral type");
648  static_assert(numeric_limits<_FloatT>::radix == 2, "FloatT has incorrect radix");
649  static_assert((_IsSame<_FloatT, float>::value || _IsSame<_FloatT, double>::value
650                 || _IsSame<_FloatT,long double>::value), "unsupported floating point type");
651  return _FloatBigger ? numeric_limits<_IntT>::max() :  (numeric_limits<_IntT>::max() >> _Bits << _Bits);
652}
653
654// Convert a floating point number to the specified integral type after
655// clamping to the integral types representable range.
656//
657// The behavior is undefined if `__r` is NaN.
658template <class _IntT, class _RealT>
659_LIBCPP_INLINE_VISIBILITY
660_IntT __clamp_to_integral(_RealT __r) _NOEXCEPT {
661  using _Lim = numeric_limits<_IntT>;
662  const _IntT _MaxVal = __max_representable_int_for_float<_IntT, _RealT>();
663  if (__r >= ::nextafter(static_cast<_RealT>(_MaxVal), INFINITY)) {
664    return _Lim::max();
665  } else if (__r <= _Lim::lowest()) {
666    return _Lim::min();
667  }
668  return static_cast<_IntT>(__r);
669}
670
671_LIBCPP_END_NAMESPACE_STD
672
673_LIBCPP_POP_MACROS
674
675#endif // _LIBCPP_CMATH
676