isnanf.m4 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # isnanf.m4 serial 18
  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. dnl Check how to get or define isnanf().
  7. AC_DEFUN([gl_FUNC_ISNANF],
  8. [
  9. AC_REQUIRE([gl_MATH_H_DEFAULTS])
  10. ISNANF_LIBM=
  11. gl_HAVE_ISNANF_NO_LIBM
  12. if test $gl_cv_func_isnanf_no_libm = no; then
  13. gl_HAVE_ISNANF_IN_LIBM
  14. if test $gl_cv_func_isnanf_in_libm = yes; then
  15. ISNANF_LIBM=-lm
  16. fi
  17. fi
  18. dnl The variable gl_func_isnanf set here is used by isnan.m4.
  19. if test $gl_cv_func_isnanf_no_libm = yes \
  20. || test $gl_cv_func_isnanf_in_libm = yes; then
  21. save_LIBS="$LIBS"
  22. LIBS="$LIBS $ISNANF_LIBM"
  23. gl_ISNANF_WORKS
  24. LIBS="$save_LIBS"
  25. case "$gl_cv_func_isnanf_works" in
  26. *yes) gl_func_isnanf=yes ;;
  27. *) gl_func_isnanf=no; ISNANF_LIBM= ;;
  28. esac
  29. else
  30. gl_func_isnanf=no
  31. fi
  32. if test $gl_func_isnanf != yes; then
  33. HAVE_ISNANF=0
  34. fi
  35. AC_SUBST([ISNANF_LIBM])
  36. ])
  37. dnl Check how to get or define isnanf() without linking with libm.
  38. AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
  39. [
  40. gl_HAVE_ISNANF_NO_LIBM
  41. if test $gl_cv_func_isnanf_no_libm = yes; then
  42. gl_ISNANF_WORKS
  43. fi
  44. if test $gl_cv_func_isnanf_no_libm = yes \
  45. && { case "$gl_cv_func_isnanf_works" in
  46. *yes) true;;
  47. *) false;;
  48. esac
  49. }; then
  50. gl_func_isnanf_no_libm=yes
  51. AC_DEFINE([HAVE_ISNANF_IN_LIBC], [1],
  52. [Define if the isnan(float) function is available in libc.])
  53. else
  54. gl_func_isnanf_no_libm=no
  55. fi
  56. ])
  57. dnl Prerequisites of replacement isnanf definition. It does not need -lm.
  58. AC_DEFUN([gl_PREREQ_ISNANF],
  59. [
  60. gl_FLOAT_EXPONENT_LOCATION
  61. ])
  62. dnl Test whether isnanf() can be used without libm.
  63. AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
  64. [
  65. AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
  66. [gl_cv_func_isnanf_no_libm],
  67. [
  68. AC_LINK_IFELSE(
  69. [AC_LANG_PROGRAM(
  70. [[#include <math.h>
  71. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  72. # undef isnanf
  73. # define isnanf(x) __builtin_isnan ((float)(x))
  74. #elif defined isnan
  75. # undef isnanf
  76. # define isnanf(x) isnan ((float)(x))
  77. #endif
  78. float x;]],
  79. [[return isnanf (x);]])],
  80. [gl_cv_func_isnanf_no_libm=yes],
  81. [gl_cv_func_isnanf_no_libm=no])
  82. ])
  83. ])
  84. dnl Test whether isnanf() can be used with libm.
  85. AC_DEFUN([gl_HAVE_ISNANF_IN_LIBM],
  86. [
  87. AC_CACHE_CHECK([whether isnan(float) can be used with libm],
  88. [gl_cv_func_isnanf_in_libm],
  89. [
  90. save_LIBS="$LIBS"
  91. LIBS="$LIBS -lm"
  92. AC_LINK_IFELSE(
  93. [AC_LANG_PROGRAM(
  94. [[#include <math.h>
  95. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  96. # undef isnanf
  97. # define isnanf(x) __builtin_isnan ((float)(x))
  98. #elif defined isnan
  99. # undef isnanf
  100. # define isnanf(x) isnan ((float)(x))
  101. #endif
  102. float x;]],
  103. [[return isnanf (x);]])],
  104. [gl_cv_func_isnanf_in_libm=yes],
  105. [gl_cv_func_isnanf_in_libm=no])
  106. LIBS="$save_LIBS"
  107. ])
  108. ])
  109. dnl Test whether isnanf() rejects Infinity (this fails on Solaris 2.5.1),
  110. dnl recognizes a NaN (this fails on IRIX 6.5 with cc), and recognizes a NaN
  111. dnl with in-memory representation 0x7fbfffff (this fails on IRIX 6.5).
  112. AC_DEFUN([gl_ISNANF_WORKS],
  113. [
  114. AC_REQUIRE([AC_PROG_CC])
  115. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  116. AC_REQUIRE([gl_FLOAT_EXPONENT_LOCATION])
  117. AC_CACHE_CHECK([whether isnan(float) works], [gl_cv_func_isnanf_works],
  118. [
  119. AC_RUN_IFELSE(
  120. [AC_LANG_SOURCE([[
  121. #include <math.h>
  122. #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
  123. # undef isnanf
  124. # define isnanf(x) __builtin_isnan ((float)(x))
  125. #elif defined isnan
  126. # undef isnanf
  127. # define isnanf(x) isnan ((float)(x))
  128. #endif
  129. /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
  130. #ifdef __DECC
  131. static float
  132. NaN ()
  133. {
  134. static float zero = 0.0f;
  135. return zero / zero;
  136. }
  137. #else
  138. # define NaN() (0.0f / 0.0f)
  139. #endif
  140. #define NWORDS \
  141. ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  142. typedef union { unsigned int word[NWORDS]; float value; } memory_float;
  143. int main()
  144. {
  145. int result = 0;
  146. if (isnanf (1.0f / 0.0f))
  147. result |= 1;
  148. if (!isnanf (NaN ()))
  149. result |= 2;
  150. #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
  151. /* The isnanf function should be immune against changes in the sign bit and
  152. in the mantissa bits. The xor operation twiddles a bit that can only be
  153. a sign bit or a mantissa bit. */
  154. if (FLT_EXPBIT0_WORD == 0 && FLT_EXPBIT0_BIT > 0)
  155. {
  156. memory_float m;
  157. m.value = NaN ();
  158. /* Set the bits below the exponent to 01111...111. */
  159. m.word[0] &= -1U << FLT_EXPBIT0_BIT;
  160. m.word[0] |= (1U << (FLT_EXPBIT0_BIT - 1)) - 1;
  161. if (!isnanf (m.value))
  162. result |= 4;
  163. }
  164. #endif
  165. return result;
  166. }]])],
  167. [gl_cv_func_isnanf_works=yes],
  168. [gl_cv_func_isnanf_works=no],
  169. [case "$host_os" in
  170. irix* | solaris*) gl_cv_func_isnanf_works="guessing no" ;;
  171. mingw*) # Guess yes on mingw, no on MSVC.
  172. AC_EGREP_CPP([Known], [
  173. #ifdef __MINGW32__
  174. Known
  175. #endif
  176. ],
  177. [gl_cv_func_isnanf_works="guessing yes"],
  178. [gl_cv_func_isnanf_works="guessing no"])
  179. ;;
  180. *) gl_cv_func_isnanf_works="guessing yes" ;;
  181. esac
  182. ])
  183. ])
  184. ])