Add isnan module.
[gnulib.git] / lib / math.in.h
1 /* A GNU-like <math.h>.
2
3    Copyright (C) 2002-2003, 2007-2008 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18 #ifndef _GL_MATH_H
19
20 /* The include_next requires a split double-inclusion guard.  */
21 #@INCLUDE_NEXT@ @NEXT_MATH_H@
22
23 #ifndef _GL_MATH_H
24 #define _GL_MATH_H
25
26
27 /* The definition of GL_LINK_WARNING is copied here.  */
28
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34
35 /* POSIX allows platforms that don't support NAN.  But all major
36    machines in the past 15 years have supported something close to
37    IEEE NaN, so we define this unconditionally.  We also must define
38    it on platforms like Solaris 10, where NAN is present but defined
39    as a function pointer rather than a floating point constant.  */
40 #if !defined NAN || @REPLACE_NAN@
41 # undef NAN
42   /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
43 # ifdef __DECC
44 static float
45 _NaN ()
46 {
47   static float zero = 0.0f;
48   return zero / zero;
49 }
50 #  define NAN (_NaN())
51 # else
52 #  define NAN (0.0f / 0.0f)
53 # endif
54 #endif
55
56 /* Solaris 10 defines HUGE_VAL, but as a function pointer rather
57    than a floating point constant.  */
58 #if @REPLACE_HUGE_VAL@
59 # undef HUGE_VAL
60 # define HUGE_VAL (1.0 / 0.0)
61 #endif
62
63 /* Write x as
64      x = mantissa * 2^exp
65    where
66      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
67      If x is zero: mantissa = x, exp = 0.
68      If x is infinite or NaN: mantissa = x, exp unspecified.
69    Store exp in *EXPPTR and return mantissa.  */
70 #if @GNULIB_FREXP@
71 # if @REPLACE_FREXP@
72 #  define frexp rpl_frexp
73 extern double frexp (double x, int *expptr);
74 # endif
75 #elif defined GNULIB_POSIXCHECK
76 # undef frexp
77 # define frexp(x,e) \
78     (GL_LINK_WARNING ("frexp is unportable - " \
79                       "use gnulib module frexp for portability"), \
80      frexp (x, e))
81 #endif
82
83
84 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
85 extern long double acosl (long double x);
86 #endif
87 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
88 # undef acosl
89 # define acosl(x) \
90     (GL_LINK_WARNING ("acosl is unportable - " \
91                       "use gnulib module mathl for portability"), \
92      acosl (x))
93 #endif
94
95
96 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
97 extern long double asinl (long double x);
98 #endif
99 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
100 # undef asinl
101 # define asinl(x) \
102     (GL_LINK_WARNING ("asinl is unportable - " \
103                       "use gnulib module mathl for portability"), \
104      asinl (x))
105 #endif
106
107
108 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
109 extern long double atanl (long double x);
110 #endif
111 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
112 # undef atanl
113 # define atanl(x) \
114     (GL_LINK_WARNING ("atanl is unportable - " \
115                       "use gnulib module mathl for portability"), \
116      atanl (x))
117 #endif
118
119
120 #if @GNULIB_CEILF@
121 # if @REPLACE_CEILF@
122 #  define ceilf rpl_ceilf
123 extern float ceilf (float x);
124 # endif
125 #elif defined GNULIB_POSIXCHECK
126 # undef ceilf
127 # define ceilf(x) \
128     (GL_LINK_WARNING ("ceilf is unportable - " \
129                       "use gnulib module ceilf for portability"), \
130      ceilf (x))
131 #endif
132
133 #if @GNULIB_CEILL@
134 # if @REPLACE_CEILL@
135 #  define ceill rpl_ceill
136 extern long double ceill (long double x);
137 # endif
138 #elif defined GNULIB_POSIXCHECK
139 # undef ceill
140 # define ceill(x) \
141     (GL_LINK_WARNING ("ceill is unportable - " \
142                       "use gnulib module ceill for portability"), \
143      ceill (x))
144 #endif
145
146
147 #if @GNULIB_MATHL@ || !@HAVE_DECL_COSL@
148 extern long double cosl (long double x);
149 #endif
150 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
151 # undef cosl
152 # define cosl(x) \
153     (GL_LINK_WARNING ("cosl is unportable - " \
154                       "use gnulib module mathl for portability"), \
155      cosl (x))
156 #endif
157
158
159 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
160 extern long double expl (long double x);
161 #endif
162 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
163 # undef expl
164 # define expl(x) \
165     (GL_LINK_WARNING ("expl is unportable - " \
166                       "use gnulib module mathl for portability"), \
167      expl (x))
168 #endif
169
170
171 #if @GNULIB_FLOORF@
172 # if @REPLACE_FLOORF@
173 #  define floorf rpl_floorf
174 extern float floorf (float x);
175 # endif
176 #elif defined GNULIB_POSIXCHECK
177 # undef floorf
178 # define floorf(x) \
179     (GL_LINK_WARNING ("floorf is unportable - " \
180                       "use gnulib module floorf for portability"), \
181      floorf (x))
182 #endif
183
184 #if @GNULIB_FLOORL@
185 # if @REPLACE_FLOORL@
186 #  define floorl rpl_floorl
187 extern long double floorl (long double x);
188 # endif
189 #elif defined GNULIB_POSIXCHECK
190 # undef floorl
191 # define floorl(x) \
192     (GL_LINK_WARNING ("floorl is unportable - " \
193                       "use gnulib module floorl for portability"), \
194      floorl (x))
195 #endif
196
197
198 /* Write x as
199      x = mantissa * 2^exp
200    where
201      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
202      If x is zero: mantissa = x, exp = 0.
203      If x is infinite or NaN: mantissa = x, exp unspecified.
204    Store exp in *EXPPTR and return mantissa.  */
205 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
206 # define frexpl rpl_frexpl
207 #endif
208 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
209 extern long double frexpl (long double x, int *expptr);
210 #endif
211 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
212 # undef frexpl
213 # define frexpl(x,e) \
214     (GL_LINK_WARNING ("frexpl is unportable - " \
215                       "use gnulib module frexpl for portability"), \
216      frexpl (x, e))
217 #endif
218
219
220 /* Return x * 2^exp.  */
221 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
222 # define ldexpl rpl_ldexpl
223 #endif
224 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
225 extern long double ldexpl (long double x, int exp);
226 #endif
227 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
228 # undef ldexpl
229 # define ldexpl(x,e) \
230     (GL_LINK_WARNING ("ldexpl is unportable - " \
231                       "use gnulib module ldexpl for portability"), \
232      ldexpl (x, e))
233 #endif
234
235
236 #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
237 extern long double logl (long double x);
238 #endif
239 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
240 # undef logl
241 # define logl(x) \
242     (GL_LINK_WARNING ("logl is unportable - " \
243                       "use gnulib module mathl for portability"), \
244      logl (x))
245 #endif
246
247
248 #if @GNULIB_ROUNDF@
249 # if @REPLACE_ROUNDF@
250 #  undef roundf
251 #  define roundf rpl_roundf
252 extern float roundf (float x);
253 # endif
254 #elif defined GNULIB_POSIXCHECK
255 # undef roundf
256 # define roundf(x) \
257     (GL_LINK_WARNING ("roundf is unportable - " \
258                       "use gnulib module roundf for portability"), \
259      roundf (x))
260 #endif
261
262 #if @GNULIB_ROUND@
263 # if @REPLACE_ROUND@
264 #  undef round
265 #  define round rpl_round
266 extern double round (double x);
267 # endif
268 #elif defined GNULIB_POSIXCHECK
269 # undef round
270 # define round(x) \
271     (GL_LINK_WARNING ("round is unportable - " \
272                       "use gnulib module round for portability"), \
273      round (x))
274 #endif
275
276 #if @GNULIB_ROUNDL@
277 # if @REPLACE_ROUNDL@
278 #  undef roundl
279 #  define roundl rpl_roundl
280 extern long double roundl (long double x);
281 # endif
282 #elif defined GNULIB_POSIXCHECK
283 # undef roundl
284 # define roundl(x) \
285     (GL_LINK_WARNING ("roundl is unportable - " \
286                       "use gnulib module roundl for portability"), \
287      roundl (x))
288 #endif
289
290
291 #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
292 extern long double sinl (long double x);
293 #endif
294 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
295 # undef sinl
296 # define sinl(x) \
297     (GL_LINK_WARNING ("sinl is unportable - " \
298                       "use gnulib module mathl for portability"), \
299      sinl (x))
300 #endif
301
302
303 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
304 extern long double sqrtl (long double x);
305 #endif
306 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
307 # undef sqrtl
308 # define sqrtl(x) \
309     (GL_LINK_WARNING ("sqrtl is unportable - " \
310                       "use gnulib module mathl for portability"), \
311      sqrtl (x))
312 #endif
313
314
315 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
316 extern long double tanl (long double x);
317 #endif
318 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
319 # undef tanl
320 # define tanl(x) \
321     (GL_LINK_WARNING ("tanl is unportable - " \
322                       "use gnulib module mathl for portability"), \
323      tanl (x))
324 #endif
325
326
327 #if @GNULIB_TRUNCF@
328 # if !@HAVE_DECL_TRUNCF@
329 #  define truncf rpl_truncf
330 extern float truncf (float x);
331 # endif
332 #elif defined GNULIB_POSIXCHECK
333 # undef truncf
334 # define truncf(x) \
335     (GL_LINK_WARNING ("truncf is unportable - " \
336                       "use gnulib module truncf for portability"), \
337      truncf (x))
338 #endif
339
340 #if @GNULIB_TRUNC@
341 # if !@HAVE_DECL_TRUNC@
342 #  define trunc rpl_trunc
343 extern double trunc (double x);
344 # endif
345 #elif defined GNULIB_POSIXCHECK
346 # undef trunc
347 # define trunc(x) \
348     (GL_LINK_WARNING ("trunc is unportable - " \
349                       "use gnulib module trunc for portability"), \
350      trunc (x))
351 #endif
352
353 #if @GNULIB_TRUNCL@
354 # if @REPLACE_TRUNCL@
355 #  undef truncl
356 #  define truncl rpl_truncl
357 extern long double truncl (long double x);
358 # endif
359 #elif defined GNULIB_POSIXCHECK
360 # undef truncl
361 # define truncl(x) \
362     (GL_LINK_WARNING ("truncl is unportable - " \
363                       "use gnulib module truncl for portability"), \
364      truncl (x))
365 #endif
366
367
368 #if @GNULIB_ISFINITE@
369 # if @REPLACE_ISFINITE@
370 extern int gl_isfinitef (float x);
371 extern int gl_isfinited (double x);
372 extern int gl_isfinitel (long double x);
373 #  undef isfinite
374 #  define isfinite(x) \
375    (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
376     sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
377     gl_isfinitef (x))
378 # endif
379 #elif defined GNULIB_POSIXCHECK
380   /* How to override a macro?  */
381 #endif
382
383
384 #if @GNULIB_ISNAN@
385 # if @REPLACE_ISNAN@
386 /* We can't just use the isnanf macro (e.g.) as exposed by
387    isnanf.h (e.g.) here, because those may end up being macros
388    that recursively expand back to isnan.  So use the gnulib
389    replacements for them directly. */
390 #  if HAVE_ISNANF && __GNUC__ >= 4
391 #   define gl_isnan_f(x) __builtin_isnan ((float)(x))
392 #  else
393 extern int rpl_isnanf (float x);
394 #   define gl_isnan_f(x) rpl_isnanf (x)
395 #  endif
396 #  if HAVE_ISNAND && __GNUC__ >= 4
397 #   define gl_isnan_d(x) __builtin_isnan ((double)(x))
398 #  else
399 extern int rpl_isnand (double x);
400 #   define gl_isnan_d(x) rpl_isnand (x)
401 #  endif
402 #  if HAVE_ISNANL && __GNUC__ >= 4
403 #   define gl_isnan_l(x) __builtin_isnan ((long double)(x))
404 #  else
405 extern int rpl_isnanl (long double x);
406 #   define gl_isnan_l(x) rpl_isnanl (x)
407 #  endif
408 #  undef isnan
409 #  define isnan(x) \
410    (sizeof (x) == sizeof (long double) ? gl_isnan_l (x) : \
411     sizeof (x) == sizeof (double) ? gl_isnan_d (x) : \
412     gl_isnan_f (x))
413 # endif
414 #elif defined GNULIB_POSIXCHECK
415   /* How to override a macro?  */
416 #endif
417
418
419 #if @GNULIB_SIGNBIT@
420 # if @REPLACE_SIGNBIT_USING_GCC@
421 #  undef signbit
422    /* GCC 4.0 and newer provides three built-ins for signbit.  */
423 #  define signbit(x) \
424    (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
425     sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
426     __builtin_signbitf (x))
427 # endif
428 # if @REPLACE_SIGNBIT@
429 #  undef signbit
430 extern int gl_signbitf (float arg);
431 extern int gl_signbitd (double arg);
432 extern int gl_signbitl (long double arg);
433 #  if __GNUC__ >= 2 && !__STRICT_ANSI__
434 #   if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
435 #    define gl_signbitf(arg) \
436        ({ union { float _value;                                         \
437                   unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
438                 } _m;                                                   \
439           _m._value = (arg);                                            \
440           (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1;          \
441         })
442 #   endif
443 #   if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT
444 #    define gl_signbitd(arg) \
445        ({ union { double _value;                                                \
446                   unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
447                 } _m;                                                   \
448           _m._value = (arg);                                            \
449           (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1;          \
450         })
451 #   endif
452 #   if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT
453 #    define gl_signbitl(arg) \
454        ({ union { long double _value;                                   \
455                   unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
456                 } _m;                                                   \
457           _m._value = (arg);                                            \
458           (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1;                \
459         })
460 #   endif
461 #  endif
462 #  define signbit(x) \
463    (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
464     sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
465     gl_signbitf (x))
466 # endif
467 #elif defined GNULIB_POSIXCHECK
468   /* How to override a macro?  */
469 #endif
470
471
472 #ifdef __cplusplus
473 }
474 #endif
475
476 #endif /* _GL_MATH_H */
477 #endif /* _GL_MATH_H */