Merged revision 156805 into branch.
[official-gcc.git] / libstdc++-v3 / include / c_std / cmath
blobe97089664d9b47b41d0115d9cf4f38e2ac01ecd1
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.
6 //
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)
11 // any later version.
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.
30  *
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
34  *  as macros in C).
35  */
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>
50 #include <math.h>
52 // Get rid of those macros defined in <math.h> in lieu of real functions.
53 #undef abs
54 #undef div
55 #undef acos
56 #undef asin
57 #undef atan
58 #undef atan2
59 #undef ceil
60 #undef cos
61 #undef cosh
62 #undef exp
63 #undef fabs
64 #undef floor
65 #undef fmod
66 #undef frexp
67 #undef ldexp
68 #undef log
69 #undef log10
70 #undef modf
71 #undef pow
72 #undef sin
73 #undef sinh
74 #undef sqrt
75 #undef tan
76 #undef tanh
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);
84   inline double
85   abs(double __x)
86   { return __builtin_fabs(__x); }
88   inline float
89   abs(float __x)
90   { return __builtin_fabsf(__x); }
92   inline long double
93   abs(long double __x)
94   { return __builtin_fabsl(__x); }
96   template<typename _Tp>
97     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
98                                            double>::__type
99     abs(_Tp __x)
100     { return __builtin_fabs(__x); }
102   using ::acos;
104   inline float
105   acos(float __x)
106   { return __builtin_acosf(__x); }
108   inline long double
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, 
114                                            double>::__type
115     acos(_Tp __x)
116     { return __builtin_acos(__x); }
118   using ::asin;
120   inline float
121   asin(float __x)
122   { return __builtin_asinf(__x); }
124   inline long double
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,
130                                            double>::__type
131     asin(_Tp __x)
132     { return __builtin_asin(__x); }
134   using ::atan;
136   inline float
137   atan(float __x)
138   { return __builtin_atanf(__x); }
140   inline long double
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, 
146                                            double>::__type
147     atan(_Tp __x)
148     { return __builtin_atan(__x); }
150   using ::atan2;
152   inline float
153   atan2(float __y, float __x)
154   { return __builtin_atan2f(__y, __x); }
156   inline long double
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, 
163                                            double>::__type
164     atan2(_Tp __y, _Up __x)
165     { return __builtin_atan2(__y, __x); }
167   using ::ceil;
169   inline float
170   ceil(float __x)
171   { return __builtin_ceilf(__x); }
173   inline long double
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, 
179                                            double>::__type
180     ceil(_Tp __x)
181     { return __builtin_ceil(__x); }
183   using ::cos;
185   inline float
186   cos(float __x)
187   { return __builtin_cosf(__x); }
189   inline long double
190   cos(long double __x)
191   { return __builtin_cosl(__x); }
193   template<typename _Tp>
194     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
195                                            double>::__type
196     cos(_Tp __x)
197     { return __builtin_cos(__x); }
199   using ::cosh;
201   inline float
202   cosh(float __x)
203   { return __builtin_coshf(__x); }
205   inline long double
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, 
211                                            double>::__type
212     cosh(_Tp __x)
213     { return __builtin_cosh(__x); }
215   using ::exp;
217   inline float
218   exp(float __x)
219   { return __builtin_expf(__x); }
221   inline long double
222   exp(long double __x)
223   { return __builtin_expl(__x); }
225   template<typename _Tp>
226     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
227                                            double>::__type
228     exp(_Tp __x)
229     { return __builtin_exp(__x); }
231   using ::fabs;
233   inline float
234   fabs(float __x)
235   { return __builtin_fabsf(__x); }
237   inline long double
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, 
243                                            double>::__type
244     fabs(_Tp __x)
245     { return __builtin_fabs(__x); }
247   using ::floor;
249   inline float
250   floor(float __x)
251   { return __builtin_floorf(__x); }
253   inline long double
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, 
259                                            double>::__type
260     floor(_Tp __x)
261     { return __builtin_floor(__x); }
263   using ::fmod;
265   inline float
266   fmod(float __x, float __y)
267   { return __builtin_fmodf(__x, __y); }
269   inline long double
270   fmod(long double __x, long double __y)
271   { return __builtin_fmodl(__x, __y); }
273   using ::frexp;
275   inline float
276   frexp(float __x, int* __exp)
277   { return __builtin_frexpf(__x, __exp); }
279   inline long double
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, 
285                                            double>::__type
286     frexp(_Tp __x, int* __exp)
287     { return __builtin_frexp(__x, __exp); }
289   using ::ldexp;
291   inline float
292   ldexp(float __x, int __exp)
293   { return __builtin_ldexpf(__x, __exp); }
295   inline long double
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, 
301                                            double>::__type
302     ldexp(_Tp __x, int __exp)
303     { return __builtin_ldexp(__x, __exp); }
305   using ::log;
307   inline float
308   log(float __x)
309   { return __builtin_logf(__x); }
311   inline long double
312   log(long double __x)
313   { return __builtin_logl(__x); }
315   template<typename _Tp>
316     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
317                                            double>::__type
318     log(_Tp __x)
319     { return __builtin_log(__x); }
321   using ::log10;
323   inline float
324   log10(float __x)
325   { return __builtin_log10f(__x); }
327   inline long double
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, 
333                                            double>::__type
334     log10(_Tp __x)
335     { return __builtin_log10(__x); }
337   using ::modf;
339   inline float
340   modf(float __x, float* __iptr)
341   { return __builtin_modff(__x, __iptr); }
343   inline long double
344   modf(long double __x, long double* __iptr)
345   { return __builtin_modfl(__x, __iptr); }
347   template<typename _Tp>
348     inline _Tp
349     __pow_helper(_Tp __x, int __n)
350     {
351       return __n < 0
352         ? _Tp(1)/__cmath_power(__x, -__n)
353         : __cmath_power(__x, __n);
354     }
356   using ::pow;
358   inline float
359   pow(float __x, float __y)
360   { return __builtin_powf(__x, __y); }
362   inline long double
363   pow(long double __x, long double __y)
364   { return __builtin_powl(__x, __y); }
366   inline double
367   pow(double __x, int __i)
368   { return __builtin_powi(__x, __i); }
370   inline float
371   pow(float __x, int __n)
372   { return __builtin_powif(__x, __n); }
374   inline long double
375   pow(long double __x, int __n)
376   { return __builtin_powil(__x, __n); }
378   using ::sin;
380   inline float
381   sin(float __x)
382   { return __builtin_sinf(__x); }
384   inline long double
385   sin(long double __x)
386   { return __builtin_sinl(__x); }
388   template<typename _Tp>
389     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
390                                            double>::__type
391     sin(_Tp __x)
392     { return __builtin_sin(__x); }
394   using ::sinh;
396   inline float
397   sinh(float __x)
398   { return __builtin_sinhf(__x); }
400   inline long double
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, 
406                                            double>::__type
407     sinh(_Tp __x)
408     { return __builtin_sinh(__x); }
410   using ::sqrt;
412   inline float
413   sqrt(float __x)
414   { return __builtin_sqrtf(__x); }
416   inline long double
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, 
422                                            double>::__type
423     sqrt(_Tp __x)
424     { return __builtin_sqrt(__x); }
426   using ::tan;
428   inline float
429   tan(float __x)
430   { return __builtin_tanf(__x); }
432   inline long double
433   tan(long double __x)
434   { return __builtin_tanl(__x); }
436   template<typename _Tp>
437     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
438                                            double>::__type
439     tan(_Tp __x)
440     { return __builtin_tan(__x); }
442   using ::tanh;
444   inline float
445   tanh(float __x)
446   { return __builtin_tanhf(__x); }
448   inline long double
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, 
454                                            double>::__type
455     tanh(_Tp __x)
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.
464 #undef fpclassify
465 #undef isfinite
466 #undef isinf
467 #undef isnan
468 #undef isnormal
469 #undef signbit
470 #undef isgreater
471 #undef isgreaterequal
472 #undef isless
473 #undef islessequal
474 #undef islessgreater
475 #undef isunordered
477 _GLIBCXX_BEGIN_NAMESPACE(std)
479   template<typename _Tp>
480     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
481                                            int>::__type
482     fpclassify(_Tp __f)
483     {
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));
487     }
489   template<typename _Tp>
490     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
491                                            int>::__type
492     isfinite(_Tp __f)
493     {
494       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
495       return __builtin_isfinite(__type(__f));
496     }
498   template<typename _Tp>
499     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
500                                            int>::__type
501     isinf(_Tp __f)
502     {
503       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
504       return __builtin_isinf(__type(__f));
505     }
507   template<typename _Tp>
508     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
509                                            int>::__type
510     isnan(_Tp __f)
511     {
512       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
513       return __builtin_isnan(__type(__f));
514     }
516   template<typename _Tp>
517     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
518                                            int>::__type
519     isnormal(_Tp __f)
520     {
521       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
522       return __builtin_isnormal(__type(__f));
523     }
525   template<typename _Tp>
526     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
527                                            int>::__type
528     signbit(_Tp __f)
529     {
530       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
531       return __builtin_signbit(__type(__f));
532     }
534   template<typename _Tp>
535     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
536                                            int>::__type
537     isgreater(_Tp __f1, _Tp __f2)
538     {
539       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
540       return __builtin_isgreater(__type(__f1), __type(__f2));
541     }
543   template<typename _Tp>
544     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
545                                            int>::__type
546     isgreaterequal(_Tp __f1, _Tp __f2)
547     {
548       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
549       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
550     }
552   template<typename _Tp>
553     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
554                                            int>::__type
555     isless(_Tp __f1, _Tp __f2)
556     {
557       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
558       return __builtin_isless(__type(__f1), __type(__f2));
559     }
561   template<typename _Tp>
562     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value, 
563                                            int>::__type
564     islessequal(_Tp __f1, _Tp __f2)
565     {
566       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
567       return __builtin_islessequal(__type(__f1), __type(__f2));
568     }
570   template<typename _Tp>
571     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
572                                            int>::__type
573     islessgreater(_Tp __f1, _Tp __f2)
574     {
575       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
576       return __builtin_islessgreater(__type(__f1), __type(__f2));
577     }
579   template<typename _Tp>
580     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
581                                            int>::__type
582     isunordered(_Tp __f1, _Tp __f2)
583     {
584       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
585       return __builtin_isunordered(__type(__f1), __type(__f2));
586     }
588 _GLIBCXX_END_NAMESPACE
590 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
591 #endif
593 #ifndef _GLIBCXX_EXPORT_TEMPLATE
594 # include <bits/cmath.tcc>
595 #endif
597 #endif