mirror of
https://github.com/iNavFlight/inav.git
synced 2025-07-26 01:35:35 +03:00
Add bitarray functions for setting and clearing all the array
- bitArraySetAll() and bitArrayClrAll() set and clear the whole array, with the size (in bytes) specified by the caller (to make them work like bitArrayFindFirstSet()) - macros BITARRAY_SET_ALL() and BITARRAY_CLR_ALL() call those two new functions using sizeof(array) as its size
This commit is contained in:
parent
9af8bd1236
commit
fe4c215886
3 changed files with 33 additions and 0 deletions
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "bitarray.h"
|
#include "bitarray.h"
|
||||||
|
|
||||||
|
@ -37,6 +38,16 @@ void bitArrayClr(bitarrayElement_t *array, unsigned bit)
|
||||||
BITARRAY_BIT_OP((uint32_t*)array, bit, &=~);
|
BITARRAY_BIT_OP((uint32_t*)array, bit, &=~);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void bitArraySetAll(bitarrayElement_t *array, size_t size)
|
||||||
|
{
|
||||||
|
memset(array, 0xFF, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bitArrayClrAll(bitarrayElement_t *array, size_t size)
|
||||||
|
{
|
||||||
|
memset(array, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
__attribute__((always_inline)) static inline uint8_t __CTZ(uint32_t val)
|
__attribute__((always_inline)) static inline uint8_t __CTZ(uint32_t val)
|
||||||
{
|
{
|
||||||
// __builtin_ctz is not defined for zero, since it's arch
|
// __builtin_ctz is not defined for zero, since it's arch
|
||||||
|
|
|
@ -34,6 +34,12 @@ bool bitArrayGet(const bitarrayElement_t *array, unsigned bit);
|
||||||
void bitArraySet(bitarrayElement_t *array, unsigned bit);
|
void bitArraySet(bitarrayElement_t *array, unsigned bit);
|
||||||
void bitArrayClr(bitarrayElement_t *array, unsigned bit);
|
void bitArrayClr(bitarrayElement_t *array, unsigned bit);
|
||||||
|
|
||||||
|
void bitArraySetAll(bitarrayElement_t *array, size_t size);
|
||||||
|
void bitArrayClrAll(bitarrayElement_t *array, size_t size);
|
||||||
|
|
||||||
|
#define BITARRAY_SET_ALL(array) bitArraySetAll(array, sizeof(array))
|
||||||
|
#define BITARRAY_CLR_ALL(array) bitArrayClrAll(array, sizeof(array))
|
||||||
|
|
||||||
// Returns the first set bit with pos >= start_bit, or -1 if all bits
|
// Returns the first set bit with pos >= start_bit, or -1 if all bits
|
||||||
// are zero. Note that size must indicate the size of array in bytes.
|
// are zero. Note that size must indicate the size of array in bytes.
|
||||||
// In most cases, you should use the BITARRAY_FIND_FIRST_SET() macro
|
// In most cases, you should use the BITARRAY_FIND_FIRST_SET() macro
|
||||||
|
|
|
@ -67,3 +67,19 @@ TEST(BitArrayTest, TestFind)
|
||||||
EXPECT_EQ(bitArrayFindFirstSet(p, 0, sizeof(p)), -1);
|
EXPECT_EQ(bitArrayFindFirstSet(p, 0, sizeof(p)), -1);
|
||||||
EXPECT_EQ(bitArrayFindFirstSet(p, 64, sizeof(p)), -1);
|
EXPECT_EQ(bitArrayFindFirstSet(p, 64, sizeof(p)), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(BitArrayTest, TestSetClrAll)
|
||||||
|
{
|
||||||
|
const int bits = 32 * 4;
|
||||||
|
|
||||||
|
BITARRAY_DECLARE(p, bits);
|
||||||
|
BITARRAY_CLR_ALL(p);
|
||||||
|
|
||||||
|
EXPECT_EQ(-1, BITARRAY_FIND_FIRST_SET(p, 0));
|
||||||
|
|
||||||
|
BITARRAY_SET_ALL(p);
|
||||||
|
|
||||||
|
for (int ii = 0; ii < bits; ii++) {
|
||||||
|
EXPECT_EQ(ii, BITARRAY_FIND_FIRST_SET(p, ii));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue