POK
cos.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* @(#)s_cos.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 /* cos(x)
30  * Return cosine function of x.
31  *
32  * kernel function:
33  * __kernel_sin ... sine function on [-pi/4,pi/4]
34  * __kernel_cos ... cosine function on [-pi/4,pi/4]
35  * __ieee754_rem_pio2 ... argument reduction routine
36  *
37  * Method.
38  * Let S,C and T denote the sin, cos and tan respectively on
39  * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
40  * in [-pi/4 , +pi/4], and let n = k mod 4.
41  * We have
42  *
43  * n sin(x) cos(x) tan(x)
44  * ----------------------------------------------------------
45  * 0 S C T
46  * 1 C -S -1/T
47  * 2 -S -C T
48  * 3 -C S -1/T
49  * ----------------------------------------------------------
50  *
51  * Special cases:
52  * Let trig be any of sin, cos, or tan.
53  * trig(+-INF) is NaN, with signals;
54  * trig(NaN) is that NaN;
55  *
56  * Accuracy:
57  * TRIG(x) returns trig(x) nearly rounded
58  */
59 #ifdef POK_NEEDS_LIBMATH
60 #include <libm.h>
61 #include "namespace.h"
62 #include "math_private.h"
63 
64 #if 0 /* notyet */
65 #ifdef __weak_alias
66 __weak_alias(cos, _cos)
67 #endif
68 #endif
69 
70 double
71 cos(double x)
72 {
73  double y[2],z=0.0;
74  int32_t n, ix;
75 
76  /* High word of x. */
77  GET_HIGH_WORD(ix,x);
78 
79  /* |x| ~< pi/4 */
80  ix &= 0x7fffffff;
81  if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
82 
83  /* cos(Inf or NaN) is NaN */
84  else if (ix>=0x7ff00000) return x-x;
85 
86  /* argument reduction needed */
87  else {
88  n = __ieee754_rem_pio2(x,y);
89  switch(n&3) {
90  case 0: return __kernel_cos(y[0],y[1]);
91  case 1: return -__kernel_sin(y[0],y[1],1);
92  case 2: return -__kernel_cos(y[0],y[1]);
93  default:
94  return __kernel_sin(y[0],y[1],1);
95  }
96  }
97 }
98 
99 #endif