~obeancomputer/bitter

c36a53dd95fc6806305b6752326665b9bab7d63c — ocsmit 1 year, 2 months ago 239a41d
size -> width
3 files changed, 26 insertions(+), 25 deletions(-)

M src/bitarr.c
M src/bitarr.h
M src/bitops.c
M src/bitarr.c => src/bitarr.c +1 -1
@@ 11,7 11,7 @@ BitArray* BitArray_calloc(unsigned int n_entries, unsigned int l)
    }
    // Set values
    bitarr->l = l;
    bitarr->size = WORD_SIZE;
    bitarr->width = WORD_SIZE;
    
    return bitarr;
}

M src/bitarr.h => src/bitarr.h +7 -6
@@ 84,16 84,16 @@
 *
 * @var BitArray.l 
 *  number of bits to store each value in A
 * @var BitArray.size
 *  size of each member in v
 * @var BitArray.width
 *  number of bits of each member in v (e.g. 32)
 * @var BitArray.v[]
 *  compressed version of A (v)
 *
 */
typedef struct {
  unsigned int l;       // number of bits to store each value in A
  unsigned int size;    // size of each member in v
  unsigned int v[];   // compressed version of A (v)
  unsigned int l;       
  unsigned int width;   
  unsigned int length;
  unsigned int v[];   
} BitArray;




@@ 123,4 123,5 @@ void BitArray_free(BitArray *bitarr);
 */
BitArray* BitArray_init(unsigned int A[], unsigned int length, unsigned int l);


#endif // BITARR_H_

M src/bitops.c => src/bitops.c +18 -18
@@ 13,19 13,19 @@ extern inline unsigned int sig_bit_idx(unsigned int j, unsigned int word_size)

unsigned int BitArray_bitread(BitArray* bit_arr, unsigned int j) {
  // TODO: Add range checking
  return (bit_arr->v[j/bit_arr->size] >> (j % bit_arr->size)) & 1;
  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
  bit_arr->v[j/bit_arr->size] |= 1 << (j % bit_arr->size);
  bit_arr->v[j/bit_arr->width] |= 1 << (j % bit_arr->width);
}

void BitArray_bitclear(BitArray* bit_arr, unsigned int j)
{
  // Shift word left to bit idx, AND w/ NOT(1)
  bit_arr->v[j/bit_arr->size] &= ~(1 << (j % bit_arr->size));
  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)


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

  // Confined w/in single word
  if (j1 / bit_arr->size == j / bit_arr->size) { 
  if (j1 / bit_arr->width == j / bit_arr->width) { 
    return ( 
      // Shift word right
      (bit_arr->v[j/bit_arr->size] >> (j1 % bit_arr->size)) & 
      (bit_arr->v[j/bit_arr->width] >> (j1 % bit_arr->width)) & 
      // AND on bit vector of 1s the necessary length to extract only needed 
      // bits 
      ((1 << (j-j1+1)) - 1)


@@ 46,12 46,12 @@ unsigned int BitArray_bitsread(BitArray* bit_arr, unsigned int j1, unsigned int 
  // Spans two words
  return (
  // Get bits in the first word
  (bit_arr->v[j1/bit_arr->size] >> (j1 % bit_arr->size)) | 
  (bit_arr->v[j1/bit_arr->width] >> (j1 % bit_arr->width)) | 
  // Bits in second word
  (bit_arr->v[j/bit_arr->size] &  ((1 << ((j+1) % bit_arr->size)) - 1)) <<
  (bit_arr->v[j/bit_arr->width] &  ((1 << ((j+1) % bit_arr->width)) - 1)) <<
    // Shift bits from second word n bits from first word left to make 
    // room for concatenation
    (bit_arr->size - (j1 % bit_arr->size))
    (bit_arr->width - (j1 % bit_arr->width))
  );
}



@@ 64,26 64,26 @@ unsigned int BitArray_read(BitArray* bit_arr, unsigned int i)
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->size;
  unsigned int w = bit_arr->width;

  // Confined w/in single word
  if (j1 / bit_arr->size == j / bit_arr->size) { 
  if (j1 / bit_arr->width == j / bit_arr->width) { 
    // Clear bits
    bit_arr->v[j/bit_arr->size] &= ~((unsigned) ((1 << (j-j1+1)) - 1) << (j1 % bit_arr->size));
    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->size] |= x << (j1 % bit_arr->size); 
  } else {
    bit_arr->v[j/bit_arr->width] |= x << (j1 % bit_arr->width); 
  } else {  
    // Spans two words
    bit_arr->v[j1/bit_arr->size] = (
    bit_arr->v[j1/bit_arr->width] = (
      // Get bits in first word to store lower bits
      (bit_arr->v[j1/bit_arr->size] & ((1 << (j1 % bit_arr->size)) - 1)) | 
      (bit_arr->v[j1/bit_arr->width] & ((1 << (j1 % bit_arr->width)) - 1)) | 
      // Write bits 
      (x <<  (j1 % bit_arr->size))
      (x <<  (j1 % bit_arr->width))
    );
    //
    bit_arr->v[j/bit_arr->size] = (
    bit_arr->v[j/bit_arr->width] = (
      // Get bits in second word to store lower bits
      (bit_arr->v[j/bit_arr->size] & ~((1 << ((j+1) % w)) - 1)) | 
      (bit_arr->v[j/bit_arr->width] & ~((1 << ((j+1) % w)) - 1)) | 
      // Write bits 
      (x >> (w - (j1 % w)))
    );