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