1a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* @(#)s_cbrt.c 5.1 93/09/24 */ 2a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* 3a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ==================================================== 4a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 6a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Developed at SunPro, a Sun Microsystems, Inc. business. 7a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Permission to use, copy, modify, and distribute this 8a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * software is freely granted, provided that this notice 9a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * is preserved. 10a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ==================================================== 11a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 12a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Optimized by Bruce D. Evans. 13a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 14a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 15a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h> 16a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$"); 17a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 18a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math.h" 19a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math_private.h" 20a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 21a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* cbrt(x) 22a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Return cube root of x 23a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 24a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesstatic const u_int32_t 25a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */ 26a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */ 27a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 28a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */ 29a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesstatic const double 30a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott HughesP0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */ 31a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott HughesP1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */ 32a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott HughesP2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */ 33a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott HughesP3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */ 34a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott HughesP4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */ 35a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 36a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesdouble 37a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughescbrt(double x) 38a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes{ 39a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes int32_t hx; 40a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes union { 41a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes double value; 42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes uint64_t bits; 43a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } u; 44a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes double r,s,t=0.0,w; 45a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes u_int32_t sign; 46a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes u_int32_t high,low; 47a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 48a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes EXTRACT_WORDS(hx,low,x); 49a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes sign=hx&0x80000000; /* sign= sign(x) */ 50a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes hx ^=sign; 51a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 52a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 53a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 54a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Rough cbrt to 5 bits: 55a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3) 56a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * where e is integral and >= 0, m is real and in [0, 1), and "/" and 57a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * "%" are integer division and modulus with rounding towards minus 58a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * infinity. The RHS is always >= the LHS and has a maximum relative 59a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * error of about 1 in 16. Adding a bias of -0.03306235651 to the 60a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE 61a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * floating point representation, for finite positive normal values, 62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ordinary integer divison of the value in bits magically gives 63a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * almost exactly the RHS of the above provided we first subtract the 64a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * exponent bias (1023 for doubles) and later add it back. We do the 65a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * subtraction virtually to keep e >= 0 so that ordinary integer 66a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * division rounds towards minus infinity; this is also efficient. 67a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 68a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if(hx<0x00100000) { /* zero or subnormal? */ 69a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if((hx|low)==0) 70a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes return(x); /* cbrt(0) is itself */ 71a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 72a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t*=x; 73a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes GET_HIGH_WORD(high,t); 74a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes INSERT_WORDS(t,sign|((high&0x7fffffff)/3+B2),0); 75a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } else 76a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes INSERT_WORDS(t,sign|(hx/3+B1),0); 77a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 78a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 79a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * New cbrt to 23 bits: 80a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x) 81a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r) 82a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation 83a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this 84a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * gives us bounds for r = t**3/x. 85a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 86a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Try to optimize for parallel evaluation as in k_tanf.c. 87a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 88a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes r=(t*t)*(t/x); 89a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t=t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4)); 90a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 91a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 92a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Round t away from zero to 23 bits (sloppily except for ensuring that 93a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * the result is larger in magnitude than cbrt(x) but not much more than 94a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 2 23-bit ulps larger). With rounding towards zero, the error bound 95a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps 96a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * in the rounded t, the infinite-precision error in the Newton 97a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * approximation barely affects third digit in the final error 98a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 0.667; the error in the rounded t can be up to about 3 23-bit ulps 99a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * before the final error is larger than 0.667 ulps. 100a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 101a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes u.value=t; 102a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes u.bits=(u.bits+0x80000000)&0xffffffffc0000000ULL; 103a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t=u.value; 104a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 105a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* one step Newton iteration to 53 bits with error < 0.667 ulps */ 106a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes s=t*t; /* t*t is exact */ 107a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes r=x/s; /* error <= 0.5 ulps; |r| < |t| */ 108a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes w=t+t; /* t+t is exact */ 109a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ 110a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ 111a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 112a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes return(t); 113a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes} 114a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 115a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#if (LDBL_MANT_DIG == 53) 116a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__weak_reference(cbrt, cbrtl); 117a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#endif 118