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