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