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