xref: /csrg-svn/lib/libm/ieee/cbrt.c (revision 24719)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)cbrt.c	1.1 (Berkeley) 5/23/85; 1.2 (ucb.elefunt) 09/12/85";
17 #endif not lint
18 
19 /* kahan's cube root (53 bits IEEE double precision)
20  * for IEEE machines only
21  * coded in C by K.C. Ng, 4/30/85
22  *
23  * Accuracy:
24  *	better than 0.667 ulps according to an error analysis. Maximum
25  * error observed was 0.666 ulps in an 1,000,000 random arguments test.
26  *
27  * Warning: this code is semi machine dependent; the ordering of words in
28  * a floating point number must be known in advance. I assume that the
29  * long interger at the address of a floating point number will be the
30  * leading 32 bits of that floating point number (i.e., sign, exponent,
31  * and the 20 most significant bits).
32  * On a National machine, it has different ordering; therefore, this code
33  * must be compiled with flag -DNATIONAL.
34  */
35 #ifndef VAX
36 
37 static unsigned long B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
38 	             B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
39 static double
40 	    C= 19./35.,
41 	    D= -864./1225.,
42 	    E= 99./70.,
43 	    F= 45./28.,
44 	    G= 5./14.;
45 
46 double cbrt(x)
47 double x;
48 {
49 	double r,s,t=0.0,w;
50 	unsigned long *px = (unsigned long *) &x,
51 	              *pt = (unsigned long *) &t,
52 		      mexp,sign;
53 
54 #ifdef NATIONAL /* ordering of words in a floating points number */
55 	int n0=1,n1=0;
56 #else
57 	int n0=0,n1=1;
58 #endif
59 
60 	mexp=px[n0]&0x7ff00000;
61 	if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */
62 	if(x==0.0) return(x);		/* cbrt(0) is itself */
63 
64 	sign=px[n0]&0x80000000; /* sign= sign(x) */
65 	px[n0] ^= sign;		/* x=|x| */
66 
67 
68     /* rough cbrt to 5 bits */
69 	if(mexp==0) 		/* subnormal number */
70 	  {pt[n0]=0x43500000; 	/* set t= 2**54 */
71 	   t*=x; pt[n0]=pt[n0]/3+B2;
72 	  }
73 	else
74 	  pt[n0]=px[n0]/3+B1;
75 
76 
77     /* new cbrt to 23 bits, may be implemented in single precision */
78 	r=t*t/x;
79 	s=C+r*t;
80 	t*=G+F/(s+E+D/s);
81 
82     /* chopped to 20 bits and make it larger than cbrt(x) */
83 	pt[n1]=0; pt[n0]+=0x00000001;
84 
85 
86     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
87 	s=t*t;		/* t*t is exact */
88 	r=x/s;
89 	w=t+t;
90 	r=(r-t)/(w+r);	/* r-s is exact */
91 	t=t+t*r;
92 
93 
94     /* retore the sign bit */
95 	pt[n0] |= sign;
96 	return(t);
97 }
98 #endif
99