isfinite.m4 5.6 KB

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