isnanl.m4 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # isnanl.m4 serial 22
  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_FUNC_ISNANL],
  7. [
  8. AC_REQUIRE([gl_MATH_H_DEFAULTS])
  9. ISNANL_LIBM=
  10. gl_HAVE_ISNANL_NO_LIBM
  11. if test $gl_cv_func_isnanl_no_libm = no; then
  12. gl_HAVE_ISNANL_IN_LIBM
  13. if test $gl_cv_func_isnanl_in_libm = yes; then
  14. ISNANL_LIBM=-lm
  15. fi
  16. fi
  17. dnl The variable gl_func_isnanl set here is used by isnan.m4.
  18. if test $gl_cv_func_isnanl_no_libm = yes \
  19. || test $gl_cv_func_isnanl_in_libm = yes; then
  20. save_LIBS="$LIBS"
  21. LIBS="$LIBS $ISNANL_LIBM"
  22. gl_FUNC_ISNANL_WORKS
  23. LIBS="$save_LIBS"
  24. case "$gl_cv_func_isnanl_works" in
  25. *yes) gl_func_isnanl=yes ;;
  26. *) gl_func_isnanl=no; ISNANL_LIBM= ;;
  27. esac
  28. else
  29. gl_func_isnanl=no
  30. fi
  31. if test $gl_func_isnanl != yes; then
  32. HAVE_ISNANL=0
  33. fi
  34. AC_SUBST([ISNANL_LIBM])
  35. ])
  36. AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
  37. [
  38. gl_HAVE_ISNANL_NO_LIBM
  39. gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
  40. if test $gl_func_isnanl_no_libm = yes; then
  41. gl_FUNC_ISNANL_WORKS
  42. case "$gl_cv_func_isnanl_works" in
  43. *yes) ;;
  44. *) gl_func_isnanl_no_libm=no ;;
  45. esac
  46. fi
  47. if test $gl_func_isnanl_no_libm = yes; then
  48. AC_DEFINE([HAVE_ISNANL_IN_LIBC], [1],
  49. [Define if the isnan(long double) function is available in libc.])
  50. fi
  51. ])
  52. dnl Prerequisites of replacement isnanl definition. It does not need -lm.
  53. AC_DEFUN([gl_PREREQ_ISNANL],
  54. [
  55. gl_LONG_DOUBLE_EXPONENT_LOCATION
  56. AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
  57. ])
  58. dnl Test whether isnanl() can be used without libm.
  59. AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
  60. [
  61. AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
  62. [gl_cv_func_isnanl_no_libm],
  63. [
  64. AC_LINK_IFELSE(
  65. [AC_LANG_PROGRAM(
  66. [[#include <math.h>
  67. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  68. # undef isnanl
  69. # define isnanl(x) __builtin_isnan ((long double)(x))
  70. #elif defined isnan
  71. # undef isnanl
  72. # define isnanl(x) isnan ((long double)(x))
  73. #endif
  74. long double x;]],
  75. [[return isnanl (x);]])],
  76. [gl_cv_func_isnanl_no_libm=yes],
  77. [gl_cv_func_isnanl_no_libm=no])
  78. ])
  79. ])
  80. dnl Test whether isnanl() can be used with libm.
  81. AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
  82. [
  83. AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
  84. [gl_cv_func_isnanl_in_libm],
  85. [
  86. save_LIBS="$LIBS"
  87. LIBS="$LIBS -lm"
  88. AC_LINK_IFELSE(
  89. [AC_LANG_PROGRAM(
  90. [[#include <math.h>
  91. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  92. # undef isnanl
  93. # define isnanl(x) __builtin_isnan ((long double)(x))
  94. #elif defined isnan
  95. # undef isnanl
  96. # define isnanl(x) isnan ((long double)(x))
  97. #endif
  98. long double x;]],
  99. [[return isnanl (x);]])],
  100. [gl_cv_func_isnanl_in_libm=yes],
  101. [gl_cv_func_isnanl_in_libm=no])
  102. LIBS="$save_LIBS"
  103. ])
  104. ])
  105. dnl Test whether isnanl() recognizes all canonical numbers which are neither
  106. dnl finite nor infinite.
  107. AC_DEFUN([gl_FUNC_ISNANL_WORKS],
  108. [
  109. AC_REQUIRE([AC_PROG_CC])
  110. AC_REQUIRE([gl_BIGENDIAN])
  111. AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
  112. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  113. AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
  114. [
  115. AC_RUN_IFELSE(
  116. [AC_LANG_SOURCE([[
  117. #include <float.h>
  118. #include <limits.h>
  119. #include <math.h>
  120. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  121. # undef isnanl
  122. # define isnanl(x) __builtin_isnan ((long double)(x))
  123. #elif defined isnan
  124. # undef isnanl
  125. # define isnanl(x) isnan ((long double)(x))
  126. #endif
  127. #define NWORDS \
  128. ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  129. typedef union { unsigned int word[NWORDS]; long double value; }
  130. memory_long_double;
  131. /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
  132. runtime type conversion. */
  133. #ifdef __sgi
  134. static long double NaNl ()
  135. {
  136. double zero = 0.0;
  137. return zero / zero;
  138. }
  139. #else
  140. # define NaNl() (0.0L / 0.0L)
  141. #endif
  142. int main ()
  143. {
  144. int result = 0;
  145. if (!isnanl (NaNl ()))
  146. result |= 1;
  147. {
  148. memory_long_double m;
  149. unsigned int i;
  150. /* The isnanl function should be immune against changes in the sign bit and
  151. in the mantissa bits. The xor operation twiddles a bit that can only be
  152. a sign bit or a mantissa bit (since the exponent never extends to
  153. bit 31). */
  154. m.value = NaNl ();
  155. m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
  156. for (i = 0; i < NWORDS; i++)
  157. m.word[i] |= 1;
  158. if (!isnanl (m.value))
  159. result |= 1;
  160. }
  161. #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
  162. /* Representation of an 80-bit 'long double' as an initializer for a sequence
  163. of 'unsigned int' words. */
  164. # ifdef WORDS_BIGENDIAN
  165. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  166. { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
  167. ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
  168. (unsigned int) (mantlo) << 16 \
  169. }
  170. # else
  171. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  172. { mantlo, manthi, exponent }
  173. # endif
  174. { /* Quiet NaN. */
  175. static memory_long_double x =
  176. { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
  177. if (!isnanl (x.value))
  178. result |= 2;
  179. }
  180. {
  181. /* Signalling NaN. */
  182. static memory_long_double x =
  183. { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
  184. if (!isnanl (x.value))
  185. result |= 2;
  186. }
  187. /* isnanl should return something even for noncanonical values. */
  188. { /* Pseudo-NaN. */
  189. static memory_long_double x =
  190. { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
  191. if (isnanl (x.value) && !isnanl (x.value))
  192. result |= 4;
  193. }
  194. { /* Pseudo-Infinity. */
  195. static memory_long_double x =
  196. { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
  197. if (isnanl (x.value) && !isnanl (x.value))
  198. result |= 8;
  199. }
  200. { /* Pseudo-Zero. */
  201. static memory_long_double x =
  202. { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
  203. if (isnanl (x.value) && !isnanl (x.value))
  204. result |= 16;
  205. }
  206. { /* Unnormalized number. */
  207. static memory_long_double x =
  208. { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
  209. if (isnanl (x.value) && !isnanl (x.value))
  210. result |= 32;
  211. }
  212. { /* Pseudo-Denormal. */
  213. static memory_long_double x =
  214. { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
  215. if (isnanl (x.value) && !isnanl (x.value))
  216. result |= 64;
  217. }
  218. #endif
  219. return result;
  220. }]])],
  221. [gl_cv_func_isnanl_works=yes],
  222. [gl_cv_func_isnanl_works=no],
  223. [case "$host_os" in
  224. mingw*) # Guess yes on mingw, no on MSVC.
  225. AC_EGREP_CPP([Known], [
  226. #ifdef __MINGW32__
  227. Known
  228. #endif
  229. ],
  230. [gl_cv_func_isnanl_works="guessing yes"],
  231. [gl_cv_func_isnanl_works="guessing no"])
  232. ;;
  233. *) gl_cv_func_isnanl_works="guessing yes" ;;
  234. esac
  235. ])
  236. ])
  237. ])