Merge commit 'snapshot-start' into stable
[gnulib.git] / m4 / frexpl.m4
1 # frexpl.m4 serial 16
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 #if defined __i386__ && defined __FreeBSD__
119 # undef LDBL_MIN_EXP
120 # define LDBL_MIN_EXP    (-16381)
121 #endif
122 #if (defined _ARCH_PPC || defined _POWER) && defined _AIX && (LDBL_MANT_DIG == 106) && defined __GNUC__
123 # undef LDBL_MIN_EXP
124 # define LDBL_MIN_EXP DBL_MIN_EXP
125 #endif
126 #if defined __sgi && (LDBL_MANT_DIG >= 106)
127 # if defined __GNUC__
128 #  undef LDBL_MIN_EXP
129 #  define LDBL_MIN_EXP DBL_MIN_EXP
130 # endif
131 #endif
132 extern
133 #ifdef __cplusplus
134 "C"
135 #endif
136 long double frexpl (long double, int *);
137 int main()
138 {
139   int result = 0;
140   volatile long double x;
141   /* Test on finite numbers that fails on AIX 5.1.  */
142   x = 16.0L;
143   {
144     int exp = -9999;
145     frexpl (x, &exp);
146     if (exp != 5)
147       result |= 1;
148   }
149   /* Test on finite numbers that fails on MacOS X 10.4, because its frexpl
150      function returns an invalid (incorrectly normalized) value: it returns
151                y = { 0x3fe028f5, 0xc28f5c28, 0x3c9eb851, 0xeb851eb8 }
152      but the correct result is
153           0.505L = { 0x3fe028f5, 0xc28f5c29, 0xbc547ae1, 0x47ae1480 }  */
154   x = 1.01L;
155   {
156     int exp = -9999;
157     long double y = frexpl (x, &exp);
158     if (!(exp == 1 && y == 0.505L))
159       result |= 2;
160   }
161   /* Test on large finite numbers.  This fails on BeOS at i = 16322, while
162      LDBL_MAX_EXP = 16384.
163      In the loop end test, we test x against Infinity, rather than comparing
164      i with LDBL_MAX_EXP, because BeOS <float.h> has a wrong LDBL_MAX_EXP.  */
165   {
166     int i;
167     for (i = 1, x = 1.0L; x != x + x; i++, x *= 2.0L)
168       {
169         int exp = -9999;
170         frexpl (x, &exp);
171         if (exp != i)
172           {
173             result |= 4;
174             break;
175           }
176       }
177   }
178   /* Test on denormalized numbers.  */
179   {
180     int i;
181     for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
182       ;
183     if (x > 0.0L)
184       {
185         int exp;
186         long double y = frexpl (x, &exp);
187         /* On machines with IEEE854 arithmetic: x = 1.68105e-4932,
188            exp = -16382, y = 0.5.  On MacOS X 10.5: exp = -16384, y = 0.5.  */
189         if (exp != LDBL_MIN_EXP - 1)
190           result |= 8;
191       }
192   }
193   /* Test on infinite numbers.  */
194   x = 1.0L / 0.0L;
195   {
196     int exp;
197     long double y = frexpl (x, &exp);
198     if (y != x)
199       result |= 16;
200   }
201   return result;
202 }]])],
203         [gl_cv_func_frexpl_works=yes],
204         [gl_cv_func_frexpl_works=no],
205         [
206 changequote(,)dnl
207          case "$host_os" in
208            aix | aix[3-6]* | beos* | darwin* | irix* | mingw* | pw*)
209               gl_cv_func_frexpl_works="guessing no";;
210            *) gl_cv_func_frexpl_works="guessing yes";;
211          esac
212 changequote([,])dnl
213         ])
214     ])
215 ])