2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / c_global / cmath
blobdd26db1a718795841e578de2dcdff603ec14f3b7
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
19 // along with this library; see the file COPYING.  If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, 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 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
51 #include_next <math.h>
53 #ifndef _GLIBCXX_CMATH
54 #define _GLIBCXX_CMATH 1
56 // Get rid of those macros defined in <math.h> in lieu of real functions.
57 #undef abs
58 #undef div
59 #undef acos
60 #undef asin
61 #undef atan
62 #undef atan2
63 #undef ceil
64 #undef cos
65 #undef cosh
66 #undef exp
67 #undef fabs
68 #undef floor
69 #undef fmod
70 #undef frexp
71 #undef ldexp
72 #undef log
73 #undef log10
74 #undef modf
75 #undef pow
76 #undef sin
77 #undef sinh
78 #undef sqrt
79 #undef tan
80 #undef tanh
82 _GLIBCXX_BEGIN_NAMESPACE(std)
84   // Forward declaration of a helper function.  This really should be
85   // an `exported' forward declaration.
86   template<typename _Tp>
87     _Tp __cmath_power(_Tp, unsigned int);
89   template<typename _Tp>
90     inline _Tp
91     __pow_helper(_Tp __x, int __n)
92     {
93       return __n < 0
94         ? _Tp(1)/__cmath_power(__x, -__n)
95         : __cmath_power(__x, __n);
96     }
98   inline double
99   abs(double __x)
100   { return __builtin_fabs(__x); }
102   inline float
103   abs(float __x)
104   { return __builtin_fabsf(__x); }
106   inline long double
107   abs(long double __x)
108   { return __builtin_fabsl(__x); }
110   using ::acos;
112   inline float
113   acos(float __x)
114   { return __builtin_acosf(__x); }
116   inline long double
117   acos(long double __x)
118   { return __builtin_acosl(__x); }
120   template<typename _Tp>
121     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
122                                            double>::__type
123     acos(_Tp __x)
124     { return __builtin_acos(__x); }
126   using ::asin;
128   inline float
129   asin(float __x)
130   { return __builtin_asinf(__x); }
132   inline long double
133   asin(long double __x)
134   { return __builtin_asinl(__x); }
136   template<typename _Tp>
137     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
138                                            double>::__type
139     asin(_Tp __x)
140     { return __builtin_asin(__x); }
142   using ::atan;
144   inline float
145   atan(float __x)
146   { return __builtin_atanf(__x); }
148   inline long double
149   atan(long double __x)
150   { return __builtin_atanl(__x); }
152   template<typename _Tp>
153     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
154                                            double>::__type
155     atan(_Tp __x)
156     { return __builtin_atan(__x); }
158   using ::atan2;
160   inline float
161   atan2(float __y, float __x)
162   { return __builtin_atan2f(__y, __x); }
164   inline long double
165   atan2(long double __y, long double __x)
166   { return __builtin_atan2l(__y, __x); }
168   template<typename _Tp, typename _Up>
169     inline
170     typename __gnu_cxx::__promote_2<
171     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
172                                     && __is_arithmetic<_Up>::__value,
173                                     _Tp>::__type, _Up>::__type
174     atan2(_Tp __y, _Up __x)
175     {
176       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
177       return atan2(__type(__y), __type(__x));
178     }
180   using ::ceil;
182   inline float
183   ceil(float __x)
184   { return __builtin_ceilf(__x); }
186   inline long double
187   ceil(long double __x)
188   { return __builtin_ceill(__x); }
190   template<typename _Tp>
191     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
192                                            double>::__type
193     ceil(_Tp __x)
194     { return __builtin_ceil(__x); }
196   using ::cos;
198   inline float
199   cos(float __x)
200   { return __builtin_cosf(__x); }
202   inline long double
203   cos(long double __x)
204   { return __builtin_cosl(__x); }
206   template<typename _Tp>
207     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
208                                            double>::__type
209     cos(_Tp __x)
210     { return __builtin_cos(__x); }
212   using ::cosh;
214   inline float
215   cosh(float __x)
216   { return __builtin_coshf(__x); }
218   inline long double
219   cosh(long double __x)
220   { return __builtin_coshl(__x); }
222   template<typename _Tp>
223     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
224                                            double>::__type
225     cosh(_Tp __x)
226     { return __builtin_cosh(__x); }
228   using ::exp;
230   inline float
231   exp(float __x)
232   { return __builtin_expf(__x); }
234   inline long double
235   exp(long double __x)
236   { return __builtin_expl(__x); }
238   template<typename _Tp>
239     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
240                                            double>::__type
241     exp(_Tp __x)
242     { return __builtin_exp(__x); }
244   using ::fabs;
246   inline float
247   fabs(float __x)
248   { return __builtin_fabsf(__x); }
250   inline long double
251   fabs(long double __x)
252   { return __builtin_fabsl(__x); }
254   template<typename _Tp>
255     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
256                                            double>::__type
257     fabs(_Tp __x)
258     { return __builtin_fabs(__x); }
260   using ::floor;
262   inline float
263   floor(float __x)
264   { return __builtin_floorf(__x); }
266   inline long double
267   floor(long double __x)
268   { return __builtin_floorl(__x); }
270   template<typename _Tp>
271     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
272                                            double>::__type
273     floor(_Tp __x)
274     { return __builtin_floor(__x); }
276   using ::fmod;
278   inline float
279   fmod(float __x, float __y)
280   { return __builtin_fmodf(__x, __y); }
282   inline long double
283   fmod(long double __x, long double __y)
284   { return __builtin_fmodl(__x, __y); }
286   using ::frexp;
288   inline float
289   frexp(float __x, int* __exp)
290   { return __builtin_frexpf(__x, __exp); }
292   inline long double
293   frexp(long double __x, int* __exp)
294   { return __builtin_frexpl(__x, __exp); }
296   template<typename _Tp>
297     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
298                                            double>::__type
299     frexp(_Tp __x, int* __exp)
300     { return __builtin_frexp(__x, __exp); }
302   using ::ldexp;
304   inline float
305   ldexp(float __x, int __exp)
306   { return __builtin_ldexpf(__x, __exp); }
308   inline long double
309   ldexp(long double __x, int __exp)
310   { return __builtin_ldexpl(__x, __exp); }
312   template<typename _Tp>
313     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
314                                            double>::__type
315   ldexp(_Tp __x, int __exp)
316   { return __builtin_ldexp(__x, __exp); }
318   using ::log;
320   inline float
321   log(float __x)
322   { return __builtin_logf(__x); }
324   inline long double
325   log(long double __x)
326   { return __builtin_logl(__x); }
328   template<typename _Tp>
329     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
330                                            double>::__type
331     log(_Tp __x)
332     { return __builtin_log(__x); }
334   using ::log10;
336   inline float
337   log10(float __x)
338   { return __builtin_log10f(__x); }
340   inline long double
341   log10(long double __x)
342   { return __builtin_log10l(__x); }
344   template<typename _Tp>
345     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
346                                            double>::__type
347     log10(_Tp __x)
348     { return __builtin_log10(__x); }
350   using ::modf;
352   inline float
353   modf(float __x, float* __iptr)
354   { return __builtin_modff(__x, __iptr); }
356   inline long double
357   modf(long double __x, long double* __iptr)
358   { return __builtin_modfl(__x, __iptr); }
360   using ::pow;
362   inline float
363   pow(float __x, float __y)
364   { return __builtin_powf(__x, __y); }
366   inline long double
367   pow(long double __x, long double __y)
368   { return __builtin_powl(__x, __y); }
370 #ifndef __GXX_EXPERIMENTAL_CXX0X__
371   // _GLIBCXX_RESOLVE_LIB_DEFECTS
372   // DR 550. What should the return type of pow(float,int) be?
373   inline double
374   pow(double __x, int __i)
375   { return __builtin_powi(__x, __i); }
377   inline float
378   pow(float __x, int __n)
379   { return __builtin_powif(__x, __n); }
381   inline long double
382   pow(long double __x, int __n)
383   { return __builtin_powil(__x, __n); }
384 #endif
386   template<typename _Tp, typename _Up>
387     inline
388     typename __gnu_cxx::__promote_2<
389     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
390                                     && __is_arithmetic<_Up>::__value,
391                                     _Tp>::__type, _Up>::__type
392     pow(_Tp __x, _Up __y)
393     {
394       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
395       return pow(__type(__x), __type(__y));
396     }
398   using ::sin;
400   inline float
401   sin(float __x)
402   { return __builtin_sinf(__x); }
404   inline long double
405   sin(long double __x)
406   { return __builtin_sinl(__x); }
408   template<typename _Tp>
409     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
410                                            double>::__type
411     sin(_Tp __x)
412     { return __builtin_sin(__x); }
414   using ::sinh;
416   inline float
417   sinh(float __x)
418   { return __builtin_sinhf(__x); }
420   inline long double
421   sinh(long double __x)
422   { return __builtin_sinhl(__x); }
424   template<typename _Tp>
425     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
426                                            double>::__type
427     sinh(_Tp __x)
428     { return __builtin_sinh(__x); }
430   using ::sqrt;
432   inline float
433   sqrt(float __x)
434   { return __builtin_sqrtf(__x); }
436   inline long double
437   sqrt(long double __x)
438   { return __builtin_sqrtl(__x); }
440   template<typename _Tp>
441     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
442                                            double>::__type
443     sqrt(_Tp __x)
444     { return __builtin_sqrt(__x); }
446   using ::tan;
448   inline float
449   tan(float __x)
450   { return __builtin_tanf(__x); }
452   inline long double
453   tan(long double __x)
454   { return __builtin_tanl(__x); }
456   template<typename _Tp>
457     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
458                                            double>::__type
459     tan(_Tp __x)
460     { return __builtin_tan(__x); }
462   using ::tanh;
464   inline float
465   tanh(float __x)
466   { return __builtin_tanhf(__x); }
468   inline long double
469   tanh(long double __x)
470   { return __builtin_tanhl(__x); }
472   template<typename _Tp>
473     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
474                                            double>::__type
475     tanh(_Tp __x)
476     { return __builtin_tanh(__x); }
478 _GLIBCXX_END_NAMESPACE
480 #if _GLIBCXX_USE_C99_MATH
481 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
483 // These are possible macros imported from C99-land.
484 #undef fpclassify
485 #undef isfinite
486 #undef isinf
487 #undef isnan
488 #undef isnormal
489 #undef signbit
490 #undef isgreater
491 #undef isgreaterequal
492 #undef isless
493 #undef islessequal
494 #undef islessgreater
495 #undef isunordered
497 _GLIBCXX_BEGIN_NAMESPACE(std)
499   template<typename _Tp>
500     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
501                                            int>::__type
502     fpclassify(_Tp __f)
503     {
504       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
505       return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
506                                   FP_SUBNORMAL, FP_ZERO, __type(__f));
507     }
509   template<typename _Tp>
510     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
511                                            int>::__type
512     isfinite(_Tp __f)
513     {
514       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
515       return __builtin_isfinite(__type(__f));
516     }
518   template<typename _Tp>
519     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
520                                            int>::__type
521     isinf(_Tp __f)
522     {
523       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
524       return __builtin_isinf(__type(__f));
525     }
527   template<typename _Tp>
528     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
529                                            int>::__type
530     isnan(_Tp __f)
531     {
532       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
533       return __builtin_isnan(__type(__f));
534     }
536   template<typename _Tp>
537     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
538                                            int>::__type
539     isnormal(_Tp __f)
540     {
541       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
542       return __builtin_isnormal(__type(__f));
543     }
545   template<typename _Tp>
546     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
547                                            int>::__type
548     signbit(_Tp __f)
549     {
550       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
551       return __builtin_signbit(__type(__f));
552     }
554   template<typename _Tp>
555     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
556                                            int>::__type
557     isgreater(_Tp __f1, _Tp __f2)
558     {
559       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
560       return __builtin_isgreater(__type(__f1), __type(__f2));
561     }
563   template<typename _Tp>
564     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
565                                            int>::__type
566     isgreaterequal(_Tp __f1, _Tp __f2)
567     {
568       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
569       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
570     }
572   template<typename _Tp>
573     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
574                                            int>::__type
575     isless(_Tp __f1, _Tp __f2)
576     {
577       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
578       return __builtin_isless(__type(__f1), __type(__f2));
579     }
581   template<typename _Tp>
582     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
583                                            int>::__type
584     islessequal(_Tp __f1, _Tp __f2)
585     {
586       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
587       return __builtin_islessequal(__type(__f1), __type(__f2));
588     }
590   template<typename _Tp>
591     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
592                                            int>::__type
593     islessgreater(_Tp __f1, _Tp __f2)
594     {
595       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
596       return __builtin_islessgreater(__type(__f1), __type(__f2));
597     }
599   template<typename _Tp>
600     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
601                                            int>::__type
602     isunordered(_Tp __f1, _Tp __f2)
603     {
604       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
605       return __builtin_isunordered(__type(__f1), __type(__f2));
606     }
608 _GLIBCXX_END_NAMESPACE
610 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
611 #endif
613 #ifndef _GLIBCXX_EXPORT_TEMPLATE
614 # include <bits/cmath.tcc>
615 #endif
617 #ifdef __GXX_EXPERIMENTAL_CXX0X__
618 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
619 #    error C++0x header cannot be included from TR1 header
620 #  endif
621 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
622 #    include <tr1_impl/cmath>
623 #  else
624 #    define _GLIBCXX_INCLUDE_AS_CXX0X
625 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
626 #    define _GLIBCXX_END_NAMESPACE_TR1
627 #    define _GLIBCXX_TR1
628 #    include <tr1_impl/cmath>
629 #    undef _GLIBCXX_TR1
630 #    undef _GLIBCXX_END_NAMESPACE_TR1
631 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
632 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
633 #  endif
634 #endif
636 #endif