Bring in FreeBSD's msun code for our libm.
[dragonfly.git] / lib / libm / src / s_cbrtf.c
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  * $FreeBSD: head/lib/msun/src/s_cbrtf.c 176451 2008-02-22 02:30:36Z das $
17  */
18
19 #include "math.h"
20 #include "math_private.h"
21
22 /* cbrtf(x)
23  * Return cube root of x
24  */
25 static const unsigned
26         B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
27         B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
28
29 float
30 cbrtf(float x)
31 {
32         double r,T;
33         float t;
34         int32_t hx;
35         u_int32_t sign;
36         u_int32_t high;
37
38         GET_FLOAT_WORD(hx,x);
39         sign=hx&0x80000000;             /* sign= sign(x) */
40         hx  ^=sign;
41         if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */
42
43     /* rough cbrt to 5 bits */
44         if(hx<0x00800000) {             /* zero or subnormal? */
45             if(hx==0)
46                 return(x);              /* cbrt(+-0) is itself */
47             SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */
48             t*=x;
49             GET_FLOAT_WORD(high,t);
50             SET_FLOAT_WORD(t,sign|((high&0x7fffffff)/3+B2));
51         } else
52             SET_FLOAT_WORD(t,sign|(hx/3+B1));
53
54     /*
55      * First step Newton iteration (solving t*t-x/t == 0) to 16 bits.  In
56      * double precision so that its terms can be arranged for efficiency
57      * without causing overflow or underflow.
58      */
59         T=t;
60         r=T*T*T;
61         T=T*((double)x+x+r)/(x+r+r);
62
63     /*
64      * Second step Newton iteration to 47 bits.  In double precision for
65      * efficiency and accuracy.
66      */
67         r=T*T*T;
68         T=T*((double)x+x+r)/(x+r+r);
69
70     /* rounding to 24 bits is perfect in round-to-nearest mode */
71         return(T);
72 }