Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libm / common / R / cosf.c
blob7927d7383481bf2e6e58e701d6a3f4b7b8177ff7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #pragma weak __cosf = cosf
32 * See sincosf.c
35 #include "libm.h"
37 extern const int _TBL_ipio2_inf[];
38 extern int __rem_pio2m(double *, double *, int, int, int, const int *);
39 #if defined(__i386) && !defined(__amd64)
40 extern int __swapRP(int);
41 #endif
43 static const double C[] = {
44 1.85735322054308378716204874632872525989806770558e-0003,
45 -1.95035094218403635082921458859320791358115801259e-0004,
46 5.38400550766074785970952495168558701485841707252e+0002,
47 -3.31975110777873728964197739157371509422022905947e+0001,
48 1.09349482127188401868272000389539985058873853699e-0003,
49 -5.03324285989964979398034700054920226866107675091e-0004,
50 2.43792880266971107750418061559602239831538067410e-0005,
51 9.14499072605666582228127405245558035523741471271e+0002,
52 -3.63151270591815439197122504991683846785293207730e+0001,
53 0.636619772367581343075535, /* 2^ -1 * 1.45F306DC9C883 */
54 0.5,
55 1.570796326734125614166, /* 2^ 0 * 1.921FB54400000 */
56 6.077100506506192601475e-11, /* 2^-34 * 1.0B4611A626331 */
59 #define S0 C[0]
60 #define S1 C[1]
61 #define S2 C[2]
62 #define S3 C[3]
63 #define C0 C[4]
64 #define C1 C[5]
65 #define C2 C[6]
66 #define C3 C[7]
67 #define C4 C[8]
68 #define invpio2 C[9]
69 #define half C[10]
70 #define pio2_1 C[11]
71 #define pio2_t C[12]
73 float
74 cosf(float x)
76 double y, z, w;
77 float f;
78 int n, ix, hx, hy;
79 volatile int i __unused;
81 hx = *((int *)&x);
82 ix = hx & 0x7fffffff;
84 y = (double)x;
86 if (ix <= 0x4016cbe4) { /* |x| < 3*pi/4 */
87 if (ix <= 0x3f490fdb) { /* |x| < pi/4 */
88 if (ix <= 0x39800000) { /* |x| <= 2**-12 */
89 i = (int)y;
90 return (1.0f);
92 z = y * y;
93 return ((float)(((C0 + z * C1) + (z * z) * C2) *
94 (C3 + z * (C4 + z))));
95 } else if (hx > 0) {
96 y = (y - pio2_1) - pio2_t;
97 z = y * y;
98 return ((float)-((y * (S0 + z * S1)) *
99 (S2 + z * (S3 + z))));
100 } else {
101 y = (y + pio2_1) + pio2_t;
102 z = y * y;
103 return ((float)((y * (S0 + z * S1)) *
104 (S2 + z * (S3 + z))));
106 } else if (ix <= 0x49c90fdb) { /* |x| < 2^19*pi */
107 #if defined(__i386) && !defined(__amd64)
108 int rp;
110 rp = __swapRP(fp_extended);
111 #endif
112 w = y * invpio2;
113 if (hx < 0)
114 n = (int)(w - half);
115 else
116 n = (int)(w + half);
117 y = (y - n * pio2_1) - n * pio2_t;
118 n++;
119 #if defined(__i386) && !defined(__amd64)
120 if (rp != fp_extended)
121 (void) __swapRP(rp);
122 #endif
123 } else {
124 if (ix >= 0x7f800000)
125 return (x / x); /* cos(Inf or NaN) is NaN */
126 hy = ((int *)&y)[HIWORD];
127 n = ((hy >> 20) & 0x7ff) - 1046;
128 ((int *)&w)[HIWORD] = (hy & 0xfffff) | 0x41600000;
129 ((int *)&w)[LOWORD] = ((int *)&y)[LOWORD];
130 n = __rem_pio2m(&w, &y, n, 1, 0, _TBL_ipio2_inf) + 1;
133 if (n & 1) {
134 /* compute cos y */
135 z = y * y;
136 f = (float)(((C0 + z * C1) + (z * z) * C2) *
137 (C3 + z * (C4 + z)));
138 } else {
139 /* compute sin y */
140 z = y * y;
141 f = (float)((y * (S0 + z * S1)) * (S2 + z * (S3 + z)));
144 return ((n & 2)? -f : f);