diff options
author | Joel Challis <git@zvecr.com> | 2021-02-07 23:16:15 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-07 23:16:15 +0000 |
commit | 99bffc2a21ebed07fd767ad2a9a7e1aadd491ef3 (patch) | |
tree | 499edfaa4b2a180047f7ca5f6bc882e77f5262fa /quantum/bitwise.c | |
parent | 7e828795534f7351df54d2c0545b2ed159b1bfde (diff) | |
download | qmk_firmware-99bffc2a21ebed07fd767ad2a9a7e1aadd491ef3.tar.gz qmk_firmware-99bffc2a21ebed07fd767ad2a9a7e1aadd491ef3.zip |
Migrate some tmk_core files to quantum (#11791)
* Migrate some tmk_core files to quantum
* Fix build errors
Diffstat (limited to 'quantum/bitwise.c')
-rw-r--r-- | quantum/bitwise.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/quantum/bitwise.c b/quantum/bitwise.c new file mode 100644 index 000000000..861cca005 --- /dev/null +++ b/quantum/bitwise.c | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | #include "util.h" | ||
19 | |||
20 | // bit population - return number of on-bit | ||
21 | __attribute__((noinline)) uint8_t bitpop(uint8_t bits) { | ||
22 | uint8_t c; | ||
23 | for (c = 0; bits; c++) bits &= bits - 1; | ||
24 | return c; | ||
25 | /* | ||
26 | const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; | ||
27 | return bit_count[bits>>4] + bit_count[bits&0x0F] | ||
28 | */ | ||
29 | } | ||
30 | |||
31 | uint8_t bitpop16(uint16_t bits) { | ||
32 | uint8_t c; | ||
33 | for (c = 0; bits; c++) bits &= bits - 1; | ||
34 | return c; | ||
35 | } | ||
36 | |||
37 | uint8_t bitpop32(uint32_t bits) { | ||
38 | uint8_t c; | ||
39 | for (c = 0; bits; c++) bits &= bits - 1; | ||
40 | return c; | ||
41 | } | ||
42 | |||
43 | // most significant on-bit - return highest location of on-bit | ||
44 | // NOTE: return 0 when bit0 is on or all bits are off | ||
45 | __attribute__((noinline)) uint8_t biton(uint8_t bits) { | ||
46 | uint8_t n = 0; | ||
47 | if (bits >> 4) { | ||
48 | bits >>= 4; | ||
49 | n += 4; | ||
50 | } | ||
51 | if (bits >> 2) { | ||
52 | bits >>= 2; | ||
53 | n += 2; | ||
54 | } | ||
55 | if (bits >> 1) { | ||
56 | bits >>= 1; | ||
57 | n += 1; | ||
58 | } | ||
59 | return n; | ||
60 | } | ||
61 | |||
62 | uint8_t biton16(uint16_t bits) { | ||
63 | uint8_t n = 0; | ||
64 | if (bits >> 8) { | ||
65 | bits >>= 8; | ||
66 | n += 8; | ||
67 | } | ||
68 | if (bits >> 4) { | ||
69 | bits >>= 4; | ||
70 | n += 4; | ||
71 | } | ||
72 | if (bits >> 2) { | ||
73 | bits >>= 2; | ||
74 | n += 2; | ||
75 | } | ||
76 | if (bits >> 1) { | ||
77 | bits >>= 1; | ||
78 | n += 1; | ||
79 | } | ||
80 | return n; | ||
81 | } | ||
82 | |||
83 | uint8_t biton32(uint32_t bits) { | ||
84 | uint8_t n = 0; | ||
85 | if (bits >> 16) { | ||
86 | bits >>= 16; | ||
87 | n += 16; | ||
88 | } | ||
89 | if (bits >> 8) { | ||
90 | bits >>= 8; | ||
91 | n += 8; | ||
92 | } | ||
93 | if (bits >> 4) { | ||
94 | bits >>= 4; | ||
95 | n += 4; | ||
96 | } | ||
97 | if (bits >> 2) { | ||
98 | bits >>= 2; | ||
99 | n += 2; | ||
100 | } | ||
101 | if (bits >> 1) { | ||
102 | bits >>= 1; | ||
103 | n += 1; | ||
104 | } | ||
105 | return n; | ||
106 | } | ||
107 | |||
108 | __attribute__((noinline)) uint8_t bitrev(uint8_t bits) { | ||
109 | bits = (bits & 0x0f) << 4 | (bits & 0xf0) >> 4; | ||
110 | bits = (bits & 0b00110011) << 2 | (bits & 0b11001100) >> 2; | ||
111 | bits = (bits & 0b01010101) << 1 | (bits & 0b10101010) >> 1; | ||
112 | return bits; | ||
113 | } | ||
114 | |||
115 | uint16_t bitrev16(uint16_t bits) { | ||
116 | bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8); | ||
117 | return bits; | ||
118 | } | ||
119 | |||
120 | uint32_t bitrev32(uint32_t bits) { | ||
121 | bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16); | ||
122 | return bits; | ||
123 | } | ||