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