ShadowDance 发表于 2025-10-29 01:27

分享一个大佬写的一个CRC32table表生成源代码

#include <stdio.h>
#include <stdint.h>

#if 0
#define POLY      0x04c11db7
#define POSTFIX      "04c11db7"
#define POLY      0x1EDC6F41
#define POSTFIX      "1edc6f41"
#define POLY      0xA833982B
#define POSTFIX      "a833982b"
#define POLY      0x000000AF
#define POSTFIX      "000000af"
#endif

#define POLY      0x814141AB
#define POSTFIX      "814141ab"

int main (void)
{
    uint32_t i, j;
    uint32_t c;
    uint32_t table;

    for (i = 0; i < 256; i++) {
      for (c = i << 24, j = 8; j > 0; --j)
            c = c & 0x80000000 ? (c << 1) ^ POLY : (c << 1);
      table = c;
    }

    printf ("static const uint32_t crc32_table_" POSTFIX "[] =\n{\n");
    for (i = 0; i < 256; i += 4)
    {
      printf ("0x%08x, 0x%08x, 0x%08x, 0x%08x",
                table, table, table, table);
      if (i + 4 < 256)
            putchar (',');
      putchar ('\n');
    }
    printf ("};\n");
    return 0;
}这段代码可以输出CRC32的查找表。原作者可是大佬!


发光的梦 发表于 2025-11-1 21:51

查表法还是挺方便的实现方式。达到速度与计算复杂度均衡的一个实现。

抒情黎明 发表于 2025-11-2 15:34

学习了,一直不会这个查找表的生成

VelvetNight 发表于 2025-11-10 15:19

现在AI是不是很容易这个需求。
页: [1]
查看完整版本: 分享一个大佬写的一个CRC32table表生成源代码