xref: /freebsd/sys/dev/bwn/if_bwn_cordic.h (revision 06c3fb27)
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 #ifndef	__IF_BWN_CORDIC_H__
30 #define	__IF_BWN_CORDIC_H__
31 
32 /*
33  * These functions are used by the PHY code.
34  */
35 
36 /* Complex number using 2 32-bit signed integers */
37 struct bwn_c32 {
38 	int32_t i;
39 	int32_t q;
40 };
41 
42 #define	CORDIC_CONVERT(value)	(((value) >= 0) ?	\
43 	    ((((value) >> 15) + 1) >> 1) :		\
44 	    -((((-(value)) >> 15) + 1) >> 1))
45 
46 static const uint32_t bwn_arctg[] = {
47     2949120, 1740967, 919879, 466945, 234379, 117304, 58666, 29335, 14668,
48     7334, 3667, 1833, 917, 458, 229, 115, 57, 29,
49 };
50 
51 /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
52 static inline struct bwn_c32
53 bwn_cordic(int theta)
54 {
55 	uint8_t i;
56 	int32_t tmp;
57 	int8_t signx = 1;
58 	uint32_t angle = 0;
59 	struct bwn_c32 ret = { .i = 39797, .q = 0, };
60 
61 	while (theta > (180 << 16))
62 		theta -= (360 << 16);
63 	while (theta < -(180 << 16))
64 		theta += (360 << 16);
65 
66 	if (theta > (90 << 16)) {
67 		theta -= (180 << 16);
68 		signx = -1;
69 	} else if (theta < -(90 << 16)) {
70 		theta += (180 << 16);
71 		signx = -1;
72 	}
73 
74 	for (i = 0; i <= 17; i++) {
75 		if (theta > angle) {
76 			tmp = ret.i - (ret.q >> i);
77 			ret.q += ret.i >> i;
78 			ret.i = tmp;
79 			angle += bwn_arctg[i];
80 		} else {
81 			tmp = ret.i + (ret.q >> i);
82 			ret.q -= ret.i >> i;
83 			ret.i = tmp;
84 			angle -= bwn_arctg[i];
85 		}
86 	}
87 
88 	ret.i *= signx;
89 	ret.q *= signx;
90 
91 	return ret;
92 }
93 
94 #endif	/* __IF_BWN_CORDIC_H__ */
95