Improve name: "count-one-bits" is better than "popcount".
[gnulib.git] / lib / math_.h
1 /* A GNU-like <math.h>.
2
3    Copyright (C) 2002-2003, 2007 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _GL_MATH_H
20
21 /* The include_next requires a split double-inclusion guard.  */
22 #@INCLUDE_NEXT@ @NEXT_MATH_H@
23
24 #ifndef _GL_MATH_H
25 #define _GL_MATH_H
26
27
28 /* The definition of GL_LINK_WARNING is copied here.  */
29
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35
36 /* Write x as
37      x = mantissa * 2^exp
38    where
39      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
40      If x is zero: mantissa = x, exp = 0.
41      If x is infinite or NaN: mantissa = x, exp unspecified.
42    Store exp and return mantissa.  */
43 #if @GNULIB_FREXP@
44 # if @REPLACE_FREXP@
45 #  define frexp rpl_frexp
46 extern double frexp (double x, int *exp);
47 # endif
48 #elif defined GNULIB_POSIXCHECK
49 # undef frexp
50 # define frexp(x,e) \
51     (GL_LINK_WARNING ("frexp is unportable - " \
52                       "use gnulib module frexp for portability"), \
53      frexp (x, e))
54 #endif
55
56
57 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
58 extern long double acosl (long double x);
59 #endif
60 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
61 # undef acosl
62 # define acosl(x) \
63     (GL_LINK_WARNING ("acosl is unportable - " \
64                       "use gnulib module mathl for portability"), \
65      acosl (x))
66 #endif
67
68 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
69 extern long double asinl (long double x);
70 #endif
71 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
72 # undef asinl
73 # define asinl(x) \
74     (GL_LINK_WARNING ("asinl is unportable - " \
75                       "use gnulib module mathl for portability"), \
76      asinl (x))
77 #endif
78
79 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
80 extern long double atanl (long double x);
81 #endif
82 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
83 # undef atanl
84 # define atanl(x) \
85     (GL_LINK_WARNING ("atanl is unportable - " \
86                       "use gnulib module mathl for portability"), \
87      atanl (x))
88 #endif
89
90 #if @GNULIB_MATHL@ || !@HAVE_DECL_CEILL@
91 extern long double ceill (long double x);
92 #endif
93 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
94 # undef ceill
95 # define ceill(x) \
96     (GL_LINK_WARNING ("ceill is unportable - " \
97                       "use gnulib module mathl for portability"), \
98      ceill (x))
99 #endif
100
101 #if @GNULIB_MATHL@ || !@HAVE_DECL_COSL@
102 extern long double cosl (long double x);
103 #endif
104 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
105 # undef cosl
106 # define cosl(x) \
107     (GL_LINK_WARNING ("cosl is unportable - " \
108                       "use gnulib module mathl for portability"), \
109      cosl (x))
110 #endif
111
112 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
113 extern long double expl (long double x);
114 #endif
115 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
116 # undef expl
117 # define expl(x) \
118     (GL_LINK_WARNING ("expl is unportable - " \
119                       "use gnulib module mathl for portability"), \
120      expl (x))
121 #endif
122
123 #if @GNULIB_MATHL@ || !@HAVE_DECL_FLOORL@
124 extern long double floorl (long double x);
125 #endif
126 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
127 # undef floorl
128 # define floorl(x) \
129     (GL_LINK_WARNING ("floorl is unportable - " \
130                       "use gnulib module mathl for portability"), \
131      floorl (x))
132 #endif
133
134 /* Write x as
135      x = mantissa * 2^exp
136    where
137      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
138      If x is zero: mantissa = x, exp = 0.
139      If x is infinite or NaN: mantissa = x, exp unspecified.
140    Store exp and return mantissa.  */
141 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
142 # define frexpl rpl_frexpl
143 #endif
144 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
145 extern long double frexpl (long double x, int *exp);
146 #endif
147 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
148 # undef frexpl
149 # define frexpl(x,e) \
150     (GL_LINK_WARNING ("frexpl is unportable - " \
151                       "use gnulib module frexpl for portability"), \
152      frexpl (x, e))
153 #endif
154
155 /* Return x * 2^exp.  */
156 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
157 # define ldexpl rpl_ldexpl
158 #endif
159 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
160 extern long double ldexpl (long double x, int exp);
161 #endif
162 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
163 # undef ldexpl
164 # define ldexpl(x,e) \
165     (GL_LINK_WARNING ("ldexpl is unportable - " \
166                       "use gnulib module ldexpl for portability"), \
167      ldexpl (x, e))
168 #endif
169
170 #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
171 extern long double logl (long double x);
172 #endif
173 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
174 # undef logl
175 # define logl(x) \
176     (GL_LINK_WARNING ("logl is unportable - " \
177                       "use gnulib module mathl for portability"), \
178      logl (x))
179 #endif
180
181 #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
182 extern long double sinl (long double x);
183 #endif
184 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
185 # undef sinl
186 # define sinl(x) \
187     (GL_LINK_WARNING ("sinl is unportable - " \
188                       "use gnulib module mathl for portability"), \
189      sinl (x))
190 #endif
191
192 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
193 extern long double sqrtl (long double x);
194 #endif
195 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
196 # undef sqrtl
197 # define sqrtl(x) \
198     (GL_LINK_WARNING ("sqrtl is unportable - " \
199                       "use gnulib module mathl for portability"), \
200      sqrtl (x))
201 #endif
202
203 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
204 extern long double tanl (long double x);
205 #endif
206 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
207 # undef tanl
208 # define tanl(x) \
209     (GL_LINK_WARNING ("tanl is unportable - " \
210                       "use gnulib module mathl for portability"), \
211      tanl (x))
212 #endif
213
214
215 #if @GNULIB_SIGNBIT@
216 # if @REPLACE_SIGNBIT@
217 #  undef signbit
218 extern int gl_signbitf (float arg);
219 extern int gl_signbitd (double arg);
220 extern int gl_signbitl (long double arg);
221 #  if __GNUC__ >= 2 && !__STRICT_ANSI__
222 #   if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
223 #    define gl_signbitf(arg) \
224        ({ union { float _value;                                         \
225                   unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
226                 } _m;                                                   \
227           _m._value = (arg);                                            \
228           (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1;          \
229         })
230 #   endif
231 #   if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT
232 #    define gl_signbitd(arg) \
233        ({ union { double _value;                                                \
234                   unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
235                 } _m;                                                   \
236           _m._value = (arg);                                            \
237           (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1;          \
238         })
239 #   endif
240 #   if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT
241 #    define gl_signbitl(arg) \
242        ({ union { long double _value;                                   \
243                   unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
244                 } _m;                                                   \
245           _m._value = (arg);                                            \
246           (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1;                \
247         })
248 #   endif
249 #  endif
250 #  define signbit(x) \
251    (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
252     sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
253     gl_signbitf (x))
254 # endif
255 #elif defined GNULIB_POSIXCHECK
256   /* How to override a macro?  */
257 #endif
258
259
260 #ifdef __cplusplus
261 }
262 #endif
263
264 #endif /* _GL_MATH_H */
265 #endif /* _GL_MATH_H */