f8ef24bdd639593c4fc357d5424497843f452aff
[gnulib.git] / m4 / log2.m4
1 # log2.m4 serial 1
2 dnl Copyright (C) 2010-2012 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_LOG2],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10
11   dnl Persuade glibc <math.h> to declare log2().
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13
14   dnl Determine LOG2_LIBM.
15   gl_COMMON_DOUBLE_MATHFUNC([log2])
16
17   dnl Test whether log2() exists.
18   save_LIBS="$LIBS"
19   LIBS="$LIBS $LOG2_LIBM"
20   AC_CHECK_FUNCS([log2])
21   LIBS="$save_LIBS"
22   if test $ac_cv_func_log2 = yes; then
23     HAVE_LOG2=1
24     dnl Also check whether it's declared.
25     dnl IRIX 6.5 has log2() in libm but doesn't declare it in <math.h>.
26     AC_CHECK_DECL([log2], , [HAVE_DECL_LOG2=0], [[#include <math.h>]])
27
28     save_LIBS="$LIBS"
29     LIBS="$LIBS $LOG2_LIBM"
30     gl_FUNC_LOG2_WORKS
31     LIBS="$save_LIBS"
32     case "$gl_cv_func_log2_works" in
33       *yes) ;;
34       *) REPLACE_LOG2=1 ;;
35     esac
36   else
37     HAVE_LOG2=0
38     HAVE_DECL_LOG2=0
39   fi
40   if test $HAVE_LOG2 = 0 || test $REPLACE_LOG2 = 1; then
41     dnl Find libraries needed to link lib/log2.c.
42     AC_REQUIRE([gl_FUNC_ISNAND])
43     AC_REQUIRE([gl_FUNC_FREXP])
44     AC_REQUIRE([gl_FUNC_LOG])
45     LOG2_LIBM=
46     dnl Append $ISNAND_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
47     case " $LOG2_LIBM " in
48       *" $ISNAND_LIBM "*) ;;
49       *) LOG2_LIBM="$LOG2_LIBM $ISNAND_LIBM" ;;
50     esac
51     dnl Append $FREXP_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
52     case " $LOG2_LIBM " in
53       *" $FREXP_LIBM "*) ;;
54       *) LOG2_LIBM="$LOG2_LIBM $FREXP_LIBM" ;;
55     esac
56     dnl Append $LOG_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
57     case " $LOG2_LIBM " in
58       *" $LOG_LIBM "*) ;;
59       *) LOG2_LIBM="$LOG2_LIBM $LOG_LIBM" ;;
60     esac
61   fi
62 ])
63
64 dnl Test whether log2() works.
65 dnl On OSF/1 5.1, log2(-0.0) is NaN.
66 dnl On Cygwin 1.7.9, log2(2^29) is not exactly 29.
67 AC_DEFUN([gl_FUNC_LOG2_WORKS],
68 [
69   AC_REQUIRE([AC_PROG_CC])
70   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
71   AC_CACHE_CHECK([whether log2 works], [gl_cv_func_log2_works],
72     [
73       AC_RUN_IFELSE(
74         [AC_LANG_SOURCE([[
75 #include <math.h>
76 #ifndef log2 /* for Cygwin 1.7.x */
77 extern
78 #ifdef __cplusplus
79 "C"
80 #endif
81 double log2 (double);
82 #endif
83 volatile double x;
84 volatile double y;
85 int main ()
86 {
87   int result = 0;
88   /* This test fails on OSF/1 5.1.  */
89   x = -0.0;
90   y = log2 (x);
91   if (!(y + y == y))
92     result |= 1;
93   /* This test fails on Cygwin 1.7.9.  */
94   x = 536870912.0;
95   y = log2 (x);
96   if (!(y == 29.0))
97     result |= 2;
98   return result;
99 }
100 ]])],
101         [gl_cv_func_log2_works=yes],
102         [gl_cv_func_log2_works=no],
103         [case "$host_os" in
104            cygwin* | osf*) gl_cv_func_log2_works="guessing no";;
105            *)              gl_cv_func_log2_works="guessing yes";;
106          esac
107         ])
108     ])
109 ])