s_cbrt.c revision 153382
12116Sjkh/* @(#)s_cbrt.c 5.1 93/09/24 */ 22116Sjkh/* 32116Sjkh * ==================================================== 42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 52116Sjkh * 62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 72116Sjkh * Permission to use, copy, modify, and distribute this 88870Srgrimes * software is freely granted, provided that this notice 92116Sjkh * is preserved. 102116Sjkh * ==================================================== 112116Sjkh */ 122116Sjkh 132116Sjkh#ifndef lint 1450476Speterstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_cbrt.c 153382 2005-12-13 18:22:00Z bde $"; 152116Sjkh#endif 162116Sjkh 172116Sjkh#include "math.h" 182116Sjkh#include "math_private.h" 192116Sjkh 202116Sjkh/* cbrt(x) 212116Sjkh * Return cube root of x 222116Sjkh */ 232116Sjkhstatic const u_int32_t 24153306Sbde B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */ 25153306Sbde B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */ 262116Sjkh 272116Sjkhstatic const double 282116SjkhC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 292116SjkhD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 302116SjkhE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 312116SjkhF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 322116SjkhG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 332116Sjkh 3497413Salfreddouble 3597413Salfredcbrt(double x) 362116Sjkh{ 372116Sjkh int32_t hx; 382116Sjkh double r,s,t=0.0,w; 392116Sjkh u_int32_t sign; 402116Sjkh u_int32_t high,low; 412116Sjkh 422116Sjkh GET_HIGH_WORD(hx,x); 432116Sjkh sign=hx&0x80000000; /* sign= sign(x) */ 442116Sjkh hx ^=sign; 452116Sjkh if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 462116Sjkh GET_LOW_WORD(low,x); 478870Srgrimes if((hx|low)==0) 482116Sjkh return(x); /* cbrt(0) is itself */ 492116Sjkh 502116Sjkh SET_HIGH_WORD(x,hx); /* x <- |x| */ 51153306Sbde /* 52153306Sbde * Rough cbrt to 5 bits: 53153306Sbde * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3) 54153306Sbde * where e is integral and >= 0, m is real and in [0, 1), and "/" and 55153306Sbde * "%" are integer division and modulus with rounding towards minus 56153306Sbde * infinity. The RHS is always >= the LHS and has a maximum relative 57153306Sbde * error of about 1 in 16. Adding a bias of -0.03306235651 to the 58153306Sbde * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE 59153306Sbde * floating point representation, for finite positive normal values, 60153306Sbde * ordinary integer divison of the value in bits magically gives 61153306Sbde * almost exactly the RHS of the above provided we first subtract the 62153306Sbde * exponent bias (1023 for doubles) and later add it back. We do the 63153306Sbde * subtraction virtually to keep e >= 0 so that ordinary integer 64153306Sbde * division rounds towards minus infinity; this is also efficient. 65153306Sbde */ 66153382Sbde if(hx<0x00100000) { /* subnormal number */ 67153382Sbde SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 68153382Sbde t*=x; 69153382Sbde GET_HIGH_WORD(high,t); 70153382Sbde SET_HIGH_WORD(t,high/3+B2); 71153382Sbde } else 72153382Sbde SET_HIGH_WORD(t,hx/3+B1); 732116Sjkh 74153306Sbde /* new cbrt to 23 bits; may be implemented in single precision */ 752116Sjkh r=t*t/x; 762116Sjkh s=C+r*t; 778870Srgrimes t*=G+F/(s+E+D/s); 782116Sjkh 79153306Sbde /* chop t to 20 bits and make it larger than cbrt(x) */ 802116Sjkh GET_HIGH_WORD(high,t); 812116Sjkh INSERT_WORDS(t,high+0x00000001,0); 822116Sjkh 83153306Sbde /* one step Newton iteration to 53 bits with error less than 0.667 ulps */ 842116Sjkh s=t*t; /* t*t is exact */ 852116Sjkh r=x/s; 862116Sjkh w=t+t; 87153306Sbde r=(r-t)/(w+r); /* r-t is exact */ 882116Sjkh t=t+t*r; 892116Sjkh 90153306Sbde /* restore the sign bit */ 912116Sjkh GET_HIGH_WORD(high,t); 922116Sjkh SET_HIGH_WORD(t,high|sign); 932116Sjkh return(t); 942116Sjkh} 95