1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file include/cmath
33 * This is a Standard C++ Library file. You should @c #include this file
34 * in your programs, rather than any of the "*.h" implementation files.
36 * This is the C++ version of the Standard C Library header @c math.h,
37 * and its contents are (mostly) the same as that header, but are all
38 * contained in the namespace @c std (except for names which are defined
43 // ISO C++ 14882: 26.5 C library
46 #ifndef _GLIBCXX_CMATH
47 #define _GLIBCXX_CMATH 1
49 #pragma GCC system_header
51 #include <bits/c++config.h>
52 #include <bits/cpp_type_traits.h>
53 #include <ext/type_traits.h>
57 // Get rid of those macros defined in <math.h> in lieu of real functions.
83 _GLIBCXX_BEGIN_NAMESPACE(std)
85 // Forward declaration of a helper function. This really should be
86 // an `exported' forward declaration.
87 template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
91 { return __builtin_fabs(__x); }
95 { return __builtin_fabsf(__x); }
99 { return __builtin_fabsl(__x); }
105 { return __builtin_acosf(__x); }
108 acos(long double __x)
109 { return __builtin_acosl(__x); }
111 template<typename _Tp>
112 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
115 { return __builtin_acos(__x); }
121 { return __builtin_asinf(__x); }
124 asin(long double __x)
125 { return __builtin_asinl(__x); }
127 template<typename _Tp>
128 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
131 { return __builtin_asin(__x); }
137 { return __builtin_atanf(__x); }
140 atan(long double __x)
141 { return __builtin_atanl(__x); }
143 template<typename _Tp>
144 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
147 { return __builtin_atan(__x); }
152 atan2(float __y, float __x)
153 { return __builtin_atan2f(__y, __x); }
156 atan2(long double __y, long double __x)
157 { return __builtin_atan2l(__y, __x); }
159 template<typename _Tp, typename _Up>
161 typename __gnu_cxx::__enable_if<__traitand<__is_integer<_Tp>,
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
462 // These are possible macros imported from C99-land. For strict
463 // conformance, remove possible C99-injected names from the global
464 // namespace, and sequester them in the __gnu_cxx extension namespace.
466 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
468 template<typename _Tp>
470 __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
472 _GLIBCXX_END_NAMESPACE
474 // Only undefine the C99 FP macros, if actually captured for namespace movement
482 #undef isgreaterequal
488 _GLIBCXX_BEGIN_NAMESPACE(std)
490 template<typename _Tp>
491 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
495 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
496 return ::__gnu_cxx::__capture_fpclassify(__type(__f));
499 template<typename _Tp>
500 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
504 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
505 return __builtin_isfinite(__type(__f));
508 template<typename _Tp>
509 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
513 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
514 return __builtin_isinf(__type(__f));
517 template<typename _Tp>
518 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
522 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
523 return __builtin_isnan(__type(__f));
526 template<typename _Tp>
527 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
531 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
532 return __builtin_isnormal(__type(__f));
535 template<typename _Tp>
536 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
540 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
541 return __builtin_signbit(__type(__f));
544 template<typename _Tp>
545 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
547 isgreater(_Tp __f1, _Tp __f2)
549 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
550 return __builtin_isgreater(__type(__f1), __type(__f2));
553 template<typename _Tp>
554 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
556 isgreaterequal(_Tp __f1, _Tp __f2)
558 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
559 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
562 template<typename _Tp>
563 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
565 isless(_Tp __f1, _Tp __f2)
567 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
568 return __builtin_isless(__type(__f1), __type(__f2));
571 template<typename _Tp>
572 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
574 islessequal(_Tp __f1, _Tp __f2)
576 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
577 return __builtin_islessequal(__type(__f1), __type(__f2));
580 template<typename _Tp>
581 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
583 islessgreater(_Tp __f1, _Tp __f2)
585 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
586 return __builtin_islessgreater(__type(__f1), __type(__f2));
589 template<typename _Tp>
590 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
592 isunordered(_Tp __f1, _Tp __f2)
594 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
595 return __builtin_isunordered(__type(__f1), __type(__f2));
598 _GLIBCXX_END_NAMESPACE
600 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
603 #ifndef _GLIBCXX_EXPORT_TEMPLATE
604 # include <bits/cmath.tcc>