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.
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],
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,
24 # error "signbit should be a macro"
27 ]gl_SIGNBIT_TEST_PROGRAM
29 [gl_cv_func_signbit=yes],
30 [gl_cv_func_signbit=no],
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" ;;
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
45 AC_CACHE_CHECK([for signbit compiler built-ins], [gl_cv_func_signbit_gcc],
51 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
52 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
53 __builtin_signbitf (x))
55 # error "signbit should be three compiler built-ins"
58 ]gl_SIGNBIT_TEST_PROGRAM
60 [gl_cv_func_signbit_gcc=yes],
61 [gl_cv_func_signbit_gcc=no],
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"
69 gl_cv_func_signbit_gcc="guessing no"
72 # If we don't know, assume the worst.
73 *) gl_cv_func_signbit_gcc="guessing no" ;;
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
81 REPLACE_SIGNBIT_USING_GCC=1
84 case "$gl_cv_func_signbit" in
87 dnl REPLACE_SIGNBIT=1 makes sure the signbit[fdl] functions get built.
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],
104 [[return copysignf (x, y) < 0;]])],
105 [gl_cv_func_copysignf_no_libm=yes],
106 [gl_cv_func_copysignf_no_libm=no])
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.])
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],
126 [[return copysign (x, y) < 0;]])],
127 [gl_cv_func_copysign_no_libm=yes],
128 [gl_cv_func_copysign_no_libm=no])
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.])
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],
148 [[return copysignl (x, y) < 0;]])],
149 [gl_cv_func_copysignl_no_libm=yes],
150 [gl_cv_func_copysignl_no_libm=no])
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.])
164 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [[
166 Needed because GCC 4 constant-folds __builtin_signbitl (literal)
167 but cannot constant-fold __builtin_signbitl (variable). */
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. */
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;
187 long double m0l = -p0l;
190 if (signbit (vf)) /* link check */
193 float plus_inf = 1.0f / p0f;
194 float minus_inf = -1.0f / p0f;
195 if (!(!signbit (255.0f)
198 && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
199 && !signbit (plus_inf)
200 && signbit (minus_inf)))
203 if (signbit (vd)) /* link check */
206 double plus_inf = 1.0 / p0d;
207 double minus_inf = -1.0 / p0d;
208 if (!(!signbit (255.0)
211 && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
212 && !signbit (plus_inf)
213 && signbit (minus_inf)))
216 if (signbit (vl)) /* link check */
219 long double plus_inf = 1.0L / p0l;
220 long double minus_inf = -1.0L / p0l;
221 if (signbit (255.0L))
223 if (!signbit (-255.0L))
227 if (!(memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l)))
229 if (signbit (plus_inf))
231 if (!signbit (minus_inf))
238 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
240 gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
243 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
245 gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
248 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
250 gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
253 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
255 AC_CACHE_CHECK([where to find the sign bit in a '$1'],
263 ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
264 typedef union { $1 value; unsigned int word[NWORDS]; }
266 static memory_float plus = { 1.0$3 };
267 static memory_float minus = { -1.0$3 };
272 FILE *fp = fopen ("conftest.out", "w");
275 /* Find the different bit. */
277 for (j = 0; j < NWORDS; j++)
279 unsigned int x = plus.word[j] ^ minus.word[j];
280 if ((x & (x - 1)) || (x && m))
282 /* More than one bit difference. */
283 fprintf (fp, "unknown");
296 fprintf (fp, "unknown");
300 /* Now m = plus.word[k] ^ ~minus.word[k]. */
301 if (plus.word[k] & ~minus.word[k])
303 /* Oh? The sign bit is set in the positive and cleared in the negative
305 fprintf (fp, "unknown");
312 fprintf (fp, "word %d bit %d", (int) k, (int) i);
313 if (fclose (fp) != 0)
318 [$2=`cat conftest.out`],
321 dnl When cross-compiling, we don't know. It depends on the
322 dnl ABI and compiler version. There are too many cases.
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'.])
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],
343 gl_FLOATTYPE_SIGNBIT_CODE([float], [f], [f])
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],
350 gl_FLOATTYPE_SIGNBIT_CODE([double], [d], [])
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],
357 gl_FLOATTYPE_SIGNBIT_CODE([long double], [l], [L])
360 AC_DEFUN([gl_FLOATTYPE_SIGNBIT_CODE],
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. */
372 for (i = 0; i < sizeof ($1); i++)
373 if (u.b[i] & (plus_one.b[i] ^ minus_one.b[i]))