~obeancomputer/bitter

fa43358340a7b2b222673352b3c1c1d8db12de26 — ocsmit 1 year, 2 months ago 6eb7185
formating
1 files changed, 18 insertions(+), 3 deletions(-)

M src/bitops.c
M src/bitops.c => src/bitops.c +18 -3
@@ 11,11 11,14 @@ extern inline unsigned int sig_bit_idx(unsigned int j, unsigned int word_size)
  return ((j) % word_size) + 1;
}

// -- Single bit ops ----------------------------------------------------------

unsigned int BitArray_bitread(BitArray* bit_arr, unsigned int j) {
  // TODO: Add range checking
  return (bit_arr->v[j/bit_arr->width] >> (j % bit_arr->width)) & 1;
}

  
void BitArray_bitset(BitArray* bit_arr, unsigned int j)
{
  // Shift word left to bit idx, OR w/ 1


@@ 28,7 31,12 @@ void BitArray_bitclear(BitArray* bit_arr, unsigned int j)
  bit_arr->v[j/bit_arr->width] &= ~(1 << (j % bit_arr->width));
}

unsigned int BitArray_bitsread(BitArray* bit_arr, unsigned int j1, unsigned int j)

// -- Reading -----------------------------------------------------------------

unsigned int BitArray_bitsread(
  BitArray* bit_arr, unsigned int j1, unsigned int j
)
{
  if (j1 > j) return 0; // Early return if start idx > end idx



@@ 55,6 63,7 @@ unsigned int BitArray_bitsread(BitArray* bit_arr, unsigned int j1, unsigned int 
  );
}


unsigned int BitArray_read(BitArray* bit_arr, unsigned int i)
{
  if (i >= bit_arr->n) {


@@ 64,8 73,11 @@ unsigned int BitArray_read(BitArray* bit_arr, unsigned int i)
  return BitArray_bitsread(bit_arr, i*bit_arr->l, (i+1)*bit_arr->l-1); 
}

// -- Writing -----------------------------------------------------------------

void BitArray_bitswrite(BitArray* bit_arr, unsigned int j1, unsigned int j, unsigned int x)
void BitArray_bitswrite(
  BitArray* bit_arr, unsigned int j1, unsigned int j, unsigned int x
)
{
  if (j1 > j) return; // Early return if start idx > end idx
  unsigned int w = bit_arr->width;


@@ 73,7 85,9 @@ void BitArray_bitswrite(BitArray* bit_arr, unsigned int j1, unsigned int j, unsi
  // Confined w/in single word
  if (j1 / bit_arr->width == j / bit_arr->width) { 
    // Clear bits
    bit_arr->v[j/bit_arr->width] &= ~((unsigned) ((1 << (j-j1+1)) - 1) << (j1 % bit_arr->width));
    bit_arr->v[j/bit_arr->width] &= (
      ~((unsigned) ((1 << (j-j1+1)) - 1) << (j1 % bit_arr->width))
    );
    // Write x bits
    bit_arr->v[j/bit_arr->width] |= x << (j1 % bit_arr->width); 
  } else {  


@@ 94,6 108,7 @@ void BitArray_bitswrite(BitArray* bit_arr, unsigned int j1, unsigned int j, unsi
  }
}


void BitArray_write(BitArray* bit_arr, unsigned int i, unsigned int x)
{
  if (i >= bit_arr->n) {