Merge branch 'upstream' into stable
[gnulib.git] / m4 / signbit.m4
1 # signbit.m4 serial 7
2 dnl Copyright (C) 2007-2010 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       REPLACE_SIGNBIT=1
62       AC_LIBOBJ([signbitf])
63       AC_LIBOBJ([signbitd])
64       AC_LIBOBJ([signbitl])
65       gl_FLOAT_SIGN_LOCATION
66       gl_DOUBLE_SIGN_LOCATION
67       gl_LONG_DOUBLE_SIGN_LOCATION
68       if test "$gl_cv_cc_float_signbit" = unknown; then
69         dnl Test whether copysignf() is declared.
70         AC_CHECK_DECLS([copysignf], , , [#include <math.h>])
71         if test "$ac_cv_have_decl_copysignf" = yes; then
72           dnl Test whether copysignf() can be used without libm.
73           AC_CACHE_CHECK([whether copysignf can be used without linking with libm],
74             [gl_cv_func_copysignf_no_libm],
75             [
76               AC_LINK_IFELSE(
77                 [AC_LANG_PROGRAM(
78                    [[#include <math.h>
79                      float x, y;]],
80                    [[return copysignf (x, y) < 0;]])],
81                 [gl_cv_func_copysignf_no_libm=yes],
82                 [gl_cv_func_copysignf_no_libm=no])
83             ])
84           if test $gl_cv_func_copysignf_no_libm = yes; then
85             AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], [1],
86               [Define if the copysignf function is declared in <math.h> and available in libc.])
87           fi
88         fi
89       fi
90       if test "$gl_cv_cc_double_signbit" = unknown; then
91         dnl Test whether copysign() is declared.
92         AC_CHECK_DECLS([copysign], , , [#include <math.h>])
93         if test "$ac_cv_have_decl_copysign" = yes; then
94           dnl Test whether copysign() can be used without libm.
95           AC_CACHE_CHECK([whether copysign can be used without linking with libm],
96             [gl_cv_func_copysign_no_libm],
97             [
98               AC_LINK_IFELSE(
99                 [AC_LANG_PROGRAM(
100                    [[#include <math.h>
101                      double x, y;]],
102                    [[return copysign (x, y) < 0;]])],
103                 [gl_cv_func_copysign_no_libm=yes],
104                 [gl_cv_func_copysign_no_libm=no])
105             ])
106           if test $gl_cv_func_copysign_no_libm = yes; then
107             AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], [1],
108               [Define if the copysign function is declared in <math.h> and available in libc.])
109           fi
110         fi
111       fi
112       if test "$gl_cv_cc_long_double_signbit" = unknown; then
113         dnl Test whether copysignl() is declared.
114         AC_CHECK_DECLS([copysignl], , , [#include <math.h>])
115         if test "$ac_cv_have_decl_copysignl" = yes; then
116           dnl Test whether copysignl() can be used without libm.
117           AC_CACHE_CHECK([whether copysignl can be used without linking with libm],
118             [gl_cv_func_copysignl_no_libm],
119             [
120               AC_LINK_IFELSE(
121                 [AC_LANG_PROGRAM(
122                    [[#include <math.h>
123                      long double x, y;]],
124                    [[return copysignl (x, y) < 0;]])],
125                 [gl_cv_func_copysignl_no_libm=yes],
126                 [gl_cv_func_copysignl_no_libm=no])
127             ])
128           if test $gl_cv_func_copysignl_no_libm = yes; then
129             AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], [1],
130               [Define if the copysignl function is declared in <math.h> and available in libc.])
131           fi
132         fi
133       fi
134     fi
135   fi
136 ])
137
138 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [[
139 /* Global variables.
140    Needed because GCC 4 constant-folds __builtin_signbitl (literal)
141    but cannot constant-fold            __builtin_signbitl (variable).  */
142 float vf;
143 double vd;
144 long double vl;
145 int main ()
146 {
147 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
148    So we use -p0f and -p0d instead.  */
149 float p0f = 0.0f;
150 float m0f = -p0f;
151 double p0d = 0.0;
152 double m0d = -p0d;
153 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
154    So we use another constant expression instead.
155    But that expression does not work on other platforms, such as when
156    cross-compiling to PowerPC on MacOS X 10.5.  */
157 long double p0l = 0.0L;
158 #if defined __hpux || defined __sgi
159 long double m0l = -LDBL_MIN * LDBL_MIN;
160 #else
161 long double m0l = -p0l;
162 #endif
163   if (signbit (vf))
164     vf++;
165   {
166     float plus_inf = 1.0f / p0f;
167     float minus_inf = -1.0f / p0f;
168     if (!(!signbit (255.0f)
169           && signbit (-255.0f)
170           && !signbit (p0f)
171           && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
172           && !signbit (plus_inf)
173           && signbit (minus_inf)))
174       return 1;
175   }
176   if (signbit (vd))
177     vd++;
178   {
179     double plus_inf = 1.0 / p0d;
180     double minus_inf = -1.0 / p0d;
181     if (!(!signbit (255.0)
182           && signbit (-255.0)
183           && !signbit (p0d)
184           && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
185           && !signbit (plus_inf)
186           && signbit (minus_inf)))
187       return 1;
188   }
189   if (signbit (vl))
190     vl++;
191   {
192     long double plus_inf = 1.0L / p0l;
193     long double minus_inf = -1.0L / p0l;
194     if (!(!signbit (255.0L)
195           && signbit (-255.0L)
196           && !signbit (p0l)
197           && (memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l))
198           && !signbit (plus_inf)
199           && signbit (minus_inf)))
200       return 1;
201   }
202   return 0;
203 }
204 ]])
205
206 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
207 [
208   gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
209 ])
210
211 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
212 [
213   gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
214 ])
215
216 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
217 [
218   gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
219 ])
220
221 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
222 [
223   AC_CACHE_CHECK([where to find the sign bit in a '$1'],
224     [$2],
225     [
226       AC_RUN_IFELSE(
227         [AC_LANG_SOURCE([[
228 #include <stddef.h>
229 #include <stdio.h>
230 #define NWORDS \
231   ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
232 typedef union { $1 value; unsigned int word[NWORDS]; }
233         memory_float;
234 static memory_float plus = { 1.0$3 };
235 static memory_float minus = { -1.0$3 };
236 int main ()
237 {
238   size_t j, k, i;
239   unsigned int m;
240   FILE *fp = fopen ("conftest.out", "w");
241   if (fp == NULL)
242     return 1;
243   /* Find the different bit.  */
244   k = 0; m = 0;
245   for (j = 0; j < NWORDS; j++)
246     {
247       unsigned int x = plus.word[j] ^ minus.word[j];
248       if ((x & (x - 1)) || (x && m))
249         {
250           /* More than one bit difference.  */
251           fprintf (fp, "unknown");
252           return 1;
253         }
254       if (x)
255         {
256           k = j;
257           m = x;
258         }
259     }
260   if (m == 0)
261     {
262       /* No difference.  */
263       fprintf (fp, "unknown");
264       return 1;
265     }
266   /* Now m = plus.word[k] ^ ~minus.word[k].  */
267   if (plus.word[k] & ~minus.word[k])
268     {
269       /* Oh? The sign bit is set in the positive and cleared in the negative
270          numbers?  */
271       fprintf (fp, "unknown");
272       return 1;
273     }
274   for (i = 0; ; i++)
275     if ((m >> i) & 1)
276       break;
277   fprintf (fp, "word %d bit %d", (int) k, (int) i);
278   return (fclose (fp) != 0);
279 }
280         ]])],
281         [$2=`cat conftest.out`],
282         [$2="unknown"],
283         [
284           dnl When cross-compiling, we don't know. It depends on the
285           dnl ABI and compiler version. There are too many cases.
286           $2="unknown"
287         ])
288       rm -f conftest.out
289     ])
290   case "$]$2[" in
291     word*bit*)
292       word=`echo "$]$2[" | sed -e 's/word //' -e 's/ bit.*//'`
293       bit=`echo "$]$2[" | sed -e 's/word.*bit //'`
294       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_WORD], [$word],
295         [Define as the word index where to find the sign of '$1'.])
296       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_BIT], [$bit],
297         [Define as the bit index in the word where to find the sign of '$1'.])
298       ;;
299   esac
300 ])