xref: /freebsd/lib/msun/src/s_cbrtf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1 /* s_cbrtf.c -- float version of s_cbrt.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  * Debugged and optimized by Bruce D. Evans.
4  */
5 
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16 
17 #include "math.h"
18 #include "math_private.h"
19 
20 /* cbrtf(x)
21  * Return cube root of x
22  */
23 static const unsigned
24 	B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
25 	B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
26 
27 float
cbrtf(float x)28 cbrtf(float x)
29 {
30 	double r,T;
31 	float t;
32 	int32_t hx;
33 	u_int32_t sign;
34 	u_int32_t high;
35 
36 	GET_FLOAT_WORD(hx,x);
37 	sign=hx&0x80000000; 		/* sign= sign(x) */
38 	hx  ^=sign;
39 	if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
40 
41     /* rough cbrt to 5 bits */
42 	if(hx<0x00800000) { 		/* zero or subnormal? */
43 	    if(hx==0)
44 		return(x);		/* cbrt(+-0) is itself */
45 	    SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
46 	    t*=x;
47 	    GET_FLOAT_WORD(high,t);
48 	    SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
49 	} else
50 	    SET_FLOAT_WORD(t,sign|(hx/3+B1));
51 
52     /*
53      * First step Newton iteration (solving t*t-x/t == 0) to 16 bits.  In
54      * double precision so that its terms can be arranged for efficiency
55      * without causing overflow or underflow.
56      */
57 	T=t;
58 	r=T*T*T;
59 	T=T*((double)x+x+r)/(x+r+r);
60 
61     /*
62      * Second step Newton iteration to 47 bits.  In double precision for
63      * efficiency and accuracy.
64      */
65 	r=T*T*T;
66 	T=T*((double)x+x+r)/(x+r+r);
67 
68     /* rounding to 24 bits is perfect in round-to-nearest mode */
69 	return(T);
70 }
71