changeset 16678:2f6973f75a06

New module 'log2f'. * lib/math.in.h (log2f): New declaration. * lib/log2f.c: New file. * m4/log2f.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whether log2f is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_LOG2F, HAVE_DECL_LOG2F, REPLACE_LOG2F. * modules/math (Makefile.am): Substitute GNULIB_LOG2F, HAVE_DECL_LOG2F, REPLACE_LOG2F. * modules/log2f: New file. * tests/test-math-c++.cc: Check the declaration of log2f. * doc/posix-functions/log2f.texi: Mention the new module and the IRIX and OSF/1 and Cygwin problems.
author Bruno Haible <bruno@clisp.org>
date Sun, 11 Mar 2012 17:57:12 +0100
parents 05117c6c2f05
children bb4c399d2025
files ChangeLog doc/posix-functions/log2f.texi lib/log2f.c lib/math.in.h m4/log2f.m4 m4/math_h.m4 modules/log2f modules/math tests/test-math-c++.cc
diffstat 9 files changed, 305 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2012-03-11  Bruno Haible  <bruno@clisp.org>
+
+	New module 'log2f'.
+	* lib/math.in.h (log2f): New declaration.
+	* lib/log2f.c: New file.
+	* m4/log2f.m4: New file.
+	* m4/math_h.m4 (gl_MATH_H): Test whether log2f is declared.
+	(gl_MATH_H_DEFAULTS): Initialize GNULIB_LOG2F, HAVE_DECL_LOG2F,
+	REPLACE_LOG2F.
+	* modules/math (Makefile.am): Substitute GNULIB_LOG2F, HAVE_DECL_LOG2F,
+	REPLACE_LOG2F.
+	* modules/log2f: New file.
+	* tests/test-math-c++.cc: Check the declaration of log2f.
+	* doc/posix-functions/log2f.texi: Mention the new module and the IRIX
+	and OSF/1 and Cygwin problems.
+
 2012-03-11  Bruno Haible  <bruno@clisp.org>
 
 	Tests for module 'log2'.
--- a/doc/posix-functions/log2f.texi
+++ b/doc/posix-functions/log2f.texi
@@ -4,19 +4,28 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/log2f.html}
 
-Gnulib module: ---
+Gnulib module: log2f
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, older IRIX 6.5, Solaris 9, MSVC 9,
+Interix 3.5.
+@item
+This function is not declared on some platforms:
+IRIX 6.5.
+@item
+This function is only provided as a macro on some platforms:
+Cygwin 1.5.x.
+@item
+This function returns a wrong value for a minus zero argument on some platforms:
+OSF/1 5.1.
+@item
+This function returns slightly wrong values for exact powers of 2 on some platforms:
+Cygwin 1.7.9.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, IRIX 6.5, Solaris 9, MSVC 9,
-Interix 3.5.
-@item
-This function is only provided as a macro on some platforms:
-Cygwin 1.5.x.
 @end itemize
new file mode 100644
--- /dev/null
+++ b/lib/log2f.c
@@ -0,0 +1,85 @@
+/* Base 2 logarithm.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <math.h>
+
+#if HAVE_LOG2
+
+float
+log2f (float x)
+{
+  return (float) log2 ((double) x);
+}
+
+#else
+
+/* Best possible approximation of log(2) as a 'float'.  */
+#define LOG2 0.693147180559945309417232121458176568075f
+
+/* Best possible approximation of 1/log(2) as a 'float'.  */
+#define LOG2_INVERSE 1.44269504088896340735992468100189213743f
+
+/* sqrt(0.5).  */
+#define SQRT_HALF 0.707106781186547524400844362104849039284f
+
+float
+log2f (float x)
+{
+  if (isnanf (x))
+    return x;
+
+  if (x <= 0.0f)
+    {
+      if (x == 0.0f)
+        /* Return -Infinity.  */
+        return - HUGE_VALF;
+      else
+        {
+          /* Return NaN.  */
+#if defined _MSC_VER
+          static float zero;
+          return zero / zero;
+#else
+          return 0.0f / 0.0f;
+#endif
+        }
+    }
+
+  /* Decompose x into
+       x = 2^e * y
+     where
+       e is an integer,
+       1/2 < y < 2.
+     Then log2(x) = e + log2(y) = e + log(y)/log(2).  */
+  {
+    int e;
+    float y;
+
+    y = frexpf (x, &e);
+    if (y < SQRT_HALF)
+      {
+        y = 2.0f * y;
+        e = e - 1;
+      }
+
+    return (float) e + logf (y) * LOG2_INVERSE;
+  }
+}
+
+#endif
--- a/lib/math.in.h
+++ b/lib/math.in.h
@@ -1291,6 +1291,30 @@
 #endif
 
 
