2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
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 * The argument reduction and testing for exceptional cases was
13 * written by Steven G. Kargl with input from Bruce D. Evans
14 * and David A. Schultz.
17 #include <sys/cdefs.h>
25 #include "math_private.h"
27 #define BIAS (LDBL_MAX_EXP - 1)
30 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
35 union IEEEl2bits u, v;
36 long double r, s, t, w;
44 expsign = u.xbits.expsign;
48 * If x = +-Inf, then cbrt(x) = +-Inf.
49 * If x = NaN, then cbrt(x) = NaN.
51 if (k == BIAS + LDBL_MAX_EXP)
56 /* If x = +-0, then cbrt(x) = +-0. */
57 if ((u.bits.manh | u.bits.manl) == 0)
59 /* Adjust subnormal numbers. */
65 u.xbits.expsign = BIAS;
81 v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
84 * The following is the guts of s_cbrtf, with the handling of
85 * special values removed and extra care for accuracy not taken,
86 * but with most of the extra accuracy not discarded.
89 /* ~5-bit estimate: */
91 GET_FLOAT_WORD(hx, fx);
92 SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
94 /* ~16-bit estimate: */
98 dt = dt * (dx + dx + dr) / (dx + dr + dr);
100 /* ~47-bit estimate: */
102 dt = dt * (dx + dx + dr) / (dx + dr + dr);
104 #if LDBL_MANT_DIG == 64
106 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
107 * Round it away from zero to 32 bits (32 so that t*t is exact, and
108 * away from zero for technical reasons).
110 volatile double vd2 = 0x1.0p32;
111 volatile double vd1 = 0x1.0p-31;
112 #define vd ((long double)vd2 + vd1)
114 t = dt + vd - 0x1.0p32;
115 #elif LDBL_MANT_DIG == 113
117 * Round dt away from zero to 47 bits. Since we don't trust the 47,
118 * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and
119 * might be avoidable in this case, since on most machines dt will
120 * have been evaluated in 53-bit precision and the technical reasons
121 * for rounding up might not apply to either case in cbrtl() since
122 * dt is much more accurate than needed.
124 t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
126 #error "Unsupported long double format"
130 * Final step Newton iteration to 64 or 113 bits with
133 s=t*t; /* t*t is exact */
134 r=x/s; /* error <= 0.5 ulps; |r| < |t| */
135 w=t+t; /* t+t is exact */
136 r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
137 t=t+t*r; /* error <= (0.5 + 0.5/3) * ulp */