1 // -*- C++ -*- C forwarding header.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /** @file include/cmath
32 * This is a Standard C++ Library file. You should @c #include this file
33 * in your programs, rather than any of the "*.h" implementation files.
34 *
35 * This is the C++ version of the Standard C Library header @c math.h,
36 * and its contents are (mostly) the same as that header, but are all
37 * contained in the namespace @c std (except for names which are defined
38 * as macros in C).
39 */
40
41 //
42 // ISO C++ 14882: 26.5 C library
43 //
44
45 #ifndef _GLIBCXX_CMATH
46 #define _GLIBCXX_CMATH 1
47
48 #pragma GCC system_header
49
50 #include <bits/c++config.h>
51 #include <bits/cpp_type_traits.h>
52 #include <ext/type_traits.h>
53
54 #include <math.h>
55
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
81
82 _GLIBCXX_BEGIN_NAMESPACE(std)
83
84 // Forward declaration of a helper function. This really should be
85 // an `exported' forward declaration.
86 template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
87
88 inline double
abs(double __x)89 abs(double __x)
90 { return __builtin_fabs(__x); }
91
92 inline float
abs(float __x)93 abs(float __x)
94 { return __builtin_fabsf(__x); }
95
96 inline long double
abs(long double __x)97 abs(long double __x)
98 { return __builtin_fabsl(__x); }
99
100 using ::acos;
101
102 inline float
acos(float __x)103 acos(float __x)
104 { return __builtin_acosf(__x); }
105
106 inline long double
acos(long double __x)107 acos(long double __x)
108 { return __builtin_acosl(__x); }
109
110 template<typename _Tp>
111 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
112 double>::__type
acos(_Tp __x)113 acos(_Tp __x)
114 { return __builtin_acos(__x); }
115
116 using ::asin;
117
118 inline float
asin(float __x)119 asin(float __x)
120 { return __builtin_asinf(__x); }
121
122 inline long double
asin(long double __x)123 asin(long double __x)
124 { return __builtin_asinl(__x); }
125
126 template<typename _Tp>
127 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
128 double>::__type
asin(_Tp __x)129 asin(_Tp __x)
130 { return __builtin_asin(__x); }
131
132 using ::atan;
133
134 inline float
atan(float __x)135 atan(float __x)
136 { return __builtin_atanf(__x); }
137
138 inline long double
atan(long double __x)139 atan(long double __x)
140 { return __builtin_atanl(__x); }
141
142 template<typename _Tp>
143 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
144 double>::__type
atan(_Tp __x)145 atan(_Tp __x)
146 { return __builtin_atan(__x); }
147
148 using ::atan2;
149
150 inline float
atan2(float __y,float __x)151 atan2(float __y, float __x)
152 { return __builtin_atan2f(__y, __x); }
153
154 inline long double
atan2(long double __y,long double __x)155 atan2(long double __y, long double __x)
156 { return __builtin_atan2l(__y, __x); }
157
158 template<typename _Tp, typename _Up>
159 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value
160 && __is_integer<_Up>::__value,
161 double>::__type
atan2(_Tp __y,_Up __x)162 atan2(_Tp __y, _Up __x)
163 { return __builtin_atan2(__y, __x); }
164
165 using ::ceil;
166
167 inline float
ceil(float __x)168 ceil(float __x)
169 { return __builtin_ceilf(__x); }
170
171 inline long double
ceil(long double __x)172 ceil(long double __x)
173 { return __builtin_ceill(__x); }
174
175 template<typename _Tp>
176 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
177 double>::__type
ceil(_Tp __x)178 ceil(_Tp __x)
179 { return __builtin_ceil(__x); }
180
181 using ::cos;
182
183 inline float
cos(float __x)184 cos(float __x)
185 { return __builtin_cosf(__x); }
186
187 inline long double
cos(long double __x)188 cos(long double __x)
189 { return __builtin_cosl(__x); }
190
191 template<typename _Tp>
192 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
193 double>::__type
cos(_Tp __x)194 cos(_Tp __x)
195 { return __builtin_cos(__x); }
196
197 using ::cosh;
198
199 inline float
cosh(float __x)200 cosh(float __x)
201 { return __builtin_coshf(__x); }
202
203 inline long double
cosh(long double __x)204 cosh(long double __x)
205 { return __builtin_coshl(__x); }
206
207 template<typename _Tp>
208 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
209 double>::__type
cosh(_Tp __x)210 cosh(_Tp __x)
211 { return __builtin_cosh(__x); }
212
213 using ::exp;
214
215 inline float
exp(float __x)216 exp(float __x)
217 { return __builtin_expf(__x); }
218
219 inline long double
exp(long double __x)220 exp(long double __x)
221 { return __builtin_expl(__x); }
222
223 template<typename _Tp>
224 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
225 double>::__type
exp(_Tp __x)226 exp(_Tp __x)
227 { return __builtin_exp(__x); }
228
229 using ::fabs;
230
231 inline float
fabs(float __x)232 fabs(float __x)
233 { return __builtin_fabsf(__x); }
234
235 inline long double
fabs(long double __x)236 fabs(long double __x)
237 { return __builtin_fabsl(__x); }
238
239 template<typename _Tp>
240 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
241 double>::__type
fabs(_Tp __x)242 fabs(_Tp __x)
243 { return __builtin_fabs(__x); }
244
245 using ::floor;
246
247 inline float
floor(float __x)248 floor(float __x)
249 { return __builtin_floorf(__x); }
250
251 inline long double
floor(long double __x)252 floor(long double __x)
253 { return __builtin_floorl(__x); }
254
255 template<typename _Tp>
256 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
257 double>::__type
floor(_Tp __x)258 floor(_Tp __x)
259 { return __builtin_floor(__x); }
260
261 using ::fmod;
262
263 inline float
fmod(float __x,float __y)264 fmod(float __x, float __y)
265 { return __builtin_fmodf(__x, __y); }
266
267 inline long double
fmod(long double __x,long double __y)268 fmod(long double __x, long double __y)
269 { return __builtin_fmodl(__x, __y); }
270
271 using ::frexp;
272
273 inline float
frexp(float __x,int * __exp)274 frexp(float __x, int* __exp)
275 { return __builtin_frexpf(__x, __exp); }
276
277 inline long double
frexp(long double __x,int * __exp)278 frexp(long double __x, int* __exp)
279 { return __builtin_frexpl(__x, __exp); }
280
281 template<typename _Tp>
282 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
283 double>::__type
frexp(_Tp __x,int * __exp)284 frexp(_Tp __x, int* __exp)
285 { return __builtin_frexp(__x, __exp); }
286
287 using ::ldexp;
288
289 inline float
ldexp(float __x,int __exp)290 ldexp(float __x, int __exp)
291 { return __builtin_ldexpf(__x, __exp); }
292
293 inline long double
ldexp(long double __x,int __exp)294 ldexp(long double __x, int __exp)
295 { return __builtin_ldexpl(__x, __exp); }
296
297 template<typename _Tp>
298 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
299 double>::__type
ldexp(_Tp __x,int __exp)300 ldexp(_Tp __x, int __exp)
301 { return __builtin_ldexp(__x, __exp); }
302
303 using ::log;
304
305 inline float
log(float __x)306 log(float __x)
307 { return __builtin_logf(__x); }
308
309 inline long double
log(long double __x)310 log(long double __x)
311 { return __builtin_logl(__x); }
312
313 template<typename _Tp>
314 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
315 double>::__type
log(_Tp __x)316 log(_Tp __x)
317 { return __builtin_log(__x); }
318
319 using ::log10;
320
321 inline float
log10(float __x)322 log10(float __x)
323 { return __builtin_log10f(__x); }
324
325 inline long double
log10(long double __x)326 log10(long double __x)
327 { return __builtin_log10l(__x); }
328
329 template<typename _Tp>
330 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
331 double>::__type
log10(_Tp __x)332 log10(_Tp __x)
333 { return __builtin_log10(__x); }
334
335 using ::modf;
336
337 inline float
modf(float __x,float * __iptr)338 modf(float __x, float* __iptr)
339 { return __builtin_modff(__x, __iptr); }
340
341 inline long double
modf(long double __x,long double * __iptr)342 modf(long double __x, long double* __iptr)
343 { return __builtin_modfl(__x, __iptr); }
344
345 template<typename _Tp>
346 inline _Tp
__pow_helper(_Tp __x,int __n)347 __pow_helper(_Tp __x, int __n)
348 {
349 return __n < 0
350 ? _Tp(1)/__cmath_power(__x, -__n)
351 : __cmath_power(__x, __n);
352 }
353
354 using ::pow;
355
356 inline float
pow(float __x,float __y)357 pow(float __x, float __y)
358 { return __builtin_powf(__x, __y); }
359
360 inline long double
pow(long double __x,long double __y)361 pow(long double __x, long double __y)
362 { return __builtin_powl(__x, __y); }
363
364 inline double
pow(double __x,int __i)365 pow(double __x, int __i)
366 { return __builtin_powi(__x, __i); }
367
368 inline float
pow(float __x,int __n)369 pow(float __x, int __n)
370 { return __builtin_powif(__x, __n); }
371
372 inline long double
pow(long double __x,int __n)373 pow(long double __x, int __n)
374 { return __builtin_powil(__x, __n); }
375
376 using ::sin;
377
378 inline float
sin(float __x)379 sin(float __x)
380 { return __builtin_sinf(__x); }
381
382 inline long double
sin(long double __x)383 sin(long double __x)
384 { return __builtin_sinl(__x); }
385
386 template<typename _Tp>
387 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
388 double>::__type
sin(_Tp __x)389 sin(_Tp __x)
390 { return __builtin_sin(__x); }
391
392 using ::sinh;
393
394 inline float
sinh(float __x)395 sinh(float __x)
396 { return __builtin_sinhf(__x); }
397
398 inline long double
sinh(long double __x)399 sinh(long double __x)
400 { return __builtin_sinhl(__x); }
401
402 template<typename _Tp>
403 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
404 double>::__type
sinh(_Tp __x)405 sinh(_Tp __x)
406 { return __builtin_sinh(__x); }
407
408 using ::sqrt;
409
410 inline float
sqrt(float __x)411 sqrt(float __x)
412 { return __builtin_sqrtf(__x); }
413
414 inline long double
sqrt(long double __x)415 sqrt(long double __x)
416 { return __builtin_sqrtl(__x); }
417
418 template<typename _Tp>
419 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
420 double>::__type
sqrt(_Tp __x)421 sqrt(_Tp __x)
422 { return __builtin_sqrt(__x); }
423
424 using ::tan;
425
426 inline float
tan(float __x)427 tan(float __x)
428 { return __builtin_tanf(__x); }
429
430 inline long double
tan(long double __x)431 tan(long double __x)
432 { return __builtin_tanl(__x); }
433
434 template<typename _Tp>
435 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
436 double>::__type
tan(_Tp __x)437 tan(_Tp __x)
438 { return __builtin_tan(__x); }
439
440 using ::tanh;
441
442 inline float
tanh(float __x)443 tanh(float __x)
444 { return __builtin_tanhf(__x); }
445
446 inline long double
tanh(long double __x)447 tanh(long double __x)
448 { return __builtin_tanhl(__x); }
449
450 template<typename _Tp>
451 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
452 double>::__type
tanh(_Tp __x)453 tanh(_Tp __x)
454 { return __builtin_tanh(__x); }
455
456 _GLIBCXX_END_NAMESPACE
457
458 #if _GLIBCXX_USE_C99_MATH
459 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
460 // These are possible macros imported from C99-land. For strict
461 // conformance, remove possible C99-injected names from the global
462 // namespace, and sequester them in the __gnu_cxx extension namespace.
463
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)464 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
465
466 template<typename _Tp>
467 inline int
468 __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
469
470 template<typename _Tp>
471 inline int
__capture_isfinite(_Tp __f)472 __capture_isfinite(_Tp __f) { return isfinite(__f); }
473
474 template<typename _Tp>
475 inline int
__capture_isinf(_Tp __f)476 __capture_isinf(_Tp __f) { return isinf(__f); }
477
478 template<typename _Tp>
479 inline int
__capture_isnan(_Tp __f)480 __capture_isnan(_Tp __f) { return isnan(__f); }
481
482 template<typename _Tp>
483 inline int
__capture_isnormal(_Tp __f)484 __capture_isnormal(_Tp __f) { return isnormal(__f); }
485
486 template<typename _Tp>
487 inline int
__capture_signbit(_Tp __f)488 __capture_signbit(_Tp __f) { return signbit(__f); }
489
490 template<typename _Tp>
491 inline int
__capture_isgreater(_Tp __f1,_Tp __f2)492 __capture_isgreater(_Tp __f1, _Tp __f2)
493 { return isgreater(__f1, __f2); }
494
495 template<typename _Tp>
496 inline int
__capture_isgreaterequal(_Tp __f1,_Tp __f2)497 __capture_isgreaterequal(_Tp __f1, _Tp __f2)
498 { return isgreaterequal(__f1, __f2); }
499
500 template<typename _Tp>
501 inline int
__capture_isless(_Tp __f1,_Tp __f2)502 __capture_isless(_Tp __f1, _Tp __f2) { return isless(__f1, __f2); }
503
504 template<typename _Tp>
505 inline int
__capture_islessequal(_Tp __f1,_Tp __f2)506 __capture_islessequal(_Tp __f1, _Tp __f2)
507 { return islessequal(__f1, __f2); }
508
509 template<typename _Tp>
510 inline int
__capture_islessgreater(_Tp __f1,_Tp __f2)511 __capture_islessgreater(_Tp __f1, _Tp __f2)
512 { return islessgreater(__f1, __f2); }
513
514 template<typename _Tp>
515 inline int
__capture_isunordered(_Tp __f1,_Tp __f2)516 __capture_isunordered(_Tp __f1, _Tp __f2)
517 { return isunordered(__f1, __f2); }
518
519 _GLIBCXX_END_NAMESPACE
520
521 // Only undefine the C99 FP macros, if actually captured for namespace movement
522 #undef fpclassify
523 #undef isfinite
524 #undef isinf
525 #undef isnan
526 #undef isnormal
527 #undef signbit
528 #undef isgreater
529 #undef isgreaterequal
530 #undef isless
531 #undef islessequal
532 #undef islessgreater
533 #undef isunordered
534
_GLIBCXX_BEGIN_NAMESPACE(std)535 _GLIBCXX_BEGIN_NAMESPACE(std)
536
537 template<typename _Tp>
538 inline int
539 fpclassify(_Tp __f) { return ::__gnu_cxx::__capture_fpclassify(__f); }
540
541 template<typename _Tp>
542 inline int
isfinite(_Tp __f)543 isfinite(_Tp __f) { return ::__gnu_cxx::__capture_isfinite(__f); }
544
545 template<typename _Tp>
546 inline int
isinf(_Tp __f)547 isinf(_Tp __f) { return ::__gnu_cxx::__capture_isinf(__f); }
548
549 template<typename _Tp>
550 inline int
isnan(_Tp __f)551 isnan(_Tp __f) { return ::__gnu_cxx::__capture_isnan(__f); }
552
553 template<typename _Tp>
554 inline int
isnormal(_Tp __f)555 isnormal(_Tp __f) { return ::__gnu_cxx::__capture_isnormal(__f); }
556
557 template<typename _Tp>
558 inline int
signbit(_Tp __f)559 signbit(_Tp __f) { return ::__gnu_cxx::__capture_signbit(__f); }
560
561 template<typename _Tp>
562 inline int
isgreater(_Tp __f1,_Tp __f2)563 isgreater(_Tp __f1, _Tp __f2)
564 { return ::__gnu_cxx::__capture_isgreater(__f1, __f2); }
565
566 template<typename _Tp>
567 inline int
isgreaterequal(_Tp __f1,_Tp __f2)568 isgreaterequal(_Tp __f1, _Tp __f2)
569 { return ::__gnu_cxx::__capture_isgreaterequal(__f1, __f2); }
570
571 template<typename _Tp>
572 inline int
isless(_Tp __f1,_Tp __f2)573 isless(_Tp __f1, _Tp __f2)
574 { return ::__gnu_cxx::__capture_isless(__f1, __f2); }
575
576 template<typename _Tp>
577 inline int
islessequal(_Tp __f1,_Tp __f2)578 islessequal(_Tp __f1, _Tp __f2)
579 { return ::__gnu_cxx::__capture_islessequal(__f1, __f2); }
580
581 template<typename _Tp>
582 inline int
islessgreater(_Tp __f1,_Tp __f2)583 islessgreater(_Tp __f1, _Tp __f2)
584 { return ::__gnu_cxx::__capture_islessgreater(__f1, __f2); }
585
586 template<typename _Tp>
587 inline int
isunordered(_Tp __f1,_Tp __f2)588 isunordered(_Tp __f1, _Tp __f2)
589 { return ::__gnu_cxx::__capture_isunordered(__f1, __f2); }
590
591 _GLIBCXX_END_NAMESPACE
592
593 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
594 #endif
595
596 #ifndef _GLIBCXX_EXPORT_TEMPLATE
597 # include <bits/cmath.tcc>
598 #endif
599
600 #endif
601