Esempio n. 1
0
static void test_bv_xor(TestCase *tc, void *data)
{
#   define XOR_SIZE 1000
    static const int xor_cnt = 500;
    BitVector *xor_bv;
    BitVector *bv1 = bv_new();
    BitVector *bv2 = bv_new();
    char set[XOR_SIZE];
    char set1[XOR_SIZE];
    char set2[XOR_SIZE];
    int i;
    int count = 0;
    (void)data;

    memset(set, 0, XOR_SIZE);
    memset(set1, 0, XOR_SIZE);
    memset(set2, 0, XOR_SIZE);
    for (i = 0; i < xor_cnt; i++) {
        int bit = rand() % XOR_SIZE;
        bv_set(bv1, bit);
        set1[bit] = 1;
    }
    for (i = 0; i < xor_cnt; i++) {
        int bit = rand() % XOR_SIZE;
        bv_set(bv2, bit);
        set2[bit] = 1;
    }
    for (i = 0; i < XOR_SIZE; i++) {
        if (set1[i] != set2[i]) {
            set[i] = 1;
            count++;
        }
    }

    xor_bv = bv_xor(bv1, bv2);

    Aiequal(count, xor_bv->count);
    for (i = 0; i < XOR_SIZE; i++) {
        if (!Aiequal(set[i], bv_get(xor_bv, i))) {
            Tmsg("At position %d, bv1 is %d and bv2 is %d\n", i,
                 bv_get(bv1, i), bv_get(bv2, i));
        }
    }

    bv1 = bv_xor_x(bv1, bv2);
    Assert(bv_eq(bv1, xor_bv), "BitVectors should be equal");

    bv_destroy(bv2);
    bv_destroy(xor_bv);

    bv2 = bv_new();
    xor_bv = bv_xor(bv1, bv2);

    Assert(bv_eq(bv1, xor_bv), "XORed BitVector equals bv1");

    bv_destroy(bv1);
    bv_destroy(bv2);
    bv_destroy(xor_bv);
}
Esempio n. 2
0
static void test_bv_or(TestCase *tc, void *data)
{
#   define OR_SIZE 1000
    static const int or_cnt = 500;
    BitVector *or_bv;
    BitVector *bv1 = bv_new();
    BitVector *bv2 = bv_new();
    char set[OR_SIZE];
    int i;
    int count = 0;
    (void)data;

    memset(set, 0, OR_SIZE);
    for (i = 0; i < or_cnt; i++) {
        int bit = rand() % OR_SIZE;
        bv_set(bv1, bit);
        if (0 == set[bit]) {
            count++;
            set[bit] = 1;
        }
    }
    for (i = 0; i < or_cnt; i++) {
        int bit = rand() % OR_SIZE;
        bv_set(bv2, bit);
        if (0 == set[bit]) {
            count++;
            set[bit] = 1;
        }
    }

    or_bv = bv_or(bv1, bv2);

    Aiequal(count, or_bv->count);
    for (i = 0; i < OR_SIZE; i++) {
        Aiequal(set[i], bv_get(or_bv, i));
    }

    bv1 = bv_or_x(bv1, bv2);
    Assert(bv_eq(bv1, or_bv), "BitVectors should be equal");

    bv_destroy(bv2);
    bv_destroy(or_bv);

    bv2 = bv_new();
    or_bv = bv_or(bv1, bv2);

    Assert(bv_eq(bv1, or_bv), "ORed BitVector equals bv1");

    bv_destroy(bv1);
    bv_destroy(bv2);
    bv_destroy(or_bv);
}
Esempio n. 3
0
static void test_bv_eq_hash(TestCase *tc, void *data)
{
    static int const COUNT = 1000;
    int i;
    BitVector *bv1 = bv_new();
    BitVector *bv2 = bv_new();
    (void)data;

    test_bveq(bv1, bv2);
    Assert(bv_eq(bv1, bv1), "bv_eq on self should work");

    bv_set(bv1, 1);
    test_bvneq(bv1, bv2);

    bv_set(bv2, 11);
    test_bvneq(bv1, bv2);

    bv_set(bv1, 11);
    bv_set(bv2, 1);
    test_bveq(bv1, bv2);

    /* This will increase size of bv1 to 1000 */
    bv_unset(bv1, 1000); 
    /* difference in size shouldn't matter */
    test_bveq(bv1, bv2);

    for (i = 0; i < COUNT; i++) {
        int bit = rand() % COUNT;
        bv_set(bv1, bit);
        bv_set(bv2, bit);
    }
    test_bveq(bv1, bv2);

    bv_destroy(bv1);
    bv_destroy(bv2);

    /* although the saet bits will be equal, the extension will be different*/
    bv1 = set_bits(bv_new(), "1, 3, 5");
    bv2 = bv_not_x(set_bits(bv_new(), "0, 2, 4"));
    bv_set(bv2, 5);
    test_bvneq(bv1, bv2);

    bv_destroy(bv2);
    bv2 = set_bits(bv_new(), "1, 3, 5");
    bv1 = bv_not_x(bv1);
    bv2 = bv_not_x(bv2);
    bv_unset(bv1, 1000);
    test_bvneq(bv1, bv2);

    bv_destroy(bv1);
    bv_destroy(bv2);
}
Esempio n. 4
0
static void scc_mark (void) {
     unsigned int i;
     i_bb_t b;
     for (b = i_fg_root; b; b = b->lnext)
	  b->color = WHITE;
     scn = 0;
     NEW(i_sccs, num_bb+1);
     for (i = dfn; i <= num_bb; i++)
	  if (nodes[i]->color == WHITE) {
	       ++scn;
	       if (num_i) i_sccs[scn].iv = bv_new(num_i);
	       if (num_f) i_sccs[scn].fv = bv_new(num_f);
	       scc_visit(nodes[i]);
	  }
}
Esempio n. 5
0
static VALUE
frt_bv_alloc(VALUE klass)
{
    BitVector *bv = bv_new();
    VALUE rbv = Data_Wrap_Struct(klass, NULL, &frt_bv_free, bv);
    object_add(bv, rbv);
    return rbv;
}
Esempio n. 6
0
/*******************************************************************************
wl_new

IN:     int size    size of worklist

RETURN: worklist *  new worklist
*******************************************************************************/
worklist *wl_new(int size) {
	worklist *w;

	w = DNEW(worklist);
	w->W_stack = DMNEW(int, size);
	w->W_top = 0;
	w->W_bv = bv_new(size);
#ifdef WL_DEBUG_CHECK
	w->size = size;
#endif
	return w;
}
Esempio n. 7
0
static void test_bv_combined_boolean_ops(TestCase *tc, void *data)
{
    BitVector *bv1 = bv_new();
    BitVector *bv2 = bv_new();
    BitVector *bv3;
    BitVector *bv4;
    BitVector *bv5;
    BitVector *bv_empty = bv_new();
    (void)data;

    set_bits(bv1, "1, 5, 7");
    set_bits(bv2, "1, 8, 20");

    bv3 = bv_not(bv1);
    Aiequal(bv3->size, bv1->size);

    bv4 = bv_and(bv1, bv3);
    Assert(bv_eq(bv4, bv_empty), "bv & ~bv == empty BitVector");
    bv_destroy(bv4);

    bv4 = bv_and(bv2, bv3);
    bv5 = set_bits(bv_new(), "8, 20");
    Assert(bv_eq(bv4, bv5), "~[1,5,7] & [1,8,20] == [8,20]");
    bv_destroy(bv4);
    bv_destroy(bv5);

    bv4 = bv_or(bv1, bv3);
    bv5 = bv_not(bv_empty);
    Assert(bv_eq(bv4, bv5), "bv | ~bv == all 1s");
    bv_destroy(bv4);
    bv_destroy(bv5);

    bv4 = bv_or(bv2, bv3);
    bv5 = bv_not_x(set_bits(bv_new(), "5, 7"));
    Assert(bv_eq(bv4, bv5), "~[1,5,7] | [1,8,20] == ~[5, 7]");
    bv_destroy(bv4);
    bv_destroy(bv5);

    bv4 = bv_xor(bv1, bv3);
    bv5 = bv_not(bv_empty);
    Assert(bv_eq(bv4, bv5), "bv ^ ~bv == full BitVector");
    bv_destroy(bv4);
    bv_destroy(bv5);

    bv4 = bv_xor(bv2, bv3);
    bv5 = bv_not_x(set_bits(bv_new(), "5, 7, 8, 20"));
    Assert(bv_eq(bv4, bv5), "~[1,5,7] ^ [1,8,20] == ~[5, 7, 8, 20]");
    bv_destroy(bv4);
    bv_destroy(bv5);

    bv_destroy(bv1);
    bv_destroy(bv2);
    bv_destroy(bv3);
    bv_destroy(bv_empty);
}
Esempio n. 8
0
static void test_bv_not(TestCase *tc, void *data)
{
    BitVector *bv = bv_new(), *not_bv;
    int i;
    (void)data;
    set_bits(bv, "1, 5, 25, 41, 97, 185");
    Aiequal(186, bv->size);

    not_bv = bv_not(bv);
    Aiequal(bv->count, not_bv->count);
    for (i = 0; i < 300; i++) {
        Aiequal(1 - bv_get(bv, i), bv_get(not_bv, i));
    }

    bv_not_x(bv);
    Assert(bv_eq(bv, not_bv), "BitVectors equal");

    bv_destroy(bv);
    bv_destroy(not_bv);
}
Esempio n. 9
0
/**
 * Test simple BitVector scanning
 */
