Skip to content

Commit 2834619

Browse files
authored
[HashRecognize] Introduce CRC macros, factoring code (#262)
1 parent b9f9a11 commit 2834619

File tree

13 files changed

+159
-408
lines changed

13 files changed

+159
-408
lines changed
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY 4129
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 0x8000;
10-
for (unsigned int i = 1; i < 256; i <<= 1) {
11-
crc = (crc << 1) ^ (crc & 0x8000 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < i; j++)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_BE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint16_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 2; ++i) {
27-
uint8_t pos = (crc ^ (data << (i << 3))) >> 8;
28-
crc = (crc << 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_BE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint16_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 16; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_shl = crc << 1;
41-
crc = (xor_crc_data & 0x8000) ? (crc_shl ^ GENPOLY) : crc_shl;
42-
data <<= 1;
43-
}
23+
CRCLOOP_BE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint16_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY 4129
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 0x8000;
10-
for (unsigned int i = 1; i < 256; i <<= 1) {
11-
crc = (crc << 1) ^ (crc & 0x8000 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < i; j++)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_BE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint32_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 4; ++i) {
27-
uint8_t pos = (crc ^ (data << (i << 3))) >> 8;
28-
crc = (crc << 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_BE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint32_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 32; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_shl = crc << 1;
41-
crc = (xor_crc_data & 0x8000) ? (crc_shl ^ GENPOLY) : crc_shl;
42-
data <<= 1;
43-
}
23+
CRCLOOP_BE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint32_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY 4129
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 0x8000;
10-
for (unsigned int i = 1; i < 256; i <<= 1) {
11-
crc = (crc << 1) ^ (crc & 0x8000 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < i; j++)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_BE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint8_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 1; ++i) {
27-
uint8_t pos = (crc ^ (data << (i << 3))) >> 8;
28-
crc = (crc << 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_BE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint8_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 8; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_shl = crc << 1;
41-
crc = (xor_crc_data & 0x8000) ? (crc_shl ^ GENPOLY) : crc_shl;
42-
data <<= 1;
43-
}
23+
CRCLOOP_BE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint8_t data[8] = {0, 1, 11, 16, 129, 255, 142, 255};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY -24575
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 1;
10-
for (unsigned int i = 128; i; i >>= 1) {
11-
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < 256; j += 2 * i)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_LE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint16_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 2; ++i) {
27-
uint8_t pos = crc ^ (data >> (i << 3));
28-
crc = (crc >> 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_LE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint16_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 16; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_lshr = crc >> 1;
41-
crc = (xor_crc_data & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
42-
data >>= 1;
43-
}
23+
CRCLOOP_LE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint16_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY -24575
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 1;
10-
for (unsigned int i = 128; i; i >>= 1) {
11-
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < 256; j += 2 * i)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_LE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint32_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 4; ++i) {
27-
uint8_t pos = crc ^ (data >> (i << 3));
28-
crc = (crc >> 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_LE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint32_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 32; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_lshr = crc >> 1;
41-
crc = (xor_crc_data & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
42-
data >>= 1;
43-
}
23+
CRCLOOP_LE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint32_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}
Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
1+
#include "crc_macros.h"
12
#include <stdint.h>
2-
#include <stdlib.h>
33

44
#define GENPOLY -24575
55

66
static uint16_t CRCTable[256];
77

8-
static void crc_init(void) {
9-
uint16_t crc = 1;
10-
for (unsigned int i = 128; i; i >>= 1) {
11-
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
12-
for (unsigned int j = 0; j < 256; j += 2 * i)
13-
CRCTable[i + j] = crc ^ CRCTable[j];
14-
}
15-
}
8+
static void crc_init(void) { CRCINIT_LE(uint16_t, CRCTable, GENPOLY); }
169

1710
// This table-lookup should be equivalent to the code emitted when optimizing
1811
// CRC with HashRecognize. This function itself will be untouched by
1912
// HashRecognize.
2013
static uint16_t crc_table(uint16_t crc_initval, uint8_t data) {
2114
uint16_t crc = crc_initval;
22-
23-
if (CRCTable[255] == 0)
24-
crc_init();
25-
26-
for (size_t i = 0; i < 1; ++i) {
27-
uint8_t pos = crc ^ (data >> (i << 3));
28-
crc = (crc >> 8) ^ CRCTable[pos];
29-
}
30-
15+
CRCTABLE_LE(uint16_t, CRCTable, crc_init, crc, data);
3116
return crc;
3217
}
3318

3419
static uint16_t crc_loop(uint16_t crc_initval, uint8_t data) {
3520
uint16_t crc = crc_initval;
3621

3722
// This loop will be optimized by HashRecognize.
38-
for (size_t i = 0; i < 8; ++i) {
39-
uint16_t xor_crc_data = crc ^ data;
40-
uint16_t crc_lshr = crc >> 1;
41-
crc = (xor_crc_data & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
42-
data >>= 1;
43-
}
23+
CRCLOOP_LE(uint16_t, GENPOLY, crc, data);
4424
return crc;
4525
}
4626

4727
int main() {
48-
// These are random hand-picked values.
49-
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
50-
static const uint8_t data[8] = {0, 1, 11, 16, 129, 255, 142, 255};
51-
for (size_t i = 0; i < 8; ++i) {
52-
uint16_t actual = crc_loop(crc_initval[i], data[i]);
53-
uint16_t expected = crc_table(crc_initval[i], data[i]);
54-
if (actual != expected)
55-
return 1;
56-
}
57-
return 0;
28+
int res = 0;
29+
VERIFY_RESULT(crc_table, crc_loop, res);
30+
return res;
5831
}

0 commit comments

Comments
 (0)