124580Szliu /* 224580Szliu * Copyright (c) 1985 Regents of the University of California. 324580Szliu * 424580Szliu * Use and reproduction of this software are granted in accordance with 524580Szliu * the terms and conditions specified in the Berkeley Software License 624580Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724580Szliu * source, and inclusion of this notice) with the additional understanding 824580Szliu * that all recipients should regard themselves as participants in an 924580Szliu * ongoing research project and hence should feel obligated to report 1024580Szliu * their experiences (good or bad) with these elementary function codes, 1124580Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224580Szliu */ 1324580Szliu 1424580Szliu #ifndef lint 1524719Selefunt static char sccsid[] = 16*33938Sbostic "@(#)cbrt.c 1.1 (Berkeley) 5/23/85; 5.2 (ucb.elefunt) 04/04/88"; 1731856Szliu #endif /* not lint */ 1824580Szliu 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 3724580Szliu static unsigned long B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 3824580Szliu B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 3924580Szliu static double 4024580Szliu C= 19./35., 4124580Szliu D= -864./1225., 4224580Szliu E= 99./70., 4324580Szliu F= 45./28., 4424580Szliu G= 5./14.; 4524580Szliu 4624580Szliu double cbrt(x) 4724580Szliu double x; 4824580Szliu { 4924580Szliu double r,s,t=0.0,w; 5024580Szliu unsigned long *px = (unsigned long *) &x, 5124580Szliu *pt = (unsigned long *) &t, 5224580Szliu mexp,sign; 5324580Szliu 5431856Szliu #ifdef national /* ordering of words in a floating points number */ 5524580Szliu int n0=1,n1=0; 5631856Szliu #else /* national */ 5724580Szliu int n0=0,n1=1; 5831856Szliu #endif /* national */ 5924580Szliu 6024580Szliu mexp=px[n0]&0x7ff00000; 6124580Szliu if(mexp==0x7ff00000) return(x); /* cbrt(NaN,INF) is itself */ 6224580Szliu if(x==0.0) return(x); /* cbrt(0) is itself */ 6324580Szliu 6424580Szliu sign=px[n0]&0x80000000; /* sign= sign(x) */ 6524580Szliu px[n0] ^= sign; /* x=|x| */ 6624580Szliu 6724580Szliu 6824580Szliu /* rough cbrt to 5 bits */ 6924580Szliu if(mexp==0) /* subnormal number */ 7024580Szliu {pt[n0]=0x43500000; /* set t= 2**54 */ 7124580Szliu t*=x; pt[n0]=pt[n0]/3+B2; 7224580Szliu } 7324580Szliu else 7424580Szliu pt[n0]=px[n0]/3+B1; 7524580Szliu 7624580Szliu 7724580Szliu /* new cbrt to 23 bits, may be implemented in single precision */ 7824580Szliu r=t*t/x; 7924580Szliu s=C+r*t; 8024580Szliu t*=G+F/(s+E+D/s); 8124580Szliu 8224580Szliu /* chopped to 20 bits and make it larger than cbrt(x) */ 8324580Szliu pt[n1]=0; pt[n0]+=0x00000001; 8424580Szliu 8524580Szliu 8624580Szliu /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 8724580Szliu s=t*t; /* t*t is exact */ 8824580Szliu r=x/s; 8924580Szliu w=t+t; 90*33938Sbostic r=(r-t)/(w+r); /* r-t is exact */ 9124580Szliu t=t+t*r; 9224580Szliu 9324580Szliu 9424580Szliu /* retore the sign bit */ 9524580Szliu pt[n0] |= sign; 9624580Szliu return(t); 9724580Szliu } 9824580Szliu #endif 99