mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-24 05:53:49 +01:00
Merge pull request #17710 from benpicco/sys/bitfield-ops
sys/bitfield: add support for bit-wise bitfield operations
This commit is contained in:
commit
60fb36eb1e
@ -21,11 +21,11 @@
|
||||
#include "bitfield.h"
|
||||
#include "irq.h"
|
||||
|
||||
int bf_get_unset(uint8_t field[], int size)
|
||||
int bf_get_unset(uint8_t field[], size_t size)
|
||||
{
|
||||
int result = -1;
|
||||
int nbytes = (size + 7) / 8;
|
||||
int i = 0;
|
||||
size_t i = 0;
|
||||
|
||||
unsigned state = irq_disable();
|
||||
|
||||
@ -43,5 +43,5 @@ int bf_get_unset(uint8_t field[], int size)
|
||||
}
|
||||
|
||||
irq_restore(state);
|
||||
return(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -89,18 +89,105 @@ static inline bool bf_isset(uint8_t field[], size_t idx)
|
||||
return (field[idx / 8] & (1u << (7 - (idx % 8))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise OR operation on two bitfields
|
||||
* `out = a | b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_or(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
|
||||
{
|
||||
len = (len + 7) / 8;
|
||||
while (len--) {
|
||||
out[len] = a[len] | b[len];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise AND operation on two bitfields
|
||||
* `out = a & b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_and(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
|
||||
{
|
||||
len = (len + 7) / 8;
|
||||
while (len--) {
|
||||
out[len] = a[len] & b[len];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise XOR operation on two bitfields
|
||||
* `out = a ^ b`
|
||||
*
|
||||
* @pre The size of @p a, @p b and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The first bitfield
|
||||
* @param[in] b The second bitfield
|
||||
* @param[in] len The number of bits in the bitfields
|
||||
*/
|
||||
static inline void bf_xor(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
|
||||
{
|
||||
len = (len + 7) / 8;
|
||||
while (len--) {
|
||||
out[len] = a[len] ^ b[len];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform a bitwise NOT operation on a bitfield
|
||||
* `out = ~a`
|
||||
*
|
||||
* @pre The size of @p a and @p out must be at least @p len bits
|
||||
*
|
||||
* @note This operation will also affect unused bits of the bytes that make up
|
||||
* the bitfield.
|
||||
*
|
||||
* @param[out] out The resulting bitfield
|
||||
* @param[in] a The bitfield to invert
|
||||
* @param[in] len The number of bits in the bitfield
|
||||
*/
|
||||
static inline void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
|
||||
{
|
||||
len = (len + 7) / 8;
|
||||
while (len--) {
|
||||
out[len] = ~a[len];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically get the number of an unset bit and set it
|
||||
*
|
||||
* This function can be used to record e.g., empty entries in an array.
|
||||
*
|
||||
* @param[in,out] field The bitfield
|
||||
* @param[in] size The size of the bitfield
|
||||
* @param[in] len The number of bits in the bitfield to consider
|
||||
*
|
||||
* @return number of bit that was set
|
||||
* @return -1 if no bit was unset
|
||||
*/
|
||||
int bf_get_unset(uint8_t field[], int size);
|
||||
int bf_get_unset(uint8_t field[], size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -197,6 +197,31 @@ static void test_bf_get_unset_lastbyte(void)
|
||||
TEST_ASSERT_EQUAL_INT(39, res);
|
||||
}
|
||||
|
||||
static void test_bf_ops(void)
|
||||
{
|
||||
const uint8_t zero[3] = {0};
|
||||
const uint8_t set[3] = { 0xFF, 0xFF, 0xFF };
|
||||
|
||||
uint8_t a[3] = { 0xAA, 0x55, 0xF0 };
|
||||
uint8_t b[3] = { 0x55, 0xAA, 0x0F };
|
||||
uint8_t c[3];
|
||||
|
||||
bf_or(c, a, b, 24);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(c, set, sizeof(c)));
|
||||
|
||||
bf_inv(c, c, 24);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(c, zero, sizeof(c)));
|
||||
|
||||
bf_and(c, a, b, 24);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(c, zero, sizeof(c)));
|
||||
|
||||
bf_inv(c, c, 24);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(c, set, sizeof(c)));
|
||||
|
||||
bf_xor(c, c, a, 24);
|
||||
TEST_ASSERT_EQUAL_INT(0, memcmp(c, b, sizeof(c)));
|
||||
}
|
||||
|
||||
Test *tests_bitfield_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
@ -208,6 +233,7 @@ Test *tests_bitfield_tests(void)
|
||||
new_TestFixture(test_bf_get_unset_firstbyte),
|
||||
new_TestFixture(test_bf_get_unset_middle),
|
||||
new_TestFixture(test_bf_get_unset_lastbyte),
|
||||
new_TestFixture(test_bf_ops),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(bitfield_tests, NULL, NULL, fixtures);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user