xref: /csrg-svn/lib/libm/ieee/cbrt.c (revision 34123)
1*34123Sbostic /*
224580Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34123Sbostic  * All rights reserved.
4*34123Sbostic  *
5*34123Sbostic  * Redistribution and use in source and binary forms are permitted
6*34123Sbostic  * provided that this notice is preserved and that due credit is given
7*34123Sbostic  * to the University of California at Berkeley. The name of the University
8*34123Sbostic  * may not be used to endorse or promote products derived from this
9*34123Sbostic  * software without specific prior written permission. This software
10*34123Sbostic  * is provided ``as is'' without express or implied warranty.
11*34123Sbostic  *
12*34123Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34123Sbostic  * research project and hence should feel obligated to report their
14*34123Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34123Sbostic  * the sendbug(8) program, to the authors.
1624580Szliu  */
1724580Szliu 
1824580Szliu #ifndef lint
19*34123Sbostic static char sccsid[] = "@(#)cbrt.c	5.3 (Berkeley) 04/29/88";
20*34123Sbostic #endif /* not lint */
2124580Szliu 
2224580Szliu /* kahan's cube root (53 bits IEEE double precision)
2324580Szliu  * for IEEE machines only
2424580Szliu  * coded in C by K.C. Ng, 4/30/85
2524580Szliu  *
2624580Szliu  * Accuracy:
2724580Szliu  *	better than 0.667 ulps according to an error analysis. Maximum
2824580Szliu  * error observed was 0.666 ulps in an 1,000,000 random arguments test.
2924580Szliu  *
3024580Szliu  * Warning: this code is semi machine dependent; the ordering of words in
3124580Szliu  * a floating point number must be known in advance. I assume that the
3224580Szliu  * long interger at the address of a floating point number will be the
3324580Szliu  * leading 32 bits of that floating point number (i.e., sign, exponent,
3424580Szliu  * and the 20 most significant bits).
3524580Szliu  * On a National machine, it has different ordering; therefore, this code
3624580Szliu  * must be compiled with flag -DNATIONAL.
3724580Szliu  */
3831856Szliu #if !defined(vax)&&!defined(tahoe)
3924580Szliu 
4024580Szliu static unsigned long B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
4124580Szliu 	             B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
4224580Szliu static double
4324580Szliu 	    C= 19./35.,
4424580Szliu 	    D= -864./1225.,
4524580Szliu 	    E= 99./70.,
4624580Szliu 	    F= 45./28.,
4724580Szliu 	    G= 5./14.;
4824580Szliu 
4924580Szliu double cbrt(x)
5024580Szliu double x;
5124580Szliu {
5224580Szliu 	double r,s,t=0.0,w;
5324580Szliu 	unsigned long *px = (unsigned long *) &x,
5424580Szliu 	              *pt = (unsigned long *) &t,
5524580Szliu 		      mexp,sign;
5624580Szliu 
5731856Szliu #ifdef national /* ordering of words in a floating points number */
5824580Szliu 	int n0=1,n1=0;
5931856Szliu #else	/* national */
6024580Szliu 	int n0=0,n1=1;
6131856Szliu #endif	/* national */
6224580Szliu 
6324580Szliu 	mexp=px[n0]&0x7ff00000;
6424580Szliu 	if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */
6524580Szliu 	if(x==0.0) return(x);		/* cbrt(0) is itself */
6624580Szliu 
6724580Szliu 	sign=px[n0]&0x80000000; /* sign= sign(x) */
6824580Szliu 	px[n0] ^= sign;		/* x=|x| */
6924580Szliu 
7024580Szliu 
7124580Szliu     /* rough cbrt to 5 bits */
7224580Szliu 	if(mexp==0) 		/* subnormal number */
7324580Szliu 	  {pt[n0]=0x43500000; 	/* set t= 2**54 */
7424580Szliu 	   t*=x; pt[n0]=pt[n0]/3+B2;
7524580Szliu 	  }
7624580Szliu 	else
7724580Szliu 	  pt[n0]=px[n0]/3+B1;
7824580Szliu 
7924580Szliu 
8024580Szliu     /* new cbrt to 23 bits, may be implemented in single precision */
8124580Szliu 	r=t*t/x;
8224580Szliu 	s=C+r*t;
8324580Szliu 	t*=G+F/(s+E+D/s);
8424580Szliu 
8524580Szliu     /* chopped to 20 bits and make it larger than cbrt(x) */
8624580Szliu 	pt[n1]=0; pt[n0]+=0x00000001;
8724580Szliu 
8824580Szliu 
8924580Szliu     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
9024580Szliu 	s=t*t;		/* t*t is exact */
9124580Szliu 	r=x/s;
9224580Szliu 	w=t+t;
9333938Sbostic 	r=(r-t)/(w+r);	/* r-t is exact */
9424580Szliu 	t=t+t*r;
9524580Szliu 
9624580Szliu 
9724580Szliu     /* retore the sign bit */
9824580Szliu 	pt[n0] |= sign;
9924580Szliu 	return(t);
10024580Szliu }
10124580Szliu #endif
102