obstack-zprintf: Add more tests.
[gnulib.git] / m4 / log2.m4
blobefeabcd303c95730a90d2931419b1c1b65f1d525
1 # log2.m4
2 # serial 15
3 dnl Copyright (C) 2010-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_LOG2],
10   m4_divert_text([DEFAULTS], [gl_log2_required=plain])
11   AC_REQUIRE([gl_MATH_H_DEFAULTS])
13   dnl Persuade glibc <math.h> to declare log2().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   dnl Determine LOG2_LIBM.
17   gl_COMMON_DOUBLE_MATHFUNC([log2])
19   dnl Test whether log2() exists.
20   saved_LIBS="$LIBS"
21   LIBS="$LIBS $LOG2_LIBM"
22   gl_CHECK_FUNCS_ANDROID([log2], [[#include <math.h>]])
23   LIBS="$saved_LIBS"
24   if test $ac_cv_func_log2 = yes; then
25     HAVE_LOG2=1
26     dnl Also check whether it's declared.
27     dnl IRIX 6.5 has log2() in libm but doesn't declare it in <math.h>.
28     AC_CHECK_DECL([log2], , [HAVE_DECL_LOG2=0], [[#include <math.h>]])
30     saved_LIBS="$LIBS"
31     LIBS="$LIBS $LOG2_LIBM"
32     gl_FUNC_LOG2_WORKS
33     LIBS="$saved_LIBS"
34     case "$gl_cv_func_log2_works" in
35       *yes) ;;
36       *) REPLACE_LOG2=1 ;;
37     esac
39     m4_ifdef([gl_FUNC_LOG2_IEEE], [
40       if test $gl_log2_required = ieee && test $REPLACE_LOG2 = 0; then
41         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
42         AC_CACHE_CHECK([whether log2 works according to ISO C 99 with IEC 60559],
43           [gl_cv_func_log2_ieee],
44           [
45             saved_LIBS="$LIBS"
46             LIBS="$LIBS $LOG2_LIBM"
47             AC_RUN_IFELSE(
48               [AC_LANG_SOURCE([[
49 #ifndef __NO_MATH_INLINES
50 # define __NO_MATH_INLINES 1 /* for glibc */
51 #endif
52 #include <math.h>
53 #ifndef log2 /* for Cygwin 1.7.x */
54 extern
55 #ifdef __cplusplus
56 "C"
57 #endif
58 double log2 (double);
59 #endif
60 /* Compare two numbers with ==.
61    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
62    'x == x' test.  */
63 static int
64 numeric_equal (double x, double y)
66   return x == y;
68 static double dummy (double x) { return 0; }
69 int main (int argc, char *argv[])
71   double (* volatile my_log2) (double) = argc ? log2 : dummy;
72   /* Test log2(negative).
73      This test fails on NetBSD 5.1 and Solaris 10/x86_64.  */
74   double y = my_log2 (-1.0);
75   if (numeric_equal (y, y))
76     return 1;
77   return 0;
79               ]])],
80               [gl_cv_func_log2_ieee=yes],
81               [gl_cv_func_log2_ieee=no],
82               [case "$host_os" in
83                                      # Guess yes on glibc systems.
84                  *-gnu* | gnu*)      gl_cv_func_log2_ieee="guessing yes" ;;
85                                      # Guess yes on musl systems.
86                  *-musl* | midipix*) gl_cv_func_log2_ieee="guessing yes" ;;
87                                      # Guess yes on native Windows.
88                  mingw* | windows*)  gl_cv_func_log2_ieee="guessing yes" ;;
89                                      # If we don't know, obey --enable-cross-guesses.
90                  *)                  gl_cv_func_log2_ieee="$gl_cross_guess_normal" ;;
91                esac
92               ])
93             LIBS="$saved_LIBS"
94           ])
95         case "$gl_cv_func_log2_ieee" in
96           *yes) ;;
97           *) REPLACE_LOG2=1 ;;
98         esac
99       fi
100     ])
101   else
102     HAVE_LOG2=0
103     HAVE_DECL_LOG2=0
104     case "$gl_cv_onwards_func_log2" in
105       future*) REPLACE_LOG2=1 ;;
106     esac
107   fi
108   if test $HAVE_LOG2 = 0 || test $REPLACE_LOG2 = 1; then
109     dnl Find libraries needed to link lib/log2.c.
110     AC_REQUIRE([gl_FUNC_ISNAND])
111     AC_REQUIRE([gl_FUNC_FREXP])
112     AC_REQUIRE([gl_FUNC_LOG])
113     LOG2_LIBM=
114     dnl Append $ISNAND_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
115     case " $LOG2_LIBM " in
116       *" $ISNAND_LIBM "*) ;;
117       *) LOG2_LIBM="$LOG2_LIBM $ISNAND_LIBM" ;;
118     esac
119     dnl Append $FREXP_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
120     case " $LOG2_LIBM " in
121       *" $FREXP_LIBM "*) ;;
122       *) LOG2_LIBM="$LOG2_LIBM $FREXP_LIBM" ;;
123     esac
124     dnl Append $LOG_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
125     case " $LOG2_LIBM " in
126       *" $LOG_LIBM "*) ;;
127       *) LOG2_LIBM="$LOG2_LIBM $LOG_LIBM" ;;
128     esac
129   fi
132 dnl Test whether log2() works.
133 dnl On OSF/1 5.1, log2(-0.0) is NaN.
134 dnl On Cygwin 1.7.9, log2(2^29) is not exactly 29.
135 AC_DEFUN([gl_FUNC_LOG2_WORKS],
137   AC_REQUIRE([AC_PROG_CC])
138   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
139   AC_CACHE_CHECK([whether log2 works], [gl_cv_func_log2_works],
140     [
141       AC_RUN_IFELSE(
142         [AC_LANG_SOURCE([[
143 #include <math.h>
144 #ifndef log2 /* for Cygwin 1.7.x */
145 extern
146 #ifdef __cplusplus
148 #endif
149 double log2 (double);
150 #endif
151 volatile double x;
152 volatile double y;
153 int main ()
155   int result = 0;
156   /* This test fails on OSF/1 5.1.  */
157   x = -0.0;
158   y = log2 (x);
159   if (!(y + y == y))
160     result |= 1;
161   /* This test fails on Cygwin 1.7.9.  */
162   x = 536870912.0;
163   y = log2 (x);
164   if (!(y == 29.0))
165     result |= 2;
166   return result;
168 ]])],
169         [gl_cv_func_log2_works=yes],
170         [gl_cv_func_log2_works=no],
171         [case "$host_os" in
172            cygwin* | osf*)    gl_cv_func_log2_works="guessing no" ;;
173                               # Guess yes on native Windows.
174            mingw* | windows*) gl_cv_func_log2_works="guessing yes" ;;
175            *)                 gl_cv_func_log2_works="guessing yes" ;;
176          esac
177         ])
178     ])