Skip to content

Instantly share code, notes, and snippets.

@enjoylife
Created October 29, 2012 18:46
Show Gist options
  • Save enjoylife/3975639 to your computer and use it in GitHub Desktop.
Save enjoylife/3975639 to your computer and use it in GitHub Desktop.
Flexible array test case
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define CEILING(x,y) (((x) + (y) - 1) / (y))
#define LEADINGBIT(r) ((sizeof(unsigned long int)*8) - __builtin_clzl(r))
#define BITLAST(k,n) ((k) & ((1<<(n))-1)) // for geting subset of bits
#define BITSUBSET(k,m,n) BITLAST((k)>>(m),((n)-(m)))
#define BUFF 32 // for printing of binary string
char *binrep (unsigned int val, char *buff, int sz);
int main()
{
char buff[BUFF+1];
// proof of correct ceilling macro
assert(CEILING(7,4) == 2);
// proof of implicit floor
assert(7/4 == 1);
assert(1/2 == 0);
assert(LEADINGBIT(4) == 3);
assert(LEADINGBIT(1) == 1);
assert(LEADINGBIT(8) == 4);
unsigned int x,y;
long int a = 0;
unsigned int data_size = 1, super_count = 0, super_last_count = 0, super_size=1, length = 20;
long int ** index = malloc(length * sizeof(long int));;
fflush(stdout);
printf("\n");
for(x = 0 ; x < length; x++){
index[x] = malloc(data_size * sizeof(long int));
for(y = 0; y < data_size; y++){
index[x][y]=a;
a++;
printf("%-3ld ", index[x][y]);
}
super_last_count++;
printf("\n");
if(super_last_count == super_size){
super_last_count = 0;
if(super_count%2){
super_size *=2;
super_count++;
} else {
data_size *= 2;
super_count++;
}
}
}
printf("Finished element dump\n");
long int get_index(unsigned long int i){
unsigned long int r,k,b,e,p,p2,skipb,datab,superb,indexb;
printf("\nTrying to get %ld\n", i);
r = i +1; // correct
k = LEADINGBIT(r)-1; // correct
b = BITSUBSET(r,k-k/2,k);//Maybe correct
e = BITSUBSET(r,0, CEILING(k,2)); //correct
p = (1 << k)-1 ; // ????
printf("R: [%s]b\n",binrep(r,buff,BUFF));
printf("k/2=[%ld], Ceil(k,2)=[%ld]\n",k/2,CEILING(k,2));
printf("K: [%ld] is the leading 1 bit\n",k);
printf("B: [%ld]\n",b);
printf("E: [%ld]\n",e);
printf("P: [%ld] data blocks prior to our superblock\n",p);
printf("p+b,e : [%ld,%ld] \n",p+b,e);
return index[p+b][e];
}
assert(get_index(0)==0);
assert(get_index(1)==1);
assert(get_index(2)==2);
assert(get_index(3)==3);
return 0;
}
/* returns a string rep of a integer */
char *binrep (unsigned int val, char *buff, int sz) {
char *pbuff = buff;
/* Must be able to store one character at least. */
if (sz < 1) return NULL;
/* Special case for zero to ensure some output. */
if (val == 0) {
*pbuff++ = '0';
*pbuff = '\0';
return buff;
}
/* Work from the end of the buffer back. */
pbuff += sz;
*pbuff-- = '\0';
/* For each bit (going backwards) store character. */
while (val != 0) {
if (sz-- == 0) return NULL;
*pbuff-- = ((val & 1) == 1) ? '1' : '0';
/* Get next bit. */
val >>= 1;
}
return pbuff+1;
}
[clemensm@gaia:23]> ./a.out
0
1 2
3 4
5 6
7 8 9 10
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
27 28 29 30
31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86
87 88 89 90 91 92 93 94
95 96 97 98 99 100 101 102
103 104 105 106 107 108 109 110
Finished element dump
Trying to get 0
R: [1]b
k/2=[0], Ceil(k,2)=[0]
K: [0] is the leading 1 bit
B: [0]
E: [0]
P: [0] data blocks prior to our superblock
p+b,e : [0,0]
Trying to get 1
R: [10]b
k/2=[0], Ceil(k,2)=[1]
K: [1] is the leading 1 bit
B: [0]
E: [0]
P: [1] data blocks prior to our superblock
p+b,e : [1,0]
Trying to get 2
R: [11]b
k/2=[0], Ceil(k,2)=[1]
K: [1] is the leading 1 bit
B: [0]
E: [1]
P: [1] data blocks prior to our superblock
p+b,e : [1,1]
Trying to get 3
R: [100]b
k/2=[1], Ceil(k,2)=[1]
K: [2] is the leading 1 bit
B: [0]
E: [0]
P: [3] data blocks prior to our superblock
p+b,e : [3,0]
a.out: test_array.c:81: main: Assertion `get_index(3)==3' failed.
Abort (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment