Merge commit 'snapshot-start' into stable
[gnulib.git] / m4 / signbit.m4
1 # signbit.m4 serial 10
2 dnl Copyright (C) 2007-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_SIGNBIT],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_CACHE_CHECK([for signbit macro], [gl_cv_func_signbit],
11     [
12       AC_RUN_IFELSE(
13         [AC_LANG_SOURCE([[
14 #include <math.h>
15 /* If signbit is defined as a function, don't use it, since calling it for
16    'float' or 'long double' arguments would involve conversions.
17    If signbit is not declared at all but exists as a library function, don't
18    use it, since the prototype may not match.
19    If signbit is not declared at all but exists as a compiler built-in, don't
20    use it, since it's preferable to use __builtin_signbit* (no warnings,
21    no conversions).  */
22 #ifndef signbit
23 # error "signbit should be a macro"
24 #endif
25 #include <string.h>
26 ]gl_SIGNBIT_TEST_PROGRAM
27 ])],
28         [gl_cv_func_signbit=yes],
29         [gl_cv_func_signbit=no],
30         [gl_cv_func_signbit="guessing no"])
31     ])
32   dnl GCC 4.0 and newer provides three built-ins for signbit.
33   dnl They can be used without warnings, also in C++, regardless of <math.h>.
34   dnl But they may expand to calls to functions, which may or may not be in
35   dnl libc.
36   AC_CACHE_CHECK([for signbit compiler built-ins], [gl_cv_func_signbit_gcc],
37     [
38       AC_RUN_IFELSE(
39         [AC_LANG_SOURCE([[
40 #if __GNUC__ >= 4
41 # define signbit(x) \
42    (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
43     sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
44     __builtin_signbitf (x))
45 #else
46 # error "signbit should be three compiler built-ins"
47 #endif
48 #include <string.h>
49 ]gl_SIGNBIT_TEST_PROGRAM
50 ])],
51         [gl_cv_func_signbit_gcc=yes],
52         [gl_cv_func_signbit_gcc=no],
53         [gl_cv_func_signbit_gcc="guessing no"])
54     ])
55   dnl Use the compiler built-ins whenever possible, because they are more
56   dnl efficient than the system library functions (if they exist).
57   if test "$gl_cv_func_signbit_gcc" = yes; then
58     REPLACE_SIGNBIT_USING_GCC=1
59   else
60     if test "$gl_cv_func_signbit" != yes; then
61       dnl REPLACE_SIGNBIT=1 makes sure the signbit[fdl] functions get built.
62       REPLACE_SIGNBIT=1
63       gl_FLOAT_SIGN_LOCATION
64       gl_DOUBLE_SIGN_LOCATION
65       gl_LONG_DOUBLE_SIGN_LOCATION
66       if test "$gl_cv_cc_float_signbit" = unknown; then
67         dnl Test whether copysignf() is declared.
68         AC_CHECK_DECLS([copysignf], , , [#include <math.h>])
69         if test "$ac_cv_have_decl_copysignf" = yes; then
70           dnl Test whether copysignf() can be used without libm.
71           AC_CACHE_CHECK([whether copysignf can be used without linking with libm],
72             [gl_cv_func_copysignf_no_libm],
73             [
74               AC_LINK_IFELSE(
75                 [AC_LANG_PROGRAM(
76                    [[#include <math.h>
77                      float x, y;]],
78                    [[return copysignf (x, y) < 0;]])],
79                 [gl_cv_func_copysignf_no_libm=yes],
80                 [gl_cv_func_copysignf_no_libm=no])
81             ])
82           if test $gl_cv_func_copysignf_no_libm = yes; then
83             AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], [1],
84               [Define if the copysignf function is declared in <math.h> and available in libc.])
85           fi
86         fi
87       fi
88       if test "$gl_cv_cc_double_signbit" = unknown; then
89         dnl Test whether copysign() is declared.
90         AC_CHECK_DECLS([copysign], , , [#include <math.h>])
91         if test "$ac_cv_have_decl_copysign" = yes; then
92           dnl Test whether copysign() can be used without libm.
93           AC_CACHE_CHECK([whether copysign can be used without linking with libm],
94             [gl_cv_func_copysign_no_libm],
95             [
96               AC_LINK_IFELSE(
97                 [AC_LANG_PROGRAM(
98                    [[#include <math.h>
99                      double x, y;]],
100                    [[return copysign (x, y) < 0;]])],
101                 [gl_cv_func_copysign_no_libm=yes],
102                 [gl_cv_func_copysign_no_libm=no])
103             ])
104           if test $gl_cv_func_copysign_no_libm = yes; then
105             AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], [1],
106               [Define if the copysign function is declared in <math.h> and available in libc.])
107           fi
108         fi
109       fi
110       if test "$gl_cv_cc_long_double_signbit" = unknown; then
111         dnl Test whether copysignl() is declared.
112         AC_CHECK_DECLS([copysignl], , , [#include <math.h>])
113         if test "$ac_cv_have_decl_copysignl" = yes; then
114           dnl Test whether copysignl() can be used without libm.
115           AC_CACHE_CHECK([whether copysignl can be used without linking with libm],
116             [gl_cv_func_copysignl_no_libm],
117             [
118               AC_LINK_IFELSE(
119                 [AC_LANG_PROGRAM(
120                    [[#include <math.h>
121                      long double x, y;]],
122                    [[return copysignl (x, y) < 0;]])],
123                 [gl_cv_func_copysignl_no_libm=yes],
124                 [gl_cv_func_copysignl_no_libm=no])
125             ])
126           if test $gl_cv_func_copysignl_no_libm = yes; then
127             AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], [1],
128               [Define if the copysignl function is declared in <math.h> and available in libc.])
129           fi
130         fi
131       fi
132     fi
133   fi
134 ])
135
136 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [[
137 /* Global variables.
138    Needed because GCC 4 constant-folds __builtin_signbitl (literal)
139    but cannot constant-fold            __builtin_signbitl (variable).  */
140 float vf;
141 double vd;
142 long double vl;
143 int main ()
144 {
145 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
146    So we use -p0f and -p0d instead.  */
147 float p0f = 0.0f;
148 float m0f = -p0f;
149 double p0d = 0.0;
150 double m0d = -p0d;
151 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
152    So we use another constant expression instead.
153    But that expression does not work on other platforms, such as when
154    cross-compiling to PowerPC on MacOS X 10.5.  */
155 long double p0l = 0.0L;
156 #if defined __hpux || defined __sgi
157 long double m0l = -LDBL_MIN * LDBL_MIN;
158 #else
159 long double m0l = -p0l;
160 #endif
161   int result = 0;
162   if (signbit (vf)) /* link check */
163     vf++;
164   {
165     float plus_inf = 1.0f / p0f;
166     float minus_inf = -1.0f / p0f;
167     if (!(!signbit (255.0f)
168           && signbit (-255.0f)
169           && !signbit (p0f)
170           && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
171           && !signbit (plus_inf)
172           && signbit (minus_inf)))
173       result |= 1;
174   }
175   if (signbit (vd)) /* link check */
176     vd++;
177   {
178     double plus_inf = 1.0 / p0d;
179     double minus_inf = -1.0 / p0d;
180     if (!(!signbit (255.0)
181           && signbit (-255.0)
182           && !signbit (p0d)
183           && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
184           && !signbit (plus_inf)
185           && signbit (minus_inf)))
186       result |= 2;
187   }
188   if (signbit (vl)) /* link check */
189     vl++;
190   {
191     long double plus_inf = 1.0L / p0l;
192     long double minus_inf = -1.0L / p0l;
193     if (signbit (255.0L))
194       result |= 4;
195     if (!signbit (-255.0L))
196       result |= 4;
197     if (signbit (p0l))
198       result |= 8;
199     if (!(memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l)))
200       result |= 16;
201     if (signbit (plus_inf))
202       result |= 32;
203     if (!signbit (minus_inf))
204       result |= 64;
205   }
206   return result;
207 }
208 ]])
209
210 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
211 [
212   gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
213 ])
214
215 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
216 [
217   gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
218 ])
219
220 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
221 [
222   gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
223 ])
224
225 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
226 [
227   AC_CACHE_CHECK([where to find the sign bit in a '$1'],
228     [$2],
229     [
230       AC_RUN_IFELSE(
231         [AC_LANG_SOURCE([[
232 #include <stddef.h>
233 #include <stdio.h>
234 #define NWORDS \
235   ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
236 typedef union { $1 value; unsigned int word[NWORDS]; }
237         memory_float;
238 static memory_float plus = { 1.0$3 };
239 static memory_float minus = { -1.0$3 };
240 int main ()
241 {
242   size_t j, k, i;
243   unsigned int m;
244   FILE *fp = fopen ("conftest.out", "w");
245   if (fp == NULL)
246     return 1;
247   /* Find the different bit.  */
248   k = 0; m = 0;
249   for (j = 0; j < NWORDS; j++)
250     {
251       unsigned int x = plus.word[j] ^ minus.word[j];
252       if ((x & (x - 1)) || (x && m))
253         {
254           /* More than one bit difference.  */
255           fprintf (fp, "unknown");
256           return 2;
257         }
258       if (x)
259         {
260           k = j;
261           m = x;
262         }
263     }
264   if (m == 0)
265     {
266       /* No difference.  */
267       fprintf (fp, "unknown");
268       return 3;
269     }
270   /* Now m = plus.word[k] ^ ~minus.word[k].  */
271   if (plus.word[k] & ~minus.word[k])
272     {
273       /* Oh? The sign bit is set in the positive and cleared in the negative
274          numbers?  */
275       fprintf (fp, "unknown");
276       return 4;
277     }
278   for (i = 0; ; i++)
279     if ((m >> i) & 1)
280       break;
281   fprintf (fp, "word %d bit %d", (int) k, (int) i);
282   if (fclose (fp) != 0)
283     return 5;
284   return 0;
285 }
286         ]])],
287         [$2=`cat conftest.out`],
288         [$2="unknown"],
289         [
290           dnl When cross-compiling, we don't know. It depends on the
291           dnl ABI and compiler version. There are too many cases.
292           $2="unknown"
293         ])
294       rm -f conftest.out
295     ])
296   case "$]$2[" in
297     word*bit*)
298       word=`echo "$]$2[" | sed -e 's/word //' -e 's/ bit.*//'`
299       bit=`echo "$]$2[" | sed -e 's/word.*bit //'`
300       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_WORD], [$word],
301         [Define as the word index where to find the sign of '$1'.])
302       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_BIT], [$bit],
303         [Define as the bit index in the word where to find the sign of '$1'.])
304       ;;
305   esac
306 ])
307
308 # Expands to code that defines a function signbitf(float).
309 # It extracts the sign bit of a non-NaN value.
310 AC_DEFUN([gl_FLOAT_SIGNBIT_CODE],
311 [
312   gl_FLOATTYPE_SIGNBIT_CODE([float], [f], [f])
313 ])
314
315 # Expands to code that defines a function signbitd(double).
316 # It extracts the sign bit of a non-NaN value.
317 AC_DEFUN([gl_DOUBLE_SIGNBIT_CODE],
318 [
319   gl_FLOATTYPE_SIGNBIT_CODE([double], [d], [])
320 ])
321
322 # Expands to code that defines a function signbitl(long double).
323 # It extracts the sign bit of a non-NaN value.
324 AC_DEFUN([gl_LONG_DOUBLE_SIGNBIT_CODE],
325 [
326   gl_FLOATTYPE_SIGNBIT_CODE([long double], [l], [L])
327 ])
328
329 AC_DEFUN([gl_FLOATTYPE_SIGNBIT_CODE],
330 [[
331 static int
332 signbit$2 ($1 value)
333 {
334   typedef union { $1 f; unsigned char b[sizeof ($1)]; } float_union;
335   static float_union plus_one = { 1.0$3 };   /* unused bits are zero here */
336   static float_union minus_one = { -1.0$3 }; /* unused bits are zero here */
337   /* Compute the sign bit mask as the XOR of plus_one and minus_one.  */
338   float_union u;
339   unsigned int i;
340   u.f = value;
341   for (i = 0; i < sizeof ($1); i++)
342     if (u.b[i] & (plus_one.b[i] ^ minus_one.b[i]))
343       return 1;
344   return 0;
345 }
346 ]])