MPRESS_BYTE_ARRAY(k, K0_o, K0_e);
#define COMPRESS_BYTE_ARRAY(a,var_o,var_e) {\
var_e = U32BIG(((u32*)(a))[1]);\
var_o = var_e >> 1;\
COMPRESS_LONG(var_e);\
COMPRESS_LONG(var_o);\
t1_e = U32BIG(((u32*)(a))[0]);\
t1_o = t1_e >> 1;\
COMPRESS_LONG(t1_e);\
COMPRESS_LONG(t1_o);\
var_e |= t1_e << 16;\
var_o |= t1_o << 16;\
}
#define COMPRESS_LONG(x) {\
x &= 0x55555555;\
x = (x | (x >> 1)) & 0x33333333;\
x = (x | (x >> 2)) & 0x0f0f0f0f;\
x = (x | (x >> 4)) & 0x00ff00ff;\
x = (x | (x >> 8)) & 0x0000ffff;\
}
测试代码
void printBinary(unsigned char * str, u8 *a,int len) {
printf("%8s:\n", str);
int i,j,count=0;
u8 t;
for (i = 0; i < len; i++) {
t = a[i];
printf("0x%02x : ",t);
for (j = 0; j < 8; j++) {
printf("[%2d]%d ", (len-1-i)*8 +j, t / 0x80);// 下标 ,取最高位
t = t * 2;
}
printf("\n");
}
printf("\n");
}
void zxfCOMPRESS_LONG(u32 t) {
u32 x = t, t1, t2, t3, t4;
printBinary("x",(&x), 4);
x &= 0x55555555;
printBinary("x: x &= 0x55555555", (u8*)(&x), 4);
t1 = (x | (x >> 1));
printBinary("t1: t1 = (x | (x >> 1))", (u8*)(&t1), 4);
x = t1 & 0x33333333;
printBinary("x: x = t1 & 0x33333333;", (u8*)(&x), 4);
t2 = (x | (x >> 2));
printBinary("t2: t2 = (x | (x >> 2));", (u8*)(&t2), 4);
x = t2 & 0x0f0f0f0f;
printBinary("x: x = t2 & 0x0f0f0f0f;", (u8*)(&x), 4);
t3 = (x | (x >> 4));
printBinary("t3: t3 = (x | (x >> 4));", (u8*)(&t3), 4);
x = t3 & 0x00ff00ff;
printBinary("x: x = t3 & 0x00ff00ff;", (u8*)(&x), 4);
t4 = (x | (x >> 8));
printBinary("t4: t4 = (x | (x >> 8));", (u8*)(&t4), 4);
x = t4 & 0x0000ffff;
printBinary("x:x = t4 & 0x0000ffff;", (u8*)(&x), 4);
t++;
}
int main()
{
u32 t = 0xffffffff;
zxfCOMPRESS_LONG( t);
return 0;
}
#define EXPAND_U32(var,var_o,var_e) {\
/*var 32-bit, and var_o/e 16-bit*/\
t0_e = (var_e);\
t0_o = (var_o);\
EXPAND_SHORT(t0_e);\
EXPAND_SHORT(t0_o);\
var = t0_e | (t0_o << 1);\
}
#define EXPAND_SHORT(x) {\
x &= 0x0000ffff;\
x = (x | (x << 8)) & 0x00ff00ff;\
x = (x | (x << 4)) & 0x0f0f0f0f;\
x = (x | (x << 2)) & 0x33333333;\
x = (x | (x << 1)) & 0x55555555;\
}