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