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