Path: blob/master/src/java.base/share/native/libfdlibm/s_cos.c
41152 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* cos(x)26* Return cosine function of x.27*28* kernel function:29* __kernel_sin ... sine function on [-pi/4,pi/4]30* __kernel_cos ... cosine function on [-pi/4,pi/4]31* __ieee754_rem_pio2 ... argument reduction routine32*33* Method.34* Let S,C and T denote the sin, cos and tan respectively on35* [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/236* in [-pi/4 , +pi/4], and let n = k mod 4.37* We have38*39* n sin(x) cos(x) tan(x)40* ----------------------------------------------------------41* 0 S C T42* 1 C -S -1/T43* 2 -S -C T44* 3 -C S -1/T45* ----------------------------------------------------------46*47* Special cases:48* Let trig be any of sin, cos, or tan.49* trig(+-INF) is NaN, with signals;50* trig(NaN) is that NaN;51*52* Accuracy:53* TRIG(x) returns trig(x) nearly rounded54*/5556#include "fdlibm.h"5758#ifdef __STDC__59double cos(double x)60#else61double cos(x)62double x;63#endif64{65double y[2],z=0.0;66int n, ix;6768/* High word of x. */69ix = __HI(x);7071/* |x| ~< pi/4 */72ix &= 0x7fffffff;73if(ix <= 0x3fe921fb) return __kernel_cos(x,z);7475/* cos(Inf or NaN) is NaN */76else if (ix>=0x7ff00000) return x-x;7778/* argument reduction needed */79else {80n = __ieee754_rem_pio2(x,y);81switch(n&3) {82case 0: return __kernel_cos(y[0],y[1]);83case 1: return -__kernel_sin(y[0],y[1],1);84case 2: return -__kernel_cos(y[0],y[1]);85default:86return __kernel_sin(y[0],y[1],1);87}88}89}909192