1 2 /* @(#)s_sin.c 5.1 93/09/24 */ 3 /* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunPro, a Sun Microsystems, Inc. business. 8 * Permission to use, copy, modify, and distribute this 9 * software is freely granted, provided that this notice 10 * is preserved. 11 * ==================================================== 12 */ 13 14 /* 15 FUNCTION 16 <<sin>>, <<sinf>>, <<cos>>, <<cosf>>---sine or cosine 17 INDEX 18 sin 19 INDEX 20 sinf 21 INDEX 22 cos 23 INDEX 24 cosf 25 ANSI_SYNOPSIS 26 #include <math.h> 27 double sin(double <[x]>); 28 float sinf(float <[x]>); 29 double cos(double <[x]>); 30 float cosf(float <[x]>); 31 32 TRAD_SYNOPSIS 33 #include <math.h> 34 double sin(<[x]>) 35 double <[x]>; 36 float sinf(<[x]>) 37 float <[x]>; 38 39 double cos(<[x]>) 40 double <[x]>; 41 float cosf(<[x]>) 42 float <[x]>; 43 44 DESCRIPTION 45 <<sin>> and <<cos>> compute (respectively) the sine and cosine 46 of the argument <[x]>. Angles are specified in radians. 47 48 <<sinf>> and <<cosf>> are identical, save that they take and 49 return <<float>> values. 50 51 52 RETURNS 53 The sine or cosine of <[x]> is returned. 54 55 PORTABILITY 56 <<sin>> and <<cos>> are ANSI C. 57 <<sinf>> and <<cosf>> are extensions. 58 59 QUICKREF 60 sin ansi pure 61 sinf - pure 62 */ 63 64 /* sin(x) 65 * Return sine function of x. 66 * 67 * kernel function: 68 * __kernel_sin ... sine function on [-pi/4,pi/4] 69 * __kernel_cos ... cose function on [-pi/4,pi/4] 70 * __ieee754_rem_pio2 ... argument reduction routine 71 * 72 * Method. 73 * Let S,C and T denote the sin, cos and tan respectively on 74 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 75 * in [-pi/4 , +pi/4], and let n = k mod 4. 76 * We have 77 * 78 * n sin(x) cos(x) tan(x) 79 * ---------------------------------------------------------- 80 * 0 S C T 81 * 1 C -S -1/T 82 * 2 -S -C T 83 * 3 -C S -1/T 84 * ---------------------------------------------------------- 85 * 86 * Special cases: 87 * Let trig be any of sin, cos, or tan. 88 * trig(+-INF) is NaN, with signals; 89 * trig(NaN) is that NaN; 90 * 91 * Accuracy: 92 * TRIG(x) returns trig(x) nearly rounded 93 */ 94 95 #include "fdlibm.h" 96 97 #ifndef _DOUBLE_IS_32BITS 98 99 #ifdef __STDC__ sin(double x)100 double sin(double x) 101 #else 102 double sin(x) 103 double x; 104 #endif 105 { 106 double y[2],z=0.0; 107 __int32_t n,ix; 108 109 /* High word of x. */ 110 GET_HIGH_WORD(ix,x); 111 112 /* |x| ~< pi/4 */ 113 ix &= 0x7fffffff; 114 if(ix <= 0x3fe921fb) return __kernel_sin(x,z,0); 115 116 /* sin(Inf or NaN) is NaN */ 117 else if (ix>=0x7ff00000) return x-x; 118 119 /* argument reduction needed */ 120 else { 121 n = __ieee754_rem_pio2(x,y); 122 switch(n&3) { 123 case 0: return __kernel_sin(y[0],y[1],1); 124 case 1: return __kernel_cos(y[0],y[1]); 125 case 2: return -__kernel_sin(y[0],y[1],1); 126 default: 127 return -__kernel_cos(y[0],y[1]); 128 } 129 } 130 } 131 132 #endif /* _DOUBLE_IS_32BITS */ 133