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