s_cos.c revision 2116
154359Sroberto/* @(#)s_cos.c 5.1 93/09/24 */
254359Sroberto/*
354359Sroberto * ====================================================
454359Sroberto * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
554359Sroberto *
654359Sroberto * Developed at SunPro, a Sun Microsystems, Inc. business.
754359Sroberto * Permission to use, copy, modify, and distribute this
854359Sroberto * software is freely granted, provided that this notice
954359Sroberto * is preserved.
1054359Sroberto * ====================================================
1154359Sroberto */
1254359Sroberto
1354359Sroberto#ifndef lint
1454359Srobertostatic char rcsid[] = "$Id: s_cos.c,v 1.5 1994/08/18 23:06:34 jtc Exp $";
1554359Sroberto#endif
1654359Sroberto
1754359Sroberto/* cos(x)
1854359Sroberto * Return cosine function of x.
1954359Sroberto *
2054359Sroberto * kernel function:
2154359Sroberto *	__kernel_sin		... sine function on [-pi/4,pi/4]
2254359Sroberto *	__kernel_cos		... cosine function on [-pi/4,pi/4]
2354359Sroberto *	__ieee754_rem_pio2	... argument reduction routine
2454359Sroberto *
2554359Sroberto * Method.
2654359Sroberto *      Let S,C and T denote the sin, cos and tan respectively on
2754359Sroberto *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
2854359Sroberto *	in [-pi/4 , +pi/4], and let n = k mod 4.
2954359Sroberto *	We have
3054359Sroberto *
3154359Sroberto *          n        sin(x)      cos(x)        tan(x)
3254359Sroberto *     ----------------------------------------------------------
3354359Sroberto *	    0	       S	   C		 T
3454359Sroberto *	    1	       C	  -S		-1/T
3554359Sroberto *	    2	      -S	  -C		 T
3654359Sroberto *	    3	      -C	   S		-1/T
3754359Sroberto *     ----------------------------------------------------------
3854359Sroberto *
3954359Sroberto * Special cases:
4054359Sroberto *      Let trig be any of sin, cos, or tan.
4154359Sroberto *      trig(+-INF)  is NaN, with signals;
4254359Sroberto *      trig(NaN)    is that NaN;
4354359Sroberto *
4454359Sroberto * Accuracy:
4554359Sroberto *	TRIG(x) returns trig(x) nearly rounded
4654359Sroberto */
4754359Sroberto
4854359Sroberto#include "math.h"
4954359Sroberto#include "math_private.h"
5054359Sroberto
5154359Sroberto#ifdef __STDC__
5254359Sroberto	double cos(double x)
5354359Sroberto#else
5454359Sroberto	double cos(x)
5554359Sroberto	double x;
5654359Sroberto#endif
5754359Sroberto{
5854359Sroberto	double y[2],z=0.0;
5954359Sroberto	int32_t n, ix;
6054359Sroberto
6154359Sroberto    /* High word of x. */
6254359Sroberto	GET_HIGH_WORD(ix,x);
6354359Sroberto
6454359Sroberto    /* |x| ~< pi/4 */
6554359Sroberto	ix &= 0x7fffffff;
6654359Sroberto	if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
6754359Sroberto
6854359Sroberto    /* cos(Inf or NaN) is NaN */
6954359Sroberto	else if (ix>=0x7ff00000) return x-x;
7054359Sroberto
7154359Sroberto    /* argument reduction needed */
7254359Sroberto	else {
7354359Sroberto	    n = __ieee754_rem_pio2(x,y);
7454359Sroberto	    switch(n&3) {
7554359Sroberto		case 0: return  __kernel_cos(y[0],y[1]);
7654359Sroberto		case 1: return -__kernel_sin(y[0],y[1],1);
7754359Sroberto		case 2: return -__kernel_cos(y[0],y[1]);
7854359Sroberto		default:
7954359Sroberto		        return  __kernel_sin(y[0],y[1],1);
8054359Sroberto	    }
8154359Sroberto	}
8254359Sroberto}
8354359Sroberto