From af95cd82312c495ebe5336adf88c882ca7c0fc7f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 1 Mar 2012 02:03:36 +0100 Subject: [PATCH] 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. --- ChangeLog | 15 ++++++++ doc/posix-functions/cbrtf.texi | 11 +++--- lib/cbrtf.c | 62 ++++++++++++++++++++++++++++++++++ lib/math.in.h | 14 ++++++++ m4/cbrtf.m4 | 48 ++++++++++++++++++++++++++ m4/math_h.m4 | 7 ++-- modules/cbrtf | 36 ++++++++++++++++++++ modules/math | 3 ++ tests/test-math-c++.cc | 3 ++ 9 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 lib/cbrtf.c create mode 100644 m4/cbrtf.m4 create mode 100644 modules/cbrtf diff --git a/ChangeLog b/ChangeLog index 864175dd01..01b4e95eb9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2012-02-29 Bruno Haible + + 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. + 2012-02-29 Bruno Haible cbrt: Provide replacement on MSVC and Minix. diff --git a/doc/posix-functions/cbrtf.texi b/doc/posix-functions/cbrtf.texi index 4d98051e4a..1794bb3967 100644 --- a/doc/posix-functions/cbrtf.texi +++ b/doc/posix-functions/cbrtf.texi @@ -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 +@item +This function is missing on some platforms: +Minix 3.1.8, AIX 5.1, Solaris 9, MSVC 9. +@item +This function is not declared on some platforms: +IRIX 6.5. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -Minix 3.1.8, AIX 5.1, IRIX 6.5, Solaris 9, MSVC 9. @end itemize diff --git a/lib/cbrtf.c b/lib/cbrtf.c new file mode 100644 index 0000000000..6320d0e6bc --- /dev/null +++ b/lib/cbrtf.c @@ -0,0 +1,62 @@ +/* Compute cubic root of float value. + Copyright (C) 1997, 2012 Free Software Foundation, Inc. + + Contributed by Dirk Alboth and + Ulrich Drepper , 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 . */ + +#include + +/* Specification. */ +#include + +/* 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; +} diff --git a/lib/math.in.h b/lib/math.in.h index 9c8e70bd02..9515424e4f 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -288,6 +288,20 @@ _GL_WARN_ON_USE (atan2f, "atan2f is unportable - " #endif +#if @GNULIB_CBRTF@ +# if !@HAVE_DECL_CBRTF@ +_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 !@HAVE_CBRT@ _GL_FUNCDECL_SYS (cbrt, double, (double x)); diff --git a/m4/cbrtf.m4 b/m4/cbrtf.m4 new file mode 100644 index 0000000000..cf2c6b74cd --- /dev/null +++ b/m4/cbrtf.m4 @@ -0,0 +1,48 @@ +# 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 . + AC_CHECK_DECL([cbrtf], , [HAVE_DECL_CBRTF=0], [[#include ]]) + 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]) +]) diff --git a/m4/math_h.m4 b/m4/math_h.m4 index eada1de3dd..512589867e 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -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 @@ AC_DEFUN([gl_MATH_H], dnl corresponding gnulib module is not in use. gl_WARN_ON_USE_PREPARE([[#include ]], [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 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], 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 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], 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 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], 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]) diff --git a/modules/cbrtf b/modules/cbrtf new file mode 100644 index 0000000000..8de0368bd0 --- /dev/null +++ b/modules/cbrtf @@ -0,0 +1,36 @@ +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: + + +Link: +$(CBRTF_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/math b/modules/math index 2d5440227d..26507b553d 100644 --- a/modules/math +++ b/modules/math @@ -36,6 +36,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -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 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -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 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -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' \ diff --git a/tests/test-math-c++.cc b/tests/test-math-c++.cc index e79cb0b06e..a0a687426b 100644 --- a/tests/test-math-c++.cc +++ b/tests/test-math-c++.cc @@ -68,6 +68,9 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::atan2f, float, (float, float)); #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 -- 2.39.5