149393c00Smartynas /*-
249393c00Smartynas * ====================================================
349393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
449393c00Smartynas * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
549393c00Smartynas *
649393c00Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
749393c00Smartynas * Permission to use, copy, modify, and distribute this
849393c00Smartynas * software is freely granted, provided that this notice
949393c00Smartynas * is preserved.
1049393c00Smartynas * ====================================================
1149393c00Smartynas *
1249393c00Smartynas * The argument reduction and testing for exceptional cases was
1349393c00Smartynas * written by Steven G. Kargl with input from Bruce D. Evans
1449393c00Smartynas * and David A. Schultz.
1549393c00Smartynas */
1649393c00Smartynas
1749393c00Smartynas #include <float.h>
1849393c00Smartynas #include <ieeefp.h>
1949393c00Smartynas #include <math.h>
2049393c00Smartynas
2149393c00Smartynas #include "math_private.h"
2249393c00Smartynas
2349393c00Smartynas #define BIAS (LDBL_MAX_EXP - 1)
2449393c00Smartynas
2549393c00Smartynas static const unsigned
2649393c00Smartynas B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
2749393c00Smartynas
2849393c00Smartynas long double
cbrtl(long double x)2949393c00Smartynas cbrtl(long double x)
3049393c00Smartynas {
3149393c00Smartynas long double v, r, s, t, w;
3249393c00Smartynas double dr, dt, dx;
3349393c00Smartynas float ft, fx;
3449393c00Smartynas uint64_t hx, lx;
3549393c00Smartynas uint16_t expsign;
3649393c00Smartynas int k;
3749393c00Smartynas
3849393c00Smartynas GET_LDOUBLE_MSW64(hx,x);
3949393c00Smartynas k = (hx>>48)&0x7fff;
4049393c00Smartynas
4149393c00Smartynas /*
4249393c00Smartynas * If x = +-Inf, then cbrt(x) = +-Inf.
4349393c00Smartynas * If x = NaN, then cbrt(x) = NaN.
4449393c00Smartynas */
4549393c00Smartynas if (k == BIAS + LDBL_MAX_EXP)
4649393c00Smartynas return (x + x);
4749393c00Smartynas
4849393c00Smartynas if (k == 0) {
4949393c00Smartynas /* If x = +-0, then cbrt(x) = +-0. */
5049393c00Smartynas GET_LDOUBLE_WORDS64(hx,lx,x);
5149393c00Smartynas if (((hx&0x7fffffffffffffffLL)|lx) == 0) {
5249393c00Smartynas return (x);
5349393c00Smartynas }
5449393c00Smartynas /* Adjust subnormal numbers. */
5549393c00Smartynas x *= 0x1.0p514;
5649393c00Smartynas GET_LDOUBLE_MSW64(hx,x);
5749393c00Smartynas k = (hx>>48)&0x7fff;
5849393c00Smartynas k -= BIAS + 514;
5949393c00Smartynas } else
6049393c00Smartynas k -= BIAS;
6149393c00Smartynas GET_LDOUBLE_MSW64(hx,x);
6249393c00Smartynas hx = (hx&0x8000ffffffffffffLL)|((uint64_t)BIAS<<48);
6349393c00Smartynas SET_LDOUBLE_MSW64(x,hx);
6449393c00Smartynas v = 1;
6549393c00Smartynas
6649393c00Smartynas switch (k % 3) {
6749393c00Smartynas case 1:
6849393c00Smartynas case -2:
6949393c00Smartynas x = 2*x;
7049393c00Smartynas k--;
7149393c00Smartynas break;
7249393c00Smartynas case 2:
7349393c00Smartynas case -1:
7449393c00Smartynas x = 4*x;
7549393c00Smartynas k -= 2;
7649393c00Smartynas break;
7749393c00Smartynas }
7849393c00Smartynas GET_LDOUBLE_MSW64(hx,x);
79*9fc13282Smartynas expsign = ((hx>>48) & 0x8000) | (BIAS + k / 3);
8049393c00Smartynas hx = (hx&0x8000ffffffffffffLL)|((uint64_t)expsign<<48);
8149393c00Smartynas SET_LDOUBLE_MSW64(x,hx);
8249393c00Smartynas
8349393c00Smartynas /*
8449393c00Smartynas * The following is the guts of s_cbrtf, with the handling of
8549393c00Smartynas * special values removed and extra care for accuracy not taken,
8649393c00Smartynas * but with most of the extra accuracy not discarded.
8749393c00Smartynas */
8849393c00Smartynas
8949393c00Smartynas /* ~5-bit estimate: */
9049393c00Smartynas fx = x;
9149393c00Smartynas GET_FLOAT_WORD(hx, fx);
9249393c00Smartynas SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
9349393c00Smartynas
9449393c00Smartynas /* ~16-bit estimate: */
9549393c00Smartynas dx = x;
9649393c00Smartynas dt = ft;
9749393c00Smartynas dr = dt * dt * dt;
9849393c00Smartynas dt = dt * (dx + dx + dr) / (dx + dr + dr);
9949393c00Smartynas
10049393c00Smartynas /* ~47-bit estimate: */
10149393c00Smartynas dr = dt * dt * dt;
10249393c00Smartynas dt = dt * (dx + dx + dr) / (dx + dr + dr);
10349393c00Smartynas
10449393c00Smartynas /*
10549393c00Smartynas * Round dt away from zero to 47 bits. Since we don't trust the 47,
10649393c00Smartynas * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and
10749393c00Smartynas * might be avoidable in this case, since on most machines dt will
10849393c00Smartynas * have been evaluated in 53-bit precision and the technical reasons
10949393c00Smartynas * for rounding up might not apply to either case in cbrtl() since
11049393c00Smartynas * dt is much more accurate than needed.
11149393c00Smartynas */
11249393c00Smartynas t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
11349393c00Smartynas
11449393c00Smartynas /*
11549393c00Smartynas * Final step Newton iteration to 64 or 113 bits with
11649393c00Smartynas * error < 0.667 ulps
11749393c00Smartynas */
11849393c00Smartynas s=t*t; /* t*t is exact */
11949393c00Smartynas r=x/s; /* error <= 0.5 ulps; |r| < |t| */
12049393c00Smartynas w=t+t; /* t+t is exact */
12149393c00Smartynas r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
12249393c00Smartynas t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */
12349393c00Smartynas
12449393c00Smartynas t *= v;
12549393c00Smartynas return (t);
12649393c00Smartynas }
127