xref: /freebsd/lib/msun/src/s_cbrtf.c (revision 20a990117de27fff03b77503a05ed58d727b988a)
13a8617a8SJordan K. Hubbard /* s_cbrtf.c -- float version of s_cbrt.c.
23a8617a8SJordan K. Hubbard  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3ec761d75SBruce Evans  * Debugged and optimized by Bruce D. Evans.
43a8617a8SJordan K. Hubbard  */
53a8617a8SJordan K. Hubbard 
63a8617a8SJordan K. Hubbard /*
73a8617a8SJordan K. Hubbard  * ====================================================
83a8617a8SJordan K. Hubbard  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
93a8617a8SJordan K. Hubbard  *
103a8617a8SJordan K. Hubbard  * Developed at SunPro, a Sun Microsystems, Inc. business.
113a8617a8SJordan K. Hubbard  * Permission to use, copy, modify, and distribute this
123a8617a8SJordan K. Hubbard  * software is freely granted, provided that this notice
133a8617a8SJordan K. Hubbard  * is preserved.
143a8617a8SJordan K. Hubbard  * ====================================================
153a8617a8SJordan K. Hubbard  */
163a8617a8SJordan K. Hubbard 
173a8617a8SJordan K. Hubbard #ifndef lint
187f3dea24SPeter Wemm static char rcsid[] = "$FreeBSD$";
193a8617a8SJordan K. Hubbard #endif
203a8617a8SJordan K. Hubbard 
213a8617a8SJordan K. Hubbard #include "math.h"
223a8617a8SJordan K. Hubbard #include "math_private.h"
233a8617a8SJordan K. Hubbard 
243a8617a8SJordan K. Hubbard /* cbrtf(x)
253a8617a8SJordan K. Hubbard  * Return cube root of x
263a8617a8SJordan K. Hubbard  */
273a8617a8SJordan K. Hubbard static const unsigned
28af7f9913SBruce Evans 	B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
29af7f9913SBruce Evans 	B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
303a8617a8SJordan K. Hubbard 
3159b19ff1SAlfred Perlstein float
3259b19ff1SAlfred Perlstein cbrtf(float x)
333a8617a8SJordan K. Hubbard {
344bb97803SBruce Evans 	double r,T;
354bb97803SBruce Evans 	float t;
363a8617a8SJordan K. Hubbard 	int32_t hx;
373a8617a8SJordan K. Hubbard 	u_int32_t sign;
383a8617a8SJordan K. Hubbard 	u_int32_t high;
393a8617a8SJordan K. Hubbard 
403a8617a8SJordan K. Hubbard 	GET_FLOAT_WORD(hx,x);
413a8617a8SJordan K. Hubbard 	sign=hx&0x80000000; 		/* sign= sign(x) */
423a8617a8SJordan K. Hubbard 	hx  ^=sign;
433a8617a8SJordan K. Hubbard 	if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
443a8617a8SJordan K. Hubbard 
453a8617a8SJordan K. Hubbard     /* rough cbrt to 5 bits */
4620a99011SBruce Evans 	if(hx<0x00800000) { 		/* zero or subnormal? */
4720a99011SBruce Evans 	    if(hx==0)
4820a99011SBruce Evans 		return(x);		/* cbrt(+-0) is itself */
497d5a4821SBruce Evans 	    SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
507d5a4821SBruce Evans 	    t*=x;
517d5a4821SBruce Evans 	    GET_FLOAT_WORD(high,t);
52ec761d75SBruce Evans 	    SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
537d5a4821SBruce Evans 	} else
54ec761d75SBruce Evans 	    SET_FLOAT_WORD(t,sign|(hx/3+B1));
553a8617a8SJordan K. Hubbard 
56fd289100SBruce Evans     /*
57fd289100SBruce Evans      * First step Newton iteration (solving t*t-x/t == 0) to 16 bits.  In
58fd289100SBruce Evans      * double precision so that its terms can be arranged for efficiency
59fd289100SBruce Evans      * without causing overflow or underflow.
60fd289100SBruce Evans      */
614bb97803SBruce Evans 	T=t;
624bb97803SBruce Evans 	r=T*T*T;
63fd289100SBruce Evans 	T=T*((double)x+x+r)/(x+r+r);
643a8617a8SJordan K. Hubbard 
65fd289100SBruce Evans     /*
66fd289100SBruce Evans      * Second step Newton iteration to 47 bits.  In double precision for
67fd289100SBruce Evans      * efficiency and accuracy.
68fd289100SBruce Evans      */
694bb97803SBruce Evans 	r=T*T*T;
70fd289100SBruce Evans 	T=T*((double)x+x+r)/(x+r+r);
716de073b4SBruce Evans 
724bb97803SBruce Evans     /* rounding to 24 bits is perfect in round-to-nearest mode */
734bb97803SBruce Evans 	return(T);
743a8617a8SJordan K. Hubbard }
75