李老师用C++模板实现了一个crc table的例子,
看后觉得过瘾,这水平不是盖的。
但那个是C++写的,为了考虑各位C玩家的感受,
酒鬼想了想,用C语言的宏定义,复刻了一个版本,
同样编译器计算,代码如下(gcc编译验证通过)
- #include <stdint.h>
- #include <stdio.h>
- #include "macro_loop.h"
- #define CRC_LOOP(n,m) (((m) >> 1) ^ (-(int32_t)((m) & 1) & 0xEDB88320))
- #define CRC(n) FORA7(CRC_LOOP, (n))
- #define CRC_ARRAY(n,m) m CRC(n),
- const static uint32_t crc32tbl[256] = {
- FORB255(CRC_ARRAY, )
- };
- int main ()
- {
- int i;
- for (i = 0; i < 256; i++) {
- printf("%x ", crc32tbl[i]);
- }
- return 0;
- }
这个循环和编译器计算的秘密,在macro_loop.h中。
代码很长,附件上传了。
|