1# log2.m4 serial 3
2dnl Copyright (C) 2010-2014 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_LOG2],
8[
9  m4_divert_text([DEFAULTS], [gl_log2_required=plain])
10  AC_REQUIRE([gl_MATH_H_DEFAULTS])
11
12  dnl Persuade glibc <math.h> to declare log2().
13  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15  dnl Determine LOG2_LIBM.
16  gl_COMMON_DOUBLE_MATHFUNC([log2])
17
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>]])
28
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
37
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 */
53extern
54#ifdef __cplusplus
55"C"
56#endif
57double 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.  */
62static int
63numeric_equal (double x, double y)
64{
65  return x == y;
66}
67static double dummy (double x) { return 0; }
68int main (int argc, char *argv[])
69{
70  double (*my_log2) (double) = argc ? log2 : dummy;
71  /* Test log2(negative).
72     This test fails on NetBSD 5.1 and Solaris 11 2011-11.  */
73  double y = my_log2 (-1.0);
74  if (numeric_equal (y, y))
75    return 1;
76  return 0;
77}
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*) gl_cv_func_log2_ieee="guessing yes" ;;
84                         # If we don't know, assume the worst.
85                 *)      gl_cv_func_log2_ieee="guessing no" ;;
86               esac
87              ])
88            LIBS="$save_LIBS"
89          ])
90        case "$gl_cv_func_log2_ieee" in
91          *yes) ;;
92          *) REPLACE_LOG2=1 ;;
93        esac
94      fi
95    ])
96  else
97    HAVE_LOG2=0
98    HAVE_DECL_LOG2=0
99  fi
100  if test $HAVE_LOG2 = 0 || test $REPLACE_LOG2 = 1; then
101    dnl Find libraries needed to link lib/log2.c.
102    AC_REQUIRE([gl_FUNC_ISNAND])
103    AC_REQUIRE([gl_FUNC_FREXP])
104    AC_REQUIRE([gl_FUNC_LOG])
105    LOG2_LIBM=
106    dnl Append $ISNAND_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
107    case " $LOG2_LIBM " in
108      *" $ISNAND_LIBM "*) ;;
109      *) LOG2_LIBM="$LOG2_LIBM $ISNAND_LIBM" ;;
110    esac
111    dnl Append $FREXP_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
112    case " $LOG2_LIBM " in
113      *" $FREXP_LIBM "*) ;;
114      *) LOG2_LIBM="$LOG2_LIBM $FREXP_LIBM" ;;
115    esac
116    dnl Append $LOG_LIBM to LOG2_LIBM, avoiding gratuitous duplicates.
117    case " $LOG2_LIBM " in
118      *" $LOG_LIBM "*) ;;
119      *) LOG2_LIBM="$LOG2_LIBM $LOG_LIBM" ;;
120    esac
121  fi
122])
123
124dnl Test whether log2() works.
125dnl On OSF/1 5.1, log2(-0.0) is NaN.
126dnl On Cygwin 1.7.9, log2(2^29) is not exactly 29.
127AC_DEFUN([gl_FUNC_LOG2_WORKS],
128[
129  AC_REQUIRE([AC_PROG_CC])
130  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
131  AC_CACHE_CHECK([whether log2 works], [gl_cv_func_log2_works],
132    [
133      AC_RUN_IFELSE(
134        [AC_LANG_SOURCE([[
135#include <math.h>
136#ifndef log2 /* for Cygwin 1.7.x */
137extern
138#ifdef __cplusplus
139"C"
140#endif
141double log2 (double);
142#endif
143volatile double x;
144volatile double y;
145int main ()
146{
147  int result = 0;
148  /* This test fails on OSF/1 5.1.  */
149  x = -0.0;
150  y = log2 (x);
151  if (!(y + y == y))
152    result |= 1;
153  /* This test fails on Cygwin 1.7.9.  */
154  x = 536870912.0;
155  y = log2 (x);
156  if (!(y == 29.0))
157    result |= 2;
158  return result;
159}
160]])],
161        [gl_cv_func_log2_works=yes],
162        [gl_cv_func_log2_works=no],
163        [case "$host_os" in
164           cygwin* | osf*) gl_cv_func_log2_works="guessing no";;
165           *)              gl_cv_func_log2_works="guessing yes";;
166         esac
167        ])
168    ])
169])
170