blob: 0f06486fb34f3ecb721973dfad86072845b96236 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- cmath -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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
Howard Hinnante95552c2013-01-14 20:56:22 +0000139bool signbit(arithmetic x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
Howard Hinnante95552c2013-01-14 20:56:22 +0000141int fpclassify(arithmetic x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnante95552c2013-01-14 20:56:22 +0000143bool isfinite(arithmetic x);
144bool isinf(arithmetic x);
145bool isnan(arithmetic x);
146bool isnormal(arithmetic x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnante95552c2013-01-14 20:56:22 +0000148bool 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);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
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
Marshall Clowe52b0022016-05-17 14:52:19 +0000211double 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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215int 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
299} // std
300
301*/
302
303#include <__config>
304#include <math.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000305#include <version>
Louis Dionne4000a2a2019-09-04 13:35:03 +0000306#include <type_traits>
Howard Hinnant69d80702011-10-27 16:24:42 +0000307
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000308#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311
Louis Dionne4000a2a2019-09-04 13:35:03 +0000312_LIBCPP_PUSH_MACROS
313#include <__undef_macros>
314
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315_LIBCPP_BEGIN_NAMESPACE_STD
316
Howard Hinnantc8827ba2011-05-13 21:52:40 +0000317using ::signbit;
318using ::fpclassify;
319using ::isfinite;
320using ::isinf;
321using ::isnan;
322using ::isnormal;
323using ::isgreater;
324using ::isgreaterequal;
325using ::isless;
326using ::islessequal;
327using ::islessgreater;
328using ::isunordered;
329using ::isunordered;
330
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331using ::float_t;
332using ::double_t;
333
Richard Smith081bb592015-10-08 20:40:34 +0000334#ifndef _AIX
Eric Fiseliereaad27b2015-03-18 15:24:18 +0000335using ::abs;
336#endif
337
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338using ::acos;
339using ::acosf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340using ::asin;
341using ::asinf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342using ::atan;
343using ::atanf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344using ::atan2;
345using ::atan2f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346using ::ceil;
347using ::ceilf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348using ::cos;
349using ::cosf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350using ::cosh;
351using ::coshf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352
353using ::exp;
354using ::expf;
355
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356using ::fabs;
357using ::fabsf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358using ::floor;
359using ::floorf;
Richard Smith081bb592015-10-08 20:40:34 +0000360
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361using ::fmod;
362using ::fmodf;
Richard Smith081bb592015-10-08 20:40:34 +0000363
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364using ::frexp;
365using ::frexpf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366using ::ldexp;
367using ::ldexpf;
Richard Smith081bb592015-10-08 20:40:34 +0000368
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369using ::log;
370using ::logf;
Richard Smith081bb592015-10-08 20:40:34 +0000371
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372using ::log10;
373using ::log10f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374using ::modf;
375using ::modff;
Richard Smith081bb592015-10-08 20:40:34 +0000376
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377using ::pow;
378using ::powf;
379
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380using ::sin;
381using ::sinf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382using ::sinh;
383using ::sinhf;
Richard Smith081bb592015-10-08 20:40:34 +0000384
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385using ::sqrt;
386using ::sqrtf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387using ::tan;
388using ::tanf;
Richard Smith081bb592015-10-08 20:40:34 +0000389
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390using ::tanh;
391using ::tanhf;
392
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393using ::acosh;
394using ::acoshf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395using ::asinh;
396using ::asinhf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397using ::atanh;
398using ::atanhf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399using ::cbrt;
400using ::cbrtf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402using ::copysign;
403using ::copysignf;
404
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405using ::erf;
406using ::erff;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407using ::erfc;
408using ::erfcf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409using ::exp2;
410using ::exp2f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411using ::expm1;
412using ::expm1f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413using ::fdim;
414using ::fdimf;
Marshall Clow48313ff2014-03-05 17:09:51 +0000415using ::fmaf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416using ::fma;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417using ::fmax;
418using ::fmaxf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419using ::fmin;
420using ::fminf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421using ::hypot;
422using ::hypotf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423using ::ilogb;
424using ::ilogbf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425using ::lgamma;
426using ::lgammaf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427using ::llrint;
428using ::llrintf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429using ::llround;
430using ::llroundf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431using ::log1p;
432using ::log1pf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433using ::log2;
434using ::log2f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435using ::logb;
436using ::logbf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437using ::lrint;
438using ::lrintf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439using ::lround;
440using ::lroundf;
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000441
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442using ::nan;
443using ::nanf;
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000444
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445using ::nearbyint;
446using ::nearbyintf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447using ::nextafter;
448using ::nextafterf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449using ::nexttoward;
450using ::nexttowardf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451using ::remainder;
452using ::remainderf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453using ::remquo;
454using ::remquof;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455using ::rint;
456using ::rintf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457using ::round;
458using ::roundf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459using ::scalbln;
460using ::scalblnf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461using ::scalbn;
462using ::scalbnf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463using ::tgamma;
464using ::tgammaf;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465using ::trunc;
466using ::truncf;
Howard Hinnant69d80702011-10-27 16:24:42 +0000467
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468using ::acosl;
469using ::asinl;
470using ::atanl;
471using ::atan2l;
472using ::ceill;
473using ::cosl;
474using ::coshl;
475using ::expl;
476using ::fabsl;
477using ::floorl;
478using ::fmodl;
479using ::frexpl;
480using ::ldexpl;
481using ::logl;
482using ::log10l;
483using ::modfl;
484using ::powl;
485using ::sinl;
486using ::sinhl;
487using ::sqrtl;
488using ::tanl;
Richard Smith081bb592015-10-08 20:40:34 +0000489
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490using ::tanhl;
491using ::acoshl;
492using ::asinhl;
493using ::atanhl;
494using ::cbrtl;
Richard Smith081bb592015-10-08 20:40:34 +0000495
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496using ::copysignl;
Richard Smith081bb592015-10-08 20:40:34 +0000497
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498using ::erfl;
499using ::erfcl;
500using ::exp2l;
501using ::expm1l;
502using ::fdiml;
503using ::fmal;
504using ::fmaxl;
505using ::fminl;
506using ::hypotl;
507using ::ilogbl;
508using ::lgammal;
509using ::llrintl;
510using ::llroundl;
511using ::log1pl;
512using ::log2l;
513using ::logbl;
514using ::lrintl;
515using ::lroundl;
516using ::nanl;
517using ::nearbyintl;
518using ::nextafterl;
519using ::nexttowardl;
520using ::remainderl;
521using ::remquol;
522using ::rintl;
523using ::roundl;
524using ::scalblnl;
525using ::scalbnl;
526using ::tgammal;
527using ::truncl;
528
Marshall Clowe52b0022016-05-17 14:52:19 +0000529#if _LIBCPP_STD_VER > 14
530inline _LIBCPP_INLINE_VISIBILITY float hypot( float x, float y, float z ) { return sqrt(x*x + y*y + z*z); }
531inline _LIBCPP_INLINE_VISIBILITY double hypot( double x, double y, double z ) { return sqrt(x*x + y*y + z*z); }
532inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); }
533
534template <class _A1, class _A2, class _A3>
535inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +0000536typename _EnableIf
Marshall Clowe52b0022016-05-17 14:52:19 +0000537<
Hal Finkelba7fef32016-10-01 20:38:44 +0000538 is_arithmetic<_A1>::value &&
539 is_arithmetic<_A2>::value &&
540 is_arithmetic<_A3>::value,
541 __promote<_A1, _A2, _A3>
Marshall Clowe52b0022016-05-17 14:52:19 +0000542>::type
543hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT
544{
Hal Finkelba7fef32016-10-01 20:38:44 +0000545 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)), "");
Marshall Clowe52b0022016-05-17 14:52:19 +0000549 return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
550}
551#endif
552
Hal Finkel5d741202016-10-01 20:38:31 +0000553template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000554_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000555_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000556__libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000557{
558#if __has_builtin(__builtin_isnan)
559 return __builtin_isnan(__lcpp_x);
560#else
561 return isnan(__lcpp_x);
562#endif
563}
564
565template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000566_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000567_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000568__libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000569{
570 return isnan(__lcpp_x);
571}
572
573template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000574_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000575_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000576__libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000577{
578#if __has_builtin(__builtin_isinf)
579 return __builtin_isinf(__lcpp_x);
580#else
581 return isinf(__lcpp_x);
582#endif
583}
584
585template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000586_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000587_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000588__libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000589{
590 return isinf(__lcpp_x);
591}
592
593template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000594_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000595_LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000596__libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000597{
598#if __has_builtin(__builtin_isfinite)
599 return __builtin_isfinite(__lcpp_x);
600#else
601 return isfinite(__lcpp_x);
602#endif
603}
604
605template <class _A1>
Louis Dionne16fe2952018-07-11 23:14:33 +0000606_LIBCPP_INLINE_VISIBILITY
Justin Lebar24c1a072016-11-15 19:15:57 +0000607_LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type
Duncan P. N. Exon Smith4a297202017-07-07 05:13:36 +0000608__libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT
Hal Finkel5d741202016-10-01 20:38:31 +0000609{
610 return isfinite(__lcpp_x);
611}
612
Marshall Clowf5860ad2019-04-25 17:44:18 +0000613#if _LIBCPP_STD_VER > 17
614template <typename _Fp>
615constexpr
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
628constexpr float
629lerp(float __a, float __b, float __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
630
631constexpr double
632lerp(double __a, double __b, double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
633
634constexpr long double
635lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
636
637#endif // _LIBCPP_STD_VER > 17
638
Louis Dionne4000a2a2019-09-04 13:35:03 +0000639template <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");
Louis Dionneac4b2c42019-10-01 12:12:21 +0000647 static_assert((_IsSame<_FloatT, float>::value || _IsSame<_FloatT, double>::value
648 || _IsSame<_FloatT,long double>::value), "unsupported floating point type");
Louis Dionne4000a2a2019-09-04 13:35:03 +0000649 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.
656template <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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669_LIBCPP_END_NAMESPACE_STD
670
Louis Dionne4000a2a2019-09-04 13:35:03 +0000671_LIBCPP_POP_MACROS
672
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673#endif // _LIBCPP_CMATH