xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/c_std/cmath (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1// -*- C++ -*- C forwarding header.
2
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.
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 @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 */
36
37//
38// ISO C++ 14882: 26.5  C library
39//
40
41#ifndef _GLIBCXX_CMATH
42#define _GLIBCXX_CMATH 1
43
44#pragma GCC system_header
45
46#include <bits/c++config.h>
47#include <bits/cpp_type_traits.h>
48#include <ext/type_traits.h>
49
50#include <math.h>
51
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
77
78_GLIBCXX_BEGIN_NAMESPACE(std)
79
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);
83
84  inline double
85  abs(double __x)
86  { return __builtin_fabs(__x); }
87
88  inline float
89  abs(float __x)
90  { return __builtin_fabsf(__x); }
91
92  inline long double
93  abs(long double __x)
94  { return __builtin_fabsl(__x); }
95
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); }
101
102  using ::acos;
103
104  inline float
105  acos(float __x)
106  { return __builtin_acosf(__x); }
107
108  inline long double
109  acos(long double __x)
110  { return __builtin_acosl(__x); }
111
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); }
117
118  using ::asin;
119
120  inline float
121  asin(float __x)
122  { return __builtin_asinf(__x); }
123
124  inline long double
125  asin(long double __x)
126  { return __builtin_asinl(__x); }
127
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); }
133
134  using ::atan;
135
136  inline float
137  atan(float __x)
138  { return __builtin_atanf(__x); }
139
140  inline long double
141  atan(long double __x)
142  { return __builtin_atanl(__x); }
143
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); }
149
150  using ::atan2;
151
152  inline float
153  atan2(float __y, float __x)
154  { return __builtin_atan2f(__y, __x); }
155
156  inline long double
157  atan2(long double __y, long double __x)
158  { return __builtin_atan2l(__y, __x); }
159
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); }
166
167  using ::ceil;
168
169  inline float
170  ceil(float __x)
171  { return __builtin_ceilf(__x); }
172
173  inline long double
174  ceil(long double __x)
175  { return __builtin_ceill(__x); }
176
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); }
182
183  using ::cos;
184
185  inline float
186  cos(float __x)
187  { return __builtin_cosf(__x); }
188
189  inline long double
190  cos(long double __x)
191  { return __builtin_cosl(__x); }
192
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); }
198
199  using ::cosh;
200
201  inline float
202  cosh(float __x)
203  { return __builtin_coshf(__x); }
204
205  inline long double
206  cosh(long double __x)
207  { return __builtin_coshl(__x); }
208
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); }
214
215  using ::exp;
216
217  inline float
218  exp(float __x)
219  { return __builtin_expf(__x); }
220
221  inline long double
222  exp(long double __x)
223  { return __builtin_expl(__x); }
224
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); }
230
231  using ::fabs;
232
233  inline float
234  fabs(float __x)
235  { return __builtin_fabsf(__x); }
236
237  inline long double
238  fabs(long double __x)
239  { return __builtin_fabsl(__x); }
240
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); }
246
247  using ::floor;
248
249  inline float
250  floor(float __x)
251  { return __builtin_floorf(__x); }
252
253  inline long double
254  floor(long double __x)
255  { return __builtin_floorl(__x); }
256
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); }
262
263  using ::fmod;
264
265  inline float
266  fmod(float __x, float __y)
267  { return __builtin_fmodf(__x, __y); }
268
269  inline long double
270  fmod(long double __x, long double __y)
271  { return __builtin_fmodl(__x, __y); }
272
273  using ::frexp;
274
275  inline float
276  frexp(float __x, int* __exp)
277  { return __builtin_frexpf(__x, __exp); }
278
279  inline long double
280  frexp(long double __x, int* __exp)
281  { return __builtin_frexpl(__x, __exp); }
282
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); }
288
289  using ::ldexp;
290
291  inline float
292  ldexp(float __x, int __exp)
293  { return __builtin_ldexpf(__x, __exp); }
294
295  inline long double
296  ldexp(long double __x, int __exp)
297  { return __builtin_ldexpl(__x, __exp); }
298
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); }
304
305  using ::log;
306
307  inline float
308  log(float __x)
309  { return __builtin_logf(__x); }
310
311  inline long double
312  log(long double __x)
313  { return __builtin_logl(__x); }
314
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); }
320
321  using ::log10;
322
323  inline float
324  log10(float __x)
325  { return __builtin_log10f(__x); }
326
327  inline long double
328  log10(long double __x)
329  { return __builtin_log10l(__x); }
330
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); }
336
337  using ::modf;
338
339  inline float
340  modf(float __x, float* __iptr)
341  { return __builtin_modff(__x, __iptr); }
342
343  inline long double
344  modf(long double __x, long double* __iptr)
345  { return __builtin_modfl(__x, __iptr); }
346
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    }
355
356  using ::pow;
357
358  inline float
359  pow(float __x, float __y)
360  { return __builtin_powf(__x, __y); }
361
362  inline long double
363  pow(long double __x, long double __y)
364  { return __builtin_powl(__x, __y); }
365
366  inline double
367  pow(double __x, int __i)
368  { return __builtin_powi(__x, __i); }
369
370  inline float
371  pow(float __x, int __n)
372  { return __builtin_powif(__x, __n); }
373
374  inline long double
375  pow(long double __x, int __n)
376  { return __builtin_powil(__x, __n); }
377
378  using ::sin;
379
380  inline float
381  sin(float __x)
382  { return __builtin_sinf(__x); }
383
384  inline long double
385  sin(long double __x)
386  { return __builtin_sinl(__x); }
387
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); }
393
394  using ::sinh;
395
396  inline float
397  sinh(float __x)
398  { return __builtin_sinhf(__x); }
399
400  inline long double
401  sinh(long double __x)
402  { return __builtin_sinhl(__x); }
403
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); }
409
410  using ::sqrt;
411
412  inline float
413  sqrt(float __x)
414  { return __builtin_sqrtf(__x); }
415
416  inline long double
417  sqrt(long double __x)
418  { return __builtin_sqrtl(__x); }
419
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); }
425
426  using ::tan;
427
428  inline float
429  tan(float __x)
430  { return __builtin_tanf(__x); }
431
432  inline long double
433  tan(long double __x)
434  { return __builtin_tanl(__x); }
435
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); }
441
442  using ::tanh;
443
444  inline float
445  tanh(float __x)
446  { return __builtin_tanhf(__x); }
447
448  inline long double
449  tanh(long double __x)
450  { return __builtin_tanhl(__x); }
451
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); }
457
458_GLIBCXX_END_NAMESPACE
459
460#if _GLIBCXX_USE_C99_MATH
461#if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
462
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
476
477_GLIBCXX_BEGIN_NAMESPACE(std)
478
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    }
488
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    }
497
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    }
506
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    }
515
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    }
524
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    }
533
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    }
542
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    }
551
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    }
560
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    }
569
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    }
578
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    }
587
588_GLIBCXX_END_NAMESPACE
589
590#endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
591#endif
592
593#ifndef _GLIBCXX_EXPORT_TEMPLATE
594# include <bits/cmath.tcc>
595#endif
596
597#endif
598