/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrt.c */1/*2* ====================================================3* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.4*5* Developed at SunPro, a Sun Microsystems, Inc. business.6* Permission to use, copy, modify, and distribute this7* software is freely granted, provided that this notice8* is preserved.9* ====================================================10*11* Optimized by Bruce D. Evans.12*/13/* cbrt(x)14* Return cube root of x15*/1617#include <math.h>18#include <stdint.h>19#include "libm.h"2021static const uint32_t22B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */23B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */2425/* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */26static const double27P0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */28P1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */29P2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */30P3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */31P4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */3233double __cdecl cbrt(double x)34{35union {double f; uint64_t i;} u = {x};36double_t r,s,t,w;37uint32_t hx = u.i>>32 & 0x7fffffff;3839if (hx >= 0x7ff00000) /* cbrt(NaN,INF) is itself */40return x+x;4142/*43* Rough cbrt to 5 bits:44* cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)45* where e is integral and >= 0, m is real and in [0, 1), and "/" and46* "%" are integer division and modulus with rounding towards minus47* infinity. The RHS is always >= the LHS and has a maximum relative48* error of about 1 in 16. Adding a bias of -0.03306235651 to the49* (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE50* floating point representation, for finite positive normal values,51* ordinary integer divison of the value in bits magically gives52* almost exactly the RHS of the above provided we first subtract the53* exponent bias (1023 for doubles) and later add it back. We do the54* subtraction virtually to keep e >= 0 so that ordinary integer55* division rounds towards minus infinity; this is also efficient.56*/57if (hx < 0x00100000) { /* zero or subnormal? */58u.f = x*0x1p54;59hx = u.i>>32 & 0x7fffffff;60if (hx == 0)61return x; /* cbrt(0) is itself */62hx = hx/3 + B2;63} else64hx = hx/3 + B1;65u.i &= 1ULL<<63;66u.i |= (uint64_t)hx << 32;67t = u.f;6869/*70* New cbrt to 23 bits:71* cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)72* where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)73* to within 2**-23.5 when |r - 1| < 1/10. The rough approximation74* has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this75* gives us bounds for r = t**3/x.76*77* Try to optimize for parallel evaluation as in __tanf.c.78*/79r = (t*t)*(t/x);80t = t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4));8182/*83* Round t away from zero to 23 bits (sloppily except for ensuring that84* the result is larger in magnitude than cbrt(x) but not much more than85* 2 23-bit ulps larger). With rounding towards zero, the error bound86* would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps87* in the rounded t, the infinite-precision error in the Newton88* approximation barely affects third digit in the final error89* 0.667; the error in the rounded t can be up to about 3 23-bit ulps90* before the final error is larger than 0.667 ulps.91*/92u.f = t;93u.i = (u.i + 0x80000000) & 0xffffffffc0000000ULL;94t = u.f;9596/* one step Newton iteration to 53 bits with error < 0.667 ulps */97s = t*t; /* t*t is exact */98r = x/s; /* error <= 0.5 ulps; |r| < |t| */99w = t+t; /* t+t is exact */100r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */101t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */102return t;103}104105106