2016-11-17 03:19:05 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simple bitmask class
|
|
|
|
*/
|
|
|
|
|
2017-05-05 00:13:14 -03:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-02 18:43:08 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-04-16 01:48:16 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
|
|
|
|
2024-07-07 17:16:22 -03:00
|
|
|
template<uint16_t NUMBITS>
|
2016-11-17 03:19:05 -04:00
|
|
|
class Bitmask {
|
2024-07-07 17:16:22 -03:00
|
|
|
static constexpr uint16_t NUMWORDS = ((NUMBITS+31)/32);
|
|
|
|
|
|
|
|
static_assert(NUMBITS > 0, "must store something");
|
2024-07-07 16:58:07 -03:00
|
|
|
// for first_set()'s return value
|
2024-07-07 17:16:22 -03:00
|
|
|
static_assert(NUMBITS <= INT16_MAX, "must fit in int16_t");
|
2024-07-07 16:58:07 -03:00
|
|
|
// so that 1U << bits is in range
|
|
|
|
static_assert(sizeof(unsigned int) >= sizeof(uint32_t), "int too small");
|
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
public:
|
2024-07-07 17:16:22 -03:00
|
|
|
Bitmask() {
|
2017-08-31 13:21:16 -03:00
|
|
|
clearall();
|
2016-11-17 03:19:05 -04:00
|
|
|
}
|
2019-04-11 09:12:26 -03:00
|
|
|
|
|
|
|
Bitmask &operator=(const Bitmask&other) {
|
2024-07-07 17:16:22 -03:00
|
|
|
memcpy(bits, other.bits, sizeof(bits[0])*NUMWORDS);
|
2019-04-11 09:12:26 -03:00
|
|
|
return *this;
|
2016-11-17 03:19:05 -04:00
|
|
|
}
|
|
|
|
|
2022-04-29 08:50:19 -03:00
|
|
|
bool operator==(const Bitmask&other) {
|
2024-07-07 17:16:22 -03:00
|
|
|
return memcmp(bits, other.bits, sizeof(bits[0])*NUMWORDS) == 0;
|
2022-04-29 08:50:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Bitmask&other) {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-11-02 18:43:08 -03:00
|
|
|
Bitmask(const Bitmask &other) = delete;
|
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
// set given bitnumber
|
|
|
|
void set(uint16_t bit) {
|
2024-07-07 16:57:02 -03:00
|
|
|
if (!validate(bit)) {
|
|
|
|
return; // ignore access of invalid bit
|
2017-08-31 13:21:16 -03:00
|
|
|
}
|
2016-11-17 03:19:05 -04:00
|
|
|
uint16_t word = bit/32;
|
|
|
|
uint8_t ofs = bit & 0x1f;
|
|
|
|
bits[word] |= (1U << ofs);
|
|
|
|
}
|
|
|
|
|
2017-08-23 01:57:15 -03:00
|
|
|
// set all bits
|
|
|
|
void setall(void) {
|
2024-07-07 16:50:07 -03:00
|
|
|
// set all words to 111...
|
2024-07-07 17:16:22 -03:00
|
|
|
for (uint16_t i=0; i<NUMWORDS; i++) {
|
2017-08-23 01:57:15 -03:00
|
|
|
bits[i] = 0xffffffff;
|
|
|
|
}
|
2024-07-07 16:50:07 -03:00
|
|
|
// ensure out-of-range bits in the last word, if any exist, are 0
|
2024-07-07 17:16:22 -03:00
|
|
|
uint16_t num_valid_bits = NUMBITS % 32;
|
2024-07-07 16:50:07 -03:00
|
|
|
if (num_valid_bits) { // word has out of range bits
|
2024-07-07 17:16:22 -03:00
|
|
|
bits[NUMWORDS-1] = (1U << num_valid_bits) - 1;
|
2024-07-07 16:50:07 -03:00
|
|
|
}
|
2017-08-23 01:57:15 -03:00
|
|
|
}
|
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
// clear given bitnumber
|
|
|
|
void clear(uint16_t bit) {
|
2024-07-07 16:57:02 -03:00
|
|
|
if (!validate(bit)) {
|
|
|
|
return; // ignore access of invalid bit
|
|
|
|
}
|
2016-11-17 03:19:05 -04:00
|
|
|
uint16_t word = bit/32;
|
|
|
|
uint8_t ofs = bit & 0x1f;
|
|
|
|
bits[word] &= ~(1U << ofs);
|
|
|
|
}
|
|
|
|
|
2022-10-09 21:37:30 -03:00
|
|
|
// set given bitnumber to on/off
|
|
|
|
void setonoff(uint16_t bit, bool onoff) {
|
|
|
|
if (onoff) {
|
|
|
|
set(bit);
|
|
|
|
} else {
|
|
|
|
clear(bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
// clear all bits
|
|
|
|
void clearall(void) {
|
2024-07-07 17:16:22 -03:00
|
|
|
memset(bits, 0, NUMWORDS*sizeof(bits[0]));
|
2016-11-17 03:19:05 -04:00
|
|
|
}
|
2017-08-23 01:58:31 -03:00
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
// return true if given bitnumber is set
|
2017-08-31 02:59:16 -03:00
|
|
|
bool get(uint16_t bit) const {
|
2024-07-07 16:57:02 -03:00
|
|
|
if (!validate(bit)) {
|
|
|
|
return false; // pretend invalid bit is not set
|
|
|
|
}
|
2016-11-17 03:19:05 -04:00
|
|
|
uint16_t word = bit/32;
|
|
|
|
uint8_t ofs = bit & 0x1f;
|
|
|
|
return (bits[word] & (1U << ofs)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if all bits are clear
|
2017-08-31 02:59:16 -03:00
|
|
|
bool empty(void) const {
|
2024-07-07 17:16:22 -03:00
|
|
|
for (uint16_t i=0; i<NUMWORDS; i++) {
|
2016-11-17 03:19:05 -04:00
|
|
|
if (bits[i] != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-23 01:57:15 -03:00
|
|
|
|
|
|
|
// return number of bits set
|
2017-08-31 02:59:16 -03:00
|
|
|
uint16_t count() const {
|
2017-08-23 01:57:15 -03:00
|
|
|
uint16_t sum = 0;
|
2024-07-07 17:16:22 -03:00
|
|
|
for (uint16_t i=0; i<NUMWORDS; i++) {
|
2024-07-07 16:58:07 -03:00
|
|
|
sum += __builtin_popcount(bits[i]);
|
2017-08-23 01:57:15 -03:00
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:19:55 -03:00
|
|
|
// return first bit set, or -1 if none set
|
|
|
|
int16_t first_set() const {
|
2024-07-07 17:16:22 -03:00
|
|
|
for (uint16_t i=0; i<NUMWORDS; i++) {
|
2024-07-07 16:58:07 -03:00
|
|
|
if (bits[i] != 0) {
|
|
|
|
return i*32 + __builtin_ffs(bits[i]) - 1;
|
2018-12-06 18:51:50 -04:00
|
|
|
}
|
2018-08-08 02:19:55 -03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-31 13:21:16 -03:00
|
|
|
// return number of bits available
|
2017-08-31 02:59:16 -03:00
|
|
|
uint16_t size() const {
|
2024-07-07 17:16:22 -03:00
|
|
|
return NUMBITS;
|
2017-08-23 01:57:15 -03:00
|
|
|
}
|
|
|
|
|
2016-11-17 03:19:05 -04:00
|
|
|
private:
|
2024-07-07 16:57:02 -03:00
|
|
|
bool validate(uint16_t bit) const {
|
2024-07-07 17:16:22 -03:00
|
|
|
if (bit >= NUMBITS) {
|
2024-07-07 16:57:02 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::bitmask_range);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-07 17:16:22 -03:00
|
|
|
uint32_t bits[NUMWORDS];
|
2016-11-17 03:19:05 -04:00
|
|
|
};
|