xref: /freebsd/lib/msun/src/s_cbrtf.c (revision ec761d750157385a47a850f8e6612ce1e0ebac10)
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 
313a8617a8SJordan K. Hubbard static const float
323a8617a8SJordan K. Hubbard C =  5.4285717010e-01, /* 19/35     = 0x3f0af8b0 */
333a8617a8SJordan K. Hubbard D = -7.0530611277e-01, /* -864/1225 = 0xbf348ef1 */
343a8617a8SJordan K. Hubbard E =  1.4142856598e+00, /* 99/70     = 0x3fb50750 */
353a8617a8SJordan K. Hubbard F =  1.6071428061e+00, /* 45/28     = 0x3fcdb6db */
363a8617a8SJordan K. Hubbard G =  3.5714286566e-01; /* 5/14      = 0x3eb6db6e */
373a8617a8SJordan K. Hubbard 
3859b19ff1SAlfred Perlstein float
3959b19ff1SAlfred Perlstein cbrtf(float x)
403a8617a8SJordan K. Hubbard {
416de073b4SBruce Evans 	float r,s,t,w;
423a8617a8SJordan K. Hubbard 	int32_t hx;
433a8617a8SJordan K. Hubbard 	u_int32_t sign;
443a8617a8SJordan K. Hubbard 	u_int32_t high;
453a8617a8SJordan K. Hubbard 
463a8617a8SJordan K. Hubbard 	GET_FLOAT_WORD(hx,x);
473a8617a8SJordan K. Hubbard 	sign=hx&0x80000000; 		/* sign= sign(x) */
483a8617a8SJordan K. Hubbard 	hx  ^=sign;
493a8617a8SJordan K. Hubbard 	if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
503a8617a8SJordan K. Hubbard 	if(hx==0)
513a8617a8SJordan K. Hubbard 	    return(x);		/* cbrt(0) is itself */
523a8617a8SJordan K. Hubbard 
533a8617a8SJordan K. Hubbard     /* rough cbrt to 5 bits */
547d5a4821SBruce Evans 	if(hx<0x00800000) { 		/* subnormal number */
557d5a4821SBruce Evans 	    SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
567d5a4821SBruce Evans 	    t*=x;
577d5a4821SBruce Evans 	    GET_FLOAT_WORD(high,t);
58ec761d75SBruce Evans 	    SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
597d5a4821SBruce Evans 	} else
60ec761d75SBruce Evans 	    SET_FLOAT_WORD(t,sign|(hx/3+B1));
613a8617a8SJordan K. Hubbard 
623a8617a8SJordan K. Hubbard     /* new cbrt to 23 bits */
633a8617a8SJordan K. Hubbard 	r=t*t/x;
643a8617a8SJordan K. Hubbard 	s=C+r*t;
653a8617a8SJordan K. Hubbard 	t*=G+F/(s+E+D/s);
663a8617a8SJordan K. Hubbard 
67ec761d75SBruce Evans     /* chop t to 12 bits and make it larger in magnitude than cbrt(x) */
686de073b4SBruce Evans 	GET_FLOAT_WORD(high,t);
69288a8c86SBruce Evans 	SET_FLOAT_WORD(t,(high&0xfffff000)+0x00001000);
706de073b4SBruce Evans 
71288a8c86SBruce Evans     /* one step Newton iteration to 24 bits with error less than 0.667 ulps */
726de073b4SBruce Evans 	s=t*t;		/* t*t is exact */
736de073b4SBruce Evans 	r=x/s;
746de073b4SBruce Evans 	w=t+t;
756de073b4SBruce Evans 	r=(r-t)/(w+r);	/* r-t is exact */
766de073b4SBruce Evans 	t=t+t*r;
776de073b4SBruce Evans 
783a8617a8SJordan K. Hubbard 	return(t);
793a8617a8SJordan K. Hubbard }
80