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 #include "math.h"
183a8617a8SJordan K. Hubbard #include "math_private.h"
193a8617a8SJordan K. Hubbard
203a8617a8SJordan K. Hubbard /* cbrtf(x)
213a8617a8SJordan K. Hubbard * Return cube root of x
223a8617a8SJordan K. Hubbard */
233a8617a8SJordan K. Hubbard static const unsigned
24af7f9913SBruce Evans B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
25af7f9913SBruce Evans B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
263a8617a8SJordan K. Hubbard
2759b19ff1SAlfred Perlstein float
cbrtf(float x)2859b19ff1SAlfred Perlstein cbrtf(float x)
293a8617a8SJordan K. Hubbard {
304bb97803SBruce Evans double r,T;
314bb97803SBruce Evans float t;
323a8617a8SJordan K. Hubbard int32_t hx;
333a8617a8SJordan K. Hubbard u_int32_t sign;
343a8617a8SJordan K. Hubbard u_int32_t high;
353a8617a8SJordan K. Hubbard
363a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hx,x);
373a8617a8SJordan K. Hubbard sign=hx&0x80000000; /* sign= sign(x) */
383a8617a8SJordan K. Hubbard hx ^=sign;
393a8617a8SJordan K. Hubbard if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
403a8617a8SJordan K. Hubbard
413a8617a8SJordan K. Hubbard /* rough cbrt to 5 bits */
4220a99011SBruce Evans if(hx<0x00800000) { /* zero or subnormal? */
4320a99011SBruce Evans if(hx==0)
4420a99011SBruce Evans return(x); /* cbrt(+-0) is itself */
457d5a4821SBruce Evans SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
467d5a4821SBruce Evans t*=x;
477d5a4821SBruce Evans GET_FLOAT_WORD(high,t);
48ec761d75SBruce Evans SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
497d5a4821SBruce Evans } else
50ec761d75SBruce Evans SET_FLOAT_WORD(t,sign|(hx/3+B1));
513a8617a8SJordan K. Hubbard
52fd289100SBruce Evans /*
53fd289100SBruce Evans * First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In
54fd289100SBruce Evans * double precision so that its terms can be arranged for efficiency
55fd289100SBruce Evans * without causing overflow or underflow.
56fd289100SBruce Evans */
574bb97803SBruce Evans T=t;
584bb97803SBruce Evans r=T*T*T;
59fd289100SBruce Evans T=T*((double)x+x+r)/(x+r+r);
603a8617a8SJordan K. Hubbard
61fd289100SBruce Evans /*
62fd289100SBruce Evans * Second step Newton iteration to 47 bits. In double precision for
63fd289100SBruce Evans * efficiency and accuracy.
64fd289100SBruce Evans */
654bb97803SBruce Evans r=T*T*T;
66fd289100SBruce Evans T=T*((double)x+x+r)/(x+r+r);
676de073b4SBruce Evans
684bb97803SBruce Evans /* rounding to 24 bits is perfect in round-to-nearest mode */
694bb97803SBruce Evans return(T);
703a8617a8SJordan K. Hubbard }
71