s_cbrt.c revision 1.5
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.5Sjtcstatic char rcsid[] = "$Id: s_cbrt.c,v 1.5 1994/08/10 20:31:59 jtc Exp $"; 151.3Sjtc#endif 161.1Sjtc 171.5Sjtc#include "math.h" 181.5Sjtc#include "math_private.h" 191.1Sjtc 201.1Sjtc/* cbrt(x) 211.1Sjtc * Return cube root of x 221.1Sjtc */ 231.1Sjtc#ifdef __STDC__ 241.1Sjtcstatic const unsigned 251.1Sjtc#else 261.1Sjtcstatic unsigned 271.1Sjtc#endif 281.1Sjtc B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ 291.1Sjtc B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 301.1Sjtc 311.1Sjtc#ifdef __STDC__ 321.1Sjtcstatic const double 331.1Sjtc#else 341.1Sjtcstatic double 351.1Sjtc#endif 361.1SjtcC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 371.1SjtcD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 381.1SjtcE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 391.1SjtcF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 401.1SjtcG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 411.1Sjtc 421.1Sjtc#ifdef __STDC__ 431.1Sjtc double cbrt(double x) 441.1Sjtc#else 451.1Sjtc double cbrt(x) 461.1Sjtc double x; 471.1Sjtc#endif 481.1Sjtc{ 491.4Sjtc int hx; 501.1Sjtc double r,s,t=0.0,w; 511.5Sjtc unsigned sign; 521.5Sjtc unsigned int high,low; 531.1Sjtc 541.5Sjtc GET_HIGH_WORD(hx,x); 551.1Sjtc sign=hx&0x80000000; /* sign= sign(x) */ 561.1Sjtc hx ^=sign; 571.1Sjtc if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 581.5Sjtc GET_LOW_WORD(low,x); 591.5Sjtc if((hx|low)==0) 601.1Sjtc return(x); /* cbrt(0) is itself */ 611.1Sjtc 621.5Sjtc SET_HIGH_WORD(x,hx); /* x <- |x| */ 631.1Sjtc /* rough cbrt to 5 bits */ 641.1Sjtc if(hx<0x00100000) /* subnormal number */ 651.5Sjtc {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 661.5Sjtc t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); 671.1Sjtc } 681.1Sjtc else 691.5Sjtc SET_HIGH_WORD(t,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.5Sjtc GET_HIGH_WORD(high,t); 791.5Sjtc INSERT_WORDS(t,high+0x00000001,0); 801.1Sjtc 811.1Sjtc 821.1Sjtc /* one step newton iteration to 53 bits with error less than 0.667 ulps */ 831.1Sjtc s=t*t; /* t*t is exact */ 841.1Sjtc r=x/s; 851.1Sjtc w=t+t; 861.1Sjtc r=(r-t)/(w+r); /* r-s is exact */ 871.1Sjtc t=t+t*r; 881.1Sjtc 891.1Sjtc /* retore the sign bit */ 901.5Sjtc GET_HIGH_WORD(high,t); 911.5Sjtc SET_HIGH_WORD(t,high|sign); 921.1Sjtc return(t); 931.1Sjtc} 94