hypot-ieee: Work around test failure on OSF/1 and native Windows.
authorBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 19:00:38 +0000 (20:00 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 19:51:42 +0000 (20:51 +0100)
* lib/math.in.h (hypot): New declaration.
* lib/hypot.c: New file.
* m4/hypot-ieee.m4: New file.
* m4/hypot.m4 (gl_FUNC_HYPOT): If gl_FUNC_HYPOT_IEEE is present, test
whether hypot works with mixed NaN and Infinity arguments. Replace it
if not.
* m4/math_h.m4 (gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOT,
REPLACE_HYPOT.
* modules/math (Makefile.am): Substitute GNULIB_HYPOT, REPLACE_HYPOT.
* modules/hypot (Files): Add lib/hypot.c.
(Depends-on): Add dependencies.
(configure.ac): Arrange to compile replacement if REPLACE_HYPOT is 1.
* modules/hypot-ieee (Files): Add m4/hypot-ieee.m4.
(configure.ac): Invoke gl_FUNC_HYPOT_IEEE.
* tests/test-math-c++.cc: Check the declaration of hypot.
* doc/posix-functions/hypot.texi: Mention the hypot-ieee module.

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

index 8f610ec..ab60280 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2012-02-29  Bruno Haible  <bruno@clisp.org>
 
+       hypot-ieee: Work around test failure on OSF/1 and native Windows.
+       * lib/math.in.h (hypot): New declaration.
+       * lib/hypot.c: New file.
+       * m4/hypot-ieee.m4: New file.
+       * m4/hypot.m4 (gl_FUNC_HYPOT): If gl_FUNC_HYPOT_IEEE is present, test
+       whether hypot works with mixed NaN and Infinity arguments. Replace it
+       if not.
+       * m4/math_h.m4 (gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOT,
+       REPLACE_HYPOT.
+       * modules/math (Makefile.am): Substitute GNULIB_HYPOT, REPLACE_HYPOT.
+       * modules/hypot (Files): Add lib/hypot.c.
+       (Depends-on): Add dependencies.
+       (configure.ac): Arrange to compile replacement if REPLACE_HYPOT is 1.
+       * modules/hypot-ieee (Files): Add m4/hypot-ieee.m4.
+       (configure.ac): Invoke gl_FUNC_HYPOT_IEEE.
+       * tests/test-math-c++.cc: Check the declaration of hypot.
+       * doc/posix-functions/hypot.texi: Mention the hypot-ieee module.
+
        Tests for module 'hypotl-ieee'.
        * modules/hypotl-ieee-tests: New file.
        * tests/test-hypotl-ieee.c: New file.
index 84d4b9d..eda9a6b 100644 (file)
@@ -4,12 +4,20 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/hypot.html}
 
-Gnulib module: hypot
+Gnulib module: hypot or hypot-ieee
 
-Portability problems fixed by Gnulib:
+Portability problems fixed by either Gnulib module @code{hypot} or @code{hypot-ieee}:
 @itemize
 @end itemize
 
+Portability problems fixed by Gnulib module @code{hypot-ieee}:
+@itemize
+@item
+When the arguments are mixed NaN and Infinity, this function returns a wrong
+value on some platforms:
+OSF/1 5.1, mingw, MSVC 9.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @end itemize
diff --git a/lib/hypot.c b/lib/hypot.c
new file mode 100644 (file)
index 0000000..9665c83
--- /dev/null
@@ -0,0 +1,79 @@
+/* Hypotenuse of a right-angled triangle.
+   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/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2012.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <math.h>
+
+double
+hypot (double x, double y)
+{
+  if (isfinite (x) && isfinite (y))
+    {
+      /* Determine absolute values.  */
+      x = fabs (x);
+      y = fabs (y);
+
+      {
+        /* Find the bigger and the smaller one.  */
+        double a;
+        double b;
+
+        if (x >= y)
+          {
+            a = x;
+            b = y;
+          }
+        else
+          {
+            a = y;
+            b = x;
+          }
+        /* Now 0 <= b <= a.  */
+
+        {
+          int e;
+          double an;
+          double bn;
+
+          /* Write a = an * 2^e, b = bn * 2^e with 0 <= bn <= an < 1.  */
+          an = frexp (a, &e);
+          bn = ldexp (b, - e);
+
+          {
+            double cn;
+
+            /* Through the normalization, no unneeded overflow or underflow
+               will occur here.  */
+            cn = sqrt (an * an + bn * bn);
+            return ldexp (cn, e);
+          }
+        }
+      }
+    }
+  else
+    {
+      if (isinf (x) || isinf (y))
+        /* x or y is infinite.  Return +Infinity.  */
+        return HUGE_VAL;
+      else
+        /* x or y is NaN.  Return NaN.  */
+        return x + y;
+    }
+}
index 0abbd3a..80b9a8d 100644 (file)
@@ -833,6 +833,27 @@ _GL_WARN_ON_USE (hypotf, "hypotf is unportable - "
 #endif
 
 /* Return sqrt(x^2+y^2).  */
+#if @GNULIB_HYPOT@
+# if @REPLACE_HYPOT@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef hypot
+#   define hypot rpl_hypot
+#  endif
+_GL_FUNCDECL_RPL (hypot, double, (double x, double y));
+_GL_CXXALIAS_RPL (hypot, double, (double x, double y));
+# else
+_GL_CXXALIAS_SYS (hypot, double, (double x, double y));
+# endif
+_GL_CXXALIASWARN (hypot);
+#elif defined GNULIB_POSIXCHECK
+# undef hypot
+# if HAVE_RAW_DECL_HYPOT
+_GL_WARN_ON_USE (hypotf, "hypot has portability problems - "
+                 "use gnulib module hypot for portability");
+# endif
+#endif
+
+/* Return sqrt(x^2+y^2).  */
 #if @GNULIB_HYPOTL@
 # if !@HAVE_HYPOTL@
 _GL_FUNCDECL_SYS (hypotl, long double, (long double x, long double y));
diff --git a/m4/hypot-ieee.m4 b/m4/hypot-ieee.m4
new file mode 100644 (file)
index 0000000..4b88470
--- /dev/null
@@ -0,0 +1,15 @@
+# hypot-ieee.m4 serial 1
+dnl Copyright (C) 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.
+
+dnl This macro is in a separate file (not in hypot.m4 and not inlined in the
+dnl module description), so that gl_FUNC_HYPOT can test whether 'aclocal' has
+dnl found uses of this macro.
+
+AC_DEFUN([gl_FUNC_HYPOT_IEEE],
+[
+  m4_divert_text([INIT_PREPARE], [gl_hypot_required=ieee])
+  AC_REQUIRE([gl_FUNC_HYPOT])
+])
index b8faf88..1837a01 100644 (file)
@@ -1,4 +1,4 @@
-# hypot.m4 serial 1
+# hypot.m4 serial 2
 dnl Copyright (C) 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,
@@ -6,6 +6,85 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_HYPOT],
 [
+  m4_divert_text([DEFAULTS], [gl_hypot_required=plain])
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+
   dnl Determine HYPOT_LIBM.
   gl_COMMON_DOUBLE_MATHFUNC([hypot])
+
+  m4_ifdef([gl_FUNC_HYPOT_IEEE], [
+    if test $gl_hypot_required = ieee && test $REPLACE_HYPOT = 0; then
+      AC_CACHE_CHECK([whether hypot works according to ISO C 99 with IEC 60559],
+        [gl_cv_func_hypot_ieee],
+        [
+          save_LIBS="$LIBS"
+          LIBS="$LIBS $HYPOT_LIBM"
+          AC_RUN_IFELSE(
+            [AC_LANG_SOURCE([[
+#ifndef __NO_MATH_INLINES
+# define __NO_MATH_INLINES 1 /* for glibc */
+#endif
+#include <math.h>
+/* Compare two numbers with ==.
+   This is a separate function because IRIX 6.5 "cc -O" miscompiles an
+   'x == x' test.  */
+static int
+numeric_equal (double x, double y)
+{
+  return x == y;
+}
+static double dummy (double x, double y) { return 0; }
+double zero;
+double one = 1.0;
+int main (int argc, char *argv[])
+{
+  double (*my_hypot) (double, double) = argc ? hypot : dummy;
+  double f;
+  /* Test hypot(NaN,Infinity).
+     This test fails on OSF/1 5.1 and native Windows.  */
+  f = my_hypot (zero / zero, one / zero);
+  if (!numeric_equal (f, f))
+    return 1;
+  return 0;
+}
+            ]])],
+            [gl_cv_func_hypot_ieee=yes],
+            [gl_cv_func_hypot_ieee=no],
+            [gl_cv_func_hypot_ieee="guessing no"])
+          LIBS="$save_LIBS"
+        ])
+      case "$gl_cv_func_hypot_ieee" in
+        *yes) ;;
+        *) REPLACE_HYPOT=1 ;;
+      esac
+    fi
+  ])
+  if test $REPLACE_HYPOT = 1; then
+    dnl Find libraries needed to link lib/hypot.c.
+    AC_REQUIRE([gl_FUNC_FABS])
+    AC_REQUIRE([gl_FUNC_FREXP])
+    AC_REQUIRE([gl_FUNC_LDEXP])
+    AC_REQUIRE([gl_FUNC_SQRT])
+    HYPOT_LIBM=
+    dnl Append $FABS_LIBM to HYPOT_LIBM, avoiding gratuitous duplicates.
+    case " $HYPOT_LIBM " in
+      *" $FABS_LIBM "*) ;;
+      *) HYPOT_LIBM="$HYPOT_LIBM $FABS_LIBM" ;;
+    esac
+    dnl Append $FREXP_LIBM to HYPOT_LIBM, avoiding gratuitous duplicates.
+    case " $HYPOT_LIBM " in
+      *" $FREXP_LIBM "*) ;;
+      *) HYPOT_LIBM="$HYPOT_LIBM $FREXP_LIBM" ;;
+    esac
+    dnl Append $LDEXP_LIBM to HYPOT_LIBM, avoiding gratuitous duplicates.
+    case " $HYPOT_LIBM " in
+      *" $LDEXP_LIBM "*) ;;
+      *) HYPOT_LIBM="$HYPOT_LIBM $LDEXP_LIBM" ;;
+    esac
+    dnl Append $SQRT_LIBM to HYPOT_LIBM, avoiding gratuitous duplicates.
+    case " $HYPOT_LIBM " in
+      *" $SQRT_LIBM "*) ;;
+      *) HYPOT_LIBM="$HYPOT_LIBM $SQRT_LIBM" ;;
+    esac
+  fi
 ])
