xref: /openbsd-src/lib/libm/src/ld128/s_cbrtl.c (revision 4a39ccd02c887d988c1a5398dd2142879056da5f)
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 
17 #include <float.h>
18 #include <ieeefp.h>
19 #include <math.h>
20 
21 #include "math_private.h"
22 
23 #define	BIAS	(LDBL_MAX_EXP - 1)
24 
25 static const unsigned
26     B1 = 709958130;	/* B1 = (127-127.0/3-0.03306235651)*2**23 */
27 
28 long double
cbrtl(long double x)29 cbrtl(long double x)
30 {
31 	long double v, r, s, t, w;
32 	double dr, dt, dx;
33 	float ft, fx;
34 	uint64_t hx, lx;
35 	uint16_t expsign;
36 	int k;
37 
38 	GET_LDOUBLE_MSW64(hx,x);
39 	k = (hx>>48)&0x7fff;
40 
41 	/*
42 	 * If x = +-Inf, then cbrt(x) = +-Inf.
43 	 * If x = NaN, then cbrt(x) = NaN.
44 	 */
45 	if (k == BIAS + LDBL_MAX_EXP)
46 		return (x + x);
47 
48 	if (k == 0) {
49 		/* If x = +-0, then cbrt(x) = +-0. */
50 		GET_LDOUBLE_WORDS64(hx,lx,x);
51 		if (((hx&0x7fffffffffffffffLL)|lx) == 0) {
52 			return (x);
53 		}
54 		/* Adjust subnormal numbers. */
55 		x *= 0x1.0p514;
56 		GET_LDOUBLE_MSW64(hx,x);
57 		k = (hx>>48)&0x7fff;
58 		k -= BIAS + 514;
59 	} else
60 		k -= BIAS;
61 	GET_LDOUBLE_MSW64(hx,x);
62 	hx = (hx&0x8000ffffffffffffLL)|((uint64_t)BIAS<<48);
63 	SET_LDOUBLE_MSW64(x,hx);
64 	v = 1;
65 
66 	switch (k % 3) {
67 	case 1:
68 	case -2:
69 		x = 2*x;
70 		k--;
71 		break;
72 	case 2:
73 	case -1:
74 		x = 4*x;
75 		k -= 2;
76 		break;
77 	}
78 	GET_LDOUBLE_MSW64(hx,x);
79 	expsign = ((hx>>48) & 0x8000) | (BIAS + k / 3);
80 	hx = (hx&0x8000ffffffffffffLL)|((uint64_t)expsign<<48);
81 	SET_LDOUBLE_MSW64(x,hx);
82 
83 	/*
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.
87 	 */
88 
89 	/* ~5-bit estimate: */
90 	fx = x;
91 	GET_FLOAT_WORD(hx, fx);
92 	SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
93 
94 	/* ~16-bit estimate: */
95 	dx = x;
96 	dt = ft;
97 	dr = dt * dt * dt;
98 	dt = dt * (dx + dx + dr) / (dx + dr + dr);
99 
100 	/* ~47-bit estimate: */
101 	dr = dt * dt * dt;
102 	dt = dt * (dx + dx + dr) / (dx + dr + dr);
103 
104 	/*
105 	 * Round dt away from zero to 47 bits.  Since we don't trust the 47,
106 	 * add 2 47-bit ulps instead of 1 to round up.  Rounding is slow and
107 	 * might be avoidable in this case, since on most machines dt will
108 	 * have been evaluated in 53-bit precision and the technical reasons
109 	 * for rounding up might not apply to either case in cbrtl() since
110 	 * dt is much more accurate than needed.
111 	 */
112 	t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
113 
114 	/*
115 	 * Final step Newton iteration to 64 or 113 bits with
116 	 * error < 0.667 ulps
117 	 */
118 	s=t*t;				/* t*t is exact */
119 	r=x/s;				/* error <= 0.5 ulps; |r| < |t| */
120 	w=t+t;				/* t+t is exact */
121 	r=(r-t)/(w+r);			/* r-t is exact; w+r ~= 3*t */
122 	t=t+t*r;			/* error <= 0.5 + 0.5/3 + epsilon */
123 
124 	t *= v;
125 	return (t);
126 }
127