xref: /csrg-svn/lib/libm/ieee/cbrt.c (revision 42656)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)cbrt.c	5.6 (Berkeley) 06/01/90";
25 #endif /* not lint */
26 
27 #include <sys/stdc.h>
28 
29 /* kahan's cube root (53 bits IEEE double precision)
30  * for IEEE machines only
31  * coded in C by K.C. Ng, 4/30/85
32  *
33  * Accuracy:
34  *	better than 0.667 ulps according to an error analysis. Maximum
35  * error observed was 0.666 ulps in an 1,000,000 random arguments test.
36  *
37  * Warning: this code is semi machine dependent; the ordering of words in
38  * a floating point number must be known in advance. I assume that the
39  * long interger at the address of a floating point number will be the
40  * leading 32 bits of that floating point number (i.e., sign, exponent,
41  * and the 20 most significant bits).
42  * On a National machine, it has different ordering; therefore, this code
43  * must be compiled with flag -DNATIONAL.
44  */
45 #if !defined(vax)&&!defined(tahoe)
46 
47 static const unsigned long
48 		     B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
49 	             B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
50 static const double
51 	    C= 19./35.,
52 	    D= -864./1225.,
53 	    E= 99./70.,
54 	    F= 45./28.,
55 	    G= 5./14.;
56 
57 double cbrt(x)
58 double x;
59 {
60 	double r,s,t=0.0,w;
61 	unsigned long *px = (unsigned long *) &x,
62 	              *pt = (unsigned long *) &t,
63 		      mexp,sign;
64 
65 #ifdef national /* ordering of words in a floating points number */
66 	const int n0=1,n1=0;
67 #else	/* national */
68 	const int n0=0,n1=1;
69 #endif	/* national */
70 
71 	mexp=px[n0]&0x7ff00000;
72 	if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */
73 	if(x==0.0) return(x);		/* cbrt(0) is itself */
74 
75 	sign=px[n0]&0x80000000; /* sign= sign(x) */
76 	px[n0] ^= sign;		/* x=|x| */
77 
78 
79     /* rough cbrt to 5 bits */
80 	if(mexp==0) 		/* subnormal number */
81 	  {pt[n0]=0x43500000; 	/* set t= 2**54 */
82 	   t*=x; pt[n0]=pt[n0]/3+B2;
83 	  }
84 	else
85 	  pt[n0]=px[n0]/3+B1;
86 
87 
88     /* new cbrt to 23 bits, may be implemented in single precision */
89 	r=t*t/x;
90 	s=C+r*t;
91 	t*=G+F/(s+E+D/s);
92 
93     /* chopped to 20 bits and make it larger than cbrt(x) */
94 	pt[n1]=0; pt[n0]+=0x00000001;
95 
96 
97     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
98 	s=t*t;		/* t*t is exact */
99 	r=x/s;
100 	w=t+t;
101 	r=(r-t)/(w+r);	/* r-t is exact */
102 	t=t+t*r;
103 
104 
105     /* retore the sign bit */
106 	pt[n0] |= sign;
107 	return(t);
108 }
109 #endif
110