isfinite.m4 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # isfinite.m4 serial 17
  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_ISFINITE],
  7. [
  8. AC_REQUIRE([gl_MATH_H_DEFAULTS])
  9. AC_REQUIRE([AC_CANONICAL_HOST])
  10. dnl Persuade glibc <math.h> to declare isfinite.
  11. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
  12. AC_CHECK_DECLS([isfinite], , , [[#include <math.h>]])
  13. if test "$ac_cv_have_decl_isfinite" = yes; then
  14. gl_CHECK_MATH_LIB([ISFINITE_LIBM],
  15. [x = isfinite (x) + isfinite ((float) x);])
  16. if test "$ISFINITE_LIBM" != missing; then
  17. dnl Test whether isfinite() on 'long double' works.
  18. gl_ISFINITEL_WORKS
  19. case "$gl_cv_func_isfinitel_works" in
  20. *yes) ;;
  21. *) ISFINITE_LIBM=missing;;
  22. esac
  23. dnl Also, isfinite() on 'double' does not work on Linux/ia64 (because of
  24. dnl signalling NaNs). But this does not have to be tested, since
  25. dnl isfinite(long double) also does not work in this situation.
  26. fi
  27. fi
  28. dnl On Solaris 10, with CC in C++ mode, isfinite is not available although
  29. dnl is with cc in C mode. This cannot be worked around by defining
  30. dnl _XOPEN_SOURCE=600, because the latter does not work in C++ mode on
  31. dnl Solaris 11.0. Therefore use the replacement functions on Solaris.
  32. if test "$ac_cv_have_decl_isfinite" != yes \
  33. || test "$ISFINITE_LIBM" = missing \
  34. || { case "$host_os" in solaris*) true;; *) false;; esac; }; then
  35. REPLACE_ISFINITE=1
  36. dnl No libraries are needed to link lib/isfinite.c.
  37. ISFINITE_LIBM=
  38. fi
  39. AC_SUBST([ISFINITE_LIBM])
  40. ])
  41. dnl Test whether isfinite() on 'long double' recognizes all canonical values
  42. dnl which are neither finite nor infinite.
  43. AC_DEFUN([gl_ISFINITEL_WORKS],
  44. [
  45. AC_REQUIRE([AC_PROG_CC])
  46. AC_REQUIRE([gl_BIGENDIAN])
  47. AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
  48. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  49. AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
  50. [
  51. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  52. #include <float.h>
  53. #include <limits.h>
  54. #include <math.h>
  55. #define NWORDS \
  56. ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  57. typedef union { unsigned int word[NWORDS]; long double value; }
  58. memory_long_double;
  59. /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
  60. runtime type conversion. */
  61. #ifdef __sgi
  62. static long double NaNl ()
  63. {
  64. double zero = 0.0;
  65. return zero / zero;
  66. }
  67. #else
  68. # define NaNl() (0.0L / 0.0L)
  69. #endif
  70. int main ()
  71. {
  72. int result = 0;
  73. {
  74. memory_long_double m;
  75. unsigned int i;
  76. /* The isfinite macro should be immune against changes in the sign bit and
  77. in the mantissa bits. The xor operation twiddles a bit that can only be
  78. a sign bit or a mantissa bit (since the exponent never extends to
  79. bit 31). */
  80. m.value = NaNl ();
  81. m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
  82. for (i = 0; i < NWORDS; i++)
  83. m.word[i] |= 1;
  84. if (isfinite (m.value))
  85. result |= 1;
  86. }
  87. #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
  88. /* Representation of an 80-bit 'long double' as an initializer for a sequence
  89. of 'unsigned int' words. */
  90. # ifdef WORDS_BIGENDIAN
  91. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  92. { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
  93. ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
  94. (unsigned int) (mantlo) << 16 \
  95. }
  96. # else
  97. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  98. { mantlo, manthi, exponent }
  99. # endif
  100. { /* Quiet NaN. */
  101. static memory_long_double x =
  102. { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
  103. if (isfinite (x.value))
  104. result |= 2;
  105. }
  106. {
  107. /* Signalling NaN. */
  108. static memory_long_double x =
  109. { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
  110. if (isfinite (x.value))
  111. result |= 2;
  112. }
  113. /* isfinite should return something even for noncanonical values. */
  114. { /* Pseudo-NaN. */
  115. static memory_long_double x =
  116. { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
  117. if (isfinite (x.value) && !isfinite (x.value))
  118. result |= 4;
  119. }
  120. { /* Pseudo-Infinity. */
  121. static memory_long_double x =
  122. { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
  123. if (isfinite (x.value) && !isfinite (x.value))
  124. result |= 8;
  125. }
  126. { /* Pseudo-Zero. */
  127. static memory_long_double x =
  128. { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
  129. if (isfinite (x.value) && !isfinite (x.value))
  130. result |= 16;
  131. }
  132. { /* Unnormalized number. */
  133. static memory_long_double x =
  134. { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
  135. if (isfinite (x.value) && !isfinite (x.value))
  136. result |= 32;
  137. }
  138. { /* Pseudo-Denormal. */
  139. static memory_long_double x =
  140. { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
  141. if (isfinite (x.value) && !isfinite (x.value))
  142. result |= 64;
  143. }
  144. #endif
  145. return result;
  146. }]])],
  147. [gl_cv_func_isfinitel_works=yes],
  148. [gl_cv_func_isfinitel_works=no],
  149. [case "$host_os" in
  150. # Guess no on native Windows.
  151. mingw*) gl_cv_func_isfinitel_works="guessing no" ;;
  152. *) gl_cv_func_isfinitel_works="guessing yes" ;;
  153. esac
  154. ])
  155. ])
  156. ])