From 178834fedcf12d09655bb0c11d8ff74b650989e6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 29 Feb 2012 13:22:12 +0100 Subject: [PATCH] New module 'hypotf'. * lib/math.in.h (hypotf): New declaration. * lib/hypotf.c: New file. * m4/hypotf.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whether hypotf is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOTF, HAVE_HYPOTF, REPLACE_HYPOTF. * modules/math (Makefile.am): Substitute GNULIB_HYPOTF, HAVE_HYPOTF, REPLACE_HYPOTF. * modules/hypotf: New file. * tests/test-math-c++.cc: Check the hypotf declaration. * doc/posix-functions/hypotf.texi: Mention the new module. --- ChangeLog | 13 +++++++ doc/posix-functions/hypotf.texi | 11 ++++-- lib/hypotf.c | 26 +++++++++++++ lib/math.in.h | 25 +++++++++++++ m4/hypotf.m4 | 83 +++++++++++++++++++++++++++++++++++++++++ m4/math_h.m4 | 7 +++- modules/hypotf | 32 ++++++++++++++++ modules/math | 3 ++ tests/test-math-c++.cc | 3 ++ 9 files changed, 197 insertions(+), 6 deletions(-) create mode 100644 lib/hypotf.c create mode 100644 m4/hypotf.m4 create mode 100644 modules/hypotf diff --git a/ChangeLog b/ChangeLog index 7a727f1a8..a566d06f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2012-02-29 Bruno Haible + New module 'hypotf'. + * lib/math.in.h (hypotf): New declaration. + * lib/hypotf.c: New file. + * m4/hypotf.m4: New file. + * m4/math_h.m4 (gl_MATH_H): Test whether hypotf is declared. + (gl_MATH_H_DEFAULTS): Initialize GNULIB_HYPOTF, HAVE_HYPOTF, + REPLACE_HYPOTF. + * modules/math (Makefile.am): Substitute GNULIB_HYPOTF, HAVE_HYPOTF, + REPLACE_HYPOTF. + * modules/hypotf: New file. + * tests/test-math-c++.cc: Check the hypotf declaration. + * doc/posix-functions/hypotf.texi: Mention the new module. + hypot: Prepare for hypotf module. * m4/hypot.m4: New file. * modules/hypot (Files): Add m4/hypot.m4. diff --git a/doc/posix-functions/hypotf.texi b/doc/posix-functions/hypotf.texi index c964e45f3..debdc5e1d 100644 --- a/doc/posix-functions/hypotf.texi +++ b/doc/posix-functions/hypotf.texi @@ -4,15 +4,18 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/hypotf.html} -Gnulib module: --- +Gnulib module: hypotf Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 9, MSVC 9. +@item +This function produces wrong values on some platforms: +NetBSD 5.1, OpenBSD 4.9. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 9, MSVC 9. @end itemize diff --git a/lib/hypotf.c b/lib/hypotf.c new file mode 100644 index 000000000..ad5aa8907 --- /dev/null +++ b/lib/hypotf.c @@ -0,0 +1,26 @@ +/* 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 . */ + +#include + +/* Specification. */ +#include + +float +hypotf (float x, float y) +{ + return (float) hypot ((double) x, (double) y); +} diff --git a/lib/math.in.h b/lib/math.in.h index a2d1489a4..d82e72023 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -808,6 +808,31 @@ _GL_WARN_ON_USE (frexpl, "frexpl is unportable - " #endif +/* Return sqrt(x^2+y^2). */ +#if @GNULIB_HYPOTF@ +# if @REPLACE_HYPOTF@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef hypotf +# define hypotf rpl_hypotf +# endif +_GL_FUNCDECL_RPL (hypotf, float, (float x, float y)); +_GL_CXXALIAS_RPL (hypotf, float, (float x, float y)); +# else +# if !@HAVE_HYPOTF@ +_GL_FUNCDECL_SYS (hypotf, float, (float x, float y)); +# endif +_GL_CXXALIAS_SYS (hypotf, float, (float x, float y)); +# endif +_GL_CXXALIASWARN (hypotf); +#elif defined GNULIB_POSIXCHECK +# undef hypotf +# if HAVE_RAW_DECL_HYPOTF +_GL_WARN_ON_USE (hypotf, "hypotf is unportable - " + "use gnulib module hypotf for portability"); +# endif +#endif + + /* Return x * 2^exp. */ #if @GNULIB_LDEXPF@ # if !@HAVE_LDEXPF@ diff --git a/m4/hypotf.m4 b/m4/hypotf.m4 new file mode 100644 index 000000000..67eb5e50a --- /dev/null +++ b/m4/hypotf.m4 @@ -0,0 +1,83 @@ +# hypotf.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_HYPOTF], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + AC_REQUIRE([gl_FUNC_HYPOT]) + + dnl Test whether hypotf() exists. Assume that hypotf(), if it exists, is + dnl defined in the same library as fmod(). + save_LIBS="$LIBS" + LIBS="$LIBS $HYPOT_LIBM" + AC_CHECK_FUNCS([hypotf]) + LIBS="$save_LIBS" + if test $ac_cv_func_hypotf = yes; then + HYPOTF_LIBM="$HYPOT_LIBM" + save_LIBS="$LIBS" + LIBS="$LIBS $HYPOTF_LIBM" + gl_FUNC_HYPOTF_WORKS + LIBS="$save_LIBS" + case "$gl_cv_func_hypotf_works" in + *yes) ;; + *) REPLACE_HYPOTF=1 ;; + esac + else + HAVE_HYPOTF=0 + fi + if test $HAVE_HYPOTF = 0 || test $REPLACE_HYPOTF = 1; then + dnl Find libraries needed to link lib/hypotf.c. + HYPOTF_LIBM="$HYPOT_LIBM" + fi + AC_SUBST([HYPOTF_LIBM]) +]) + +dnl Test whether hypotf() works. +dnl It returns wrong values on NetBSD 5.1/x86_64 and OpenBSD 4.9/x86. +AC_DEFUN([gl_FUNC_HYPOTF_WORKS], +[ + AC_REQUIRE([AC_PROG_CC]) + AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles + AC_CACHE_CHECK([whether hypotf works], [gl_cv_func_hypotf_works], + [ + AC_RUN_IFELSE( + [AC_LANG_SOURCE([[ +#include +#include +volatile float x; +volatile float y; +volatile float z; +int main () +{ + int result = 0; + /* This test fails on NetBSD 5.1. */ + { + x = FLT_MIN * 2.0f; + y = FLT_MIN * 3.0f; + z = hypotf (x, y); + if (!(z >= FLT_MIN * 2.0f && z <= FLT_MIN * 4.0f)) + result |= 1; + } + /* This test fails on OpenBSD 4.9. */ + { + x = FLT_MAX; + y = FLT_MAX * 0.5f; + z = hypotf (x, y); + if (!(z > 0 && z == z + z)) + result |= 2; + } + return result; +} +]])], + [gl_cv_func_hypotf_works=yes], + [gl_cv_func_hypotf_works=no], + [case "$host_os" in + netbsd* | openbsd*) gl_cv_func_hypotf_works="guessing no";; + *) gl_cv_func_hypotf_works="guessing yes";; + esac + ]) + ]) +]) diff --git a/m4/math_h.m4 b/m4/math_h.m4 index 6f9389dc1..26878d903 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,4 +1,4 @@ -# math_h.m4 serial 71 +# math_h.m4 serial 72 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 + fmod fmodf fmodl frexpf frexpl hypotf 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 @@ -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_HYPOTF=0; AC_SUBST([GNULIB_HYPOTF]) GNULIB_ISFINITE=0; AC_SUBST([GNULIB_ISFINITE]) GNULIB_ISINF=0; AC_SUBST([GNULIB_ISINF]) GNULIB_ISNAN=0; AC_SUBST([GNULIB_ISNAN]) @@ -154,6 +155,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], HAVE_FMODF=1; AC_SUBST([HAVE_FMODF]) HAVE_FMODL=1; AC_SUBST([HAVE_FMODL]) HAVE_FREXPF=1; AC_SUBST([HAVE_FREXPF]) + HAVE_HYPOTF=1; AC_SUBST([HAVE_HYPOTF]) HAVE_ISNANF=1; AC_SUBST([HAVE_ISNANF]) HAVE_ISNAND=1; AC_SUBST([HAVE_ISNAND]) HAVE_ISNANL=1; AC_SUBST([HAVE_ISNANL]) @@ -220,6 +222,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_HYPOTF=0; AC_SUBST([REPLACE_HYPOTF]) REPLACE_ISFINITE=0; AC_SUBST([REPLACE_ISFINITE]) REPLACE_ISINF=0; AC_SUBST([REPLACE_ISINF]) REPLACE_ISNAN=0; AC_SUBST([REPLACE_ISNAN]) diff --git a/modules/hypotf b/modules/hypotf new file mode 100644 index 000000000..82e09aa8a --- /dev/null +++ b/modules/hypotf @@ -0,0 +1,32 @@ +Description: +hypotf() function: length of a vector in the plane. + +Files: +lib/hypotf.c +m4/hypotf.m4 +m4/mathfunc.m4 + +Depends-on: +math +hypot [test $HAVE_HYPOTF = 0 || test $REPLACE_HYPOTF = 1] + +configure.ac: +gl_FUNC_HYPOTF +if test $HAVE_HYPOTF = 0 || test $REPLACE_HYPOTF = 1; then + AC_LIBOBJ([hypotf]) +fi +gl_MATH_MODULE_INDICATOR([hypotf]) + +Makefile.am: + +Include: + + +Link: +$(HYPOTF_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/math b/modules/math index 6ffbe1982..0472adbc6 100644 --- a/modules/math +++ b/modules/math @@ -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_HYPOTF''@/$(GNULIB_HYPOTF)/g' \ -e 's/@''GNULIB_ISFINITE''@/$(GNULIB_ISFINITE)/g' \ -e 's/@''GNULIB_ISINF''@/$(GNULIB_ISINF)/g' \ -e 's/@''GNULIB_ISNAN''@/$(GNULIB_ISNAN)/g' \ @@ -122,6 +123,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_FMODF''@|$(HAVE_FMODF)|g' \ -e 's|@''HAVE_FMODL''@|$(HAVE_FMODL)|g' \ -e 's|@''HAVE_FREXPF''@|$(HAVE_FREXPF)|g' \ + -e 's|@''HAVE_HYPOTF''@|$(HAVE_HYPOTF)|g' \ -e 's|@''HAVE_ISNANF''@|$(HAVE_ISNANF)|g' \ -e 's|@''HAVE_ISNAND''@|$(HAVE_ISNAND)|g' \ -e 's|@''HAVE_ISNANL''@|$(HAVE_ISNANL)|g' \ @@ -189,6 +191,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_HYPOTF''@|$(REPLACE_HYPOTF)|g' \ -e 's|@''REPLACE_ISFINITE''@|$(REPLACE_ISFINITE)|g' \ -e 's|@''REPLACE_ISINF''@|$(REPLACE_ISINF)|g' \ -e 's|@''REPLACE_ISNAN''@|$(REPLACE_ISNAN)|g' \ diff --git a/tests/test-math-c++.cc b/tests/test-math-c++.cc index b6534f026..05ea2144e 100644 --- a/tests/test-math-c++.cc +++ b/tests/test-math-c++.cc @@ -155,6 +155,9 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::frexp, double, (double, int *)); SIGNATURE_CHECK (GNULIB_NAMESPACE::frexpl, long double, (long double, int *)); #endif +#if GNULIB_TEST_HYPOTF +SIGNATURE_CHECK (GNULIB_NAMESPACE::hypotf, float, (float, float)); +#endif //SIGNATURE_CHECK (GNULIB_NAMESPACE::hypot, double, (double, double)); //SIGNATURE_CHECK (GNULIB_NAMESPACE::j0, double, (double)); -- 2.11.0