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