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