From a0dccde75e6a3b9c930e3577fae7a5bf9351ec09 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 29 Feb 2012 23:42:53 +0100 Subject: 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. --- lib/cbrt.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 lib/cbrt.c (limited to 'lib/cbrt.c') 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; +} -- cgit v1.2.1