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.
16 * FreeBSD SVN: 219576 (2011-03-12)
24 #include "math_private.h"
26 #define BIAS (LDBL_MAX_EXP - 1)
29 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
34 union IEEEl2bits u, v;
35 long double r, s, t, w;
43 expsign = u.xbits.expsign;
47 * If x = +-Inf, then cbrt(x) = +-Inf.
48 * If x = NaN, then cbrt(x) = NaN.
50 if (k == BIAS + LDBL_MAX_EXP)
62 /* If x = +-0, then cbrt(x) = +-0. */
63 if ((u.bits.manh | u.bits.manl) == 0) {
70 /* Adjust subnormal numbers. */
76 u.xbits.expsign = BIAS;
92 v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
95 * The following is the guts of s_cbrtf, with the handling of
96 * special values removed and extra care for accuracy not taken,
97 * but with most of the extra accuracy not discarded.
100 /* ~5-bit estimate: */
102 GET_FLOAT_WORD(hx, fx);
103 SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
105 /* ~16-bit estimate: */
109 dt = dt * (dx + dx + dr) / (dx + dr + dr);
111 /* ~47-bit estimate: */
113 dt = dt * (dx + dx + dr) / (dx + dr + dr);
116 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
117 * Round it away from zero to 32 bits (32 so that t*t is exact, and
118 * away from zero for technical reasons).
120 volatile double vd2 = 0x1.0p32;
121 volatile double vd1 = 0x1.0p-31;
122 #define vd ((long double)vd2 + vd1)
124 t = dt + vd - 0x1.0p32;
127 * Final step Newton iteration to 64 or 113 bits with
130 s=t*t; /* t*t is exact */
131 r=x/s; /* error <= 0.5 ulps; |r| < |t| */
132 w=t+t; /* t+t is exact */
133 r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
134 t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */