Exploit the __builtin_signbit GCC built-in.
[gnulib.git] / m4 / signbit.m4
1 # signbit.m4 serial 3
2 dnl Copyright (C) 2007-2008 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_TRY_RUN([
13 #include <math.h>
14 /* If signbit is defined as a function, don't use it, since calling it for
15    'float' or 'long double' arguments would involve conversions.
16    If signbit is not declared at all but exists as a library function, don't
17    use it, since the prototype may not match.
18    If signbit is not declared at all but exists as a compiler built-in, don't
19    use it, since it's preferable to use __builtin_signbit* (no warnings,
20    no conversions).  */
21 #ifndef signbit
22 # error "signbit should be a macro"
23 #endif
24 #include <string.h>
25 ]gl_SIGNBIT_TEST_PROGRAM
26 , [gl_cv_func_signbit=yes], [gl_cv_func_signbit=no],
27         [gl_cv_func_signbit="guessing no"])
28     ])
29   dnl GCC 4.0 and newer provides three built-ins for signbit.
30   dnl They can be used without warnings, also in C++, regardless of <math.h>.
31   dnl But they may expand to calls to functions, which may or may not be in
32   dnl libc.
33   AC_CACHE_CHECK([for signbit compiler built-ins], [gl_cv_func_signbit_gcc],
34     [
35       AC_TRY_RUN([
36 #if __GNUC__ >= 4
37 # define signbit(x) \
38    (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
39     sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
40     __builtin_signbitf (x))
41 #else
42 # error "signbit should be three compiler built-ins"
43 #endif
44 #include <string.h>
45 ]gl_SIGNBIT_TEST_PROGRAM
46 , [gl_cv_func_signbit_gcc=yes], [gl_cv_func_signbit_gcc=no],
47         [gl_cv_func_signbit_gcc="guessing no"])
48     ])
49   dnl Use the compiler built-ins whenever possible, because they are more
50   dnl efficient than the system library functions (if they exist).
51   if test "$gl_cv_func_signbit_gcc" = yes; then
52     REPLACE_SIGNBIT_USING_GCC=1
53   else
54     if test "$gl_cv_func_signbit" != yes; then
55       REPLACE_SIGNBIT=1
56       AC_LIBOBJ([signbitf])
57       AC_LIBOBJ([signbitd])
58       AC_LIBOBJ([signbitl])
59       gl_FLOAT_SIGN_LOCATION
60       gl_DOUBLE_SIGN_LOCATION
61       gl_LONG_DOUBLE_SIGN_LOCATION
62       if test "$gl_cv_cc_float_signbit" = unknown; then
63         dnl Test whether copysignf() is declared.
64         AC_CHECK_DECLS([copysignf], , , [#include <math.h>])
65         if test "$ac_cv_have_decl_copysignf" = yes; then
66           dnl Test whether copysignf() can be used without libm.
67           AC_CACHE_CHECK([whether copysignf can be used without linking with libm],
68             [gl_cv_func_copysignf_no_libm],
69             [
70               AC_TRY_LINK([#include <math.h>
71                            float x, y;],
72                           [return copysignf (x, y) < 0;],
73                 [gl_cv_func_copysignf_no_libm=yes],
74                 [gl_cv_func_copysignf_no_libm=no])
75             ])
76           if test $gl_cv_func_copysignf_no_libm = yes; then
77             AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], 1,
78               [Define if the copysignf function is declared in <math.h> and available in libc.])
79           fi
80         fi
81       fi
82       if test "$gl_cv_cc_double_signbit" = unknown; then
83         dnl Test whether copysign() is declared.
84         AC_CHECK_DECLS([copysign], , , [#include <math.h>])
85         if test "$ac_cv_have_decl_copysign" = yes; then
86           dnl Test whether copysign() can be used without libm.
87           AC_CACHE_CHECK([whether copysign can be used without linking with libm],
88             [gl_cv_func_copysign_no_libm],
89             [
90               AC_TRY_LINK([#include <math.h>
91                            double x, y;],
92                           [return copysign (x, y) < 0;],
93                 [gl_cv_func_copysign_no_libm=yes],
94                 [gl_cv_func_copysign_no_libm=no])
95             ])
96           if test $gl_cv_func_copysign_no_libm = yes; then
97             AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], 1,
98               [Define if the copysign function is declared in <math.h> and available in libc.])
99           fi
100         fi
101       fi
102       if test "$gl_cv_cc_long_double_signbit" = unknown; then
103         dnl Test whether copysignl() is declared.
104         AC_CHECK_DECLS([copysignl], , , [#include <math.h>])
105         if test "$ac_cv_have_decl_copysignl" = yes; then
106           dnl Test whether copysignl() can be used without libm.
107           AC_CACHE_CHECK([whether copysignl can be used without linking with libm],
108             [gl_cv_func_copysignl_no_libm],
109             [
110               AC_TRY_LINK([#include <math.h>
111                            long double x, y;],
112                           [return copysignl (x, y) < 0;],
113                 [gl_cv_func_copysignl_no_libm=yes],
114                 [gl_cv_func_copysignl_no_libm=no])
115             ])
116           if test $gl_cv_func_copysignl_no_libm = yes; then
117             AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], 1,
118               [Define if the copysignl function is declared in <math.h> and available in libc.])
119           fi
120         fi
121       fi
122     fi
123   fi
124 ])
125
126 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [
127 float p0f = 0.0f;
128 float m0f = -0.0f;
129 double p0d = 0.0;
130 double m0d = -0.0;
131 long double p0l = 0.0L;
132 long double m0l = -0.0L;
133 int main ()
134 {
135   {
136     float plus_inf = 1.0f / p0f;
137     float minus_inf = -1.0f / p0f;
138     if (!(!signbit (255.0f)
139           && signbit (-255.0f)
140           && !signbit (p0f)
141           && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
142           && !signbit (plus_inf)
143           && signbit (minus_inf)))
144       return 1;
145   }
146   {
147     double plus_inf = 1.0 / p0d;
148     double minus_inf = -1.0 / p0d;
149     if (!(!signbit (255.0)
150           && signbit (-255.0)
151           && !signbit (p0d)
152           && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
153           && !signbit (plus_inf)
154           && signbit (minus_inf)))
155       return 1;
156   }
157   {
158     long double plus_inf = 1.0L / p0l;
159     long double minus_inf = -1.0L / p0l;
160     if (!(!signbit (255.0L)
161           && signbit (-255.0L)
162           && !signbit (p0l)
163           && (memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l))
164           && !signbit (plus_inf)
165           && signbit (minus_inf)))
166       return 1;
167   }
168   return 0;
169 }
170 ])
171
172 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
173 [
174   gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
175 ])
176
177 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
178 [
179   gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
180 ])
181
182 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
183 [
184   gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
185 ])
186
187 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
188 [
189   AC_CACHE_CHECK([where to find the sign bit in a '$1'],
190     [$2],
191     [
192       AC_TRY_RUN([
193 #include <stddef.h>
194 #include <stdio.h>
195 #define NWORDS \
196   ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
197 typedef union { $1 value; unsigned int word[NWORDS]; }
198         memory_float;
199 static memory_float plus = { 1.0$3 };
200 static memory_float minus = { -1.0$3 };
201 int main ()
202 {
203   size_t j, k, i;
204   unsigned int m;
205   FILE *fp = fopen ("conftest.out", "w");
206   if (fp == NULL)
207     return 1;
208   /* Find the different bit.  */
209   k = 0; m = 0;
210   for (j = 0; j < NWORDS; j++)
211     {
212       unsigned int x = plus.word[j] ^ minus.word[j];
213       if ((x & (x - 1)) || (x && m))
214         {
215           /* More than one bit difference.  */
216           fprintf (fp, "unknown");
217           return 1;
218         }
219       if (x)
220         {
221           k = j;
222           m = x;
223         }
224     }
225   if (m == 0)
226     {
227       /* No difference.  */
228       fprintf (fp, "unknown");
229       return 1;
230     }
231   /* Now m = plus.word[k] ^ ~minus.word[k].  */
232   if (plus.word[k] & ~minus.word[k])
233     {
234       /* Oh? The sign bit is set in the positive and cleared in the negative
235          numbers?  */
236       fprintf (fp, "unknown");
237       return 1;
238     }
239   for (i = 0; ; i++)
240     if ((m >> i) & 1)
241       break;
242   fprintf (fp, "word %d bit %d", (int) k, (int) i);
243   return (fclose (fp) != 0);
244 }
245         ],
246         [$2=`cat conftest.out`],
247         [$2="unknown"],
248         [
249           dnl When cross-compiling, we don't know. It depends on the
250           dnl ABI and compiler version. There are too many cases.
251           $2="unknown"
252         ])
253       rm -f conftest.out
254     ])
255   case "$]$2[" in
256     word*bit*)
257       word=`echo "$]$2[" | sed -e 's/word //' -e 's/ bit.*//'`
258       bit=`echo "$]$2[" | sed -e 's/word.*bit //'`
259       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_WORD], [$word],
260         [Define as the word index where to find the sign of '$1'.])
261       AC_DEFINE_UNQUOTED([$4][_SIGNBIT_BIT], [$bit],
262         [Define as the bit index in the word where to find the sign of '$1'.])
263       ;;
264   esac
265 ])