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