Dedupe extrakey report struct, and send functions in V-USB & LUFA (#7993)
[jackhill/qmk/firmware.git] / tmk_core / common / util.c
CommitLineData
a074364c 1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
b624f32f 21uint8_t bitpop(uint8_t bits) {
a074364c 22 uint8_t c;
b624f32f 23 for (c = 0; bits; c++) bits &= bits - 1;
a074364c 24 return c;
b624f32f 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 */
a074364c 29}
30
b624f32f 31uint8_t bitpop16(uint16_t bits) {
a074364c 32 uint8_t c;
b624f32f 33 for (c = 0; bits; c++) bits &= bits - 1;
a074364c 34 return c;
35}
36
b624f32f 37uint8_t bitpop32(uint32_t bits) {
a074364c 38 uint8_t c;
b624f32f 39 for (c = 0; bits; c++) bits &= bits - 1;
a074364c 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
b624f32f 45uint8_t biton(uint8_t bits) {
a074364c 46 uint8_t n = 0;
b624f32f 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 }
a074364c 59 return n;
60}
61
b624f32f 62uint8_t biton16(uint16_t bits) {
a074364c 63 uint8_t n = 0;
b624f32f 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 }
a074364c 80 return n;
81}
82
b624f32f 83uint8_t biton32(uint32_t bits) {
a074364c 84 uint8_t n = 0;
b624f32f 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 }
a074364c 105 return n;
106}
107
b624f32f 108uint8_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;
a074364c 112 return bits;
113}
114
b624f32f 115uint16_t bitrev16(uint16_t bits) {
116 bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8);
a074364c 117 return bits;
118}
119
b624f32f 120uint32_t bitrev32(uint32_t bits) {
121 bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16);
a074364c 122 return bits;
123}