12116Sjkh/* @(#)s_cos.c 5.1 93/09/24 */ 22116Sjkh/* 32116Sjkh * ==================================================== 42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 52116Sjkh * 62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 72116Sjkh * Permission to use, copy, modify, and distribute this 88870Srgrimes * software is freely granted, provided that this notice 92116Sjkh * is preserved. 102116Sjkh * ==================================================== 112116Sjkh */ 122116Sjkh 13176385Sbde#include <sys/cdefs.h> 14176385Sbde__FBSDID("$FreeBSD$"); 152116Sjkh 162116Sjkh/* cos(x) 172116Sjkh * Return cosine function of x. 182116Sjkh * 192116Sjkh * kernel function: 202116Sjkh * __kernel_sin ... sine function on [-pi/4,pi/4] 212116Sjkh * __kernel_cos ... cosine function on [-pi/4,pi/4] 222116Sjkh * __ieee754_rem_pio2 ... argument reduction routine 232116Sjkh * 242116Sjkh * Method. 258870Srgrimes * Let S,C and T denote the sin, cos and tan respectively on 268870Srgrimes * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 272116Sjkh * in [-pi/4 , +pi/4], and let n = k mod 4. 282116Sjkh * We have 292116Sjkh * 302116Sjkh * n sin(x) cos(x) tan(x) 312116Sjkh * ---------------------------------------------------------- 322116Sjkh * 0 S C T 332116Sjkh * 1 C -S -1/T 342116Sjkh * 2 -S -C T 352116Sjkh * 3 -C S -1/T 362116Sjkh * ---------------------------------------------------------- 372116Sjkh * 382116Sjkh * Special cases: 392116Sjkh * Let trig be any of sin, cos, or tan. 402116Sjkh * trig(+-INF) is NaN, with signals; 412116Sjkh * trig(NaN) is that NaN; 422116Sjkh * 432116Sjkh * Accuracy: 448870Srgrimes * TRIG(x) returns trig(x) nearly rounded 452116Sjkh */ 462116Sjkh 47176360Sdas#include <float.h> 48176360Sdas 492116Sjkh#include "math.h" 50176385Sbde#define INLINE_REM_PIO2 512116Sjkh#include "math_private.h" 52176385Sbde#include "e_rem_pio2.c" 532116Sjkh 5497413Salfreddouble 55117912Spetercos(double x) 562116Sjkh{ 572116Sjkh double y[2],z=0.0; 582116Sjkh int32_t n, ix; 592116Sjkh 602116Sjkh /* High word of x. */ 612116Sjkh GET_HIGH_WORD(ix,x); 622116Sjkh 632116Sjkh /* |x| ~< pi/4 */ 642116Sjkh ix &= 0x7fffffff; 65151620Sbde if(ix <= 0x3fe921fb) { 66218509Sdas if(ix<0x3e46a09e) /* if x < 2**-27 * sqrt(2) */ 67151620Sbde if(((int)x)==0) return 1.0; /* generate inexact */ 68151620Sbde return __kernel_cos(x,z); 69151620Sbde } 702116Sjkh 712116Sjkh /* cos(Inf or NaN) is NaN */ 722116Sjkh else if (ix>=0x7ff00000) return x-x; 732116Sjkh 742116Sjkh /* argument reduction needed */ 752116Sjkh else { 762116Sjkh n = __ieee754_rem_pio2(x,y); 772116Sjkh switch(n&3) { 782116Sjkh case 0: return __kernel_cos(y[0],y[1]); 792116Sjkh case 1: return -__kernel_sin(y[0],y[1],1); 802116Sjkh case 2: return -__kernel_cos(y[0],y[1]); 812116Sjkh default: 822116Sjkh return __kernel_sin(y[0],y[1],1); 832116Sjkh } 842116Sjkh } 852116Sjkh} 86176360Sdas 87176360Sdas#if (LDBL_MANT_DIG == 53) 88176360Sdas__weak_reference(cos, cosl); 89176360Sdas#endif 90