Fix COPYING.EXCEPTION license notices
[gnulib.git] / m4 / frexp.m4
blob6eea5f924e661f7dc7fd554d02eea1a036fef41a
1 # frexp.m4
2 # serial 20
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_FREXP],
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
12   FREXP_LIBM=
13   if test $gl_cv_func_frexp_no_libm = no; then
14     AC_CACHE_CHECK([whether frexp() can be used with libm],
15       [gl_cv_func_frexp_in_libm],
16       [
17         saved_LIBS="$LIBS"
18         LIBS="$LIBS -lm"
19         AC_LINK_IFELSE(
20           [AC_LANG_PROGRAM(
21              [[#include <math.h>
22                double x;]],
23              [[int e; return frexp (x, &e) > 0;]])],
24           [gl_cv_func_frexp_in_libm=yes],
25           [gl_cv_func_frexp_in_libm=no])
26         LIBS="$saved_LIBS"
27       ])
28     if test $gl_cv_func_frexp_in_libm = yes; then
29       FREXP_LIBM=-lm
30     fi
31   fi
32   if test $gl_cv_func_frexp_no_libm = yes \
33      || test $gl_cv_func_frexp_in_libm = yes; then
34     saved_LIBS="$LIBS"
35     LIBS="$LIBS $FREXP_LIBM"
36     gl_FUNC_FREXP_WORKS
37     LIBS="$saved_LIBS"
38     case "$gl_cv_func_frexp_works" in
39       *yes) gl_func_frexp=yes ;;
40       *)    gl_func_frexp=no; REPLACE_FREXP=1; FREXP_LIBM= ;;
41     esac
42   else
43     gl_func_frexp=no
44   fi
45   if test $gl_func_frexp = yes; then
46     AC_DEFINE([HAVE_FREXP], [1],
47       [Define if the frexp() function is available and works.])
48   fi
49   AC_SUBST([FREXP_LIBM])
52 AC_DEFUN([gl_FUNC_FREXP_NO_LIBM],
54   AC_REQUIRE([gl_MATH_H_DEFAULTS])
55   AC_REQUIRE([gl_CHECK_FREXP_NO_LIBM])
56   if test $gl_cv_func_frexp_no_libm = yes; then
57     gl_FUNC_FREXP_WORKS
58     case "$gl_cv_func_frexp_works" in
59       *yes) gl_func_frexp_no_libm=yes ;;
60       *)    gl_func_frexp_no_libm=no; REPLACE_FREXP=1 ;;
61     esac
62   else
63     gl_func_frexp_no_libm=no
64     dnl Set REPLACE_FREXP here because the system may have frexp in libm.
65     REPLACE_FREXP=1
66   fi
67   if test $gl_func_frexp_no_libm = yes; then
68     AC_DEFINE([HAVE_FREXP_IN_LIBC], [1],
69       [Define if the frexp() function is available in libc.])
70   fi
73 dnl Test whether frexp() can be used without linking with libm.
74 dnl Set gl_cv_func_frexp_no_libm to 'yes' or 'no' accordingly.
75 AC_DEFUN([gl_CHECK_FREXP_NO_LIBM],
77   AC_CACHE_CHECK([whether frexp() can be used without linking with libm],
78     [gl_cv_func_frexp_no_libm],
79     [
80       AC_LINK_IFELSE(
81         [AC_LANG_PROGRAM(
82            [[#include <math.h>
83              double x;]],
84            [[int e; return frexp (x, &e) > 0;]])],
85         [gl_cv_func_frexp_no_libm=yes],
86         [gl_cv_func_frexp_no_libm=no])
87     ])
90 dnl Test whether frexp() works also on denormalized numbers (this fails e.g. on
91 dnl NetBSD 3.0), on infinite numbers (this fails e.g. on IRIX 6.5 and mingw),
92 dnl and on negative zero (this fails e.g. on NetBSD 4.99 and mingw).
93 AC_DEFUN([gl_FUNC_FREXP_WORKS],
95   AC_REQUIRE([AC_PROG_CC])
96   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
97   AC_CHECK_DECLS_ONCE([alarm])
98   AC_CACHE_CHECK([whether frexp works], [gl_cv_func_frexp_works],
99     [
100       AC_RUN_IFELSE(
101         [AC_LANG_SOURCE([[
102 #include <float.h>
103 #include <math.h>
104 #include <string.h>
105 #if HAVE_DECL_ALARM
106 # include <signal.h>
107 # include <unistd.h>
108 #endif
109 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
110    ICC 10.0 has a bug when optimizing the expression -zero.
111    The expression -DBL_MIN * DBL_MIN does not work when cross-compiling
112    to PowerPC on Mac OS X 10.5.  */
113 #if defined __hpux || defined __sgi || defined __ICC
114 static double
115 compute_minus_zero (void)
117   return -DBL_MIN * DBL_MIN;
119 # define minus_zero compute_minus_zero ()
120 #else
121 double minus_zero = -0.0;
122 #endif
123 int main()
125   int result = 0;
126   int i;
127   volatile double x;
128   double zero = 0.0;
129 #if HAVE_DECL_ALARM
130   /* NeXTstep 3.3 frexp() runs into an endless loop when called on an infinite
131      number.  Let the test fail in this case.  */
132   signal (SIGALRM, SIG_DFL);
133   alarm (5);
134 #endif
135   /* Test on denormalized numbers.  */
136   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
137     ;
138   if (x > 0.0)
139     {
140       int exp;
141       double y = frexp (x, &exp);
142       /* On machines with IEEE754 arithmetic: x = 1.11254e-308, exp = -1022.
143          On NetBSD: y = 0.75. Correct: y = 0.5.  */
144       if (y != 0.5)
145         result |= 1;
146     }
147   /* Test on infinite numbers.  */
148   x = 1.0 / zero;
149   {
150     int exp;
151     double y = frexp (x, &exp);
152     if (y != x)
153       result |= 2;
154   }
155   /* Test on negative zero.  */
156   x = minus_zero;
157   {
158     int exp;
159     double y = frexp (x, &exp);
160     double x1 = x;
161     if (memcmp (&y, &x1, sizeof x1))
162       result |= 4;
163   }
164   return result;
165 }]])],
166         [gl_cv_func_frexp_works=yes],
167         [gl_cv_func_frexp_works=no],
168         [case "$host_os" in
169            netbsd* | irix*) gl_cv_func_frexp_works="guessing no" ;;
170            # Guess yes with MSVC, no with mingw.
171            windows*-msvc*)
172              gl_cv_func_frexp_works="guessing yes"
173              ;;
174            mingw* | windows*)
175              AC_EGREP_CPP([Good], [
176 #ifdef _MSC_VER
177  Good
178 #endif
179                ],
180                [gl_cv_func_frexp_works="guessing yes"],
181                [gl_cv_func_frexp_works="guessing no"])
182              ;;
183            *) gl_cv_func_frexp_works="guessing yes" ;;
184          esac
185         ])
186     ])