bug-gnulib
[Top][All Lists]
Advanced

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

new module 'cbrtf'


From: Bruno Haible
Subject: new module 'cbrtf'
Date: Thu, 01 Mar 2012 02:05:20 +0100
User-agent: KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; )

The cube-root function cbrtf() is also not portable. This patch introduces
a new module for it.


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

        New module 'cbrtf'.
        * lib/math.in.h (cbrtf): New declaration.
        * lib/cbrtf.c: New file.
        * m4/cbrtf.m4: New file.
        * m4/math_h.m4 (gl_MATH_H): Test whether cbrtf is declared.
        (gl_MATH_H_DEFAULTS): Initialize GNULIB_CBRTF, HAVE_CBRTF,
        HAVE_DECL_CBRTF.
        * modules/math (Makefile.am): Substitute GNULIB_CBRTF, HAVE_CBRTF,
        HAVE_DECL_CBRTF.
        * modules/cbrtf: New file.
        * tests/test-math-c++.cc: Check the declaration of cbrtf.
        * doc/posix-functions/cbrtf.texi: Mention the new module.

================================= lib/cbrtf.c =================================
/* Compute cubic root of float value.
   Copyright (C) 1997, 2012 Free Software Foundation, Inc.

   Contributed by Dirk Alboth <address@hidden> and
   Ulrich Drepper <address@hidden>, 1997.

   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>

/* Code based on glibc/sysdeps/ieee754/flt-32/s_cbrtf.c.  */

#define CBRT2 1.2599210498948731648             /* 2^(1/3) */
#define SQR_CBRT2 1.5874010519681994748         /* 2^(2/3) */

static const double factor[5] =
{
  1.0 / SQR_CBRT2,
  1.0 / CBRT2,
  1.0,
  CBRT2,
  SQR_CBRT2
};


float
cbrtf (float x)
{
  if (isfinite (x) && x != 0.0f)
    {
      float xm, ym, u, t2;
      int xe;

      /* Reduce X.  XM now is an range 1.0 to 0.5.  */
      xm = frexpf (fabsf (x), &xe);

      u = (0.492659620528969547
           + (0.697570460207922770 - 0.191502161678719066 * xm) * xm);

      t2 = u * u * u;

      ym = u * (t2 + 2.0 * xm) / (2.0 * t2 + xm) * factor[2 + xe % 3];

      return ldexpf (x > 0.0 ? ym : -ym, xe / 3);
    }
  else
    return x + x;
}
================================= m4/cbrtf.m4 =================================
# cbrtf.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_CBRTF],
[
  AC_REQUIRE([gl_MATH_H_DEFAULTS])
  AC_REQUIRE([gl_FUNC_CBRT])

  dnl Test whether cbrtf() exists. Assume that cbrtf(), if it exists, is
  dnl defined in the same library as cbrt().
  save_LIBS="$LIBS"
  LIBS="$LIBS $CBRT_LIBM"
  AC_CHECK_FUNCS([cbrtf])
  LIBS="$save_LIBS"
  if test $ac_cv_func_cbrtf = yes; then
    CBRTF_LIBM="$CBRT_LIBM"
    dnl Also check whether it's declared.
    dnl IRIX 6.5 has cbrtf() in libm but doesn't declare it in <math.h>.
    AC_CHECK_DECL([cbrtf], , [HAVE_DECL_CBRTF=0], [[#include <math.h>]])
  else
    HAVE_CBRTF=0
    HAVE_DECL_CBRTF=0
    dnl Find libraries needed to link lib/cbrtf.c.
    AC_REQUIRE([gl_FUNC_FABSF])
    AC_REQUIRE([gl_FUNC_FREXPF])
    AC_REQUIRE([gl_FUNC_LDEXPF])
    CBRTF_LIBM=
    dnl Append $FABSF_LIBM to CBRTF_LIBM, avoiding gratuitous duplicates.
    case " $CBRTF_LIBM " in
      *" $FABSF_LIBM "*) ;;
      *) CBRTF_LIBM="$CBRTF_LIBM $FABSF_LIBM" ;;
    esac
    dnl Append $FREXPF_LIBM to CBRTF_LIBM, avoiding gratuitous duplicates.
    case " $CBRTF_LIBM " in
      *" $FREXPF_LIBM "*) ;;
      *) CBRTF_LIBM="$CBRTF_LIBM $FREXPF_LIBM" ;;
    esac
    dnl Append $LDEXPF_LIBM to CBRTF_LIBM, avoiding gratuitous duplicates.
    case " $CBRTF_LIBM " in
      *" $LDEXPF_LIBM "*) ;;
      *) CBRTF_LIBM="$CBRTF_LIBM $LDEXPF_LIBM" ;;
    esac
  fi
  AC_SUBST([CBRTF_LIBM])
])
================================ modules/cbrtf ================================
Description:
cbrtf() function: cube root.

