summaryrefslogtreecommitdiff
path: root/usr/src/lib/libm/common/R/cosf.c
blob: 34b436be56832441e4c7705f63b702196ef338b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma weak cosf = __cosf

/*
 * See sincosf.c
 */

#include "libm.h"

extern const int _TBL_ipio2_inf[];
extern int __rem_pio2m(double *, double *, int, int, int, const int *);
#if defined(__i386) && !defined(__amd64)
extern int __swapRP(int);
#endif

static const double C[] = {
	1.85735322054308378716204874632872525989806770558e-0003,
	-1.95035094218403635082921458859320791358115801259e-0004,
	5.38400550766074785970952495168558701485841707252e+0002,
	-3.31975110777873728964197739157371509422022905947e+0001,
	1.09349482127188401868272000389539985058873853699e-0003,
	-5.03324285989964979398034700054920226866107675091e-0004,
	2.43792880266971107750418061559602239831538067410e-0005,
	9.14499072605666582228127405245558035523741471271e+0002,
	-3.63151270591815439197122504991683846785293207730e+0001,
	0.636619772367581343075535,	/* 2^ -1  * 1.45F306DC9C883 */
	0.5,
	1.570796326734125614166,	/* 2^  0  * 1.921FB54400000 */
	6.077100506506192601475e-11,	/* 2^-34  * 1.0B4611A626331 */
};

#define	S0	C[0]
#define	S1	C[1]
#define	S2	C[2]
#define	S3	C[3]
#define	C0	C[4]
#define	C1	C[5]
#define	C2	C[6]
#define	C3	C[7]
#define	C4	C[8]
#define	invpio2	C[9]
#define	half	C[10]
#define	pio2_1  C[11]
#define	pio2_t	C[12]

float
cosf(float x)
{
	double	y, z, w;
	float	f;
	int	n, ix, hx, hy;
	volatile int i;

	hx = *((int *)&x);
	ix = hx & 0x7fffffff;

	y = (double)x;

	if (ix <= 0x4016cbe4) {		/* |x| < 3*pi/4 */
		if (ix <= 0x3f490fdb) {		/* |x| < pi/4 */
			if (ix <= 0x39800000) {	/* |x| <= 2**-12 */
				i = (int)y;
#ifdef lint
				i = i;
#endif
				return (1.0f);
			}
			z = y * y;
			return ((float)(((C0 + z * C1) + (z * z) * C2) *
			    (C3 + z * (C4 + z))));
		} else if (hx > 0) {
			y = (y - pio2_1) - pio2_t;
			z = y * y;
			return ((float)-((y * (S0 + z * S1)) *
			    (S2 + z * (S3 + z))));
		} else {
			y = (y + pio2_1) + pio2_t;
			z = y * y;
			return ((float)((y * (S0 + z * S1)) *
			    (S2 + z * (S3 + z))));
		}
	} else if (ix <= 0x49c90fdb) {	/* |x| < 2^19*pi */
#if defined(__i386) && !defined(__amd64)
		int	rp;

		rp = __swapRP(fp_extended);
#endif
		w = y * invpio2;
		if (hx < 0)
			n = (int)(w - half);
		else
			n = (int)(w + half);
		y = (y - n * pio2_1) - n * pio2_t;
		n++;
#if defined(__i386) && !defined(__amd64)
		if (rp != fp_extended)
			(void) __swapRP(rp);
#endif
	} else {
		if (ix >= 0x7f800000)
			return (x / x); /* cos(Inf or NaN) is NaN */
		hy = ((int *)&y)[HIWORD];
		n = ((hy >> 20) & 0x7ff) - 1046;
		((int *)&w)[HIWORD] = (hy & 0xfffff) | 0x41600000;
		((int *)&w)[LOWORD] = ((int *)&y)[LOWORD];
		n = __rem_pio2m(&w, &y, n, 1, 0, _TBL_ipio2_inf) + 1;
	}

	if (n & 1) {
		/* compute cos y */
		z = y * y;
		f = (float)(((C0 + z * C1) + (z * z) * C2) *
		    (C3 + z * (C4 + z)));
	} else {
		/* compute sin y */
		z = y * y;
		f = (float)((y * (S0 + z * S1)) * (S2 + z * (S3 + z)));
	}

	return ((n & 2)? -f : f);
}