index c3176a4..6ea39bb 100644 (file)
@@ -1,4 +1,4 @@
-# math_h.m4 serial 73
+# math_h.m4 serial 74
 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,
@@ -92,6 +92,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   GNULIB_FREXPF=0;     AC_SUBST([GNULIB_FREXPF])
   GNULIB_FREXP=0;      AC_SUBST([GNULIB_FREXP])
   GNULIB_FREXPL=0;     AC_SUBST([GNULIB_FREXPL])
+  GNULIB_HYPOT=0;      AC_SUBST([GNULIB_HYPOT])
   GNULIB_HYPOTF=0;     AC_SUBST([GNULIB_HYPOTF])
   GNULIB_HYPOTL=0;     AC_SUBST([GNULIB_HYPOTL])
   GNULIB_ISFINITE=0;   AC_SUBST([GNULIB_ISFINITE])
@@ -224,6 +225,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   REPLACE_FREXP=0;             AC_SUBST([REPLACE_FREXP])
   REPLACE_FREXPL=0;            AC_SUBST([REPLACE_FREXPL])
   REPLACE_HUGE_VAL=0;          AC_SUBST([REPLACE_HUGE_VAL])
+  REPLACE_HYPOT=0;             AC_SUBST([REPLACE_HYPOT])
   REPLACE_HYPOTF=0;            AC_SUBST([REPLACE_HYPOTF])
   REPLACE_ISFINITE=0;          AC_SUBST([REPLACE_ISFINITE])
   REPLACE_ISINF=0;             AC_SUBST([REPLACE_ISINF])
