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