134123Sbostic /* 224580Szliu * Copyright (c) 1985 Regents of the University of California. 334123Sbostic * All rights reserved. 434123Sbostic * 542657Sbostic * %sccs.include.redist.c% 624580Szliu */ 724580Szliu 824580Szliu #ifndef lint 9*46401Sbostic static char sccsid[] = "@(#)cbrt.c 5.9 (Berkeley) 02/13/91"; 1034123Sbostic #endif /* not lint */ 1124580Szliu 12*46401Sbostic #include <sys/cdefs.h> 1342656Sbostic 1424580Szliu /* kahan's cube root (53 bits IEEE double precision) 1524580Szliu * for IEEE machines only 1624580Szliu * coded in C by K.C. Ng, 4/30/85 1724580Szliu * 1824580Szliu * Accuracy: 1924580Szliu * better than 0.667 ulps according to an error analysis. Maximum 2024580Szliu * error observed was 0.666 ulps in an 1,000,000 random arguments test. 2124580Szliu * 2224580Szliu * Warning: this code is semi machine dependent; the ordering of words in 2324580Szliu * a floating point number must be known in advance. I assume that the 2424580Szliu * long interger at the address of a floating point number will be the 2524580Szliu * leading 32 bits of that floating point number (i.e., sign, exponent, 2624580Szliu * and the 20 most significant bits). 2724580Szliu * On a National machine, it has different ordering; therefore, this code 2824580Szliu * must be compiled with flag -DNATIONAL. 2924580Szliu */ 3031856Szliu #if !defined(vax)&&!defined(tahoe) 3124580Szliu 3235681Sbostic static const unsigned long 3335681Sbostic B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 3424580Szliu B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 3535681Sbostic static const double 3624580Szliu C= 19./35., 3724580Szliu D= -864./1225., 3824580Szliu E= 99./70., 3924580Szliu F= 45./28., 4024580Szliu G= 5./14.; 4124580Szliu 4224580Szliu double cbrt(x) 4324580Szliu double x; 4424580Szliu { 4524580Szliu double r,s,t=0.0,w; 4624580Szliu unsigned long *px = (unsigned long *) &x, 4724580Szliu *pt = (unsigned long *) &t, 4824580Szliu mexp,sign; 4924580Szliu 5031856Szliu #ifdef national /* ordering of words in a floating points number */ 5135681Sbostic const int n0=1,n1=0; 5231856Szliu #else /* national */ 5335681Sbostic const int n0=0,n1=1; 5431856Szliu #endif /* national */ 5524580Szliu 5624580Szliu mexp=px[n0]&0x7ff00000; 5724580Szliu if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 5824580Szliu if(x==0.0) return(x); /* cbrt(0) is itself */ 5924580Szliu 6024580Szliu sign=px[n0]&0x80000000; /* sign= sign(x) */ 6124580Szliu px[n0] ^= sign; /* x=|x| */ 6224580Szliu 6324580Szliu 6424580Szliu /* rough cbrt to 5 bits */ 6524580Szliu if(mexp==0) /* subnormal number */ 6624580Szliu {pt[n0]=0x43500000; /* set t= 2**54 */ 6724580Szliu t*=x; pt[n0]=pt[n0]/3+B2; 6824580Szliu } 6924580Szliu else 7024580Szliu pt[n0]=px[n0]/3+B1; 7124580Szliu 7224580Szliu 7324580Szliu /* new cbrt to 23 bits, may be implemented in single precision */ 7424580Szliu r=t*t/x; 7524580Szliu s=C+r*t; 7624580Szliu t*=G+F/(s+E+D/s); 7724580Szliu 7824580Szliu /* chopped to 20 bits and make it larger than cbrt(x) */ 7924580Szliu pt[n1]=0; pt[n0]+=0x00000001; 8024580Szliu 8124580Szliu 8224580Szliu /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 8324580Szliu s=t*t; /* t*t is exact */ 8424580Szliu r=x/s; 8524580Szliu w=t+t; 8633938Sbostic r=(r-t)/(w+r); /* r-t is exact */ 8724580Szliu t=t+t*r; 8824580Szliu 8924580Szliu 9024580Szliu /* retore the sign bit */ 9124580Szliu pt[n0] |= sign; 9224580Szliu return(t); 9324580Szliu } 9424580Szliu #endif 95