Skip to content

[HashRecognize] Add tests to verify CRC optz #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SingleSource/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory(SignlessTypes)
add_subdirectory(Threads)
add_subdirectory(Vector)
add_subdirectory(Vectorizer)
add_subdirectory(HashRecognize)
add_subdirectory(X86)
add_subdirectory(AArch64)

Expand Down
1 change: 1 addition & 0 deletions SingleSource/UnitTests/HashRecognize/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
llvm_singlesource()
5 changes: 5 additions & 0 deletions SingleSource/UnitTests/HashRecognize/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SingleSource/UnitTests/HashRecognize/Makefile
LEVEL = ../../..

include $(LEVEL)/Makefile.config
include $(LEVEL)/SingleSource/Makefile.singlesrc
58 changes: 58 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc16.be.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY 4129

static uint16_t CRCTable[256];

static void crc_init(void) {
uint16_t crc = 0x8000;
for (unsigned int i = 1; i < 256; i <<= 1) {
crc = (crc << 1) ^ (crc & 0x8000 ? GENPOLY : 0);
for (unsigned int j = 0; j < i; j++)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint16_t crc_table(uint16_t crc_initval, uint16_t data) {
uint16_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 2; ++i) {
uint16_t pos = (crc >> 8) ^ (data << (i << 3) >> 8);
crc = (crc << 8) ^ CRCTable[pos & 0xFF];
}

return crc;
}

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

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 16; ++i) {
uint16_t xor_crc_data = crc ^ data;
uint16_t crc_shl = crc << 1;
crc = (xor_crc_data & 0x8000) ? (crc_shl ^ GENPOLY) : crc_shl;
data <<= 1;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
static const uint16_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
for (size_t i = 0; i < 8; ++i) {
uint16_t actual = crc_loop(crc_initval[i], data[i]);
uint16_t expected = crc_table(crc_initval[i], data[i]);
if (actual != expected)
return 1;
}
return 0;
}
55 changes: 55 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc16.be.nodata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY 4129

static uint16_t CRCTable[256];

