xref: /freebsd/lib/msun/src/s_cbrtf.c (revision 4bb9780353ed9c7311a608508fea74bc29262d92)
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 	if(hx==0)
453a8617a8SJordan K. Hubbard 	    return(x);			/* cbrt(0) is itself */
463a8617a8SJordan K. Hubbard 
473a8617a8SJordan K. Hubbard     /* rough cbrt to 5 bits */
487d5a4821SBruce Evans 	if(hx<0x00800000) { 		/* subnormal number */
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 
564bb97803SBruce Evans     /* first step Newton iteration (solving t*t-x/t == 0) to 16 bits */
574bb97803SBruce Evans     /* in double precision to avoid problems with denormals */
584bb97803SBruce Evans 	T=t;
594bb97803SBruce Evans 	r=T*T*T;
604bb97803SBruce Evans 	T=T*(x+x+r)/(x+r+r);
613a8617a8SJordan K. Hubbard 
624bb97803SBruce Evans     /* second step Newton iteration to 47 bits */
634bb97803SBruce Evans     /* in double precision for accuracy */
644bb97803SBruce Evans 	r=T*T*T;
654bb97803SBruce Evans 	T=T*(x+x+r)/(x+r+r);
666de073b4SBruce Evans 
674bb97803SBruce Evans     /* rounding to 24 bits is perfect in round-to-nearest mode */
684bb97803SBruce Evans 	return(T);
693a8617a8SJordan K. Hubbard }
70