xref: /csrg-svn/lib/libm/ieee/cbrt.c (revision 35681)
134123Sbostic /*
224580Szliu  * Copyright (c) 1985 Regents of the University of California.
334123Sbostic  * All rights reserved.
434123Sbostic  *
534123Sbostic  * Redistribution and use in source and binary forms are permitted
634925Sbostic  * provided that the above copyright notice and this paragraph are
734925Sbostic  * duplicated in all such forms and that any documentation,
834925Sbostic  * advertising materials, and other materials related to such
934925Sbostic  * distribution and use acknowledge that the software was developed
1034925Sbostic  * by the University of California, Berkeley.  The name of the
1134925Sbostic  * University may not be used to endorse or promote products derived
1234925Sbostic  * from this software without specific prior written permission.
1334925Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434925Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534925Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634123Sbostic  *
1734123Sbostic  * All recipients should regard themselves as participants in an ongoing
1834123Sbostic  * research project and hence should feel obligated to report their
1934123Sbostic  * experiences (good or bad) with these elementary function codes, using
2034123Sbostic  * the sendbug(8) program, to the authors.
2124580Szliu  */
2224580Szliu 
2324580Szliu #ifndef lint
24*35681Sbostic static char sccsid[] = "@(#)cbrt.c	5.5 (Berkeley) 09/22/88";
2534123Sbostic #endif /* not lint */
2624580Szliu 
2724580Szliu /* kahan's cube root (53 bits IEEE double precision)
2824580Szliu  * for IEEE machines only
2924580Szliu  * coded in C by K.C. Ng, 4/30/85
3024580Szliu  *
3124580Szliu  * Accuracy:
3224580Szliu  *	better than 0.667 ulps according to an error analysis. Maximum
3324580Szliu  * error observed was 0.666 ulps in an 1,000,000 random arguments test.
3424580Szliu  *
3524580Szliu  * Warning: this code is semi machine dependent; the ordering of words in
3624580Szliu  * a floating point number must be known in advance. I assume that the
3724580Szliu  * long interger at the address of a floating point number will be the
3824580Szliu  * leading 32 bits of that floating point number (i.e., sign, exponent,
3924580Szliu  * and the 20 most significant bits).
4024580Szliu  * On a National machine, it has different ordering; therefore, this code
4124580Szliu  * must be compiled with flag -DNATIONAL.
4224580Szliu  */
4331856Szliu #if !defined(vax)&&!defined(tahoe)
4424580Szliu 
45*35681Sbostic static const unsigned long
46*35681Sbostic 		     B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
4724580Szliu 	             B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
48*35681Sbostic static const double
4924580Szliu 	    C= 19./35.,
5024580Szliu 	    D= -864./1225.,
5124580Szliu 	    E= 99./70.,
5224580Szliu 	    F= 45./28.,
5324580Szliu 	    G= 5./14.;
5424580Szliu 
5524580Szliu double cbrt(x)
5624580Szliu double x;
5724580Szliu {
5824580Szliu 	double r,s,t=0.0,w;
5924580Szliu 	unsigned long *px = (unsigned long *) &x,
6024580Szliu 	              *pt = (unsigned long *) &t,
6124580Szliu 		      mexp,sign;
6224580Szliu 
6331856Szliu #ifdef national /* ordering of words in a floating points number */
64*35681Sbostic 	const int n0=1,n1=0;
6531856Szliu #else	/* national */
66*35681Sbostic 	const int n0=0,n1=1;
6731856Szliu #endif	/* national */
6824580Szliu 
6924580Szliu 	mexp=px[n0]&0x7ff00000;
7024580Szliu 	if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */
7124580Szliu 	if(x==0.0) return(x);		/* cbrt(0) is itself */
7224580Szliu 
7324580Szliu 	sign=px[n0]&0x80000000; /* sign= sign(x) */
7424580Szliu 	px[n0] ^= sign;		/* x=|x| */
7524580Szliu 
7624580Szliu 
7724580Szliu     /* rough cbrt to 5 bits */
7824580Szliu 	if(mexp==0) 		/* subnormal number */
7924580Szliu 	  {pt[n0]=0x43500000; 	/* set t= 2**54 */
8024580Szliu 	   t*=x; pt[n0]=pt[n0]/3+B2;
8124580Szliu 	  }
8224580Szliu 	else
8324580Szliu 	  pt[n0]=px[n0]/3+B1;
8424580Szliu 
8524580Szliu 
8624580Szliu     /* new cbrt to 23 bits, may be implemented in single precision */
8724580Szliu 	r=t*t/x;
8824580Szliu 	s=C+r*t;
8924580Szliu 	t*=G+F/(s+E+D/s);
9024580Szliu 
9124580Szliu     /* chopped to 20 bits and make it larger than cbrt(x) */
9224580Szliu 	pt[n1]=0; pt[n0]+=0x00000001;
9324580Szliu 
9424580Szliu 
9524580Szliu     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
9624580Szliu 	s=t*t;		/* t*t is exact */
9724580Szliu 	r=x/s;
9824580Szliu 	w=t+t;
9933938Sbostic 	r=(r-t)/(w+r);	/* r-t is exact */
10024580Szliu 	t=t+t*r;
10124580Szliu 
10224580Szliu 
10324580Szliu     /* retore the sign bit */
10424580Szliu 	pt[n0] |= sign;
10524580Szliu 	return(t);
10624580Szliu }
10724580Szliu #endif
108