POK
tan.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_tan.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 #ifdef POK_NEEDS_LIBMATH
30 
31 /* tan(x)
32  * Return tangent function of x.
33  *
34  * kernel function:
35  * __kernel_tan ... tangent function on [-pi/4,pi/4]
36  * __ieee754_rem_pio2 ... argument reduction routine
37  *
38  * Method.
39  * Let S,C and T denote the sin, cos and tan respectively on
40  * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
41  * in [-pi/4 , +pi/4], and let n = k mod 4.
42  * We have
43  *
44  * n sin(x) cos(x) tan(x)
45  * ----------------------------------------------------------
46  * 0 S C T
47  * 1 C -S -1/T
48  * 2 -S -C T
49  * 3 -C S -1/T
50  * ----------------------------------------------------------
51  *
52  * Special cases:
53  * Let trig be any of sin, cos, or tan.
54  * trig(+-INF) is NaN, with signals;
55  * trig(NaN) is that NaN;
56  *
57  * Accuracy:
58  * TRIG(x) returns trig(x) nearly rounded
59  */
60 
61 #include <libm.h>
62 #include "math_private.h"
63 
64 double
65 tan(double x)
66 {
67  double y[2],z=0.0;
68  int32_t n, ix;
69 
70  /* High word of x. */
71  GET_HIGH_WORD(ix,x);
72 
73  /* |x| ~< pi/4 */
74  ix &= 0x7fffffff;
75  if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1);
76 
77  /* tan(Inf or NaN) is NaN */
78  else if (ix>=0x7ff00000) return x-x; /* NaN */
79 
80  /* argument reduction needed */
81  else {
82  n = __ieee754_rem_pio2(x,y);
83  return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even
84  -1 -- n odd */
85  }
86 }
87 
88 #endif