fix long double const workaround in cbrtl
[musl] / src / math / cbrtl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtl.c */
2 /*-
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  *
13  * The argument reduction and testing for exceptional cases was
14  * written by Steven G. Kargl with input from Bruce D. Evans
15  * and David A. Schultz.
16  */
17
18 #include "libm.h"
19
20 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
21 long double cbrtl(long double x)
22 {
23         return cbrt(x);
24 }
25 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
26 #define BIAS    (LDBL_MAX_EXP - 1)
27 static const unsigned
28 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
29
30 long double cbrtl(long double x)
31 {
32         union IEEEl2bits u, v;
33         long double r, s, t, w;
34         double dr, dt, dx;
35         float ft, fx;
36         uint32_t hx;
37         uint16_t expsign;
38         int k;
39
40         u.e = x;
41         expsign = u.xbits.expsign;
42         k = expsign & 0x7fff;
43
44         /*
45          * If x = +-Inf, then cbrt(x) = +-Inf.
46          * If x = NaN, then cbrt(x) = NaN.
47          */
48         if (k == BIAS + LDBL_MAX_EXP)
49                 return x + x;
50
51 // FIXME: extended precision is default on linux..
52 #undef __i386__
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 #if LDBL_MANT_DIG == 64
116         /*
117          * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
118          * Round it away from zero to 32 bits (32 so that t*t is exact, and
119          * away from zero for technical reasons).
120          */
121         t = dt + (0x1.0p32L + 0x1.0p-32L) - 0x1.0p32;
122 #elif LDBL_MANT_DIG == 113
123         /*
124          * Round dt away from zero to 47 bits.  Since we don't trust the 47,
125          * add 2 47-bit ulps instead of 1 to round up.  Rounding is slow and
126          * might be avoidable in this case, since on most machines dt will
127          * have been evaluated in 53-bit precision and the technical reasons
128          * for rounding up might not apply to either case in cbrtl() since
129          * dt is much more accurate than needed.
130          */
131         t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
132 #else
133 #error "Unsupported long double format"
134 #endif
135
136         /*
137          * Final step Newton iteration to 64 or 113 bits with
138          * error < 0.667 ulps
139          */
140         s = t*t;         /* t*t is exact */
141         r = x/s;         /* error <= 0.5 ulps; |r| < |t| */
142         w = t+t;         /* t+t is exact */
143         r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
144         t = t+t*r;       /* error <= 0.5 + 0.5/3 + epsilon */
145
146         t *= v.e;
147 #ifdef __i386__
148         if (oprec != FP_PE)
149                 fpsetprec(oprec);
150 #endif
151         return t;
152 }
153 #endif