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*42656Sbostic static char sccsid[] = "@(#)cbrt.c 5.6 (Berkeley) 06/01/90"; 2534123Sbostic #endif /* not lint */ 2624580Szliu 27*42656Sbostic #include <sys/stdc.h> 28*42656Sbostic 2924580Szliu /* kahan's cube root (53 bits IEEE double precision) 3024580Szliu * for IEEE machines only 3124580Szliu * coded in C by K.C. Ng, 4/30/85 3224580Szliu * 3324580Szliu * Accuracy: 3424580Szliu * better than 0.667 ulps according to an error analysis. Maximum 3524580Szliu * error observed was 0.666 ulps in an 1,000,000 random arguments test. 3624580Szliu * 3724580Szliu * Warning: this code is semi machine dependent; the ordering of words in 3824580Szliu * a floating point number must be known in advance. I assume that the 3924580Szliu * long interger at the address of a floating point number will be the 4024580Szliu * leading 32 bits of that floating point number (i.e., sign, exponent, 4124580Szliu * and the 20 most significant bits). 4224580Szliu * On a National machine, it has different ordering; therefore, this code 4324580Szliu * must be compiled with flag -DNATIONAL. 4424580Szliu */ 4531856Szliu #if !defined(vax)&&!defined(tahoe) 4624580Szliu 4735681Sbostic static const unsigned long 4835681Sbostic B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 4924580Szliu B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 5035681Sbostic static const double 5124580Szliu C= 19./35., 5224580Szliu D= -864./1225., 5324580Szliu E= 99./70., 5424580Szliu F= 45./28., 5524580Szliu G= 5./14.; 5624580Szliu 5724580Szliu double cbrt(x) 5824580Szliu double x; 5924580Szliu { 6024580Szliu double r,s,t=0.0,w; 6124580Szliu unsigned long *px = (unsigned long *) &x, 6224580Szliu *pt = (unsigned long *) &t, 6324580Szliu mexp,sign; 6424580Szliu 6531856Szliu #ifdef national /* ordering of words in a floating points number */ 6635681Sbostic const int n0=1,n1=0; 6731856Szliu #else /* national */ 6835681Sbostic const int n0=0,n1=1; 6931856Szliu #endif /* national */ 7024580Szliu 7124580Szliu mexp=px[n0]&0x7ff00000; 7224580Szliu if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 7324580Szliu if(x==0.0) return(x); /* cbrt(0) is itself */ 7424580Szliu 7524580Szliu sign=px[n0]&0x80000000; /* sign= sign(x) */ 7624580Szliu px[n0] ^= sign; /* x=|x| */ 7724580Szliu 7824580Szliu 7924580Szliu /* rough cbrt to 5 bits */ 8024580Szliu if(mexp==0) /* subnormal number */ 8124580Szliu {pt[n0]=0x43500000; /* set t= 2**54 */ 8224580Szliu t*=x; pt[n0]=pt[n0]/3+B2; 8324580Szliu } 8424580Szliu else 8524580Szliu pt[n0]=px[n0]/3+B1; 8624580Szliu 8724580Szliu 8824580Szliu /* new cbrt to 23 bits, may be implemented in single precision */ 8924580Szliu r=t*t/x; 9024580Szliu s=C+r*t; 9124580Szliu t*=G+F/(s+E+D/s); 9224580Szliu 9324580Szliu /* chopped to 20 bits and make it larger than cbrt(x) */ 9424580Szliu pt[n1]=0; pt[n0]+=0x00000001; 9524580Szliu 9624580Szliu 9724580Szliu /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 9824580Szliu s=t*t; /* t*t is exact */ 9924580Szliu r=x/s; 10024580Szliu w=t+t; 10133938Sbostic r=(r-t)/(w+r); /* r-t is exact */ 10224580Szliu t=t+t*r; 10324580Szliu 10424580Szliu 10524580Szliu /* retore the sign bit */ 10624580Szliu pt[n0] |= sign; 10724580Szliu return(t); 10824580Szliu } 10924580Szliu #endif 110