bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new module 'hypotf'


From: Bruno Haible
Subject: new module 'hypotf'
Date: Wed, 29 Feb 2012 13:24:43 +0100
User-agent: KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; )

Another unportable math function is hypotf().

After adding the usual replacement code and a unit test, I get test failures
on OpenBSD 4.9:

  test-hypotf.c:47: assertion failed
  FAIL: test-hypotf

  /* Overflow.  */
  x = FLT_MAX;
  y = FLT_MAX * 0.5f;
  z = hypotf (x, y);
  ASSERT (z == HUGE_VALF);
  The value computed here is a NaN, when it should be +Inf.

and on NetBSD 5.1:

  test-hypotf.c:59: assertion failed
  FAIL: test-hypotf

  /* No underflow.  */
  x = FLT_MIN * 2.0f;
  y = FLT_MIN * 3.0f;
  z = hypotf (x, y);
  ASSERT (z >= FLT_MIN * 2.0f && z <= FLT_MIN * 4.0f);
  The value computed here is too large by a factor of 2^127.


2012-02-29  Bruno Haible  <address@hidden>

        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.

================================ lib/hypotf.c ================================
/* 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/>.  */

#include <config.h>

/* Specification.  */
#include <math.h>

float
hypotf (float x, float y)
{
  return (float) hypot ((double) x, (double) y);
}
================================ m4/hypotf.m4 ================================
# 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 <float.h>
#include <math.h>
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
        ])
    ])
])
=============================== modules/hypotf ===============================
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:
<math.h>

Link:
$(HYPOTF_LIBM)

License:
LGPL

Maintainer:
Bruno Haible
==============================================================================
--- doc/posix-functions/hypotf.texi.orig        Wed Feb 29 13:15:56 2012
+++ doc/posix-functions/hypotf.texi     Wed Feb 29 12:53:26 2012
@@ -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
address@hidden
+This function is missing on some platforms:
+Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 9, MSVC 9.
address@hidden
+This function produces wrong values on some platforms:
+NetBSD 5.1, OpenBSD 4.9.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
address@hidden
-This function is missing on some platforms:
-Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 9, MSVC 9.
 @end itemize
--- lib/math.in.h.orig  Wed Feb 29 13:15:56 2012
+++ lib/math.in.h       Wed Feb 29 12:43:05 2012
@@ -808,6 +808,31 @@
 #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 address@hidden@
+_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 address@hidden@
--- m4/math_h.m4.orig   Wed Feb 29 13:15:56 2012
+++ m4/math_h.m4        Wed Feb 29 12:43:38 2012
@@ -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 @@
     [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 @@
   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 @@
   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 @@
   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])
--- modules/math.orig   Wed Feb 29 13:15:56 2012
+++ modules/math        Wed Feb 29 12:44:02 2012
@@ -60,6 +60,7 @@
              -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 @@
              -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 @@
              -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' \
--- tests/test-math-c++.cc.orig Wed Feb 29 13:15:56 2012
+++ tests/test-math-c++.cc      Wed Feb 29 12:12:29 2012
@@ -155,6 +155,9 @@
 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));




reply via email to

[Prev in Thread] Current Thread [Next in Thread]