xref: /freebsd/sys/dev/bwn/if_bwn_cordic.h (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*-
2  * Copyright (c) 2016 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	__IF_BWN_CORDIC_H__
32 #define	__IF_BWN_CORDIC_H__
33 
34 /*
35  * These functions are used by the PHY code.
36  */
37 
38 /* Complex number using 2 32-bit signed integers */
39 struct bwn_c32 {
40 	int32_t i;
41 	int32_t q;
42 };
43 
44 #define	CORDIC_CONVERT(value)	(((value) >= 0) ?	\
45 	    ((((value) >> 15) + 1) >> 1) :		\
46 	    -((((-(value)) >> 15) + 1) >> 1))
47 
48 static const uint32_t bwn_arctg[] = {
49     2949120, 1740967, 919879, 466945, 234379, 117304, 58666, 29335, 14668,
50     7334, 3667, 1833, 917, 458, 229, 115, 57, 29,
51 };
52 
53 /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
54 static inline struct bwn_c32
55 bwn_cordic(int theta)
56 {
57 	uint8_t i;
58 	int32_t tmp;
59 	int8_t signx = 1;
60 	uint32_t angle = 0;
61 	struct bwn_c32 ret = { .i = 39797, .q = 0, };
62 
63 	while (theta > (180 << 16))
64 		theta -= (360 << 16);
65 	while (theta < -(180 << 16))
66 		theta += (360 << 16);
67 
68 	if (theta > (90 << 16)) {
69 		theta -= (180 << 16);
70 		signx = -1;
71 	} else if (theta < -(90 << 16)) {
72 		theta += (180 << 16);
73 		signx = -1;
74 	}
75 
76 	for (i = 0; i <= 17; i++) {
77 		if (theta > angle) {
78 			tmp = ret.i - (ret.q >> i);
79 			ret.q += ret.i >> i;
80 			ret.i = tmp;
81 			angle += bwn_arctg[i];
82 		} else {
83 			tmp = ret.i + (ret.q >> i);
84 			ret.q -= ret.i >> i;
85 			ret.i = tmp;
86 			angle -= bwn_arctg[i];
87 		}
88 	}
89 
90 	ret.i *= signx;
91 	ret.q *= signx;
92 
93 	return ret;
94 }
95 
96 #endif	/* __IF_BWN_CORDIC_H__ */
97