134123Sbostic /* 224580Szliu * Copyright (c) 1985 Regents of the University of California. 334123Sbostic * All rights reserved. 434123Sbostic * 5*42657Sbostic * %sccs.include.redist.c% 634123Sbostic * 734123Sbostic * All recipients should regard themselves as participants in an ongoing 834123Sbostic * research project and hence should feel obligated to report their 934123Sbostic * experiences (good or bad) with these elementary function codes, using 1034123Sbostic * the sendbug(8) program, to the authors. 1124580Szliu */ 1224580Szliu 1324580Szliu #ifndef lint 14*42657Sbostic static char sccsid[] = "@(#)cbrt.c 5.7 (Berkeley) 06/01/90"; 1534123Sbostic #endif /* not lint */ 1624580Szliu 1742656Sbostic #include <sys/stdc.h> 1842656Sbostic 1924580Szliu /* kahan's cube root (53 bits IEEE double precision) 2024580Szliu * for IEEE machines only 2124580Szliu * coded in C by K.C. Ng, 4/30/85 2224580Szliu * 2324580Szliu * Accuracy: 2424580Szliu * better than 0.667 ulps according to an error analysis. Maximum 2524580Szliu * error observed was 0.666 ulps in an 1,000,000 random arguments test. 2624580Szliu * 2724580Szliu * Warning: this code is semi machine dependent; the ordering of words in 2824580Szliu * a floating point number must be known in advance. I assume that the 2924580Szliu * long interger at the address of a floating point number will be the 3024580Szliu * leading 32 bits of that floating point number (i.e., sign, exponent, 3124580Szliu * and the 20 most significant bits). 3224580Szliu * On a National machine, it has different ordering; therefore, this code 3324580Szliu * must be compiled with flag -DNATIONAL. 3424580Szliu */ 3531856Szliu #if !defined(vax)&&!defined(tahoe) 3624580Szliu 3735681Sbostic static const unsigned long 3835681Sbostic B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 3924580Szliu B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 4035681Sbostic static const double 4124580Szliu C= 19./35., 4224580Szliu D= -864./1225., 4324580Szliu E= 99./70., 4424580Szliu F= 45./28., 4524580Szliu G= 5./14.; 4624580Szliu 4724580Szliu double cbrt(x) 4824580Szliu double x; 4924580Szliu { 5024580Szliu double r,s,t=0.0,w; 5124580Szliu unsigned long *px = (unsigned long *) &x, 5224580Szliu *pt = (unsigned long *) &t, 5324580Szliu mexp,sign; 5424580Szliu 5531856Szliu #ifdef national /* ordering of words in a floating points number */ 5635681Sbostic const int n0=1,n1=0; 5731856Szliu #else /* national */ 5835681Sbostic const int n0=0,n1=1; 5931856Szliu #endif /* national */ 6024580Szliu 6124580Szliu mexp=px[n0]&0x7ff00000; 6224580Szliu if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 6324580Szliu if(x==0.0) return(x); /* cbrt(0) is itself */ 6424580Szliu 6524580Szliu sign=px[n0]&0x80000000; /* sign= sign(x) */ 6624580Szliu px[n0] ^= sign; /* x=|x| */ 6724580Szliu 6824580Szliu 6924580Szliu /* rough cbrt to 5 bits */ 7024580Szliu if(mexp==0) /* subnormal number */ 7124580Szliu {pt[n0]=0x43500000; /* set t= 2**54 */ 7224580Szliu t*=x; pt[n0]=pt[n0]/3+B2; 7324580Szliu } 7424580Szliu else 7524580Szliu pt[n0]=px[n0]/3+B1; 7624580Szliu 7724580Szliu 7824580Szliu /* new cbrt to 23 bits, may be implemented in single precision */ 7924580Szliu r=t*t/x; 8024580Szliu s=C+r*t; 8124580Szliu t*=G+F/(s+E+D/s); 8224580Szliu 8324580Szliu /* chopped to 20 bits and make it larger than cbrt(x) */ 8424580Szliu pt[n1]=0; pt[n0]+=0x00000001; 8524580Szliu 8624580Szliu 8724580Szliu /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 8824580Szliu s=t*t; /* t*t is exact */ 8924580Szliu r=x/s; 9024580Szliu w=t+t; 9133938Sbostic r=(r-t)/(w+r); /* r-t is exact */ 9224580Szliu t=t+t*r; 9324580Szliu 9424580Szliu 9524580Szliu /* retore the sign bit */ 9624580Szliu pt[n0] |= sign; 9724580Szliu return(t); 9824580Szliu } 9924580Szliu #endif 100