create a_regress_simple_evar
[liba.git] / src / crc.c
blobfc6ff4c21839ae2179863b6bb8424483adbfd454
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 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
36 value = table[value ^ *p++];
38 return value;
41 void a_crc16m_init(a_u16 table[0x100], a_u16 poly)
43 for (unsigned int c = 0; c != 0x100; ++c)
45 unsigned int value = c << 8;
46 for (unsigned int b = 8; b; --b)
48 unsigned int sig = A_U16_C(0x8000) & value;
49 value <<= 1;
50 if (sig) { value ^= poly; }
52 table[c] = (a_u16)value;
55 void a_crc16l_init(a_u16 table[0x100], a_u16 poly)
57 poly = a_u16_rev(poly);
58 for (unsigned int c = 0; c != 0x100; ++c)
60 unsigned int value = c;
61 for (unsigned int b = 8; b; --b)
63 unsigned int sig = value & 1;
64 value >>= 1;
65 if (sig) { value ^= poly; }
67 table[c] = (a_u16)value;
70 a_u16 a_crc16m(a_u16 const table[0x100], void const *pdata, a_size nbyte, a_u16 value)
72 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
74 value = (a_u16)((value << 8) ^ table[((value >> 8) ^ *p++) & 0xFF]);
76 return value;
78 a_u16 a_crc16l(a_u16 const table[0x100], void const *pdata, a_size nbyte, a_u16 value)
80 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
82 value = (a_u16)((value >> 8) ^ table[(value ^ *p++) & 0xFF]);
84 return value;
87 void a_crc32m_init(a_u32 table[0x100], a_u32 poly)
89 for (a_u32 c = 0; c != 0x100; ++c)
91 a_u32 value = c << 24;
92 for (unsigned int b = 8; b; --b)
94 a_u32 sig = A_U32_C(0x80000000) & value;
95 value <<= 1;
96 if (sig) { value ^= poly; }
98 table[c] = value;
101 void a_crc32l_init(a_u32 table[0x100], a_u32 poly)
103 poly = a_u32_rev(poly);
104 for (unsigned int c = 0; c != 0x100; ++c)
106 a_u32 value = c;
107 for (unsigned int b = 8; b; --b)
109 a_u32 sig = value & 1;
110 value >>= 1;
111 if (sig) { value ^= poly; }
113 table[c] = value;
116 a_u32 a_crc32m(a_u32 const table[0x100], void const *pdata, a_size nbyte, a_u32 value)
118 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
120 value = (value << 8) ^ table[((value >> 24) ^ *p++) & 0xFF];
122 return value;
124 a_u32 a_crc32l(a_u32 const table[0x100], void const *pdata, a_size nbyte, a_u32 value)
126 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
128 value = (value >> 8) ^ table[(value ^ *p++) & 0xFF];
130 return value;
133 void a_crc64m_init(a_u64 table[0x100], a_u64 poly)
135 for (a_u64 c = 0; c != 0x100; ++c)
137 a_u64 value = c << 56;
138 for (unsigned int b = 8; b; --b)
140 a_u64 sig = A_U64_C(0x8000000000000000) & value;
141 value <<= 1;
142 if (sig) { value ^= poly; }
144 table[c] = value;
147 void a_crc64l_init(a_u64 table[0x100], a_u64 poly)
149 poly = a_u64_rev(poly);
150 for (unsigned int c = 0; c != 0x100; ++c)
152 a_u64 value = c;
153 for (unsigned int b = 8; b; --b)
155 a_u64 sig = value & 1;
156 value >>= 1;
157 if (sig) { value ^= poly; }
159 table[c] = value;
162 a_u64 a_crc64m(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value)
164 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
166 value = (value << 8) ^ table[((value >> 56) ^ *p++) & 0xFF];
168 return value;
170 a_u64 a_crc64l(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value)
172 for (a_byte const *p = (a_byte const *)pdata; nbyte; --nbyte)
174 value = (value >> 8) ^ table[(value ^ *p++) & 0xFF];
176 return value;