s_sin.c revision 117912
1184610Salfred/* @(#)s_sin.c 5.1 93/09/24 */ 2184610Salfred/* 3184610Salfred * ==================================================== 4184610Salfred * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5184610Salfred * 6184610Salfred * Developed at SunPro, a Sun Microsystems, Inc. business. 7184610Salfred * Permission to use, copy, modify, and distribute this 8184610Salfred * software is freely granted, provided that this notice 9184610Salfred * is preserved. 10184610Salfred * ==================================================== 11184610Salfred */ 12184610Salfred 13184610Salfred#ifndef lint 14184610Salfredstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_sin.c 117912 2003-07-23 04:53:47Z peter $"; 15184610Salfred#endif 16184610Salfred 17184610Salfred/* sin(x) 18184610Salfred * Return sine function of x. 19184610Salfred * 20184610Salfred * kernel function: 21184610Salfred * __kernel_sin ... sine function on [-pi/4,pi/4] 22184610Salfred * __kernel_cos ... cose function on [-pi/4,pi/4] 23184610Salfred * __ieee754_rem_pio2 ... argument reduction routine 24184610Salfred * 25184610Salfred * Method. 26184610Salfred * Let S,C and T denote the sin, cos and tan respectively on 27184610Salfred * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 28184610Salfred * in [-pi/4 , +pi/4], and let n = k mod 4. 29184610Salfred * We have 30184610Salfred * 31184610Salfred * n sin(x) cos(x) tan(x) 32184610Salfred * ---------------------------------------------------------- 33184610Salfred * 0 S C T 34213852Shselasky * 1 C -S -1/T 35213852Shselasky * 2 -S -C T 36213852Shselasky * 3 -C S -1/T 37213852Shselasky * ---------------------------------------------------------- 38213852Shselasky * 39213852Shselasky * Special cases: 40184610Salfred * Let trig be any of sin, cos, or tan. 41184610Salfred * trig(+-INF) is NaN, with signals; 42184610Salfred * trig(NaN) is that NaN; 43184610Salfred * 44184610Salfred * Accuracy: 45184610Salfred * TRIG(x) returns trig(x) nearly rounded 46184610Salfred */ 47184610Salfred 48184610Salfred#include "math.h" 49184610Salfred#include "math_private.h" 50184610Salfred 51184610Salfreddouble 52184610Salfredsin(double x) 53184610Salfred{ 54184610Salfred double y[2],z=0.0; 55184610Salfred int32_t n, ix; 56184610Salfred 57184610Salfred /* High word of x. */ 58192984Sthompsa GET_HIGH_WORD(ix,x); 59188622Sthompsa 60184610Salfred /* |x| ~< pi/4 */ 61184610Salfred ix &= 0x7fffffff; 62184610Salfred if(ix <= 0x3fe921fb) return __kernel_sin(x,z,0); 63188987Sthompsa 64188987Sthompsa /* sin(Inf or NaN) is NaN */ 65184610Salfred else if (ix>=0x7ff00000) return x-x; 66184610Salfred 67184610Salfred /* argument reduction needed */ 68184610Salfred else { 69184610Salfred n = __ieee754_rem_pio2(x,y); 70184610Salfred switch(n&3) { 71184610Salfred case 0: return __kernel_sin(y[0],y[1],1); 72184610Salfred case 1: return __kernel_cos(y[0],y[1]); 73184610Salfred case 2: return -__kernel_sin(y[0],y[1],1); 74184610Salfred default: 75184610Salfred return -__kernel_cos(y[0],y[1]); 76184610Salfred } 77184610Salfred } 78188622Sthompsa} 79188622Sthompsa