Home | History | Annotate | Download | only in 4.4.3
      1 // -*- C++ -*- C forwarding header.
      2 
      3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
      4 // 2006, 2007, 2008, 2009
      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.
     12 
     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.
     17 
     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.
     21 
     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/>.
     26 
     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 "*.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  */
     36 
     37 //
     38 // ISO C++ 14882: 26.5  C library
     39 //
     40 
     41 #pragma GCC system_header
     42 
     43 #include <bits/c++config.h>
     44 #include <bits/cpp_type_traits.h>
     45 #include <ext/type_traits.h>
     46 #include <math.h>
     47 
     48 #ifndef _GLIBCXX_CMATH
     49 #define _GLIBCXX_CMATH 1
     50 
     51 // Get rid of those macros defined in <math.h> in lieu of real functions.
     52 #undef abs
     53 #undef div
     54 #undef acos
     55 #undef asin
     56 #undef atan
     57 #undef atan2
     58 #undef ceil
     59 #undef cos
     60 #undef cosh
     61 #undef exp
     62 #undef fabs
     63 #undef floor
     64 #undef fmod
     65 #undef frexp
     66 #undef ldexp
     67 #undef log
     68 #undef log10
     69 #undef modf
     70 #undef pow
     71 #undef sin
     72 #undef sinh
     73 #undef sqrt
     74 #undef tan
     75 #undef tanh
     76 
     77 _GLIBCXX_BEGIN_NAMESPACE(std)
     78 
     79   // Forward declaration of a helper function.  This really should be
     80   // an `exported' forward declaration.
     81   template<typename _Tp>
     82     _Tp __cmath_power(_Tp, unsigned int);
     83 
     84   template<typename _Tp>
     85     inline _Tp
     86     __pow_helper(_Tp __x, int __n)
     87     {
     88       return __n < 0
     89         ? _Tp(1)/__cmath_power(__x, -__n)
     90         : __cmath_power(__x, __n);
     91     }
     92 
     93   inline double
     94   abs(double __x)
     95   { return __builtin_fabs(__x); }
     96 
     97   inline float
     98   abs(float __x)
     99   { return __builtin_fabsf(__x); }
    100 
    101   inline long double
    102   abs(long double __x)
    103   { return __builtin_fabsl(__x); }
    104 
    105   using ::acos;
    106 
    107   inline float
    108   acos(float __x)
    109   { return __builtin_acosf(__x); }
    110 
    111   inline long double
    112   acos(long double __x)
    113   { return __builtin_acosl(__x); }
    114 
    115   template<typename _Tp>
    116     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    117 					   double>::__type
    118     acos(_Tp __x)
    119     { return __builtin_acos(__x); }
    120 
    121   using ::asin;
    122 
    123   inline float
    124   asin(float __x)
    125   { return __builtin_asinf(__x); }
    126 
    127   inline long double
    128   asin(long double __x)
    129   { return __builtin_asinl(__x); }
    130 
    131   template<typename _Tp>
    132     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
    133 					   double>::__type
    134     asin(_Tp __x)
    135     { return __builtin_asin(__x); }
    136 
    137   using ::atan;
    138 
    139   inline float
    140   atan(float __x)
    141   { return __builtin_atanf(__x); }
    142 
    143   inline long double
    144   atan(long double __x)
    145   { return __builtin_atanl(__x); }
    146 
    147   template<typename _Tp>
    148     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    149 					   double>::__type
    150     atan(_Tp __x)
    151     { return __builtin_atan(__x); }
    152 
    153   using ::atan2;
    154 
    155   inline float
    156   atan2(float __y, float __x)
    157   { return __builtin_atan2f(__y, __x); }
    158 
    159   inline long double
    160   atan2(long double __y, long double __x)
    161   { return __builtin_atan2l(__y, __x); }
    162 
    163   template<typename _Tp, typename _Up>
    164     inline
    165     typename __gnu_cxx::__promote_2<
    166     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    167 				    && __is_arithmetic<_Up>::__value,
    168 				    _Tp>::__type, _Up>::__type
    169     atan2(_Tp __y, _Up __x)
    170     {
    171       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    172       return atan2(__type(__y), __type(__x));
    173     }
    174 
    175   using ::ceil;
    176 
    177   inline float
    178   ceil(float __x)
    179   { return __builtin_ceilf(__x); }
    180 
    181   inline long double
    182   ceil(long double __x)
    183   { return __builtin_ceill(__x); }
    184 
    185   template<typename _Tp>
    186     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    187 					   double>::__type
    188     ceil(_Tp __x)
    189     { return __builtin_ceil(__x); }
    190 
    191   using ::cos;
    192 
    193   inline float
    194   cos(float __x)
    195   { return __builtin_cosf(__x); }
    196 
    197   inline long double
    198   cos(long double __x)
    199   { return __builtin_cosl(__x); }
    200 
    201   template<typename _Tp>
    202     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    203 					   double>::__type
    204     cos(_Tp __x)
    205     { return __builtin_cos(__x); }
    206 
    207   using ::cosh;
    208 
    209   inline float
    210   cosh(float __x)
    211   { return __builtin_coshf(__x); }
    212 
    213   inline long double
    214   cosh(long double __x)
    215   { return __builtin_coshl(__x); }
    216 
    217   template<typename _Tp>
    218     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    219 					   double>::__type
    220     cosh(_Tp __x)
    221     { return __builtin_cosh(__x); }
    222 
    223   using ::exp;
    224 
    225   inline float
    226   exp(float __x)
    227   { return __builtin_expf(__x); }
    228 
    229   inline long double
    230   exp(long double __x)
    231   { return __builtin_expl(__x); }
    232 
    233   template<typename _Tp>
    234     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    235 					   double>::__type
    236     exp(_Tp __x)
    237     { return __builtin_exp(__x); }
    238 
    239   using ::fabs;
    240 
    241   inline float
    242   fabs(float __x)
    243   { return __builtin_fabsf(__x); }
    244 
    245   inline long double
    246   fabs(long double __x)
    247   { return __builtin_fabsl(__x); }
    248 
    249   template<typename _Tp>
    250     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    251 					   double>::__type
    252     fabs(_Tp __x)
    253     { return __builtin_fabs(__x); }
    254 
    255   using ::floor;
    256 
    257   inline float
    258   floor(float __x)
    259   { return __builtin_floorf(__x); }
    260 
    261   inline long double
    262   floor(long double __x)
    263   { return __builtin_floorl(__x); }
    264 
    265   template<typename _Tp>
    266     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    267 					   double>::__type
    268     floor(_Tp __x)
    269     { return __builtin_floor(__x); }
    270 
    271   using ::fmod;
    272 
    273   inline float
    274   fmod(float __x, float __y)
    275   { return __builtin_fmodf(__x, __y); }
    276 
    277   inline long double
    278   fmod(long double __x, long double __y)
    279   { return __builtin_fmodl(__x, __y); }
    280 
    281   using ::frexp;
    282 
    283   inline float
    284   frexp(float __x, int* __exp)
    285   { return __builtin_frexpf(__x, __exp); }
    286 
    287   inline long double
    288   frexp(long double __x, int* __exp)
    289   { return __builtin_frexpl(__x, __exp); }
    290 
    291   template<typename _Tp>
    292     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    293 					   double>::__type
    294     frexp(_Tp __x, int* __exp)
    295     { return __builtin_frexp(__x, __exp); }
    296 
    297   using ::ldexp;
    298 
    299   inline float
    300   ldexp(float __x, int __exp)
    301   { return __builtin_ldexpf(__x, __exp); }
    302 
    303   inline long double
    304   ldexp(long double __x, int __exp)
    305   { return __builtin_ldexpl(__x, __exp); }
    306 
    307   template<typename _Tp>
    308     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    309 					   double>::__type
    310   ldexp(_Tp __x, int __exp)
    311   { return __builtin_ldexp(__x, __exp); }
    312 
    313   using ::log;
    314 
    315   inline float
    316   log(float __x)
    317   { return __builtin_logf(__x); }
    318 
    319   inline long double
    320   log(long double __x)
    321   { return __builtin_logl(__x); }
    322 
    323   template<typename _Tp>
    324     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    325 					   double>::__type
    326     log(_Tp __x)
    327     { return __builtin_log(__x); }
    328 
    329   using ::log10;
    330 
    331   inline float
    332   log10(float __x)
    333   { return __builtin_log10f(__x); }
    334 
    335   inline long double
    336   log10(long double __x)
    337   { return __builtin_log10l(__x); }
    338 
    339   template<typename _Tp>
    340     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    341 					   double>::__type
    342     log10(_Tp __x)
    343     { return __builtin_log10(__x); }
    344 
    345   using ::modf;
    346 
    347   inline float
    348   modf(float __x, float* __iptr)
    349   { return __builtin_modff(__x, __iptr); }
    350 
    351   inline long double
    352   modf(long double __x, long double* __iptr)
    353   { return __builtin_modfl(__x, __iptr); }
    354 
    355   using ::pow;
    356 
    357   inline float
    358   pow(float __x, float __y)
    359   { return __builtin_powf(__x, __y); }
    360 
    361   inline long double
    362   pow(long double __x, long double __y)
    363   { return __builtin_powl(__x, __y); }
    364 
    365 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    366   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    367   // DR 550. What should the return type of pow(float,int) be?
    368   inline double
    369   pow(double __x, int __i)
    370   { return __builtin_powi(__x, __i); }
    371 
    372   inline float
    373   pow(float __x, int __n)
    374   { return __builtin_powif(__x, __n); }
    375 
    376   inline long double
    377   pow(long double __x, int __n)
    378   { return __builtin_powil(__x, __n); }
    379 #endif
    380 
    381   template<typename _Tp, typename _Up>
    382     inline
    383     typename __gnu_cxx::__promote_2<
    384     typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
    385 				    && __is_arithmetic<_Up>::__value,
    386 				    _Tp>::__type, _Up>::__type
    387     pow(_Tp __x, _Up __y)
    388     {
    389       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
    390       return pow(__type(__x), __type(__y));
    391     }
    392 
    393   using ::sin;
    394 
    395   inline float
    396   sin(float __x)
    397   { return __builtin_sinf(__x); }
    398 
    399   inline long double
    400   sin(long double __x)
    401   { return __builtin_sinl(__x); }
    402 
    403   template<typename _Tp>
    404     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    405 					   double>::__type
    406     sin(_Tp __x)
    407     { return __builtin_sin(__x); }
    408 
    409   using ::sinh;
    410 
    411   inline float
    412   sinh(float __x)
    413   { return __builtin_sinhf(__x); }
    414 
    415   inline long double
    416   sinh(long double __x)
    417   { return __builtin_sinhl(__x); }
    418 
    419   template<typename _Tp>
    420     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    421 					   double>::__type
    422     sinh(_Tp __x)
    423     { return __builtin_sinh(__x); }
    424 
    425   using ::sqrt;
    426 
    427   inline float
    428   sqrt(float __x)
    429   { return __builtin_sqrtf(__x); }
    430 
    431   inline long double
    432   sqrt(long double __x)
    433   { return __builtin_sqrtl(__x); }
    434 
    435   template<typename _Tp>
    436     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    437 					   double>::__type
    438     sqrt(_Tp __x)
    439     { return __builtin_sqrt(__x); }
    440 
    441   using ::tan;
    442 
    443   inline float
    444   tan(float __x)
    445   { return __builtin_tanf(__x); }
    446 
    447   inline long double
    448   tan(long double __x)
    449   { return __builtin_tanl(__x); }
    450 
    451   template<typename _Tp>
    452     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    453 					   double>::__type
    454     tan(_Tp __x)
    455     { return __builtin_tan(__x); }
    456 
    457   using ::tanh;
    458 
    459   inline float
    460   tanh(float __x)
    461   { return __builtin_tanhf(__x); }
    462 
    463   inline long double
    464   tanh(long double __x)
    465   { return __builtin_tanhl(__x); }
    466 
    467   template<typename _Tp>
    468     inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
    469 					   double>::__type
    470     tanh(_Tp __x)
    471     { return __builtin_tanh(__x); }
    472 
    473 _GLIBCXX_END_NAMESPACE
    474 
    475 #if _GLIBCXX_USE_C99_MATH
    476 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
    477 
    478 // These are possible macros imported from C99-land.
    479 #undef fpclassify
    480 #undef isfinite
    481 #undef isinf
    482 #undef isnan
    483 #undef isnormal
    484 #undef signbit
    485 #undef isgreater
    486 #undef isgreaterequal
    487 #undef isless
    488 #undef islessequal
    489 #undef islessgreater
    490 #undef isunordered
    491 
    492 _GLIBCXX_BEGIN_NAMESPACE(std)
    493 
    494   template<typename _Tp>
    495     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    496 					   int>::__type
    497     fpclassify(_Tp __f)
    498     {
    499       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    500       return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
    501 				  FP_SUBNORMAL, FP_ZERO, __type(__f));
    502     }
    503 
    504   template<typename _Tp>
    505     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    506 					   int>::__type
    507     isfinite(_Tp __f)
    508     {
    509       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    510       return __builtin_isfinite(__type(__f));
    511     }
    512 
    513   template<typename _Tp>
    514     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    515 					   int>::__type
    516     isinf(_Tp __f)
    517     {
    518       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    519       return __builtin_isinf(__type(__f));
    520     }
    521 
    522   template<typename _Tp>
    523     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    524 					   int>::__type
    525     isnan(_Tp __f)
    526     {
    527       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    528       return __builtin_isnan(__type(__f));
    529     }
    530 
    531   template<typename _Tp>
    532     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    533 					   int>::__type
    534     isnormal(_Tp __f)
    535     {
    536       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    537       return __builtin_isnormal(__type(__f));
    538     }
    539 
    540   template<typename _Tp>
    541     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    542 					   int>::__type
    543     signbit(_Tp __f)
    544     {
    545       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    546       return __builtin_signbit(__type(__f));
    547     }
    548 
    549   template<typename _Tp>
    550     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    551 					   int>::__type
    552     isgreater(_Tp __f1, _Tp __f2)
    553     {
    554       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    555       return __builtin_isgreater(__type(__f1), __type(__f2));
    556     }
    557 
    558   template<typename _Tp>
    559     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    560 					   int>::__type
    561     isgreaterequal(_Tp __f1, _Tp __f2)
    562     {
    563       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    564       return __builtin_isgreaterequal(__type(__f1), __type(__f2));
    565     }
    566 
    567   template<typename _Tp>
    568     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    569 					   int>::__type
    570     isless(_Tp __f1, _Tp __f2)
    571     {
    572       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    573       return __builtin_isless(__type(__f1), __type(__f2));
    574     }
    575 
    576   template<typename _Tp>
    577     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    578 					   int>::__type
    579     islessequal(_Tp __f1, _Tp __f2)
    580     {
    581       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    582       return __builtin_islessequal(__type(__f1), __type(__f2));
    583     }
    584 
    585   template<typename _Tp>
    586     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    587 					   int>::__type
    588     islessgreater(_Tp __f1, _Tp __f2)
    589     {
    590       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    591       return __builtin_islessgreater(__type(__f1), __type(__f2));
    592     }
    593 
    594   template<typename _Tp>
    595     inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
    596 					   int>::__type
    597     isunordered(_Tp __f1, _Tp __f2)
    598     {
    599       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
    600       return __builtin_isunordered(__type(__f1), __type(__f2));
    601     }
    602 
    603 _GLIBCXX_END_NAMESPACE
    604 
    605 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
    606 #endif
    607 
    608 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    609 # include <bits/cmath.tcc>
    610 #endif
    611 
    612 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    613 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
    614 #    error C++0x header cannot be included from TR1 header
    615 #  endif
    616 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
    617 #    include <tr1_impl/cmath>
    618 #  else
    619 #    define _GLIBCXX_INCLUDE_AS_CXX0X
    620 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
    621 #    define _GLIBCXX_END_NAMESPACE_TR1
    622 #    define _GLIBCXX_TR1
    623 #    include <tr1_impl/cmath>
    624 #    undef _GLIBCXX_TR1
    625 #    undef _GLIBCXX_END_NAMESPACE_TR1
    626 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
    627 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
    628 #  endif
    629 #endif
    630 
    631 #endif
    632