Include bits/mathcalls.h for more _FloatN, _FloatNx types.
[glibc.git] / math / math.h
blob326304202a8cf68c292d4dd1dde407db3f8dfb65
1 /* Declarations for math functions.
2 Copyright (C) 1991-2017 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 #if defined log && defined __GNUC__
30 # warning A macro called log was already defined when <math.h> was included.
31 # warning This will cause compilation problems.
32 #endif
34 __BEGIN_DECLS
36 /* Get definitions of __intmax_t and __uintmax_t. */
37 #include <bits/types.h>
39 /* Get machine-dependent vector math functions declarations. */
40 #include <bits/math-vector.h>
42 /* Gather machine dependent type support. */
43 #include <bits/floatn.h>
45 /* Value returned on overflow. With IEEE 754 floating point, this is
46 +Infinity, otherwise the largest representable positive value. */
47 #if __GNUC_PREREQ (3, 3)
48 # define HUGE_VAL (__builtin_huge_val ())
49 #else
50 /* This may provoke compiler warnings, and may not be rounded to
51 +Infinity in all IEEE 754 rounding modes, but is the best that can
52 be done in ISO C while remaining a constant expression. 10,000 is
53 greater than the maximum (decimal) exponent for all supported
54 floating-point formats and widths. */
55 # define HUGE_VAL 1e10000
56 #endif
57 #ifdef __USE_ISOC99
58 # if __GNUC_PREREQ (3, 3)
59 # define HUGE_VALF (__builtin_huge_valf ())
60 # define HUGE_VALL (__builtin_huge_vall ())
61 # else
62 # define HUGE_VALF 1e10000f
63 # define HUGE_VALL 1e10000L
64 # endif
65 #endif
66 #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
67 # define HUGE_VAL_F16 (__builtin_huge_valf16 ())
68 #endif
69 #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
70 # define HUGE_VAL_F32 (__builtin_huge_valf32 ())
71 #endif
72 #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
73 # define HUGE_VAL_F64 (__builtin_huge_valf64 ())
74 #endif
75 #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
76 # define HUGE_VAL_F128 (__builtin_huge_valf128 ())
77 #endif
78 #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
79 # define HUGE_VAL_F32X (__builtin_huge_valf32x ())
80 #endif
81 #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
82 # define HUGE_VAL_F64X (__builtin_huge_valf64x ())
83 #endif
84 #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
85 # define HUGE_VAL_F128X (__builtin_huge_valf128x ())
86 #endif
88 #ifdef __USE_ISOC99
89 /* IEEE positive infinity. */
90 # if __GNUC_PREREQ (3, 3)
91 # define INFINITY (__builtin_inff ())
92 # else
93 # define INFINITY HUGE_VALF
94 # endif
96 /* IEEE Not A Number. */
97 # if __GNUC_PREREQ (3, 3)
98 # define NAN (__builtin_nanf (""))
99 # else
100 /* This will raise an "invalid" exception outside static initializers,
101 but is the best that can be done in ISO C while remaining a
102 constant expression. */
103 # define NAN (0.0f / 0.0f)
104 # endif
105 #endif /* __USE_ISOC99 */
107 #if __GLIBC_USE (IEC_60559_BFP_EXT)
108 /* Signaling NaN macros, if supported. */
109 # if __GNUC_PREREQ (3, 3)
110 # define SNANF (__builtin_nansf (""))
111 # define SNAN (__builtin_nans (""))
112 # define SNANL (__builtin_nansl (""))
113 # endif
114 #endif
115 #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
116 # define SNANF16 (__builtin_nansf16 (""))
117 #endif
118 #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
119 # define SNANF32 (__builtin_nansf32 (""))
120 #endif
121 #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
122 # define SNANF64 (__builtin_nansf64 (""))
123 #endif
124 #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
125 # define SNANF128 (__builtin_nansf128 (""))
126 #endif
127 #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
128 # define SNANF32X (__builtin_nansf32x (""))
129 #endif
130 #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
131 # define SNANF64X (__builtin_nansf64x (""))
132 #endif
133 #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
134 # define SNANF128X (__builtin_nansf128x (""))
135 #endif
137 /* Get __GLIBC_FLT_EVAL_METHOD. */
138 #include <bits/flt-eval-method.h>
140 #ifdef __USE_ISOC99
141 /* Define the following typedefs.
143 float_t floating-point type at least as wide as `float' used
144 to evaluate `float' expressions
145 double_t floating-point type at least as wide as `double' used
146 to evaluate `double' expressions
148 # if __GLIBC_FLT_EVAL_METHOD == 0 || __GLIBC_FLT_EVAL_METHOD == 16
149 typedef float float_t;
150 typedef double double_t;
151 # elif __GLIBC_FLT_EVAL_METHOD == 1
152 typedef double float_t;
153 typedef double double_t;
154 # elif __GLIBC_FLT_EVAL_METHOD == 2
155 typedef long double float_t;
156 typedef long double double_t;
157 # elif __GLIBC_FLT_EVAL_METHOD == 32
158 typedef _Float32 float_t;
159 typedef double double_t;
160 # elif __GLIBC_FLT_EVAL_METHOD == 33
161 typedef _Float32x float_t;
162 typedef _Float32x double_t;
163 # elif __GLIBC_FLT_EVAL_METHOD == 64
164 typedef _Float64 float_t;
165 typedef _Float64 double_t;
166 # elif __GLIBC_FLT_EVAL_METHOD == 65
167 typedef _Float64x float_t;
168 typedef _Float64x double_t;
169 # elif __GLIBC_FLT_EVAL_METHOD == 128
170 typedef _Float128 float_t;
171 typedef _Float128 double_t;
172 # elif __GLIBC_FLT_EVAL_METHOD == 129
173 typedef _Float128x float_t;
174 typedef _Float128x double_t;
175 # else
176 # error "Unknown __GLIBC_FLT_EVAL_METHOD"
177 # endif
178 #endif
180 /* Define macros for the return values of ilogb and llogb, based on
181 __FP_LOGB0_IS_MIN and __FP_LOGBNAN_IS_MIN.
183 FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
184 FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
185 FP_LLOGB0 Expands to a value returned by `llogb (0.0)'.
186 FP_LLOGBNAN Expands to a value returned by `llogb (NAN)'.
190 #include <bits/fp-logb.h>
191 #ifdef __USE_ISOC99
192 # if __FP_LOGB0_IS_MIN
193 # define FP_ILOGB0 (-2147483647 - 1)
194 # else
195 # define FP_ILOGB0 (-2147483647)
196 # endif
197 # if __FP_LOGBNAN_IS_MIN
198 # define FP_ILOGBNAN (-2147483647 - 1)
199 # else
200 # define FP_ILOGBNAN 2147483647
201 # endif
202 #endif
203 #if __GLIBC_USE (IEC_60559_BFP_EXT)
204 # if __WORDSIZE == 32
205 # define __FP_LONG_MAX 0x7fffffffL
206 # else
207 # define __FP_LONG_MAX 0x7fffffffffffffffL
208 # endif
209 # if __FP_LOGB0_IS_MIN
210 # define FP_LLOGB0 (-__FP_LONG_MAX - 1)
211 # else
212 # define FP_LLOGB0 (-__FP_LONG_MAX)
213 # endif
214 # if __FP_LOGBNAN_IS_MIN
215 # define FP_LLOGBNAN (-__FP_LONG_MAX - 1)
216 # else
217 # define FP_LLOGBNAN __FP_LONG_MAX
218 # endif
219 #endif
221 /* Get the architecture specific values describing the floating-point
222 evaluation. The following symbols will get defined:
224 FP_FAST_FMA
225 FP_FAST_FMAF
226 FP_FAST_FMAL
227 If defined it indicates that the `fma' function
228 generally executes about as fast as a multiply and an add.
229 This macro is defined only iff the `fma' function is
230 implemented directly with a hardware multiply-add instructions.
233 #include <bits/fp-fast.h>
235 #if __GLIBC_USE (IEC_60559_BFP_EXT)
236 /* Rounding direction macros for fromfp functions. */
237 enum
239 FP_INT_UPWARD =
240 # define FP_INT_UPWARD 0
241 FP_INT_UPWARD,
242 FP_INT_DOWNWARD =
243 # define FP_INT_DOWNWARD 1
244 FP_INT_DOWNWARD,
245 FP_INT_TOWARDZERO =
246 # define FP_INT_TOWARDZERO 2
247 FP_INT_TOWARDZERO,
248 FP_INT_TONEARESTFROMZERO =
249 # define FP_INT_TONEARESTFROMZERO 3
250 FP_INT_TONEARESTFROMZERO,
251 FP_INT_TONEAREST =
252 # define FP_INT_TONEAREST 4
253 FP_INT_TONEAREST,
255 #endif
257 /* The file <bits/mathcalls.h> contains the prototypes for all the
258 actual math functions. These macros are used for those prototypes,
259 so we can easily declare each function as both `name' and `__name',
260 and can declare the float versions `namef' and `__namef'. */
262 #define __SIMD_DECL(function) __CONCAT (__DECL_SIMD_, function)
264 #define __MATHCALL_VEC(function, suffix, args) \
265 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
266 __MATHCALL (function, suffix, args)
268 #define __MATHDECL_VEC(type, function,suffix, args) \
269 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
270 __MATHDECL(type, function,suffix, args)
272 #define __MATHCALL(function,suffix, args) \
273 __MATHDECL (_Mdouble_,function,suffix, args)
274 #define __MATHDECL(type, function,suffix, args) \
275 __MATHDECL_1(type, function,suffix, args); \
276 __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
277 #define __MATHCALLX(function,suffix, args, attrib) \
278 __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
279 #define __MATHDECLX(type, function,suffix, args, attrib) \
280 __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
281 __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
282 #define __MATHDECL_1(type, function,suffix, args) \
283 extern type __MATH_PRECNAME(function,suffix) args __THROW
285 #define _Mdouble_ double
286 #define __MATH_PRECNAME(name,r) __CONCAT(name,r)
287 #define __MATH_DECLARING_DOUBLE 1
288 #define __MATH_DECLARING_FLOATN 0
289 #include <bits/mathcalls-helper-functions.h>
290 #include <bits/mathcalls.h>
291 #undef _Mdouble_
292 #undef __MATH_PRECNAME
293 #undef __MATH_DECLARING_DOUBLE
294 #undef __MATH_DECLARING_FLOATN
296 #ifdef __USE_ISOC99
299 /* Include the file of declarations again, this time using `float'
300 instead of `double' and appending f to each function name. */
302 # ifndef _Mfloat_
303 # define _Mfloat_ float
304 # endif
305 # define _Mdouble_ _Mfloat_
306 # define __MATH_PRECNAME(name,r) name##f##r
307 # define __MATH_DECLARING_DOUBLE 0
308 # define __MATH_DECLARING_FLOATN 0
309 # include <bits/mathcalls-helper-functions.h>
310 # include <bits/mathcalls.h>
311 # undef _Mdouble_
312 # undef __MATH_PRECNAME
313 # undef __MATH_DECLARING_DOUBLE
314 # undef __MATH_DECLARING_FLOATN
316 # if !(defined __NO_LONG_DOUBLE_MATH && defined _LIBC) \
317 || defined __LDBL_COMPAT \
318 || defined _LIBC_TEST
319 # ifdef __LDBL_COMPAT
321 # ifdef __USE_ISOC99
322 extern float __nldbl_nexttowardf (float __x, long double __y)
323 __THROW __attribute__ ((__const__));
324 # ifdef __REDIRECT_NTH
325 extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
326 __nldbl_nexttowardf)
327 __attribute__ ((__const__));
328 extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
329 nextafter) __attribute__ ((__const__));
330 extern long double __REDIRECT_NTH (nexttowardl,
331 (long double __x, long double __y),
332 nextafter) __attribute__ ((__const__));
333 # endif
334 # endif
336 # undef __MATHDECL_1
337 # define __MATHDECL_2(type, function,suffix, args, alias) \
338 extern type __REDIRECT_NTH(__MATH_PRECNAME(function,suffix), \
339 args, alias)
340 # define __MATHDECL_1(type, function,suffix, args) \
341 __MATHDECL_2(type, function,suffix, args, __CONCAT(function,suffix))
342 # endif
344 /* Include the file of declarations again, this time using `long double'
345 instead of `double' and appending l to each function name. */
347 # ifndef _Mlong_double_
348 # define _Mlong_double_ long double
349 # endif
350 # define _Mdouble_ _Mlong_double_
351 # define __MATH_PRECNAME(name,r) name##l##r
352 # define __MATH_DECLARING_DOUBLE 0
353 # define __MATH_DECLARING_FLOATN 0
354 # define __MATH_DECLARE_LDOUBLE 1
355 # include <bits/mathcalls-helper-functions.h>
356 # include <bits/mathcalls.h>
357 # undef _Mdouble_
358 # undef __MATH_PRECNAME
359 # undef __MATH_DECLARING_DOUBLE
360 # undef __MATH_DECLARING_FLOATN
362 # endif /* !(__NO_LONG_DOUBLE_MATH && _LIBC) || __LDBL_COMPAT */
364 #endif /* Use ISO C99. */
366 /* Include the file of declarations for _FloatN and _FloatNx
367 types. */
369 #if __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !defined _LIBC)
370 # ifndef _Mfloat16_
371 # define _Mfloat16_ _Float16
372 # endif
373 # define _Mdouble_ _Mfloat16_
374 # define __MATH_PRECNAME(name,r) name##f16##r
375 # define __MATH_DECLARING_DOUBLE 0
376 # define __MATH_DECLARING_FLOATN 1
377 # if __HAVE_DISTINCT_FLOAT16
378 # include <bits/mathcalls-helper-functions.h>
379 # endif
380 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
381 # include <bits/mathcalls.h>
382 # endif
383 # undef _Mdouble_
384 # undef __MATH_PRECNAME
385 # undef __MATH_DECLARING_DOUBLE
386 # undef __MATH_DECLARING_FLOATN
387 #endif /* __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !_LIBC). */
389 #if __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !defined _LIBC)
390 # ifndef _Mfloat32_
391 # define _Mfloat32_ _Float32
392 # endif
393 # define _Mdouble_ _Mfloat32_
394 # define __MATH_PRECNAME(name,r) name##f32##r
395 # define __MATH_DECLARING_DOUBLE 0
396 # define __MATH_DECLARING_FLOATN 1
397 # if __HAVE_DISTINCT_FLOAT32
398 # include <bits/mathcalls-helper-functions.h>
399 # endif
400 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
401 # include <bits/mathcalls.h>
402 # endif
403 # undef _Mdouble_
404 # undef __MATH_PRECNAME
405 # undef __MATH_DECLARING_DOUBLE
406 # undef __MATH_DECLARING_FLOATN
407 #endif /* __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !_LIBC). */
409 #if __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !defined _LIBC)
410 # ifndef _Mfloat64_
411 # define _Mfloat64_ _Float64
412 # endif
413 # define _Mdouble_ _Mfloat64_
414 # define __MATH_PRECNAME(name,r) name##f64##r
415 # define __MATH_DECLARING_DOUBLE 0
416 # define __MATH_DECLARING_FLOATN 1
417 # if __HAVE_DISTINCT_FLOAT64
418 # include <bits/mathcalls-helper-functions.h>
419 # endif
420 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
421 # include <bits/mathcalls.h>
422 # endif
423 # undef _Mdouble_
424 # undef __MATH_PRECNAME
425 # undef __MATH_DECLARING_DOUBLE
426 # undef __MATH_DECLARING_FLOATN
427 #endif /* __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !_LIBC). */
429 #if __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !defined _LIBC)
430 # ifndef _Mfloat128_
431 # define _Mfloat128_ _Float128
432 # endif
433 # define _Mdouble_ _Mfloat128_
434 # define __MATH_PRECNAME(name,r) name##f128##r
435 # define __MATH_DECLARING_DOUBLE 0
436 # define __MATH_DECLARING_FLOATN 1
437 # if __HAVE_DISTINCT_FLOAT128
438 # include <bits/mathcalls-helper-functions.h>
439 # endif
440 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
441 # include <bits/mathcalls.h>
442 # endif
443 # undef _Mdouble_
444 # undef __MATH_PRECNAME
445 # undef __MATH_DECLARING_DOUBLE
446 # undef __MATH_DECLARING_FLOATN
447 #endif /* __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !_LIBC). */
449 #if __HAVE_DISTINCT_FLOAT32X || (__HAVE_FLOAT32X && !defined _LIBC)
450 # ifndef _Mfloat32x_
451 # define _Mfloat32x_ _Float32x
452 # endif
453 # define _Mdouble_ _Mfloat32x_
454 # define __MATH_PRECNAME(name,r) name##f32x##r
455 # define __MATH_DECLARING_DOUBLE 0
456 # define __MATH_DECLARING_FLOATN 1
457 # if __HAVE_DISTINCT_FLOAT32X
458 # include <bits/mathcalls-helper-functions.h>
459 # endif
460 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
461 # include <bits/mathcalls.h>
462 # endif
463 # undef _Mdouble_
464 # undef __MATH_PRECNAME
465 # undef __MATH_DECLARING_DOUBLE
466 # undef __MATH_DECLARING_FLOATN
467 #endif /* __HAVE_DISTINCT_FLOAT32X || (__HAVE_FLOAT32X && !_LIBC). */
469 #if __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !defined _LIBC)
470 # ifndef _Mfloat64x_
471 # define _Mfloat64x_ _Float64x
472 # endif
473 # define _Mdouble_ _Mfloat64x_
474 # define __MATH_PRECNAME(name,r) name##f64x##r
475 # define __MATH_DECLARING_DOUBLE 0
476 # define __MATH_DECLARING_FLOATN 1
477 # if __HAVE_DISTINCT_FLOAT64X
478 # include <bits/mathcalls-helper-functions.h>
479 # endif
480 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
481 # include <bits/mathcalls.h>
482 # endif
483 # undef _Mdouble_
484 # undef __MATH_PRECNAME
485 # undef __MATH_DECLARING_DOUBLE
486 # undef __MATH_DECLARING_FLOATN
487 #endif /* __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !_LIBC). */
489 #if __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !defined _LIBC)
490 # ifndef _Mfloat128x_
491 # define _Mfloat128x_ _Float128x
492 # endif
493 # define _Mdouble_ _Mfloat128x_
494 # define __MATH_PRECNAME(name,r) name##f128x##r
495 # define __MATH_DECLARING_DOUBLE 0
496 # define __MATH_DECLARING_FLOATN 1
497 # if __HAVE_DISTINCT_FLOAT128X
498 # include <bits/mathcalls-helper-functions.h>
499 # endif
500 # if __GLIBC_USE (IEC_60559_TYPES_EXT)
501 # include <bits/mathcalls.h>
502 # endif
503 # undef _Mdouble_
504 # undef __MATH_PRECNAME
505 # undef __MATH_DECLARING_DOUBLE
506 # undef __MATH_DECLARING_FLOATN
507 #endif /* __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !_LIBC). */
509 #undef __MATHDECL_1
510 #undef __MATHDECL
511 #undef __MATHCALL
514 #if defined __USE_MISC || defined __USE_XOPEN
515 /* This variable is used by `gamma' and `lgamma'. */
516 extern int signgam;
517 #endif
520 /* Depending on the type of TG_ARG, call an appropriately suffixed
521 version of FUNC with arguments (including parentheses) ARGS.
522 Suffixed functions may not exist for long double if it has the same
523 format as double, or for other types with the same format as float,
524 double or long double. The behavior is undefined if the argument
525 does not have a real floating type. The definition may use a
526 conditional expression, so all suffixed versions of FUNC must
527 return the same type (FUNC may include a cast if necessary rather
528 than being a single identifier). */
529 #ifdef __NO_LONG_DOUBLE_MATH
530 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
531 (sizeof (TG_ARG) == sizeof (float) ? FUNC ## f ARGS : FUNC ARGS)
532 #elif __HAVE_DISTINCT_FLOAT128
533 # if __HAVE_GENERIC_SELECTION
534 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
535 _Generic ((TG_ARG), \
536 float: FUNC ## f ARGS, \
537 default: FUNC ARGS, \
538 long double: FUNC ## l ARGS, \
539 _Float128: FUNC ## f128 ARGS)
540 # else
541 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
542 __builtin_choose_expr \
543 (__builtin_types_compatible_p (__typeof (TG_ARG), float), \
544 FUNC ## f ARGS, \
545 __builtin_choose_expr \
546 (__builtin_types_compatible_p (__typeof (TG_ARG), double), \
547 FUNC ARGS, \
548 __builtin_choose_expr \
549 (__builtin_types_compatible_p (__typeof (TG_ARG), long double), \
550 FUNC ## l ARGS, \
551 FUNC ## f128 ARGS)))
552 # endif
553 #else
554 # define __MATH_TG(TG_ARG, FUNC, ARGS) \
555 (sizeof (TG_ARG) == sizeof (float) \
556 ? FUNC ## f ARGS \
557 : sizeof (TG_ARG) == sizeof (double) \
558 ? FUNC ARGS \
559 : FUNC ## l ARGS)
560 #endif
562 /* ISO C99 defines some generic macros which work on any data type. */
563 #ifdef __USE_ISOC99
565 /* All floating-point numbers can be put in one of these categories. */
566 enum
568 FP_NAN =
569 # define FP_NAN 0
570 FP_NAN,
571 FP_INFINITE =
572 # define FP_INFINITE 1
573 FP_INFINITE,
574 FP_ZERO =
575 # define FP_ZERO 2
576 FP_ZERO,
577 FP_SUBNORMAL =
578 # define FP_SUBNORMAL 3
579 FP_SUBNORMAL,
580 FP_NORMAL =
581 # define FP_NORMAL 4
582 FP_NORMAL
585 /* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan,
586 so disable builtins if this is enabled. When fixed in a newer GCC,
587 the __SUPPORT_SNAN__ check may be skipped for those versions. */
589 /* Return number of classification appropriate for X. */
590 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__ \
591 && (!defined __OPTIMIZE_SIZE__ || defined __cplusplus)
592 /* The check for __cplusplus allows the use of the builtin, even
593 when optimization for size is on. This is provided for
594 libstdc++, only to let its configure test work when it is built
595 with -Os. No further use of this definition of fpclassify is
596 expected in C++ mode, since libstdc++ provides its own version
597 of fpclassify in cmath (which undefines fpclassify). */
598 # define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
599 FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
600 # else
601 # define fpclassify(x) __MATH_TG ((x), __fpclassify, (x))
602 # endif
604 /* Return nonzero value if sign of X is negative. */
605 # if __GNUC_PREREQ (6,0)
606 # define signbit(x) __builtin_signbit (x)
607 # elif defined __cplusplus
608 /* In C++ mode, __MATH_TG cannot be used, because it relies on
609 __builtin_types_compatible_p, which is a C-only builtin.
610 The check for __cplusplus allows the use of the builtin instead of
611 __MATH_TG. This is provided for libstdc++, only to let its configure
612 test work. No further use of this definition of signbit is expected
613 in C++ mode, since libstdc++ provides its own version of signbit
614 in cmath (which undefines signbit). */
615 # define signbit(x) __builtin_signbitl (x)
616 # elif __GNUC_PREREQ (4,0)
617 # define signbit(x) __MATH_TG ((x), __builtin_signbit, (x))
618 # else
619 # define signbit(x) __MATH_TG ((x), __signbit, (x))
620 # endif
622 /* Return nonzero value if X is not +-Inf or NaN. */
623 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
624 # define isfinite(x) __builtin_isfinite (x)
625 # else
626 # define isfinite(x) __MATH_TG ((x), __finite, (x))
627 # endif
629 /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
630 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
631 # define isnormal(x) __builtin_isnormal (x)
632 # else
633 # define isnormal(x) (fpclassify (x) == FP_NORMAL)
634 # endif
636 /* Return nonzero value if X is a NaN. We could use `fpclassify' but
637 we already have this functions `__isnan' and it is faster. */
638 # if __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
639 # define isnan(x) __builtin_isnan (x)
640 # else
641 # define isnan(x) __MATH_TG ((x), __isnan, (x))
642 # endif
644 /* Return nonzero value if X is positive or negative infinity. */
645 # if __HAVE_DISTINCT_FLOAT128 && !__GNUC_PREREQ (7,0) \
646 && !defined __SUPPORT_SNAN__ && !defined __cplusplus
647 /* Since __builtin_isinf_sign is broken for float128 before GCC 7.0,
648 use the helper function, __isinff128, with older compilers. This is
649 only provided for C mode, because in C++ mode, GCC has no support
650 for __builtin_types_compatible_p (and when in C++ mode, this macro is
651 not used anyway, because libstdc++ headers undefine it). */
652 # define isinf(x) \
653 (__builtin_types_compatible_p (__typeof (x), _Float128) \
654 ? __isinff128 (x) : __builtin_isinf_sign (x))
655 # elif __GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__
656 # define isinf(x) __builtin_isinf_sign (x)
657 # else
658 # define isinf(x) __MATH_TG ((x), __isinf, (x))
659 # endif
661 /* Bitmasks for the math_errhandling macro. */
662 # define MATH_ERRNO 1 /* errno set by math functions. */
663 # define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */
665 /* By default all functions support both errno and exception handling.
666 In gcc's fast math mode and if inline functions are defined this
667 might not be true. */
668 # ifndef __FAST_MATH__
669 # define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
670 # endif
672 #endif /* Use ISO C99. */
674 #if __GLIBC_USE (IEC_60559_BFP_EXT)
675 # include <bits/iscanonical.h>
677 /* Return nonzero value if X is a signaling NaN. */
678 # ifndef __cplusplus
679 # define issignaling(x) __MATH_TG ((x), __issignaling, (x))
680 # else
681 /* In C++ mode, __MATH_TG cannot be used, because it relies on
682 __builtin_types_compatible_p, which is a C-only builtin. On the
683 other hand, overloading provides the means to distinguish between
684 the floating-point types. The overloading resolution will match
685 the correct parameter (regardless of type qualifiers (i.e.: const
686 and volatile)). */
687 extern "C++" {
688 inline int issignaling (float __val) { return __issignalingf (__val); }
689 inline int issignaling (double __val) { return __issignaling (__val); }
690 inline int
691 issignaling (long double __val)
693 # ifdef __NO_LONG_DOUBLE_MATH
694 return __issignaling (__val);
695 # else
696 return __issignalingl (__val);
697 # endif
699 # if __HAVE_DISTINCT_FLOAT128
700 inline int issignaling (_Float128 __val) { return __issignalingf128 (__val); }
701 # endif
702 } /* extern C++ */
703 # endif
705 /* Return nonzero value if X is subnormal. */
706 # define issubnormal(x) (fpclassify (x) == FP_SUBNORMAL)
708 /* Return nonzero value if X is zero. */
709 # ifndef __cplusplus
710 # ifdef __SUPPORT_SNAN__
711 # define iszero(x) (fpclassify (x) == FP_ZERO)
712 # else
713 # define iszero(x) (((__typeof (x)) (x)) == 0)
714 # endif
715 # else /* __cplusplus */
716 extern "C++" {
717 # ifdef __SUPPORT_SNAN__
718 inline int
719 iszero (float __val)
721 return __fpclassifyf (__val) == FP_ZERO;
723 inline int
724 iszero (double __val)
726 return __fpclassify (__val) == FP_ZERO;
728 inline int
729 iszero (long double __val)
731 # ifdef __NO_LONG_DOUBLE_MATH
732 return __fpclassify (__val) == FP_ZERO;
733 # else
734 return __fpclassifyl (__val) == FP_ZERO;
735 # endif
737 # if __HAVE_DISTINCT_FLOAT128
738 inline int
739 iszero (_Float128 __val)
741 return __fpclassifyf128 (__val) == FP_ZERO;
743 # endif
744 # else
745 template <class __T> inline bool
746 iszero (__T __val)
748 return __val == 0;
750 # endif
751 } /* extern C++ */
752 # endif /* __cplusplus */
753 #endif /* Use IEC_60559_BFP_EXT. */
755 #ifdef __USE_XOPEN
756 /* X/Open wants another strange constant. */
757 # define MAXFLOAT 3.40282347e+38F
758 #endif
761 /* Some useful constants. */
762 #if defined __USE_MISC || defined __USE_XOPEN
763 # define M_E 2.7182818284590452354 /* e */
764 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
765 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
766 # define M_LN2 0.69314718055994530942 /* log_e 2 */
767 # define M_LN10 2.30258509299404568402 /* log_e 10 */
768 # define M_PI 3.14159265358979323846 /* pi */
769 # define M_PI_2 1.57079632679489661923 /* pi/2 */
770 # define M_PI_4 0.78539816339744830962 /* pi/4 */
771 # define M_1_PI 0.31830988618379067154 /* 1/pi */
772 # define M_2_PI 0.63661977236758134308 /* 2/pi */
773 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
774 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
775 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
776 #endif
778 /* The above constants are not adequate for computation using `long double's.
779 Therefore we provide as an extension constants with similar names as a
780 GNU extension. Provide enough digits for the 128-bit IEEE quad. */
781 #ifdef __USE_GNU
782 # define M_El 2.718281828459045235360287471352662498L /* e */
783 # define M_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
784 # define M_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
785 # define M_LN2l 0.693147180559945309417232121458176568L /* log_e 2 */
786 # define M_LN10l 2.302585092994045684017991454684364208L /* log_e 10 */
787 # define M_PIl 3.141592653589793238462643383279502884L /* pi */
788 # define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
789 # define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
790 # define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
791 # define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
792 # define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
793 # define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
794 # define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
795 #endif
797 #if __HAVE_FLOAT128 && defined __USE_GNU
798 # define M_Ef128 __f128 (2.718281828459045235360287471352662498) /* e */
799 # define M_LOG2Ef128 __f128 (1.442695040888963407359924681001892137) /* log_2 e */
800 # define M_LOG10Ef128 __f128 (0.434294481903251827651128918916605082) /* log_10 e */
801 # define M_LN2f128 __f128 (0.693147180559945309417232121458176568) /* log_e 2 */
802 # define M_LN10f128 __f128 (2.302585092994045684017991454684364208) /* log_e 10 */
803 # define M_PIf128 __f128 (3.141592653589793238462643383279502884) /* pi */
804 # define M_PI_2f128 __f128 (1.570796326794896619231321691639751442) /* pi/2 */
805 # define M_PI_4f128 __f128 (0.785398163397448309615660845819875721) /* pi/4 */
806 # define M_1_PIf128 __f128 (0.318309886183790671537767526745028724) /* 1/pi */
807 # define M_2_PIf128 __f128 (0.636619772367581343075535053490057448) /* 2/pi */
808 # define M_2_SQRTPIf128 __f128 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
809 # define M_SQRT2f128 __f128 (1.414213562373095048801688724209698079) /* sqrt(2) */
810 # define M_SQRT1_2f128 __f128 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
811 #endif
813 /* When compiling in strict ISO C compatible mode we must not use the
814 inline functions since they, among other things, do not set the
815 `errno' variable correctly. */
816 #if defined __STRICT_ANSI__ && !defined __NO_MATH_INLINES
817 # define __NO_MATH_INLINES 1
818 #endif
820 #ifdef __USE_ISOC99
821 # if __GNUC_PREREQ (3, 1)
822 /* ISO C99 defines some macros to compare number while taking care for
823 unordered numbers. Many FPUs provide special instructions to support
824 these operations. Generic support in GCC for these as builtins went
825 in 2.97, but not all cpus added their patterns until 3.1. Therefore
826 we enable the builtins from 3.1 onwards and use a generic implementation
827 othwerwise. */
828 # define isgreater(x, y) __builtin_isgreater(x, y)
829 # define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
830 # define isless(x, y) __builtin_isless(x, y)
831 # define islessequal(x, y) __builtin_islessequal(x, y)
832 # define islessgreater(x, y) __builtin_islessgreater(x, y)
833 # define isunordered(x, y) __builtin_isunordered(x, y)
834 # else
835 # define isgreater(x, y) \
836 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
837 !isunordered (__x, __y) && __x > __y; }))
838 # define isgreaterequal(x, y) \
839 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
840 !isunordered (__x, __y) && __x >= __y; }))
841 # define isless(x, y) \
842 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
843 !isunordered (__x, __y) && __x < __y; }))
844 # define islessequal(x, y) \
845 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
846 !isunordered (__x, __y) && __x <= __y; }))
847 # define islessgreater(x, y) \
848 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
849 !isunordered (__x, __y) && __x != __y; }))
850 /* isunordered must always check both operands first for signaling NaNs. */
851 # define isunordered(x, y) \
852 (__extension__ ({ __typeof__ (x) __u = (x); __typeof__ (y) __v = (y); \
853 __u != __v && (__u != __u || __v != __v); }))
854 # endif
855 #endif
857 /* Get machine-dependent inline versions (if there are any). */
858 #ifdef __USE_EXTERN_INLINES
859 # include <bits/mathinline.h>
860 #endif
862 /* Define special entry points to use when the compiler got told to
863 only expect finite results. */
864 #if defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0
866 /* Include bits/math-finite.h for double. */
867 # define _Mdouble_ double
868 # define __MATH_DECLARING_DOUBLE 1
869 # define __MATH_DECLARING_FLOATN 0
870 # define __REDIRFROM_X(function, reentrant) \
871 function ## reentrant
872 # define __REDIRTO_X(function, reentrant) \
873 __ ## function ## reentrant ## _finite
874 # include <bits/math-finite.h>
875 # undef _Mdouble_
876 # undef __MATH_DECLARING_DOUBLE
877 # undef __MATH_DECLARING_FLOATN
878 # undef __REDIRFROM_X
879 # undef __REDIRTO_X
881 /* When __USE_ISOC99 is defined, include math-finite for float and
882 long double, as well. */
883 # ifdef __USE_ISOC99
885 /* Include bits/math-finite.h for float. */
886 # define _Mdouble_ float
887 # define __MATH_DECLARING_DOUBLE 0
888 # define __MATH_DECLARING_FLOATN 0
889 # define __REDIRFROM_X(function, reentrant) \
890 function ## f ## reentrant
891 # define __REDIRTO_X(function, reentrant) \
892 __ ## function ## f ## reentrant ## _finite
893 # include <bits/math-finite.h>
894 # undef _Mdouble_
895 # undef __MATH_DECLARING_DOUBLE
896 # undef __MATH_DECLARING_FLOATN
897 # undef __REDIRFROM_X
898 # undef __REDIRTO_X
900 /* Include bits/math-finite.h for long double. */
901 # ifdef __MATH_DECLARE_LDOUBLE
902 # define _Mdouble_ long double
903 # define __MATH_DECLARING_DOUBLE 0
904 # define __MATH_DECLARING_FLOATN 0
905 # define __REDIRFROM_X(function, reentrant) \
906 function ## l ## reentrant
907 # ifdef __NO_LONG_DOUBLE_MATH
908 # define __REDIRTO_X(function, reentrant) \
909 __ ## function ## reentrant ## _finite
910 # else
911 # define __REDIRTO_X(function, reentrant) \
912 __ ## function ## l ## reentrant ## _finite
913 # endif
914 # include <bits/math-finite.h>
915 # undef _Mdouble_
916 # undef __MATH_DECLARING_DOUBLE
917 # undef __MATH_DECLARING_FLOATN
918 # undef __REDIRFROM_X
919 # undef __REDIRTO_X
920 # endif
922 # endif /* __USE_ISOC99. */
924 /* Include bits/math-finite.h for float128. */
925 # if (__HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !defined _LIBC)) \
926 && __GLIBC_USE (IEC_60559_TYPES_EXT)
927 # define _Mdouble_ _Float128
928 # define __MATH_DECLARING_DOUBLE 0
929 # define __MATH_DECLARING_FLOATN 1
930 # define __REDIRFROM_X(function, reentrant) \
931 function ## f128 ## reentrant
932 # if __HAVE_DISTINCT_FLOAT128
933 # define __REDIRTO_X(function, reentrant) \
934 __ ## function ## f128 ## reentrant ## _finite
935 # else
936 # define __REDIRTO_X(function, reentrant) \
937 __ ## function ## l ## reentrant ## _finite
938 # endif
939 # include <bits/math-finite.h>
940 # undef _Mdouble_
941 # undef __MATH_DECLARING_DOUBLE
942 # undef __MATH_DECLARING_FLOATN
943 # undef __REDIRFROM_X
944 # undef __REDIRTO_X
945 # endif
946 #endif /* __FINITE_MATH_ONLY__ > 0. */
948 #if __GLIBC_USE (IEC_60559_BFP_EXT)
949 /* An expression whose type has the widest of the evaluation formats
950 of X and Y (which are of floating-point types). */
951 # if __FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ > 64
952 # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0L)
953 # elif __FLT_EVAL_METHOD__ == 1 || __FLT_EVAL_METHOD__ > 32
954 # define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0)
955 # else
956 # define __MATH_EVAL_FMT2(x, y) ((x) + (y))
957 # endif
959 /* Return X == Y but raising "invalid" and setting errno if X or Y is
960 a NaN. */
961 # define iseqsig(x, y) \
962 __MATH_TG (__MATH_EVAL_FMT2 (x, y), __iseqsig, ((x), (y)))
963 #endif
965 __END_DECLS
968 #endif /* math.h */