pabigot
0.1.1
C++ support classes
|
Go to the documentation of this file. 8 #ifndef PABIGOT_CRC_HPP 9 #define PABIGOT_CRC_HPP 16 #include <type_traits> 47 template <
typename UI>
52 UI msb = (static_cast<UI>(1) << (bits - 1));
72 template <
unsigned int B>
75 return (B<=8) + (B<=16) + (B<=32) + (B<=64);
96 template <
unsigned int C>
103 static constexpr
unsigned int width = 8;
104 using fast_type = uint_fast8_t;
105 using least_type = uint_least8_t;
111 static constexpr
unsigned int width = 16;
112 using fast_type = uint_fast16_t;
113 using least_type = uint_least16_t;
119 static constexpr
unsigned int width = 32;
120 using fast_type = uint_fast32_t;
121 using least_type = uint_least32_t;
127 static constexpr
unsigned int width = 64;
128 using fast_type = uint_fast64_t;
129 using least_type = uint_least64_t;
138 template <
unsigned int C>
142 using typename super::fast_type;
143 using typename super::least_type;
144 using fast_limits = std::numeric_limits<fast_type>;
149 static constexpr fast_type
153 return reverse_low_bits<fast_type>(v, bits);
167 static constexpr fast_type
170 return ((bits == fast_limits::digits)
171 ? static_cast<fast_type>(-1)
172 : ((static_cast<fast_type>(1) << bits) - 1));
189 template <
unsigned int B>
197 static constexpr
unsigned int width = B;
215 static constexpr
unsigned int fast_width = std::numeric_limits<fast_type>::digits;
218 static constexpr
unsigned int least_width = std::numeric_limits<least_type>::digits;
241 unsigned int n =
width)
264 const unsigned int n)
267 rv ^= msg << (
width - n);
268 for (
unsigned int bi{}; bi < n; ++bi) {
269 bool xor_poly{
msbit & rv};
281 template <
typename CRC,
285 using least_type =
typename CRC::least_type;
286 return std::array<least_type,
sizeof...(n)>{{CRC::lookup_for_byte(n)...}};
295 template <
typename CRC>
298 return lookup_table<CRC>(std::make_integer_sequence<size_t, 256>{});
309 template <
unsigned int W,
315 static constexpr
unsigned int width = W;
381 static constexpr uint8_t*
391 *bp++ =
crc >> (8 * nb);
412 std::uint_fast8_t
byte)
479 template <
typename CRC>
482 using super_ =
typename CRC::uint_traits;
489 using typename super_::fast_type;
492 using typename super_::least_type;
501 static constexpr
unsigned int width = CRC::width;
503 static constexpr least_type
poly = CRC::poly;
505 static constexpr least_type
init = CRC::init;
507 static constexpr least_type
xorout = CRC::xorout;
509 static constexpr
bool refin = CRC::refin;
511 static constexpr
bool refout = CRC::refout;
515 static constexpr
size_t size = CRC::size;
523 static constexpr fast_type
526 fast_type
crc = CRC::init;
531 crc = super_::reverse(
crc, CRC::width);
540 table{details::lookup_table<CRC>()}
543 Tabler (
const Tabler&) =
delete;
544 Tabler& operator= (
const Tabler&) =
delete;
545 Tabler (
const Tabler&&) =
delete;
546 Tabler& operator= (
const Tabler&&) =
delete;
569 *bp++ =
crc >> (8 * nb);
590 constexpr fast_type index_mask = 0xFFu;
594 crc =
table[((
crc >> (CRC::width - 8)) ^ octet) & index_mask] ^ (
crc << 8);
596 return CRC::mask &
crc;
616 template <
typename InputIterator>
622 using src_type =
typename std::iterator_traits<InputIterator>::value_type;
623 using limits = std::numeric_limits<src_type>;
626 constexpr
auto bits_per_chunk = limits::digits + (limits::is_signed ? 1U : 0U);
627 static_assert(bits_per_chunk == 8,
628 "cannot do table-driven from non-octet source");
631 while (last != first) {
632 rv =
append(static_cast<uint8_t>(*first), rv);
651 if (CRC::refin ^ CRC::refout) {
652 crc = super_::reverse(
crc, CRC::width);
654 return static_cast<least_type>(
crc);
658 static constexpr fast_type
init = make_init();
662 static constexpr least_type
residue = CRC::residue();
695 template <
unsigned int W,
700 uintmax_t XorOut = 0>
775 template <
typename InputIterator>
781 using src_type =
typename std::iterator_traits<InputIterator>::value_type;
782 constexpr
auto bits_per_chunk =
783 std::numeric_limits<src_type>::digits
784 + (std::numeric_limits<src_type>::is_signed ? 1U : 0U);
786 "cannot aggregate from source exceeding CRC operating type capacity");
788 while (last != first) {
792 fast_type chunk = static_cast<least_type>(*first);
830 return static_cast<least_type>(
crc);
844 using CRC32 =
crc<32, 0x04c11db7,
true,
true, -1, -1>;
CRC calculation using Rocksoft^tm Model characteristics.
Definition: crc.hpp:701
static constexpr fast_type lsbit
Mask isolating the least significant bit in a value.
Definition: crc.hpp:228
static constexpr bool refout
true iff the output CRC is to be reflected.
Definition: crc.hpp:717
static constexpr bool refin
true iff input data is to be reflected
Definition: crc.hpp:509
constexpr fast_type append(InputIterator first, InputIterator last, fast_type crc=init) const
Table-driven calculation of a CRC from a sequence of octet values.
Definition: crc.hpp:618
static constexpr unsigned int width
The width of the CRC in bits.
Definition: crc.hpp:315
static constexpr fast_type crc_apply(const fast_type &poly, const fast_type &crc, const fast_type &msg, const unsigned int n)
Apply message bits into the CRC.
Definition: crc.hpp:261
static constexpr unsigned int least_width
The number of bits used in least_type, should that be useful.
Definition: crc.hpp:218
static constexpr fast_type poly
The CRC polynomial in normal form (all bits except bit width which is known to be 1 are provided).
Definition: crc.hpp:748
static constexpr least_type xorout
A value subtracted (xor'd) from the calculated result prior to returning it.
Definition: crc.hpp:756
static constexpr fast_type reverse(const fast_type &v, unsigned int bits)
Reverse bits [0..bits) of v.
Definition: crc.hpp:150
static constexpr fast_type mask
The mask that discards bits of a fast_type that would be outside the width limit.
Definition: crc.hpp:344
Support for byte order manipulation and packed storage.
constexpr unsigned int uint_category()
Compute the #uint_size_traits category value for a required bit length.
Definition: crc.hpp:73
typename uint_traits::fast_type fast_type
The type used to hold values while they're being operated on.
Definition: crc.hpp:207
details::uint_support< width > support_traits
Traits class providing native integral types and parameter-independent values supporting width-bit op...
Definition: crc.hpp:323
CRC crc_type
The underlying crc type.
Definition: crc.hpp:486
static constexpr bool refout
true iff the final CRC is to be reflected
Definition: crc.hpp:511
static constexpr unsigned int size
The width of the CRC in bytes.
Definition: crc.hpp:318
constexpr auto lookup_table(std::index_sequence< n... >)
Use a template parameter pack to fill out a 256-element std::initializer_list for a std::array.
Definition: crc.hpp:283
constexpr least_type finalize(fast_type crc) const
Perform necessary post-processing to get the final checksum.
Definition: crc.hpp:644
static constexpr least_type init
CRC initial value before message bits are processed.
Definition: crc.hpp:505
typename support_traits::least_type least_type
A small native unsigned integral type capable of holding width-bit CRC values.
Definition: crc.hpp:340
typename uint_traits::least_type least_type
The type used to hold values while they're being stored.
Definition: crc.hpp:212
static constexpr least_type residue
The value expected when calculating the finalized CRC over an aggregate of a message and its store()d...
Definition: crc.hpp:662
Extend the size-related traits with functionality operating on the specific types.
Definition: crc.hpp:139
static constexpr fast_type reflect(const fast_type &v, unsigned int n=width)
Reflect a value around its center.
Definition: crc.hpp:240
static constexpr bool refin
true iff the input message bit stream is reflected.
Definition: crc.hpp:359
static constexpr size_t size
The number of bytes required to store a CRC value of #width bits.
Definition: crc.hpp:515
Entry-point to identifying unsigned integral types supporting a specific number of bits.
Definition: crc.hpp:97
static constexpr unsigned int width
Number of bits in the CRC.
Definition: crc.hpp:501
uint8_t * store(least_type crc, uint8_t *bp) const
Store a finalized CRC into a buffer in a way that enables crc::residue().
Definition: crc.hpp:560
static constexpr fast_type mask
A mask value that will discard bits at and above bit number width in a value.
Definition: crc.hpp:222
std::array< least_type, 256 > table_type
How the CRC table elements are stored.
Definition: crc.hpp:495
static constexpr UI reverse_low_bits(const UI &v, unsigned int bits)
Reverse bits [0..bits) of v.
Definition: crc.hpp:49
static constexpr least_type residue()
Value expected for CRC(x || XE(CRC(x))).
Definition: crc.hpp:737
Root for all pabigot namespaces.
Definition: gap.hpp:15
static constexpr uint8_t * store(fast_type crc, uint8_t *bp)
Store a finalized CRC into a buffer in a way that enables crc::residue().
Definition: crc.hpp:382
constexpr fast_type append(uint8_t octet, fast_type crc=init) const
Table-driven update of a CRC given a data octet.
Definition: crc.hpp:587
Rocksoft^TM model parameters for the checksum algorithm supported by this type.
Definition: crc.hpp:499
static constexpr unsigned int width
The number of bits for which this class is targeted.
Definition: crc.hpp:197
const table_type table
The CRC table, indexed by unreflected input octet.
Definition: crc.hpp:665
static constexpr least_type lookup_for_byte(const fast_type &poly, std::uint_fast8_t byte)
Calculate a single lookup table entry using the given polynomial.
Definition: crc.hpp:411
static constexpr fast_type init
The CRC initial value in the form required for table calculations.
Definition: crc.hpp:658
static constexpr fast_type msbit
Mask isolating the most significant bit in a value.
Definition: crc.hpp:225
static constexpr fast_type append(InputIterator first, InputIterator last, const fast_type &crc=init)
Augment a checksum with additional data.
Definition: crc.hpp:777
static constexpr least_type init
The initial value of the CRC register before any message bits have been processed.
Definition: crc.hpp:752
static constexpr least_type lookup_for_byte(std::uint_fast8_t byte)
Delegate to super_::lookup_for_byte.
Definition: crc.hpp:804
static constexpr least_type finalize(fast_type crc)
Calculate the final CRC value for a sequence.
Definition: crc.hpp:824
Class encapsulating everything necessary for table-driven CRC calculations.
Definition: crc.hpp:480
static constexpr fast_type mask_for_bits(unsigned int bits)
Helper to calculate the value of a B-bit mask as an instance of the unsigned type T.
Definition: crc.hpp:168
static constexpr unsigned int fast_width
The number of bits used in fast_type, should that be useful.
Definition: crc.hpp:215
static constexpr least_type poly
CRC polynomial.
Definition: crc.hpp:503
typename support_traits::fast_type fast_type
A fast native unsigned integral type capable of holding width-bit CRC values.
Definition: crc.hpp:333
static constexpr tabler_type instantiate_tabler()
Construct an object that does table-driven CRC calculations for this algorithm.
Definition: crc.hpp:835
static constexpr fast_type reflect(const fast_type &v)
Delegate to support_traits::reflect.
Definition: crc.hpp:363
CRC core framework using Rocksoft^tm Model characteristics.
Definition: crc.hpp:311
Type and policy traits providing support for operations on B-bit values.
Definition: crc.hpp:190
static constexpr least_type xorout
Value used to mutate unreflected final CRC.
Definition: crc.hpp:507
static constexpr fast_type crc_apply(const fast_type &poly, const fast_type &crc, fast_type msg, unsigned int n)
Apply message bits into a CRC.
Definition: crc.hpp:440
details::uint_traits< uint_category< B >()> uint_traits
Characteristics of the unsigned integer support required for a width-bit value.
Definition: crc.hpp:201