frexp.m4 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # frexp.m4 serial 16
  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_FUNC_FREXP],
  7. [
  8. AC_REQUIRE([gl_MATH_H_DEFAULTS])
  9. AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
  10. FREXP_LIBM=
  11. if test $gl_cv_func_frexp_no_libm = no; then
  12. AC_CACHE_CHECK([whether frexp() can be used with libm],
  13. [gl_cv_func_frexp_in_libm],
  14. [
  15. save_LIBS="$LIBS"
  16. LIBS="$LIBS -lm"
  17. AC_LINK_IFELSE(
  18. [AC_LANG_PROGRAM(
  19. [[#include <math.h>
  20. double x;]],
  21. [[int e; return frexp (x, &e) > 0;]])],
  22. [gl_cv_func_frexp_in_libm=yes],
  23. [gl_cv_func_frexp_in_libm=no])
  24. LIBS="$save_LIBS"
  25. ])
  26. if test $gl_cv_func_frexp_in_libm = yes; then
  27. FREXP_LIBM=-lm
  28. fi
  29. fi
  30. if test $gl_cv_func_frexp_no_libm = yes \
  31. || test $gl_cv_func_frexp_in_libm = yes; then
  32. save_LIBS="$LIBS"
  33. LIBS="$LIBS $FREXP_LIBM"
  34. gl_FUNC_FREXP_WORKS
  35. LIBS="$save_LIBS"
  36. case "$gl_cv_func_frexp_works" in
  37. *yes) gl_func_frexp=yes ;;
  38. *) gl_func_frexp=no; REPLACE_FREXP=1; FREXP_LIBM= ;;
  39. esac
  40. else
  41. gl_func_frexp=no
  42. fi
  43. if test $gl_func_frexp = yes; then
  44. AC_DEFINE([HAVE_FREXP], [1],
  45. [Define if the frexp() function is available and works.])
  46. fi
  47. AC_SUBST([FREXP_LIBM])
  48. ])
  49. AC_DEFUN([gl_FUNC_FREXP_NO_LIBM],
  50. [
  51. AC_REQUIRE([gl_MATH_H_DEFAULTS])
  52. AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
  53. if test $gl_cv_func_frexp_no_libm = yes; then
  54. gl_FUNC_FREXP_WORKS
  55. case "$gl_cv_func_frexp_works" in
  56. *yes) gl_func_frexp_no_libm=yes ;;
  57. *) gl_func_frexp_no_libm=no; REPLACE_FREXP=1 ;;
  58. esac
  59. else
  60. gl_func_frexp_no_libm=no
  61. dnl Set REPLACE_FREXP here because the system may have frexp in libm.
  62. REPLACE_FREXP=1
  63. fi
  64. if test $gl_func_frexp_no_libm = yes; then
  65. AC_DEFINE([HAVE_FREXP_IN_LIBC], [1],
  66. [Define if the frexp() function is available in libc.])
  67. fi
  68. ])
  69. dnl Test whether frexp() can be used without linking with libm.
  70. dnl Set gl_cv_func_frexp_no_libm to 'yes' or 'no' accordingly.
  71. AC_DEFUN([gl_CHECK_FREXP_NO_LIBM],
  72. [
  73. AC_CACHE_CHECK([whether frexp() can be used without linking with libm],
  74. [gl_cv_func_frexp_no_libm],
  75. [
  76. AC_LINK_IFELSE(
  77. [AC_LANG_PROGRAM(
  78. [[#include <math.h>
  79. double x;]],
  80. [[int e; return frexp (x, &e) > 0;]])],
  81. [gl_cv_func_frexp_no_libm=yes],
  82. [gl_cv_func_frexp_no_libm=no])
  83. ])
  84. ])
  85. dnl Test whether frexp() works also on denormalized numbers (this fails e.g. on
  86. dnl NetBSD 3.0), on infinite numbers (this fails e.g. on IRIX 6.5 and mingw),
  87. dnl and on negative zero (this fails e.g. on NetBSD 4.99 and mingw).
  88. AC_DEFUN([gl_FUNC_FREXP_WORKS],
  89. [
  90. AC_REQUIRE([AC_PROG_CC])
  91. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  92. AC_CHECK_DECLS_ONCE([alarm])
  93. AC_CACHE_CHECK([whether frexp works], [gl_cv_func_frexp_works],
  94. [
  95. AC_RUN_IFELSE(
  96. [AC_LANG_SOURCE([[
  97. #include <float.h>
  98. #include <math.h>
  99. #include <string.h>
  100. #if HAVE_DECL_ALARM
  101. # include <signal.h>
  102. # include <unistd.h>
  103. #endif
  104. /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
  105. ICC 10.0 has a bug when optimizing the expression -zero.
  106. The expression -DBL_MIN * DBL_MIN does not work when cross-compiling
  107. to PowerPC on Mac OS X 10.5. */
  108. #if defined __hpux || defined __sgi || defined __ICC
  109. static double
  110. compute_minus_zero (void)
  111. {
  112. return -DBL_MIN * DBL_MIN;
  113. }
  114. # define minus_zero compute_minus_zero ()
  115. #else
  116. double minus_zero = -0.0;
  117. #endif
  118. int main()
  119. {
  120. int result = 0;
  121. int i;
  122. volatile double x;
  123. double zero = 0.0;
  124. #if HAVE_DECL_ALARM
  125. /* NeXTstep 3.3 frexp() runs into an endless loop when called on an infinite
  126. number. Let the test fail in this case. */
  127. signal (SIGALRM, SIG_DFL);
  128. alarm (5);
  129. #endif
  130. /* Test on denormalized numbers. */
  131. for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
  132. ;
  133. if (x > 0.0)
  134. {
  135. int exp;
  136. double y = frexp (x, &exp);
  137. /* On machines with IEEE754 arithmetic: x = 1.11254e-308, exp = -1022.
  138. On NetBSD: y = 0.75. Correct: y = 0.5. */
  139. if (y != 0.5)
  140. result |= 1;
  141. }
  142. /* Test on infinite numbers. */
  143. x = 1.0 / zero;
  144. {
  145. int exp;
  146. double y = frexp (x, &exp);
  147. if (y != x)
  148. result |= 2;
  149. }
  150. /* Test on negative zero. */
  151. x = minus_zero;
  152. {
  153. int exp;
  154. double y = frexp (x, &exp);
  155. if (memcmp (&y, &x, sizeof x))
  156. result |= 4;
  157. }
  158. return result;
  159. }]])],
  160. [gl_cv_func_frexp_works=yes],
  161. [gl_cv_func_frexp_works=no],
  162. [case "$host_os" in
  163. netbsd* | irix*) gl_cv_func_frexp_works="guessing no" ;;
  164. mingw*) # Guess yes with MSVC, no with mingw.
  165. AC_EGREP_CPP([Good], [
  166. #ifdef _MSC_VER
  167. Good
  168. #endif
  169. ],
  170. [gl_cv_func_frexp_works="guessing yes"],
  171. [gl_cv_func_frexp_works="guessing no"])
  172. ;;
  173. *) gl_cv_func_frexp_works="guessing yes" ;;
  174. esac
  175. ])
  176. ])
  177. ])