Files:
lib/cbrtf.c
m4/cbrtf.m4
m4/cbrt.m4
m4/mathfunc.m4

Depends-on:
math
isfinite        [test $HAVE_CBRTF = 0]
fabsf           [test $HAVE_CBRTF = 0]
frexpf          [test $HAVE_CBRTF = 0]
ldexpf          [test $HAVE_CBRTF = 0]

configure.ac:
gl_FUNC_CBRTF
if test $HAVE_CBRTF = 0; then
  AC_LIBOBJ([cbrtf])
fi
gl_MATH_MODULE_INDICATOR([cbrtf])

Makefile.am:

Include:
<math.h>

Link:
$(CBRTF_LIBM)

License:
LGPL

Maintainer:
Bruno Haible
===============================================================================
--- doc/posix-functions/cbrtf.texi.orig Thu Mar  1 01:57:14 2012
+++ doc/posix-functions/cbrtf.texi      Thu Mar  1 01:42:52 2012
@@ -4,15 +4,18 @@
 
 POSIX specification:@* 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/cbrtf.html}
 
-Gnulib module: ---
+Gnulib module: cbrtf
 
 Portability problems fixed by Gnulib:
 @itemize
address@hidden
+This function is missing on some platforms:
+Minix 3.1.8, AIX 5.1, Solaris 9, MSVC 9.
address@hidden
+This function is not declared on some platforms:
+IRIX 6.5.
 @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, IRIX 6.5, Solaris 9, MSVC 9.
 @end itemize
--- lib/math.in.h.orig  Thu Mar  1 01:57:14 2012
+++ lib/math.in.h       Thu Mar  1 01:43:15 2012
@@ -288,6 +288,20 @@
 #endif
 
 
+#if @GNULIB_CBRTF@
+# if address@hidden@
+_GL_FUNCDECL_SYS (cbrtf, float, (float x));
+# endif
+_GL_CXXALIAS_SYS (cbrtf, float, (float x));
+_GL_CXXALIASWARN (cbrtf);
+#elif defined GNULIB_POSIXCHECK
+# undef cbrtf
+# if HAVE_RAW_DECL_CBRTF
+_GL_WARN_ON_USE (cbrtf, "cbrtf is unportable - "
+                 "use gnulib module cbrtf for portability");
+# endif
+#endif
+
 #if @GNULIB_CBRT@
 # if address@hidden@
 _GL_FUNCDECL_SYS (cbrt, double, (double x));
--- m4/math_h.m4.orig   Thu Mar  1 01:57:14 2012
+++ m4/math_h.m4        Thu Mar  1 01:45:07 2012
@@ -1,4 +1,4 @@
-# math_h.m4 serial 76
+# math_h.m4 serial 77
 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,