static void test_bv_scan(TestCase *tc, void *data)
{
    int i;
    BitVector *bv = bv_new();
    BitVector *not_bv;
    (void)data; /* suppress unused argument warning */

    for (i = 6; i <= 10; i++) {
        bv_set(bv, i);
    }
    not_bv = bv_not(bv);
    for (i = 6; i <= 10; i++) {
        Aiequal(i, bv_scan_next(bv));
        Aiequal(i, bv_scan_next_unset(not_bv));
    }
    Aiequal(-1, bv_scan_next(bv));
    Aiequal(-1, bv_scan_next_unset(bv));
    bv_destroy(bv);
    bv_destroy(not_bv);
}
Esempio n. 10
0
File: cfg.c Progetto: berkus/lang-e
/* bb_alloc: return a new, empty basic block */
static inline i_bb_t bb_alloc (void) {
     i_bb_t b;
     num_bb++;
     NEW0(b, 1);
     if (num_i) {
	  b->iv_def = bv_new(num_i);
	  b->iv_use = bv_new(num_i);
	  b->ilv_out = bv_new(num_i);
     }
     if (num_f) {
	  b->fv_def = bv_new(num_f);
	  b->fv_use = bv_new(num_f);
	  b->flv_out = bv_new(num_f);
     }
     return b;
}
Esempio n. 11
0
/**
 * Test basic BitVector get/set/unset operations
 */
