1 /* A GNU-like <math.h>.
3 Copyright (C) 2002-2003, 2007-2010 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 @PRAGMA_SYSTEM_HEADER@
24 /* The include_next requires a split double-inclusion guard. */
25 #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_MATH_H@
31 /* The definition of GL_LINK_WARNING is copied here. */
33 /* The definition of _GL_ARG_NONNULL is copied here. */
35 /* Helper macros to define a portability warning for the
36 classification macro FUNC called with VALUE. POSIX declares the
37 classification macros with an argument of real-floating (that is,
38 one of float, double, or long double). */
39 #define _GL_WARN_REAL_FLOATING_DECL(func) \
41 rpl_ ## func ## f (float f) \
46 rpl_ ## func ## d (double d) \
51 rpl_ ## func ## l (long double l) \
55 _GL_WARN_ON_USE (rpl_ ## func ## f, #func " is unportable - " \
56 "use gnulib module " #func " for portability"); \
57 _GL_WARN_ON_USE (rpl_ ## func ## d, #func " is unportable - " \
58 "use gnulib module " #func " for portability"); \
59 _GL_WARN_ON_USE (rpl_ ## func ## l, #func " is unportable - " \
60 "use gnulib module " #func " for portability")
61 #define _GL_WARN_REAL_FLOATING_IMPL(func, value) \
62 (sizeof (value) == sizeof (float) ? rpl_ ## func ## f (value) \
63 : sizeof (value) == sizeof (double) ? rpl_ ## func ## d (value) \
64 : rpl_ ## func ## l (value))
72 /* POSIX allows platforms that don't support NAN. But all major
73 machines in the past 15 years have supported something close to
74 IEEE NaN, so we define this unconditionally. We also must define
75 it on platforms like Solaris 10, where NAN is present but defined
76 as a function pointer rather than a floating point constant. */
77 #if !defined NAN || @REPLACE_NAN@
79 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
84 static float zero = 0.0f;
89 # define NAN (0.0f / 0.0f)
93 /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
94 than a floating point constant. */
95 #if @REPLACE_HUGE_VAL@
97 # define HUGE_VAL (1.0 / 0.0)
103 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
104 If x is zero: mantissa = x, exp = 0.
105 If x is infinite or NaN: mantissa = x, exp unspecified.
106 Store exp in *EXPPTR and return mantissa. */
109 # define frexp rpl_frexp
110 extern double frexp (double x, int *expptr) _GL_ARG_NONNULL ((2));
112 #elif defined GNULIB_POSIXCHECK
114 # define frexp(x,e) \
115 (GL_LINK_WARNING ("frexp is unportable - " \
116 "use gnulib module frexp for portability"), \
122 # if !@HAVE_ACOSL@ || !@HAVE_DECL_ACOSL@
123 extern long double acosl (long double x);
125 #elif defined GNULIB_POSIXCHECK
128 (GL_LINK_WARNING ("acosl is unportable - " \
129 "use gnulib module mathl for portability"), \
135 # if !@HAVE_ASINL@ || !@HAVE_DECL_ASINL@
136 extern long double asinl (long double x);
138 #elif defined GNULIB_POSIXCHECK
141 (GL_LINK_WARNING ("asinl is unportable - " \
142 "use gnulib module mathl for portability"), \
148 # if !@HAVE_ATANL@ || !@HAVE_DECL_ATANL@
149 extern long double atanl (long double x);
151 #elif defined GNULIB_POSIXCHECK
154 (GL_LINK_WARNING ("atanl is unportable - " \
155 "use gnulib module mathl for portability"), \
162 # define ceilf rpl_ceilf
163 extern float ceilf (float x);
165 #elif defined GNULIB_POSIXCHECK
168 (GL_LINK_WARNING ("ceilf is unportable - " \
169 "use gnulib module ceilf for portability"), \
175 # define ceill rpl_ceill
176 extern long double ceill (long double x);
178 #elif defined GNULIB_POSIXCHECK
181 (GL_LINK_WARNING ("ceill is unportable - " \
182 "use gnulib module ceill for portability"), \
190 # define cosl rpl_cosl
192 # if !@HAVE_COSL@ || !@HAVE_DECL_COSL@
193 extern long double cosl (long double x);
195 #elif defined GNULIB_POSIXCHECK
198 (GL_LINK_WARNING ("cosl is unportable - " \
199 "use gnulib module mathl for portability"), \
205 # if !@HAVE_EXPL@ || !@HAVE_DECL_EXPL@
206 extern long double expl (long double x);
208 #elif defined GNULIB_POSIXCHECK
211 (GL_LINK_WARNING ("expl is unportable - " \
212 "use gnulib module mathl for portability"), \
218 # if @REPLACE_FLOORF@
219 # define floorf rpl_floorf
220 extern float floorf (float x);
222 #elif defined GNULIB_POSIXCHECK
225 (GL_LINK_WARNING ("floorf is unportable - " \
226 "use gnulib module floorf for portability"), \
231 # if @REPLACE_FLOORL@
232 # define floorl rpl_floorl
233 extern long double floorl (long double x);
235 #elif defined GNULIB_POSIXCHECK
238 (GL_LINK_WARNING ("floorl is unportable - " \
239 "use gnulib module floorl for portability"), \
247 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
248 If x is zero: mantissa = x, exp = 0.
249 If x is infinite or NaN: mantissa = x, exp unspecified.
250 Store exp in *EXPPTR and return mantissa. */
251 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
252 # define frexpl rpl_frexpl
254 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
255 extern long double frexpl (long double x, int *expptr) _GL_ARG_NONNULL ((2));
257 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
259 # define frexpl(x,e) \
260 (GL_LINK_WARNING ("frexpl is unportable - " \
261 "use gnulib module frexpl for portability"), \
266 /* Return x * 2^exp. */
267 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
268 # define ldexpl rpl_ldexpl
270 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
271 extern long double ldexpl (long double x, int exp);
273 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
275 # define ldexpl(x,e) \
276 (GL_LINK_WARNING ("ldexpl is unportable - " \
277 "use gnulib module ldexpl for portability"), \
285 # define logl rpl_logl
287 # if !@HAVE_LOGL@ || !@HAVE_DECL_LOGL@
288 extern long double logl (long double x);
290 #elif defined GNULIB_POSIXCHECK
293 (GL_LINK_WARNING ("logl is unportable - " \
294 "use gnulib module mathl for portability"), \
300 # if @REPLACE_ROUNDF@
302 # define roundf rpl_roundf
303 extern float roundf (float x);
305 #elif defined GNULIB_POSIXCHECK
308 (GL_LINK_WARNING ("roundf is unportable - " \
309 "use gnulib module roundf for portability"), \
316 # define round rpl_round
317 extern double round (double x);
319 #elif defined GNULIB_POSIXCHECK
322 (GL_LINK_WARNING ("round is unportable - " \
323 "use gnulib module round for portability"), \
328 # if @REPLACE_ROUNDL@
330 # define roundl rpl_roundl
331 extern long double roundl (long double x);
333 #elif defined GNULIB_POSIXCHECK
336 (GL_LINK_WARNING ("roundl is unportable - " \
337 "use gnulib module roundl for portability"), \
345 # define sinl rpl_sinl
347 # if !@HAVE_SINL@ || !@HAVE_DECL_SINL@
348 extern long double sinl (long double x);
350 #elif defined GNULIB_POSIXCHECK
353 (GL_LINK_WARNING ("sinl is unportable - " \
354 "use gnulib module mathl for portability"), \
360 # if !@HAVE_SQRTL@ || !@HAVE_DECL_SQRTL@
361 extern long double sqrtl (long double x);
363 #elif defined GNULIB_POSIXCHECK
366 (GL_LINK_WARNING ("sqrtl is unportable - " \
367 "use gnulib module mathl for portability"), \
373 # if !@HAVE_TANL@ || !@HAVE_DECL_TANL@
374 extern long double tanl (long double x);
376 #elif defined GNULIB_POSIXCHECK
379 (GL_LINK_WARNING ("tanl is unportable - " \
380 "use gnulib module mathl for portability"), \
386 # if !@HAVE_DECL_TRUNCF@
387 # define truncf rpl_truncf
388 extern float truncf (float x);
390 #elif defined GNULIB_POSIXCHECK
393 (GL_LINK_WARNING ("truncf is unportable - " \
394 "use gnulib module truncf for portability"), \
399 # if !@HAVE_DECL_TRUNC@
400 # define trunc rpl_trunc
401 extern double trunc (double x);
403 #elif defined GNULIB_POSIXCHECK
406 (GL_LINK_WARNING ("trunc is unportable - " \
407 "use gnulib module trunc for portability"), \
412 # if @REPLACE_TRUNCL@
414 # define truncl rpl_truncl
415 extern long double truncl (long double x);
417 #elif defined GNULIB_POSIXCHECK
420 (GL_LINK_WARNING ("truncl is unportable - " \
421 "use gnulib module truncl for portability"), \
426 #if @GNULIB_ISFINITE@
427 # if @REPLACE_ISFINITE@
428 extern int gl_isfinitef (float x);
429 extern int gl_isfinited (double x);
430 extern int gl_isfinitel (long double x);
432 # define isfinite(x) \
433 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
434 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
437 #elif defined GNULIB_POSIXCHECK
438 # if defined isfinite
439 _GL_WARN_REAL_FLOATING_DECL (isfinite);
441 # define isfinite(x) _GL_WARN_REAL_FLOATING_IMPL (isfinite, x)
448 extern int gl_isinff (float x);
449 extern int gl_isinfd (double x);
450 extern int gl_isinfl (long double x);
453 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
454 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
457 #elif defined GNULIB_POSIXCHECK
459 _GL_WARN_REAL_FLOATING_DECL (isinf);
461 # define isinf(x) _GL_WARN_REAL_FLOATING_IMPL (isinf, x)
467 /* Test for NaN for 'float' numbers. */
469 /* The original <math.h> included above provides a declaration of isnan macro
470 or (older) isnanf function. */
472 /* GCC 4.0 and newer provides three built-ins for isnan. */
474 # define isnanf(x) __builtin_isnanf ((float)(x))
477 # define isnanf(x) isnan ((float)(x))
480 /* Test whether X is a NaN. */
482 # define isnanf rpl_isnanf
483 extern int isnanf (float x);
488 /* Test for NaN for 'double' numbers.
489 This function is a gnulib extension, unlike isnan() which applied only
490 to 'double' numbers earlier but now is a type-generic macro. */
492 /* The original <math.h> included above provides a declaration of isnan macro. */
494 /* GCC 4.0 and newer provides three built-ins for isnan. */
496 # define isnand(x) __builtin_isnan ((double)(x))
499 # define isnand(x) isnan ((double)(x))
502 /* Test whether X is a NaN. */
504 # define isnand rpl_isnand
505 extern int isnand (double x);
510 /* Test for NaN for 'long double' numbers. */
512 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
514 /* GCC 4.0 and newer provides three built-ins for isnan. */
516 # define isnanl(x) __builtin_isnanl ((long double)(x))
519 # define isnanl(x) isnan ((long double)(x))
522 /* Test whether X is a NaN. */
524 # define isnanl rpl_isnanl
525 extern int isnanl (long double x);
529 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
532 /* We can't just use the isnanf macro (e.g.) as exposed by
533 isnanf.h (e.g.) here, because those may end up being macros
534 that recursively expand back to isnan. So use the gnulib
535 replacements for them directly. */
536 # if @HAVE_ISNANF@ && __GNUC__ >= 4
537 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
539 extern int rpl_isnanf (float x);
540 # define gl_isnan_f(x) rpl_isnanf (x)
542 # if @HAVE_ISNAND@ && __GNUC__ >= 4
543 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
545 extern int rpl_isnand (double x);
546 # define gl_isnan_d(x) rpl_isnand (x)
548 # if @HAVE_ISNANL@ && __GNUC__ >= 4
549 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
551 extern int rpl_isnanl (long double x);
552 # define gl_isnan_l(x) rpl_isnanl (x)
556 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
557 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
560 #elif defined GNULIB_POSIXCHECK
562 _GL_WARN_REAL_FLOATING_DECL (isnan);
564 # define isnan(x) _GL_WARN_REAL_FLOATING_IMPL (isnan, x)
570 # if @REPLACE_SIGNBIT_USING_GCC@
572 /* GCC 4.0 and newer provides three built-ins for signbit. */
573 # define signbit(x) \
574 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
575 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
576 __builtin_signbitf (x))
578 # if @REPLACE_SIGNBIT@
580 extern int gl_signbitf (float arg);
581 extern int gl_signbitd (double arg);
582 extern int gl_signbitl (long double arg);
583 # if __GNUC__ >= 2 && !__STRICT_ANSI__
584 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
585 # define gl_signbitf_OPTIMIZED_MACRO
586 # define gl_signbitf(arg) \
587 ({ union { float _value; \
588 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
591 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
594 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
595 # define gl_signbitd_OPTIMIZED_MACRO
596 # define gl_signbitd(arg) \
597 ({ union { double _value; \
598 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
601 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
604 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
605 # define gl_signbitl_OPTIMIZED_MACRO
606 # define gl_signbitl(arg) \
607 ({ union { long double _value; \
608 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
611 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
615 # define signbit(x) \
616 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
617 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
620 #elif defined GNULIB_POSIXCHECK
622 _GL_WARN_REAL_FLOATING_DECL (signbit);
624 # define signbit(x) _GL_WARN_REAL_FLOATING_IMPL (signbit, x)
633 #endif /* _GL_MATH_H */
634 #endif /* _GL_MATH_H */