Skip to content
Snippets Groups Projects
gen_intrange.c 2.43 KiB
Newer Older
Kia's avatar
Kia committed
#include <string.h>
#include <stdlib.h>
Kia's avatar
Kia committed
#include <assert.h>
#include <stdio.h>



struct charset {
	uint8_t bitmap[32]; // 32 * 8 = 256
};


void charset_set(struct charset *cs, uint8_t char_idx, uint8_t indicator) {

	int arr_idx = char_idx / 8;
	int bit_idx = char_idx % 8;

	uint8_t newbit = 1;
	if(indicator == 1) {
		cs->bitmap[arr_idx] = cs->bitmap[arr_idx] | (newbit << bit_idx);
	} else {
		cs->bitmap[arr_idx] = cs->bitmap[arr_idx] & ~(newbit << bit_idx);
	}
}
Kia's avatar
Kia committed

int main(int argc, char *argv[]) {
Kia's avatar
Kia committed
    assert(argc == 4);
    uint8_t bytes  = atoi(argv[1]);
    uint64_t low   = atoi(argv[2]);
    uint64_t high  = atoi(argv[3]);
Kia's avatar
Kia committed
    printf("LOW = %llu\nHIGH=%llu\n", low, high);
Kia's avatar
Kia committed
    struct charset *cs = (struct charset *) calloc(sizeof(struct charset), 1);
Kia's avatar
Kia committed

Kia's avatar
Kia committed
    for (int i=0; i<256; i++)
    {
    	charset_set(cs, i, 1);
    }
 
    charset_set(cs, 255, 1);
    charset_set(cs, 254, 0);

    for (int i = 0; i<32; i++)
    	{
    		printf("bitmapism %d = %hhu\n",i,  cs->bitmap[i]);
}
Kia's avatar
Kia committed
void gen_int_range(uint64_t low, uint64_t high, uint8_t bytes) {
Kia's avatar
Kia committed
  // Possible FIXME: TallerThanMe
Kia's avatar
Kia committed
  if (1 == bytes) {
    HCharset cs = new_charset(mm__);
    for (uint64_t i=low; i<=high; ++i) {
      charset_set(cs, i, 1);
    }
    HCFS_ADD_CHARSET(cs);
  }
  else if (1 < bytes) {
    uint8_t low_head, hi_head;
    low_head = ((low >> (8*(bytes - 1))) & 0xFF);
    hi_head = ((high >> (8*(bytes - 1))) & 0xFF);
    if (low_head != hi_head) {
      HCFS_BEGIN_CHOICE() {
	HCFS_BEGIN_SEQ() {
	  HCFS_ADD_CHAR(low_head);
	  gen_int_range(mm__, stk__, low & ((1 << (8 * (bytes - 1))) - 1), ((1 << (8*(bytes-1)))-1), bytes-1);
	} HCFS_END_SEQ();
	HCFS_BEGIN_SEQ() {
	  HCharset hd = new_charset(mm__);
	  HCharset rest = new_charset(mm__);
	  for (int i = 0; i < 256; i++) {
	    charset_set(hd, i, (i > low_head && i < hi_head));
	    charset_set(rest, i, 1);
	  }
	  HCFS_ADD_CHARSET(hd);
	  for (int i = 2; i < bytes; i++)
	    HCFS_ADD_CHARSET(rest);
	} HCFS_END_SEQ();
	HCFS_BEGIN_SEQ() {
	  HCFS_ADD_CHAR(hi_head);
	  gen_int_range(mm__, stk__, 0, high & ((1 << (8 * (bytes - 1))) - 1), bytes-1);
	} HCFS_END_SEQ();
      } HCFS_END_CHOICE();
    } else {
      // TODO: find a way to merge this with the higher-up SEQ
      HCFS_BEGIN_CHOICE() {
	HCFS_BEGIN_SEQ() {
	  HCFS_ADD_CHAR(low_head);
	  gen_int_range(mm__, stk__,
			low & ((1 << (8 * (bytes - 1))) - 1),
			high & ((1 << (8 * (bytes - 1))) - 1),
			bytes - 1);
	} HCFS_END_SEQ();
      } HCFS_END_CHOICE();
    }
  }
Kia's avatar
Kia committed
}