1 /*-
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  *
12  * The argument reduction and testing for exceptional cases was
13  * written by Steven G. Kargl with input from Bruce D. Evans
14  * and David A. Schultz.
15  */
16 
17 //__FBSDID("$FreeBSD: src/lib/msun/src/s_cbrtl.c,v 1.1 2011/03/12 19:37:35 kargl Exp $");
18 
19 
20 #define	BIAS	(LDBL_MAX_EXP - 1)
21 
22 static const unsigned
23     B1 = 709958130;	/* B1 = (127-127.0/3-0.03306235651)*2**23 */
24 
25 long double
cbrtl(long double x)26 cbrtl(long double x)
27 {
28 	union IEEEl2bits u, v;
29 	long double r, s, t, w;
30 	double dr, dt, dx;
31 	float ft, fx;
32 	u_int32_t hx;
33 	u_int16_t expsign;
34 	int k;
35 
36 	u.e = x;
37 	expsign = u.xbits.expsign;
38 	k = expsign & 0x7fff;
39 
40 	/*
41 	 * If x = +-Inf, then cbrt(x) = +-Inf.
42 	 * If x = NaN, then cbrt(x) = NaN.
43 	 */
44 	if (k == BIAS + LDBL_MAX_EXP)
45 		return (x + x);
46 
47 	if (k == 0) {
48 		/* If x = +-0, then cbrt(x) = +-0. */
49 		if ((u.bits.manh | u.bits.manl) == 0) {
50 			return (x);
51 	    	}
52 		/* Adjust subnormal numbers. */
53 		u.e *= 0x1.0p514l;
54 		k = u.bits.exp;
55 		k -= BIAS + 514;
56  	} else
57 		k -= BIAS;
58 	u.xbits.expsign = BIAS;
59 	v.e = 1;
60 
61 	x = u.e;
62 	switch (k % 3) {
63 	case 1:
64 	case -2:
65 		x = 2*x;
66 		k--;
67 		break;
68 	case 2:
69 	case -1:
70 		x = 4*x;
71 		k -= 2;
72 		break;
73 	}
74 	v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
75 
76 	/*
77 	 * The following is the guts of s_cbrtf, with the handling of
78 	 * special values removed and extra care for accuracy not taken,
79 	 * but with most of the extra accuracy not discarded.
80 	 */
81 
82 	/* ~5-bit estimate: */
83 	fx = x;
84 	GET_FLOAT_WORD(hx, fx);
85 	SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
86 
87 	/* ~16-bit estimate: */
88 	dx = x;
89 	dt = (double)ft;
90 	dr = dt * dt * dt;
91 	dt = dt * (dx + dx + dr) / (dx + dr + dr);
92 
93 	/* ~47-bit estimate: */
94 	dr = dt * dt * dt;
95 	dt = dt * (dx + dx + dr) / (dx + dr + dr);
96 
97 #if LDBL_MANT_DIG == 64
98 	/*
99 	 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
100 	 * Round it away from zero to 32 bits (32 so that t*t is exact, and
101 	 * away from zero for technical reasons).
102 	 */
103 	volatile double vd2 = 0x1.0p32;
104 	volatile double vd1 = 0x1.0p-31;
105 	#define vd ((long double)vd2 + (long double)vd1)
106 
107 	t = (long double)dt + vd - 0x1.0p32l;
108 #elif LDBL_MANT_DIG == 113
109 	/*
110 	 * Round dt away from zero to 47 bits.  Since we don't trust the 47,
111 	 * add 2 47-bit ulps instead of 1 to round up.  Rounding is slow and
112 	 * might be avoidable in this case, since on most machines dt will
113 	 * have been evaluated in 53-bit precision and the technical reasons
114 	 * for rounding up might not apply to either case in cbrtl() since
115 	 * dt is much more accurate than needed.
116 	 */
117 	t = (long double)dt + 0x2.0p-46L + 0x1.0p60L - 0x1.0p60L;
118 #else
119 #error "Unsupported long double format"
120 #endif
121 
122 	/*
123      	 * Final step Newton iteration to 64 or 113 bits with
124 	 * error < 0.667 ulps
125 	 */
126 	s=t*t;				/* t*t is exact */
127 	r=x/s;				/* error <= 0.5 ulps; |r| < |t| */
128 	w=t+t;				/* t+t is exact */
129 	r=(r-t)/(w+r);			/* r-t is exact; w+r ~= 3*t */
130 	t=t+t*r;			/* error <= 0.5 + 0.5/3 + epsilon */
131 
132 	t *= v.e;
133 	return (t);
134 }
135