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