+#if @GNULIB_LOG2F@
+# if @REPLACE_LOG2F@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef log2f
+#   define log2f rpl_log2f
+#  endif
+_GL_FUNCDECL_RPL (log2f, float, (float x));
+_GL_CXXALIAS_RPL (log2f, float, (float x));
+# else
+#  if !@HAVE_DECL_LOG2F@
+#   undef log2f
+_GL_FUNCDECL_SYS (log2f, float, (float x));
+#  endif
+_GL_CXXALIAS_SYS (log2f, float, (float x));
+# endif
+_GL_CXXALIASWARN (log2f);
+#elif defined GNULIB_POSIXCHECK
+# undef log2f
+# if HAVE_RAW_DECL_LOG2F
+_GL_WARN_ON_USE (log2f, "log2f is unportable - "
+                 "use gnulib module log2f for portability");
+# endif
+#endif
+
 #if @GNULIB_LOG2@
 # if @REPLACE_LOG2@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
new file mode 100644
--- /dev/null
+++ b/m4/log2f.m4
@@ -0,0 +1,116 @@
+# log2f.m4 serial 1
+dnl Copyright (C) 2010-2012 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_LOG2F],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_FUNC_LOG2])
+
+  dnl Persuade glibc <math.h> to declare log2f().
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+  dnl Determine LOG2F_LIBM.
+  gl_COMMON_DOUBLE_MATHFUNC([log2f])
+
+  dnl Test whether log2f() exists.
+  save_LIBS="$LIBS"
+  LIBS="$LIBS $LOG2F_LIBM"
+  AC_CHECK_FUNCS([log2f])
+  LIBS="$save_LIBS"
+  if test $ac_cv_func_log2f = yes; then
+    HAVE_LOG2F=1
+    dnl Also check whether it's declared.
+    dnl IRIX 6.5 has log2f() in libm but doesn't declare it in <math.h>.
+    AC_CHECK_DECL([log2f], , [HAVE_DECL_LOG2F=0], [[#include <math.h>]])
+
+    save_LIBS="$LIBS"
+    LIBS="$LIBS $LOG2F_LIBM"
+    gl_FUNC_LOG2F_WORKS
+    LIBS="$save_LIBS"
+    case "$gl_cv_func_log2f_works" in
+      *yes) ;;
+      *) REPLACE_LOG2F=1 ;;
+    esac
+  else
+    HAVE_LOG2F=0
+    HAVE_DECL_LOG2F=0
+  fi
+  if test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1; then
+    dnl Find libraries needed to link lib/log2f.c.
+    if test $ac_cv_func_log2 = yes; then
+      AC_DEFINE([HAVE_LOG2], [1],
+        [Define to 1 if the log2() function is available in libc or libm.])
+      LOG2F_LIBM="$LOG2_LIBM"
+    else
+      AC_REQUIRE([gl_FUNC_ISNANF])
+      AC_REQUIRE([gl_FUNC_FREXPF])
+      AC_REQUIRE([gl_FUNC_LOGF])
+      LOG2F_LIBM=
+      dnl Append $ISNANF_LIBM to LOG2F_LIBM, avoiding gratuitous duplicates.
+      case " $LOG2F_LIBM " in
+        *" $ISNANF_LIBM "*) ;;
+        *) LOG2F_LIBM="$LOG2F_LIBM $ISNANF_LIBM" ;;
+      esac
+      dnl Append $FREXPF_LIBM to LOG2F_LIBM, avoiding gratuitous duplicates.
+      case " $LOG2F_LIBM " in
+        *" $FREXPF_LIBM "*) ;;
+        *) LOG2F_LIBM="$LOG2F_LIBM $FREXPF_LIBM" ;;
+      esac
+      dnl Append $LOGF_LIBM to LOG2F_LIBM, avoiding gratuitous duplicates.
+      case " $LOG2F_LIBM " in
+        *" $LOGF_LIBM "*) ;;
+        *) LOG2F_LIBM="$LOG2F_LIBM $LOGF_LIBM" ;;
+      esac
+    fi
+  fi
+])
+
+dnl Test whether log2() works.
+dnl On OSF/1 5.1, log2f(-0.0f) is NaN.
+dnl On Cygwin 1.7.9, log2f(2^13) is not exactly 13.
+AC_DEFUN([gl_FUNC_LOG2F_WORKS],
+[
+  AC_REQUIRE([AC_PROG_CC])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+  AC_CACHE_CHECK([whether log2f works], [gl_cv_func_log2f_works],
+    [
+      AC_RUN_IFELSE(
+        [AC_LANG_SOURCE([[
+#include <math.h>
+#ifndef log2f /* for Cygwin 1.7.x */
+extern
+#ifdef __cplusplus
+"C"
+#endif
+float log2f (float);
+#endif
+volatile float x;
+volatile float y;
+int main ()
+{
+  int result = 0;
+  /* This test fails on OSF/1 5.1.  */
+  x = -0.0f;
+  y = log2f (x);
+  if (!(y + y == y))
+    result |= 1;
+  /* This test fails on Cygwin 1.7.9.  */
+  x = 8192.0f;
+  y = log2f (x);
+  if (!(y == 13.0f))
+    result |= 2;
+  return result;
+}
+]])],
+        [gl_cv_func_log2f_works=yes],
+        [gl_cv_func_log2f_works=no],
+        [case "$host_os" in
+           cygwin* | osf*) gl_cv_func_log2f_works="guessing no";;
+           *)              gl_cv_func_log2f_works="guessing yes";;
+         esac
+        ])
+    ])
+])
--- a/m4/math_h.m4
+++ b/m4/math_h.m4
@@ -1,4 +1,4 @@
-# math_h.m4 serial 101
+# math_h.m4 serial 102
 dnl Copyright (C) 2007-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -44,7 +44,8 @@
      expf expl exp2 exp2f exp2l expm1 expm1f expm1l
      fabsf fabsl floorf floorl fma fmaf fmal
      fmod fmodf fmodl frexpf frexpl hypotf hypotl
