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_ARG_NONNULL is copied here. */
33 /* The definition of _GL_WARN_ON_USE 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 /* Assume frexp is always declared. */
115 _GL_WARN_ON_USE (frexp, "frexp is unportable - "
116 "use gnulib module frexp for portability");
121 # if !@HAVE_ACOSL@ || !@HAVE_DECL_ACOSL@
122 extern long double acosl (long double x);
124 #elif defined GNULIB_POSIXCHECK
126 # if HAVE_RAW_DECL_ACOSL
127 _GL_WARN_ON_USE (acosl, "acosl is unportable - "
128 "use gnulib module mathl for portability");
134 # if !@HAVE_ASINL@ || !@HAVE_DECL_ASINL@
135 extern long double asinl (long double x);
137 #elif defined GNULIB_POSIXCHECK
139 # if HAVE_RAW_DECL_ASINL
140 _GL_WARN_ON_USE (asinl, "asinl is unportable - "
141 "use gnulib module mathl for portability");
147 # if !@HAVE_ATANL@ || !@HAVE_DECL_ATANL@
148 extern long double atanl (long double x);
150 #elif defined GNULIB_POSIXCHECK
152 # if HAVE_RAW_DECL_ATANL
153 _GL_WARN_ON_USE (atanl, "atanl is unportable - "
154 "use gnulib module mathl for portability");
161 # define ceilf rpl_ceilf
162 extern float ceilf (float x);
164 #elif defined GNULIB_POSIXCHECK
166 # if HAVE_RAW_DECL_CEILF
167 _GL_WARN_ON_USE (ceilf, "ceilf is unportable - "
168 "use gnulib module ceilf for portability");
174 # define ceill rpl_ceill
175 extern long double ceill (long double x);
177 #elif defined GNULIB_POSIXCHECK
179 # if HAVE_RAW_DECL_CEILL
180 _GL_WARN_ON_USE (ceill, "ceill is unportable - "
181 "use gnulib module ceill for portability");
189 # define cosl rpl_cosl
191 # if !@HAVE_COSL@ || !@HAVE_DECL_COSL@
192 extern long double cosl (long double x);
194 #elif defined GNULIB_POSIXCHECK
196 # if HAVE_RAW_DECL_COSL
197 _GL_WARN_ON_USE (cosl, "cosl is unportable - "
198 "use gnulib module mathl for portability");
204 # if !@HAVE_EXPL@ || !@HAVE_DECL_EXPL@
205 extern long double expl (long double x);
207 #elif defined GNULIB_POSIXCHECK
209 # if HAVE_RAW_DECL_EXPL
210 _GL_WARN_ON_USE (expl, "expl is unportable - "
211 "use gnulib module mathl for portability");
217 # if @REPLACE_FLOORF@
218 # define floorf rpl_floorf
219 extern float floorf (float x);
221 #elif defined GNULIB_POSIXCHECK
223 # if HAVE_RAW_DECL_FLOORF
224 _GL_WARN_ON_USE (floorf, "floorf is unportable - "
225 "use gnulib module floorf for portability");
230 # if @REPLACE_FLOORL@
231 # define floorl rpl_floorl
232 extern long double floorl (long double x);
234 #elif defined GNULIB_POSIXCHECK
236 # if HAVE_RAW_DECL_FLOORL
237 _GL_WARN_ON_USE (floorl, "floorl is unportable - "
238 "use gnulib module floorl for portability");
246 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
247 If x is zero: mantissa = x, exp = 0.
248 If x is infinite or NaN: mantissa = x, exp unspecified.
249 Store exp in *EXPPTR and return mantissa. */
250 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
251 # define frexpl rpl_frexpl
253 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
254 extern long double frexpl (long double x, int *expptr) _GL_ARG_NONNULL ((2));
256 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
258 # if HAVE_RAW_DECL_FREXPL
259 _GL_WARN_ON_USE (frexpl, "frexpl is unportable - "
260 "use gnulib module frexpl for portability");
265 /* Return x * 2^exp. */
266 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
267 # define ldexpl rpl_ldexpl
269 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
270 extern long double ldexpl (long double x, int exp);
272 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
274 # if HAVE_RAW_DECL_LDEXPL
275 _GL_WARN_ON_USE (ldexpl, "ldexpl is unportable - "
276 "use gnulib module ldexpl for portability");
284 # define logl rpl_logl
286 # if !@HAVE_LOGL@ || !@HAVE_DECL_LOGL@
287 extern long double logl (long double x);
289 #elif defined GNULIB_POSIXCHECK
291 # if HAVE_RAW_DECL_LOGL
292 _GL_WARN_ON_USE (logl, "logl is unportable - "
293 "use gnulib module mathl for portability");
299 # if @REPLACE_ROUNDF@
301 # define roundf rpl_roundf
302 extern float roundf (float x);
304 #elif defined GNULIB_POSIXCHECK
306 # if HAVE_RAW_DECL_ROUNDF
307 _GL_WARN_ON_USE (roundf, "roundf is unportable - "
308 "use gnulib module roundf for portability");
315 # define round rpl_round
316 extern double round (double x);
318 #elif defined GNULIB_POSIXCHECK
320 # if HAVE_RAW_DECL_ROUND
321 _GL_WARN_ON_USE (round, "round is unportable - "
322 "use gnulib module round for portability");
327 # if @REPLACE_ROUNDL@
329 # define roundl rpl_roundl
330 extern long double roundl (long double x);
332 #elif defined GNULIB_POSIXCHECK
334 # if HAVE_RAW_DECL_ROUNDL
335 _GL_WARN_ON_USE (roundl, "roundl is unportable - "
336 "use gnulib module roundl for portability");
344 # define sinl rpl_sinl
346 # if !@HAVE_SINL@ || !@HAVE_DECL_SINL@
347 extern long double sinl (long double x);
349 #elif defined GNULIB_POSIXCHECK
351 # if HAVE_RAW_DECL_SINL
352 _GL_WARN_ON_USE (sinl, "sinl is unportable - "
353 "use gnulib module mathl for portability");
359 # if !@HAVE_SQRTL@ || !@HAVE_DECL_SQRTL@
360 extern long double sqrtl (long double x);
362 #elif defined GNULIB_POSIXCHECK
364 # if HAVE_RAW_DECL_SQRTL
365 _GL_WARN_ON_USE (sqrtl, "sqrtl is unportable - "
366 "use gnulib module mathl for portability");
372 # if !@HAVE_TANL@ || !@HAVE_DECL_TANL@
373 extern long double tanl (long double x);
375 #elif defined GNULIB_POSIXCHECK
377 # if HAVE_RAW_DECL_TANL
378 _GL_WARN_ON_USE (tanl, "tanl is unportable - "
379 "use gnulib module mathl for portability");
385 # if !@HAVE_DECL_TRUNCF@
386 # define truncf rpl_truncf
387 extern float truncf (float x);
389 #elif defined GNULIB_POSIXCHECK
391 # if HAVE_RAW_DECL_TRUNCF
392 _GL_WARN_ON_USE (truncf, "truncf is unportable - "
393 "use gnulib module truncf for portability");
398 # if !@HAVE_DECL_TRUNC@
399 # define trunc rpl_trunc
400 extern double trunc (double x);
402 #elif defined GNULIB_POSIXCHECK
404 # if HAVE_RAW_DECL_TRUNC
405 _GL_WARN_ON_USE (trunc, "trunc is unportable - "
406 "use gnulib module trunc for portability");
411 # if @REPLACE_TRUNCL@
413 # define truncl rpl_truncl
414 extern long double truncl (long double x);
416 #elif defined GNULIB_POSIXCHECK
418 # if HAVE_RAW_DECL_TRUNCL
419 _GL_WARN_ON_USE (truncl, "truncl is unportable - "
420 "use gnulib module truncl for portability");
425 #if @GNULIB_ISFINITE@
426 # if @REPLACE_ISFINITE@
427 extern int gl_isfinitef (float x);
428 extern int gl_isfinited (double x);
429 extern int gl_isfinitel (long double x);
431 # define isfinite(x) \
432 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
433 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
436 #elif defined GNULIB_POSIXCHECK
437 # if defined isfinite
438 _GL_WARN_REAL_FLOATING_DECL (isfinite);
440 # define isfinite(x) _GL_WARN_REAL_FLOATING_IMPL (isfinite, x)
447 extern int gl_isinff (float x);
448 extern int gl_isinfd (double x);
449 extern int gl_isinfl (long double x);
452 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
453 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
456 #elif defined GNULIB_POSIXCHECK
458 _GL_WARN_REAL_FLOATING_DECL (isinf);
460 # define isinf(x) _GL_WARN_REAL_FLOATING_IMPL (isinf, x)
466 /* Test for NaN for 'float' numbers. */
468 /* The original <math.h> included above provides a declaration of isnan macro
469 or (older) isnanf function. */
471 /* GCC 4.0 and newer provides three built-ins for isnan. */
473 # define isnanf(x) __builtin_isnanf ((float)(x))
476 # define isnanf(x) isnan ((float)(x))
479 /* Test whether X is a NaN. */
481 # define isnanf rpl_isnanf
482 extern int isnanf (float x);
487 /* Test for NaN for 'double' numbers.
488 This function is a gnulib extension, unlike isnan() which applied only
489 to 'double' numbers earlier but now is a type-generic macro. */
491 /* The original <math.h> included above provides a declaration of isnan macro. */
493 /* GCC 4.0 and newer provides three built-ins for isnan. */
495 # define isnand(x) __builtin_isnan ((double)(x))
498 # define isnand(x) isnan ((double)(x))
501 /* Test whether X is a NaN. */
503 # define isnand rpl_isnand
504 extern int isnand (double x);
509 /* Test for NaN for 'long double' numbers. */
511 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
513 /* GCC 4.0 and newer provides three built-ins for isnan. */
515 # define isnanl(x) __builtin_isnanl ((long double)(x))
518 # define isnanl(x) isnan ((long double)(x))
521 /* Test whether X is a NaN. */
523 # define isnanl rpl_isnanl
524 extern int isnanl (long double x);
528 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
531 /* We can't just use the isnanf macro (e.g.) as exposed by
532 isnanf.h (e.g.) here, because those may end up being macros
533 that recursively expand back to isnan. So use the gnulib
534 replacements for them directly. */
535 # if @HAVE_ISNANF@ && __GNUC__ >= 4
536 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
538 extern int rpl_isnanf (float x);
539 # define gl_isnan_f(x) rpl_isnanf (x)
541 # if @HAVE_ISNAND@ && __GNUC__ >= 4
542 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
544 extern int rpl_isnand (double x);
545 # define gl_isnan_d(x) rpl_isnand (x)
547 # if @HAVE_ISNANL@ && __GNUC__ >= 4
548 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
550 extern int rpl_isnanl (long double x);
551 # define gl_isnan_l(x) rpl_isnanl (x)
555 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
556 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
559 #elif defined GNULIB_POSIXCHECK
561 _GL_WARN_REAL_FLOATING_DECL (isnan);
563 # define isnan(x) _GL_WARN_REAL_FLOATING_IMPL (isnan, x)
569 # if @REPLACE_SIGNBIT_USING_GCC@
571 /* GCC 4.0 and newer provides three built-ins for signbit. */
572 # define signbit(x) \
573 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
574 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
575 __builtin_signbitf (x))
577 # if @REPLACE_SIGNBIT@
579 extern int gl_signbitf (float arg);
580 extern int gl_signbitd (double arg);
581 extern int gl_signbitl (long double arg);
582 # if __GNUC__ >= 2 && !__STRICT_ANSI__
583 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
584 # define gl_signbitf_OPTIMIZED_MACRO
585 # define gl_signbitf(arg) \
586 ({ union { float _value; \
587 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
590 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
593 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
594 # define gl_signbitd_OPTIMIZED_MACRO
595 # define gl_signbitd(arg) \
596 ({ union { double _value; \
597 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
600 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
603 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
604 # define gl_signbitl_OPTIMIZED_MACRO
605 # define gl_signbitl(arg) \
606 ({ union { long double _value; \
607 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
610 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
614 # define signbit(x) \
615 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
616 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
619 #elif defined GNULIB_POSIXCHECK
621 _GL_WARN_REAL_FLOATING_DECL (signbit);
623 # define signbit(x) _GL_WARN_REAL_FLOATING_IMPL (signbit, x)
632 #endif /* _GL_MATH_H */
633 #endif /* _GL_MATH_H */