New module 'hypotl'.
authorBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 14:46:25 +0000 (15:46 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 14:49:02 +0000 (15:49 +0100)
* lib/math.in.h (hypotl): New declaration.
* lib/hypotl.c: New file.
* m4/hypotl.m4: New file.
* m4/math_h.m4 (gl_MATH_H): Test whether hypotf is declared.
(gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOTL, HAVE_HYPOTL.
* modules/math (Makefile.am): Substitute GNULIB_HYPOTL, HAVE_HYPOTL.
* modules/hypotl: New file.
* tests/test-math-c++.cc: Check the hypotl declaration.
* doc/posix-functions/hypotl.texi: Mention the new module.

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

index 8f48d9a..fb78b42 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2012-02-29  Bruno Haible  <bruno@clisp.org>
+
+       New module 'hypotl'.
+       * lib/math.in.h (hypotl): New declaration.
+       * lib/hypotl.c: New file.
+       * m4/hypotl.m4: New file.
+       * m4/math_h.m4 (gl_MATH_H): Test whether hypotf is declared.
+       (gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOTL, HAVE_HYPOTL.
+       * modules/math (Makefile.am): Substitute GNULIB_HYPOTL, HAVE_HYPOTL.
+       * modules/hypotl: New file.
+       * tests/test-math-c++.cc: Check the hypotl declaration.
+       * doc/posix-functions/hypotl.texi: Mention the new module.
+
 2012-02-29  Eric Blake  <eblake@redhat.com>
 
        tcgetsid: fix cygwin header bug
index d6bb099..06bea0c 100644 (file)
@@ -4,15 +4,15 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/hypotl.html}
 
-Gnulib module: ---
+Gnulib module: hypotl
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, MSVC 9, Interix 3.5, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, MSVC 9, Interix 3.5, BeOS.
 @end itemize
diff --git a/lib/hypotl.c b/lib/hypotl.c
new file mode 100644 (file)
index 0000000..6d32e88
--- /dev/null
@@ -0,0 +1,91 @@
+/* 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>
+
+#if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
+
+long double
+hypotl (long double x, long double y)
+{
+  return hypot (x, y);
+}
+
+#else
+
+long double
+hypotl (long double x, long double y)
+{
+  if (isfinite (x) && isfinite (y))
+    {
+      /* Determine absolute values.  */
+      x = fabsl (x);
+      y = fabsl (y);
+
+      {
+        /* Find the bigger and the smaller one.  */
+        long double a;
+        long double b;
+
+        if (x >= y)
+          {
+            a = x;
+            b = y;
+          }
+        else
+          {
+            a = y;
+            b = x;
+          }
+        /* Now 0 <= b <= a.  */
+
+        {
+          int e;
+          long double an;
+          long double bn;
+
+          /* Write a = an * 2^e, b = bn * 2^e with 0 <= bn <= an < 1.  */
+          an = frexpl (a, &e);
+          bn = ldexpl (b, - e);
+
+          {
+            long double cn;
+
+            /* Through the normalization, no unneeded overflow or underflow
+               will occur here.  */
+            cn = sqrtl (an * an + bn * bn);
+            return ldexpl (cn, e);
+          }
+        }
+      }
+    }
+  else
+    {
+      if (isinf (x) || isinf (y))
+        /* x or y is infinite.  Return +Infinity.  */
+        return HUGE_VALL;
+      else
+        /* x or y is NaN.  Return NaN.  */
+        return x + y;
+    }
+}
+
+#endif
index d82e720..0abbd3a 100644 (file)
@@ -832,6 +832,21 @@ _GL_WARN_ON_USE (hypotf, "hypotf is unportable - "
 # 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));
+# endif
+_GL_CXXALIAS_SYS (hypotl, long double, (long double x, long double y));
+_GL_CXXALIASWARN (hypotl);
+#elif defined GNULIB_POSIXCHECK
+# undef hypotl
+# if HAVE_RAW_DECL_HYPOTL
+_GL_WARN_ON_USE (hypotl, "hypotl is unportable - "
+                 "use gnulib module hypotl for portability");
+# endif
+#endif
+
 
 /* Return x * 2^exp.  */
 #if @GNULIB_LDEXPF@
diff --git a/m4/hypotl.m4 b/m4/hypotl.m4
new file mode 100644 (file)
index 0000000..57b472f
--- /dev/null
@@ -0,0 +1,54 @@
+# hypotl.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.
+
+AC_DEFUN([gl_FUNC_HYPOTL],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_FUNC_HYPOT])
+
+  dnl Test whether hypotl() exists. Assume that hypotl(), if it exists, is
+  dnl defined in the same library as hypot().
+  save_LIBS="$LIBS"
+  LIBS="$LIBS $HYPOT_LIBM"
+  AC_CHECK_FUNCS([hypotl])
+  LIBS="$save_LIBS"
+  if test $ac_cv_func_hypotl = yes; then
+    HYPOTL_LIBM="$HYPOT_LIBM"
+  else
+    HAVE_HYPOTL=0
+    dnl Find libraries needed to link lib/hypotl.c.
+    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
+      HYPOTL_LIBM="$HYPOT_LIBM"
+    else
+      AC_REQUIRE([gl_FUNC_FABSL])
+      AC_REQUIRE([gl_FUNC_FREXPL])
+      AC_REQUIRE([gl_FUNC_LDEXPL])
+      AC_REQUIRE([gl_FUNC_SQRTL])
+      HYPOTL_LIBM=
+      dnl Append $FABSL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
+      case " $HYPOTL_LIBM " in
+        *" $FABSL_LIBM "*) ;;
+        *) HYPOTL_LIBM="$HYPOTL_LIBM $FABSL_LIBM" ;;
+      esac
+      dnl Append $FREXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
+      case " $HYPOTL_LIBM " in
+        *" $FREXPL_LIBM "*) ;;
+        *) HYPOTL_LIBM="$HYPOTL_LIBM $FREXPL_LIBM" ;;
+      esac
+      dnl Append $LDEXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
+      case " $HYPOTL_LIBM " in
+        *" $LDEXPL_LIBM "*) ;;
+        *) HYPOTL_LIBM="$HYPOTL_LIBM $LDEXPL_LIBM" ;;
+      esac
+      dnl Append $SQRTL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
+      case " $HYPOTL_LIBM " in
+        *" $SQRTL_LIBM "*) ;;
+        *) HYPOTL_LIBM="$HYPOTL_LIBM $SQRTL_LIBM" ;;
+      esac
+    fi
+  fi
+  AC_SUBST([HYPOTL_LIBM])
+])
index 26878d9..c3176a4 100644 (file)
@@ -1,4 +1,4 @@
-# math_h.m4 serial 72
+# math_h.m4 serial 73
 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,
