1 /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrt.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
12 * Optimized by Bruce D. Evans.
15 * Return cube root of x
21 B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
22 B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
24 /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
26 P0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */
27 P1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */
28 P2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */
29 P3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */
30 P4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */
40 EXTRACT_WORDS(hx, low, x);
41 sign = hx & 0x80000000;
43 if (hx >= 0x7ff00000) /* cbrt(NaN,INF) is itself */
47 * Rough cbrt to 5 bits:
48 * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
49 * where e is integral and >= 0, m is real and in [0, 1), and "/" and
50 * "%" are integer division and modulus with rounding towards minus
51 * infinity. The RHS is always >= the LHS and has a maximum relative
52 * error of about 1 in 16. Adding a bias of -0.03306235651 to the
53 * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
54 * floating point representation, for finite positive normal values,
55 * ordinary integer divison of the value in bits magically gives
56 * almost exactly the RHS of the above provided we first subtract the
57 * exponent bias (1023 for doubles) and later add it back. We do the
58 * subtraction virtually to keep e >= 0 so that ordinary integer
59 * division rounds towards minus infinity; this is also efficient.
61 if (hx < 0x00100000) { /* zero or subnormal? */
63 return x; /* cbrt(0) is itself */
64 SET_HIGH_WORD(t, 0x43500000); /* set t = 2**54 */
66 GET_HIGH_WORD(high, t);
67 INSERT_WORDS(t, sign|((high&0x7fffffff)/3+B2), 0);
69 INSERT_WORDS(t, sign|(hx/3+B1), 0);
72 * New cbrt to 23 bits:
73 * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
74 * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
75 * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation
76 * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
77 * gives us bounds for r = t**3/x.
79 * Try to optimize for parallel evaluation as in k_tanf.c.
82 t = t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4));
85 * Round t away from zero to 23 bits (sloppily except for ensuring that
86 * the result is larger in magnitude than cbrt(x) but not much more than
87 * 2 23-bit ulps larger). With rounding towards zero, the error bound
88 * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps
89 * in the rounded t, the infinite-precision error in the Newton
90 * approximation barely affects third digit in the final error
91 * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
92 * before the final error is larger than 0.667 ulps.
95 u.bits = (u.bits + 0x80000000) & 0xffffffffc0000000ULL;
98 /* one step Newton iteration to 53 bits with error < 0.667 ulps */
99 s = t*t; /* t*t is exact */
100 r = x/s; /* error <= 0.5 ulps; |r| < |t| */
101 w = t+t; /* t+t is exact */
102 r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
103 t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */