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