static void test_bv(TestCase *tc, void *data)
{
    int i;
    BitVector *bv = bv_new();
    (void)data; /* suppress unused argument warning */

    Aiequal(0, bv->size);
    Aiequal(0, bv->count);
    Aiequal(0, bv_recount(bv));

    bv_set(bv, 10);
    Aiequal(1, bv_get(bv, 10));
    Aiequal(11, bv->size);
    Aiequal(1, bv->count);
    Aiequal(1, bv_recount(bv));

    bv_set(bv, 10);
    Aiequal(1, bv_get(bv, 10));
    Aiequal(11, bv->size);
    Aiequal(1, bv->count);
    Aiequal(1, bv_recount(bv));

    bv_set(bv, 20);
    Aiequal(1, bv_get(bv, 20));
    Aiequal(21, bv->size);
    Aiequal(2, bv->count);
    Aiequal(2, bv_recount(bv));

    bv_unset(bv, 21);
    Aiequal(0, bv_get(bv, 21));
    Aiequal(22, bv->size);
    Aiequal(2, bv->count);
    Aiequal(2, bv_recount(bv));

    bv_unset(bv, 20);
    Aiequal(0, bv_get(bv, 20));
    Aiequal(22, bv->size);
    Aiequal(1, bv->count);
    Aiequal(1, bv_recount(bv));
    Aiequal(1, bv_get(bv, 10));

    bv_set(bv, 100);
    Aiequal(1, bv_get(bv, 100));
    Aiequal(101, bv->size);
    Aiequal(2, bv->count);
    Aiequal(2, bv_recount(bv));
    Aiequal(1, bv_get(bv, 10));

    bv_clear(bv);
    Aiequal(0, bv_get(bv, 10));
    Aiequal(0, bv->size);
    Aiequal(0, bv->count);
    Aiequal(0, bv_recount(bv));
    bv_unset(bv, 20);
    Aiequal(21, bv->size);

    /* test setting bits at intervals for a large number of bits */
    bv_clear(bv);
    for (i = BV_INT; i < BV_SIZE; i += BV_INT) {
        bv_set(bv, i);
    }
    for (i = BV_INT; i < BV_SIZE; i += BV_INT) {
        Aiequal(1, bv_get(bv, i));
        Aiequal(0, bv_get(bv, i - 1));
        Aiequal(0, bv_get(bv, i + 1));
    }

    /* test setting all bits */
    bv_clear(bv);
    for (i = 0; i < BV_SIZE; i++) {
        bv_set(bv, i);
    }
    for (i = 0; i < BV_SIZE; i++) {
        Aiequal(1, bv_get(bv, i));
    }

    /* test random bits */
    bv_clear(bv);
    for (i = 0; i < BV_SIZE; i++) {
        if ((rand() % 2) == 0) {
            bv_set(bv, i);
            Aiequal(1, bv_get(bv, i));
        }
    }

    bv_destroy(bv);
}
Esempio n. 12
0
static void test_bv_and(TestCase *tc, void *data)
{
#   define AND_SIZE 1000
    static const int and_cnt = 500;
    BitVector *and_bv;
    BitVector *bv1 = bv_new();
    BitVector *bv2 = bv_new();
    BitVector *not_bv1, *not_bv2, *or_bv, *not_and_bv;
    char set1[AND_SIZE];
    char set2[AND_SIZE];
    int i;
    int count = 0;
    (void)data;

    memset(set1, 0, AND_SIZE);
    memset(set2, 0, AND_SIZE);
    for (i = 0; i < and_cnt; i++) {
        int bit = rand() % AND_SIZE;
        bv_set(bv1, bit);
        set1[bit] = 1;
    }
    for (i = 0; i < and_cnt; i++) {
        int bit = rand() % AND_SIZE;
        bv_set(bv2, bit);
        if (1 == set1[bit] && !set2[bit]) {
            count++;
            set2[bit] = 1;
        }
    }

    not_bv1 = bv_not(bv1); not_bv2 = bv_not(bv2);
    and_bv = bv_and(not_bv1, not_bv2);
    not_and_bv = bv_not(and_bv);
    or_bv = bv_or(bv1, bv2);
    Assert(bv_eq(not_and_bv, or_bv), "BitVectors should be equal");
    bv_destroy(not_bv1); bv_destroy(not_bv2);
    bv_destroy(and_bv);
    bv_destroy(not_and_bv);
    bv_destroy(or_bv);

    and_bv = bv_and(bv1, bv2);

    Aiequal(count, and_bv->count);
    for (i = 0; i < AND_SIZE; i++) {
        Aiequal(set2[i], bv_get(and_bv, i));
    }
    

    bv1 = bv_and_x(bv1, bv2);
    Assert(bv_eq(bv1, and_bv), "BitVectors should be equal");

    bv_destroy(bv2);
    bv_destroy(and_bv);

    bv2 = bv_new();
    and_bv = bv_and(bv1, bv2);

    Assert(bv_eq(bv2, and_bv), "ANDed BitVector should be empty");


    bv_destroy(bv1);
    bv_destroy(bv2);
    bv_destroy(and_bv);

    bv1 = bv_new();
    bv_not_x(bv1);
    bv2 = bv_new();
    bv_set(bv2, 10);
    bv_set(bv2, 11);
    bv_set(bv2, 20);
    and_bv = bv_and(bv1, bv2);

    Assert(bv_eq(bv2, and_bv), "ANDed BitVector should be equal");

    bv_destroy(bv1);
    bv_destroy(bv2);
    bv_destroy(and_bv);
}
Esempio n. 13
0
/** void setupDecoder(void)
/  Uses the current Convolutional Encoder to generate
/  the appropriate tables for an efficient Viterbi decoder
/  Should be called in main() before interrupt initialized
*/
void setupDecoder() {

	setPuncturing(FALSE);

	int i;
	int j;
	bv_t a = malloc (sizeof(struct bitvec));
	bv_new(a, 1);
	bv_t b = malloc (sizeof(struct bitvec));
	bv_new(b, 1);

	//*** this finds the properties of each state
	//generate the trellis (messages produced by state transitions)
	//generate decoding given two states
	for(i = 0; i < NUM_STATES; i++) {
		  for(j = 0; j < NUM_STATES; j++) {
			Trellis[i][j] = -1;
			Decode[i][j] = -1;
			if(j < 2)
				InverseTransitions[i][j] = -1;
		  }
	}
	// Trellis[0][0] = Trellis[1][2] = 0;
	// Trellis[1][0] = Trellis[0][2] = 3;
	// Trellis[2][1] = Trellis[3][3] = 1;
	// Trellis[3][1] = Trellis[2][3] = 2;

	for(i = 0; i < NUM_STATES; i++) {
		clear_vec(a);
		clear_vec(b);

		//get message and next state for input 0
		setState(i);
		encode(a,0);
		Transitions[i][0] = getState();
		if(InverseTransitions[getState()][0] == -1)
			InverseTransitions[getState()][0] = i;
		else
			InverseTransitions[getState()][1] = i;
		Trellis[i][getState()] = get(a,0,1);
		Decode[i][getState()] = 0;

		//get message and next state for input 1
		setState(i);
		encode(b,1);
		Transitions[i][1] = getState();
		if(InverseTransitions[getState()][0] == -1)
			InverseTransitions[getState()][0] = i;
		else
			InverseTransitions[getState()][1] = i;
		Trellis[i][getState()] = get(b,0,1);
		Decode[i][getState()] = 1;

		//initialize the decoded survivor paths
		paths[i] = malloc(sizeof(struct bitvec));
		bv_new(paths[i], 32);
		paths_next[i] = malloc (sizeof(struct bitvec));
		bv_new(paths_next[i], 32);

	}

	// for(i = 0; i < NUM_CODES; i++) {
	// 	  for(j = 0; j < 2; j++) {
	// 		printf("%d: %d -> %d \n", Decode[i][Transitions[i][j]], i, Transitions[i][j]);
	// 	  }
	// }
	// for(i = 0; i < NUM_CODES; i++) {
	// 	  for(j = 0; j < 2; j++) {
	// 		  printf("%d: %d -> %d \n", Decode[InverseTransitions[i][j]][i], InverseTransitions[i][j], i);
	// 	  }
	// }
	for(i = 0; i < NUM_CODES; i++) {
		for(j = 0; j < NUM_CODES; j++) {
		  printf("%d ", Trellis[i][j]);
		}

		printf("\n");
	}

	printf("Hamming distance \n");
	//pre-generate hamming distances for all messages
	for(i = 0; i < NUM_CODES; i++) {
		  load(a,i);
		  for(j = 0; j < NUM_CODES; j++) {
		    load(b,j);
		    HammingDistance[i][j] = hammingDistance(a,b);
		    printf("%d ", HammingDistance[i][j]);
		  }
		  printf("\n");
	}
	
	//reset the encoder and decoder
	clearState();
	vit_dec_reset();

	bv_free(a);
	bv_free(b);
	free(a);
	free(b);

	//set puncturing (only accomodates 2/3 puncture)
	setPuncturing(puncturedRec);
}
Esempio n. 14
0
static void bit_vector_init(bit_vector_t *bv, s4 size) {
	bv->bv = bv_new(size);
	bv->size = size;
}