src/libm/s_cos.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 12:00:21 -0400
changeset 11994 8e094f91ddab
parent 11683 48bcba563d9c
permissions -rw-r--r--
thread: fixed compiler warnings on non-Linux systems that use pthread.

(static function rtkit_setpriority was unused, moved it in with rest of
__LINUX__ section.)
slouken@2756
     1
/*
slouken@2756
     2
 * ====================================================
slouken@2756
     3
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
slouken@2756
     4
 *
slouken@2756
     5
 * Developed at SunPro, a Sun Microsystems, Inc. business.
slouken@2756
     6
 * Permission to use, copy, modify, and distribute this
slouken@2756
     7
 * software is freely granted, provided that this notice
slouken@2756
     8
 * is preserved.
slouken@2756
     9
 * ====================================================
slouken@2756
    10
 */
slouken@2756
    11
slouken@2756
    12
/* cos(x)
slouken@2756
    13
 * Return cosine function of x.
slouken@2756
    14
 *
slouken@2756
    15
 * kernel function:
slouken@2756
    16
 *	__kernel_sin		... sine function on [-pi/4,pi/4]
slouken@2756
    17
 *	__kernel_cos		... cosine function on [-pi/4,pi/4]
slouken@2756
    18
 *	__ieee754_rem_pio2	... argument reduction routine
slouken@2756
    19
 *
slouken@2756
    20
 * Method.
slouken@2756
    21
 *      Let S,C and T denote the sin, cos and tan respectively on
slouken@2756
    22
 *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
slouken@2756
    23
 *	in [-pi/4 , +pi/4], and let n = k mod 4.
slouken@2756
    24
 *	We have
slouken@2756
    25
 *
slouken@2756
    26
 *          n        sin(x)      cos(x)        tan(x)
slouken@2756
    27
 *     ----------------------------------------------------------
slouken@2756
    28
 *	    0	       S	   C		 T
slouken@2756
    29
 *	    1	       C	  -S		-1/T
slouken@2756
    30
 *	    2	      -S	  -C		 T
slouken@2756
    31
 *	    3	      -C	   S		-1/T
slouken@2756
    32
 *     ----------------------------------------------------------
slouken@2756
    33
 *
slouken@2756
    34
 * Special cases:
slouken@2756
    35
 *      Let trig be any of sin, cos, or tan.
slouken@2756
    36
 *      trig(+-INF)  is NaN, with signals;
slouken@2756
    37
 *      trig(NaN)    is that NaN;
slouken@2756
    38
 *
slouken@2756
    39
 * Accuracy:
slouken@2756
    40
 *	TRIG(x) returns trig(x) nearly rounded
slouken@2756
    41
 */
slouken@2756
    42
slouken@6044
    43
#include "math_libm.h"
slouken@2756
    44
#include "math_private.h"
slouken@2756
    45
slouken@11683
    46
double cos(double x)
slouken@2756
    47
{
slouken@11683
    48
	double y[2],z=0.0;
slouken@11683
    49
	int32_t n, ix;
slouken@2756
    50
slouken@2756
    51
    /* High word of x. */
slouken@11683
    52
	GET_HIGH_WORD(ix,x);
slouken@2756
    53
slouken@2756
    54
    /* |x| ~< pi/4 */
slouken@11683
    55
	ix &= 0x7fffffff;
slouken@11683
    56
	if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
slouken@2756
    57
slouken@2756
    58
    /* cos(Inf or NaN) is NaN */
slouken@11683
    59
	else if (ix>=0x7ff00000) return x-x;
slouken@2756
    60
slouken@2756
    61
    /* argument reduction needed */
slouken@11683
    62
	else {
slouken@11683
    63
	    n = __ieee754_rem_pio2(x,y);
slouken@11683
    64
	    switch(n&3) {
slouken@11683
    65
		case 0: return  __kernel_cos(y[0],y[1]);
slouken@11683
    66
		case 1: return -__kernel_sin(y[0],y[1],1);
slouken@11683
    67
		case 2: return -__kernel_cos(y[0],y[1]);
slouken@11683
    68
		default:
slouken@11683
    69
		        return  __kernel_sin(y[0],y[1],1);
slouken@11683
    70
	    }
slouken@11683
    71
	}
slouken@2756
    72
}
slouken@2756
    73
libm_hidden_def(cos)