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 * ==================================================== 11153386Sbde * 12153386Sbde * Optimized by Bruce D. Evans. 132116Sjkh */ 142116Sjkh 15176451Sdas#include <sys/cdefs.h> 16176451Sdas__FBSDID("$FreeBSD$"); 172116Sjkh 182116Sjkh#include "math.h" 192116Sjkh#include "math_private.h" 202116Sjkh 212116Sjkh/* cbrt(x) 222116Sjkh * Return cube root of x 232116Sjkh */ 242116Sjkhstatic const u_int32_t 25153306Sbde B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */ 26153306Sbde B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */ 272116Sjkh 28153520Sbde/* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */ 292116Sjkhstatic const double 30153520SbdeP0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */ 31153520SbdeP1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */ 32153520SbdeP2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */ 33153520SbdeP3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */ 34153520SbdeP4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */ 352116Sjkh 3697413Salfreddouble 3797413Salfredcbrt(double x) 382116Sjkh{ 392116Sjkh int32_t hx; 40153517Sbde union { 41153517Sbde double value; 42153517Sbde uint64_t bits; 43153517Sbde } u; 442116Sjkh double r,s,t=0.0,w; 452116Sjkh u_int32_t sign; 462116Sjkh u_int32_t high,low; 472116Sjkh 48153548Sbde EXTRACT_WORDS(hx,low,x); 492116Sjkh sign=hx&0x80000000; /* sign= sign(x) */ 502116Sjkh hx ^=sign; 512116Sjkh if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 522116Sjkh 53153306Sbde /* 54153306Sbde * Rough cbrt to 5 bits: 55153306Sbde * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3) 56153306Sbde * where e is integral and >= 0, m is real and in [0, 1), and "/" and 57153306Sbde * "%" are integer division and modulus with rounding towards minus 58153306Sbde * infinity. The RHS is always >= the LHS and has a maximum relative 59153306Sbde * error of about 1 in 16. Adding a bias of -0.03306235651 to the 60153306Sbde * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE 61153306Sbde * floating point representation, for finite positive normal values, 62153306Sbde * ordinary integer divison of the value in bits magically gives 63153306Sbde * almost exactly the RHS of the above provided we first subtract the 64153306Sbde * exponent bias (1023 for doubles) and later add it back. We do the 65153306Sbde * subtraction virtually to keep e >= 0 so that ordinary integer 66153306Sbde * division rounds towards minus infinity; this is also efficient. 67153306Sbde */ 68153548Sbde if(hx<0x00100000) { /* zero or subnormal? */ 69153548Sbde if((hx|low)==0) 70153548Sbde return(x); /* cbrt(0) is itself */ 71153382Sbde SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 72153382Sbde t*=x; 73153382Sbde GET_HIGH_WORD(high,t); 74153548Sbde INSERT_WORDS(t,sign|((high&0x7fffffff)/3+B2),0); 75153382Sbde } else 76153548Sbde INSERT_WORDS(t,sign|(hx/3+B1),0); 772116Sjkh 78153447Sbde /* 79153520Sbde * New cbrt to 23 bits: 80153520Sbde * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x) 81153520Sbde * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r) 82153520Sbde * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation 83153520Sbde * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this 84153520Sbde * gives us bounds for r = t**3/x. 85153520Sbde * 86153520Sbde * Try to optimize for parallel evaluation as in k_tanf.c. 87153447Sbde */ 88153520Sbde r=(t*t)*(t/x); 89153520Sbde t=t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4)); 902116Sjkh 91153517Sbde /* 92153520Sbde * Round t away from zero to 23 bits (sloppily except for ensuring that 93153517Sbde * the result is larger in magnitude than cbrt(x) but not much more than 94153520Sbde * 2 23-bit ulps larger). With rounding towards zero, the error bound 95153520Sbde * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps 96153517Sbde * in the rounded t, the infinite-precision error in the Newton 97218909Sbrucec * approximation barely affects third digit in the final error 98153520Sbde * 0.667; the error in the rounded t can be up to about 3 23-bit ulps 99153517Sbde * before the final error is larger than 0.667 ulps. 100153517Sbde */ 101153517Sbde u.value=t; 102153520Sbde u.bits=(u.bits+0x80000000)&0xffffffffc0000000ULL; 103153517Sbde t=u.value; 1042116Sjkh 105153517Sbde /* one step Newton iteration to 53 bits with error < 0.667 ulps */ 106153517Sbde s=t*t; /* t*t is exact */ 107153517Sbde r=x/s; /* error <= 0.5 ulps; |r| < |t| */ 108153517Sbde w=t+t; /* t+t is exact */ 109153517Sbde r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ 110153517Sbde t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ 1112116Sjkh 1122116Sjkh return(t); 1132116Sjkh} 114219571Skargl 115219571Skargl#if (LDBL_MANT_DIG == 53) 116219571Skargl__weak_reference(cbrt, cbrtl); 117219571Skargl#endif 118