-     ldexpf ldexpl logb log logf logl log10f log10l log1p log1pf log1pl log2
+     ldexpf ldexpl
+     logb log logf logl log10f log10l log1p log1pf log1pl log2 log2f
      modf modff modfl powf
      remainder remainderf remainderl
      rint rintf rintl round roundf roundl sinf sinl sinhf sqrtf sqrtl
@@ -124,6 +125,7 @@
   GNULIB_LOG1PF=0;     AC_SUBST([GNULIB_LOG1PF])
   GNULIB_LOG1PL=0;     AC_SUBST([GNULIB_LOG1PL])
   GNULIB_LOG2=0;       AC_SUBST([GNULIB_LOG2])
+  GNULIB_LOG2F=0;      AC_SUBST([GNULIB_LOG2F])
   GNULIB_MODF=0;       AC_SUBST([GNULIB_MODF])
   GNULIB_MODFF=0;      AC_SUBST([GNULIB_MODFF])
   GNULIB_MODFL=0;      AC_SUBST([GNULIB_MODFL])
@@ -227,6 +229,7 @@
   HAVE_DECL_LOGL=1;            AC_SUBST([HAVE_DECL_LOGL])
   HAVE_DECL_LOG10L=1;          AC_SUBST([HAVE_DECL_LOG10L])
   HAVE_DECL_LOG2=1;            AC_SUBST([HAVE_DECL_LOG2])
+  HAVE_DECL_LOG2F=1;           AC_SUBST([HAVE_DECL_LOG2F])
   HAVE_DECL_REMAINDER=1;       AC_SUBST([HAVE_DECL_REMAINDER])
   HAVE_DECL_REMAINDERL=1;      AC_SUBST([HAVE_DECL_REMAINDERL])
   HAVE_DECL_RINTF=1;           AC_SUBST([HAVE_DECL_RINTF])
@@ -276,6 +279,7 @@
   REPLACE_LOG1PF=0;            AC_SUBST([REPLACE_LOG1PF])
   REPLACE_LOG1PL=0;            AC_SUBST([REPLACE_LOG1PL])
   REPLACE_LOG2=0;              AC_SUBST([REPLACE_LOG2])
