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