rtld: increase TLS storage space (bug 2566)
[dragonfly.git] / lib / libm / src / s_cbrt.c
1 /* @(#)s_cbrt.c 5.1 93/09/24 */
2 /* $FreeBSD: head/lib/msun/src/s_cbrt.c 219571 2011-03-12 16:50:39Z kargl $ */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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  * Optimized by Bruce D. Evans.
14  */
15
16 #include "math.h"
17 #include "math_private.h"
18
19 /* cbrt(x)
20  * Return cube root of x
21  */
22 static const u_int32_t
23         B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
24         B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
25
26 /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
27 static const double
28 P0 =  1.87595182427177009643,           /* 0x3ffe03e6, 0x0f61e692 */
29 P1 = -1.88497979543377169875,           /* 0xbffe28e0, 0x92f02420 */
30 P2 =  1.621429720105354466140,          /* 0x3ff9f160, 0x4a49d6c2 */
31 P3 = -0.758397934778766047437,          /* 0xbfe844cb, 0xbee751d9 */
32 P4 =  0.145996192886612446982;          /* 0x3fc2b000, 0xd4e4edd7 */
33
34 double
35 cbrt(double x)
36 {
37         int32_t hx;
38         union {
39             double value;
40             uint64_t bits;
41         } u;
42         double r,s,t=0.0,w;
43         u_int32_t sign;
44         u_int32_t high,low;
45
46         EXTRACT_WORDS(hx,low,x);
47         sign=hx&0x80000000;             /* sign= sign(x) */
48         hx  ^=sign;
49         if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
50
51     /*
52      * Rough cbrt to 5 bits:
53      *    cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
54      * where e is integral and >= 0, m is real and in [0, 1), and "/" and
55      * "%" are integer division and modulus with rounding towards minus
56      * infinity.  The RHS is always >= the LHS and has a maximum relative
57      * error of about 1 in 16.  Adding a bias of -0.03306235651 to the
58      * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
59      * floating point representation, for finite positive normal values,
60      * ordinary integer divison of the value in bits magically gives
61      * almost exactly the RHS of the above provided we first subtract the
62      * exponent bias (1023 for doubles) and later add it back.  We do the
63      * subtraction virtually to keep e >= 0 so that ordinary integer
64      * division rounds towards minus infinity; this is also efficient.
65      */
66         if(hx<0x00100000) {             /* zero or subnormal? */
67             if((hx|low)==0)
68                 return(x);              /* cbrt(0) is itself */
69             SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
70             t*=x;
71             GET_HIGH_WORD(high,t);
72             INSERT_WORDS(t,sign|((high&0x7fffffff)/3+B2),0);
73         } else
74             INSERT_WORDS(t,sign|(hx/3+B1),0);
75
76     /*
77      * New cbrt to 23 bits:
78      *    cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
79      * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
80      * to within 2**-23.5 when |r - 1| < 1/10.  The rough approximation
81      * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
82      * gives us bounds for r = t**3/x.
83      *
84      * Try to optimize for parallel evaluation as in k_tanf.c.
85      */
86         r=(t*t)*(t/x);
87         t=t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4));
88
89     /*
90      * Round t away from zero to 23 bits (sloppily except for ensuring that
91      * the result is larger in magnitude than cbrt(x) but not much more than
92      * 2 23-bit ulps larger).  With rounding towards zero, the error bound
93      * would be ~5/6 instead of ~4/6.  With a maximum error of 2 23-bit ulps
94      * in the rounded t, the infinite-precision error in the Newton
95      * approximation barely affects third digit in the final error
96      * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
97      * before the final error is larger than 0.667 ulps.
98      */
99         u.value=t;
100         u.bits=(u.bits+0x80000000)&0xffffffffc0000000ULL;
101         t=u.value;
102
103     /* one step Newton iteration to 53 bits with error < 0.667 ulps */
104         s=t*t;                          /* t*t is exact */
105         r=x/s;                          /* error <= 0.5 ulps; |r| < |t| */
106         w=t+t;                          /* t+t is exact */
107         r=(r-t)/(w+r);                  /* r-t is exact; w+r ~= 3*t */
108         t=t+t*r;                        /* error <= 0.5 + 0.5/3 + epsilon */
109
110         return(t);
111 }
112
113 #if (LDBL_MANT_DIG == 53)
114 __weak_reference(cbrt, cbrtl);
115 #endif