GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libm/src/ld80/s_cbrtl.c Lines: 0 43 0.0 %
Date: 2017-11-13 Branches: 0 11 0.0 %

Line Branch Exec Source
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
29
cbrtl(long double x)
30
{
31
	long double v, r, s, t, w;
32
	double dr, dt, dx;
33
	float ft, fx;
34
	uint32_t hx, lx;
35
	uint16_t expsign, es;
36
	int k;
37
	volatile double vd1, vd2;
38
39
	GET_LDOUBLE_EXP(expsign,x);
40
	k = expsign & 0x7fff;
41
42
	/*
43
	 * If x = +-Inf, then cbrt(x) = +-Inf.
44
	 * If x = NaN, then cbrt(x) = NaN.
45
	 */
46
	if (k == BIAS + LDBL_MAX_EXP)
47
		return (x + x);
48
49
	if (k == 0) {
50
		/* If x = +-0, then cbrt(x) = +-0. */
51
		GET_LDOUBLE_WORDS(es,hx,lx,x);
52
		if ((hx|lx) == 0) {
53
			return (x);
54
		}
55
		/* Adjust subnormal numbers. */
56
		x *= 0x1.0p514;
57
		GET_LDOUBLE_EXP(k,x);
58
		k &= 0x7fff;
59
		k -= BIAS + 514;
60
	} else
61
		k -= BIAS;
62
	SET_LDOUBLE_EXP(x,BIAS);
63
	v = 1;
64
65
	switch (k % 3) {
66
	case 1:
67
	case -2:
68
		x = 2*x;
69
		k--;
70
		break;
71
	case 2:
72
	case -1:
73
		x = 4*x;
74
		k -= 2;
75
		break;
76
	}
77
	SET_LDOUBLE_EXP(v, (expsign & 0x8000) | (BIAS + k / 3));
78
79
	/*
80
	 * The following is the guts of s_cbrtf, with the handling of
81
	 * special values removed and extra care for accuracy not taken,
82
	 * but with most of the extra accuracy not discarded.
83
	 */
84
85
	/* ~5-bit estimate: */
86
	fx = x;
87
	GET_FLOAT_WORD(hx, fx);
88
	SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
89
90
	/* ~16-bit estimate: */
91
	dx = x;
92
	dt = ft;
93
	dr = dt * dt * dt;
94
	dt = dt * (dx + dx + dr) / (dx + dr + dr);
95
96
	/* ~47-bit estimate: */
97
	dr = dt * dt * dt;
98
	dt = dt * (dx + dx + dr) / (dx + dr + dr);
99
100
	/*
101
	 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
102
	 * Round it away from zero to 32 bits (32 so that t*t is exact, and
103
	 * away from zero for technical reasons).
104
	 */
105
	vd2 = 0x1.0p32;
106
	vd1 = 0x1.0p-31;
107
	#define vd ((long double)vd2 + vd1)
108
109
	t = dt + vd - 0x1.0p32;
110
111
	/*
112
	 * Final step Newton iteration to 64 or 113 bits with
113
	 * error < 0.667 ulps
114
	 */
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 */
120
121
	t *= v;
122
	return (t);
123
}