1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file include/cmath
28 * This is a Standard C++ Library file. You should @c #include this file
29 * in your programs, rather than any of the @a *.h implementation files.
31 * This is the C++ version of the Standard C Library header @c math.h,
32 * and its contents are (mostly) the same as that header, but are all
33 * contained in the namespace @c std (except for names which are defined
38 // ISO C++ 14882: 26.5 C library
41 #ifndef _GLIBCXX_CMATH
42 #define _GLIBCXX_CMATH 1
44 #pragma GCC system_header
46 #include <bits/c++config.h>
47 #include <bits/cpp_type_traits.h>
48 #include <ext/type_traits.h>
52 // Get rid of those macros defined in <math.h> in lieu of real functions.
78 _GLIBCXX_BEGIN_NAMESPACE(std)
80 // Forward declaration of a helper function. This really should be
81 // an `exported' forward declaration.
82 template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
86 { return __builtin_fabs(__x); }
90 { return __builtin_fabsf(__x); }
94 { return __builtin_fabsl(__x); }
96 template<typename _Tp>
97 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
100 { return __builtin_fabs(__x); }
106 { return __builtin_acosf(__x); }
109 acos(long double __x)
110 { return __builtin_acosl(__x); }
112 template<typename _Tp>
113 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
116 { return __builtin_acos(__x); }
122 { return __builtin_asinf(__x); }
125 asin(long double __x)
126 { return __builtin_asinl(__x); }
128 template<typename _Tp>
129 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
132 { return __builtin_asin(__x); }
138 { return __builtin_atanf(__x); }
141 atan(long double __x)
142 { return __builtin_atanl(__x); }
144 template<typename _Tp>
145 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
148 { return __builtin_atan(__x); }
153 atan2(float __y, float __x)
154 { return __builtin_atan2f(__y, __x); }
157 atan2(long double __y, long double __x)
158 { return __builtin_atan2l(__y, __x); }
160 template<typename _Tp, typename _Up>
161 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value
162 && __is_integer<_Up>::__value,
164 atan2(_Tp __y, _Up __x)
165 { return __builtin_atan2(__y, __x); }
171 { return __builtin_ceilf(__x); }
174 ceil(long double __x)
175 { return __builtin_ceill(__x); }
177 template<typename _Tp>
178 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
181 { return __builtin_ceil(__x); }
187 { return __builtin_cosf(__x); }
191 { return __builtin_cosl(__x); }
193 template<typename _Tp>
194 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
197 { return __builtin_cos(__x); }
203 { return __builtin_coshf(__x); }
206 cosh(long double __x)
207 { return __builtin_coshl(__x); }
209 template<typename _Tp>
210 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
213 { return __builtin_cosh(__x); }
219 { return __builtin_expf(__x); }
223 { return __builtin_expl(__x); }
225 template<typename _Tp>
226 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
229 { return __builtin_exp(__x); }
235 { return __builtin_fabsf(__x); }
238 fabs(long double __x)
239 { return __builtin_fabsl(__x); }
241 template<typename _Tp>
242 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
245 { return __builtin_fabs(__x); }
251 { return __builtin_floorf(__x); }
254 floor(long double __x)
255 { return __builtin_floorl(__x); }
257 template<typename _Tp>
258 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
261 { return __builtin_floor(__x); }
266 fmod(float __x, float __y)
267 { return __builtin_fmodf(__x, __y); }
270 fmod(long double __x, long double __y)
271 { return __builtin_fmodl(__x, __y); }
276 frexp(float __x, int* __exp)
277 { return __builtin_frexpf(__x, __exp); }
280 frexp(long double __x, int* __exp)
281 { return __builtin_frexpl(__x, __exp); }
283 template<typename _Tp>
284 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
286 frexp(_Tp __x, int* __exp)
287 { return __builtin_frexp(__x, __exp); }
292 ldexp(float __x, int __exp)
293 { return __builtin_ldexpf(__x, __exp); }
296 ldexp(long double __x, int __exp)
297 { return __builtin_ldexpl(__x, __exp); }
299 template<typename _Tp>
300 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
302 ldexp(_Tp __x, int __exp)
303 { return __builtin_ldexp(__x, __exp); }
309 { return __builtin_logf(__x); }
313 { return __builtin_logl(__x); }
315 template<typename _Tp>
316 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
319 { return __builtin_log(__x); }
325 { return __builtin_log10f(__x); }
328 log10(long double __x)
329 { return __builtin_log10l(__x); }
331 template<typename _Tp>
332 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
335 { return __builtin_log10(__x); }
340 modf(float __x, float* __iptr)
341 { return __builtin_modff(__x, __iptr); }
344 modf(long double __x, long double* __iptr)
345 { return __builtin_modfl(__x, __iptr); }
347 template<typename _Tp>
349 __pow_helper(_Tp __x, int __n)
352 ? _Tp(1)/__cmath_power(__x, -__n)
353 : __cmath_power(__x, __n);
359 pow(float __x, float __y)
360 { return __builtin_powf(__x, __y); }
363 pow(long double __x, long double __y)
364 { return __builtin_powl(__x, __y); }
367 pow(double __x, int __i)
368 { return __builtin_powi(__x, __i); }
371 pow(float __x, int __n)
372 { return __builtin_powif(__x, __n); }
375 pow(long double __x, int __n)
376 { return __builtin_powil(__x, __n); }
382 { return __builtin_sinf(__x); }
386 { return __builtin_sinl(__x); }
388 template<typename _Tp>
389 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
392 { return __builtin_sin(__x); }
398 { return __builtin_sinhf(__x); }
401 sinh(long double __x)
402 { return __builtin_sinhl(__x); }
404 template<typename _Tp>
405 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
408 { return __builtin_sinh(__x); }
414 { return __builtin_sqrtf(__x); }
417 sqrt(long double __x)
418 { return __builtin_sqrtl(__x); }
420 template<typename _Tp>
421 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
424 { return __builtin_sqrt(__x); }
430 { return __builtin_tanf(__x); }
434 { return __builtin_tanl(__x); }
436 template<typename _Tp>
437 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
440 { return __builtin_tan(__x); }
446 { return __builtin_tanhf(__x); }
449 tanh(long double __x)
450 { return __builtin_tanhl(__x); }
452 template<typename _Tp>
453 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
456 { return __builtin_tanh(__x); }
458 _GLIBCXX_END_NAMESPACE
460 #if _GLIBCXX_USE_C99_MATH
461 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
463 // These are possible macros imported from C99-land.
471 #undef isgreaterequal
477 _GLIBCXX_BEGIN_NAMESPACE(std)
479 template<typename _Tp>
480 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
484 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
485 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
486 FP_SUBNORMAL, FP_ZERO, __type(__f));
489 template<typename _Tp>
490 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
494 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
495 return __builtin_isfinite(__type(__f));
498 template<typename _Tp>
499 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
503 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
504 return __builtin_isinf(__type(__f));
507 template<typename _Tp>
508 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
512 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
513 return __builtin_isnan(__type(__f));
516 template<typename _Tp>
517 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
521 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
522 return __builtin_isnormal(__type(__f));
525 template<typename _Tp>
526 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
530 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
531 return __builtin_signbit(__type(__f));
534 template<typename _Tp>
535 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
537 isgreater(_Tp __f1, _Tp __f2)
539 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
540 return __builtin_isgreater(__type(__f1), __type(__f2));
543 template<typename _Tp>
544 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
546 isgreaterequal(_Tp __f1, _Tp __f2)
548 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
549 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
552 template<typename _Tp>
553 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
555 isless(_Tp __f1, _Tp __f2)
557 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
558 return __builtin_isless(__type(__f1), __type(__f2));
561 template<typename _Tp>
562 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
564 islessequal(_Tp __f1, _Tp __f2)
566 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
567 return __builtin_islessequal(__type(__f1), __type(__f2));
570 template<typename _Tp>
571 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
573 islessgreater(_Tp __f1, _Tp __f2)
575 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
576 return __builtin_islessgreater(__type(__f1), __type(__f2));
579 template<typename _Tp>
580 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
582 isunordered(_Tp __f1, _Tp __f2)
584 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
585 return __builtin_isunordered(__type(__f1), __type(__f2));
588 _GLIBCXX_END_NAMESPACE
590 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
593 #ifndef _GLIBCXX_EXPORT_TEMPLATE
594 # include <bits/cmath.tcc>