Modernize AC_TRY_RUN invocations.
[gnulib.git] / m4 / frexp.m4
1 # frexp.m4 serial 9
2 dnl Copyright (C) 2007-2010 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
7 AC_DEFUN([gl_FUNC_FREXP],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
11   FREXP_LIBM=
12   if test $gl_cv_func_frexp_no_libm = no; then
13     AC_CACHE_CHECK([whether frexp() can be used with libm],
14       [gl_cv_func_frexp_in_libm],
15       [
16         save_LIBS="$LIBS"
17         LIBS="$LIBS -lm"
18         AC_LINK_IFELSE(
19           [AC_LANG_PROGRAM(
20              [[#include <math.h>
21                double x;]],
22              [[int e; return frexp (x, &e) > 0;]])],
23           [gl_cv_func_frexp_in_libm=yes],
24           [gl_cv_func_frexp_in_libm=no])
25         LIBS="$save_LIBS"
26       ])
27     if test $gl_cv_func_frexp_in_libm = yes; then
28       FREXP_LIBM=-lm
29     fi
30   fi
31   if test $gl_cv_func_frexp_no_libm = yes \
32      || test $gl_cv_func_frexp_in_libm = yes; then
33     save_LIBS="$LIBS"
34     LIBS="$LIBS $FREXP_LIBM"
35     gl_FUNC_FREXP_WORKS
36     LIBS="$save_LIBS"
37     case "$gl_cv_func_frexp_works" in
38       *yes) gl_func_frexp=yes ;;
39       *)    gl_func_frexp=no; REPLACE_FREXP=1; FREXP_LIBM= ;;
40     esac
41   else
42     gl_func_frexp=no
43   fi
44   if test $gl_func_frexp = yes; then
45     AC_DEFINE([HAVE_FREXP], [1],
46       [Define if the frexp() function is available and works.])
47   else
48     AC_LIBOBJ([frexp])
49   fi
50   AC_SUBST([FREXP_LIBM])
51 ])
52
53 AC_DEFUN([gl_FUNC_FREXP_NO_LIBM],
54 [
55   AC_REQUIRE([gl_MATH_H_DEFAULTS])
56   AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
57   if test $gl_cv_func_frexp_no_libm = yes; then
58     gl_FUNC_FREXP_WORKS
59     case "$gl_cv_func_frexp_works" in
60       *yes) gl_func_frexp_no_libm=yes ;;
61       *)    gl_func_frexp_no_libm=no; REPLACE_FREXP=1 ;;
62     esac
63   else
64     gl_func_frexp_no_libm=no
65     dnl Set REPLACE_FREXP here because the system may have frexp in libm.
66     REPLACE_FREXP=1
67   fi
68   if test $gl_func_frexp_no_libm = yes; then
69     AC_DEFINE([HAVE_FREXP_IN_LIBC], [1],
70       [Define if the frexp() function is available in libc.])
71   else
72     AC_LIBOBJ([frexp])
73   fi
74 ])
75
76 dnl Test whether frexp() can be used without linking with libm.
77 dnl Set gl_cv_func_frexp_no_libm to 'yes' or 'no' accordingly.
78 AC_DEFUN([gl_CHECK_FREXP_NO_LIBM],
79 [
80   AC_CACHE_CHECK([whether frexp() can be used without linking with libm],
81     [gl_cv_func_frexp_no_libm],
82     [
83       AC_LINK_IFELSE(
84         [AC_LANG_PROGRAM(
85            [[#include <math.h>
86              double x;]],
87            [[int e; return frexp (x, &e) > 0;]])],
88         [gl_cv_func_frexp_no_libm=yes],
89         [gl_cv_func_frexp_no_libm=no])
90     ])
91 ])
92
93 dnl Test whether frexp() works also on denormalized numbers (this fails e.g. on
94 dnl NetBSD 3.0), on infinite numbers (this fails e.g. on IRIX 6.5 and mingw),
95 dnl and on negative zero (this fails e.g. on NetBSD 4.99).
96 AC_DEFUN([gl_FUNC_FREXP_WORKS],
97 [
98   AC_REQUIRE([AC_PROG_CC])
99   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
100   AC_CACHE_CHECK([whether frexp works], [gl_cv_func_frexp_works],
101     [
102       AC_RUN_IFELSE(
103         [AC_LANG_SOURCE([[
104 #include <float.h>
105 #include <math.h>
106 #include <string.h>
107 int main()
108 {
109   int i;
110   volatile double x;
111 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
112    So we use -zero instead.  */
113   double zero = 0.0;
114   /* Test on denormalized numbers.  */
115   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
116     ;
117   if (x > 0.0)
118     {
119       int exp;
120       double y = frexp (x, &exp);
121       /* On machines with IEEE754 arithmetic: x = 1.11254e-308, exp = -1022.
122          On NetBSD: y = 0.75. Correct: y = 0.5.  */
123       if (y != 0.5)
124         return 1;
125     }
126   /* Test on infinite numbers.  */
127   x = 1.0 / 0.0;
128   {
129     int exp;
130     double y = frexp (x, &exp);
131     if (y != x)
132       return 1;
133   }
134   /* Test on negative zero.  */
135   x = -zero;
136   {
137     int exp;
138     double y = frexp (x, &exp);
139     if (memcmp (&y, &x, sizeof x))
140       return 1;
141   }
142   return 0;
143 }]])],
144         [gl_cv_func_frexp_works=yes],
145         [gl_cv_func_frexp_works=no],
146         [case "$host_os" in
147            netbsd* | irix* | mingw*) gl_cv_func_frexp_works="guessing no";;
148            *)                        gl_cv_func_frexp_works="guessing yes";;
149          esac
150         ])
151     ])
152 ])