Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
tbm-mcu/source/bitops.c

65 lines
1.0 KiB

#include <stdint.h>
#include <bitops.h>
#include <macros.h>
#define BIT_WORD(n) ((n) / BIT_SIZE(unsigned long))
#define BIT_MASK(n) (1UL << ((n) % BIT_SIZE(unsigned long)))
unsigned ilog2(unsigned n)
{
unsigned ret = 0;
while (n >>= 1)
++ret;
return ret;
}
int is_bit_set(void *base, size_t n)
{
unsigned long *word = (unsigned long *)base + BIT_WORD(n);
unsigned long mask = BIT_MASK(n);
return (*word & mask) ? 1 : 0;
}
void set_bit(void *base, size_t n)
{
unsigned long *word = (unsigned long *)base + BIT_WORD(n);
unsigned long mask = BIT_MASK(n);
*word |= mask;
}
void clear_bit(void *base, size_t n)
{
unsigned long *word = (unsigned long *)base + BIT_WORD(n);
unsigned long mask = BIT_MASK(n);
*word &= ~mask;
}
size_t next_set_bit(void *base, size_t n)
{
size_t i;
for (i = 0; i < n; ++i) {
if (is_bit_set(base, i))
return i;
}
return SIZE_MAX;
}
size_t next_clear_bit(void *base, size_t n)
{
size_t i;
for (i = 0; i < n; ++i) {
if (!is_bit_set(base, i))
return i;
}
return SIZE_MAX;
}