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