s_cbrt.c revision 1.3
11.1Sjtc/* @(#)s_cbrt.c 5.1 93/09/24 */ 21.1Sjtc/* 31.1Sjtc * ==================================================== 41.1Sjtc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 51.1Sjtc * 61.1Sjtc * Developed at SunPro, a Sun Microsystems, Inc. business. 71.1Sjtc * Permission to use, copy, modify, and distribute this 81.1Sjtc * software is freely granted, provided that this notice 91.1Sjtc * is preserved. 101.1Sjtc * ==================================================== 111.1Sjtc */ 121.3Sjtc 131.3Sjtc#ifndef lint 141.3Sjtcstatic char rcsid[] = "$Id: s_cbrt.c,v 1.3 1994/02/18 02:26:15 jtc Exp $"; 151.3Sjtc#endif 161.1Sjtc 171.2Sjtc#include <math.h> 181.1Sjtc 191.1Sjtc/* cbrt(x) 201.1Sjtc * Return cube root of x 211.1Sjtc */ 221.1Sjtc#ifdef __STDC__ 231.1Sjtcstatic const unsigned 241.1Sjtc#else 251.1Sjtcstatic unsigned 261.1Sjtc#endif 271.1Sjtc B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 281.1Sjtc B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 291.1Sjtc 301.1Sjtc#ifdef __STDC__ 311.1Sjtcstatic const double 321.1Sjtc#else 331.1Sjtcstatic double 341.1Sjtc#endif 351.1SjtcC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 361.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 371.1SjtcE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 381.1SjtcF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 391.1SjtcG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 401.1Sjtc 411.1Sjtc#ifdef __STDC__ 421.1Sjtc double cbrt(double x) 431.1Sjtc#else 441.1Sjtc double cbrt(x) 451.1Sjtc double x; 461.1Sjtc#endif 471.1Sjtc{ 481.1Sjtc double one = 1.0; 491.1Sjtc int n0,hx; 501.1Sjtc double r,s,t=0.0,w; 511.1Sjtc unsigned *pt = (unsigned *) &t, sign; 521.1Sjtc 531.1Sjtc 541.1Sjtc n0 = ((*(int*)&one)>>29)^1; /* index of high word */ 551.1Sjtc hx = *( n0 + (int*)&x); /* high word of x */ 561.1Sjtc sign=hx&0x80000000; /* sign= sign(x) */ 571.1Sjtc hx ^=sign; 581.1Sjtc if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 591.1Sjtc if((hx|*(1-n0+(int*)&x))==0) 601.1Sjtc return(x); /* cbrt(0) is itself */ 611.1Sjtc 621.1Sjtc *(n0+(int*)&x) = hx; /* x <- |x| */ 631.1Sjtc /* rough cbrt to 5 bits */ 641.1Sjtc if(hx<0x00100000) /* subnormal number */ 651.1Sjtc {pt[n0]=0x43500000; /* set t= 2**54 */ 661.1Sjtc t*=x; pt[n0]=pt[n0]/3+B2; 671.1Sjtc } 681.1Sjtc else 691.1Sjtc pt[n0]=hx/3+B1; 701.1Sjtc 711.1Sjtc 721.1Sjtc /* new cbrt to 23 bits, may be implemented in single precision */ 731.1Sjtc r=t*t/x; 741.1Sjtc s=C+r*t; 751.1Sjtc t*=G+F/(s+E+D/s); 761.1Sjtc 771.1Sjtc /* chopped to 20 bits and make it larger than cbrt(x) */ 781.1Sjtc pt[1-n0]=0; pt[n0]+=0x00000001; 791.1Sjtc 801.1Sjtc 811.1Sjtc /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 821.1Sjtc s=t*t; /* t*t is exact */ 831.1Sjtc r=x/s; 841.1Sjtc w=t+t; 851.1Sjtc r=(r-t)/(w+r); /* r-s is exact */ 861.1Sjtc t=t+t*r; 871.1Sjtc 881.1Sjtc /* retore the sign bit */ 891.1Sjtc pt[n0] |= sign; 901.1Sjtc return(t); 911.1Sjtc} 92