///\file /****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2021 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_CRC_IMPLEMENTATION_INCLUDED #define ETL_CRC_IMPLEMENTATION_INCLUDED #include "../platform.h" #include "../frame_check_sequence.h" #include "../static_assert.h" #include "../binary.h" #include "../type_traits.h" #include "stdint.h" #include "crc_parameters.h" #if defined(ETL_COMPILER_KEIL) #pragma diag_suppress 1300 #endif namespace etl { namespace private_crc { //***************************************************************************** /// CRC Partial Table Entry //***************************************************************************** template class crc_partial_table_entry { private: static ETL_CONSTANT bool Do_Poly = Reflect ? (Entry & TAccumulator(1U)) != 0U : (Entry & (TAccumulator(1U) << (Accumulator_Bits - 1U))) != 0U; public: static ETL_CONSTANT TAccumulator value = Reflect ? TAccumulator(Do_Poly ? (Entry >> 1U) ^ etl::reverse_bits_const::value : (Entry >> 1U)) : TAccumulator(Do_Poly ? (Entry << 1U) ^ Polynomial : (Entry << 1U)); }; template ETL_CONSTANT TAccumulator crc_partial_table_entry::value; //***************************************************************************** /// CRC Table Entry //***************************************************************************** template class crc_table_entry { ETL_STATIC_ASSERT((Chunk_Bits == 2U) || (Chunk_Bits == 4U) || (Chunk_Bits == 8U), "Chunk bits must be 2, 4 or 8"); }; //********************************* // Chunk bit size of 2. template class crc_table_entry { public: static ETL_CONSTANT size_t Shift_Bits = size_t(Accumulator_Bits - 2U); static ETL_CONSTANT TAccumulator Entry = Reflect ? TAccumulator(Index) : TAccumulator(TAccumulator(Index) << Shift_Bits); static ETL_CONSTANT TAccumulator value = crc_partial_table_entry::value>::value; }; template ETL_CONSTANT size_t crc_table_entry::Shift_Bits; template ETL_CONSTANT TAccumulator crc_table_entry::Entry; template ETL_CONSTANT TAccumulator crc_table_entry::value; //********************************* // Chunk bit size of 4. template class crc_table_entry { public: static ETL_CONSTANT size_t Shift_Bits = size_t(Accumulator_Bits - 4U); static ETL_CONSTANT TAccumulator Entry = Reflect ? TAccumulator(Index) : TAccumulator(TAccumulator(Index) << Shift_Bits); static ETL_CONSTANT TAccumulator value = crc_partial_table_entry::value>::value>::value>::value; }; template ETL_CONSTANT size_t crc_table_entry::Shift_Bits; template ETL_CONSTANT TAccumulator crc_table_entry::Entry; template ETL_CONSTANT TAccumulator crc_table_entry::value; //********************************* // Chunk bit size of 8. template class crc_table_entry { public: static ETL_CONSTANT size_t Shift_Bits = size_t(Accumulator_Bits - 8U); static ETL_CONSTANT TAccumulator Entry = Reflect ? TAccumulator(Index) : TAccumulator(TAccumulator(Index) << Shift_Bits); static ETL_CONSTANT TAccumulator value = crc_partial_table_entry::value>::value>::value>::value>::value>::value>::value>::value; }; template ETL_CONSTANT size_t crc_table_entry::Shift_Bits; template ETL_CONSTANT TAccumulator crc_table_entry::Entry; template ETL_CONSTANT TAccumulator crc_table_entry::value; //***************************************************************************** /// CRC Update Chunk //***************************************************************************** //********************************* // Accumulator_Bits > Chunk_Bits // Not Reflected template static typename etl::enable_if<(Accumulator_Bits > Chunk_Bits) && !Reflect, TAccumulator>::type crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[]) { value &= Chunk_Mask; uint8_t index = (crc >> (Accumulator_Bits - Chunk_Bits)) ^ value; crc <<= Chunk_Bits; crc ^= table[index]; return crc; } //********************************* // Accumulator_Bits > Chunk_Bits // Reflected template static typename etl::enable_if<(Accumulator_Bits > Chunk_Bits) && Reflect, TAccumulator>::type crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[]) { value &= Chunk_Mask; uint8_t index = (crc & Chunk_Mask) ^ value; crc >>= Chunk_Bits; crc ^= table[index]; return crc; } //********************************* // Accumulator_Bits == Chunk_Bits // Not Reflected template static typename etl::enable_if<(Accumulator_Bits == Chunk_Bits) && !Reflect, TAccumulator>::type crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[]) { value &= Chunk_Mask; uint8_t index = (crc >> (Accumulator_Bits - Chunk_Bits)) ^ value; crc = table[index]; return crc; } //********************************* // Accumulator_Bits == Chunk_Bits // Reflected template static typename etl::enable_if<(Accumulator_Bits == Chunk_Bits) && Reflect, TAccumulator>::type crc_update_chunk(TAccumulator crc, uint8_t value, const TAccumulator table[]) { value &= Chunk_Mask; uint8_t index = (crc & Chunk_Mask) ^ value; crc = table[index]; return crc; } //***************************************************************************** // CRC Tables. //***************************************************************************** template struct crc_table; //********************************* // Table size of 4. template struct crc_table { //************************************************************************* TAccumulator add(TAccumulator crc, uint8_t value) const { static ETL_CONSTANT TAccumulator table[4U] = { crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value }; if ETL_IF_CONSTEXPR(Reflect) { crc = crc_update_chunk(crc, value, table); crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 1U)), table); crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 2U)), table); crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 3U)), table); } else { crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 3U)), table); crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 2U)), table); crc = crc_update_chunk(crc, (value >> (Chunk_Bits * 1U)), table); crc = crc_update_chunk(crc, value, table); } return crc; } }; //********************************* // Table size of 16. template struct crc_table { //************************************************************************* TAccumulator add(TAccumulator crc, uint8_t value) const { static ETL_CONSTANT TAccumulator table[16U] = { crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value }; if ETL_IF_CONSTEXPR(Reflect) { crc = crc_update_chunk(crc, value, table); crc = crc_update_chunk(crc, value >> Chunk_Bits, table); } else { crc = crc_update_chunk(crc, value >> Chunk_Bits, table); crc = crc_update_chunk(crc, value, table); } return crc; } }; //********************************* // Table size of 256. template struct crc_table { //************************************************************************* TAccumulator add(TAccumulator crc, uint8_t value) const { static ETL_CONSTANT TAccumulator table[256U] = { crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value, crc_table_entry::value }; crc = crc_update_chunk(crc, value, table); return crc; } }; //***************************************************************************** // CRC Policies. //***************************************************************************** template struct crc_policy; //********************************* // Policy for 256 entry table. template struct crc_policy : public crc_table { typedef typename TCrcParameters::accumulator_type accumulator_type; typedef accumulator_type value_type; //************************************************************************* ETL_CONSTEXPR accumulator_type initial() const { return TCrcParameters::Reflect ? etl::reverse_bits_const::value : TCrcParameters::Initial; } //************************************************************************* accumulator_type final(accumulator_type crc) const { return crc ^ TCrcParameters::Xor_Out; } }; //********************************* // Policy for 16 entry table. template struct crc_policy : public crc_table { typedef typename TCrcParameters::accumulator_type accumulator_type; typedef accumulator_type value_type; //************************************************************************* ETL_CONSTEXPR accumulator_type initial() const { return TCrcParameters::Reflect ? etl::reverse_bits_const::value : TCrcParameters::Initial; } //************************************************************************* accumulator_type final(accumulator_type crc) const { return crc ^ TCrcParameters::Xor_Out; } }; //********************************* // Policy for 4 entry table. template struct crc_policy : public crc_table { typedef typename TCrcParameters::accumulator_type accumulator_type; typedef accumulator_type value_type; //************************************************************************* ETL_CONSTEXPR accumulator_type initial() const { return TCrcParameters::Reflect ? etl::reverse_bits_const::value : TCrcParameters::Initial; } //************************************************************************* accumulator_type final(accumulator_type crc) const { return crc ^ TCrcParameters::Xor_Out; } }; } //***************************************************************************** /// Basic parameterised CRC type. //***************************************************************************** template class crc_type : public etl::frame_check_sequence > { public: ETL_STATIC_ASSERT((Table_Size == 4U) || (Table_Size == 16U) || (Table_Size == 256U), "Table size must be 4, 16 or 256"); //************************************************************************* /// Default constructor. //************************************************************************* crc_type() { this->reset(); } //************************************************************************* /// Constructor from range. /// \param begin Start of the range. /// \param end End of the range. //************************************************************************* template crc_type(TIterator begin, const TIterator end) { this->reset(); this->add(begin, end); } }; } #endif