1 /*-
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.
5 *
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
9 * is preserved.
10 * ====================================================
11 *
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.
15 */
16 #include <sys/cdefs.h>
17 __RCSID("$NetBSD: s_cbrtl.c,v 1.4 2024/04/03 18:53:42 christos Exp $");
18
19
20 #include "namespace.h"
21 #include <machine/ieee.h>
22 #include <float.h>
23
24 #include "math.h"
25 #include "math_private.h"
26
27 #ifdef __HAVE_LONG_DOUBLE
28 __weak_alias(cbrtl, _cbrtl)
29
30 #define BIAS (LDBL_MAX_EXP - 1)
31
32 static const unsigned
33 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
34
35 long double
cbrtl(long double x)36 cbrtl(long double x)
37 {
38 union ieee_ext_u u, v;
39 long double r, s, t, w;
40 double dr, dt, dx;
41 float ft, fx;
42 uint32_t hx;
43 uint16_t expsign;
44 int k;
45
46 u.extu_ld = x;
47 expsign = GET_EXPSIGN(&u);
48 k = expsign & 0x7fff;
49
50 /*
51 * If x = +-Inf, then cbrt(x) = +-Inf.
52 * If x = NaN, then cbrt(x) = NaN.
53 */
54 if (k == BIAS + LDBL_MAX_EXP)
55 return (x + x);
56
57 ENTERI();
58 if (k == 0) {
59 /* If x = +-0, then cbrt(x) = +-0. */
60 if ((u.extu_frach | u.extu_fracl) == 0)
61 RETURNI(x);
62 /* Adjust subnormal numbers. */
63 u.extu_ld *= 0x1.0p514;
64 k = u.extu_exp;
65 k -= BIAS + 514;
66 } else
67 k -= BIAS;
68 SET_EXPSIGN(&u, BIAS);
69 v.extu_ld = 1;
70
71 x = u.extu_ld;
72 switch (k % 3) {
73 case 1:
74 case -2:
75 x = 2*x;
76 k--;
77 break;
78 case 2:
79 case -1:
80 x = 4*x;
81 k -= 2;
82 break;
83 }
84 SET_EXPSIGN(&v, (expsign & 0x8000) | (BIAS + k / 3));
85
86 /*
87 * The following is the guts of s_cbrtf, with the handling of
88 * special values removed and extra care for accuracy not taken,
89 * but with most of the extra accuracy not discarded.
90 */
91
92 /* ~5-bit estimate: */
93 fx = x;
94 GET_FLOAT_WORD(hx, fx);
95 SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
96
97 /* ~16-bit estimate: */
98 dx = x;
99 dt = ft;
100 dr = dt * dt * dt;
101 dt = dt * (dx + dx + dr) / (dx + dr + dr);
102
103 /* ~47-bit estimate: */
104 dr = dt * dt * dt;
105 dt = dt * (dx + dx + dr) / (dx + dr + dr);
106
107 #if LDBL_MANT_DIG == 64
108 /*
109 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
110 * Round it away from zero to 32 bits (32 so that t*t is exact, and
111 * away from zero for technical reasons).
112 */
113 volatile double vd2 = 0x1.0p32;
114 volatile double vd1 = 0x1.0p-31;
115 #define vd ((long double)vd2 + vd1)
116
117 t = dt + vd - 0x1.0p32;
118 #elif LDBL_MANT_DIG == 113
119 /*
120 * Round dt away from zero to 47 bits. Since we don't trust the 47,
121 * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and
122 * might be avoidable in this case, since on most machines dt will
123 * have been evaluated in 53-bit precision and the technical reasons
124 * for rounding up might not apply to either case in cbrtl() since
125 * dt is much more accurate than needed.
126 */
127 t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
128 #else
129 #error "Unsupported long double format"
130 #endif
131
132 /*
133 * Final step Newton iteration to 64 or 113 bits with
134 * error < 0.667 ulps
135 */
136 s=t*t; /* t*t is exact */
137 r=x/s; /* error <= 0.5 ulps; |r| < |t| */
138 w=t+t; /* t+t is exact */
139 r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
140 t=t+t*r; /* error <= (0.5 + 0.5/3) * ulp */
141
142 t *= v.extu_ld;
143 RETURNI(t);
144 }
145 #endif /* __HAVE_LONG_DOUBLE */
146