log: Work around OSF/1 5.1 bug.
authorBruno Haible <bruno@clisp.org>
Fri, 9 Mar 2012 22:55:13 +0000 (23:55 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 10 Mar 2012 09:37:40 +0000 (10:37 +0100)
* lib/math.in.h (log): New declaration.
* lib/log.c: New file.
* m4/log.m4 (gl_FUNC_LOG_WORKS): New macro.
(gl_FUNC_LOG): Invoke it. Set REPLACE_LOG.
* m4/math_h.m4 (gl_MATH_H): Test whether log is declared.
(gl_MATH_H_DEFAULTS): Initialize GNULIB_LOG, REPLACE_LOG.
* modules/math (Makefile.am): Substitute GNULIB_LOG, REPLACE_LOG.
* modules/log (Files): Add lib/log.c.
(Depends-on): Add math.
(configure.ac): If REPLACE_LOG is 1, compile an override.
* tests/test-math-c++.cc: Check the declaration of log.
* doc/posix-functions/log.texi: Mention the OSF/1 5.1 problem.

ChangeLog
doc/posix-functions/log.texi
lib/log.c [new file with mode: 0644]
lib/math.in.h
m4/log.m4
m4/math_h.m4
modules/log
modules/math
tests/test-math-c++.cc

index 1829df0..ef58c93 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2012-03-09  Bruno Haible  <bruno@clisp.org>
+
+       log: Work around OSF/1 5.1 bug.
+       * lib/math.in.h (log): New declaration.
+       * lib/log.c: New file.
+       * m4/log.m4 (gl_FUNC_LOG_WORKS): New macro.
+       (gl_FUNC_LOG): Invoke it. Set REPLACE_LOG.
+       * m4/math_h.m4 (gl_MATH_H): Test whether log is declared.
+       (gl_MATH_H_DEFAULTS): Initialize GNULIB_LOG, REPLACE_LOG.
+       * modules/math (Makefile.am): Substitute GNULIB_LOG, REPLACE_LOG.
+       * modules/log (Files): Add lib/log.c.
+       (Depends-on): Add math.
+       (configure.ac): If REPLACE_LOG is 1, compile an override.
+       * tests/test-math-c++.cc: Check the declaration of log.
+       * doc/posix-functions/log.texi: Mention the OSF/1 5.1 problem.
+
 2012-03-09  Jim Meyering  <meyering@redhat.com>
 
        readtokens.c: adjust wording in a comment
index 0f79f8b..2e67ced 100644 (file)
@@ -8,6 +8,9 @@ Gnulib module: log
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function returns a wrong value for a minus zero argument on some platforms:
+OSF/1 5.1.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/log.c b/lib/log.c
new file mode 100644 (file)
index 0000000..960ec82
--- /dev/null
+++ b/lib/log.c
@@ -0,0 +1,31 @@
+/* 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>
+
+double
+log (double x)
+#undef log
+{
+  /* Work around the OSF/1 5.1 bug.  */
+  if (x == 0.0)
+    /* Return -Infinity.  */
+    return -1.0 / 0.0;
+  return log (x);
+}
index 2dba028..b718ecf 100644 (file)
@@ -1136,6 +1136,26 @@ _GL_WARN_ON_USE (logf, "logf is unportable - "
 # endif
 #endif
 
+#if @GNULIB_LOG@
+# if @REPLACE_LOG@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef log
+#   define log rpl_log
+#  endif
+_GL_FUNCDECL_RPL (log, double, (double x));
+_GL_CXXALIAS_RPL (log, double, (double x));
+# else
+_GL_CXXALIAS_SYS (log, double, (double x));
+# endif
+_GL_CXXALIASWARN (log);
+#elif defined GNULIB_POSIXCHECK
+# undef log
+# if HAVE_RAW_DECL_LOG
+_GL_WARN_ON_USE (log, "log has portability problems - "
+                 "use gnulib module log for portability");
+# endif
+#endif
+
 #if @GNULIB_LOGL@
 # if !@HAVE_LOGL@ || !@HAVE_DECL_LOGL@
 #  undef logl
index 35a2cb4..bc1c378 100644 (file)
--- a/m4/log.m4
+++ b/m4/log.m4
@@ -1,4 +1,4 @@
-# log.m4 serial 1
+# log.m4 serial 2
 dnl Copyright (C) 2011-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,
@@ -8,4 +8,45 @@ AC_DEFUN([gl_FUNC_LOG],
 [
   dnl Determine LOG_LIBM.
   gl_COMMON_DOUBLE_MATHFUNC([log])
+
+  save_LIBS="$LIBS"
+  LIBS="$LIBS $LOG_LIBM"
+  gl_FUNC_LOG_WORKS
+  LIBS="$save_LIBS"
+  case "$gl_cv_func_log_works" in
+    *yes) ;;
+    *) REPLACE_LOG=1 ;;
+  esac
+])
+
+dnl Test whether log() works.
+dnl On OSF/1 5.1, log(-0.0) is NaN.
+AC_DEFUN([gl_FUNC_LOG_WORKS],
+[
+  AC_REQUIRE([AC_PROG_CC])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+  AC_CACHE_CHECK([whether log works], [gl_cv_func_log_works],
+    [
+      AC_RUN_IFELSE(
+        [AC_LANG_SOURCE([[
+#include <math.h>
+volatile double x;
+double y;
+int main ()
+{
+  x = -0.0;
+  y = log (x);
+  if (!(y + y == y))
+    return 1;
+  return 0;
+}
+]])],
+        [gl_cv_func_log_works=yes],
+        [gl_cv_func_log_works=no],
+        [case "$host_os" in
+           osf*) gl_cv_func_log_works="guessing no";;
+           *)    gl_cv_func_log_works="guessing yes";;
+         esac
+        ])
+    ])
 ])