@@ -40,7 +40,7 @@
   dnl corresponding gnulib module is not in use.
   gl_WARN_ON_USE_PREPARE([[#include <math.h>]],
     [acosf acosl asinf asinl atanf atanl
-     cbrt ceilf ceill copysign copysignf copysignl cosf cosl coshf
+     cbrt cbrtf ceilf ceill copysign copysignf copysignl cosf cosl coshf
      expf expl fabsf fabsl floorf floorl fma fmaf fmal
      fmod fmodf fmodl frexpf frexpl hypotf hypotl
      ldexpf ldexpl logb logf logl log10f log10l modf modff modfl powf
@@ -68,6 +68,7 @@
   GNULIB_ATANL=0;      AC_SUBST([GNULIB_ATANL])
   GNULIB_ATAN2F=0;     AC_SUBST([GNULIB_ATAN2F])
   GNULIB_CBRT=0;       AC_SUBST([GNULIB_CBRT])
+  GNULIB_CBRTF=0;      AC_SUBST([GNULIB_CBRTF])
   GNULIB_CEIL=0;       AC_SUBST([GNULIB_CEIL])
   GNULIB_CEILF=0;      AC_SUBST([GNULIB_CEILF])
   GNULIB_CEILL=0;      AC_SUBST([GNULIB_CEILL])
@@ -143,6 +144,7 @@
   HAVE_ATANL=1;                AC_SUBST([HAVE_ATANL])
   HAVE_ATAN2F=1;               AC_SUBST([HAVE_ATAN2F])
   HAVE_CBRT=1;                 AC_SUBST([HAVE_CBRT])
+  HAVE_CBRTF=1;                AC_SUBST([HAVE_CBRTF])
   HAVE_COPYSIGN=1;             AC_SUBST([HAVE_COPYSIGN])
   HAVE_COPYSIGNF=1;            AC_SUBST([HAVE_COPYSIGNF])
   HAVE_COPYSIGNL=1;            AC_SUBST([HAVE_COPYSIGNL])
@@ -189,6 +191,7 @@
   HAVE_DECL_ACOSL=1;           AC_SUBST([HAVE_DECL_ACOSL])
   HAVE_DECL_ASINL=1;           AC_SUBST([HAVE_DECL_ASINL])
   HAVE_DECL_ATANL=1;           AC_SUBST([HAVE_DECL_ATANL])
+  HAVE_DECL_CBRTF=1;           AC_SUBST([HAVE_DECL_CBRTF])
   HAVE_DECL_CEILF=1;           AC_SUBST([HAVE_DECL_CEILF])
   HAVE_DECL_CEILL=1;           AC_SUBST([HAVE_DECL_CEILL])
   HAVE_DECL_COSL=1;            AC_SUBST([HAVE_DECL_COSL])
--- modules/math.orig   Thu Mar  1 01:57:14 2012
+++ modules/math        Thu Mar  1 01:45:42 2012
@@ -36,6 +36,7 @@
              -e 's/@''GNULIB_ATANL''@/$(GNULIB_ATANL)/g' \
              -e 's/@''GNULIB_ATAN2F''@/$(GNULIB_ATAN2F)/g' \
              -e 's/@''GNULIB_CBRT''@/$(GNULIB_CBRT)/g' \
+             -e 's/@''GNULIB_CBRTF''@/$(GNULIB_CBRTF)/g' \
              -e 's/@''GNULIB_CEIL''@/$(GNULIB_CEIL)/g' \
              -e 's/@''GNULIB_CEILF''@/$(GNULIB_CEILF)/g' \
              -e 's/@''GNULIB_CEILL''@/$(GNULIB_CEILL)/g' \
@@ -111,6 +112,7 @@
              -e 's|@''HAVE_ATANL''@|$(HAVE_ATANL)|g' \
              -e 's|@''HAVE_ATAN2F''@|$(HAVE_ATAN2F)|g' \
              -e 's|@''HAVE_CBRT''@|$(HAVE_CBRT)|g' \
+             -e 's|@''HAVE_CBRTF''@|$(HAVE_CBRTF)|g' \
              -e 's|@''HAVE_COPYSIGN''@|$(HAVE_COPYSIGN)|g' \
              -e 's|@''HAVE_COPYSIGNF''@|$(HAVE_COPYSIGNF)|g' \
              -e 's|@''HAVE_COPYSIGNL''@|$(HAVE_COPYSIGNL)|g' \
@@ -157,6 +159,7 @@
              -e 's|@''HAVE_DECL_ACOSL''@|$(HAVE_DECL_ACOSL)|g' \
              -e 's|@''HAVE_DECL_ASINL''@|$(HAVE_DECL_ASINL)|g' \
              -e 's|@''HAVE_DECL_ATANL''@|$(HAVE_DECL_ATANL)|g' \
+             -e 's|@''HAVE_DECL_CBRTF''@|$(HAVE_DECL_CBRTF)|g' \
              -e 's|@''HAVE_DECL_CEILF''@|$(HAVE_DECL_CEILF)|g' \
              -e 's|@''HAVE_DECL_CEILL''@|$(HAVE_DECL_CEILL)|g' \
              -e 's|@''HAVE_DECL_COSL''@|$(HAVE_DECL_COSL)|g' \
--- tests/test-math-c++.cc.orig Thu Mar  1 01:57:14 2012
+++ tests/test-math-c++.cc      Thu Mar  1 01:01:31 2012
@@ -68,6 +68,9 @@
 #endif
 //SIGNATURE_CHECK (GNULIB_NAMESPACE::atan2, double, (double, double));
 
+#if GNULIB_TEST_CBRTF
+SIGNATURE_CHECK (GNULIB_NAMESPACE::cbrtf, float, (float));
+#endif
 #if GNULIB_TEST_CBRT
 SIGNATURE_CHECK (GNULIB_NAMESPACE::cbrt, double, (double));
 #endif




reply via email to

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