1 /* A GNU-like <math.h>.
3 Copyright (C) 2002-2003, 2007-2009 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. */
39 /* POSIX allows platforms that don't support NAN. But all major
40 machines in the past 15 years have supported something close to
41 IEEE NaN, so we define this unconditionally. We also must define
42 it on platforms like Solaris 10, where NAN is present but defined
43 as a function pointer rather than a floating point constant. */
44 #if !defined NAN || @REPLACE_NAN@
46 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
51 static float zero = 0.0f;
56 # define NAN (0.0f / 0.0f)
60 /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
61 than a floating point constant. */
62 #if @REPLACE_HUGE_VAL@
64 # define HUGE_VAL (1.0 / 0.0)
70 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
71 If x is zero: mantissa = x, exp = 0.
72 If x is infinite or NaN: mantissa = x, exp unspecified.
73 Store exp in *EXPPTR and return mantissa. */
76 # define frexp rpl_frexp
77 extern double frexp (double x, int *expptr);
79 #elif defined GNULIB_POSIXCHECK
82 (GL_LINK_WARNING ("frexp is unportable - " \
83 "use gnulib module frexp for portability"), \
88 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
89 extern long double acosl (long double x);
91 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
94 (GL_LINK_WARNING ("acosl is unportable - " \
95 "use gnulib module mathl for portability"), \
100 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
101 extern long double asinl (long double x);
103 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
106 (GL_LINK_WARNING ("asinl is unportable - " \
107 "use gnulib module mathl for portability"), \
112 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
113 extern long double atanl (long double x);
115 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
118 (GL_LINK_WARNING ("atanl is unportable - " \
119 "use gnulib module mathl for portability"), \
126 # define ceilf rpl_ceilf
127 extern float ceilf (float x);
129 #elif defined GNULIB_POSIXCHECK
132 (GL_LINK_WARNING ("ceilf is unportable - " \
133 "use gnulib module ceilf for portability"), \
139 # define ceill rpl_ceill
140 extern long double ceill (long double x);
142 #elif defined GNULIB_POSIXCHECK
145 (GL_LINK_WARNING ("ceill is unportable - " \
146 "use gnulib module ceill for portability"), \
151 #if @GNULIB_MATHL@ || (!@HAVE_DECL_COSL@ && !defined cosl)
153 extern long double cosl (long double x);
155 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
158 (GL_LINK_WARNING ("cosl is unportable - " \
159 "use gnulib module mathl for portability"), \
164 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
165 extern long double expl (long double x);
167 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
170 (GL_LINK_WARNING ("expl is unportable - " \
171 "use gnulib module mathl for portability"), \
177 # if @REPLACE_FLOORF@
178 # define floorf rpl_floorf
179 extern float floorf (float x);
181 #elif defined GNULIB_POSIXCHECK
184 (GL_LINK_WARNING ("floorf is unportable - " \
185 "use gnulib module floorf for portability"), \
190 # if @REPLACE_FLOORL@
191 # define floorl rpl_floorl
192 extern long double floorl (long double x);
194 #elif defined GNULIB_POSIXCHECK
197 (GL_LINK_WARNING ("floorl is unportable - " \
198 "use gnulib module floorl for portability"), \
206 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
207 If x is zero: mantissa = x, exp = 0.
208 If x is infinite or NaN: mantissa = x, exp unspecified.
209 Store exp in *EXPPTR and return mantissa. */
210 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
211 # define frexpl rpl_frexpl
213 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
214 extern long double frexpl (long double x, int *expptr);
216 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
218 # define frexpl(x,e) \
219 (GL_LINK_WARNING ("frexpl is unportable - " \
220 "use gnulib module frexpl for portability"), \
225 /* Return x * 2^exp. */
226 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
227 # define ldexpl rpl_ldexpl
229 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
230 extern long double ldexpl (long double x, int exp);
232 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
234 # define ldexpl(x,e) \
235 (GL_LINK_WARNING ("ldexpl is unportable - " \
236 "use gnulib module ldexpl for portability"), \
241 #if @GNULIB_MATHL@ || (!@HAVE_DECL_LOGL@ && !defined logl)
243 extern long double logl (long double x);
245 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
248 (GL_LINK_WARNING ("logl is unportable - " \
249 "use gnulib module mathl for portability"), \
255 # if @REPLACE_ROUNDF@
257 # define roundf rpl_roundf
258 extern float roundf (float x);
260 #elif defined GNULIB_POSIXCHECK
263 (GL_LINK_WARNING ("roundf is unportable - " \
264 "use gnulib module roundf for portability"), \
271 # define round rpl_round
272 extern double round (double x);
274 #elif defined GNULIB_POSIXCHECK
277 (GL_LINK_WARNING ("round is unportable - " \
278 "use gnulib module round for portability"), \
283 # if @REPLACE_ROUNDL@
285 # define roundl rpl_roundl
286 extern long double roundl (long double x);
288 #elif defined GNULIB_POSIXCHECK
291 (GL_LINK_WARNING ("roundl is unportable - " \
292 "use gnulib module roundl for portability"), \
297 #if @GNULIB_MATHL@ || (!@HAVE_DECL_SINL@ && !defined sinl)
299 extern long double sinl (long double x);
301 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
304 (GL_LINK_WARNING ("sinl is unportable - " \
305 "use gnulib module mathl for portability"), \
310 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
311 extern long double sqrtl (long double x);
313 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
316 (GL_LINK_WARNING ("sqrtl is unportable - " \
317 "use gnulib module mathl for portability"), \
322 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
323 extern long double tanl (long double x);
325 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
328 (GL_LINK_WARNING ("tanl is unportable - " \
329 "use gnulib module mathl for portability"), \
335 # if !@HAVE_DECL_TRUNCF@
336 # define truncf rpl_truncf
337 extern float truncf (float x);
339 #elif defined GNULIB_POSIXCHECK
342 (GL_LINK_WARNING ("truncf is unportable - " \
343 "use gnulib module truncf for portability"), \
348 # if !@HAVE_DECL_TRUNC@
349 # define trunc rpl_trunc
350 extern double trunc (double x);
352 #elif defined GNULIB_POSIXCHECK
355 (GL_LINK_WARNING ("trunc is unportable - " \
356 "use gnulib module trunc for portability"), \
361 # if @REPLACE_TRUNCL@
363 # define truncl rpl_truncl
364 extern long double truncl (long double x);
366 #elif defined GNULIB_POSIXCHECK
369 (GL_LINK_WARNING ("truncl is unportable - " \
370 "use gnulib module truncl for portability"), \
375 #if @GNULIB_ISFINITE@
376 # if @REPLACE_ISFINITE@
377 extern int gl_isfinitef (float x);
378 extern int gl_isfinited (double x);
379 extern int gl_isfinitel (long double x);
381 # define isfinite(x) \
382 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
383 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
386 #elif defined GNULIB_POSIXCHECK
387 /* How to override a macro? */
393 extern int gl_isinff (float x);
394 extern int gl_isinfd (double x);
395 extern int gl_isinfl (long double x);
398 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
399 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
402 #elif defined GNULIB_POSIXCHECK
403 /* How to override a macro? */
408 /* Test for NaN for 'float' numbers. */
410 /* The original <math.h> included above provides a declaration of isnan macro
411 or (older) isnanf function. */
414 /* GCC 4.0 and newer provides three built-ins for isnan. */
416 # define isnanf(x) __builtin_isnanf ((float)(x))
419 # define isnanf(x) isnan ((float)(x))
422 /* Test whether X is a NaN. */
424 # define isnanf rpl_isnanf
425 extern int isnanf (float x);
430 /* Test for NaN for 'double' numbers.
431 This function is a gnulib extension, unlike isnan() which applied only
432 to 'double' numbers earlier but now is a type-generic macro. */
434 /* 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. */
458 /* GCC 4.0 and newer provides three built-ins for isnan. */
460 # define isnanl(x) __builtin_isnanl ((long double)(x))
463 # define isnanl(x) isnan ((long double)(x))
466 /* Test whether X is a NaN. */
468 # define isnanl rpl_isnanl
469 extern int isnanl (long double x);
473 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
476 /* We can't just use the isnanf macro (e.g.) as exposed by
477 isnanf.h (e.g.) here, because those may end up being macros
478 that recursively expand back to isnan. So use the gnulib
479 replacements for them directly. */
480 # if @HAVE_ISNANF@ && __GNUC__ >= 4
481 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
483 extern int rpl_isnanf (float x);
484 # define gl_isnan_f(x) rpl_isnanf (x)
486 # if @HAVE_ISNAND@ && __GNUC__ >= 4
487 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
489 extern int rpl_isnand (double x);
490 # define gl_isnan_d(x) rpl_isnand (x)
492 # if @HAVE_ISNANL@ && __GNUC__ >= 4
493 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
495 extern int rpl_isnanl (long double x);
496 # define gl_isnan_l(x) rpl_isnanl (x)
500 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
501 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
504 #elif defined GNULIB_POSIXCHECK
505 /* How to override a macro? */
510 # if @REPLACE_SIGNBIT_USING_GCC@
512 /* GCC 4.0 and newer provides three built-ins for signbit. */
513 # define signbit(x) \
514 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
515 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
516 __builtin_signbitf (x))
518 # if @REPLACE_SIGNBIT@
520 extern int gl_signbitf (float arg);
521 extern int gl_signbitd (double arg);
522 extern int gl_signbitl (long double arg);
523 # if __GNUC__ >= 2 && !__STRICT_ANSI__
524 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
525 # define gl_signbitf_OPTIMIZED_MACRO
526 # define gl_signbitf(arg) \
527 ({ union { float _value; \
528 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
531 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
534 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
535 # define gl_signbitd_OPTIMIZED_MACRO
536 # define gl_signbitd(arg) \
537 ({ union { double _value; \
538 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
541 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
544 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
545 # define gl_signbitl_OPTIMIZED_MACRO
546 # define gl_signbitl(arg) \
547 ({ union { long double _value; \
548 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
551 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
555 # define signbit(x) \
556 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
557 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
560 #elif defined GNULIB_POSIXCHECK
561 /* How to override a macro? */
569 #endif /* _GL_MATH_H */
570 #endif /* _GL_MATH_H */