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/>. */
18 #ifndef _GL_MATH_H
20 #if __GNUC__ >= 3
21 @PRAGMA_SYSTEM_HEADER@
22 #endif
24 /* The include_next requires a split double-inclusion guard. */
25 #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_MATH_H@
27 #ifndef _GL_MATH_H
28 #define _GL_MATH_H
31 /* The definition of GL_LINK_WARNING is copied here. */
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
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@
45 # undef NAN
46 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
47 # ifdef __DECC
48 static float
49 _NaN ()
50 {
51 static float zero = 0.0f;
52 return zero / zero;
53 }
54 # define NAN (_NaN())
55 # else
56 # define NAN (0.0f / 0.0f)
57 # endif
58 #endif
60 /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
61 than a floating point constant. */
62 #if @REPLACE_HUGE_VAL@
63 # undef HUGE_VAL
64 # define HUGE_VAL (1.0 / 0.0)
65 #endif
67 /* Write x as
68 x = mantissa * 2^exp
69 where
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. */
74 #if @GNULIB_FREXP@
75 # if @REPLACE_FREXP@
76 # define frexp rpl_frexp
77 extern double frexp (double x, int *expptr);
78 # endif
79 #elif defined GNULIB_POSIXCHECK
80 # undef frexp
81 # define frexp(x,e) \
82 (GL_LINK_WARNING ("frexp is unportable - " \
83 "use gnulib module frexp for portability"), \
84 frexp (x, e))
85 #endif
88 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
89 extern long double acosl (long double x);
90 #endif
91 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
92 # undef acosl
93 # define acosl(x) \
94 (GL_LINK_WARNING ("acosl is unportable - " \
95 "use gnulib module mathl for portability"), \
96 acosl (x))
97 #endif
100 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
101 extern long double asinl (long double x);
102 #endif
103 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
104 # undef asinl
105 # define asinl(x) \
106 (GL_LINK_WARNING ("asinl is unportable - " \
107 "use gnulib module mathl for portability"), \
108 asinl (x))
109 #endif
112 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
113 extern long double atanl (long double x);
114 #endif
115 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
116 # undef atanl
117 # define atanl(x) \
118 (GL_LINK_WARNING ("atanl is unportable - " \
119 "use gnulib module mathl for portability"), \
120 atanl (x))
121 #endif
124 #if @GNULIB_CEILF@
125 # if @REPLACE_CEILF@
126 # define ceilf rpl_ceilf
127 extern float ceilf (float x);
128 # endif
129 #elif defined GNULIB_POSIXCHECK
130 # undef ceilf
131 # define ceilf(x) \
132 (GL_LINK_WARNING ("ceilf is unportable - " \
133 "use gnulib module ceilf for portability"), \
134 ceilf (x))
135 #endif
137 #if @GNULIB_CEILL@
138 # if @REPLACE_CEILL@
139 # define ceill rpl_ceill
140 extern long double ceill (long double x);
141 # endif
142 #elif defined GNULIB_POSIXCHECK
143 # undef ceill
144 # define ceill(x) \
145 (GL_LINK_WARNING ("ceill is unportable - " \
146 "use gnulib module ceill for portability"), \
147 ceill (x))
148 #endif
151 #if @GNULIB_MATHL@ || !@HAVE_DECL_COSL@
152 extern long double cosl (long double x);
153 #endif
154 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
155 # undef cosl
156 # define cosl(x) \
157 (GL_LINK_WARNING ("cosl is unportable - " \
158 "use gnulib module mathl for portability"), \
159 cosl (x))
160 #endif
163 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
164 extern long double expl (long double x);
165 #endif
166 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
167 # undef expl
168 # define expl(x) \
169 (GL_LINK_WARNING ("expl is unportable - " \
170 "use gnulib module mathl for portability"), \
171 expl (x))
172 #endif
175 #if @GNULIB_FLOORF@
176 # if @REPLACE_FLOORF@
177 # define floorf rpl_floorf
178 extern float floorf (float x);
179 # endif
180 #elif defined GNULIB_POSIXCHECK
181 # undef floorf
182 # define floorf(x) \
183 (GL_LINK_WARNING ("floorf is unportable - " \
184 "use gnulib module floorf for portability"), \
185 floorf (x))
186 #endif
188 #if @GNULIB_FLOORL@
189 # if @REPLACE_FLOORL@
190 # define floorl rpl_floorl
191 extern long double floorl (long double x);
192 # endif
193 #elif defined GNULIB_POSIXCHECK
194 # undef floorl
195 # define floorl(x) \
196 (GL_LINK_WARNING ("floorl is unportable - " \
197 "use gnulib module floorl for portability"), \
198 floorl (x))
199 #endif
202 /* Write x as
203 x = mantissa * 2^exp
204 where
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
211 #endif
212 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
213 extern long double frexpl (long double x, int *expptr);
214 #endif
215 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
216 # undef frexpl
217 # define frexpl(x,e) \
218 (GL_LINK_WARNING ("frexpl is unportable - " \
219 "use gnulib module frexpl for portability"), \
220 frexpl (x, e))
221 #endif
224 /* Return x * 2^exp. */
225 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
226 # define ldexpl rpl_ldexpl
227 #endif
228 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
229 extern long double ldexpl (long double x, int exp);
230 #endif
231 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
232 # undef ldexpl
233 # define ldexpl(x,e) \
234 (GL_LINK_WARNING ("ldexpl is unportable - " \
235 "use gnulib module ldexpl for portability"), \
236 ldexpl (x, e))
237 #endif
240 #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
241 extern long double logl (long double x);
242 #endif
243 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
244 # undef logl
245 # define logl(x) \
246 (GL_LINK_WARNING ("logl is unportable - " \
247 "use gnulib module mathl for portability"), \
248 logl (x))
249 #endif
252 #if @GNULIB_ROUNDF@
253 # if @REPLACE_ROUNDF@
254 # undef roundf
255 # define roundf rpl_roundf
256 extern float roundf (float x);
257 # endif
258 #elif defined GNULIB_POSIXCHECK
259 # undef roundf
260 # define roundf(x) \
261 (GL_LINK_WARNING ("roundf is unportable - " \
262 "use gnulib module roundf for portability"), \
263 roundf (x))
264 #endif
266 #if @GNULIB_ROUND@
267 # if @REPLACE_ROUND@
268 # undef round
269 # define round rpl_round
270 extern double round (double x);
271 # endif
272 #elif defined GNULIB_POSIXCHECK
273 # undef round
274 # define round(x) \
275 (GL_LINK_WARNING ("round is unportable - " \
276 "use gnulib module round for portability"), \
277 round (x))
278 #endif
280 #if @GNULIB_ROUNDL@
281 # if @REPLACE_ROUNDL@
282 # undef roundl
283 # define roundl rpl_roundl
284 extern long double roundl (long double x);
285 # endif
286 #elif defined GNULIB_POSIXCHECK
287 # undef roundl
288 # define roundl(x) \
289 (GL_LINK_WARNING ("roundl is unportable - " \
290 "use gnulib module roundl for portability"), \
291 roundl (x))
292 #endif
295 #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
296 extern long double sinl (long double x);
297 #endif
298 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
299 # undef sinl
300 # define sinl(x) \
301 (GL_LINK_WARNING ("sinl is unportable - " \
302 "use gnulib module mathl for portability"), \
303 sinl (x))
304 #endif
307 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
308 extern long double sqrtl (long double x);
309 #endif
310 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
311 # undef sqrtl
312 # define sqrtl(x) \
313 (GL_LINK_WARNING ("sqrtl is unportable - " \
314 "use gnulib module mathl for portability"), \
315 sqrtl (x))
316 #endif
319 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
320 extern long double tanl (long double x);
321 #endif
322 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
323 # undef tanl
324 # define tanl(x) \
325 (GL_LINK_WARNING ("tanl is unportable - " \
326 "use gnulib module mathl for portability"), \
327 tanl (x))
328 #endif
331 #if @GNULIB_TRUNCF@
332 # if !@HAVE_DECL_TRUNCF@
333 # define truncf rpl_truncf
334 extern float truncf (float x);
335 # endif
336 #elif defined GNULIB_POSIXCHECK
337 # undef truncf
338 # define truncf(x) \
339 (GL_LINK_WARNING ("truncf is unportable - " \
340 "use gnulib module truncf for portability"), \
341 truncf (x))
342 #endif
344 #if @GNULIB_TRUNC@
345 # if !@HAVE_DECL_TRUNC@
346 # define trunc rpl_trunc
347 extern double trunc (double x);
348 # endif
349 #elif defined GNULIB_POSIXCHECK
350 # undef trunc
351 # define trunc(x) \
352 (GL_LINK_WARNING ("trunc is unportable - " \
353 "use gnulib module trunc for portability"), \
354 trunc (x))
355 #endif
357 #if @GNULIB_TRUNCL@
358 # if @REPLACE_TRUNCL@
359 # undef truncl
360 # define truncl rpl_truncl
361 extern long double truncl (long double x);
362 # endif
363 #elif defined GNULIB_POSIXCHECK
364 # undef truncl
365 # define truncl(x) \
366 (GL_LINK_WARNING ("truncl is unportable - " \
367 "use gnulib module truncl for portability"), \
368 truncl (x))
369 #endif
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);
377 # undef isfinite
378 # define isfinite(x) \
379 (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
380 sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
381 gl_isfinitef (x))
382 # endif
383 #elif defined GNULIB_POSIXCHECK
384 /* How to override a macro? */
385 #endif
388 #if @GNULIB_ISINF@
389 # if @REPLACE_ISINF@
390 extern int gl_isinff (float x);
391 extern int gl_isinfd (double x);
392 extern int gl_isinfl (long double x);
393 # undef isinf
394 # define isinf(x) \
395 (sizeof (x) == sizeof (long double) ? gl_isinfl (x) : \
396 sizeof (x) == sizeof (double) ? gl_isinfd (x) : \
397 gl_isinff (x))
398 # endif
399 #elif defined GNULIB_POSIXCHECK
400 /* How to override a macro? */
401 #endif
404 #if @GNULIB_ISNANF@
405 /* Test for NaN for 'float' numbers. */
406 # if @HAVE_ISNANF@
407 /* The original <math.h> included above provides a declaration of isnan macro
408 or (older) isnanf function. */
409 # include <math.h>
410 # if __GNUC__ >= 4
411 /* GCC 4.0 and newer provides three built-ins for isnan. */
412 # undef isnanf
413 # define isnanf(x) __builtin_isnanf ((float)(x))
414 # elif defined isnan
415 # undef isnanf
416 # define isnanf(x) isnan ((float)(x))
417 # endif
418 # else
419 /* Test whether X is a NaN. */
420 # undef isnanf
421 # define isnanf rpl_isnanf
422 extern int isnanf (float x);
423 # endif
424 #endif
426 #if @GNULIB_ISNAND@
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. */
430 # if @HAVE_ISNAND@
431 /* The original <math.h> included above provides a declaration of isnan macro. */
432 # include <math.h>
433 # if __GNUC__ >= 4
434 /* GCC 4.0 and newer provides three built-ins for isnan. */
435 # undef isnand
436 # define isnand(x) __builtin_isnan ((double)(x))
437 # else
438 # undef isnand
439 # define isnand(x) isnan ((double)(x))
440 # endif
441 # else
442 /* Test whether X is a NaN. */
443 # undef isnand
444 # define isnand rpl_isnand
445 extern int isnand (double x);
446 # endif
447 #endif
449 #if @GNULIB_ISNANL@
450 /* Test for NaN for 'long double' numbers. */
451 # if @HAVE_ISNANL@
452 /* The original <math.h> included above provides a declaration of isnan macro or (older) isnanl function. */
453 # include <math.h>
454 # if __GNUC__ >= 4
455 /* GCC 4.0 and newer provides three built-ins for isnan. */
456 # undef isnanl
457 # define isnanl(x) __builtin_isnanl ((long double)(x))
458 # elif defined isnan
459 # undef isnanl
460 # define isnanl(x) isnan ((long double)(x))
461 # endif
462 # else
463 /* Test whether X is a NaN. */
464 # undef isnanl
465 # define isnanl rpl_isnanl
466 extern int isnanl (long double x);
467 # endif
468 #endif
470 /* This must come *after* the snippets for GNULIB_ISNANF and GNULIB_ISNANL! */
471 #if @GNULIB_ISNAN@
472 # if @REPLACE_ISNAN@
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))
479 # else
480 extern int rpl_isnanf (float x);
481 # define gl_isnan_f(x) rpl_isnanf (x)
482 # endif
483 # if @HAVE_ISNAND@ && __GNUC__ >= 4
484 # define gl_isnan_d(x) __builtin_isnan ((double)(x))
485 # else
486 extern int rpl_isnand (double x);
487 # define gl_isnan_d(x) rpl_isnand (x)
488 # endif
489 # if @HAVE_ISNANL@ && __GNUC__ >= 4
490 # define gl_isnan_l(x) __builtin_isnan ((long double)(x))
491 # else
492 extern int rpl_isnanl (long double x);
493 # define gl_isnan_l(x) rpl_isnanl (x)
494 # endif
495 # undef isnan
496 # define isnan(x) \
497 (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
498 sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
499 gl_isnan_f (x))
500 # endif
501 #elif defined GNULIB_POSIXCHECK
502 /* How to override a macro? */
503 #endif
506 #if @GNULIB_SIGNBIT@
507 # if @REPLACE_SIGNBIT_USING_GCC@
508 # undef signbit
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))
514 # endif
515 # if @REPLACE_SIGNBIT@
516 # undef 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)]; \
526 } _m; \
527 _m._value = (arg); \
528 (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1; \
529 })
530 # endif
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)]; \
536 } _m; \
537 _m._value = (arg); \
538 (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1; \
539 })
540 # endif
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)]; \
546 } _m; \
547 _m._value = (arg); \
548 (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1; \
549 })
550 # endif
551 # endif
552 # define signbit(x) \
553 (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
554 sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
555 gl_signbitf (x))
556 # endif
557 #elif defined GNULIB_POSIXCHECK
558 /* How to override a macro? */
559 #endif
562 #ifdef __cplusplus
563 }
564 #endif
566 #endif /* _GL_MATH_H */
567 #endif /* _GL_MATH_H */