Branch data Line data Source code
1 : : /* s_cbrtf.c -- float version of s_cbrt.c.
2 : : * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 : : * Debugged and optimized by Bruce D. Evans.
4 : : */
5 : :
6 : : /*
7 : : * ====================================================
8 : : * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9 : : *
10 : : * Developed at SunPro, a Sun Microsystems, Inc. business.
11 : : * Permission to use, copy, modify, and distribute this
12 : : * software is freely granted, provided that this notice
13 : : * is preserved.
14 : : * ====================================================
15 : : */
16 : :
17 : : #include "cdefs-compat.h"
18 : : //__FBSDID("$FreeBSD: src/lib/msun/src/s_cbrtf.c,v 1.18 2008/02/22 02:30:35 das Exp $");
19 : :
20 : : #include <openlibm_math.h>
21 : :
22 : : #include "math_private.h"
23 : :
24 : : /* cbrtf(x)
25 : : * Return cube root of x
26 : : */
27 : : static const unsigned
28 : : B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
29 : : B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
30 : :
31 : : OLM_DLLEXPORT float
32 : 11 : cbrtf(float x)
33 : : {
34 : : double r,T;
35 : : float t;
36 : : int32_t hx;
37 : : u_int32_t sign;
38 : : u_int32_t high;
39 : :
40 : 11 : GET_FLOAT_WORD(hx,x);
41 : 11 : sign=hx&0x80000000; /* sign= sign(x) */
42 : 11 : hx ^=sign;
43 [ + + ]: 11 : if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
44 : :
45 : : /* rough cbrt to 5 bits */
46 [ + + ]: 8 : if(hx<0x00800000) { /* zero or subnormal? */
47 [ + - ]: 2 : if(hx==0)
48 : 2 : return(x); /* cbrt(+-0) is itself */
49 : 0 : SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
50 : 0 : t*=x;
51 : 0 : GET_FLOAT_WORD(high,t);
52 : 0 : SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
53 : : } else
54 : 6 : SET_FLOAT_WORD(t,sign|(hx/3+B1));
55 : :
56 : : /*
57 : : * First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In
58 : : * double precision so that its terms can be arranged for efficiency
59 : : * without causing overflow or underflow.
60 : : */
61 : 6 : T=t;
62 : 6 : r=T*T*T;
63 : 6 : T=T*((double)x+x+r)/(x+r+r);
64 : :
65 : : /*
66 : : * Second step Newton iteration to 47 bits. In double precision for
67 : : * efficiency and accuracy.
68 : : */
69 : 6 : r=T*T*T;
70 : 6 : T=T*((double)x+x+r)/(x+r+r);
71 : :
72 : : /* rounding to 24 bits is perfect in round-to-nearest mode */
73 : 6 : return(T);
74 : : }
|