xref: /linux/include/linux/cordic.h (revision 58d81d64)
110f8113eSArend van Spriel /*
210f8113eSArend van Spriel  * Copyright (c) 2011 Broadcom Corporation
310f8113eSArend van Spriel  *
410f8113eSArend van Spriel  * Permission to use, copy, modify, and/or distribute this software for any
510f8113eSArend van Spriel  * purpose with or without fee is hereby granted, provided that the above
610f8113eSArend van Spriel  * copyright notice and this permission notice appear in all copies.
710f8113eSArend van Spriel  *
810f8113eSArend van Spriel  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
910f8113eSArend van Spriel  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1010f8113eSArend van Spriel  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1110f8113eSArend van Spriel  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1210f8113eSArend van Spriel  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1310f8113eSArend van Spriel  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1410f8113eSArend van Spriel  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1510f8113eSArend van Spriel  */
1610f8113eSArend van Spriel #ifndef __CORDIC_H_
1710f8113eSArend van Spriel #define __CORDIC_H_
1810f8113eSArend van Spriel 
1910f8113eSArend van Spriel #include <linux/types.h>
2010f8113eSArend van Spriel 
21*58d81d64SPriit Laes #define CORDIC_ANGLE_GEN	39797
22*58d81d64SPriit Laes #define CORDIC_PRECISION_SHIFT	16
23*58d81d64SPriit Laes #define CORDIC_NUM_ITER	(CORDIC_PRECISION_SHIFT + 2)
24*58d81d64SPriit Laes 
25*58d81d64SPriit Laes #define CORDIC_FIXED(X)	((s32)((X) << CORDIC_PRECISION_SHIFT))
26*58d81d64SPriit Laes #define CORDIC_FLOAT(X)	(((X) >= 0) \
27*58d81d64SPriit Laes 		? ((((X) >> (CORDIC_PRECISION_SHIFT - 1)) + 1) >> 1) \
28*58d81d64SPriit Laes 		: -((((-(X)) >> (CORDIC_PRECISION_SHIFT - 1)) + 1) >> 1))
29*58d81d64SPriit Laes 
3010f8113eSArend van Spriel /**
3110f8113eSArend van Spriel  * struct cordic_iq - i/q coordinate.
3210f8113eSArend van Spriel  *
3310f8113eSArend van Spriel  * @i: real part of coordinate (in phase).
3410f8113eSArend van Spriel  * @q: imaginary part of coordinate (quadrature).
3510f8113eSArend van Spriel  */
3610f8113eSArend van Spriel struct cordic_iq {
3710f8113eSArend van Spriel 	s32 i;
3810f8113eSArend van Spriel 	s32 q;
3910f8113eSArend van Spriel };
4010f8113eSArend van Spriel 
4110f8113eSArend van Spriel /**
4210f8113eSArend van Spriel  * cordic_calc_iq() - calculates the i/q coordinate for given angle.
4310f8113eSArend van Spriel  *
4410f8113eSArend van Spriel  * @theta: angle in degrees for which i/q coordinate is to be calculated.
4510f8113eSArend van Spriel  * @coord: function output parameter holding the i/q coordinate.
4610f8113eSArend van Spriel  *
47c6208469SMichael Witten  * The function calculates the i/q coordinate for a given angle using the
480314322dSMichael Witten  * CORDIC algorithm. The coordinate consists of a real (i) and an
4910f8113eSArend van Spriel  * imaginary (q) part. The real part is essentially the cosine of the
5010f8113eSArend van Spriel  * angle and the imaginary part is the sine of the angle. The returned
5110f8113eSArend van Spriel  * values are scaled by 2^16 for precision. The range for theta is
5210f8113eSArend van Spriel  * for -180 degrees to +180 degrees. Passed values outside this range are
5310f8113eSArend van Spriel  * converted before doing the actual calculation.
5410f8113eSArend van Spriel  */
5510f8113eSArend van Spriel struct cordic_iq cordic_calc_iq(s32 theta);
5610f8113eSArend van Spriel 
5710f8113eSArend van Spriel #endif /* __CORDIC_H_ */
58