From 58d8afd0545d6321272bb7fb243db3295c5fdeec Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 29 Feb 2012 15:46:25 +0100 Subject: [PATCH] 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. --- ChangeLog | 13 +++++ doc/posix-functions/hypotl.texi | 8 +-- lib/hypotl.c | 91 +++++++++++++++++++++++++++++++++ lib/math.in.h | 15 ++++++ m4/hypotl.m4 | 54 +++++++++++++++++++ m4/math_h.m4 | 6 ++- modules/hypotl | 38 ++++++++++++++ modules/math | 2 + tests/test-math-c++.cc | 4 ++ 9 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 lib/hypotl.c create mode 100644 m4/hypotl.m4 create mode 100644 modules/hypotl diff --git a/ChangeLog b/ChangeLog index 8f48d9a1c6..fb78b42d30 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2012-02-29 Bruno Haible + + 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 tcgetsid: fix cygwin header bug diff --git a/doc/posix-functions/hypotl.texi b/doc/posix-functions/hypotl.texi index d6bb099242..06bea0c953 100644 --- a/doc/posix-functions/hypotl.texi +++ b/doc/posix-functions/hypotl.texi @@ -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 index 0000000000..6d32e88e3b --- /dev/null +++ b/lib/hypotl.c @@ -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 . */ + +/* Written by Bruno Haible , 2012. */ + +#include + +/* Specification. */ +#include + +#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 diff --git a/lib/math.in.h b/lib/math.in.h index d82e720232..0abbd3a003 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -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 index 0000000000..57b472f8ef --- /dev/null +++ b/m4/hypotl.m4 @@ -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]) +]) diff --git a/m4/math_h.m4 b/m4/math_h.m4 index 26878d9036..c3176a4461 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -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 index 0000000000..156350c84e --- /dev/null +++ b/modules/hypotl @@ -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: + + +Link: +$(HYPOTL_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/math b/modules/math index 0472adbc6d..d2d3dc68e1 100644 --- a/modules/math +++ b/modules/math @@ -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' \ diff --git a/tests/test-math-c++.cc b/tests/test-math-c++.cc index 05ea2144e2..34d0b3b919 100644 --- a/tests/test-math-c++.cc +++ b/tests/test-math-c++.cc @@ -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)); -- 2.39.5