index de856ec..5d1414e 100644 (file)
@@ -2,13 +2,25 @@ Description:
 hypot() function: length of a vector in the plane.
 
 Files:
+lib/hypot.c
 m4/hypot.m4
 m4/mathfunc.m4
 
 Depends-on:
+math
+isfinite        [test $REPLACE_HYPOT = 1]
+fabs            [test $REPLACE_HYPOT = 1]
+frexp           [test $REPLACE_HYPOT = 1]
+ldexp           [test $REPLACE_HYPOT = 1]
+sqrt            [test $REPLACE_HYPOT = 1]
+isinf           [test $REPLACE_HYPOT = 1]
 
 configure.ac:
 gl_FUNC_HYPOT
+if test $REPLACE_HYPOT = 1; then
+  AC_LIBOBJ([hypot])
+fi
+gl_MATH_MODULE_INDICATOR([hypot])
 
 Makefile.am:
 
index 6b9f2ac..d29d8bc 100644 (file)
@@ -2,12 +2,14 @@ Description:
 hypot() function according to ISO C 99 with IEC 60559.
 
 Files:
+m4/hypot-ieee.m4
 
 Depends-on:
 hypot
 fpieee
 
 configure.ac:
+gl_FUNC_HYPOT_IEEE
 
 Makefile.am:
 
