From 0acbdb98b296c6d5ac37a00da18ed74bf2346df9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 12 Dec 2019 20:42:37 +0000 Subject: [PATCH] Refactored CRC classes for better code sharing and reduced resource requirements. --- include/etl/private/crc16_poly_0x1021_.h | 44 +++++-------------- include/etl/private/crc16_poly_0x8005.h | 44 +++++-------------- include/etl/private/crc32_poly_0x04c11db7.h | 44 +++++-------------- include/etl/private/crc32_poly_0x1edc6f41.h | 44 +++++-------------- .../private/crc64_poly_0x42f0e1eba9ea3693.h | 42 +++++------------- include/etl/private/crc8_poly_0x07.h | 44 +++++-------------- 6 files changed, 72 insertions(+), 190 deletions(-) diff --git a/include/etl/private/crc16_poly_0x1021_.h b/include/etl/private/crc16_poly_0x1021_.h index 08cdcfab..793009d6 100644 --- a/include/etl/private/crc16_poly_0x1021_.h +++ b/include/etl/private/crc16_poly_0x1021_.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc16_table_poly_0x1021 { - protected: + public: - crc16_table_poly_0x1021() - : table(get_table()) - { - } - - static const uint16_t * const get_table() + //************************************************************************* + uint16_t add(uint16_t crc, uint8_t value) const { static const uint16_t table[256] = { @@ -80,23 +78,19 @@ namespace etl 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 }; - return table; + return (crc << 8) ^ table[((crc >> 8) ^ value) & 0xFF]; } - - const uint16_t * const table; }; + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc16_table_poly_0x1021_reflected { - protected: + public: - crc16_table_poly_0x1021_reflected() - : table(get_table()) - { - } - - static const uint16_t * const get_table() + //************************************************************************* + uint16_t add(uint16_t crc, uint8_t value) const { static const uint16_t table[256] = { @@ -118,10 +112,8 @@ namespace etl 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78 }; - return table; + return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; } - - const uint16_t * const table; }; //*************************************************************************** @@ -139,12 +131,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint16_t add(uint16_t crc, uint8_t value) const - { - return (crc << 8) ^ table[((crc >> 8) ^ value) & 0xFF]; - } - //************************************************************************* inline uint16_t final(uint16_t crc) const { @@ -166,12 +152,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint16_t add(uint16_t crc, uint8_t value) const - { - return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; - } - //************************************************************************* uint16_t final(uint16_t crc) const { diff --git a/include/etl/private/crc16_poly_0x8005.h b/include/etl/private/crc16_poly_0x8005.h index 44dfcf60..86f61343 100644 --- a/include/etl/private/crc16_poly_0x8005.h +++ b/include/etl/private/crc16_poly_0x8005.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc16_table_poly_0x8005 { - protected: + public: - crc16_table_poly_0x8005() - : table(get_table()) - { - } - - static const uint16_t * const get_table() + //************************************************************************* + uint16_t add(uint16_t crc, uint8_t value) const { static const uint16_t table[256] = { @@ -80,23 +78,19 @@ namespace etl 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231, 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202 }; - return table; + return (crc << 8) ^ table[((crc >> 8) ^ value) & 0xFF]; } - - const uint16_t * const table; }; + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc16_table_poly_0x8005_reflected { - protected: + public: - crc16_table_poly_0x8005_reflected() - : table(get_table()) - { - } - - static const uint16_t * const get_table() + //************************************************************************* + uint16_t add(uint16_t crc, uint8_t value) const { static const uint16_t table[256] = { @@ -118,10 +112,8 @@ namespace etl 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 }; - return table; + return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; } - - const uint16_t * const table; }; //*************************************************************************** @@ -139,12 +131,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint16_t add(uint16_t crc, uint8_t value) const - { - return (crc << 8) ^ table[((crc >> 8) ^ value) & 0xFF]; - } - //************************************************************************* inline uint16_t final(uint16_t crc) const { @@ -166,12 +152,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint16_t add(uint16_t crc, uint8_t value) const - { - return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; - } - //************************************************************************* inline uint16_t final(uint16_t crc) const { diff --git a/include/etl/private/crc32_poly_0x04c11db7.h b/include/etl/private/crc32_poly_0x04c11db7.h index cdbdb5f3..142d30e6 100644 --- a/include/etl/private/crc32_poly_0x04c11db7.h +++ b/include/etl/private/crc32_poly_0x04c11db7.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc32_table_poly_0x04c11db7 { - protected: + public: - crc32_table_poly_0x04c11db7() - : table(get_table()) - { - } - - static const uint32_t * const get_table() + //************************************************************************* + uint32_t add(uint32_t crc, uint8_t value) const { static const uint32_t table[256] = { @@ -80,23 +78,19 @@ namespace etl 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662, 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4 }; - return table; + return (crc << 8) ^ table[((crc >> 24) ^ value) & 0xFF]; } - - const uint32_t * const table; }; + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc32_table_poly_0x04c11db7_reflected { - protected: + public: - crc32_table_poly_0x04c11db7_reflected() - : table(get_table()) - { - } - - static const uint32_t * const get_table() + //************************************************************************* + uint32_t add(uint32_t crc, uint8_t value) const { static const uint32_t table[256] = { @@ -118,10 +112,8 @@ namespace etl 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; - return table; + return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; } - - const uint32_t * const table; }; //*************************************************************************** @@ -139,12 +131,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint32_t add(uint32_t crc, uint8_t value) const - { - return (crc << 8) ^ table[((crc >> 24) ^ value) & 0xFF]; - } - //************************************************************************* uint32_t final(uint32_t crc) const { @@ -166,12 +152,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint32_t add(uint32_t crc, uint8_t value) const - { - return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; - } - //************************************************************************* uint32_t final(uint32_t crc) const { diff --git a/include/etl/private/crc32_poly_0x1edc6f41.h b/include/etl/private/crc32_poly_0x1edc6f41.h index 51c6e323..969bdcc6 100644 --- a/include/etl/private/crc32_poly_0x1edc6f41.h +++ b/include/etl/private/crc32_poly_0x1edc6f41.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc32_table_poly_0x1edc6f41 { - protected: + public: - crc32_table_poly_0x1edc6f41() - : table(get_table()) - { - } - - static const uint32_t * const get_table() + //************************************************************************* + uint32_t add(uint32_t crc, uint8_t value) const { static const uint32_t table[256] = { @@ -80,23 +78,19 @@ namespace etl 0xAEF676CF, 0x6E37A080, 0x41F8AA48, 0x81397C07, 0x561F2F2C, 0x96DEF963, 0xB911F3AB, 0x79D025E4, 0x5DECED9E, 0x9D2D3BD1, 0xB2E23119, 0x7223E756, 0xA505B47D, 0x65C46232, 0x4A0B68FA, 0x8ACABEB5 }; - return table; + return (crc << 8) ^ table[((crc >> 24) ^ value) & 0xFF]; } - - const uint32_t * const table; }; + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc32_table_poly_0x1edc6f41_reflected { - protected: + public: - crc32_table_poly_0x1edc6f41_reflected() - : table(get_table()) - { - } - - static const uint32_t * const get_table() + //************************************************************************* + uint32_t add(uint32_t crc, uint8_t value) const { static const uint32_t table[256] = { @@ -118,10 +112,8 @@ namespace etl 0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E, 0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351 }; - return table; + return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; } - - const uint32_t * const table; }; //*************************************************************************** @@ -139,12 +131,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint32_t add(uint32_t crc, uint8_t value) const - { - return (crc << 8) ^ table[((crc >> 24) ^ value) & 0xFF]; - } - //************************************************************************* uint32_t final(uint32_t crc) const { @@ -166,12 +152,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint32_t add(uint32_t crc, uint8_t value) const - { - return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; - } - //************************************************************************* uint32_t final(uint32_t crc) const { diff --git a/include/etl/private/crc64_poly_0x42f0e1eba9ea3693.h b/include/etl/private/crc64_poly_0x42f0e1eba9ea3693.h index c1760ddd..83f46d48 100644 --- a/include/etl/private/crc64_poly_0x42f0e1eba9ea3693.h +++ b/include/etl/private/crc64_poly_0x42f0e1eba9ea3693.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc64_table_poly_0x42f0e1eba9ea3693 { - protected: + public: - crc64_table_poly_0x42f0e1eba9ea3693() - : table(get_table()) - { - } - - static const uint64_t * const get_table() + //************************************************************************* + uint64_t add(uint64_t crc, uint8_t value) const { static const uint64_t table[256] = { @@ -96,21 +94,19 @@ namespace etl 0x14DEA25F3AF9026D, 0x562E43B4931334FE, 0x913F6188692D6F4B, 0xD3CF8063C0C759D8, 0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507 }; - return table; + return (crc << 8) ^ table[((crc >> 56) ^ value) & 0xFF]; } - - const uint64_t * const table; }; + //*************************************************************************** + /// Table and add value. //*************************************************************************** class crc64_table_poly_0x42f0e1eba9ea3693_reflected { - crc64_table_poly_0x42f0e1eba9ea3693_reflected() - : table(get_table()) - { - } + public: - static const uint64_t * const get_table() + //************************************************************************* + uint64_t add(uint64_t crc, uint8_t value) const { static const uint64_t table[256] = { @@ -148,10 +144,8 @@ namespace etl 0xB6409F5CFA457B28, 0x7F2CC8C92DC2746A, 0xD2F6B4961186FC89, 0x1B9AE303C601F3CB, 0x4D77DD2C5823B7BA, 0x841B8AB98FA4B8F8, 0x29C1F6E6B3E0301B, 0xE0ADA17364673F59 }; - return table; + return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; } - - const uint64_t * const table; }; //*************************************************************************** @@ -169,12 +163,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint64_t add(uint64_t crc, uint8_t value) const - { - return (crc << 8) ^ table[((crc >> 56) ^ value) & 0xFF]; - } - //************************************************************************* uint64_t final(uint64_t crc) const { @@ -196,12 +184,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint64_t add(uint64_t crc, uint8_t value) const - { - return (crc >> 8) ^ table[(crc ^ value) & 0xFF]; - } - //************************************************************************* uint64_t final(uint64_t crc) const { diff --git a/include/etl/private/crc8_poly_0x07.h b/include/etl/private/crc8_poly_0x07.h index 181fcc99..558ea003 100644 --- a/include/etl/private/crc8_poly_0x07.h +++ b/include/etl/private/crc8_poly_0x07.h @@ -48,17 +48,15 @@ SOFTWARE. namespace etl { + //*************************************************************************** + /// //*************************************************************************** class crc8_table_poly_0x07 { - protected: + public: - crc8_table_poly_0x07() - : table(get_table()) - { - } - - static const uint8_t * const get_table() + //************************************************************************* + uint8_t add(uint8_t crc, uint8_t value) const { static const uint8_t table[256] = { @@ -80,23 +78,19 @@ namespace etl 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 }; - return table; + return table[crc ^ value]; } - - const uint8_t * const table; }; + //*************************************************************************** + /// //*************************************************************************** class crc8_table_poly_0x07_reflected { - protected: + public: - crc8_table_poly_0x07_reflected() - : table(get_table()) - { - } - - static const uint8_t * const get_table() + //************************************************************************* + uint8_t add(uint8_t crc, uint8_t value) const { static const uint8_t table[256] = { @@ -118,10 +112,8 @@ namespace etl 0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15, 0x3B, 0x0A, 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC }; - return table; + return table[crc ^ value]; } - - const uint8_t * const table; }; //*************************************************************************** @@ -139,12 +131,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint8_t add(uint8_t crc, uint8_t value) const - { - return table[crc ^ value]; - } - //************************************************************************* uint8_t final(uint8_t crc) const { @@ -166,12 +152,6 @@ namespace etl return INITIAL; } - //************************************************************************* - uint8_t add(uint8_t crc, uint8_t value) const - { - return table[crc ^ value]; - } - //************************************************************************* uint8_t final(uint8_t crc) const {