signbit.m4 13 KB

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