From a0dccde75e6a3b9c930e3577fae7a5bf9351ec09 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 29 Feb 2012 23:42:53 +0100 Subject: [PATCH] cbrt: Provide replacement on MSVC and Minix. * lib/math.in.h (cbrt): New declaration. * lib/cbrt.c: New file. * m4/cbrt.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whether cbrt is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_CBRT, HAVE_CBRT. * modules/math (Makefile.am): Substitute GNULIB_CBRT, HAVE_CBRT. * modules/cbrt (Files): Add lib/cbrt.c, m4/cbrt.m4. (Depends-on): Add dependencies. (configure.ac): Arrange to compile replacement if HAVE_CBRT is 0. * tests/test-math-c++.cc: Check the declaration of cbrt. * doc/posix-functions/cbrt.texi: Mention that the module provides a replacement. --- ChangeLog | 16 ++++++++ doc/posix-functions/cbrt.texi | 6 +-- lib/cbrt.c | 71 +++++++++++++++++++++++++++++++++++ lib/math.in.h | 15 ++++++++ m4/cbrt.m4 | 42 +++++++++++++++++++++ m4/math_h.m4 | 6 ++- modules/cbrt | 13 ++++++- modules/math | 2 + tests/test-math-c++.cc | 4 +- 9 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 lib/cbrt.c create mode 100644 m4/cbrt.m4 diff --git a/ChangeLog b/ChangeLog index 33d4a935a2..864175dd01 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2012-02-29 Bruno Haible + + cbrt: Provide replacement on MSVC and Minix. + * lib/math.in.h (cbrt): New declaration. + * lib/cbrt.c: New file. + * m4/cbrt.m4: New file. + * m4/math_h.m4 (gl_MATH_H): Test whether cbrt is declared. + (gl_MATH_H_DEFAULTS): Initialize GNULIB_CBRT, HAVE_CBRT. + * modules/math (Makefile.am): Substitute GNULIB_CBRT, HAVE_CBRT. + * modules/cbrt (Files): Add lib/cbrt.c, m4/cbrt.m4. + (Depends-on): Add dependencies. + (configure.ac): Arrange to compile replacement if HAVE_CBRT is 0. + * tests/test-math-c++.cc: Check the declaration of cbrt. + * doc/posix-functions/cbrt.texi: Mention that the module provides a + replacement. + 2012-02-29 Bruno Haible hypotl-ieee: Work around test failure on OSF/1 and native Windows. diff --git a/doc/posix-functions/cbrt.texi b/doc/posix-functions/cbrt.texi index 16c455e813..ddd3b6a97c 100644 --- a/doc/posix-functions/cbrt.texi +++ b/doc/posix-functions/cbrt.texi @@ -8,11 +8,11 @@ Gnulib module: cbrt Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +Minix 3.1.8, MSVC 9. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -Minix 3.1.8, MSVC 9. @end itemize diff --git a/lib/cbrt.c b/lib/cbrt.c new file mode 100644 index 0000000000..d3659f0a6f --- /dev/null +++ b/lib/cbrt.c @@ -0,0 +1,71 @@ +/* Compute cubic root of double 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/dbl-64/s_cbrt.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 +}; + + +double +cbrt (double x) +{ + if (isfinite (x) && x != 0.0) + { + double xm, ym, u, t2; + int xe; + + /* Reduce X. XM now is an range 1.0 to 0.5. */ + xm = frexp (fabs (x), &xe); + + u = (0.354895765043919860 + + ((1.50819193781584896 + + ((-2.11499494167371287 + + ((2.44693122563534430 + + ((-1.83469277483613086 + + (0.784932344976639262 - 0.145263899385486377 * xm) + * xm) + * xm)) + * xm)) + * xm)) + * xm)); + + t2 = u * u * u; + + ym = u * (t2 + 2.0 * xm) / (2.0 * t2 + xm) * factor[2 + xe % 3]; + + return ldexp (x > 0.0 ? ym : -ym, xe / 3); + } + else + return x + x; +} diff --git a/lib/math.in.h b/lib/math.in.h index 5f108df06a..9c8e70bd02 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -288,6 +288,21 @@ _GL_WARN_ON_USE (atan2f, "atan2f is unportable - " #endif +#if @GNULIB_CBRT@ +# if !@HAVE_CBRT@ +_GL_FUNCDECL_SYS (cbrt, double, (double x)); +# endif +_GL_CXXALIAS_SYS (cbrt, double, (double x)); +_GL_CXXALIASWARN (cbrt); +#elif defined GNULIB_POSIXCHECK +# undef cbrt +# if HAVE_RAW_DECL_CBRT +_GL_WARN_ON_USE (cbrt, "cbrt is unportable - " + "use gnulib module cbrt for portability"); +# endif +#endif + + #if @GNULIB_CEILF@ # if @REPLACE_CEILF@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/m4/cbrt.m4 b/m4/cbrt.m4 new file mode 100644 index 0000000000..065f328afd --- /dev/null +++ b/m4/cbrt.m4 @@ -0,0 +1,42 @@ +# cbrt.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_CBRT], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + + dnl Determine CBRT_LIBM. + gl_COMMON_DOUBLE_MATHFUNC([cbrt]) + + dnl Test whether cbrt() exists. + save_LIBS="$LIBS" + LIBS="$LIBS $CBRT_LIBM" + AC_CHECK_FUNCS([cbrt]) + LIBS="$save_LIBS" + if test $ac_cv_func_cbrt = no; then + HAVE_CBRT=0 + dnl Find libraries needed to link lib/cbrt.c. + AC_REQUIRE([gl_FUNC_FABS]) + AC_REQUIRE([gl_FUNC_FREXP]) + AC_REQUIRE([gl_FUNC_LDEXP]) + CBRT_LIBM= + dnl Append $FABS_LIBM to CBRT_LIBM, avoiding gratuitous duplicates. + case " $CBRT_LIBM " in + *" $FABS_LIBM "*) ;; + *) CBRT_LIBM="$CBRT_LIBM $FABS_LIBM" ;; + esac + dnl Append $FREXP_LIBM to CBRT_LIBM, avoiding gratuitous duplicates. + case " $CBRT_LIBM " in + *" $FREXP_LIBM "*) ;; + *) CBRT_LIBM="$CBRT_LIBM $FREXP_LIBM" ;; + esac + dnl Append $LDEXP_LIBM to CBRT_LIBM, avoiding gratuitous duplicates. + case " $CBRT_LIBM " in + *" $LDEXP_LIBM "*) ;; + *) CBRT_LIBM="$CBRT_LIBM $LDEXP_LIBM" ;; + esac + fi +]) diff --git a/m4/math_h.m4 b/m4/math_h.m4 index 3327dd550f..eada1de3dd 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,4 +1,4 @@ -# math_h.m4 serial 75 +# math_h.m4 serial 76 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 - ceilf ceill copysign copysignf copysignl cosf cosl coshf + cbrt 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 @@ -67,6 +67,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], GNULIB_ATANF=0; AC_SUBST([GNULIB_ATANF]) GNULIB_ATANL=0; AC_SUBST([GNULIB_ATANL]) GNULIB_ATAN2F=0; AC_SUBST([GNULIB_ATAN2F]) + GNULIB_CBRT=0; AC_SUBST([GNULIB_CBRT]) GNULIB_CEIL=0; AC_SUBST([GNULIB_CEIL]) GNULIB_CEILF=0; AC_SUBST([GNULIB_CEILF]) GNULIB_CEILL=0; AC_SUBST([GNULIB_CEILL]) @@ -141,6 +142,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], HAVE_ATANF=1; AC_SUBST([HAVE_ATANF]) HAVE_ATANL=1; AC_SUBST([HAVE_ATANL]) HAVE_ATAN2F=1; AC_SUBST([HAVE_ATAN2F]) + HAVE_CBRT=1; AC_SUBST([HAVE_CBRT]) HAVE_COPYSIGN=1; AC_SUBST([HAVE_COPYSIGN]) HAVE_COPYSIGNF=1; AC_SUBST([HAVE_COPYSIGNF]) HAVE_COPYSIGNL=1; AC_SUBST([HAVE_COPYSIGNL]) diff --git a/modules/cbrt b/modules/cbrt index fe015ee05c..3cb47118d8 100644 --- a/modules/cbrt +++ b/modules/cbrt @@ -2,12 +2,23 @@ Description: cbrt() function: cube root. Files: +lib/cbrt.c +m4/cbrt.m4 m4/mathfunc.m4 Depends-on: +math +isfinite [test $HAVE_CBRT = 0] +fabs [test $HAVE_CBRT = 0] +frexp [test $HAVE_CBRT = 0] +ldexp [test $HAVE_CBRT = 0] configure.ac: -gl_COMMON_DOUBLE_MATHFUNC([cbrt]) +gl_FUNC_CBRT +if test $HAVE_CBRT = 0; then + AC_LIBOBJ([cbrt]) +fi +gl_MATH_MODULE_INDICATOR([cbrt]) Makefile.am: diff --git a/modules/math b/modules/math index 140ff802bf..2d5440227d 100644 --- a/modules/math +++ b/modules/math @@ -35,6 +35,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_ATANF''@/$(GNULIB_ATANF)/g' \ -e 's/@''GNULIB_ATANL''@/$(GNULIB_ATANL)/g' \ -e 's/@''GNULIB_ATAN2F''@/$(GNULIB_ATAN2F)/g' \ + -e 's/@''GNULIB_CBRT''@/$(GNULIB_CBRT)/g' \ -e 's/@''GNULIB_CEIL''@/$(GNULIB_CEIL)/g' \ -e 's/@''GNULIB_CEILF''@/$(GNULIB_CEILF)/g' \ -e 's/@''GNULIB_CEILL''@/$(GNULIB_CEILL)/g' \ @@ -109,6 +110,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_ATANF''@|$(HAVE_ATANF)|g' \ -e 's|@''HAVE_ATANL''@|$(HAVE_ATANL)|g' \ -e 's|@''HAVE_ATAN2F''@|$(HAVE_ATAN2F)|g' \ + -e 's|@''HAVE_CBRT''@|$(HAVE_CBRT)|g' \ -e 's|@''HAVE_COPYSIGN''@|$(HAVE_COPYSIGN)|g' \ -e 's|@''HAVE_COPYSIGNF''@|$(HAVE_COPYSIGNF)|g' \ -e 's|@''HAVE_COPYSIGNL''@|$(HAVE_COPYSIGNL)|g' \ diff --git a/tests/test-math-c++.cc b/tests/test-math-c++.cc index 47dc173d5b..e79cb0b06e 100644 --- a/tests/test-math-c++.cc +++ b/tests/test-math-c++.cc @@ -68,7 +68,9 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::atan2f, float, (float, float)); #endif //SIGNATURE_CHECK (GNULIB_NAMESPACE::atan2, double, (double, double)); -//SIGNATURE_CHECK (GNULIB_NAMESPACE::cbrt, double, (double)); +#if GNULIB_TEST_CBRT +SIGNATURE_CHECK (GNULIB_NAMESPACE::cbrt, double, (double)); +#endif #if GNULIB_TEST_CEILF SIGNATURE_CHECK (GNULIB_NAMESPACE::ceilf, float, (float)); -- 2.39.5