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