+  REPLACE_LOG2F=0;             AC_SUBST([REPLACE_LOG2F])
   REPLACE_MODF=0;              AC_SUBST([REPLACE_MODF])
   REPLACE_MODFF=0;             AC_SUBST([REPLACE_MODFF])
   REPLACE_MODFL=0;             AC_SUBST([REPLACE_MODFL])
new file mode 100644
--- /dev/null
+++ b/modules/log2f
@@ -0,0 +1,35 @@
+Description:
+log2f() function: base 2 logarithm.
+
+Files:
+lib/log2f.c
+m4/log2f.m4
+
+Depends-on:
+math
+extensions
+log2            [test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1]
+isnanf          [test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1]
+frexpf          [test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1]
+logf            [test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1]
+
+configure.ac:
+gl_FUNC_LOG2F
+if test $HAVE_LOG2F = 0 || test $REPLACE_LOG2F = 1; then
+  AC_LIBOBJ([log2f])
+fi
+gl_MATH_MODULE_INDICATOR([log2f])
+
+Makefile.am:
+
+Include:
+<math.h>
+
+Link:
+$(LOG2F_LIBM)
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
--- a/modules/math
+++ b/modules/math
@@ -90,6 +90,7 @@
 	      -e 's/@''GNULIB_LOG1PF''@/$(GNULIB_LOG1PF)/g' \
 	      -e 's/@''GNULIB_LOG1PL''@/$(GNULIB_LOG1PL)/g' \
 	      -e 's/@''GNULIB_LOG2''@/$(GNULIB_LOG2)/g' \
+	      -e 's/@''GNULIB_LOG2F''@/$(GNULIB_LOG2F)/g' \
 	      -e 's/@''GNULIB_MODF''@/$(GNULIB_MODF)/g' \
 	      -e 's/@''GNULIB_MODFF''@/$(GNULIB_MODFF)/g' \
 	      -e 's/@''GNULIB_MODFL''@/$(GNULIB_MODFL)/g' \
@@ -193,6 +194,7 @@
 	      -e 's|@''HAVE_DECL_LOGL''@|$(HAVE_DECL_LOGL)|g' \
 	      -e 's|@''HAVE_DECL_LOG10L''@|$(HAVE_DECL_LOG10L)|g' \
 	      -e 's|@''HAVE_DECL_LOG2''@|$(HAVE_DECL_LOG2)|g' \
+	      -e 's|@''HAVE_DECL_LOG2F''@|$(HAVE_DECL_LOG2F)|g' \
 	      -e 's|@''HAVE_DECL_REMAINDER''@|$(HAVE_DECL_REMAINDER)|g' \
 	      -e 's|@''HAVE_DECL_REMAINDERL''@|$(HAVE_DECL_REMAINDERL)|g' \
 	      -e 's|@''HAVE_DECL_RINTF''@|$(HAVE_DECL_RINTF)|g' \
@@ -244,6 +246,7 @@
 	      -e 's|@''REPLACE_LOG1PF''@|$(REPLACE_LOG1PF)|g' \
 	      -e 's|@''REPLACE_LOG1PL''@|$(REPLACE_LOG1PL)|g' \
 	      -e 's|@''REPLACE_LOG2''@|$(REPLACE_LOG2)|g' \
+	      -e 's|@''REPLACE_LOG2F''@|$(REPLACE_LOG2F)|g' \
 	      -e 's|@''REPLACE_MODF''@|$(REPLACE_MODF)|g' \
 	      -e 's|@''REPLACE_MODFF''@|$(REPLACE_MODFF)|g' \
 	      -e 's|@''REPLACE_MODFL''@|$(REPLACE_MODFL)|g' \
--- a/tests/test-math-c++.cc
+++ b/tests/test-math-c++.cc
@@ -241,6 +241,9 @@
 SIGNATURE_CHECK (GNULIB_NAMESPACE::log10f, float, (float));
 #endif
 
+#if GNULIB_TEST_LOG2F
+SIGNATURE_CHECK (GNULIB_NAMESPACE::log2f, float, (float));
+#endif
 #if GNULIB_TEST_LOG2
 SIGNATURE_CHECK (GNULIB_NAMESPACE::log2, double, (double));
 #endif