s_cbrt.c revision 1.4
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.4Sjtcstatic char rcsid[] = "$Id: s_cbrt.c,v 1.4 1994/03/03 17:04:28 jtc Exp $"; 151.3Sjtc#endif 161.1Sjtc 171.2Sjtc#include <math.h> 181.4Sjtc#include <machine/endian.h> 191.4Sjtc 201.4Sjtc#if BYTE_ORDER == LITTLE_ENDIAN 211.4Sjtc#define n0 1 221.4Sjtc#else 231.4Sjtc#define n0 0 241.4Sjtc#endif 251.1Sjtc 261.1Sjtc/* cbrt(x) 271.1Sjtc * Return cube root of x 281.1Sjtc */ 291.1Sjtc#ifdef __STDC__ 301.1Sjtcstatic const unsigned 311.1Sjtc#else 321.1Sjtcstatic unsigned 331.1Sjtc#endif 341.1Sjtc B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 351.1Sjtc B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 361.1Sjtc 371.1Sjtc#ifdef __STDC__ 381.1Sjtcstatic const double 391.1Sjtc#else 401.1Sjtcstatic double 411.1Sjtc#endif 421.1SjtcC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 431.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 441.1SjtcE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 451.1SjtcF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 461.1SjtcG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 471.1Sjtc 481.1Sjtc#ifdef __STDC__ 491.1Sjtc double cbrt(double x) 501.1Sjtc#else 511.1Sjtc double cbrt(x) 521.1Sjtc double x; 531.1Sjtc#endif 541.1Sjtc{ 551.4Sjtc int hx; 561.1Sjtc double r,s,t=0.0,w; 571.1Sjtc unsigned *pt = (unsigned *) &t, sign; 581.1Sjtc 591.1Sjtc hx = *( n0 + (int*)&x); /* high word of x */ 601.1Sjtc sign=hx&0x80000000; /* sign= sign(x) */ 611.1Sjtc hx ^=sign; 621.1Sjtc if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 631.1Sjtc if((hx|*(1-n0+(int*)&x))==0) 641.1Sjtc return(x); /* cbrt(0) is itself */ 651.1Sjtc 661.1Sjtc *(n0+(int*)&x) = hx; /* x <- |x| */ 671.1Sjtc /* rough cbrt to 5 bits */ 681.1Sjtc if(hx<0x00100000) /* subnormal number */ 691.1Sjtc {pt[n0]=0x43500000; /* set t= 2**54 */ 701.1Sjtc t*=x; pt[n0]=pt[n0]/3+B2; 711.1Sjtc } 721.1Sjtc else 731.1Sjtc pt[n0]=hx/3+B1; 741.1Sjtc 751.1Sjtc 761.1Sjtc /* new cbrt to 23 bits, may be implemented in single precision */ 771.1Sjtc r=t*t/x; 781.1Sjtc s=C+r*t; 791.1Sjtc t*=G+F/(s+E+D/s); 801.1Sjtc 811.1Sjtc /* chopped to 20 bits and make it larger than cbrt(x) */ 821.1Sjtc pt[1-n0]=0; pt[n0]+=0x00000001; 831.1Sjtc 841.1Sjtc 851.1Sjtc /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 861.1Sjtc s=t*t; /* t*t is exact */ 871.1Sjtc r=x/s; 881.1Sjtc w=t+t; 891.1Sjtc r=(r-t)/(w+r); /* r-s is exact */ 901.1Sjtc t=t+t*r; 911.1Sjtc 921.1Sjtc /* retore the sign bit */ 931.1Sjtc pt[n0] |= sign; 941.1Sjtc return(t); 951.1Sjtc} 96