isnanl.m4 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # isnanl.m4 serial 17
  2. dnl Copyright (C) 2007-2014 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
  68. # undef isnanl
  69. # define isnanl(x) __builtin_isnanl ((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
  92. # undef isnanl
  93. # define isnanl(x) __builtin_isnanl ((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 numbers which are neither finite nor
  106. dnl infinite. This test fails e.g. on NetBSD/i386 and on glibc/ia64.
  107. dnl Also, the GCC >= 4.0 built-in __builtin_isnanl does not pass the tests
  108. dnl - for pseudo-denormals on i686 and x86_64,
  109. dnl - for pseudo-zeroes, unnormalized numbers, and pseudo-denormals on ia64.
  110. AC_DEFUN([gl_FUNC_ISNANL_WORKS],
  111. [
  112. AC_REQUIRE([AC_PROG_CC])
  113. AC_REQUIRE([gl_BIGENDIAN])
  114. AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
  115. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  116. AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
  117. [
  118. AC_RUN_IFELSE(
  119. [AC_LANG_SOURCE([[
  120. #include <float.h>
  121. #include <limits.h>
  122. #include <math.h>
  123. #if __GNUC__ >= 4
  124. # undef isnanl
  125. # define isnanl(x) __builtin_isnanl ((long double)(x))
  126. #elif defined isnan
  127. # undef isnanl
  128. # define isnanl(x) isnan ((long double)(x))
  129. #endif
  130. #define NWORDS \
  131. ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  132. typedef union { unsigned int word[NWORDS]; long double value; }
  133. memory_long_double;
  134. /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
  135. runtime type conversion. */
  136. #ifdef __sgi
  137. static long double NaNl ()
  138. {
  139. double zero = 0.0;
  140. return zero / zero;
  141. }
  142. #else
  143. # define NaNl() (0.0L / 0.0L)
  144. #endif
  145. int main ()
  146. {
  147. int result = 0;
  148. if (!isnanl (NaNl ()))
  149. result |= 1;
  150. {
  151. memory_long_double m;
  152. unsigned int i;
  153. /* The isnanl function should be immune against changes in the sign bit and
  154. in the mantissa bits. The xor operation twiddles a bit that can only be
  155. a sign bit or a mantissa bit (since the exponent never extends to
  156. bit 31). */
  157. m.value = NaNl ();
  158. m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
  159. for (i = 0; i < NWORDS; i++)
  160. m.word[i] |= 1;
  161. if (!isnanl (m.value))
  162. result |= 1;
  163. }
  164. #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
  165. /* Representation of an 80-bit 'long double' as an initializer for a sequence
  166. of 'unsigned int' words. */
  167. # ifdef WORDS_BIGENDIAN
  168. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  169. { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
  170. ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
  171. (unsigned int) (mantlo) << 16 \
  172. }
  173. # else
  174. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  175. { mantlo, manthi, exponent }
  176. # endif
  177. { /* Quiet NaN. */
  178. static memory_long_double x =
  179. { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
  180. if (!isnanl (x.value))
  181. result |= 2;
  182. }
  183. {
  184. /* Signalling NaN. */
  185. static memory_long_double x =
  186. { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
  187. if (!isnanl (x.value))
  188. result |= 2;
  189. }
  190. /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
  191. Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
  192. Intel IA-64 Architecture Software Developer's Manual, Volume 1:
  193. Application Architecture.
  194. Table 5-2 "Floating-Point Register Encodings"
  195. Figure 5-6 "Memory to Floating-Point Register Data Translation"
  196. */
  197. { /* Pseudo-NaN. */
  198. static memory_long_double x =
  199. { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
  200. if (!isnanl (x.value))
  201. result |= 4;
  202. }
  203. { /* Pseudo-Infinity. */
  204. static memory_long_double x =
  205. { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
  206. if (!isnanl (x.value))
  207. result |= 8;
  208. }
  209. { /* Pseudo-Zero. */
  210. static memory_long_double x =
  211. { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
  212. if (!isnanl (x.value))
  213. result |= 16;
  214. }
  215. { /* Unnormalized number. */
  216. static memory_long_double x =
  217. { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
  218. if (!isnanl (x.value))
  219. result |= 32;
  220. }
  221. { /* Pseudo-Denormal. */
  222. static memory_long_double x =
  223. { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
  224. if (!isnanl (x.value))
  225. result |= 64;
  226. }
  227. #endif
  228. return result;
  229. }]])],
  230. [gl_cv_func_isnanl_works=yes],
  231. [gl_cv_func_isnanl_works=no],
  232. [case "$host_cpu" in
  233. # Guess no on ia64, x86_64, i386.
  234. ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
  235. *)
  236. case "$host_os" in
  237. netbsd*) gl_cv_func_isnanl_works="guessing no";;
  238. *) gl_cv_func_isnanl_works="guessing yes";;
  239. esac
  240. ;;
  241. esac
  242. ])
  243. ])
  244. ])