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"), \
121 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
122 extern long double acosl (long double x);
124 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
127 (GL_LINK_WARNING ("acosl is unportable - " \
128 "use gnulib module mathl for portability"), \
133 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
134 extern long double asinl (long double x);
136 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
139 (GL_LINK_WARNING ("asinl is unportable - " \
140 "use gnulib module mathl for portability"), \
145 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
146 extern long double atanl (long double x);
148 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
151 (GL_LINK_WARNING ("atanl is unportable - " \
152 "use gnulib module mathl for portability"), \
159 # define ceilf rpl_ceilf
160 extern float ceilf (float x);
162 #elif defined GNULIB_POSIXCHECK
165 (GL_LINK_WARNING ("ceilf is unportable - " \
166 "use gnulib module ceilf for portability"), \
172 # define ceill rpl_ceill
173 extern long double ceill (long double x);
175 #elif defined GNULIB_POSIXCHECK
178 (GL_LINK_WARNING ("ceill is unportable - " \
179 "use gnulib module ceill for portability"), \
184 #if @GNULIB_MATHL@ || (!@HAVE_DECL_COSL@ && !defined cosl)
186 extern long double cosl (long double x);
188 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
191 (GL_LINK_WARNING ("cosl is unportable - " \
192 "use gnulib module mathl for portability"), \
197 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
198 extern long double expl (long double x);
200 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
203 (GL_LINK_WARNING ("expl is unportable - " \
204 "use gnulib module mathl for portability"), \
210 # if @REPLACE_FLOORF@
211 # define floorf rpl_floorf
212 extern float floorf (float x);
214 #elif defined GNULIB_POSIXCHECK
217 (GL_LINK_WARNING ("floorf is unportable - " \
218 "use gnulib module floorf for portability"), \
223 # if @REPLACE_FLOORL@
224 # define floorl rpl_floorl
225 extern long double floorl (long double x);
227 #elif defined GNULIB_POSIXCHECK
230 (GL_LINK_WARNING ("floorl is unportable - " \
231 "use gnulib module floorl for portability"), \
239 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
240 If x is zero: mantissa = x, exp = 0.
241 If x is infinite or NaN: mantissa = x, exp unspecified.
242 Store exp in *EXPPTR and return mantissa. */
243 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
244 # define frexpl rpl_frexpl
246 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
247 extern long double frexpl (long double x, int *expptr) _GL_ARG_NONNULL ((2));
249 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
251 # define frexpl(x,e) \
252 (GL_LINK_WARNING ("frexpl is unportable - " \
253 "use gnulib module frexpl for portability"), \
258 /* Return x * 2^exp. */
259 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
260 # define ldexpl rpl_ldexpl
262 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
263 extern long double ldexpl (long double x, int exp);
265 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
267 # define ldexpl(x,e) \
268 (GL_LINK_WARNING ("ldexpl is unportable - " \
269 "use gnulib module ldexpl for portability"), \
274 #if @GNULIB_MATHL@ || (!@HAVE_DECL_LOGL@ && !defined logl)
276 extern long double logl (long double x);
278 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
281 (GL_LINK_WARNING ("logl is unportable - " \
282 "use gnulib module mathl for portability"), \
288 # if @REPLACE_ROUNDF@
290 # define roundf rpl_roundf
291 extern float roundf (float x);
293 #elif defined GNULIB_POSIXCHECK
296 (GL_LINK_WARNING ("roundf is unportable - " \
297 "use gnulib module roundf for portability"), \
304 # define round rpl_round
305 extern double round (double x);
307 #elif defined GNULIB_POSIXCHECK
310 (GL_LINK_WARNING ("round is unportable - " \
311 "use gnulib module round for portability"), \
316 # if @REPLACE_ROUNDL@
318 # define roundl rpl_roundl
319 extern long double roundl (long double x);
321 #elif defined GNULIB_POSIXCHECK
324 (GL_LINK_WARNING ("roundl is unportable - " \
325 "use gnulib module roundl for portability"), \
330 #if @GNULIB_MATHL@ || (!@HAVE_DECL_SINL@ && !defined sinl)
332 extern long double sinl (long double x);
334 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
337 (GL_LINK_WARNING ("sinl is unportable - " \
338 "use gnulib module mathl for portability"), \
343 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
344 extern long double sqrtl (long double x);
346 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
349 (GL_LINK_WARNING ("sqrtl is unportable - " \
350 "use gnulib module mathl for portability"), \
355 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
356 extern long double tanl (long double x);
358 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
361 (GL_LINK_WARNING ("tanl is unportable - " \
362 "use gnulib module mathl for portability"), \
368 # if !@HAVE_DECL_TRUNCF@
369 # define truncf rpl_truncf
370 extern float truncf (float x);
372 #elif defined GNULIB_POSIXCHECK
375 (GL_LINK_WARNING ("truncf is unportable - " \
376 "use gnulib module truncf for portability"), \
381 # if !@HAVE_DECL_TRUNC@
382 # define trunc rpl_trunc
383 extern double trunc (double x);
385 #elif defined GNULIB_POSIXCHECK
388 (GL_LINK_WARNING ("trunc is unportable - " \
389 "use gnulib module trunc for portability"), \
394 # if @REPLACE_TRUNCL@
396 # define truncl rpl_truncl
397 extern long double truncl (long double x);
399 #elif defined GNULIB_POSIXCHECK
402 (GL_LINK_WARNING ("truncl is unportable - " \
403 "use gnulib module truncl for portability"), \
408 #if @GNULIB_ISFINITE@
409 # if @REPLACE_ISFINITE@
410 extern int gl_isfinitef (float x);
411 extern int gl_isfinited (double x);
412 extern int gl_isfinitel (long double x);
414 # define isfinite(x) \
415 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
416 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
419 #elif defined GNULIB_POSIXCHECK
420 # if defined isfinite
421 _GL_WARN_REAL_FLOATING_DECL (isfinite);
423 # define isfinite(x) _GL_WARN_REAL_FLOATING_IMPL (isfinite, x)
430 extern int gl_isinff (float x);
431 extern int gl_isinfd (double x);
432 extern int gl_isinfl (long double x);
435 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
436 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
439 #elif defined GNULIB_POSIXCHECK
441 _GL_WARN_REAL_FLOATING_DECL (isinf);
443 # define isinf(x) _GL_WARN_REAL_FLOATING_IMPL (isinf, x)
449 /* Test for NaN for 'float' numbers. */
451 /* The original <math.h> included above provides a declaration of isnan macro
452 or (older) isnanf function. */
454 /* GCC 4.0 and newer provides three built-ins for isnan. */
456 # define isnanf(x) __builtin_isnanf ((float)(x))
459 # define isnanf(x) isnan ((float)(x))
462 /* Test whether X is a NaN. */
464 # define isnanf rpl_isnanf
465 extern int isnanf (float x);
470 /* Test for NaN for 'double' numbers.
471 This function is a gnulib extension, unlike isnan() which applied only
472 to 'double' numbers earlier but now is a type-generic macro. */
474 /* The original <math.h> included above provides a declaration of isnan macro. */
476 /* GCC 4.0 and newer provides three built-ins for isnan. */
478 # define isnand(x) __builtin_isnan ((double)(x))
481 # define isnand(x) isnan ((double)(x))
484 /* Test whether X is a NaN. */
486 # define isnand rpl_isnand
487 extern int isnand (double x);
492 /* Test for NaN for 'long double' numbers. */
494 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
496 /* GCC 4.0 and newer provides three built-ins for isnan. */
498 # define isnanl(x) __builtin_isnanl ((long double)(x))
501 # define isnanl(x) isnan ((long double)(x))
504 /* Test whether X is a NaN. */
506 # define isnanl rpl_isnanl
507 extern int isnanl (long double x);
511 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
514 /* We can't just use the isnanf macro (e.g.) as exposed by
515 isnanf.h (e.g.) here, because those may end up being macros
516 that recursively expand back to isnan. So use the gnulib
517 replacements for them directly. */
518 # if @HAVE_ISNANF@ && __GNUC__ >= 4
519 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
521 extern int rpl_isnanf (float x);
522 # define gl_isnan_f(x) rpl_isnanf (x)
524 # if @HAVE_ISNAND@ && __GNUC__ >= 4
525 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
527 extern int rpl_isnand (double x);
528 # define gl_isnan_d(x) rpl_isnand (x)
530 # if @HAVE_ISNANL@ && __GNUC__ >= 4
531 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
533 extern int rpl_isnanl (long double x);
534 # define gl_isnan_l(x) rpl_isnanl (x)
538 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
539 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
542 #elif defined GNULIB_POSIXCHECK
544 _GL_WARN_REAL_FLOATING_DECL (isnan);
546 # define isnan(x) _GL_WARN_REAL_FLOATING_IMPL (isnan, x)
552 # if @REPLACE_SIGNBIT_USING_GCC@
554 /* GCC 4.0 and newer provides three built-ins for signbit. */
555 # define signbit(x) \
556 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
557 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
558 __builtin_signbitf (x))
560 # if @REPLACE_SIGNBIT@
562 extern int gl_signbitf (float arg);
563 extern int gl_signbitd (double arg);
564 extern int gl_signbitl (long double arg);
565 # if __GNUC__ >= 2 && !__STRICT_ANSI__
566 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
567 # define gl_signbitf_OPTIMIZED_MACRO
568 # define gl_signbitf(arg) \
569 ({ union { float _value; \
570 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
573 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
576 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
577 # define gl_signbitd_OPTIMIZED_MACRO
578 # define gl_signbitd(arg) \
579 ({ union { double _value; \
580 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
583 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
586 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
587 # define gl_signbitl_OPTIMIZED_MACRO
588 # define gl_signbitl(arg) \
589 ({ union { long double _value; \
590 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
593 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
597 # define signbit(x) \
598 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
599 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
602 #elif defined GNULIB_POSIXCHECK
604 _GL_WARN_REAL_FLOATING_DECL (signbit);
606 # define signbit(x) _GL_WARN_REAL_FLOATING_IMPL (signbit, x)
615 #endif /* _GL_MATH_H */
616 #endif /* _GL_MATH_H */