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. */
41 /* POSIX allows platforms that don't support NAN. But all major
42 machines in the past 15 years have supported something close to
43 IEEE NaN, so we define this unconditionally. We also must define
44 it on platforms like Solaris 10, where NAN is present but defined
45 as a function pointer rather than a floating point constant. */
46 #if !defined NAN || @REPLACE_NAN@
48 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
53 static float zero = 0.0f;
58 # define NAN (0.0f / 0.0f)
62 /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
63 than a floating point constant. */
64 #if @REPLACE_HUGE_VAL@
66 # define HUGE_VAL (1.0 / 0.0)
72 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
73 If x is zero: mantissa = x, exp = 0.
74 If x is infinite or NaN: mantissa = x, exp unspecified.
75 Store exp in *EXPPTR and return mantissa. */
78 # define frexp rpl_frexp
79 extern double frexp (double x, int *expptr) _GL_ARG_NONNULL ((2));
81 #elif defined GNULIB_POSIXCHECK
84 (GL_LINK_WARNING ("frexp is unportable - " \
85 "use gnulib module frexp for portability"), \
90 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
91 extern long double acosl (long double x);
93 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
96 (GL_LINK_WARNING ("acosl is unportable - " \
97 "use gnulib module mathl for portability"), \
102 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
103 extern long double asinl (long double x);
105 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
108 (GL_LINK_WARNING ("asinl is unportable - " \
109 "use gnulib module mathl for portability"), \
114 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
115 extern long double atanl (long double x);
117 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
120 (GL_LINK_WARNING ("atanl is unportable - " \
121 "use gnulib module mathl for portability"), \
128 # define ceilf rpl_ceilf
129 extern float ceilf (float x);
131 #elif defined GNULIB_POSIXCHECK
134 (GL_LINK_WARNING ("ceilf is unportable - " \
135 "use gnulib module ceilf for portability"), \
141 # define ceill rpl_ceill
142 extern long double ceill (long double x);
144 #elif defined GNULIB_POSIXCHECK
147 (GL_LINK_WARNING ("ceill is unportable - " \
148 "use gnulib module ceill for portability"), \
153 #if @GNULIB_MATHL@ || (!@HAVE_DECL_COSL@ && !defined cosl)
155 extern long double cosl (long double x);
157 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
160 (GL_LINK_WARNING ("cosl is unportable - " \
161 "use gnulib module mathl for portability"), \
166 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
167 extern long double expl (long double x);
169 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
172 (GL_LINK_WARNING ("expl is unportable - " \
173 "use gnulib module mathl for portability"), \
179 # if @REPLACE_FLOORF@
180 # define floorf rpl_floorf
181 extern float floorf (float x);
183 #elif defined GNULIB_POSIXCHECK
186 (GL_LINK_WARNING ("floorf is unportable - " \
187 "use gnulib module floorf for portability"), \
192 # if @REPLACE_FLOORL@
193 # define floorl rpl_floorl
194 extern long double floorl (long double x);
196 #elif defined GNULIB_POSIXCHECK
199 (GL_LINK_WARNING ("floorl is unportable - " \
200 "use gnulib module floorl for portability"), \
208 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
209 If x is zero: mantissa = x, exp = 0.
210 If x is infinite or NaN: mantissa = x, exp unspecified.
211 Store exp in *EXPPTR and return mantissa. */
212 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
213 # define frexpl rpl_frexpl
215 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
216 extern long double frexpl (long double x, int *expptr) _GL_ARG_NONNULL ((2));
218 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
220 # define frexpl(x,e) \
221 (GL_LINK_WARNING ("frexpl is unportable - " \
222 "use gnulib module frexpl for portability"), \
227 /* Return x * 2^exp. */
228 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
229 # define ldexpl rpl_ldexpl
231 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
232 extern long double ldexpl (long double x, int exp);
234 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
236 # define ldexpl(x,e) \
237 (GL_LINK_WARNING ("ldexpl is unportable - " \
238 "use gnulib module ldexpl for portability"), \
243 #if @GNULIB_MATHL@ || (!@HAVE_DECL_LOGL@ && !defined logl)
245 extern long double logl (long double x);
247 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
250 (GL_LINK_WARNING ("logl is unportable - " \
251 "use gnulib module mathl for portability"), \
257 # if @REPLACE_ROUNDF@
259 # define roundf rpl_roundf
260 extern float roundf (float x);
262 #elif defined GNULIB_POSIXCHECK
265 (GL_LINK_WARNING ("roundf is unportable - " \
266 "use gnulib module roundf for portability"), \
273 # define round rpl_round
274 extern double round (double x);
276 #elif defined GNULIB_POSIXCHECK
279 (GL_LINK_WARNING ("round is unportable - " \
280 "use gnulib module round for portability"), \
285 # if @REPLACE_ROUNDL@
287 # define roundl rpl_roundl
288 extern long double roundl (long double x);
290 #elif defined GNULIB_POSIXCHECK
293 (GL_LINK_WARNING ("roundl is unportable - " \
294 "use gnulib module roundl for portability"), \
299 #if @GNULIB_MATHL@ || (!@HAVE_DECL_SINL@ && !defined sinl)
301 extern long double sinl (long double x);
303 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
306 (GL_LINK_WARNING ("sinl is unportable - " \
307 "use gnulib module mathl for portability"), \
312 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
313 extern long double sqrtl (long double x);
315 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
318 (GL_LINK_WARNING ("sqrtl is unportable - " \
319 "use gnulib module mathl for portability"), \
324 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
325 extern long double tanl (long double x);
327 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
330 (GL_LINK_WARNING ("tanl is unportable - " \
331 "use gnulib module mathl for portability"), \
337 # if !@HAVE_DECL_TRUNCF@
338 # define truncf rpl_truncf
339 extern float truncf (float x);
341 #elif defined GNULIB_POSIXCHECK
344 (GL_LINK_WARNING ("truncf is unportable - " \
345 "use gnulib module truncf for portability"), \
350 # if !@HAVE_DECL_TRUNC@
351 # define trunc rpl_trunc
352 extern double trunc (double x);
354 #elif defined GNULIB_POSIXCHECK
357 (GL_LINK_WARNING ("trunc is unportable - " \
358 "use gnulib module trunc for portability"), \
363 # if @REPLACE_TRUNCL@
365 # define truncl rpl_truncl
366 extern long double truncl (long double x);
368 #elif defined GNULIB_POSIXCHECK
371 (GL_LINK_WARNING ("truncl is unportable - " \
372 "use gnulib module truncl for portability"), \
377 #if @GNULIB_ISFINITE@
378 # if @REPLACE_ISFINITE@
379 extern int gl_isfinitef (float x);
380 extern int gl_isfinited (double x);
381 extern int gl_isfinitel (long double x);
383 # define isfinite(x) \
384 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
385 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
388 #elif defined GNULIB_POSIXCHECK
389 /* How to override a macro? */
395 extern int gl_isinff (float x);
396 extern int gl_isinfd (double x);
397 extern int gl_isinfl (long double x);
400 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
401 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
404 #elif defined GNULIB_POSIXCHECK
405 /* How to override a macro? */
410 /* Test for NaN for 'float' numbers. */
412 /* The original <math.h> included above provides a declaration of isnan macro
413 or (older) isnanf function. */
415 /* GCC 4.0 and newer provides three built-ins for isnan. */
417 # define isnanf(x) __builtin_isnanf ((float)(x))
420 # define isnanf(x) isnan ((float)(x))
423 /* Test whether X is a NaN. */
425 # define isnanf rpl_isnanf
426 extern int isnanf (float x);
431 /* Test for NaN for 'double' numbers.
432 This function is a gnulib extension, unlike isnan() which applied only
433 to 'double' numbers earlier but now is a type-generic macro. */
435 /* The original <math.h> included above provides a declaration of isnan macro. */
437 /* GCC 4.0 and newer provides three built-ins for isnan. */
439 # define isnand(x) __builtin_isnan ((double)(x))
442 # define isnand(x) isnan ((double)(x))
445 /* Test whether X is a NaN. */
447 # define isnand rpl_isnand
448 extern int isnand (double x);
453 /* Test for NaN for 'long double' numbers. */
455 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
457 /* GCC 4.0 and newer provides three built-ins for isnan. */
459 # define isnanl(x) __builtin_isnanl ((long double)(x))
462 # define isnanl(x) isnan ((long double)(x))
465 /* Test whether X is a NaN. */
467 # define isnanl rpl_isnanl
468 extern int isnanl (long double x);
472 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
475 /* We can't just use the isnanf macro (e.g.) as exposed by
476 isnanf.h (e.g.) here, because those may end up being macros
477 that recursively expand back to isnan. So use the gnulib
478 replacements for them directly. */
479 # if @HAVE_ISNANF@ && __GNUC__ >= 4
480 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
482 extern int rpl_isnanf (float x);
483 # define gl_isnan_f(x) rpl_isnanf (x)
485 # if @HAVE_ISNAND@ && __GNUC__ >= 4
486 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
488 extern int rpl_isnand (double x);
489 # define gl_isnan_d(x) rpl_isnand (x)
491 # if @HAVE_ISNANL@ && __GNUC__ >= 4
492 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
494 extern int rpl_isnanl (long double x);
495 # define gl_isnan_l(x) rpl_isnanl (x)
499 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
500 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
503 #elif defined GNULIB_POSIXCHECK
504 /* How to override a macro? */
509 # if @REPLACE_SIGNBIT_USING_GCC@
511 /* GCC 4.0 and newer provides three built-ins for signbit. */
512 # define signbit(x) \
513 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
514 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
515 __builtin_signbitf (x))
517 # if @REPLACE_SIGNBIT@
519 extern int gl_signbitf (float arg);
520 extern int gl_signbitd (double arg);
521 extern int gl_signbitl (long double arg);
522 # if __GNUC__ >= 2 && !__STRICT_ANSI__
523 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
524 # define gl_signbitf_OPTIMIZED_MACRO
525 # define gl_signbitf(arg) \
526 ({ union { float _value; \
527 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
530 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
533 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
534 # define gl_signbitd_OPTIMIZED_MACRO
535 # define gl_signbitd(arg) \
536 ({ union { double _value; \
537 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
540 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
543 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
544 # define gl_signbitl_OPTIMIZED_MACRO
545 # define gl_signbitl(arg) \
546 ({ union { long double _value; \
547 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
550 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
554 # define signbit(x) \
555 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
556 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
559 #elif defined GNULIB_POSIXCHECK
560 /* How to override a macro? */
568 #endif /* _GL_MATH_H */
569 #endif /* _GL_MATH_H */