Refactor some libm type-generic macros.
[glibc.git] / math / math.h
blob75ccfc3bed0847b12a1e8bb99e9d71c6cf487bd9
1 /* Declarations for math functions.
2 Copyright (C) 1991-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 * ISO C99 Standard: 7.12 Mathematics <math.h>
23 #ifndef _MATH_H
24 #define _MATH_H 1
26 #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
27 #include <bits/libc-header-start.h>
29 __BEGIN_DECLS
31 /* Get machine-dependent vector math functions declarations. */
32 #include <bits/math-vector.h>
34 /* Get machine-dependent HUGE_VAL value (returned on overflow).
35 On all IEEE754 machines, this is +Infinity. */
36 #include <bits/huge_val.h>
37 #ifdef __USE_ISOC99
38 # include <bits/huge_valf.h>
39 # include <bits/huge_vall.h>
41 /* Get machine-dependent INFINITY value. */
42 # include <bits/inf.h>
44 /* Get machine-dependent NAN value (returned for some domain errors). */
45 # include <bits/nan.h>
46 #endif /* __USE_ISOC99 */
48 #if __GLIBC_USE (IEC_60559_BFP_EXT)
49 /* Signaling NaN macros, if supported. */
50 # if __GNUC_PREREQ (3, 3)
51 # define SNANF (__builtin_nansf (""))
52 # define SNAN (__builtin_nans (""))
53 # define SNANL (__builtin_nansl (""))
54 # endif
55 #endif
57 /* Get the architecture specific values describing the floating-point
58 evaluation. The following symbols will get defined:
60 float_t floating-point type at least as wide as `float' used
61 to evaluate `float' expressions
62 double_t floating-point type at least as wide as `double' used
63 to evaluate `double' expressions
65 FP_FAST_FMA
66 FP_FAST_FMAF
67 FP_FAST_FMAL
68 If defined it indicates that the `fma' function
69 generally executes about as fast as a multiply and an add.
70 This macro is defined only iff the `fma' function is
71 implemented directly with a hardware multiply-add instructions.
73 FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
74 FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
78 #include <bits/mathdef.h>
80 /* The file <bits/mathcalls.h> contains the prototypes for all the
81 actual math functions. These macros are used for those prototypes,
82 so we can easily declare each function as both `name' and `__name',
83 and can declare the float versions `namef' and `__namef'. */
85 #define __SIMD_DECL(function) __CONCAT (__DECL_SIMD_, function)
87 #define __MATHCALL_VEC(function, suffix, args) \
88 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
89 __MATHCALL (function, suffix, args)
91 #define __MATHDECL_VEC(type, function,suffix, args) \
92 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
93 __MATHDECL(type, function,suffix, args)
95 #define __MATHCALL(function,suffix, args) \
96 __MATHDECL (_Mdouble_,function,suffix, args)
97 #define __MATHDECL(type, function,suffix, args) \
98 __MATHDECL_1(type, function,suffix, args); \
99 __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
100 #define __MATHCALLX(function,suffix, args, attrib) \
101 __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
102 #define __MATHDECLX(type, function,suffix, args, attrib) \
103 __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
104 __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
105 #define __MATHDECL_1(type, function,suffix, args) \
106 extern type __MATH_PRECNAME(function,suffix) args __THROW
108 #define _Mdouble_ double
109 #define __MATH_PRECNAME(name,r) __CONCAT(name,r)
110 #define __MATH_DECLARING_DOUBLE 1
111 #define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_STD
112 #define _Mdouble_END_NAMESPACE __END_NAMESPACE_STD
113 #include <bits/mathcalls.h>
114 #undef _Mdouble_
115 #undef _Mdouble_BEGIN_NAMESPACE
116 #undef _Mdouble_END_NAMESPACE
117 #undef __MATH_PRECNAME
118 #undef __MATH_DECLARING_DOUBLE
120 #ifdef __USE_ISOC99
123 /* Include the file of declarations again, this time using `float'
124 instead of `double' and appending f to each function name. */
126 # ifndef _Mfloat_
127 # define _Mfloat_ float
128 # endif
129 # define _Mdouble_ _Mfloat_
130 # define __MATH_PRECNAME(name,r) name##f##r
131 # define __MATH_DECLARING_DOUBLE 0
132 # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
133 # define _Mdouble_END_NAMESPACE __END_NAMESPACE_C99
134 # include <bits/mathcalls.h>
135 # undef _Mdouble_
136 # undef _Mdouble_BEGIN_NAMESPACE
137 # undef _Mdouble_END_NAMESPACE
138 # undef __MATH_PRECNAME
139 # undef __MATH_DECLARING_DOUBLE
141 # if !(defined __NO_LONG_DOUBLE_MATH && defined _LIBC) \
142 || defined __LDBL_COMPAT \
143 || defined _LIBC_TEST
144 # ifdef __LDBL_COMPAT
146 # ifdef __USE_ISOC99
147 extern float __nldbl_nexttowardf (float __x, long double __y)
148 __THROW __attribute__ ((__const__));
149 # ifdef __REDIRECT_NTH
150 extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
151 __nldbl_nexttowardf)
152 __attribute__ ((__const__));
153 extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
154 nextafter) __attribute__ ((__const__));
155 extern long double __REDIRECT_NTH (nexttowardl,
156 (long double __x, long double __y),
157 nextafter) __attribute__ ((__const__));
158 # endif
159 # endif
161 # undef __MATHDECL_1
162 # define __MATHDECL_2(type, function,suffix, args, alias) \
163 extern type __REDIRECT_NTH(__MATH_PRECNAME(function,suffix), \
164 args, alias)
165 # define __MATHDECL_1(type, function,suffix, args) \
166 __MATHDECL_2(type, function,suffix, args, __CONCAT(function,suffix))
167 # endif
169 /* Include the file of declarations again, this time using `long double'
170 instead of `double' and appending l to each function name. */
172 # ifndef _Mlong_double_
173 # define _Mlong_double_ long double
174 # endif
175 # define _Mdouble_ _Mlong_double_
176 # define __MATH_PRECNAME(name,r) name##l##r
177 # define __MATH_DECLARING_DOUBLE 0
178 # define _Mdouble_BEGIN_NAMESPACE __BEGIN_NAMESPACE_C99
179 # define _Mdouble_END_NAMESPACE __END_NAMESPACE_C99
180 # define __MATH_DECLARE_LDOUBLE 1
181 # include <bits/mathcalls.h>
182 # undef _Mdouble_
183 # undef _Mdouble_BEGIN_NAMESPACE
184 # undef _Mdouble_END_NAMESPACE
185 # undef __MATH_PRECNAME
186 # undef __MATH_DECLARING_DOUBLE
188 # endif /* !(__NO_LONG_DOUBLE_MATH && _LIBC) || __LDBL_COMPAT */
190 #endif /* Use ISO C99. */
191 #undef __MATHDECL_1
192 #undef __MATHDECL
193 #undef __MATHCALL
196 #if defined __USE_MISC || defined __USE_XOPEN
197 /* This variable is used by `gamma' and `lgamma'. */
198 extern int signgam;
199 #endif
202 /* Depending on the type of TG_ARG, call an appropriately suffixed
203 version of FUNC with arguments (including parentheses) ARGS.
204 Suffixed functions may not exist for long double if it has the same
205 format as double, or for other types with the same format as float,
206 double or long double. The behavior is undefined if the argument
207 does not have a real floating type. The definition may use a
208 conditional expression, so all suffixed versions of FUNC must
209 return the same type (FUNC may include a cast if necessary rather
210 than being a single identifier). */
211 #ifdef __NO_LONG_DOUBLE_MATH
212 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
213 (sizeof (TG_ARG) == sizeof (float) ? FUNC ## f ARGS : FUNC ARGS)
214 #else
215 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
216 (sizeof (TG_ARG) == sizeof (float) \
217 ? FUNC ## f ARGS \
218 : sizeof (TG_ARG) == sizeof (double) \
219 ? FUNC ARGS \
220 : FUNC ## l ARGS)
221 #endif
223 /* ISO C99 defines some generic macros which work on any data type. */
224 #ifdef __USE_ISOC99
226 /* All floating-point numbers can be put in one of these categories. */
227 enum
229 FP_NAN =
230 # define FP_NAN 0
231 FP_NAN,
232 FP_INFINITE =
233 # define FP_INFINITE 1
234 FP_INFINITE,
235 FP_ZERO =
236 # define FP_ZERO 2
237 FP_ZERO,
238 FP_SUBNORMAL =
239 # define FP_SUBNORMAL 3
240 FP_SUBNORMAL,
241 FP_NORMAL =
242 # define FP_NORMAL 4
243 FP_NORMAL
246 /* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan,
247 so disable builtins if this is enabled. When fixed in a newer GCC,
248 the __SUPPORT_SNAN__ check may be skipped for those versions. */
250 /* Return number of classification appropriate for X. */
251 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__ \
252 && !defined __OPTIMIZE_SIZE__
253 # define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
254 FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
255 # else
256 # define fpclassify(x) __MATH_TG ((x), __fpclassify, (x))
257 # endif
259 /* Return nonzero value if sign of X is negative. */
260 # if __GNUC_PREREQ (4,0)
261 # define signbit(x) __MATH_TG ((x), __builtin_signbit, (x))
262 # else
263 # define signbit(x) __MATH_TG ((x), __signbit, (x))
264 # endif
266 /* Return nonzero value if X is not +-Inf or NaN. */
267 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
268 # define isfinite(x) __builtin_isfinite (x)
269 # else
270 # define isfinite(x) __MATH_TG ((x), __finite, (x))
271 # endif
273 /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
274 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
275 # define isnormal(x) __builtin_isnormal (x)
276 # else
277 # define isnormal(x) (fpclassify (x) == FP_NORMAL)
278 # endif
280 /* Return nonzero value if X is a NaN. We could use `fpclassify' but
281 we already have this functions `__isnan' and it is faster. */
282 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
283 # define isnan(x) __builtin_isnan (x)
284 # else
285 # define isnan(x) __MATH_TG ((x), __isnan, (x))
286 # endif
288 /* Return nonzero value if X is positive or negative infinity. */
289 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
290 # define isinf(x) __builtin_isinf_sign (x)
291 # else
292 # define isinf(x) __MATH_TG ((x), __isinf, (x))
293 # endif
295 /* Bitmasks for the math_errhandling macro. */
296 # define MATH_ERRNO 1 /* errno set by math functions. */
297 # define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */
299 /* By default all functions support both errno and exception handling.
300 In gcc's fast math mode and if inline functions are defined this
301 might not be true. */
302 # ifndef __FAST_MATH__
303 # define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
304 # endif
306 #endif /* Use ISO C99. */
308 #if __GLIBC_USE (IEC_60559_BFP_EXT)
309 # include <bits/iscanonical.h>
311 /* Return nonzero value if X is a signaling NaN. */
312 # define issignaling(x) __MATH_TG ((x), __issignaling, (x))
314 /* Return nonzero value if X is subnormal. */
315 # define issubnormal(x) (fpclassify (x) == FP_SUBNORMAL)
317 /* Return nonzero value if X is zero. */
318 # ifndef __cplusplus
319 # ifdef __SUPPORT_SNAN__
320 # define iszero(x) (fpclassify (x) == FP_ZERO)
321 # else
322 # define iszero(x) (((__typeof (x)) (x)) == 0)
323 # endif
324 # else /* __cplusplus */
325 extern "C++" {
326 template <class __T> inline bool
327 iszero (__T __val)
329 # ifdef __SUPPORT_SNAN__
330 return fpclassify (__val) == FP_ZERO;
331 # else
332 return __val == 0;
333 # endif
335 } /* extern C++ */
336 # endif /* __cplusplus */
337 #endif /* Use IEC_60559_BFP_EXT. */
339 #ifdef __USE_MISC
340 /* Support for various different standard error handling behaviors. */
341 typedef enum
343 _IEEE_ = -1, /* According to IEEE 754/IEEE 854. */
344 _SVID_, /* According to System V, release 4. */
345 _XOPEN_, /* Nowadays also Unix98. */
346 _POSIX_,
347 _ISOC_ /* Actually this is ISO C99. */
348 } _LIB_VERSION_TYPE;
350 /* This variable can be changed at run-time to any of the values above to
351 affect floating point error handling behavior (it may also be necessary
352 to change the hardware FPU exception settings). */
353 extern _LIB_VERSION_TYPE _LIB_VERSION;
354 #endif
357 #ifdef __USE_MISC
358 /* In SVID error handling, `matherr' is called with this description
359 of the exceptional condition.
361 We have a problem when using C++ since `exception' is a reserved
362 name in C++. */
363 # ifdef __cplusplus
364 struct __exception
365 # else
366 struct exception
367 # endif
369 int type;
370 char *name;
371 double arg1;
372 double arg2;
373 double retval;
376 # ifdef __cplusplus
377 extern int matherr (struct __exception *__exc) throw ();
378 # else
379 extern int matherr (struct exception *__exc);
380 # endif
382 # define X_TLOSS 1.41484755040568800000e+16
384 /* Types of exceptions in the `type' field. */
385 # define DOMAIN 1
386 # define SING 2
387 # define OVERFLOW 3
388 # define UNDERFLOW 4
389 # define TLOSS 5
390 # define PLOSS 6
392 /* SVID mode specifies returning this large value instead of infinity. */
393 # define HUGE 3.40282347e+38F
395 #else /* !Misc. */
397 # ifdef __USE_XOPEN
398 /* X/Open wants another strange constant. */
399 # define MAXFLOAT 3.40282347e+38F
400 # endif
402 #endif /* Misc. */
405 /* Some useful constants. */
406 #if defined __USE_MISC || defined __USE_XOPEN
407 # define M_E 2.7182818284590452354 /* e */
408 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
409 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
410 # define M_LN2 0.69314718055994530942 /* log_e 2 */
411 # define M_LN10 2.30258509299404568402 /* log_e 10 */
412 # define M_PI 3.14159265358979323846 /* pi */
413 # define M_PI_2 1.57079632679489661923 /* pi/2 */
414 # define M_PI_4 0.78539816339744830962 /* pi/4 */
415 # define M_1_PI 0.31830988618379067154 /* 1/pi */
416 # define M_2_PI 0.63661977236758134308 /* 2/pi */
417 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
418 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
419 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
420 #endif
422 /* The above constants are not adequate for computation using `long double's.
423 Therefore we provide as an extension constants with similar names as a
424 GNU extension. Provide enough digits for the 128-bit IEEE quad. */
425 #ifdef __USE_GNU
426 # define M_El 2.718281828459045235360287471352662498L /* e */
427 # define M_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
428 # define M_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
429 # define M_LN2l 0.693147180559945309417232121458176568L /* log_e 2 */
430 # define M_LN10l 2.302585092994045684017991454684364208L /* log_e 10 */
431 # define M_PIl 3.141592653589793238462643383279502884L /* pi */
432 # define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
433 # define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
434 # define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
435 # define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
436 # define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
437 # define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
438 # define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
439 #endif
442 /* When compiling in strict ISO C compatible mode we must not use the
443 inline functions since they, among other things, do not set the
444 `errno' variable correctly. */
445 #if defined __STRICT_ANSI__ && !defined __NO_MATH_INLINES
446 # define __NO_MATH_INLINES 1
447 #endif
449 #if defined __USE_ISOC99 && __GNUC_PREREQ(2,97)
450 /* ISO C99 defines some macros to compare number while taking care for
451 unordered numbers. Many FPUs provide special instructions to support
452 these operations. Generic support in GCC for these as builtins went
453 in before 3.0.0, but not all cpus added their patterns. We define
454 versions that use the builtins here, and <bits/mathinline.h> will
455 undef/redefine as appropriate for the specific GCC version in use. */
456 # define isgreater(x, y) __builtin_isgreater(x, y)
457 # define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
458 # define isless(x, y) __builtin_isless(x, y)
459 # define islessequal(x, y) __builtin_islessequal(x, y)
460 # define islessgreater(x, y) __builtin_islessgreater(x, y)
461 # define isunordered(u, v) __builtin_isunordered(u, v)
462 #endif
464 /* Get machine-dependent inline versions (if there are any). */
465 #ifdef __USE_EXTERN_INLINES
466 # include <bits/mathinline.h>
467 #endif
469 /* Define special entry points to use when the compiler got told to
470 only expect finite results. */
471 #if defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0
472 # include <bits/math-finite.h>
473 #endif
475 #ifdef __USE_ISOC99
476 /* If we've still got undefined comparison macros, provide defaults. */
478 /* Return nonzero value if X is greater than Y. */
479 # ifndef isgreater
480 # define isgreater(x, y) \
481 (__extension__ \
482 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
483 !isunordered (__x, __y) && __x > __y; }))
484 # endif
486 /* Return nonzero value if X is greater than or equal to Y. */
487 # ifndef isgreaterequal
488 # define isgreaterequal(x, y) \
489 (__extension__ \
490 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
491 !isunordered (__x, __y) && __x >= __y; }))
492 # endif
494 /* Return nonzero value if X is less than Y. */
495 # ifndef isless
496 # define isless(x, y) \
497 (__extension__ \
498 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
499 !isunordered (__x, __y) && __x < __y; }))
500 # endif
502 /* Return nonzero value if X is less than or equal to Y. */
503 # ifndef islessequal
504 # define islessequal(x, y) \
505 (__extension__ \
506 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
507 !isunordered (__x, __y) && __x <= __y; }))
508 # endif
510 /* Return nonzero value if either X is less than Y or Y is less than X. */
511 # ifndef islessgreater
512 # define islessgreater(x, y) \
513 (__extension__ \
514 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
515 !isunordered (__x, __y) && (__x < __y || __y < __x); }))
516 # endif
518 /* Return nonzero value if arguments are unordered. */
519 # ifndef isunordered
520 # define isunordered(u, v) \
521 (__extension__ \
522 ({ __typeof__(u) __u = (u); __typeof__(v) __v = (v); \
523 fpclassify (__u) == FP_NAN || fpclassify (__v) == FP_NAN; }))
524 # endif
526 #endif
528 #if __GLIBC_USE (IEC_60559_BFP_EXT)
529 /* An expression whose type has the widest of the evaluation formats
530 of X and Y (which are of floating-point types). */
531 # if __FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ > 64
532 # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0L)
533 # elif __FLT_EVAL_METHOD__ == 1 || __FLT_EVAL_METHOD__ > 32
534 # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0)
535 # else
536 # define __MATH_EVAL_FMT2(x, y) ((x) + (y))
537 # endif
539 /* Return X == Y but raising "invalid" and setting errno if X or Y is
540 a NaN. */
541 # define iseqsig(x, y) \
542 __MATH_TG (__MATH_EVAL_FMT2 (x, y), __iseqsig, ((x), (y)))
543 #endif
545 __END_DECLS
548 #endif /* math.h */