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