rework langinfo code for ABI compat and for use by time code
[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
27 #define BIAS (LDBL_MAX_EXP - 1)
28 static const unsigned 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         if (k == 0) {
52                 /* If x = +-0, then cbrt(x) = +-0. */
53                 if ((u.bits.manh | u.bits.manl) == 0)
54                         return x;
55                 /* Adjust subnormal numbers. */
56                 u.e *= 0x1.0p514;
57                 k = u.bits.exp;
58                 k -= BIAS + 514;
59         } else
60                 k -= BIAS;
61         u.xbits.expsign = BIAS;
62         v.e = 1;
63
64         x = u.e;
65         switch (k % 3) {
66         case 1:
67         case -2:
68                 x = 2*x;
69                 k--;
70                 break;
71         case 2:
72         case -1:
73                 x = 4*x;
74                 k -= 2;
75                 break;
76         }
77         v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
78
79         /*
80          * The following is the guts of s_cbrtf, with the handling of
81          * special values removed and extra care for accuracy not taken,
82          * but with most of the extra accuracy not discarded.
83          */
84
85         /* ~5-bit estimate: */
86         fx = x;
87         GET_FLOAT_WORD(hx, fx);
88         SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
89
90         /* ~16-bit estimate: */
91         dx = x;
92         dt = ft;
93         dr = dt * dt * dt;
94         dt = dt * (dx + dx + dr) / (dx + dr + dr);
95
96         /* ~47-bit estimate: */
97         dr = dt * dt * dt;
98         dt = dt * (dx + dx + dr) / (dx + dr + dr);
99
100 #if LDBL_MANT_DIG == 64
101         /*
102          * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
103          * Round it away from zero to 32 bits (32 so that t*t is exact, and
104          * away from zero for technical reasons).
105          */
106         t = dt + (0x1.0p32L + 0x1.0p-31L) - 0x1.0p32;
107 #elif LDBL_MANT_DIG == 113
108         /*
109          * Round dt away from zero to 47 bits.  Since we don't trust the 47,
110          * add 2 47-bit ulps instead of 1 to round up.  Rounding is slow and
111          * might be avoidable in this case, since on most machines dt will
112          * have been evaluated in 53-bit precision and the technical reasons
113          * for rounding up might not apply to either case in cbrtl() since
114          * dt is much more accurate than needed.
115          */
116         t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
117 #endif
118
119         /*
120          * Final step Newton iteration to 64 or 113 bits with
121          * error < 0.667 ulps
122          */
123         s = t*t;         /* t*t is exact */
124         r = x/s;         /* error <= 0.5 ulps; |r| < |t| */
125         w = t+t;         /* t+t is exact */
126         r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
127         t = t+t*r;       /* error <= 0.5 + 0.5/3 + epsilon */
128
129         t *= v.e;
130         return t;
131 }
132 #endif