cbrt: Provide replacement on MSVC and Minix.
authorBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 22:42:53 +0000 (23:42 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 29 Feb 2012 22:42:53 +0000 (23:42 +0100)
* 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
doc/posix-functions/cbrt.texi
lib/cbrt.c [new file with mode: 0644]
lib/math.in.h
m4/cbrt.m4 [new file with mode: 0644]
m4/math_h.m4
modules/cbrt
modules/math
tests/test-math-c++.cc

index 33d4a935a2194894876cac43c48a97f915d8c576..864175dd0140bb36c357da337db1dde86aeb4dd1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2012-02-29  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        hypotl-ieee: Work around test failure on OSF/1 and native Windows.
index 16c455e813c62a4c77bb8cc830fd51dfb1307e28..ddd3b6a97c3dfa0b280158c6d3046cfab4899afe 100644 (file)
@@ -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 (file)
index 0000000..d3659f0
--- /dev/null
@@ -0,0 +1,71 @@
+/* Compute cubic root of double value.
+   Copyright (C) 1997, 2012 Free Software Foundation, Inc.
+
+   Contributed by Dirk Alboth <dirka@uni-paderborn.de> and
+   Ulrich Drepper <drepper@cygnus.com>, 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/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;
+}
index 5f108df06a313049de7976061c01612229b8b132..9c8e70bd0298a975bbcee15ca1388aee06be7f5b 100644 (file)
@@ -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 (file)
index 0000000..065f328
--- /dev/null
@@ -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
+])
index 3327dd550fc0e510af716c36c92999cd6ccb65e6..eada1de3dd26714d542dcb4e08fed45355ee05b4 100644 (file)
@@ -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 <math.h>]],
     [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])
index fe015ee05cc5b080df82be3d93861f1ddd6e7166..3cb47118d8c64f55505cdef3f25a4784fff622cc 100644 (file)
@@ -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:
 
index 140ff802bfbcc04f3d1caa615737ebfc420dde43..2d5440227d1515af0b4c810adcb5a4456dd8d67f 100644 (file)
@@ -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' \
index 47dc173d5b98bd5eb48bf18e10f1195a0bb382dc..e79cb0b06e25d9cb682477fa9ff9e0cc4c474563 100644 (file)
@@ -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));