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