xref: /OK3568_Linux_fs/kernel/include/linux/cordic.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011 Broadcom Corporation
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, and/or distribute this software for any
5*4882a593Smuzhiyun  * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun  * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11*4882a593Smuzhiyun  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13*4882a593Smuzhiyun  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14*4882a593Smuzhiyun  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #ifndef __CORDIC_H_
17*4882a593Smuzhiyun #define __CORDIC_H_
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/types.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define CORDIC_ANGLE_GEN	39797
22*4882a593Smuzhiyun #define CORDIC_PRECISION_SHIFT	16
23*4882a593Smuzhiyun #define CORDIC_NUM_ITER	(CORDIC_PRECISION_SHIFT + 2)
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define CORDIC_FIXED(X)	((s32)((X) << CORDIC_PRECISION_SHIFT))
26*4882a593Smuzhiyun #define CORDIC_FLOAT(X)	(((X) >= 0) \
27*4882a593Smuzhiyun 		? ((((X) >> (CORDIC_PRECISION_SHIFT - 1)) + 1) >> 1) \
28*4882a593Smuzhiyun 		: -((((-(X)) >> (CORDIC_PRECISION_SHIFT - 1)) + 1) >> 1))
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun  * struct cordic_iq - i/q coordinate.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * @i: real part of coordinate (in phase).
34*4882a593Smuzhiyun  * @q: imaginary part of coordinate (quadrature).
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun struct cordic_iq {
37*4882a593Smuzhiyun 	s32 i;
38*4882a593Smuzhiyun 	s32 q;
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun  * cordic_calc_iq() - calculates the i/q coordinate for given angle.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * @theta: angle in degrees for which i/q coordinate is to be calculated.
45*4882a593Smuzhiyun  * @coord: function output parameter holding the i/q coordinate.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * The function calculates the i/q coordinate for a given angle using the
48*4882a593Smuzhiyun  * CORDIC algorithm. The coordinate consists of a real (i) and an
49*4882a593Smuzhiyun  * imaginary (q) part. The real part is essentially the cosine of the
50*4882a593Smuzhiyun  * angle and the imaginary part is the sine of the angle. The returned
51*4882a593Smuzhiyun  * values are scaled by 2^16 for precision. The range for theta is
52*4882a593Smuzhiyun  * for -180 degrees to +180 degrees. Passed values outside this range are
53*4882a593Smuzhiyun  * converted before doing the actual calculation.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun struct cordic_iq cordic_calc_iq(s32 theta);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #endif /* __CORDIC_H_ */
58