@@ -42,7 +42,7 @@ AC_DEFUN([gl_MATH_H],
     [acosf acosl asinf asinl atanf atanl
      ceilf ceill copysign copysignf copysignl cosf cosl coshf
      expf expl fabsf fabsl floorf floorl fma fmaf fmal
-     fmod fmodf fmodl frexpf frexpl hypotf
+     fmod fmodf fmodl frexpf frexpl hypotf hypotl
      ldexpf ldexpl logb logf logl log10f log10l modf modff modfl powf
      remainder remainderf remainderl
      rint rintf rintl round roundf roundl sinf sinl sinhf sqrtf sqrtl
@@ -93,6 +93,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   GNULIB_FREXP=0;      AC_SUBST([GNULIB_FREXP])
   GNULIB_FREXPL=0;     AC_SUBST([GNULIB_FREXPL])
   GNULIB_HYPOTF=0;     AC_SUBST([GNULIB_HYPOTF])
+  GNULIB_HYPOTL=0;     AC_SUBST([GNULIB_HYPOTL])
   GNULIB_ISFINITE=0;   AC_SUBST([GNULIB_ISFINITE])
   GNULIB_ISINF=0;      AC_SUBST([GNULIB_ISINF])
   GNULIB_ISNAN=0;      AC_SUBST([GNULIB_ISNAN])
@@ -156,6 +157,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   HAVE_FMODL=1;                AC_SUBST([HAVE_FMODL])
   HAVE_FREXPF=1;               AC_SUBST([HAVE_FREXPF])
   HAVE_HYPOTF=1;               AC_SUBST([HAVE_HYPOTF])
+  HAVE_HYPOTL=1;               AC_SUBST([HAVE_HYPOTL])
   HAVE_ISNANF=1;               AC_SUBST([HAVE_ISNANF])
   HAVE_ISNAND=1;               AC_SUBST([HAVE_ISNAND])
   HAVE_ISNANL=1;               AC_SUBST([HAVE_ISNANL])
diff --git a/modules/hypotl b/modules/hypotl
new file mode 100644 (file)
index 0000000..156350c
--- /dev/null
@@ -0,0 +1,38 @@
+Description:
+hypotl() function: length of a vector in the plane.
+
+Files:
+lib/hypotl.c
+m4/hypotl.m4
+m4/mathfunc.m4
+
+Depends-on:
+math
+hypot           [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1]
+isfinite        [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+fabsl           [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+frexpl          [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+ldexpl          [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+sqrtl           [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+isinf           [test $HAVE_HYPOTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+
+configure.ac:
+gl_FUNC_HYPOTL
+if test $HAVE_HYPOTL = 0; then
+  AC_LIBOBJ([hypotl])
+fi
+gl_MATH_MODULE_INDICATOR([hypotl])
+
+Makefile.am:
+
+Include:
+<math.h>
+
+Link:
+$(HYPOTL_LIBM)
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
index 0472adb..d2d3dc6 100644 (file)
@@ -61,6 +61,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's/@''GNULIB_FREXP''@/$(GNULIB_FREXP)/g' \
              -e 's/@''GNULIB_FREXPL''@/$(GNULIB_FREXPL)/g' \
              -e 's/@''GNULIB_HYPOTF''@/$(GNULIB_HYPOTF)/g' \
+             -e 's/@''GNULIB_HYPOTL''@/$(GNULIB_HYPOTL)/g' \
              -e 's/@''GNULIB_ISFINITE''@/$(GNULIB_ISFINITE)/g' \
              -e 's/@''GNULIB_ISINF''@/$(GNULIB_ISINF)/g' \
              -e 's/@''GNULIB_ISNAN''@/$(GNULIB_ISNAN)/g' \
@@ -124,6 +125,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''HAVE_FMODL''@|$(HAVE_FMODL)|g' \
              -e 's|@''HAVE_FREXPF''@|$(HAVE_FREXPF)|g' \
              -e 's|@''HAVE_HYPOTF''@|$(HAVE_HYPOTF)|g' \
+             -e 's|@''HAVE_HYPOTL''@|$(HAVE_HYPOTL)|g' \
              -e 's|@''HAVE_ISNANF''@|$(HAVE_ISNANF)|g' \
              -e 's|@''HAVE_ISNAND''@|$(HAVE_ISNAND)|g' \
              -e 's|@''HAVE_ISNANL''@|$(HAVE_ISNANL)|g' \
index 05ea214..34d0b3b 100644 (file)
@@ -159,6 +159,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::frexpl, long double, (long double, int *));
 SIGNATURE_CHECK (GNULIB_NAMESPACE::hypotf, float, (float, float));
 #endif
 //SIGNATURE_CHECK (GNULIB_NAMESPACE::hypot, double, (double, double));
+#if GNULIB_TEST_HYPOTL
+SIGNATURE_CHECK (GNULIB_NAMESPACE::hypotl, long double,
+                 (long double, long double));
+#endif
 
 //SIGNATURE_CHECK (GNULIB_NAMESPACE::j0, double, (double));