static void crc_init(void) {
uint16_t crc = 0x8000;
for (unsigned int i = 1; i < 256; i <<= 1) {
crc = (crc << 1) ^ (crc & 0x8000 ? GENPOLY : 0);
for (unsigned int j = 0; j < i; j++)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint16_t crc_table(uint16_t crc_initval) {
uint16_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 2; ++i) {
uint16_t pos = (crc >> 8);
crc = (crc << 8) ^ CRCTable[pos & 0xFF];
}

return crc;
}

static uint16_t crc_loop(uint16_t crc_initval) {
uint16_t crc = crc_initval;

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 16; ++i) {
uint16_t crc_shl = crc << 1;
crc = (crc & 0x8000) ? (crc_shl ^ GENPOLY) : crc_shl;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint16_t crc_initval[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
for (size_t i = 0; i < 8; ++i) {
uint16_t actual = crc_loop(crc_initval[i]);
uint16_t expected = crc_table(crc_initval[i]);
if (actual != expected)
return 1;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
58 changes: 58 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc16.le.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY -24575

static uint16_t CRCTable[256];

static void crc_init(void) {
uint16_t crc = 1;
for (unsigned int i = 128; i; i >>= 1) {
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
for (unsigned int j = 0; j < 256; j += 2 * i)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint16_t crc_table(uint16_t crc_initval, uint16_t data) {
uint16_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 2; ++i) {
uint16_t pos = crc ^ (data >> (i << 3));
crc = (crc >> 8) ^ CRCTable[pos & 0xFF];
}

return crc;
}

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

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 16; ++i) {
uint16_t xor_crc_data = crc ^ data;
uint16_t crc_lshr = crc >> 1;
crc = (xor_crc_data & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
data >>= 1;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint16_t crc_initval[8] = {0, 129, 11, 255, 16, 4129, 16384, 1};
static const uint16_t data[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
for (size_t i = 0; i < 8; ++i) {
uint16_t actual = crc_loop(crc_initval[i], data[i]);
uint16_t expected = crc_table(crc_initval[i], data[i]);
if (actual != expected)
return 1;
}
return 0;
}
55 changes: 55 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc16.le.nodata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY -24575

static uint16_t CRCTable[256];

static void crc_init(void) {
uint16_t crc = 1;
for (unsigned int i = 128; i; i >>= 1) {
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
for (unsigned int j = 0; j < 256; j += 2 * i)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint16_t crc_table(uint16_t crc_initval) {
uint16_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 2; ++i) {
uint16_t pos = crc & 0xFF;
crc = (crc >> 8) ^ CRCTable[pos];
}

return crc;
}

static uint16_t crc_loop(uint16_t crc_initval) {
uint16_t crc = crc_initval;

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 16; ++i) {
uint16_t crc_lshr = crc >> 1;
crc = (crc & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint16_t crc_initval[8] = {0, 1, 11, 16, 129, 255, 4129, 16384};
for (size_t i = 0; i < 8; ++i) {
uint16_t actual = crc_loop(crc_initval[i]);
uint16_t expected = crc_table(crc_initval[i]);
if (actual != expected)
return 1;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
58 changes: 58 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc8.be.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY 0x1D

static uint8_t CRCTable[256];

static void crc_init(void) {
uint8_t crc = 0x80;
for (unsigned int i = 1; i < 256; i <<= 1) {
crc = (crc << 1) ^ (crc & 0x80 ? GENPOLY : 0);
for (unsigned int j = 0; j < i; j++)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint8_t crc_table(uint8_t crc_initval, uint8_t data) {
uint8_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 1; ++i) {
uint8_t pos = crc ^ (data >> (i << 3));
crc = CRCTable[pos & 0xFF];
}

return crc;
}

static uint8_t crc_loop(uint8_t crc_initval, uint8_t data) {
uint8_t crc = crc_initval;

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 8; ++i) {
uint8_t xor_crc_data = crc ^ data;
uint8_t crc_shl = crc << 1;
crc = (xor_crc_data & 0x80) ? (crc_shl ^ GENPOLY) : crc_shl;
data <<= 1;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint8_t crc_initval[8] = {0, 129, 11, 255, 16, 142, 255, 1};
static const uint8_t data[8] = {0, 1, 11, 16, 129, 255, 142, 255};
for (size_t i = 0; i < 8; ++i) {
uint8_t actual = crc_loop(crc_initval[i], data[i]);
uint8_t expected = crc_table(crc_initval[i], data[i]);
if (actual != expected)
return 1;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
58 changes: 58 additions & 0 deletions SingleSource/UnitTests/HashRecognize/crc8.le.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdint.h>
#include <stdlib.h>

#define GENPOLY 0x1D

static uint8_t CRCTable[256];

static void crc_init(void) {
uint8_t crc = 1;
for (unsigned int i = 128; i; i >>= 1) {
crc = (crc >> 1) ^ (crc & 1 ? GENPOLY : 0);
for (unsigned int j = 0; j < 256; j += 2 * i)
CRCTable[i + j] = crc ^ CRCTable[j];
}
}

// This table-lookup should be equivalent to the code emitted when optimizing
// CRC with HashRecognize. This function itself will be untouched by
// HashRecognize.
static uint8_t crc_table(uint8_t crc_initval, uint8_t data) {
uint8_t crc = crc_initval;

if (CRCTable[255] == 0)
crc_init();

for (size_t i = 0; i < 1; ++i) {
uint8_t pos = crc ^ (data >> (i << 3));
crc = CRCTable[pos & 0xFF];
}

return crc;
}

static uint8_t crc_loop(uint8_t crc_initval, uint8_t data) {
uint8_t crc = crc_initval;

// This loop will be optimized by HashRecognize.
for (size_t i = 0; i < 8; ++i) {
uint8_t xor_crc_data = crc ^ data;
uint8_t crc_lshr = crc >> 1;
crc = (xor_crc_data & 1) ? (crc_lshr ^ GENPOLY) : crc_lshr;
data >>= 1;
}
return crc;
}

int main() {
// These are random hand-picked values.
static const uint8_t crc_initval[8] = {0, 129, 11, 255, 16, 142, 255, 1};
static const uint8_t data[8] = {0, 1, 11, 16, 129, 255, 142, 255};
for (size_t i = 0; i < 8; ++i) {
uint8_t actual = crc_loop(crc_initval[i], data[i]);
uint8_t expected = crc_table(crc_initval[i], data[i]);
if (actual != expected)
return 1;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
1 change: 1 addition & 0 deletions SingleSource/UnitTests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DIRS :=

DIRS += Vector
DIRS += Vectorizer
DIRS += HashRecognize

DIRS += SignlessTypes Threads C++11 Float

Expand Down