1/*
2 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* cos(x)
27 * Return cosine function of x.
28 *
29 * kernel function:
30 * __kernel_sin ... sine function on [-pi/4,pi/4]
31 * __kernel_cos ... cosine function on [-pi/4,pi/4]
32 * __ieee754_rem_pio2 ... argument reduction routine
33 *
34 * Method.
35 * Let S,C and T denote the sin, cos and tan respectively on
36 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
37 * in [-pi/4 , +pi/4], and let n = k mod 4.
38 * We have
39 *
40 * n sin(x) cos(x) tan(x)
41 * ----------------------------------------------------------
42 * 0 S C T
43 * 1 C -S -1/T
44 * 2 -S -C T
45 * 3 -C S -1/T
46 * ----------------------------------------------------------
47 *
48 * Special cases:
49 * Let trig be any of sin, cos, or tan.
50 * trig(+-INF) is NaN, with signals;
51 * trig(NaN) is that NaN;
52 *
53 * Accuracy:
54 * TRIG(x) returns trig(x) nearly rounded
55 */
56
57#include "fdlibm.h"
58
59#ifdef __STDC__
60 double cos(double x)
61#else
62 double cos(x)
63 double x;
64#endif
65{
66 double y[2],z=0.0;
67 int n, ix;
68
69 /* High word of x. */
70 ix = __HI(x);
71
72 /* |x| ~< pi/4 */
73 ix &= 0x7fffffff;
74 if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
75
76 /* cos(Inf or NaN) is NaN */
77 else if (ix>=0x7ff00000) return x-x;
78
79 /* argument reduction needed */
80 else {
81 n = __ieee754_rem_pio2(x,y);
82 switch(n&3) {
83 case 0: return __kernel_cos(y[0],y[1]);
84 case 1: return -__kernel_sin(y[0],y[1],1);
85 case 2: return -__kernel_cos(y[0],y[1]);
86 default:
87 return __kernel_sin(y[0],y[1],1);
88 }
89 }
90}
91