index 953ad19..8c46e0c 100644 (file)
@@ -1,4 +1,4 @@
-# math_h.m4 serial 92
+# math_h.m4 serial 93
 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,7 @@ AC_DEFUN([gl_MATH_H],
      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 logf logl log10f log10l modf modff modfl powf
+     ldexpf ldexpl logb log logf logl log10f log10l modf modff modfl powf
      remainder remainderf remainderl
      rint rintf rintl round roundf roundl sinf sinl sinhf sqrtf sqrtl
      tanf tanl tanhf trunc truncf truncl])
@@ -114,6 +114,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   GNULIB_LDEXPF=0;     AC_SUBST([GNULIB_LDEXPF])
   GNULIB_LDEXPL=0;     AC_SUBST([GNULIB_LDEXPL])
   GNULIB_LOGB=0;       AC_SUBST([GNULIB_LOGB])
+  GNULIB_LOG=0;        AC_SUBST([GNULIB_LOG])
   GNULIB_LOGF=0;       AC_SUBST([GNULIB_LOGF])
   GNULIB_LOGL=0;       AC_SUBST([GNULIB_LOGL])
   GNULIB_LOG10F=0;     AC_SUBST([GNULIB_LOG10F])
@@ -259,6 +260,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   REPLACE_ISINF=0;             AC_SUBST([REPLACE_ISINF])
   REPLACE_ISNAN=0;             AC_SUBST([REPLACE_ISNAN])
   REPLACE_LDEXPL=0;            AC_SUBST([REPLACE_LDEXPL])
+  REPLACE_LOG=0;               AC_SUBST([REPLACE_LOG])
   REPLACE_MODF=0;              AC_SUBST([REPLACE_MODF])
   REPLACE_MODFF=0;             AC_SUBST([REPLACE_MODFF])
   REPLACE_MODFL=0;             AC_SUBST([REPLACE_MODFL])
index 9f320a3..824f8c9 100644 (file)
@@ -2,13 +2,19 @@ Description:
 log() function: natural logarithmic function.
 
 Files:
+lib/log.c
 m4/log.m4
 m4/mathfunc.m4
 
 Depends-on:
+math
 
 configure.ac:
 gl_FUNC_LOG
+if test $REPLACE_LOG = 1; then
+  AC_LIBOBJ([log])
+fi
+gl_MATH_MODULE_INDICATOR([log])
 
 Makefile.am:
 
index 7467171..cc68724 100644 (file)
@@ -81,6 +81,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's/@''GNULIB_LDEXPF''@/$(GNULIB_LDEXPF)/g' \
              -e 's/@''GNULIB_LDEXPL''@/$(GNULIB_LDEXPL)/g' \
              -e 's/@''GNULIB_LOGB''@/$(GNULIB_LOGB)/g' \
+             -e 's/@''GNULIB_LOG''@/$(GNULIB_LOG)/g' \
              -e 's/@''GNULIB_LOGF''@/$(GNULIB_LOGF)/g' \
              -e 's/@''GNULIB_LOGL''@/$(GNULIB_LOGL)/g' \
              -e 's/@''GNULIB_LOG10F''@/$(GNULIB_LOG10F)/g' \
@@ -228,6 +229,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''REPLACE_ISNAN''@|$(REPLACE_ISNAN)|g' \
              -e 's|@''REPLACE_ITOLD''@|$(REPLACE_ITOLD)|g' \
              -e 's|@''REPLACE_LDEXPL''@|$(REPLACE_LDEXPL)|g' \
+             -e 's|@''REPLACE_LOG''@|$(REPLACE_LOG)|g' \
              -e 's|@''REPLACE_MODF''@|$(REPLACE_MODF)|g' \
              -e 's|@''REPLACE_MODFF''@|$(REPLACE_MODFF)|g' \
              -e 's|@''REPLACE_MODFL''@|$(REPLACE_MODFL)|g' \
index ed061d8..a5619b2 100644 (file)
@@ -222,7 +222,9 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::log10l, long double, (long double));
 #if GNULIB_TEST_LOGF
 SIGNATURE_CHECK (GNULIB_NAMESPACE::logf, float, (float));
 #endif
-//SIGNATURE_CHECK (GNULIB_NAMESPACE::log, double, (double));
+#if GNULIB_TEST_LOG
+SIGNATURE_CHECK (GNULIB_NAMESPACE::log, double, (double));
+#endif
 #if GNULIB_TEST_LOGL
 SIGNATURE_CHECK (GNULIB_NAMESPACE::logl, long double, (long double));
 #endif