2 dnl Copyright (C) 2007-2020 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])
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 musl systems.
35 *-musl*) gl_cv_func_signbit="guessing yes" ;;
36 # Guess yes on native Windows.
37 mingw*) gl_cv_func_signbit="guessing yes" ;;
38 # If we don't know, obey --enable-cross-guesses.
39 *) gl_cv_func_signbit="$gl_cross_guess_normal" ;;
43 dnl GCC >= 4.0 and clang provide three built-ins for signbit.
44 dnl They can be used without warnings, also in C++, regardless of <math.h>.
45 dnl But they may expand to calls to functions, which may or may not be in
47 AC_CACHE_CHECK([for signbit compiler built-ins],
48 [gl_cv_func_signbit_builtins],
52 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
54 (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
55 sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
56 __builtin_signbitf (x))
58 # error "signbit should be three compiler built-ins"
61 ]gl_SIGNBIT_TEST_PROGRAM
63 [gl_cv_func_signbit_builtins=yes],
64 [gl_cv_func_signbit_builtins=no],
66 # Guess yes on glibc systems.
67 *-gnu* | gnu*) gl_cv_func_signbit_builtins="guessing yes" ;;
68 # Guess yes on musl systems.
69 *-musl*) gl_cv_func_signbit_builtins="guessing yes" ;;
70 # Guess yes on mingw, no on MSVC.
71 mingw*) if test -n "$GCC"; then
72 gl_cv_func_signbit_builtins="guessing yes"
74 gl_cv_func_signbit_builtins="guessing no"
77 # If we don't know, obey --enable-cross-guesses.
78 *) gl_cv_func_signbit_builtins="$gl_cross_guess_normal" ;;
82 dnl Use the compiler built-ins whenever possible, because they are more
83 dnl efficient than the system library functions (if they exist).
84 case "$gl_cv_func_signbit_builtins" in
86 REPLACE_SIGNBIT_USING_BUILTINS=1
89 case "$gl_cv_func_signbit" in
92 dnl REPLACE_SIGNBIT=1 makes sure the signbit[fdl] functions get built.
98 dnl On Solaris 10, with CC in C++ mode, signbit is not available although
99 dnl is with cc in C mode. This cannot be worked around by defining
100 dnl _XOPEN_SOURCE=600, because the latter does not work in C++ mode on
101 dnl Solaris 11.0. Therefore use the replacement functions on Solaris.
107 if test $REPLACE_SIGNBIT = 1; then
108 gl_FLOAT_SIGN_LOCATION
109 gl_DOUBLE_SIGN_LOCATION
110 gl_LONG_DOUBLE_SIGN_LOCATION
111 if test "$gl_cv_cc_float_signbit" = unknown; then
112 dnl Test whether copysignf() is declared.
113 AC_CHECK_DECLS([copysignf], , , [[#include <math.h>]])
114 if test "$ac_cv_have_decl_copysignf" = yes; then
115 dnl Test whether copysignf() can be used without libm.
116 AC_CACHE_CHECK([whether copysignf can be used without linking with libm],
117 [gl_cv_func_copysignf_no_libm],
123 [[return copysignf (x, y) < 0;]])],
124 [gl_cv_func_copysignf_no_libm=yes],
125 [gl_cv_func_copysignf_no_libm=no])
127 if test $gl_cv_func_copysignf_no_libm = yes; then
128 AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], [1],
129 [Define if the copysignf function is declared in <math.h> and available in libc.])
133 if test "$gl_cv_cc_double_signbit" = unknown; then
134 dnl Test whether copysign() is declared.
135 AC_CHECK_DECLS([copysign], , , [[#include <math.h>]])
136 if test "$ac_cv_have_decl_copysign" = yes; then
137 dnl Test whether copysign() can be used without libm.
138 AC_CACHE_CHECK([whether copysign can be used without linking with libm],
139 [gl_cv_func_copysign_no_libm],
145 [[return copysign (x, y) < 0;]])],
146 [gl_cv_func_copysign_no_libm=yes],
147 [gl_cv_func_copysign_no_libm=no])
149 if test $gl_cv_func_copysign_no_libm = yes; then
150 AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], [1],
151 [Define if the copysign function is declared in <math.h> and available in libc.])
155 if test "$gl_cv_cc_long_double_signbit" = unknown; then
156 dnl Test whether copysignl() is declared.
157 AC_CHECK_DECLS([copysignl], , , [[#include <math.h>]])
158 if test "$ac_cv_have_decl_copysignl" = yes; then
159 dnl Test whether copysignl() can be used without libm.
160 AC_CACHE_CHECK([whether copysignl can be used without linking with libm],
161 [gl_cv_func_copysignl_no_libm],
167 [[return copysignl (x, y) < 0;]])],
168 [gl_cv_func_copysignl_no_libm=yes],
169 [gl_cv_func_copysignl_no_libm=no])
171 if test $gl_cv_func_copysignl_no_libm = yes; then
172 AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], [1],
173 [Define if the copysignl function is declared in <math.h> and available in libc.])
180 AC_DEFUN([gl_SIGNBIT_TEST_PROGRAM], [[
182 Needed because GCC 4 constant-folds __builtin_signbitl (literal)
183 but cannot constant-fold __builtin_signbitl (variable). */
189 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
190 So we use -p0f and -p0d instead. */
195 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
196 So we use another constant expression instead.
197 But that expression does not work on other platforms, such as when
198 cross-compiling to PowerPC on Mac OS X 10.5. */
199 long double p0l = 0.0L;
200 #if defined __hpux || defined __sgi
201 long double m0l = -LDBL_MIN * LDBL_MIN;
203 long double m0l = -p0l;
206 if (signbit (vf)) /* link check */
209 float plus_inf = 1.0f / p0f;
210 float minus_inf = -1.0f / p0f;
211 if (!(!signbit (255.0f)
214 && (memcmp (&m0f, &p0f, sizeof (float)) == 0 || signbit (m0f))
215 && !signbit (plus_inf)
216 && signbit (minus_inf)))
219 if (signbit (vd)) /* link check */
222 double plus_inf = 1.0 / p0d;
223 double minus_inf = -1.0 / p0d;
224 if (!(!signbit (255.0)
227 && (memcmp (&m0d, &p0d, sizeof (double)) == 0 || signbit (m0d))
228 && !signbit (plus_inf)
229 && signbit (minus_inf)))
232 if (signbit (vl)) /* link check */
235 long double plus_inf = 1.0L / p0l;
236 long double minus_inf = -1.0L / p0l;
237 if (signbit (255.0L))
239 if (!signbit (-255.0L))
243 if (!(memcmp (&m0l, &p0l, sizeof (long double)) == 0 || signbit (m0l)))
245 if (signbit (plus_inf))
247 if (!signbit (minus_inf))
254 AC_DEFUN([gl_FLOAT_SIGN_LOCATION],
256 gl_FLOATTYPE_SIGN_LOCATION([float], [gl_cv_cc_float_signbit], [f], [FLT])
259 AC_DEFUN([gl_DOUBLE_SIGN_LOCATION],
261 gl_FLOATTYPE_SIGN_LOCATION([double], [gl_cv_cc_double_signbit], [], [DBL])
264 AC_DEFUN([gl_LONG_DOUBLE_SIGN_LOCATION],
266 gl_FLOATTYPE_SIGN_LOCATION([long double], [gl_cv_cc_long_double_signbit], [L], [LDBL])
269 AC_DEFUN([gl_FLOATTYPE_SIGN_LOCATION],
271 AC_CACHE_CHECK([where to find the sign bit in a '$1'],
279 ((sizeof ($1) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
280 typedef union { $1 value; unsigned int word[NWORDS]; }
282 static memory_float plus = { 1.0$3 };
283 static memory_float minus = { -1.0$3 };
288 FILE *fp = fopen ("conftest.out", "w");
291 /* Find the different bit. */
293 for (j = 0; j < NWORDS; j++)
295 unsigned int x = plus.word[j] ^ minus.word[j];
296 if ((x & (x - 1)) || (x && m))
298 /* More than one bit difference. */
299 fprintf (fp, "unknown");
312 fprintf (fp, "unknown");
316 /* Now m = plus.word[k] ^ ~minus.word[k]. */
317 if (plus.word[k] & ~minus.word[k])
319 /* Oh? The sign bit is set in the positive and cleared in the negative
321 fprintf (fp, "unknown");
328 fprintf (fp, "word %d bit %d", (int) k, (int) i);
329 if (fclose (fp) != 0)
334 [$2=`cat conftest.out`],
337 dnl When cross-compiling, we don't know. It depends on the
338 dnl ABI and compiler version. There are too many cases.
345 word=`echo "$]$2[" | sed -e 's/word //' -e 's/ bit.*//'`
346 bit=`echo "$]$2[" | sed -e 's/word.*bit //'`
347 AC_DEFINE_UNQUOTED([$4][_SIGNBIT_WORD], [$word],
348 [Define as the word index where to find the sign of '$1'.])
349 AC_DEFINE_UNQUOTED([$4][_SIGNBIT_BIT], [$bit],
350 [Define as the bit index in the word where to find the sign of '$1'.])
355 # Expands to code that defines a function signbitf(float).
356 # It extracts the sign bit of a non-NaN value.
357 AC_DEFUN([gl_FLOAT_SIGNBIT_CODE],
359 gl_FLOATTYPE_SIGNBIT_CODE([float], [f], [f])
362 # Expands to code that defines a function signbitd(double).
363 # It extracts the sign bit of a non-NaN value.
364 AC_DEFUN([gl_DOUBLE_SIGNBIT_CODE],
366 gl_FLOATTYPE_SIGNBIT_CODE([double], [d], [])
369 # Expands to code that defines a function signbitl(long double).
370 # It extracts the sign bit of a non-NaN value.
371 AC_DEFUN([gl_LONG_DOUBLE_SIGNBIT_CODE],
373 gl_FLOATTYPE_SIGNBIT_CODE([long double], [l], [L])
376 AC_DEFUN([gl_FLOATTYPE_SIGNBIT_CODE],
381 typedef union { $1 f; unsigned char b[sizeof ($1)]; } float_union;
382 static float_union plus_one = { 1.0$3 }; /* unused bits are zero here */
383 static float_union minus_one = { -1.0$3 }; /* unused bits are zero here */
384 /* Compute the sign bit mask as the XOR of plus_one and minus_one. */
388 for (i = 0; i < sizeof ($1); i++)
389 if (u.b[i] & (plus_one.b[i] ^ minus_one.b[i]))