之前博客有记录BDI压缩算法以及FPC压缩算法的论文阅读记录,在github上面找到了对应的源码来源,基于对这两种压缩算法的理论理解,详细分析算法源码。
FPCCompress
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
|
static unsigned long long my_llabs ( long long x ) { unsigned long long t = x >> 63; return (x ^ t) - t; }
static unsigned my_abs ( int x ) { unsigned t = x >> 31; return (x ^ t) - t; }
long long unsigned * convertBuffer2Array (char * buffer, unsigned size, unsigned step) {
long long unsigned * values = (long long unsigned *) VG_(malloc)("cg.compress.ci.1", sizeof(long long unsigned) * size/step); unsigned int i,j; for (i = 0; i < size / step; i++) { values[i] = 0; }
for (i = 0; i < size; i += step ){ for (j = 0; j < step; j++){ values[i / step] += (long long unsigned)((unsigned char)buffer[i + j]) << (8*j); } } return values; }
|
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
| unsigned FPCCompress(char * buffer, unsigned size ){ long long unsigned * values = convertBuffer2Array(buffer, size*4, 4); unsigned compressable = 0; unsigned int i; for (i = 0; i < size; i++) { if(values[i] == 0){ compressable += 1; continue; } if(my_abs((int)(values[i])) <= 0xFF){ compressable += 1; continue; } if(my_abs((int)(values[i])) <= 0xFFFF){ compressable += 2; continue; } if(((values[i]) & 0xFFFF) == 0 ){ compressable += 2; continue; } if( my_abs((int)((values[i]) & 0xFFFF)) <= 0xFF && my_abs((int)((values[i] >> 16) & 0xFFFF)) <= 0xFF){ compressable += 2; continue; } unsigned byte0 = (values[i]) & 0xFF; unsigned byte1 = (values[i] >> 8) & 0xFF; unsigned byte2 = (values[i] >> 16) & 0xFF; unsigned byte3 = (values[i] >> 24) & 0xFF; if(byte0 == byte1 && byte0 == byte2 && byte0 == byte3){ compressable += 1; continue; } compressable += 4; } VG_(free)(values); unsigned compSize = compressable + size * 3 / 8; if(compSize < size * 4) return compSize; else return size * 4; }
|
原文作者: malizhen
原文链接: http://malizhen.github.io/2019/07/23/FPC源码分析/
版权声明: 转载请注明出处(必须保留原文作者署名及原文链接)