1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/s_tan.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier * ====================================================
4*f504f610SAugustin Cavalier * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*f504f610SAugustin Cavalier *
6*f504f610SAugustin Cavalier * Developed at SunPro, a Sun Microsystems, Inc. business.
7*f504f610SAugustin Cavalier * Permission to use, copy, modify, and distribute this
8*f504f610SAugustin Cavalier * software is freely granted, provided that this notice
9*f504f610SAugustin Cavalier * is preserved.
10*f504f610SAugustin Cavalier * ====================================================
11*f504f610SAugustin Cavalier */
12*f504f610SAugustin Cavalier /* tan(x)
13*f504f610SAugustin Cavalier * Return tangent function of x.
14*f504f610SAugustin Cavalier *
15*f504f610SAugustin Cavalier * kernel function:
16*f504f610SAugustin Cavalier * __tan ... tangent function on [-pi/4,pi/4]
17*f504f610SAugustin Cavalier * __rem_pio2 ... argument reduction routine
18*f504f610SAugustin Cavalier *
19*f504f610SAugustin Cavalier * Method.
20*f504f610SAugustin Cavalier * Let S,C and T denote the sin, cos and tan respectively on
21*f504f610SAugustin Cavalier * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
22*f504f610SAugustin Cavalier * in [-pi/4 , +pi/4], and let n = k mod 4.
23*f504f610SAugustin Cavalier * We have
24*f504f610SAugustin Cavalier *
25*f504f610SAugustin Cavalier * n sin(x) cos(x) tan(x)
26*f504f610SAugustin Cavalier * ----------------------------------------------------------
27*f504f610SAugustin Cavalier * 0 S C T
28*f504f610SAugustin Cavalier * 1 C -S -1/T
29*f504f610SAugustin Cavalier * 2 -S -C T
30*f504f610SAugustin Cavalier * 3 -C S -1/T
31*f504f610SAugustin Cavalier * ----------------------------------------------------------
32*f504f610SAugustin Cavalier *
33*f504f610SAugustin Cavalier * Special cases:
34*f504f610SAugustin Cavalier * Let trig be any of sin, cos, or tan.
35*f504f610SAugustin Cavalier * trig(+-INF) is NaN, with signals;
36*f504f610SAugustin Cavalier * trig(NaN) is that NaN;
37*f504f610SAugustin Cavalier *
38*f504f610SAugustin Cavalier * Accuracy:
39*f504f610SAugustin Cavalier * TRIG(x) returns trig(x) nearly rounded
40*f504f610SAugustin Cavalier */
41*f504f610SAugustin Cavalier
42*f504f610SAugustin Cavalier #include "libm.h"
43*f504f610SAugustin Cavalier
tan(double x)44*f504f610SAugustin Cavalier double tan(double x)
45*f504f610SAugustin Cavalier {
46*f504f610SAugustin Cavalier double y[2];
47*f504f610SAugustin Cavalier uint32_t ix;
48*f504f610SAugustin Cavalier unsigned n;
49*f504f610SAugustin Cavalier
50*f504f610SAugustin Cavalier GET_HIGH_WORD(ix, x);
51*f504f610SAugustin Cavalier ix &= 0x7fffffff;
52*f504f610SAugustin Cavalier
53*f504f610SAugustin Cavalier /* |x| ~< pi/4 */
54*f504f610SAugustin Cavalier if (ix <= 0x3fe921fb) {
55*f504f610SAugustin Cavalier if (ix < 0x3e400000) { /* |x| < 2**-27 */
56*f504f610SAugustin Cavalier /* raise inexact if x!=0 and underflow if subnormal */
57*f504f610SAugustin Cavalier FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
58*f504f610SAugustin Cavalier return x;
59*f504f610SAugustin Cavalier }
60*f504f610SAugustin Cavalier return __tan(x, 0.0, 0);
61*f504f610SAugustin Cavalier }
62*f504f610SAugustin Cavalier
63*f504f610SAugustin Cavalier /* tan(Inf or NaN) is NaN */
64*f504f610SAugustin Cavalier if (ix >= 0x7ff00000)
65*f504f610SAugustin Cavalier return x - x;
66*f504f610SAugustin Cavalier
67*f504f610SAugustin Cavalier /* argument reduction */
68*f504f610SAugustin Cavalier n = __rem_pio2(x, y);
69*f504f610SAugustin Cavalier return __tan(y[0], y[1], n&1);
70*f504f610SAugustin Cavalier }
71