1 /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtl.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
13 * The argument reduction and testing for exceptional cases was
14 * written by Steven G. Kargl with input from Bruce D. Evans
15 * and David A. Schultz.
20 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
21 long double cbrtl(long double x
)
25 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
26 static const unsigned B1
= 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
28 long double cbrtl(long double x
)
30 union ldshape u
= {x
}, v
;
31 union {float f
; uint32_t i
;} uft
;
32 long double r
, s
, t
, w
;
35 int e
= u
.i
.se
& 0x7fff;
36 int sign
= u
.i
.se
& 0x8000;
39 * If x = +-Inf, then cbrt(x) = +-Inf.
40 * If x = NaN, then cbrt(x) = NaN.
45 /* Adjust subnormal numbers. */
48 /* If x = +-0, then cbrt(x) = +-0. */
69 v
.i
.se
= sign
| (0x3fff + e
/3);
72 * The following is the guts of s_cbrtf, with the handling of
73 * special values removed and extra care for accuracy not taken,
74 * but with most of the extra accuracy not discarded.
77 /* ~5-bit estimate: */
79 uft
.i
= (uft
.i
& 0x7fffffff)/3 + B1
;
82 /* ~16-bit estimate: */
86 dt
= dt
* (dx
+ dx
+ dr
) / (dx
+ dr
+ dr
);
88 /* ~47-bit estimate: */
90 dt
= dt
* (dx
+ dx
+ dr
) / (dx
+ dr
+ dr
);
92 #if LDBL_MANT_DIG == 64
94 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
95 * Round it away from zero to 32 bits (32 so that t*t is exact, and
96 * away from zero for technical reasons).
98 t
= dt
+ (0x1.0p32L
+ 0x1.0p
-31L) - 0x1.0p32
;
99 #elif LDBL_MANT_DIG == 113
101 * Round dt away from zero to 47 bits. Since we don't trust the 47,
102 * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and
103 * might be avoidable in this case, since on most machines dt will
104 * have been evaluated in 53-bit precision and the technical reasons
105 * for rounding up might not apply to either case in cbrtl() since
106 * dt is much more accurate than needed.
108 t
= dt
+ 0x2.0p
-46 + 0x1.0p60L
- 0x1.0p60
;
112 * Final step Newton iteration to 64 or 113 bits with
115 s
= t
*t
; /* t*t is exact */
116 r
= x
/s
; /* error <= 0.5 ulps; |r| < |t| */
117 w
= t
+t
; /* t+t is exact */
118 r
= (r
-t
)/(w
+r
); /* r-t is exact; w+r ~= 3*t */
119 t
= t
+t
*r
; /* error <= 0.5 + 0.5/3 + epsilon */