xref: /minix3/lib/libm/src/s_cbrtf.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* s_cbrtf.c -- float version of s_cbrt.c.
2*2fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras  */
4*2fe8fb19SBen Gras 
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras  * ====================================================
7*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras  * is preserved.
13*2fe8fb19SBen Gras  * ====================================================
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: s_cbrtf.c,v 1.7 2002/05/26 22:01:54 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras 
24*2fe8fb19SBen Gras /* cbrtf(x)
25*2fe8fb19SBen Gras  * Return cube root of x
26*2fe8fb19SBen Gras  */
27*2fe8fb19SBen Gras static const unsigned
28*2fe8fb19SBen Gras 	B1 = 709958130, /* B1 = (84+2/3-0.03306235651)*2**23 */
29*2fe8fb19SBen Gras 	B2 = 642849266; /* B2 = (76+2/3-0.03306235651)*2**23 */
30*2fe8fb19SBen Gras 
31*2fe8fb19SBen Gras static const float
32*2fe8fb19SBen Gras C =  5.4285717010e-01, /* 19/35     = 0x3f0af8b0 */
33*2fe8fb19SBen Gras D = -7.0530611277e-01, /* -864/1225 = 0xbf348ef1 */
34*2fe8fb19SBen Gras E =  1.4142856598e+00, /* 99/70     = 0x3fb50750 */
35*2fe8fb19SBen Gras F =  1.6071428061e+00, /* 45/28     = 0x3fcdb6db */
36*2fe8fb19SBen Gras G =  3.5714286566e-01; /* 5/14      = 0x3eb6db6e */
37*2fe8fb19SBen Gras 
38*2fe8fb19SBen Gras float
cbrtf(float x)39*2fe8fb19SBen Gras cbrtf(float x)
40*2fe8fb19SBen Gras {
41*2fe8fb19SBen Gras 	float r,s,t;
42*2fe8fb19SBen Gras 	int32_t hx;
43*2fe8fb19SBen Gras 	u_int32_t sign;
44*2fe8fb19SBen Gras 	u_int32_t high;
45*2fe8fb19SBen Gras 
46*2fe8fb19SBen Gras 	GET_FLOAT_WORD(hx,x);
47*2fe8fb19SBen Gras 	sign=hx&0x80000000; 		/* sign= sign(x) */
48*2fe8fb19SBen Gras 	hx  ^=sign;
49*2fe8fb19SBen Gras 	if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
50*2fe8fb19SBen Gras 	if(hx==0)
51*2fe8fb19SBen Gras 	    return(x);		/* cbrt(0) is itself */
52*2fe8fb19SBen Gras 
53*2fe8fb19SBen Gras 	SET_FLOAT_WORD(x,hx);	/* x <- |x| */
54*2fe8fb19SBen Gras     /* rough cbrt to 5 bits */
55*2fe8fb19SBen Gras 	if(hx<0x00800000) 		/* subnormal number */
56*2fe8fb19SBen Gras 	  {SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
57*2fe8fb19SBen Gras 	   t*=x; GET_FLOAT_WORD(high,t); SET_FLOAT_WORD(t,high/3+B2);
58*2fe8fb19SBen Gras 	  }
59*2fe8fb19SBen Gras 	else
60*2fe8fb19SBen Gras 	  SET_FLOAT_WORD(t,hx/3+B1);
61*2fe8fb19SBen Gras 
62*2fe8fb19SBen Gras 
63*2fe8fb19SBen Gras     /* new cbrt to 23 bits */
64*2fe8fb19SBen Gras 	r=t*t/x;
65*2fe8fb19SBen Gras 	s=C+r*t;
66*2fe8fb19SBen Gras 	t*=G+F/(s+E+D/s);
67*2fe8fb19SBen Gras 
68*2fe8fb19SBen Gras     /* retore the sign bit */
69*2fe8fb19SBen Gras 	GET_FLOAT_WORD(high,t);
70*2fe8fb19SBen Gras 	SET_FLOAT_WORD(t,high|sign);
71*2fe8fb19SBen Gras 	return(t);
72*2fe8fb19SBen Gras }
73