format with clang-format-18
[liba.git] / src / crc.c
blobaff43d7ac11db573e5d1d6aa2ed2d70ce1aa5fc2
1 #include "a/crc.h"
3 void a_crc8m_init(a_u8 table[0x100], a_u8 poly)
5 for (unsigned int c = 0; c != 0x100; ++c)
7 unsigned int value = c;
8 for (unsigned int b = 8; b; --b)
10 unsigned int sig = A_U8_C(0x80) & value;
11 value <<= 1;
12 if (sig) { value ^= poly; }
14 table[c] = (a_u8)value;
17 void a_crc8l_init(a_u8 table[0x100], a_u8 poly)
19 poly = a_u8_rev(poly);
20 for (unsigned int c = 0; c != 0x100; ++c)
22 unsigned int value = c;
23 for (unsigned int b = 8; b; --b)
25 unsigned int sig = value & 1;
26 value >>= 1;
27 if (sig) { value ^= poly; }
29 table[c] = (a_u8)value;
32 a_u8 a_crc8(a_u8 const table[0x100], void const *pdata, a_size nbyte, a_u8 value)
34 a_byte const *p = (a_byte const *)pdata;
35 a_byte const *q = (a_byte const *)pdata + nbyte;
36 while (p != q) { value = table[value ^ *p++]; }
37 return value;
40 void a_crc16m_init(a_u16 table[0x100], a_u16 poly)
42 for (unsigned int c = 0; c != 0x100; ++c)
44 unsigned int value = c << 8;
45 for (unsigned int b = 8; b; --b)
47 unsigned int sig = A_U16_C(0x8000) & value;
48 value <<= 1;
49 if (sig) { value ^= poly; }
51 table[c] = (a_u16)value;
54 void a_crc16l_init(a_u16 table[0x100], a_u16 poly)
56 poly = a_u16_rev(poly);
57 for (unsigned int c = 0; c != 0x100; ++c)
59 unsigned int value = c;
60 for (unsigned int b = 8; b; --b)
62 unsigned int sig = value & 1;
63 value >>= 1;
64 if (sig) { value ^= poly; }
66 table[c] = (a_u16)value;
69 a_u16 a_crc16m(a_u16 const table[0x100], void const *pdata, a_size nbyte, a_u16 value)
71 a_byte const *p = (a_byte const *)pdata;
72 a_byte const *q = (a_byte const *)pdata + nbyte;
73 while (p != q) { value = (a_u16)((value << 8) ^ table[((value >> 8) ^ *p++) & 0xFF]); }
74 return value;
76 a_u16 a_crc16l(a_u16 const table[0x100], void const *pdata, a_size nbyte, a_u16 value)
78 a_byte const *p = (a_byte const *)pdata;
79 a_byte const *q = (a_byte const *)pdata + nbyte;
80 while (p != q) { value = (a_u16)((value >> 8) ^ table[(value ^ *p++) & 0xFF]); }
81 return value;
84 void a_crc32m_init(a_u32 table[0x100], a_u32 poly)
86 for (a_u32 c = 0; c != 0x100; ++c)
88 a_u32 value = c << 24;
89 for (unsigned int b = 8; b; --b)
91 a_u32 sig = A_U32_C(0x80000000) & value;
92 value <<= 1;
93 if (sig) { value ^= poly; }
95 table[c] = value;
98 void a_crc32l_init(a_u32 table[0x100], a_u32 poly)
100 poly = a_u32_rev(poly);
101 for (unsigned int c = 0; c != 0x100; ++c)
103 a_u32 value = c;
104 for (unsigned int b = 8; b; --b)
106 a_u32 sig = value & 1;
107 value >>= 1;
108 if (sig) { value ^= poly; }
110 table[c] = value;
113 a_u32 a_crc32m(a_u32 const table[0x100], void const *pdata, a_size nbyte, a_u32 value)
115 a_byte const *p = (a_byte const *)pdata;
116 a_byte const *q = (a_byte const *)pdata + nbyte;
117 while (p != q) { value = (value << 8) ^ table[((value >> 24) ^ *p++) & 0xFF]; }
118 return value;
120 a_u32 a_crc32l(a_u32 const table[0x100], void const *pdata, a_size nbyte, a_u32 value)
122 a_byte const *p = (a_byte const *)pdata;
123 a_byte const *q = (a_byte const *)pdata + nbyte;
124 while (p != q) { value = (value >> 8) ^ table[(value ^ *p++) & 0xFF]; }
125 return value;
128 void a_crc64m_init(a_u64 table[0x100], a_u64 poly)
130 for (a_u64 c = 0; c != 0x100; ++c)
132 a_u64 value = c << 56;
133 for (unsigned int b = 8; b; --b)
135 a_u64 sig = A_U64_C(0x8000000000000000) & value;
136 value <<= 1;
137 if (sig) { value ^= poly; }
139 table[c] = value;
142 void a_crc64l_init(a_u64 table[0x100], a_u64 poly)
144 poly = a_u64_rev(poly);
145 for (unsigned int c = 0; c != 0x100; ++c)
147 a_u64 value = c;
148 for (unsigned int b = 8; b; --b)
150 a_u64 sig = value & 1;
151 value >>= 1;
152 if (sig) { value ^= poly; }
154 table[c] = value;
157 a_u64 a_crc64m(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value)
159 a_byte const *p = (a_byte const *)pdata;
160 a_byte const *q = (a_byte const *)pdata + nbyte;
161 while (p != q) { value = (value << 8) ^ table[((value >> 56) ^ *p++) & 0xFF]; }
162 return value;
164 a_u64 a_crc64l(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value)
166 a_byte const *p = (a_byte const *)pdata;
167 a_byte const *q = (a_byte const *)pdata + nbyte;
168 while (p != q) { value = (value >> 8) ^ table[(value ^ *p++) & 0xFF]; }
169 return value;