isfinite.m4 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # isfinite.m4 serial 15
  2. dnl Copyright (C) 2007-2017 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 canonical values
  36. dnl which are neither finite nor infinite.
  37. AC_DEFUN([gl_ISFINITEL_WORKS],
  38. [
  39. AC_REQUIRE([AC_PROG_CC])
  40. AC_REQUIRE([gl_BIGENDIAN])
  41. AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
  42. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  43. AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
  44. [
  45. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  46. #include <float.h>
  47. #include <limits.h>
  48. #include <math.h>
  49. #define NWORDS \
  50. ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  51. typedef union { unsigned int word[NWORDS]; long double value; }
  52. memory_long_double;
  53. /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
  54. runtime type conversion. */
  55. #ifdef __sgi
  56. static long double NaNl ()
  57. {
  58. double zero = 0.0;
  59. return zero / zero;
  60. }
  61. #else
  62. # define NaNl() (0.0L / 0.0L)
  63. #endif
  64. int main ()
  65. {
  66. int result = 0;
  67. {
  68. memory_long_double m;
  69. unsigned int i;
  70. /* The isfinite macro should be immune against changes in the sign bit and
  71. in the mantissa bits. The xor operation twiddles a bit that can only be
  72. a sign bit or a mantissa bit (since the exponent never extends to
  73. bit 31). */
  74. m.value = NaNl ();
  75. m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
  76. for (i = 0; i < NWORDS; i++)
  77. m.word[i] |= 1;
  78. if (isfinite (m.value))
  79. result |= 1;
  80. }
  81. #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
  82. /* Representation of an 80-bit 'long double' as an initializer for a sequence
  83. of 'unsigned int' words. */
  84. # ifdef WORDS_BIGENDIAN
  85. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  86. { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
  87. ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
  88. (unsigned int) (mantlo) << 16 \
  89. }
  90. # else
  91. # define LDBL80_WORDS(exponent,manthi,mantlo) \
  92. { mantlo, manthi, exponent }
  93. # endif
  94. { /* Quiet NaN. */
  95. static memory_long_double x =
  96. { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
  97. if (isfinite (x.value))
  98. result |= 2;
  99. }
  100. {
  101. /* Signalling NaN. */
  102. static memory_long_double x =
  103. { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
  104. if (isfinite (x.value))
  105. result |= 2;
  106. }
  107. /* isfinite should return something even for noncanonical values. */
  108. { /* Pseudo-NaN. */
  109. static memory_long_double x =
  110. { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
  111. if (isfinite (x.value) && !isfinite (x.value))
  112. result |= 4;
  113. }
  114. { /* Pseudo-Infinity. */
  115. static memory_long_double x =
  116. { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
  117. if (isfinite (x.value) && !isfinite (x.value))
  118. result |= 8;
  119. }
  120. { /* Pseudo-Zero. */
  121. static memory_long_double x =
  122. { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
  123. if (isfinite (x.value) && !isfinite (x.value))
  124. result |= 16;
  125. }
  126. { /* Unnormalized number. */
  127. static memory_long_double x =
  128. { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
  129. if (isfinite (x.value) && !isfinite (x.value))
  130. result |= 32;
  131. }
  132. { /* Pseudo-Denormal. */
  133. static memory_long_double x =
  134. { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
  135. if (isfinite (x.value) && !isfinite (x.value))
  136. result |= 64;
  137. }
  138. #endif
  139. return result;
  140. }]])], [gl_cv_func_isfinitel_works=yes], [gl_cv_func_isfinitel_works=no],
  141. [gl_cv_func_isfinitel_works="guessing yes"])
  142. ])
  143. ])