12116Sjkh/* s_cbrtf.c -- float version of s_cbrt.c.
22116Sjkh * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3153386Sbde * Debugged and optimized by Bruce D. Evans.
42116Sjkh */
52116Sjkh
62116Sjkh/*
72116Sjkh * ====================================================
82116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
92116Sjkh *
102116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
112116Sjkh * Permission to use, copy, modify, and distribute this
128870Srgrimes * software is freely granted, provided that this notice
132116Sjkh * is preserved.
142116Sjkh * ====================================================
152116Sjkh */
162116Sjkh
17176451Sdas#include <sys/cdefs.h>
18176451Sdas__FBSDID("$FreeBSD$");
192116Sjkh
202116Sjkh#include "math.h"
212116Sjkh#include "math_private.h"
222116Sjkh
232116Sjkh/* cbrtf(x)
242116Sjkh * Return cube root of x
252116Sjkh */
268870Srgrimesstatic const unsigned
27153306Sbde	B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
28153306Sbde	B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
292116Sjkh
3097413Salfredfloat
3197413Salfredcbrtf(float x)
322116Sjkh{
33154049Sbde	double r,T;
34154049Sbde	float t;
352116Sjkh	int32_t hx;
362116Sjkh	u_int32_t sign;
372116Sjkh	u_int32_t high;
382116Sjkh
392116Sjkh	GET_FLOAT_WORD(hx,x);
402116Sjkh	sign=hx&0x80000000; 		/* sign= sign(x) */
412116Sjkh	hx  ^=sign;
422116Sjkh	if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
432116Sjkh
442116Sjkh    /* rough cbrt to 5 bits */
45170089Sbde	if(hx<0x00800000) { 		/* zero or subnormal? */
46170089Sbde	    if(hx==0)
47170089Sbde		return(x);		/* cbrt(+-0) is itself */
48153382Sbde	    SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
49153382Sbde	    t*=x;
50153382Sbde	    GET_FLOAT_WORD(high,t);
51153386Sbde	    SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
52153382Sbde	} else
53153386Sbde	    SET_FLOAT_WORD(t,sign|(hx/3+B1));
542116Sjkh
55154051Sbde    /*
56154051Sbde     * First step Newton iteration (solving t*t-x/t == 0) to 16 bits.  In
57154051Sbde     * double precision so that its terms can be arranged for efficiency
58154051Sbde     * without causing overflow or underflow.
59154051Sbde     */
60154049Sbde	T=t;
61154049Sbde	r=T*T*T;
62154051Sbde	T=T*((double)x+x+r)/(x+r+r);
632116Sjkh
64154051Sbde    /*
65154051Sbde     * Second step Newton iteration to 47 bits.  In double precision for
66154051Sbde     * efficiency and accuracy.
67154051Sbde     */
68154049Sbde	r=T*T*T;
69154051Sbde	T=T*((double)x+x+r)/(x+r+r);
70153303Sbde
71154049Sbde    /* rounding to 24 bits is perfect in round-to-nearest mode */
72154049Sbde	return(T);
732116Sjkh}
74