xref: /inferno-os/libmath/fdlibm/s_cbrt.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 /* derived from /netlib/fdlibm */
2 
3 /* @(#)s_cbrt.c 1.3 95/01/18 */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  *
14  */
15 
16 #include "fdlibm.h"
17 
18 /* cbrt(x)
19  * Return cube root of x
20  */
21 static const unsigned
22 	B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
23 	B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
24 
25 static const double
26 C =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
27 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
28 E =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
29 F =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
30 G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
31 
cbrt(double x)32 	double cbrt(double x)
33 {
34 	int	hx;
35 	double r,s,t=0.0,w;
36 	unsigned sign;
37 
38 
39 	hx = __HI(x);		/* high word of x */
40 	sign=hx&0x80000000; 		/* sign= sign(x) */
41 	hx  ^=sign;
42 	if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
43 	if((hx|__LO(x))==0)
44 	    return(x);		/* cbrt(0) is itself */
45 
46 	__HI(x) = hx;	/* x <- |x| */
47     /* rough cbrt to 5 bits */
48 	if(hx<0x00100000) 		/* subnormal number */
49 	  {__HI(t)=0x43500000; 		/* set t= 2**54 */
50 	   t*=x; __HI(t)=__HI(t)/3+B2;
51 	  }
52 	else
53 	  __HI(t)=hx/3+B1;
54 
55 
56     /* new cbrt to 23 bits, may be implemented in single precision */
57 	r=t*t/x;
58 	s=C+r*t;
59 	t*=G+F/(s+E+D/s);
60 
61     /* chopped to 20 bits and make it larger than cbrt(x) */
62 	__LO(t)=0; __HI(t)+=0x00000001;
63 
64 
65     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
66 	s=t*t;		/* t*t is exact */
67 	r=x/s;
68 	w=t+t;
69 	r=(r-t)/(w+r);	/* r-s is exact */
70 	t=t+t*r;
71 
72     /* retore the sign bit */
73 	__HI(t) |= sign;
74 	return(t);
75 }
76