libm: Sync with FreeBSD (~50 math functions added)
[dragonfly.git] / lib / libm / 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  * FreeBSD SVN: 219576 (2011-03-12)
17  */
18
19 #include <float.h>
20 #include <math.h>
21 #include <ieeefp.h>
22
23 #include "fpmath.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
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 #ifdef __i386__
54         fp_prec_t oprec;
55
56         oprec = fpgetprec();
57         if (oprec != FP_PE)
58                 fpsetprec(FP_PE);
59 #endif
60
61         if (k == 0) {
62                 /* If x = +-0, then cbrt(x) = +-0. */
63                 if ((u.bits.manh | u.bits.manl) == 0) {
64 #ifdef __i386__
65                         if (oprec != FP_PE)
66                                 fpsetprec(oprec);
67 #endif
68                         return (x);
69                 }
70                 /* Adjust subnormal numbers. */
71                 u.e *= 0x1.0p514;
72                 k = u.bits.exp;
73                 k -= BIAS + 514;
74         } else
75                 k -= BIAS;
76         u.xbits.expsign = BIAS;
77         v.e = 1;
78
79         x = u.e;
80         switch (k % 3) {
81         case 1:
82         case -2:
83                 x = 2*x;
84                 k--;
85                 break;
86         case 2:
87         case -1:
88                 x = 4*x;
89                 k -= 2;
90                 break;
91         }
92         v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
93
94         /*
95          * The following is the guts of s_cbrtf, with the handling of
96          * special values removed and extra care for accuracy not taken,
97          * but with most of the extra accuracy not discarded.
98          */
99
100         /* ~5-bit estimate: */
101         fx = x;
102         GET_FLOAT_WORD(hx, fx);
103         SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
104
105         /* ~16-bit estimate: */
106         dx = x;
107         dt = ft;
108         dr = dt * dt * dt;
109         dt = dt * (dx + dx + dr) / (dx + dr + dr);
110
111         /* ~47-bit estimate: */
112         dr = dt * dt * dt;
113         dt = dt * (dx + dx + dr) / (dx + dr + dr);
114
115         /*
116          * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
117          * Round it away from zero to 32 bits (32 so that t*t is exact, and
118          * away from zero for technical reasons).
119          */
120         volatile double vd2 = 0x1.0p32;
121         volatile double vd1 = 0x1.0p-31;
122         #define vd ((long double)vd2 + vd1)
123
124         t = dt + vd - 0x1.0p32;
125
126         /*
127          * Final step Newton iteration to 64 or 113 bits with
128          * error < 0.667 ulps
129          */
130         s=t*t;                          /* t*t is exact */
131         r=x/s;                          /* error <= 0.5 ulps; |r| < |t| */
132         w=t+t;                          /* t+t is exact */
133         r=(r-t)/(w+r);                  /* r-t is exact; w+r ~= 3*t */
134         t=t+t*r;                        /* error <= 0.5 + 0.5/3 + epsilon */
135
136         t *= v.e;
137 #ifdef __i386__
138         if (oprec != FP_PE)
139                 fpsetprec(oprec);
140 #endif
141         return (t);
142 }