Merge branch 'upstream' into stable
[gnulib.git] / m4 / frexpl.m4
1 # frexpl.m4 serial 12
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_FREXPL],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   dnl Check whether it's declared.
11   dnl MacOS X 10.3 has frexpl() in libc but doesn't declare it in <math.h>.
12   AC_CHECK_DECL([frexpl], , [HAVE_DECL_FREXPL=0], [#include <math.h>])
13   FREXPL_LIBM=
14   if test $HAVE_DECL_FREXPL = 1; then
15     gl_CHECK_FREXPL_NO_LIBM
16     if test $gl_cv_func_frexpl_no_libm = no; then
17       AC_CACHE_CHECK([whether frexpl() can be used with libm],
18         [gl_cv_func_frexpl_in_libm],
19         [
20           save_LIBS="$LIBS"
21           LIBS="$LIBS -lm"
22           AC_LINK_IFELSE(
23             [AC_LANG_PROGRAM(
24                [[#include <math.h>
25                  long double x;]],
26                [[int e; return frexpl (x, &e) > 0;]])],
27             [gl_cv_func_frexpl_in_libm=yes],
28             [gl_cv_func_frexpl_in_libm=no])
29           LIBS="$save_LIBS"
30         ])
31       if test $gl_cv_func_frexpl_in_libm = yes; then
32         FREXPL_LIBM=-lm
33       fi
34     fi
35     if test $gl_cv_func_frexpl_no_libm = yes \
36        || test $gl_cv_func_frexpl_in_libm = yes; then
37       save_LIBS="$LIBS"
38       LIBS="$LIBS $FREXPL_LIBM"
39       gl_FUNC_FREXPL_WORKS
40       LIBS="$save_LIBS"
41       case "$gl_cv_func_frexpl_works" in
42         *yes) gl_func_frexpl=yes ;;
43         *)    gl_func_frexpl=no; REPLACE_FREXPL=1; FREXPL_LIBM= ;;
44       esac
45     else
46       gl_func_frexpl=no
47     fi
48     if test $gl_func_frexpl = yes; then
49       AC_DEFINE([HAVE_FREXPL], [1],
50         [Define if the frexpl() function is available.])
51     fi
52   fi
53   if test $HAVE_DECL_FREXPL = 0 || test $gl_func_frexpl = no; then
54     AC_LIBOBJ([frexpl])
55   fi
56   AC_SUBST([FREXPL_LIBM])
57 ])
58
59 AC_DEFUN([gl_FUNC_FREXPL_NO_LIBM],
60 [
61   AC_REQUIRE([gl_MATH_H_DEFAULTS])
62   dnl Check whether it's declared.
63   dnl MacOS X 10.3 has frexpl() in libc but doesn't declare it in <math.h>.
64   AC_CHECK_DECL([frexpl], , [HAVE_DECL_FREXPL=0], [#include <math.h>])
65   if test $HAVE_DECL_FREXPL = 1; then
66     gl_CHECK_FREXPL_NO_LIBM
67     if test $gl_cv_func_frexpl_no_libm = yes; then
68       gl_FUNC_FREXPL_WORKS
69       case "$gl_cv_func_frexpl_works" in
70         *yes) gl_func_frexpl_no_libm=yes ;;
71         *)    gl_func_frexpl_no_libm=no; REPLACE_FREXPL=1 ;;
72       esac
73     else
74       gl_func_frexpl_no_libm=no
75       dnl Set REPLACE_FREXPL here because the system may have frexpl in libm.
76       REPLACE_FREXPL=1
77     fi
78     if test $gl_func_frexpl_no_libm = yes; then
79       AC_DEFINE([HAVE_FREXPL_IN_LIBC], [1],
80         [Define if the frexpl() function is available in libc.])
81     fi
82   fi
83   if test $HAVE_DECL_FREXPL = 0 || test $gl_func_frexpl_no_libm = no; then
84     AC_LIBOBJ([frexpl])
85   fi
86 ])
87
88 dnl Test whether frexpl() can be used without linking with libm.
89 dnl Set gl_cv_func_frexpl_no_libm to 'yes' or 'no' accordingly.
90 AC_DEFUN([gl_CHECK_FREXPL_NO_LIBM],
91 [
92   AC_CACHE_CHECK([whether frexpl() can be used without linking with libm],
93     [gl_cv_func_frexpl_no_libm],
94     [
95       AC_LINK_IFELSE(
96         [AC_LANG_PROGRAM(
97            [[#include <math.h>
98              long double x;]],
99            [[int e; return frexpl (x, &e) > 0;]])],
100         [gl_cv_func_frexpl_no_libm=yes],
101         [gl_cv_func_frexpl_no_libm=no])
102     ])
103 ])
104
105 dnl Test whether frexpl() works on finite numbers (this fails on
106 dnl MacOS X 10.4/PowerPC, on AIX 5.1, and on BeOS), on denormalized numbers
107 dnl (this fails on MacOS X 10.5/i386), and also on infinite numbers (this
108 dnl fails e.g. on IRIX 6.5 and mingw).
109 AC_DEFUN([gl_FUNC_FREXPL_WORKS],
110 [
111   AC_REQUIRE([AC_PROG_CC])
112   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
113   AC_CACHE_CHECK([whether frexpl works], [gl_cv_func_frexpl_works],
114     [
115       AC_RUN_IFELSE(
116         [AC_LANG_SOURCE([[
117 #include <float.h>
118 #include <math.h>
119 /* Override the values of <float.h>, like done in float.in.h.  */
120 #if defined __i386__ && (defined __BEOS__ || defined __OpenBSD__)
121 # undef LDBL_MIN_EXP
122 # define LDBL_MIN_EXP    (-16381)
123 #endif
124 extern long double frexpl (long double, int *);
125 int main()
126 {
127   volatile long double x;
128   /* Test on finite numbers that fails on AIX 5.1.  */
129   x = 16.0L;
130   {
131     int exp = -9999;
132     frexpl (x, &exp);
133     if (exp != 5)
134       return 1;
135   }
136   /* Test on finite numbers that fails on MacOS X 10.4, because its frexpl
137      function returns an invalid (incorrectly normalized) value: it returns
138                y = { 0x3fe028f5, 0xc28f5c28, 0x3c9eb851, 0xeb851eb8 }
139      but the correct result is
140           0.505L = { 0x3fe028f5, 0xc28f5c29, 0xbc547ae1, 0x47ae1480 }  */
141   x = 1.01L;
142   {
143     int exp = -9999;
144     long double y = frexpl (x, &exp);
145     if (!(exp == 1 && y == 0.505L))
146       return 1;
147   }
148   /* Test on large finite numbers.  This fails on BeOS at i = 16322, while
149      LDBL_MAX_EXP = 16384.
150      In the loop end test, we test x against Infinity, rather than comparing
151      i with LDBL_MAX_EXP, because BeOS <float.h> has a wrong LDBL_MAX_EXP.  */
152   {
153     int i;
154     for (i = 1, x = 1.0L; x != x + x; i++, x *= 2.0L)
155       {
156         int exp = -9999;
157         frexpl (x, &exp);
158         if (exp != i)
159           return 1;
160       }
161   }
162   /* Test on denormalized numbers.  */
163   {
164     int i;
165     for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
166       ;
167     if (x > 0.0L)
168       {
169         int exp;
170         long double y = frexpl (x, &exp);
171         /* On machines with IEEE854 arithmetic: x = 1.68105e-4932,
172            exp = -16382, y = 0.5.  On MacOS X 10.5: exp = -16384, y = 0.5.  */
173         if (exp != LDBL_MIN_EXP - 1)
174           return 1;
175       }
176   }
177   /* Test on infinite numbers.  */
178   x = 1.0L / 0.0L;
179   {
180     int exp;
181     long double y = frexpl (x, &exp);
182     if (y != x)
183       return 1;
184   }
185   return 0;
186 }]])],
187         [gl_cv_func_frexpl_works=yes],
188         [gl_cv_func_frexpl_works=no],
189         [
190 changequote(,)dnl
191          case "$host_os" in
192            aix | aix[3-6]* | beos* | darwin* | irix* | mingw* | pw*)
193               gl_cv_func_frexpl_works="guessing no";;
194            *) gl_cv_func_frexpl_works="guessing yes";;
195          esac
196 changequote([,])dnl
197         ])
198     ])
199 ])