1 #define MAIN_(x) A_CAST_2(x, _crc)
4 #if !defined __cplusplus
5 #define WRITE_TABLE(bit, row, fmt) \
6 static void write_table##bit(FILE *out, a_u##bit ctx[0x100], char const *label) \
8 (void)fprintf(out, "uint%i_t const %s[0x%X] = {\n", bit, label, 0x100); \
9 (void)fprintf(out, " /* clang-format off */\n"); \
10 for (a_size i = 0; i != 0x100 / (row); ++i) \
12 (void)fprintf(out, " "); \
13 for (a_size j = 0; j != (row); ++j) \
15 (void)fprintf(out, "0x%0" #fmt PRIX##bit ",", ctx[(row) * i + j]); \
16 if (j != (row)-1) { (void)fputc(' ', out); } \
18 (void)fputc('\n', out); \
20 (void)fprintf(out, " /* clang-format on */\n"); \
21 (void)fprintf(out, "};\n"); \
26 WRITE_TABLE(64, 4, 16)
27 static void create_table(FILE *out
)
29 (void)fprintf(out
, "#include <stdint.h>\n");
32 a_crc8m_init(table8
, 0x07);
33 write_table8(out
, table8
, "CRC8M");
34 a_crc8l_init(table8
, 0x07); // 0xE0
35 write_table8(out
, table8
, "CRC8L");
38 a_crc16m_init(table16
, 0x8005);
39 write_table16(out
, table16
, "CRC16M");
40 a_crc16l_init(table16
, 0x8005); // 0xA001
41 write_table16(out
, table16
, "CRC16L");
44 a_crc32m_init(table32
, 0x04C11DB7);
45 write_table32(out
, table32
, "CRC32M");
46 a_crc32l_init(table32
, 0x04C11DB7); // 0xEDB88320
47 write_table32(out
, table32
, "CRC32L");
50 a_crc64m_init(table64
, 0x42F0E1EBA9EA3693);
51 write_table64(out
, table64
, "CRC64M");
52 a_crc64l_init(table64
, 0x42F0E1EBA9EA3693); // 0xC96C5795D7870F42
53 write_table64(out
, table64
, "CRC64L");
57 #endif /* __cplusplus */
59 static void test(void)
61 #define TEXT "0123456789"
62 #define SIZE (sizeof(TEXT) - 1)
65 a_crc8m_init(table8
, A_U8_C(0x07));
66 TEST_BUG(a_crc8(table8
, TEXT
, SIZE
, 0) == A_U8_C(0x45));
67 a_crc8l_init(table8
, A_U8_C(0x31)); // 0x8C
68 TEST_BUG(a_crc8(table8
, TEXT
, SIZE
, 0) == A_U8_C(0x75));
71 a_crc16m_init(table16
, A_U16_C(0x1021));
72 TEST_BUG(a_crc16m(table16
, TEXT
, SIZE
, 0) == A_U16_C(0x9C58));
73 a_crc16l_init(table16
, A_U16_C(0x8005)); // 0xA001
74 TEST_BUG(a_crc16l(table16
, TEXT
, SIZE
, 0) == A_U16_C(0x443D));
77 a_crc32m_init(table32
, A_U32_C(0x1EDC6F41));
78 TEST_BUG(a_crc32m(table32
, TEXT
, SIZE
, 0) == A_U32_C(0x512B456E));
79 a_crc32l_init(table32
, A_U32_C(0x04C11DB7)); // 0xEDB88320
80 TEST_BUG(a_crc32l(table32
, TEXT
, SIZE
, 0) == A_U32_C(0x450EAFB0));
83 a_crc64m_init(table64
, A_U64_C(0x000000000000001B));
84 TEST_BUG(a_crc64m(table64
, TEXT
, SIZE
, 0) == A_U64_C(0xE4FFBEA588AFC790));
85 a_crc64l_init(table64
, A_U64_C(0x42F0E1EBA9EA3693)); // 0xC96C5795D7870F42
86 TEST_BUG(a_crc64l(table64
, TEXT
, SIZE
, 0) == A_U64_C(0xDA60676A5CDE0008));
92 int MAIN(int argc
, char *argv
[]) // NOLINT(misc-definitions-in-headers)
94 #if !defined __cplusplus
98 char const *name
= argv
[argc
- 1];
99 int file
= a_cast_s(int, *name
);
102 out
= fopen(name
, "wb");
110 if (file
&& fclose(out
) == EOF
)
115 #else /* !__cplusplus */
118 #endif /* __cplusplus */