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_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@
152 extern long double cosl (long double x
);
154 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
157 (GL_LINK_WARNING ("cosl is unportable - " \
158 "use gnulib module mathl for portability"), \
163 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
164 extern long double expl (long double x
);
166 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
169 (GL_LINK_WARNING ("expl is unportable - " \
170 "use gnulib module mathl for portability"), \
176 # if @REPLACE_FLOORF@
177 # define floorf rpl_floorf
178 extern float floorf (float x
);
180 #elif defined GNULIB_POSIXCHECK
183 (GL_LINK_WARNING ("floorf is unportable - " \
184 "use gnulib module floorf for portability"), \
189 # if @REPLACE_FLOORL@
190 # define floorl rpl_floorl
191 extern long double floorl (long double x
);
193 #elif defined GNULIB_POSIXCHECK
196 (GL_LINK_WARNING ("floorl is unportable - " \
197 "use gnulib module floorl for portability"), \
205 If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
206 If x is zero: mantissa = x, exp = 0.
207 If x is infinite or NaN: mantissa = x, exp unspecified.
208 Store exp in *EXPPTR and return mantissa. */
209 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
210 # define frexpl rpl_frexpl
212 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
213 extern long double frexpl (long double x
, int *expptr
);
215 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
217 # define frexpl(x,e) \
218 (GL_LINK_WARNING ("frexpl is unportable - " \
219 "use gnulib module frexpl for portability"), \
224 /* Return x * 2^exp. */
225 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
226 # define ldexpl rpl_ldexpl
228 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
229 extern long double ldexpl (long double x
, int exp
);
231 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
233 # define ldexpl(x,e) \
234 (GL_LINK_WARNING ("ldexpl is unportable - " \
235 "use gnulib module ldexpl for portability"), \
240 #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
241 extern long double logl (long double x
);
243 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
246 (GL_LINK_WARNING ("logl is unportable - " \
247 "use gnulib module mathl for portability"), \
253 # if @REPLACE_ROUNDF@
255 # define roundf rpl_roundf
256 extern float roundf (float x
);
258 #elif defined GNULIB_POSIXCHECK
261 (GL_LINK_WARNING ("roundf is unportable - " \
262 "use gnulib module roundf for portability"), \
269 # define round rpl_round
270 extern double round (double x
);
272 #elif defined GNULIB_POSIXCHECK
275 (GL_LINK_WARNING ("round is unportable - " \
276 "use gnulib module round for portability"), \
281 # if @REPLACE_ROUNDL@
283 # define roundl rpl_roundl
284 extern long double roundl (long double x
);
286 #elif defined GNULIB_POSIXCHECK
289 (GL_LINK_WARNING ("roundl is unportable - " \
290 "use gnulib module roundl for portability"), \
295 #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
296 extern long double sinl (long double x
);
298 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
301 (GL_LINK_WARNING ("sinl is unportable - " \
302 "use gnulib module mathl for portability"), \
307 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
308 extern long double sqrtl (long double x
);
310 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
313 (GL_LINK_WARNING ("sqrtl is unportable - " \
314 "use gnulib module mathl for portability"), \
319 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
320 extern long double tanl (long double x
);
322 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
325 (GL_LINK_WARNING ("tanl is unportable - " \
326 "use gnulib module mathl for portability"), \
332 # if !@HAVE_DECL_TRUNCF@
333 # define truncf rpl_truncf
334 extern float truncf (float x
);
336 #elif defined GNULIB_POSIXCHECK
339 (GL_LINK_WARNING ("truncf is unportable - " \
340 "use gnulib module truncf for portability"), \
345 # if !@HAVE_DECL_TRUNC@
346 # define trunc rpl_trunc
347 extern double trunc (double x
);
349 #elif defined GNULIB_POSIXCHECK
352 (GL_LINK_WARNING ("trunc is unportable - " \
353 "use gnulib module trunc for portability"), \
358 # if @REPLACE_TRUNCL@
360 # define truncl rpl_truncl
361 extern long double truncl (long double x
);
363 #elif defined GNULIB_POSIXCHECK
366 (GL_LINK_WARNING ("truncl is unportable - " \
367 "use gnulib module truncl for portability"), \
372 #if @GNULIB_ISFINITE@
373 # if @REPLACE_ISFINITE@
374 extern int gl_isfinitef (float x
);
375 extern int gl_isfinited (double x
);
376 extern int gl_isfinitel (long double x
);
378 # define isfinite(x) \
379 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
380 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
383 #elif defined GNULIB_POSIXCHECK
384 /* How to override a macro? */
390 extern int gl_isinff (float x
);
391 extern int gl_isinfd (double x
);
392 extern int gl_isinfl (long double x
);
395 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
396 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
399 #elif defined GNULIB_POSIXCHECK
400 /* How to override a macro? */
405 /* Test for NaN for 'float' numbers. */
407 /* The original <math.h> included above provides a declaration of isnan macro
408 or (older) isnanf function. */
411 /* GCC 4.0 and newer provides three built-ins for isnan. */
413 # define isnanf(x) __builtin_isnanf ((float)(x))
416 # define isnanf(x) isnan ((float)(x))
419 /* Test whether X is a NaN. */
421 # define isnanf rpl_isnanf
422 extern int isnanf (float x
);
427 /* Test for NaN for 'double' numbers.
428 This function is a gnulib extension, unlike isnan() which applied only
429 to 'double' numbers earlier but now is a type-generic macro. */
431 /* The original <math.h> included above provides a declaration of isnan macro. */
434 /* GCC 4.0 and newer provides three built-ins for isnan. */
436 # define isnand(x) __builtin_isnan ((double)(x))
439 # define isnand(x) isnan ((double)(x))
442 /* Test whether X is a NaN. */
444 # define isnand rpl_isnand
445 extern int isnand (double x
);
450 /* Test for NaN for 'long double' numbers. */
452 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
455 /* GCC 4.0 and newer provides three built-ins for isnan. */
457 # define isnanl(x) __builtin_isnanl ((long double)(x))
460 # define isnanl(x) isnan ((long double)(x))
463 /* Test whether X is a NaN. */
465 # define isnanl rpl_isnanl
466 extern int isnanl (long double x
);
470 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
473 /* We can't just use the isnanf macro (e.g.) as exposed by
474 isnanf.h (e.g.) here, because those may end up being macros
475 that recursively expand back to isnan. So use the gnulib
476 replacements for them directly. */
477 # if @HAVE_ISNANF@ && __GNUC__ >= 4
478 # define gl_isnan_f(x) __builtin_isnan ((float)(x))
480 extern int rpl_isnanf (float x
);
481 # define gl_isnan_f(x) rpl_isnanf (x)
483 # if @HAVE_ISNAND@ && __GNUC__ >= 4
484 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
486 extern int rpl_isnand (double x
);
487 # define gl_isnan_d(x) rpl_isnand (x)
489 # if @HAVE_ISNANL@ && __GNUC__ >= 4
490 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
492 extern int rpl_isnanl (long double x
);
493 # define gl_isnan_l(x) rpl_isnanl (x)
497 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
498 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
501 #elif defined GNULIB_POSIXCHECK
502 /* How to override a macro? */
507 # if @REPLACE_SIGNBIT_USING_GCC@
509 /* GCC 4.0 and newer provides three built-ins for signbit. */
510 # define signbit(x) \
511 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
512 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
513 __builtin_signbitf (x))
515 # if @REPLACE_SIGNBIT@
517 extern int gl_signbitf (float arg
);
518 extern int gl_signbitd (double arg
);
519 extern int gl_signbitl (long double arg
);
520 # if __GNUC__ >= 2 && !__STRICT_ANSI__
521 # if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT && !defined gl_signbitf
522 # define gl_signbitf_OPTIMIZED_MACRO
523 # define gl_signbitf(arg) \
524 ({ union { float _value; \
525 unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
528 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
531 # if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT && !defined gl_signbitd
532 # define gl_signbitd_OPTIMIZED_MACRO
533 # define gl_signbitd(arg) \
534 ({ union { double _value; \
535 unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
538 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
541 # if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT && !defined gl_signbitl
542 # define gl_signbitl_OPTIMIZED_MACRO
543 # define gl_signbitl(arg) \
544 ({ union { long double _value; \
545 unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
548 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
552 # define signbit(x) \
553 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
554 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
557 #elif defined GNULIB_POSIXCHECK
558 /* How to override a macro? */
566 #endif /* _GL_MATH_H */
567 #endif /* _GL_MATH_H */