2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / c_std / cmath
blob948a0bece2943c2d298386a7518cb82fac475681
1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008
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 2, 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 // 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,
21 // USA.
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.
35  *
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
39  *  as macros in C).
40  */
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>
55 #include <math.h>
57 // Get rid of those macros defined in <math.h> in lieu of real functions.
58 #undef abs
59 #undef div
60 #undef acos
61 #undef asin
62 #undef atan
63 #undef atan2
64 #undef ceil
65 #undef cos
66 #undef cosh
67 #undef exp
68 #undef fabs
69 #undef floor
70 #undef fmod
71 #undef frexp
72 #undef ldexp
73 #undef log
74 #undef log10
75 #undef modf
76 #undef pow
77 #undef sin
78 #undef sinh
79 #undef sqrt
80 #undef tan
81 #undef tanh
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);
89   inline double
90   abs(double __x)
91   { return __builtin_fabs(__x); }
93   inline float
94   abs(float __x)
95   { return __builtin_fabsf(__x); }
97   inline long double
98   abs(long double __x)
99   { return __builtin_fabsl(__x); }
101   using ::acos;
103   inline float
104   acos(float __x)
105   { return __builtin_acosf(__x); }
107   inline long double
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, 
113                                            double>::__type
114     acos(_Tp __x)
115     { return __builtin_acos(__x); }
117   using ::asin;
119   inline float
120   asin(float __x)
121   { return __builtin_asinf(__x); }
123   inline long double
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,
129                                          double>::__type
130     asin(_Tp __x)
131     { return __builtin_asin(__x); }
133   using ::atan;
135   inline float
136   atan(float __x)
137   { return __builtin_atanf(__x); }
139   inline long double
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, 
145                                          double>::__type
146     atan(_Tp __x)
147     { return __builtin_atan(__x); }
149   using ::atan2;
151   inline float
152   atan2(float __y, float __x)
153   { return __builtin_atan2f(__y, __x); }
155   inline long double
156   atan2(long double __y, long double __x)
157   { return __builtin_atan2l(__y, __x); }
159   template<typename _Tp, typename _Up>
160     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value
161                                            && __is_integer<_Up>::__value, 
162                                            double>::__type
163     atan2(_Tp __y, _Up __x)
164     { return __builtin_atan2(__y, __x); }
166   using ::ceil;
168   inline float
169   ceil(float __x)
170   { return __builtin_ceilf(__x); }
172   inline long double
173   ceil(long double __x)
174   { return __builtin_ceill(__x); }
176   template<typename _Tp>
177     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
178                                            double>::__type
179     ceil(_Tp __x)
180     { return __builtin_ceil(__x); }
182   using ::cos;
184   inline float
185   cos(float __x)
186   { return __builtin_cosf(__x); }
188   inline long double
189   cos(long double __x)
190   { return __builtin_cosl(__x); }
192   template<typename _Tp>
193     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
194                                            double>::__type
195     cos(_Tp __x)
196     { return __builtin_cos(__x); }
198   using ::cosh;
200   inline float
201   cosh(float __x)
202   { return __builtin_coshf(__x); }
204   inline long double
205   cosh(long double __x)
206   { return __builtin_coshl(__x); }
208   template<typename _Tp>
209     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
210                                            double>::__type
211     cosh(_Tp __x)
212     { return __builtin_cosh(__x); }
214   using ::exp;
216   inline float
217   exp(float __x)
218   { return __builtin_expf(__x); }
220   inline long double
221   exp(long double __x)
222   { return __builtin_expl(__x); }
224   template<typename _Tp>
225     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
226                                            double>::__type
227     exp(_Tp __x)
228     { return __builtin_exp(__x); }
230   using ::fabs;
232   inline float
233   fabs(float __x)
234   { return __builtin_fabsf(__x); }
236   inline long double
237   fabs(long double __x)
238   { return __builtin_fabsl(__x); }
240   template<typename _Tp>
241     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
242                                            double>::__type
243     fabs(_Tp __x)
244     { return __builtin_fabs(__x); }
246   using ::floor;
248   inline float
249   floor(float __x)
250   { return __builtin_floorf(__x); }
252   inline long double
253   floor(long double __x)
254   { return __builtin_floorl(__x); }
256   template<typename _Tp>
257     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
258                                            double>::__type
259     floor(_Tp __x)
260     { return __builtin_floor(__x); }
262   using ::fmod;
264   inline float
265   fmod(float __x, float __y)
266   { return __builtin_fmodf(__x, __y); }
268   inline long double
269   fmod(long double __x, long double __y)
270   { return __builtin_fmodl(__x, __y); }
272   using ::frexp;
274   inline float
275   frexp(float __x, int* __exp)
276   { return __builtin_frexpf(__x, __exp); }
278   inline long double
279   frexp(long double __x, int* __exp)
280   { return __builtin_frexpl(__x, __exp); }
282   template<typename _Tp>
283     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
284                                            double>::__type
285     frexp(_Tp __x, int* __exp)
286     { return __builtin_frexp(__x, __exp); }
288   using ::ldexp;
290   inline float
291   ldexp(float __x, int __exp)
292   { return __builtin_ldexpf(__x, __exp); }
294   inline long double
295   ldexp(long double __x, int __exp)
296   { return __builtin_ldexpl(__x, __exp); }
298   template<typename _Tp>
299     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
300                                            double>::__type
301   ldexp(_Tp __x, int __exp)
302   { return __builtin_ldexp(__x, __exp); }
304   using ::log;
306   inline float
307   log(float __x)
308   { return __builtin_logf(__x); }
310   inline long double
311   log(long double __x)
312   { return __builtin_logl(__x); }
314   template<typename _Tp>
315     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
316                                            double>::__type
317     log(_Tp __x)
318     { return __builtin_log(__x); }
320   using ::log10;
322   inline float
323   log10(float __x)
324   { return __builtin_log10f(__x); }
326   inline long double
327   log10(long double __x)
328   { return __builtin_log10l(__x); }
330   template<typename _Tp>
331     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
332                                            double>::__type
333     log10(_Tp __x)
334     { return __builtin_log10(__x); }
336   using ::modf;
338   inline float
339   modf(float __x, float* __iptr)
340   { return __builtin_modff(__x, __iptr); }
342   inline long double
343   modf(long double __x, long double* __iptr)
344   { return __builtin_modfl(__x, __iptr); }
346   template<typename _Tp>
347     inline _Tp
348     __pow_helper(_Tp __x, int __n)
349     {
350       return __n < 0
351         ? _Tp(1)/__cmath_power(__x, -__n)
352         : __cmath_power(__x, __n);
353     }
355   using ::pow;
357   inline float
358   pow(float __x, float __y)
359   { return __builtin_powf(__x, __y); }
361   inline long double
362   pow(long double __x, long double __y)
363   { return __builtin_powl(__x, __y); }
365   inline double
366   pow(double __x, int __i)
367   { return __builtin_powi(__x, __i); }
369   inline float
370   pow(float __x, int __n)
371   { return __builtin_powif(__x, __n); }
373   inline long double
374   pow(long double __x, int __n)
375   { return __builtin_powil(__x, __n); }
377   using ::sin;
379   inline float
380   sin(float __x)
381   { return __builtin_sinf(__x); }
383   inline long double
384   sin(long double __x)
385   { return __builtin_sinl(__x); }
387   template<typename _Tp>
388     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
389                                            double>::__type
390     sin(_Tp __x)
391     { return __builtin_sin(__x); }
393   using ::sinh;
395   inline float
396   sinh(float __x)
397   { return __builtin_sinhf(__x); }
399   inline long double
400   sinh(long double __x)
401   { return __builtin_sinhl(__x); }
403   template<typename _Tp>
404     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
405                                            double>::__type
406     sinh(_Tp __x)
407     { return __builtin_sinh(__x); }
409   using ::sqrt;
411   inline float
412   sqrt(float __x)
413   { return __builtin_sqrtf(__x); }
415   inline long double
416   sqrt(long double __x)
417   { return __builtin_sqrtl(__x); }
419   template<typename _Tp>
420     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
421                                            double>::__type
422     sqrt(_Tp __x)
423     { return __builtin_sqrt(__x); }
425   using ::tan;
427   inline float
428   tan(float __x)
429   { return __builtin_tanf(__x); }
431   inline long double
432   tan(long double __x)
433   { return __builtin_tanl(__x); }
435   template<typename _Tp>
436     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
437                                            double>::__type
438     tan(_Tp __x)
439     { return __builtin_tan(__x); }
441   using ::tanh;
443   inline float
444   tanh(float __x)
445   { return __builtin_tanhf(__x); }
447   inline long double
448   tanh(long double __x)
449   { return __builtin_tanhl(__x); }
451   template<typename _Tp>
452     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
453                                            double>::__type
454     tanh(_Tp __x)
455     { return __builtin_tanh(__x); }
457 _GLIBCXX_END_NAMESPACE
459 #if _GLIBCXX_USE_C99_MATH
460 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
462 // These are possible macros imported from C99-land.
463 #undef fpclassify
464 #undef isfinite
465 #undef isinf
466 #undef isnan
467 #undef isnormal
468 #undef signbit
469 #undef isgreater
470 #undef isgreaterequal
471 #undef isless
472 #undef islessequal
473 #undef islessgreater
474 #undef isunordered
476 _GLIBCXX_BEGIN_NAMESPACE(std)
478   template<typename _Tp>
479     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
480                                            int>::__type
481     fpclassify(_Tp __f)
482     {
483       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
484       return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
485                                   FP_SUBNORMAL, FP_ZERO, __type(__f));
486     }
488   template<typename _Tp>
489     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
490                                            int>::__type
491     isfinite(_Tp __f)
492     {
493       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
494       return __builtin_isfinite(__type(__f));
495     }
497   template<typename _Tp>
498     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
499                                            int>::__type
500     isinf(_Tp __f)
501     {
502       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
503       return __builtin_isinf(__type(__f));
504     }
506   template<typename _Tp>
507     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
508                                            int>::__type
509     isnan(_Tp __f)
510     {
511       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
512       return __builtin_isnan(__type(__f));
513     }
515   template<typename _Tp>
516     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
517                                            int>::__type
518     isnormal(_Tp __f)
519     {
520       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
521       return __builtin_isnormal(__type(__f));
522     }
524   template<typename _Tp>
525     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
526                                            int>::__type
527     signbit(_Tp __f)
528     {
529       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
530       return __builtin_signbit(__type(__f));
531     }
533   template<typename _Tp>
534     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
535                                            int>::__type
536     isgreater(_Tp __f1, _Tp __f2)
537     {
538       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
539       return __builtin_isgreater(__type(__f1), __type(__f2));
540     }
542   template<typename _Tp>
543     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
544                                            int>::__type
545     isgreaterequal(_Tp __f1, _Tp __f2)
546     {
547       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
548       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
549     }
551   template<typename _Tp>
552     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
553                                            int>::__type
554     isless(_Tp __f1, _Tp __f2)
555     {
556       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
557       return __builtin_isless(__type(__f1), __type(__f2));
558     }
560   template<typename _Tp>
561     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value, 
562                                            int>::__type
563     islessequal(_Tp __f1, _Tp __f2)
564     {
565       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
566       return __builtin_islessequal(__type(__f1), __type(__f2));
567     }
569   template<typename _Tp>
570     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
571                                            int>::__type
572     islessgreater(_Tp __f1, _Tp __f2)
573     {
574       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
575       return __builtin_islessgreater(__type(__f1), __type(__f2));
576     }
578   template<typename _Tp>
579     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
580                                            int>::__type
581     isunordered(_Tp __f1, _Tp __f2)
582     {
583       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
584       return __builtin_isunordered(__type(__f1), __type(__f2));
585     }
587 _GLIBCXX_END_NAMESPACE
589 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
590 #endif
592 #ifndef _GLIBCXX_EXPORT_TEMPLATE
593 # include <bits/cmath.tcc>
594 #endif
596 #endif