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