index d2d3dc6..63fd0d0 100644 (file)
@@ -60,6 +60,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's/@''GNULIB_FREXPF''@/$(GNULIB_FREXPF)/g' \
              -e 's/@''GNULIB_FREXP''@/$(GNULIB_FREXP)/g' \
              -e 's/@''GNULIB_FREXPL''@/$(GNULIB_FREXPL)/g' \
+             -e 's/@''GNULIB_HYPOT''@/$(GNULIB_HYPOT)/g' \
              -e 's/@''GNULIB_HYPOTF''@/$(GNULIB_HYPOTF)/g' \
              -e 's/@''GNULIB_HYPOTL''@/$(GNULIB_HYPOTL)/g' \
              -e 's/@''GNULIB_ISFINITE''@/$(GNULIB_ISFINITE)/g' \
@@ -193,6 +194,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''REPLACE_FREXP''@|$(REPLACE_FREXP)|g' \
              -e 's|@''REPLACE_FREXPL''@|$(REPLACE_FREXPL)|g' \
              -e 's|@''REPLACE_HUGE_VAL''@|$(REPLACE_HUGE_VAL)|g' \
+             -e 's|@''REPLACE_HYPOT''@|$(REPLACE_HYPOT)|g' \
              -e 's|@''REPLACE_HYPOTF''@|$(REPLACE_HYPOTF)|g' \
              -e 's|@''REPLACE_ISFINITE''@|$(REPLACE_ISFINITE)|g' \
              -e 's|@''REPLACE_ISINF''@|$(REPLACE_ISINF)|g' \
index 34d0b3b..47dc173 100644 (file)
@@ -158,7 +158,9 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::frexpl, long double, (long double, int *));
 #if GNULIB_TEST_HYPOTF
 SIGNATURE_CHECK (GNULIB_NAMESPACE::hypotf, float, (float, float));
 #endif
-//SIGNATURE_CHECK (GNULIB_NAMESPACE::hypot, double, (double, double));
+#if GNULIB_TEST_HYPOT
+SIGNATURE_CHECK (GNULIB_NAMESPACE::hypot, double, (double, double));
+#endif
 #if GNULIB_TEST_HYPOTL
 SIGNATURE_CHECK (GNULIB_NAMESPACE::hypotl, long double,
                  (long double, long double));