diff --git a/SingleSource/UnitTests/CMakeLists.txt b/SingleSource/UnitTests/CMakeLists.txt index c04bd2347b..9c43e84955 100644 --- a/SingleSource/UnitTests/CMakeLists.txt +++ b/SingleSource/UnitTests/CMakeLists.txt @@ -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) diff --git a/SingleSource/UnitTests/HashRecognize/CMakeLists.txt b/SingleSource/UnitTests/HashRecognize/CMakeLists.txt new file mode 100644 index 0000000000..630458a9a5 --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/CMakeLists.txt @@ -0,0 +1 @@ +llvm_singlesource() diff --git a/SingleSource/UnitTests/HashRecognize/Makefile b/SingleSource/UnitTests/HashRecognize/Makefile new file mode 100644 index 0000000000..bbfea18952 --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/Makefile @@ -0,0 +1,5 @@ +# SingleSource/UnitTests/HashRecognize/Makefile +LEVEL = ../../.. + +include $(LEVEL)/Makefile.config +include $(LEVEL)/SingleSource/Makefile.singlesrc diff --git a/SingleSource/UnitTests/HashRecognize/crc16.be.c b/SingleSource/UnitTests/HashRecognize/crc16.be.c new file mode 100644 index 0000000000..dc457e59e6 --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.be.c @@ -0,0 +1,58 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.c b/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.c new file mode 100644 index 0000000000..6d009d21de --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.c @@ -0,0 +1,55 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.reference_output b/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.be.nodata.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/HashRecognize/crc16.be.reference_output b/SingleSource/UnitTests/HashRecognize/crc16.be.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.be.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/HashRecognize/crc16.le.c b/SingleSource/UnitTests/HashRecognize/crc16.le.c new file mode 100644 index 0000000000..1b338cf320 --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.le.c @@ -0,0 +1,58 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.c b/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.c new file mode 100644 index 0000000000..4a29741840 --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.c @@ -0,0 +1,55 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.reference_output b/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.le.nodata.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/HashRecognize/crc16.le.reference_output b/SingleSource/UnitTests/HashRecognize/crc16.le.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc16.le.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/HashRecognize/crc8.be.c b/SingleSource/UnitTests/HashRecognize/crc8.be.c new file mode 100644 index 0000000000..31f44405ae --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc8.be.c @@ -0,0 +1,58 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc8.be.reference_output b/SingleSource/UnitTests/HashRecognize/crc8.be.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc8.be.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/HashRecognize/crc8.le.c b/SingleSource/UnitTests/HashRecognize/crc8.le.c new file mode 100644 index 0000000000..263913a37e --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc8.le.c @@ -0,0 +1,58 @@ +#include +#include + +#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; +} diff --git a/SingleSource/UnitTests/HashRecognize/crc8.le.reference_output b/SingleSource/UnitTests/HashRecognize/crc8.le.reference_output new file mode 100644 index 0000000000..ca916d098d --- /dev/null +++ b/SingleSource/UnitTests/HashRecognize/crc8.le.reference_output @@ -0,0 +1 @@ +exit 0 diff --git a/SingleSource/UnitTests/Makefile b/SingleSource/UnitTests/Makefile index cacb4b8740..65a0c9b994 100644 --- a/SingleSource/UnitTests/Makefile +++ b/SingleSource/UnitTests/Makefile @@ -9,6 +9,7 @@ DIRS := DIRS += Vector DIRS += Vectorizer +DIRS += HashRecognize DIRS += SignlessTypes Threads C++11 Float