]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/math.h
x86-64: Add vector exp2/exp2f implementation to libmvec
[thirdparty/glibc.git] / math / math.h
CommitLineData
f7eac6eb 1/* Declarations for math functions.
2b778ceb 2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
ba1ffaa1 3 This file is part of the GNU C Library.
28f540f4 4
ba1ffaa1 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 9
ba1ffaa1
UD
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
41bdb6e2 13 Lesser General Public License for more details.
28f540f4 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
28f540f4
RM
18
19/*
d1646309 20 * ISO C99 Standard: 7.12 Mathematics <math.h>
28f540f4
RM
21 */
22
23#ifndef _MATH_H
28f540f4 24#define _MATH_H 1
5107cf1d 25
bf91be88
JM
26#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
27#include <bits/libc-header-start.h>
28f540f4 28
b38042f5
FW
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
33
28f540f4
RM
34__BEGIN_DECLS
35
423c2b9d
JM
36/* Get definitions of __intmax_t and __uintmax_t. */
37#include <bits/types.h>
38
202d48da
AS
39/* Get machine-dependent vector math functions declarations. */
40#include <bits/math-vector.h>
41
fcee5905
PM
42/* Gather machine dependent type support. */
43#include <bits/floatn.h>
44
a617bd11
ZW
45/* Value returned on overflow. With IEEE 754 floating point, this is
46 +Infinity, otherwise the largest representable positive value. */
a60eca2e
JM
47#if __GNUC_PREREQ (3, 3)
48# define HUGE_VAL (__builtin_huge_val ())
49#else
a617bd11
ZW
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. */
a60eca2e
JM
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
5d220788
JM
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
fcee5905 75#if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
a60eca2e 76# define HUGE_VAL_F128 (__builtin_huge_valf128 ())
fcee5905 77#endif
5d220788
JM
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
fcee5905 87
a86573f4 88#ifdef __USE_ISOC99
5ef1b213
JM
89/* IEEE positive infinity. */
90# if __GNUC_PREREQ (3, 3)
91# define INFINITY (__builtin_inff ())
92# else
93# define INFINITY HUGE_VALF
94# endif
28f540f4 95
c0c49d60
JM
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
a86573f4
RH
105#endif /* __USE_ISOC99 */
106
858045ad 107#if __GLIBC_USE (IEC_60559_BFP_EXT)
f82a4bdb
JM
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
858045ad
JM
115#if (__HAVE_FLOAT16 \
116 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
117 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
118# define SNANF16 (__builtin_nansf16 (""))
119#endif
858045ad
JM
120#if (__HAVE_FLOAT32 \
121 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
122 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
123# define SNANF32 (__builtin_nansf32 (""))
124#endif
858045ad
JM
125#if (__HAVE_FLOAT64 \
126 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
127 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
128# define SNANF64 (__builtin_nansf64 (""))
129#endif
858045ad
JM
130#if (__HAVE_FLOAT128 \
131 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
132 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
302bb1a3
PM
133# define SNANF128 (__builtin_nansf128 (""))
134#endif
858045ad
JM
135#if (__HAVE_FLOAT32X \
136 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
137 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
138# define SNANF32X (__builtin_nansf32x (""))
139#endif
858045ad
JM
140#if (__HAVE_FLOAT64X \
141 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
142 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
143# define SNANF64X (__builtin_nansf64x (""))
144#endif
858045ad
JM
145#if (__HAVE_FLOAT128X \
146 && __GLIBC_USE (IEC_60559_TYPES_EXT) \
147 && (defined __USE_GNU || !__GLIBC_USE (ISOC2X)))
ecc7da5b
JM
148# define SNANF128X (__builtin_nansf128x (""))
149#endif
f82a4bdb 150
93eb85ce
JM
151/* Get __GLIBC_FLT_EVAL_METHOD. */
152#include <bits/flt-eval-method.h>
153
154#ifdef __USE_ISOC99
155/* Define the following typedefs.
89a12e6f
JM
156
157 float_t floating-point type at least as wide as `float' used
158 to evaluate `float' expressions
159 double_t floating-point type at least as wide as `double' used
160 to evaluate `double' expressions
93eb85ce
JM
161*/
162# if __GLIBC_FLT_EVAL_METHOD == 0 || __GLIBC_FLT_EVAL_METHOD == 16
163typedef float float_t;
164typedef double double_t;
165# elif __GLIBC_FLT_EVAL_METHOD == 1
166typedef double float_t;
167typedef double double_t;
168# elif __GLIBC_FLT_EVAL_METHOD == 2
169typedef long double float_t;
170typedef long double double_t;
171# elif __GLIBC_FLT_EVAL_METHOD == 32
172typedef _Float32 float_t;
173typedef double double_t;
174# elif __GLIBC_FLT_EVAL_METHOD == 33
175typedef _Float32x float_t;
176typedef _Float32x double_t;
177# elif __GLIBC_FLT_EVAL_METHOD == 64
178typedef _Float64 float_t;
179typedef _Float64 double_t;
180# elif __GLIBC_FLT_EVAL_METHOD == 65
181typedef _Float64x float_t;
182typedef _Float64x double_t;
183# elif __GLIBC_FLT_EVAL_METHOD == 128
184typedef _Float128 float_t;
185typedef _Float128 double_t;
186# elif __GLIBC_FLT_EVAL_METHOD == 129
187typedef _Float128x float_t;
188typedef _Float128x double_t;
189# else
190# error "Unknown __GLIBC_FLT_EVAL_METHOD"
191# endif
192#endif
193
55a38f82 194/* Define macros for the return values of ilogb and llogb, based on
b2491db6 195 __FP_LOGB0_IS_MIN and __FP_LOGBNAN_IS_MIN.
f11e220d
JM
196
197 FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
198 FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
55a38f82
JM
199 FP_LLOGB0 Expands to a value returned by `llogb (0.0)'.
200 FP_LLOGBNAN Expands to a value returned by `llogb (NAN)'.
f11e220d
JM
201
202*/
203
b2491db6
JM
204#include <bits/fp-logb.h>
205#ifdef __USE_ISOC99
206# if __FP_LOGB0_IS_MIN
207# define FP_ILOGB0 (-2147483647 - 1)
208# else
209# define FP_ILOGB0 (-2147483647)
210# endif
211# if __FP_LOGBNAN_IS_MIN
212# define FP_ILOGBNAN (-2147483647 - 1)
213# else
214# define FP_ILOGBNAN 2147483647
215# endif
216#endif
0175c9e9 217#if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
55a38f82
JM
218# if __WORDSIZE == 32
219# define __FP_LONG_MAX 0x7fffffffL
220# else
221# define __FP_LONG_MAX 0x7fffffffffffffffL
222# endif
223# if __FP_LOGB0_IS_MIN
224# define FP_LLOGB0 (-__FP_LONG_MAX - 1)
225# else
226# define FP_LLOGB0 (-__FP_LONG_MAX)
227# endif
228# if __FP_LOGBNAN_IS_MIN
229# define FP_LLOGBNAN (-__FP_LONG_MAX - 1)
230# else
231# define FP_LLOGBNAN __FP_LONG_MAX
232# endif
233#endif
b2491db6 234
93eb85ce
JM
235/* Get the architecture specific values describing the floating-point
236 evaluation. The following symbols will get defined:
89a12e6f
JM
237
238 FP_FAST_FMA
239 FP_FAST_FMAF
240 FP_FAST_FMAL
241 If defined it indicates that the `fma' function
242 generally executes about as fast as a multiply and an add.
243 This macro is defined only iff the `fma' function is
244 implemented directly with a hardware multiply-add instructions.
89a12e6f
JM
245*/
246
f11e220d 247#include <bits/fp-fast.h>
28f540f4 248
0175c9e9 249#if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
423c2b9d
JM
250/* Rounding direction macros for fromfp functions. */
251enum
252 {
253 FP_INT_UPWARD =
254# define FP_INT_UPWARD 0
255 FP_INT_UPWARD,
256 FP_INT_DOWNWARD =
257# define FP_INT_DOWNWARD 1
258 FP_INT_DOWNWARD,
259 FP_INT_TOWARDZERO =
260# define FP_INT_TOWARDZERO 2
261 FP_INT_TOWARDZERO,
262 FP_INT_TONEARESTFROMZERO =
263# define FP_INT_TONEARESTFROMZERO 3
264 FP_INT_TONEARESTFROMZERO,
265 FP_INT_TONEAREST =
266# define FP_INT_TONEAREST 4
267 FP_INT_TONEAREST,
268 };
269#endif
270
5107cf1d
UD
271/* The file <bits/mathcalls.h> contains the prototypes for all the
272 actual math functions. These macros are used for those prototypes,
273 so we can easily declare each function as both `name' and `__name',
f7eac6eb
RM
274 and can declare the float versions `namef' and `__namef'. */
275
202d48da
AS
276#define __SIMD_DECL(function) __CONCAT (__DECL_SIMD_, function)
277
278#define __MATHCALL_VEC(function, suffix, args) \
279 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
280 __MATHCALL (function, suffix, args)
281
c9a8c526
AS
282#define __MATHDECL_VEC(type, function,suffix, args) \
283 __SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
284 __MATHDECL(type, function,suffix, args)
285
f7eac6eb 286#define __MATHCALL(function,suffix, args) \
377a515b 287 __MATHDECL (_Mdouble_,function,suffix, args)
f7eac6eb
RM
288#define __MATHDECL(type, function,suffix, args) \
289 __MATHDECL_1(type, function,suffix, args); \
377a515b 290 __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
3e5f5557
UD
291#define __MATHCALLX(function,suffix, args, attrib) \
292 __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
293#define __MATHDECLX(type, function,suffix, args, attrib) \
294 __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
295 __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
77ad9735 296#define __MATHDECL_1_IMPL(type, function, suffix, args) \
c1422e5b 297 extern type __MATH_PRECNAME(function,suffix) args __THROW
77ad9735
TMQMF
298#define __MATHDECL_1(type, function, suffix, args) \
299 __MATHDECL_1_IMPL(type, function, suffix, args)
8dbfea3a
TMQMF
300/* Ignore the alias by default. The alias is only useful with
301 redirections. */
302#define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
303 __MATHDECL_1(type, function, suffix, args)
77ad9735
TMQMF
304
305#define __MATHREDIR(type, function, suffix, args, to) \
306 extern type __REDIRECT_NTH (__MATH_PRECNAME (function, suffix), args, to)
f7eac6eb 307
0ac5ae23 308#define _Mdouble_ double
377a515b 309#define __MATH_PRECNAME(name,r) __CONCAT(name,r)
de20571d 310#define __MATH_DECLARING_DOUBLE 1
fcee5905 311#define __MATH_DECLARING_FLOATN 0
ff80ec42 312#include <bits/mathcalls-helper-functions.h>
5107cf1d 313#include <bits/mathcalls.h>
f7eac6eb
RM
314#undef _Mdouble_
315#undef __MATH_PRECNAME
de20571d 316#undef __MATH_DECLARING_DOUBLE
fcee5905 317#undef __MATH_DECLARING_FLOATN
f7eac6eb 318
acd7f096 319#ifdef __USE_ISOC99
76060ec0 320
999493cb
RM
321
322/* Include the file of declarations again, this time using `float'
f7eac6eb
RM
323 instead of `double' and appending f to each function name. */
324
eda162dd 325# define _Mdouble_ float
7071ad79 326# define __MATH_PRECNAME(name,r) name##f##r
de20571d 327# define __MATH_DECLARING_DOUBLE 0
fcee5905 328# define __MATH_DECLARING_FLOATN 0
ff80ec42 329# include <bits/mathcalls-helper-functions.h>
478b92f0
UD
330# include <bits/mathcalls.h>
331# undef _Mdouble_
332# undef __MATH_PRECNAME
de20571d 333# undef __MATH_DECLARING_DOUBLE
fcee5905 334# undef __MATH_DECLARING_FLOATN
76060ec0 335
7071ad79 336# if !(defined __NO_LONG_DOUBLE_MATH && defined _LIBC) \
dc6fe23b
WN
337 || defined __LDBL_COMPAT \
338 || defined _LIBC_TEST
c6251f03
RM
339# ifdef __LDBL_COMPAT
340
7b943af6 341# ifdef __USE_ISOC99
c6251f03 342extern float __nldbl_nexttowardf (float __x, long double __y)
42247128 343 __THROW __attribute__ ((__const__));
c6251f03
RM
344# ifdef __REDIRECT_NTH
345extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
346 __nldbl_nexttowardf)
347 __attribute__ ((__const__));
348extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
349 nextafter) __attribute__ ((__const__));
42247128
RM
350extern long double __REDIRECT_NTH (nexttowardl,
351 (long double __x, long double __y),
352 nextafter) __attribute__ ((__const__));
c6251f03
RM
353# endif
354# endif
355
c6251f03 356# undef __MATHDECL_1
c6251f03 357# define __MATHDECL_1(type, function,suffix, args) \
77ad9735 358 __MATHREDIR(type, function, suffix, args, __CONCAT(function,suffix))
8dbfea3a 359
e2239af3 360# elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
8dbfea3a
TMQMF
361# ifdef __REDIRECT_NTH
362# ifdef __USE_ISOC99
363extern float __REDIRECT_NTH (nexttowardf, (float __x, long double __y),
364 __nexttowardf_to_ieee128)
365 __attribute__ ((__const__));
366extern double __REDIRECT_NTH (nexttoward, (double __x, long double __y),
367 __nexttoward_to_ieee128)
368 __attribute__ ((__const__));
369
370#define __dremieee128 __remainderieee128
371#define __gammaieee128 __lgammaieee128
372
373# endif
374# endif
375
376# undef __MATHDECL_1
377# undef __MATHDECL_ALIAS
378
379# define __REDIRTO(function, suffix) \
380 __ ## function ## ieee128 ## suffix
381# define __REDIRTO_ALT(function, suffix) \
382 __ ## function ## f128 ## suffix
383
384# define __MATHDECL_1(type, function, suffix, args) \
385 __MATHREDIR (type, function, suffix, args, __REDIRTO (function, suffix))
386# define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
387 __MATHREDIR (type, function, suffix, args, __REDIRTO_ALT (alias, suffix))
c6251f03
RM
388# endif
389
998832a4
AS
390/* Include the file of declarations again, this time using `long double'
391 instead of `double' and appending l to each function name. */
392
eda162dd 393# define _Mdouble_ long double
7071ad79 394# define __MATH_PRECNAME(name,r) name##l##r
de20571d 395# define __MATH_DECLARING_DOUBLE 0
fcee5905 396# define __MATH_DECLARING_FLOATN 0
0ac5ae23 397# define __MATH_DECLARE_LDOUBLE 1
ff80ec42 398# include <bits/mathcalls-helper-functions.h>
fe0ec73e 399# include <bits/mathcalls.h>
8dbfea3a 400
fe0ec73e
UD
401# undef _Mdouble_
402# undef __MATH_PRECNAME
de20571d 403# undef __MATH_DECLARING_DOUBLE
fcee5905 404# undef __MATH_DECLARING_FLOATN
fe0ec73e 405
8dbfea3a 406# if defined __LDBL_COMPAT \
e2239af3 407 || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
8dbfea3a
TMQMF
408# undef __REDIRTO
409# undef __REDIRTO_ALT
77ad9735 410# undef __MATHDECL_1
8dbfea3a 411# undef __MATHDECL_ALIAS
77ad9735
TMQMF
412# define __MATHDECL_1(type, function, suffix, args) \
413 __MATHDECL_1_IMPL(type, function, suffix, args)
8dbfea3a
TMQMF
414# define __MATHDECL_ALIAS(type, function, suffix, args, alias) \
415 __MATHDECL_1(type, function, suffix, args)
77ad9735 416# endif
7071ad79 417# endif /* !(__NO_LONG_DOUBLE_MATH && _LIBC) || __LDBL_COMPAT */
377a515b 418
acd7f096 419#endif /* Use ISO C99. */
fcee5905 420
7e9d7073
JM
421/* Include the file of declarations for _FloatN and _FloatNx
422 types. */
423
424#if __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !defined _LIBC)
eda162dd 425# define _Mdouble_ _Float16
7e9d7073
JM
426# define __MATH_PRECNAME(name,r) name##f16##r
427# define __MATH_DECLARING_DOUBLE 0
428# define __MATH_DECLARING_FLOATN 1
429# if __HAVE_DISTINCT_FLOAT16
430# include <bits/mathcalls-helper-functions.h>
431# endif
432# if __GLIBC_USE (IEC_60559_TYPES_EXT)
433# include <bits/mathcalls.h>
434# endif
435# undef _Mdouble_
436# undef __MATH_PRECNAME
437# undef __MATH_DECLARING_DOUBLE
438# undef __MATH_DECLARING_FLOATN
439#endif /* __HAVE_DISTINCT_FLOAT16 || (__HAVE_FLOAT16 && !_LIBC). */
440
441#if __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !defined _LIBC)
eda162dd 442# define _Mdouble_ _Float32
7e9d7073
JM
443# define __MATH_PRECNAME(name,r) name##f32##r
444# define __MATH_DECLARING_DOUBLE 0
445# define __MATH_DECLARING_FLOATN 1
446# if __HAVE_DISTINCT_FLOAT32
447# include <bits/mathcalls-helper-functions.h>
448# endif
449# if __GLIBC_USE (IEC_60559_TYPES_EXT)
450# include <bits/mathcalls.h>
451# endif
452# undef _Mdouble_
453# undef __MATH_PRECNAME
454# undef __MATH_DECLARING_DOUBLE
455# undef __MATH_DECLARING_FLOATN
456#endif /* __HAVE_DISTINCT_FLOAT32 || (__HAVE_FLOAT32 && !_LIBC). */
457
458#if __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !defined _LIBC)
eda162dd 459# define _Mdouble_ _Float64
7e9d7073
JM
460# define __MATH_PRECNAME(name,r) name##f64##r
461# define __MATH_DECLARING_DOUBLE 0
462# define __MATH_DECLARING_FLOATN 1
463# if __HAVE_DISTINCT_FLOAT64
464# include <bits/mathcalls-helper-functions.h>
465# endif
466# if __GLIBC_USE (IEC_60559_TYPES_EXT)
467# include <bits/mathcalls.h>
468# endif
469# undef _Mdouble_
470# undef __MATH_PRECNAME
471# undef __MATH_DECLARING_DOUBLE
472# undef __MATH_DECLARING_FLOATN
473#endif /* __HAVE_DISTINCT_FLOAT64 || (__HAVE_FLOAT64 && !_LIBC). */
fcee5905
PM
474
475#if __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !defined _LIBC)
eda162dd 476# define _Mdouble_ _Float128
fcee5905
PM
477# define __MATH_PRECNAME(name,r) name##f128##r
478# define __MATH_DECLARING_DOUBLE 0
479# define __MATH_DECLARING_FLOATN 1
480# if __HAVE_DISTINCT_FLOAT128
481# include <bits/mathcalls-helper-functions.h>
482# endif
483# if __GLIBC_USE (IEC_60559_TYPES_EXT)
484# include <bits/mathcalls.h>
485# endif
486# undef _Mdouble_
487# undef __MATH_PRECNAME
488# undef __MATH_DECLARING_DOUBLE
489# undef __MATH_DECLARING_FLOATN
7e9d7073
JM
490#endif /* __HAVE_DISTINCT_FLOAT128 || (__HAVE_FLOAT128 && !_LIBC). */
491
492#if __HAVE_DISTINCT_FLOAT32X || (__HAVE_FLOAT32X && !defined _LIBC)
eda162dd 493# define _Mdouble_ _Float32x
7e9d7073
JM
494# define __MATH_PRECNAME(name,r) name##f32x##r
495# define __MATH_DECLARING_DOUBLE 0
496# define __MATH_DECLARING_FLOATN 1
497# if __HAVE_DISTINCT_FLOAT32X
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_FLOAT32X || (__HAVE_FLOAT32X && !_LIBC). */
508
509#if __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !defined _LIBC)
eda162dd 510# define _Mdouble_ _Float64x
7e9d7073
JM
511# define __MATH_PRECNAME(name,r) name##f64x##r
512# define __MATH_DECLARING_DOUBLE 0
513# define __MATH_DECLARING_FLOATN 1
514# if __HAVE_DISTINCT_FLOAT64X
515# include <bits/mathcalls-helper-functions.h>
516# endif
517# if __GLIBC_USE (IEC_60559_TYPES_EXT)
518# include <bits/mathcalls.h>
519# endif
520# undef _Mdouble_
521# undef __MATH_PRECNAME
522# undef __MATH_DECLARING_DOUBLE
523# undef __MATH_DECLARING_FLOATN
524#endif /* __HAVE_DISTINCT_FLOAT64X || (__HAVE_FLOAT64X && !_LIBC). */
525
526#if __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !defined _LIBC)
eda162dd 527# define _Mdouble_ _Float128x
7e9d7073
JM
528# define __MATH_PRECNAME(name,r) name##f128x##r
529# define __MATH_DECLARING_DOUBLE 0
530# define __MATH_DECLARING_FLOATN 1
531# if __HAVE_DISTINCT_FLOAT128X
532# include <bits/mathcalls-helper-functions.h>
533# endif
534# if __GLIBC_USE (IEC_60559_TYPES_EXT)
535# include <bits/mathcalls.h>
536# endif
537# undef _Mdouble_
538# undef __MATH_PRECNAME
539# undef __MATH_DECLARING_DOUBLE
540# undef __MATH_DECLARING_FLOATN
541#endif /* __HAVE_DISTINCT_FLOAT128X || (__HAVE_FLOAT128X && !_LIBC). */
fcee5905 542
77ad9735 543#undef __MATHDECL_1_IMPL
4cca6b86 544#undef __MATHDECL_1
8dbfea3a 545#undef __MATHDECL_ALIAS
4cca6b86
UD
546#undef __MATHDECL
547#undef __MATHCALL
548
63716ab2
JM
549/* Declare functions returning a narrower type. */
550#define __MATHCALL_NARROW_ARGS_1 (_Marg_ __x)
551#define __MATHCALL_NARROW_ARGS_2 (_Marg_ __x, _Marg_ __y)
552#define __MATHCALL_NARROW_ARGS_3 (_Marg_ __x, _Marg_ __y, _Marg_ __z)
553#define __MATHCALL_NARROW_NORMAL(func, nargs) \
554 extern _Mret_ func __MATHCALL_NARROW_ARGS_ ## nargs __THROW
555#define __MATHCALL_NARROW_REDIR(func, redir, nargs) \
556 extern _Mret_ __REDIRECT_NTH (func, __MATHCALL_NARROW_ARGS_ ## nargs, \
557 redir)
558#define __MATHCALL_NARROW(func, redir, nargs) \
559 __MATHCALL_NARROW_NORMAL (func, nargs)
560
0175c9e9 561#if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
63716ab2
JM
562
563# define _Mret_ float
564# define _Marg_ double
565# define __MATHCALL_NAME(name) f ## name
566# include <bits/mathcalls-narrow.h>
567# undef _Mret_
568# undef _Marg_
569# undef __MATHCALL_NAME
570
571# define _Mret_ float
572# define _Marg_ long double
573# define __MATHCALL_NAME(name) f ## name ## l
574# ifdef __LDBL_COMPAT
575# define __MATHCALL_REDIR_NAME(name) f ## name
abd38358 576# define __MATHCALL_REDIR_NAME2(name) f ## name
63716ab2 577# undef __MATHCALL_NARROW
8dbfea3a
TMQMF
578# define __MATHCALL_NARROW(func, redir, nargs) \
579 __MATHCALL_NARROW_REDIR (func, redir, nargs)
e2239af3 580# elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
8dbfea3a 581# define __MATHCALL_REDIR_NAME(name) __ ## f32 ## name ## ieee128
abd38358 582# define __MATHCALL_REDIR_NAME2(name) __ ## f32 ## name ## ieee128
8dbfea3a 583# undef __MATHCALL_NARROW
63716ab2
JM
584# define __MATHCALL_NARROW(func, redir, nargs) \
585 __MATHCALL_NARROW_REDIR (func, redir, nargs)
586# endif
587# include <bits/mathcalls-narrow.h>
588# undef _Mret_
589# undef _Marg_
590# undef __MATHCALL_NAME
8dbfea3a 591# if defined __LDBL_COMPAT \
e2239af3 592 || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
63716ab2 593# undef __MATHCALL_REDIR_NAME
abd38358 594# undef __MATHCALL_REDIR_NAME2
63716ab2
JM
595# undef __MATHCALL_NARROW
596# define __MATHCALL_NARROW(func, redir, nargs) \
597 __MATHCALL_NARROW_NORMAL (func, nargs)
598# endif
599
600# define _Mret_ double
601# define _Marg_ long double
602# define __MATHCALL_NAME(name) d ## name ## l
603# ifdef __LDBL_COMPAT
604# define __MATHCALL_REDIR_NAME(name) __nldbl_d ## name ## l
abd38358 605# define __MATHCALL_REDIR_NAME2(name) name
63716ab2 606# undef __MATHCALL_NARROW
8dbfea3a
TMQMF
607# define __MATHCALL_NARROW(func, redir, nargs) \
608 __MATHCALL_NARROW_REDIR (func, redir, nargs)
e2239af3 609# elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
8dbfea3a 610# define __MATHCALL_REDIR_NAME(name) __ ## f64 ## name ## ieee128
abd38358 611# define __MATHCALL_REDIR_NAME2(name) __ ## f64 ## name ## ieee128
8dbfea3a 612# undef __MATHCALL_NARROW
63716ab2
JM
613# define __MATHCALL_NARROW(func, redir, nargs) \
614 __MATHCALL_NARROW_REDIR (func, redir, nargs)
615# endif
616# include <bits/mathcalls-narrow.h>
617# undef _Mret_
618# undef _Marg_
619# undef __MATHCALL_NAME
8dbfea3a 620# if defined __LDBL_COMPAT \
e2239af3 621 || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
63716ab2 622# undef __MATHCALL_REDIR_NAME
abd38358 623# undef __MATHCALL_REDIR_NAME2
63716ab2
JM
624# undef __MATHCALL_NARROW
625# define __MATHCALL_NARROW(func, redir, nargs) \
626 __MATHCALL_NARROW_NORMAL (func, nargs)
627# endif
628
629#endif
630
631#if __GLIBC_USE (IEC_60559_TYPES_EXT)
632
633# if __HAVE_FLOAT16 && __HAVE_FLOAT32
634# define _Mret_ _Float16
635# define _Marg_ _Float32
636# define __MATHCALL_NAME(name) f16 ## name ## f32
637# include <bits/mathcalls-narrow.h>
638# undef _Mret_
639# undef _Marg_
640# undef __MATHCALL_NAME
641# endif
642
643# if __HAVE_FLOAT16 && __HAVE_FLOAT32X
644# define _Mret_ _Float16
645# define _Marg_ _Float32x
646# define __MATHCALL_NAME(name) f16 ## name ## f32x
647# include <bits/mathcalls-narrow.h>
648# undef _Mret_
649# undef _Marg_
650# undef __MATHCALL_NAME
651# endif
652
653# if __HAVE_FLOAT16 && __HAVE_FLOAT64
654# define _Mret_ _Float16
655# define _Marg_ _Float64
656# define __MATHCALL_NAME(name) f16 ## name ## f64
657# include <bits/mathcalls-narrow.h>
658# undef _Mret_
659# undef _Marg_
660# undef __MATHCALL_NAME
661# endif
662
663# if __HAVE_FLOAT16 && __HAVE_FLOAT64X
664# define _Mret_ _Float16
665# define _Marg_ _Float64x
666# define __MATHCALL_NAME(name) f16 ## name ## f64x
667# include <bits/mathcalls-narrow.h>
668# undef _Mret_
669# undef _Marg_
670# undef __MATHCALL_NAME
671# endif
672
673# if __HAVE_FLOAT16 && __HAVE_FLOAT128
674# define _Mret_ _Float16
675# define _Marg_ _Float128
676# define __MATHCALL_NAME(name) f16 ## name ## f128
677# include <bits/mathcalls-narrow.h>
678# undef _Mret_
679# undef _Marg_
680# undef __MATHCALL_NAME
681# endif
682
683# if __HAVE_FLOAT16 && __HAVE_FLOAT128X
684# define _Mret_ _Float16
685# define _Marg_ _Float128x
686# define __MATHCALL_NAME(name) f16 ## name ## f128x
687# include <bits/mathcalls-narrow.h>
688# undef _Mret_
689# undef _Marg_
690# undef __MATHCALL_NAME
691# endif
692
693# if __HAVE_FLOAT32 && __HAVE_FLOAT32X
694# define _Mret_ _Float32
695# define _Marg_ _Float32x
696# define __MATHCALL_NAME(name) f32 ## name ## f32x
697# include <bits/mathcalls-narrow.h>
698# undef _Mret_
699# undef _Marg_
700# undef __MATHCALL_NAME
701# endif
702
703# if __HAVE_FLOAT32 && __HAVE_FLOAT64
704# define _Mret_ _Float32
705# define _Marg_ _Float64
706# define __MATHCALL_NAME(name) f32 ## name ## f64
707# include <bits/mathcalls-narrow.h>
708# undef _Mret_
709# undef _Marg_
710# undef __MATHCALL_NAME
711# endif
712
713# if __HAVE_FLOAT32 && __HAVE_FLOAT64X
714# define _Mret_ _Float32
715# define _Marg_ _Float64x
716# define __MATHCALL_NAME(name) f32 ## name ## f64x
717# include <bits/mathcalls-narrow.h>
718# undef _Mret_
719# undef _Marg_
720# undef __MATHCALL_NAME
721# endif
722
723# if __HAVE_FLOAT32 && __HAVE_FLOAT128
724# define _Mret_ _Float32
725# define _Marg_ _Float128
726# define __MATHCALL_NAME(name) f32 ## name ## f128
727# include <bits/mathcalls-narrow.h>
728# undef _Mret_
729# undef _Marg_
730# undef __MATHCALL_NAME
731# endif
732
733# if __HAVE_FLOAT32 && __HAVE_FLOAT128X
734# define _Mret_ _Float32
735# define _Marg_ _Float128x
736# define __MATHCALL_NAME(name) f32 ## name ## f128x
737# include <bits/mathcalls-narrow.h>
738# undef _Mret_
739# undef _Marg_
740# undef __MATHCALL_NAME
741# endif
742
743# if __HAVE_FLOAT32X && __HAVE_FLOAT64
744# define _Mret_ _Float32x
745# define _Marg_ _Float64
746# define __MATHCALL_NAME(name) f32x ## name ## f64
747# include <bits/mathcalls-narrow.h>
748# undef _Mret_
749# undef _Marg_
750# undef __MATHCALL_NAME
751# endif
752
753# if __HAVE_FLOAT32X && __HAVE_FLOAT64X
754# define _Mret_ _Float32x
755# define _Marg_ _Float64x
756# define __MATHCALL_NAME(name) f32x ## name ## f64x
757# include <bits/mathcalls-narrow.h>
758# undef _Mret_
759# undef _Marg_
760# undef __MATHCALL_NAME
761# endif
762
763# if __HAVE_FLOAT32X && __HAVE_FLOAT128
764# define _Mret_ _Float32x
765# define _Marg_ _Float128
766# define __MATHCALL_NAME(name) f32x ## name ## f128
767# include <bits/mathcalls-narrow.h>
768# undef _Mret_
769# undef _Marg_
770# undef __MATHCALL_NAME
771# endif
772
773# if __HAVE_FLOAT32X && __HAVE_FLOAT128X
774# define _Mret_ _Float32x
775# define _Marg_ _Float128x
776# define __MATHCALL_NAME(name) f32x ## name ## f128x
777# include <bits/mathcalls-narrow.h>
778# undef _Mret_
779# undef _Marg_
780# undef __MATHCALL_NAME
781# endif
782
783# if __HAVE_FLOAT64 && __HAVE_FLOAT64X
784# define _Mret_ _Float64
785# define _Marg_ _Float64x
786# define __MATHCALL_NAME(name) f64 ## name ## f64x
787# include <bits/mathcalls-narrow.h>
788# undef _Mret_
789# undef _Marg_
790# undef __MATHCALL_NAME
791# endif
792
793# if __HAVE_FLOAT64 && __HAVE_FLOAT128
794# define _Mret_ _Float64
795# define _Marg_ _Float128
796# define __MATHCALL_NAME(name) f64 ## name ## f128
797# include <bits/mathcalls-narrow.h>
798# undef _Mret_
799# undef _Marg_
800# undef __MATHCALL_NAME
801# endif
802
803# if __HAVE_FLOAT64 && __HAVE_FLOAT128X
804# define _Mret_ _Float64
805# define _Marg_ _Float128x
806# define __MATHCALL_NAME(name) f64 ## name ## f128x
807# include <bits/mathcalls-narrow.h>
808# undef _Mret_
809# undef _Marg_
810# undef __MATHCALL_NAME
811# endif
812
813# if __HAVE_FLOAT64X && __HAVE_FLOAT128
814# define _Mret_ _Float64x
815# define _Marg_ _Float128
816# define __MATHCALL_NAME(name) f64x ## name ## f128
817# include <bits/mathcalls-narrow.h>
818# undef _Mret_
819# undef _Marg_
820# undef __MATHCALL_NAME
821# endif
822
823# if __HAVE_FLOAT64X && __HAVE_FLOAT128X
824# define _Mret_ _Float64x
825# define _Marg_ _Float128x
826# define __MATHCALL_NAME(name) f64x ## name ## f128x
827# include <bits/mathcalls-narrow.h>
828# undef _Mret_
829# undef _Marg_
830# undef __MATHCALL_NAME
831# endif
832
833# if __HAVE_FLOAT128 && __HAVE_FLOAT128X
834# define _Mret_ _Float128
835# define _Marg_ _Float128x
836# define __MATHCALL_NAME(name) f128 ## name ## f128x
837# include <bits/mathcalls-narrow.h>
838# undef _Mret_
839# undef _Marg_
840# undef __MATHCALL_NAME
841# endif
842
843#endif
844
845#undef __MATHCALL_NARROW_ARGS_1
846#undef __MATHCALL_NARROW_ARGS_2
847#undef __MATHCALL_NARROW_ARGS_3
848#undef __MATHCALL_NARROW_NORMAL
849#undef __MATHCALL_NARROW_REDIR
850#undef __MATHCALL_NARROW
4cca6b86 851
00d8bc81 852#if defined __USE_MISC || defined __USE_XOPEN
4cca6b86
UD
853/* This variable is used by `gamma' and `lgamma'. */
854extern int signgam;
855#endif
377a515b 856
4ca70e1a
JM
857#if (__HAVE_DISTINCT_FLOAT16 \
858 || __HAVE_DISTINCT_FLOAT32 \
859 || __HAVE_DISTINCT_FLOAT64 \
860 || __HAVE_DISTINCT_FLOAT32X \
861 || __HAVE_DISTINCT_FLOAT64X \
862 || __HAVE_DISTINCT_FLOAT128X)
863# error "Unsupported _FloatN or _FloatNx types for <math.h>."
864#endif
377a515b 865
b4e75104
JM
866/* Depending on the type of TG_ARG, call an appropriately suffixed
867 version of FUNC with arguments (including parentheses) ARGS.
868 Suffixed functions may not exist for long double if it has the same
869 format as double, or for other types with the same format as float,
870 double or long double. The behavior is undefined if the argument
871 does not have a real floating type. The definition may use a
872 conditional expression, so all suffixed versions of FUNC must
873 return the same type (FUNC may include a cast if necessary rather
874 than being a single identifier). */
875#ifdef __NO_LONG_DOUBLE_MATH
4ca70e1a
JM
876# if __HAVE_DISTINCT_FLOAT128
877# error "Distinct _Float128 without distinct long double not supported."
878# endif
b4e75104
JM
879# define __MATH_TG(TG_ARG, FUNC, ARGS) \
880 (sizeof (TG_ARG) == sizeof (float) ? FUNC ## f ARGS : FUNC ARGS)
477bf19a
GG
881#elif __HAVE_DISTINCT_FLOAT128
882# if __HAVE_GENERIC_SELECTION
4ca70e1a
JM
883# if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT32
884# define __MATH_TG_F32(FUNC, ARGS) _Float32: FUNC ## f ARGS,
885# else
886# define __MATH_TG_F32(FUNC, ARGS)
887# endif
888# if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT64X
889# if __HAVE_FLOAT64X_LONG_DOUBLE
890# define __MATH_TG_F64X(FUNC, ARGS) _Float64x: FUNC ## l ARGS,
891# else
892# define __MATH_TG_F64X(FUNC, ARGS) _Float64x: FUNC ## f128 ARGS,
893# endif
894# else
895# define __MATH_TG_F64X(FUNC, ARGS)
896# endif
897# define __MATH_TG(TG_ARG, FUNC, ARGS) \
477bf19a
GG
898 _Generic ((TG_ARG), \
899 float: FUNC ## f ARGS, \
4ca70e1a 900 __MATH_TG_F32 (FUNC, ARGS) \
477bf19a
GG
901 default: FUNC ARGS, \
902 long double: FUNC ## l ARGS, \
4ca70e1a 903 __MATH_TG_F64X (FUNC, ARGS) \
477bf19a
GG
904 _Float128: FUNC ## f128 ARGS)
905# else
4ca70e1a
JM
906# if __HAVE_FLOATN_NOT_TYPEDEF
907# error "Non-typedef _FloatN but no _Generic."
908# endif
477bf19a
GG
909# define __MATH_TG(TG_ARG, FUNC, ARGS) \
910 __builtin_choose_expr \
911 (__builtin_types_compatible_p (__typeof (TG_ARG), float), \
912 FUNC ## f ARGS, \
913 __builtin_choose_expr \
914 (__builtin_types_compatible_p (__typeof (TG_ARG), double), \
915 FUNC ARGS, \
916 __builtin_choose_expr \
917 (__builtin_types_compatible_p (__typeof (TG_ARG), long double), \
918 FUNC ## l ARGS, \
919 FUNC ## f128 ARGS)))
920# endif
b4e75104
JM
921#else
922# define __MATH_TG(TG_ARG, FUNC, ARGS) \
923 (sizeof (TG_ARG) == sizeof (float) \
924 ? FUNC ## f ARGS \
925 : sizeof (TG_ARG) == sizeof (double) \
926 ? FUNC ARGS \
927 : FUNC ## l ARGS)
928#endif
929
ec751a23 930/* ISO C99 defines some generic macros which work on any data type. */
638bed90 931#ifdef __USE_ISOC99
377a515b
UD
932
933/* All floating-point numbers can be put in one of these categories. */
934enum
935 {
0155d5b2
JM
936 FP_NAN =
937# define FP_NAN 0
938 FP_NAN,
939 FP_INFINITE =
940# define FP_INFINITE 1
941 FP_INFINITE,
942 FP_ZERO =
943# define FP_ZERO 2
944 FP_ZERO,
945 FP_SUBNORMAL =
946# define FP_SUBNORMAL 3
947 FP_SUBNORMAL,
948 FP_NORMAL =
949# define FP_NORMAL 4
950 FP_NORMAL
377a515b
UD
951 };
952
8df4e219
WD
953/* GCC bug 66462 means we cannot use the math builtins with -fsignaling-nan,
954 so disable builtins if this is enabled. When fixed in a newer GCC,
955 the __SUPPORT_SNAN__ check may be skipped for those versions. */
956
377a515b 957/* Return number of classification appropriate for X. */
4997e8f3
AZ
958# if ((__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
959 || __glibc_clang_prereq (2,8)) \
c5c4a626
GG
960 && (!defined __OPTIMIZE_SIZE__ || defined __cplusplus)
961 /* The check for __cplusplus allows the use of the builtin, even
962 when optimization for size is on. This is provided for
963 libstdc++, only to let its configure test work when it is built
964 with -Os. No further use of this definition of fpclassify is
965 expected in C++ mode, since libstdc++ provides its own version
966 of fpclassify in cmath (which undefines fpclassify). */
8df4e219
WD
967# define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
968 FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
68337808 969# else
b4e75104 970# define fpclassify(x) __MATH_TG ((x), __fpclassify, (x))
68337808 971# endif
377a515b
UD
972
973/* Return nonzero value if sign of X is negative. */
4997e8f3 974# if __GNUC_PREREQ (6,0) || __glibc_clang_prereq (3,3)
fcee5905 975# define signbit(x) __builtin_signbit (x)
386e1c26
RN
976# elif defined __cplusplus
977 /* In C++ mode, __MATH_TG cannot be used, because it relies on
978 __builtin_types_compatible_p, which is a C-only builtin.
979 The check for __cplusplus allows the use of the builtin instead of
980 __MATH_TG. This is provided for libstdc++, only to let its configure
981 test work. No further use of this definition of signbit is expected
982 in C++ mode, since libstdc++ provides its own version of signbit
983 in cmath (which undefines signbit). */
984# define signbit(x) __builtin_signbitl (x)
fcee5905 985# elif __GNUC_PREREQ (4,0)
b4e75104 986# define signbit(x) __MATH_TG ((x), __builtin_signbit, (x))
68337808 987# else
b4e75104 988# define signbit(x) __MATH_TG ((x), __signbit, (x))
68337808 989# endif
377a515b
UD
990
991/* Return nonzero value if X is not +-Inf or NaN. */
4997e8f3
AZ
992# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
993 || __glibc_clang_prereq (2,8)
8df4e219 994# define isfinite(x) __builtin_isfinite (x)
68337808 995# else
b4e75104 996# define isfinite(x) __MATH_TG ((x), __finite, (x))
68337808 997# endif
377a515b
UD
998
999/* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
4997e8f3
AZ
1000# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
1001 || __glibc_clang_prereq (2,8)
8df4e219
WD
1002# define isnormal(x) __builtin_isnormal (x)
1003# else
1004# define isnormal(x) (fpclassify (x) == FP_NORMAL)
1005# endif
377a515b
UD
1006
1007/* Return nonzero value if X is a NaN. We could use `fpclassify' but
1008 we already have this functions `__isnan' and it is faster. */
4997e8f3
AZ
1009# if (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
1010 || __glibc_clang_prereq (2,8)
8df4e219 1011# define isnan(x) __builtin_isnan (x)
68337808 1012# else
b4e75104 1013# define isnan(x) __MATH_TG ((x), __isnan, (x))
68337808 1014# endif
28f540f4 1015
0e012e87 1016/* Return nonzero value if X is positive or negative infinity. */
fcee5905 1017# if __HAVE_DISTINCT_FLOAT128 && !__GNUC_PREREQ (7,0) \
47a67213
GG
1018 && !defined __SUPPORT_SNAN__ && !defined __cplusplus
1019 /* Since __builtin_isinf_sign is broken for float128 before GCC 7.0,
1020 use the helper function, __isinff128, with older compilers. This is
1021 only provided for C mode, because in C++ mode, GCC has no support
1022 for __builtin_types_compatible_p (and when in C++ mode, this macro is
1023 not used anyway, because libstdc++ headers undefine it). */
fcee5905
PM
1024# define isinf(x) \
1025 (__builtin_types_compatible_p (__typeof (x), _Float128) \
1026 ? __isinff128 (x) : __builtin_isinf_sign (x))
4997e8f3
AZ
1027# elif (__GNUC_PREREQ (4,4) && !defined __SUPPORT_SNAN__) \
1028 || __glibc_clang_prereq (3,7)
8df4e219 1029# define isinf(x) __builtin_isinf_sign (x)
68337808 1030# else
b4e75104 1031# define isinf(x) __MATH_TG ((x), __isinf, (x))
68337808 1032# endif
bfce746a 1033
12b64309
UD
1034/* Bitmasks for the math_errhandling macro. */
1035# define MATH_ERRNO 1 /* errno set by math functions. */
1036# define MATH_ERREXCEPT 2 /* Exceptions raised by math functions. */
1037
3c7d0312
WD
1038/* By default all math functions support both errno and exception handling
1039 (except for soft floating point implementations which may only support
1040 errno handling). If errno handling is disabled, exceptions are still
1041 supported by GLIBC. Set math_errhandling to 0 with -ffast-math (this is
1042 nonconforming but it is more useful than leaving it undefined). */
1043# ifdef __FAST_MATH__
1044# define math_errhandling 0
1045# elif defined __NO_MATH_ERRNO__
1046# define math_errhandling (MATH_ERREXCEPT)
1047# else
7b943af6
UD
1048# define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
1049# endif
1050
ec751a23 1051#endif /* Use ISO C99. */
377a515b 1052
0175c9e9 1053#if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
29cb9293
JM
1054# include <bits/iscanonical.h>
1055
57267616 1056/* Return nonzero value if X is a signaling NaN. */
a16e8bc0
GG
1057# ifndef __cplusplus
1058# define issignaling(x) __MATH_TG ((x), __issignaling, (x))
1059# else
1060 /* In C++ mode, __MATH_TG cannot be used, because it relies on
1061 __builtin_types_compatible_p, which is a C-only builtin. On the
1062 other hand, overloading provides the means to distinguish between
1063 the floating-point types. The overloading resolution will match
1064 the correct parameter (regardless of type qualifiers (i.e.: const
aa0235df 1065 and volatile)). */
a16e8bc0
GG
1066extern "C++" {
1067inline int issignaling (float __val) { return __issignalingf (__val); }
1068inline int issignaling (double __val) { return __issignaling (__val); }
3d7b66f6
GG
1069inline int
1070issignaling (long double __val)
1071{
1072# ifdef __NO_LONG_DOUBLE_MATH
1073 return __issignaling (__val);
1074# else
1075 return __issignalingl (__val);
1076# endif
1077}
5db7d705
TMQMF
1078# if __HAVE_FLOAT128_UNLIKE_LDBL
1079/* When using an IEEE 128-bit long double, _Float128 is defined as long double
1080 in C++. */
a16e8bc0
GG
1081inline int issignaling (_Float128 __val) { return __issignalingf128 (__val); }
1082# endif
1083} /* extern C++ */
1084# endif
d942e95c
JM
1085
1086/* Return nonzero value if X is subnormal. */
1087# define issubnormal(x) (fpclassify (x) == FP_SUBNORMAL)
bb8081f5
JM
1088
1089/* Return nonzero value if X is zero. */
b3918c44
FW
1090# ifndef __cplusplus
1091# ifdef __SUPPORT_SNAN__
1092# define iszero(x) (fpclassify (x) == FP_ZERO)
1093# else
1094# define iszero(x) (((__typeof (x)) (x)) == 0)
1095# endif
1096# else /* __cplusplus */
e37208ce 1097extern "C++" {
42496114
GG
1098# ifdef __SUPPORT_SNAN__
1099inline int
1100iszero (float __val)
1101{
1102 return __fpclassifyf (__val) == FP_ZERO;
1103}
1104inline int
1105iszero (double __val)
1106{
1107 return __fpclassify (__val) == FP_ZERO;
1108}
1109inline int
1110iszero (long double __val)
1111{
1112# ifdef __NO_LONG_DOUBLE_MATH
1113 return __fpclassify (__val) == FP_ZERO;
1114# else
1115 return __fpclassifyl (__val) == FP_ZERO;
1116# endif
1117}
5db7d705
TMQMF
1118# if __HAVE_FLOAT128_UNLIKE_LDBL
1119 /* When using an IEEE 128-bit long double, _Float128 is defined as long double
1120 in C++. */
42496114
GG
1121inline int
1122iszero (_Float128 __val)
1123{
1124 return __fpclassifyf128 (__val) == FP_ZERO;
1125}
1126# endif
1127# else
b3918c44
FW
1128template <class __T> inline bool
1129iszero (__T __val)
1130{
b3918c44 1131 return __val == 0;
b3918c44 1132}
42496114 1133# endif
e37208ce 1134} /* extern C++ */
b3918c44 1135# endif /* __cplusplus */
bf91be88 1136#endif /* Use IEC_60559_BFP_EXT. */
57267616 1137
813378e9 1138#ifdef __USE_XOPEN
377a515b 1139/* X/Open wants another strange constant. */
813378e9
JM
1140# define MAXFLOAT 3.40282347e+38F
1141#endif
28f540f4 1142
28f540f4 1143
28f540f4 1144/* Some useful constants. */
498afc54 1145#if defined __USE_MISC || defined __USE_XOPEN
0a614877
UD
1146# define M_E 2.7182818284590452354 /* e */
1147# define M_LOG2E 1.4426950408889634074 /* log_2 e */
1148# define M_LOG10E 0.43429448190325182765 /* log_10 e */
1149# define M_LN2 0.69314718055994530942 /* log_e 2 */
1150# define M_LN10 2.30258509299404568402 /* log_e 10 */
1151# define M_PI 3.14159265358979323846 /* pi */
1152# define M_PI_2 1.57079632679489661923 /* pi/2 */
1153# define M_PI_4 0.78539816339744830962 /* pi/4 */
1154# define M_1_PI 0.31830988618379067154 /* 1/pi */
1155# define M_2_PI 0.63661977236758134308 /* 2/pi */
1156# define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
1157# define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
1158# define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
714a562f
UD
1159#endif
1160
0a614877
UD
1161/* The above constants are not adequate for computation using `long double's.
1162 Therefore we provide as an extension constants with similar names as a
9a24f906 1163 GNU extension. Provide enough digits for the 128-bit IEEE quad. */
0a614877 1164#ifdef __USE_GNU
2d10d547
JM
1165# define M_El 2.718281828459045235360287471352662498L /* e */
1166# define M_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
1167# define M_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
1168# define M_LN2l 0.693147180559945309417232121458176568L /* log_e 2 */
1169# define M_LN10l 2.302585092994045684017991454684364208L /* log_e 10 */
1170# define M_PIl 3.141592653589793238462643383279502884L /* pi */
1171# define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
1172# define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
1173# define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
1174# define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
1175# define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
1176# define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
1177# define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
0a614877 1178#endif
710f7bab 1179
ce12269f
JM
1180#if __HAVE_FLOAT16 && defined __USE_GNU
1181# define M_Ef16 __f16 (2.718281828459045235360287471352662498) /* e */
1182# define M_LOG2Ef16 __f16 (1.442695040888963407359924681001892137) /* log_2 e */
1183# define M_LOG10Ef16 __f16 (0.434294481903251827651128918916605082) /* log_10 e */
1184# define M_LN2f16 __f16 (0.693147180559945309417232121458176568) /* log_e 2 */
1185# define M_LN10f16 __f16 (2.302585092994045684017991454684364208) /* log_e 10 */
1186# define M_PIf16 __f16 (3.141592653589793238462643383279502884) /* pi */
1187# define M_PI_2f16 __f16 (1.570796326794896619231321691639751442) /* pi/2 */
1188# define M_PI_4f16 __f16 (0.785398163397448309615660845819875721) /* pi/4 */
1189# define M_1_PIf16 __f16 (0.318309886183790671537767526745028724) /* 1/pi */
1190# define M_2_PIf16 __f16 (0.636619772367581343075535053490057448) /* 2/pi */
1191# define M_2_SQRTPIf16 __f16 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1192# define M_SQRT2f16 __f16 (1.414213562373095048801688724209698079) /* sqrt(2) */
1193# define M_SQRT1_2f16 __f16 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1194#endif
1195
1196#if __HAVE_FLOAT32 && defined __USE_GNU
1197# define M_Ef32 __f32 (2.718281828459045235360287471352662498) /* e */
1198# define M_LOG2Ef32 __f32 (1.442695040888963407359924681001892137) /* log_2 e */
1199# define M_LOG10Ef32 __f32 (0.434294481903251827651128918916605082) /* log_10 e */
1200# define M_LN2f32 __f32 (0.693147180559945309417232121458176568) /* log_e 2 */
1201# define M_LN10f32 __f32 (2.302585092994045684017991454684364208) /* log_e 10 */
1202# define M_PIf32 __f32 (3.141592653589793238462643383279502884) /* pi */
1203# define M_PI_2f32 __f32 (1.570796326794896619231321691639751442) /* pi/2 */
1204# define M_PI_4f32 __f32 (0.785398163397448309615660845819875721) /* pi/4 */
1205# define M_1_PIf32 __f32 (0.318309886183790671537767526745028724) /* 1/pi */
1206# define M_2_PIf32 __f32 (0.636619772367581343075535053490057448) /* 2/pi */
1207# define M_2_SQRTPIf32 __f32 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1208# define M_SQRT2f32 __f32 (1.414213562373095048801688724209698079) /* sqrt(2) */
1209# define M_SQRT1_2f32 __f32 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1210#endif
1211
1212#if __HAVE_FLOAT64 && defined __USE_GNU
1213# define M_Ef64 __f64 (2.718281828459045235360287471352662498) /* e */
1214# define M_LOG2Ef64 __f64 (1.442695040888963407359924681001892137) /* log_2 e */
1215# define M_LOG10Ef64 __f64 (0.434294481903251827651128918916605082) /* log_10 e */
1216# define M_LN2f64 __f64 (0.693147180559945309417232121458176568) /* log_e 2 */
1217# define M_LN10f64 __f64 (2.302585092994045684017991454684364208) /* log_e 10 */
1218# define M_PIf64 __f64 (3.141592653589793238462643383279502884) /* pi */
1219# define M_PI_2f64 __f64 (1.570796326794896619231321691639751442) /* pi/2 */
1220# define M_PI_4f64 __f64 (0.785398163397448309615660845819875721) /* pi/4 */
1221# define M_1_PIf64 __f64 (0.318309886183790671537767526745028724) /* 1/pi */
1222# define M_2_PIf64 __f64 (0.636619772367581343075535053490057448) /* 2/pi */
1223# define M_2_SQRTPIf64 __f64 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1224# define M_SQRT2f64 __f64 (1.414213562373095048801688724209698079) /* sqrt(2) */
1225# define M_SQRT1_2f64 __f64 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1226#endif
1227
fcee5905
PM
1228#if __HAVE_FLOAT128 && defined __USE_GNU
1229# define M_Ef128 __f128 (2.718281828459045235360287471352662498) /* e */
1230# define M_LOG2Ef128 __f128 (1.442695040888963407359924681001892137) /* log_2 e */
1231# define M_LOG10Ef128 __f128 (0.434294481903251827651128918916605082) /* log_10 e */
1232# define M_LN2f128 __f128 (0.693147180559945309417232121458176568) /* log_e 2 */
1233# define M_LN10f128 __f128 (2.302585092994045684017991454684364208) /* log_e 10 */
1234# define M_PIf128 __f128 (3.141592653589793238462643383279502884) /* pi */
1235# define M_PI_2f128 __f128 (1.570796326794896619231321691639751442) /* pi/2 */
1236# define M_PI_4f128 __f128 (0.785398163397448309615660845819875721) /* pi/4 */
1237# define M_1_PIf128 __f128 (0.318309886183790671537767526745028724) /* 1/pi */
1238# define M_2_PIf128 __f128 (0.636619772367581343075535053490057448) /* 2/pi */
1239# define M_2_SQRTPIf128 __f128 (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1240# define M_SQRT2f128 __f128 (1.414213562373095048801688724209698079) /* sqrt(2) */
1241# define M_SQRT1_2f128 __f128 (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1242#endif
28f540f4 1243
ce12269f
JM
1244#if __HAVE_FLOAT32X && defined __USE_GNU
1245# define M_Ef32x __f32x (2.718281828459045235360287471352662498) /* e */
1246# define M_LOG2Ef32x __f32x (1.442695040888963407359924681001892137) /* log_2 e */
1247# define M_LOG10Ef32x __f32x (0.434294481903251827651128918916605082) /* log_10 e */
1248# define M_LN2f32x __f32x (0.693147180559945309417232121458176568) /* log_e 2 */
1249# define M_LN10f32x __f32x (2.302585092994045684017991454684364208) /* log_e 10 */
1250# define M_PIf32x __f32x (3.141592653589793238462643383279502884) /* pi */
1251# define M_PI_2f32x __f32x (1.570796326794896619231321691639751442) /* pi/2 */
1252# define M_PI_4f32x __f32x (0.785398163397448309615660845819875721) /* pi/4 */
1253# define M_1_PIf32x __f32x (0.318309886183790671537767526745028724) /* 1/pi */
1254# define M_2_PIf32x __f32x (0.636619772367581343075535053490057448) /* 2/pi */
1255# define M_2_SQRTPIf32x __f32x (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1256# define M_SQRT2f32x __f32x (1.414213562373095048801688724209698079) /* sqrt(2) */
1257# define M_SQRT1_2f32x __f32x (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1258#endif
1259
1260#if __HAVE_FLOAT64X && defined __USE_GNU
1261# define M_Ef64x __f64x (2.718281828459045235360287471352662498) /* e */
1262# define M_LOG2Ef64x __f64x (1.442695040888963407359924681001892137) /* log_2 e */
1263# define M_LOG10Ef64x __f64x (0.434294481903251827651128918916605082) /* log_10 e */
1264# define M_LN2f64x __f64x (0.693147180559945309417232121458176568) /* log_e 2 */
1265# define M_LN10f64x __f64x (2.302585092994045684017991454684364208) /* log_e 10 */
1266# define M_PIf64x __f64x (3.141592653589793238462643383279502884) /* pi */
1267# define M_PI_2f64x __f64x (1.570796326794896619231321691639751442) /* pi/2 */
1268# define M_PI_4f64x __f64x (0.785398163397448309615660845819875721) /* pi/4 */
1269# define M_1_PIf64x __f64x (0.318309886183790671537767526745028724) /* 1/pi */
1270# define M_2_PIf64x __f64x (0.636619772367581343075535053490057448) /* 2/pi */
1271# define M_2_SQRTPIf64x __f64x (1.128379167095512573896158903121545172) /* 2/sqrt(pi) */
1272# define M_SQRT2f64x __f64x (1.414213562373095048801688724209698079) /* sqrt(2) */
1273# define M_SQRT1_2f64x __f64x (0.707106781186547524400844362104849039) /* 1/sqrt(2) */
1274#endif
1275
1276#if __HAVE_FLOAT128X && defined __USE_GNU
1277# error "M_* values needed for _Float128x"
1278#endif
1279
1e6d0723
WD
1280#ifdef __USE_ISOC99
1281# if __GNUC_PREREQ (3, 1)
f4c024d1
UD
1282/* ISO C99 defines some macros to compare number while taking care for
1283 unordered numbers. Many FPUs provide special instructions to support
1284 these operations. Generic support in GCC for these as builtins went
1e6d0723
WD
1285 in 2.97, but not all cpus added their patterns until 3.1. Therefore
1286 we enable the builtins from 3.1 onwards and use a generic implementation
1287 othwerwise. */
1288# define isgreater(x, y) __builtin_isgreater(x, y)
1289# define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
1290# define isless(x, y) __builtin_isless(x, y)
1291# define islessequal(x, y) __builtin_islessequal(x, y)
1292# define islessgreater(x, y) __builtin_islessgreater(x, y)
1293# define isunordered(x, y) __builtin_isunordered(x, y)
1294# else
1295# define isgreater(x, y) \
1296 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
1297 !isunordered (__x, __y) && __x > __y; }))
1298# define isgreaterequal(x, y) \
1299 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
1300 !isunordered (__x, __y) && __x >= __y; }))
1301# define isless(x, y) \
1302 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
1303 !isunordered (__x, __y) && __x < __y; }))
1304# define islessequal(x, y) \
1305 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
1306 !isunordered (__x, __y) && __x <= __y; }))
1307# define islessgreater(x, y) \
1308 (__extension__ ({ __typeof__ (x) __x = (x); __typeof__ (y) __y = (y); \
1309 !isunordered (__x, __y) && __x != __y; }))
1310/* isunordered must always check both operands first for signaling NaNs. */
1311# define isunordered(x, y) \
1312 (__extension__ ({ __typeof__ (x) __u = (x); __typeof__ (y) __v = (y); \
1313 __u != __v && (__u != __u || __v != __v); }))
1314# endif
f4c024d1
UD
1315#endif
1316
0175c9e9 1317#if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
b4e75104
JM
1318/* An expression whose type has the widest of the evaluation formats
1319 of X and Y (which are of floating-point types). */
1320# if __FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ > 64
1321# define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0L)
1322# elif __FLT_EVAL_METHOD__ == 1 || __FLT_EVAL_METHOD__ > 32
1323# define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0)
540af6e2
JM
1324# elif __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 32
1325# define __MATH_EVAL_FMT2(x, y) ((x) + (y) + 0.0f)
1e7c8fcc 1326# else
b4e75104 1327# define __MATH_EVAL_FMT2(x, y) ((x) + (y))
1e7c8fcc 1328# endif
b4e75104
JM
1329
1330/* Return X == Y but raising "invalid" and setting errno if X or Y is
1331 a NaN. */
c85e54ac
GG
1332# if !defined __cplusplus || (__cplusplus < 201103L && !defined __GNUC__)
1333# define iseqsig(x, y) \
1334 __MATH_TG (__MATH_EVAL_FMT2 (x, y), __iseqsig, ((x), (y)))
1335# else
1336/* In C++ mode, __MATH_TG cannot be used, because it relies on
1337 __builtin_types_compatible_p, which is a C-only builtin. Moreover,
1338 the comparison macros from ISO C take two floating-point arguments,
1339 which need not have the same type. Choosing what underlying function
1340 to call requires evaluating the formats of the arguments, then
1341 selecting which is wider. The macro __MATH_EVAL_FMT2 provides this
1342 information, however, only the type of the macro expansion is
1343 relevant (actually evaluating the expression would be incorrect).
1344 Thus, the type is used as a template parameter for __iseqsig_type,
1345 which calls the appropriate underlying function. */
1346extern "C++" {
1347template<typename> struct __iseqsig_type;
1348
1349template<> struct __iseqsig_type<float>
1350{
1351 static int __call (float __x, float __y) throw ()
1352 {
1353 return __iseqsigf (__x, __y);
1354 }
1355};
1356
1357template<> struct __iseqsig_type<double>
1358{
1359 static int __call (double __x, double __y) throw ()
1360 {
1361 return __iseqsig (__x, __y);
1362 }
1363};
1364
1365template<> struct __iseqsig_type<long double>
1366{
fb0e10b8 1367 static int __call (long double __x, long double __y) throw ()
c85e54ac
GG
1368 {
1369# ifndef __NO_LONG_DOUBLE_MATH
1370 return __iseqsigl (__x, __y);
1371# else
1372 return __iseqsig (__x, __y);
1373# endif
1374 }
1375};
1376
5db7d705
TMQMF
1377# if __HAVE_FLOAT128_UNLIKE_LDBL
1378 /* When using an IEEE 128-bit long double, _Float128 is defined as long double
1379 in C++. */
c85e54ac
GG
1380template<> struct __iseqsig_type<_Float128>
1381{
1382 static int __call (_Float128 __x, _Float128 __y) throw ()
1383 {
1384 return __iseqsigf128 (__x, __y);
1385 }
1386};
1387# endif
1388
1389template<typename _T1, typename _T2>
1390inline int
1391iseqsig (_T1 __x, _T2 __y) throw ()
1392{
1393# if __cplusplus >= 201103L
1394 typedef decltype (__MATH_EVAL_FMT2 (__x, __y)) _T3;
1395# else
1396 typedef __typeof (__MATH_EVAL_FMT2 (__x, __y)) _T3;
1397# endif
1398 return __iseqsig_type<_T3>::__call (__x, __y);
1399}
1400
1401} /* extern "C++" */
1402# endif /* __cplusplus */
1403
1e7c8fcc
JM
1404#endif
1405
4d585333
RM
1406__END_DECLS
1407
1408
28f540f4 1409#endif /* math.h */