CRC++ 1.0.1.0
Easy to use and fast C++ CRC library.
Loading...
Searching...
No Matches
CRC.h
Go to the documentation of this file.
1
37
38/*
39 CRC++ can be configured by setting various #defines before #including this header file:
40
41 #define crcpp_uint8 - Specifies the type used to store CRCs that have a width of 8 bits or less.
42 This type is not used in CRC calculations. Defaults to ::std::uint8_t.
43 #define crcpp_uint16 - Specifies the type used to store CRCs that have a width between 9 and 16 bits (inclusive).
44 This type is not used in CRC calculations. Defaults to ::std::uint16_t.
45 #define crcpp_uint32 - Specifies the type used to store CRCs that have a width between 17 and 32 bits (inclusive).
46 This type is not used in CRC calculations. Defaults to ::std::uint32_t.
47 #define crcpp_uint64 - Specifies the type used to store CRCs that have a width between 33 and 64 bits (inclusive).
48 This type is not used in CRC calculations. Defaults to ::std::uint64_t.
49 #define crcpp_size - This type is used for loop iteration and function signatures only. Defaults to ::std::size_t.
50 #define CRCPP_USE_NAMESPACE - Define to place all CRC++ code within the ::CRCPP namespace.
51 #define CRCPP_BRANCHLESS - Define to enable a branchless CRC implementation. The branchless implementation uses a single integer
52 multiplication in the bit-by-bit calculation instead of a small conditional. The branchless implementation
53 may be faster on processor architectures which support single-instruction integer multiplication.
54 #define CRCPP_USE_CPP11 - Define to enables C++11 features (move semantics, constexpr, static_assert, etc.).
55 #define CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS - Define to include definitions for little-used CRCs.
56*/
57
58#ifndef CRCPP_CRC_H_
59#define CRCPP_CRC_H_
60
61#if __cplusplus >= 201103L && !defined(CRCPP_USE_CPP11)
62#define CRCPP_USE_CPP11
63#endif
64
65#include <climits> // Includes CHAR_BIT
66#ifdef CRCPP_USE_CPP11
67#include <cstddef> // Includes ::std::size_t
68#include <cstdint> // Includes ::std::uint8_t, ::std::uint16_t, ::std::uint32_t, ::std::uint64_t
69#else
70#include <stddef.h> // Includes size_t
71#include <stdint.h> // Includes uint8_t, uint16_t, uint32_t, uint64_t
72#endif
73#include <limits> // Includes ::std::numeric_limits
74#include <utility> // Includes ::std::move
75
76#ifndef crcpp_uint8
77# ifdef CRCPP_USE_CPP11
79# define crcpp_uint8 ::std::uint8_t
80# else
82# define crcpp_uint8 uint8_t
83# endif
84#endif
85
86#ifndef crcpp_uint16
87# ifdef CRCPP_USE_CPP11
89# define crcpp_uint16 ::std::uint16_t
90# else
92# define crcpp_uint16 uint16_t
93# endif
94#endif
95
96#ifndef crcpp_uint32
97# ifdef CRCPP_USE_CPP11
99# define crcpp_uint32 ::std::uint32_t
100# else
102# define crcpp_uint32 uint32_t
103# endif
104#endif
105
106#ifndef crcpp_uint64
107# ifdef CRCPP_USE_CPP11
109# define crcpp_uint64 ::std::uint64_t
110# else
112# define crcpp_uint64 uint64_t
113# endif
114#endif
115
116#ifndef crcpp_size
117# ifdef CRCPP_USE_CPP11
119# define crcpp_size ::std::size_t
120# else
122# define crcpp_size size_t
123# endif
124#endif
125
126#ifdef CRCPP_USE_CPP11
128# define crcpp_constexpr constexpr
129#else
131# define crcpp_constexpr const
132#endif
133
134#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
135/* Disable warning C4127: conditional expression is constant. */
136#pragma warning(push)
137#pragma warning(disable : 4127)
138#endif
139
140#ifdef CRCPP_USE_NAMESPACE
141namespace CRCPP
142{
143#endif
144
145#ifdef CRCPP_USE_CPP11
150crcpp_constexpr char CRCPP_COPYRIGHT[] = "Copyright (c) 2022-2025, Daniel Bahr";
151#else
152#define CRCPP_MAJOR_VERSION 1
153#define CRCPP_MINOR_VERSION 2
154#define CRCPP_PATCH_VERSION 1
155#define CRCPP_REVISION_VERSION 0
156#define CRCPP_COPYRIGHT "Copyright (c) 2022-2025, Daniel Bahr"
157#endif
158
166class CRC
167{
168public:
169 // Forward declaration
170 template <typename CRCType, crcpp_uint16 CRCWidth>
171 struct Table;
172
176 template <typename CRCType, crcpp_uint16 CRCWidth>
178 {
179 CRCType polynomial;
180 CRCType initialValue;
181 CRCType finalXOR;
184
186 };
187
192 template <typename CRCType, crcpp_uint16 CRCWidth>
193 struct Table
194 {
195 // Constructors are intentionally NOT marked explicit.
197
198#ifdef CRCPP_USE_CPP11
200#endif
201
203
204 const CRCType * GetTable() const;
205
206 CRCType operator[](unsigned char index) const;
207
208 private:
209 void InitTable();
210
212 CRCType table[1 << CHAR_BIT];
213 };
214
215 // The number of bits in CRCType must be at least as large as CRCWidth.
216 // CRCType must be an unsigned integer type or a custom type with operator overloads.
217 template <typename CRCType, crcpp_uint16 CRCWidth>
218 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
219
220 template <typename CRCType, crcpp_uint16 CRCWidth>
221 static CRCType Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
222
223 template <typename CRCType, crcpp_uint16 CRCWidth>
224 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
225
226 template <typename CRCType, crcpp_uint16 CRCWidth>
227 static CRCType Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
228
229 template <typename CRCType, crcpp_uint16 CRCWidth>
230 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters);
231
232 template <typename CRCType, crcpp_uint16 CRCWidth>
233 static CRCType CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc);
234
235 template <typename CRCType, crcpp_uint16 CRCWidth>
236 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable);
237
238 template <typename CRCType, crcpp_uint16 CRCWidth>
239 static CRCType CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc);
240
241 // Common CRCs up to 64 bits.
242 // Note: Check values are the computed CRCs when given an ASCII input of "123456789" (without null terminator)
243#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
244 static const Parameters< crcpp_uint8, 4> & CRC_4_ITU();
245 static const Parameters< crcpp_uint8, 5> & CRC_5_EPC();
246 static const Parameters< crcpp_uint8, 5> & CRC_5_ITU();
247 static const Parameters< crcpp_uint8, 5> & CRC_5_USB();
250 static const Parameters< crcpp_uint8, 6> & CRC_6_ITU();
251 static const Parameters< crcpp_uint8, 6> & CRC_6_NR();
252 static const Parameters< crcpp_uint8, 7> & CRC_7();
253#endif
254 static const Parameters< crcpp_uint8, 8> & CRC_8();
255#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
256 static const Parameters< crcpp_uint8, 8> & CRC_8_EBU();
260 static const Parameters< crcpp_uint8, 8> & CRC_8_LTE();
261 static const Parameters<crcpp_uint16, 10> & CRC_10();
263 static const Parameters<crcpp_uint16, 11> & CRC_11();
269 static const Parameters<crcpp_uint16, 15> & CRC_15();
271#endif
276#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
282#endif
285#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
290#endif
293#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
296 static const Parameters<crcpp_uint32, 24> & CRC_24();
302 static const Parameters<crcpp_uint32, 30> & CRC_30();
303#endif
304 static const Parameters<crcpp_uint32, 32> & CRC_32();
306#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
307 static const Parameters<crcpp_uint32, 32> & CRC_32_C();
308#endif
311#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
312 static const Parameters<crcpp_uint32, 32> & CRC_32_Q();
314 static const Parameters<crcpp_uint64, 64> & CRC_64();
315#endif
316
317#ifdef CRCPP_USE_CPP11
318 CRC() = delete;
319 CRC(const CRC & other) = delete;
320 CRC & operator=(const CRC & other) = delete;
321 CRC(CRC && other) = delete;
322 CRC & operator=(CRC && other) = delete;
323#endif
324
325private:
326#ifndef CRCPP_USE_CPP11
327 CRC();
328 CRC(const CRC & other);
329 CRC & operator=(const CRC & other);
330#endif
331
332 template <typename IntegerType>
333 static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits);
334
335 template <typename CRCType, crcpp_uint16 CRCWidth>
336 static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
337
338 template <typename CRCType, crcpp_uint16 CRCWidth>
339 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
340
341 template <typename CRCType, crcpp_uint16 CRCWidth>
342 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
343
344 template <typename CRCType, crcpp_uint16 CRCWidth>
345 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder);
346
347 template <typename CRCType, crcpp_uint16 CRCWidth>
348 static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
349};
350
359template <typename CRCType, crcpp_uint16 CRCWidth>
361{
362 // This should take advantage of RVO and optimize out the copy.
363 return CRC::Table<CRCType, CRCWidth>(*this);
364}
365
372template <typename CRCType, crcpp_uint16 CRCWidth>
378
379#ifdef CRCPP_USE_CPP11
386template <typename CRCType, crcpp_uint16 CRCWidth>
388 parameters(::std::move(params))
389{
390 InitTable();
391}
392#endif
393
400template <typename CRCType, crcpp_uint16 CRCWidth>
405
412template <typename CRCType, crcpp_uint16 CRCWidth>
413inline const CRCType * CRC::Table<CRCType, CRCWidth>::GetTable() const
414{
415 return table;
416}
417
425template <typename CRCType, crcpp_uint16 CRCWidth>
426inline CRCType CRC::Table<CRCType, CRCWidth>::operator[](unsigned char index) const
427{
428 return table[index];
429}
430
436template <typename CRCType, crcpp_uint16 CRCWidth>
438{
439 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
440 static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
441 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
442
443 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
444 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
445
446 CRCType crc;
447 unsigned char byte = 0;
448
449 // Loop over each dividend (each possible number storable in an unsigned char)
450 do
451 {
452 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte, sizeof(byte), parameters, CRCType(0));
453
454 // This mask might not be necessary; all unit tests pass with this line commented out,
455 // but that might just be a coincidence based on the CRC parameters used for testing.
456 // In any case, this is harmless to leave in and only adds a single machine instruction per loop iteration.
457 crc &= BIT_MASK;
458
459 if (!parameters.reflectInput && CRCWidth < CHAR_BIT)
460 {
461 // Undo the special operation at the end of the CalculateRemainder()
462 // function for non-reflected CRCs < CHAR_BIT.
463 crc = static_cast<CRCType>(crc << SHIFT);
464 }
465
466 table[byte] = crc;
467 }
468 while (++byte);
469}
470
480template <typename CRCType, crcpp_uint16 CRCWidth>
481inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
482{
483 CRCType remainder = CalculateRemainder(data, size, parameters, parameters.initialValue);
484
485 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
486
487 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
488}
489
500template <typename CRCType, crcpp_uint16 CRCWidth>
501inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
502{
503 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
504
505 remainder = CalculateRemainder(data, size, parameters, remainder);
506
507 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
508
509 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
510}
511
521template <typename CRCType, crcpp_uint16 CRCWidth>
522inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
523{
524 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
525
526 CRCType remainder = CalculateRemainder(data, size, lookupTable, parameters.initialValue);
527
528 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
529
530 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
531}
532
544template <typename CRCType, crcpp_uint16 CRCWidth>
545inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
546{
547 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
548
549 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
550
551 remainder = CalculateRemainder(data, size, lookupTable, remainder);
552
553 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
554
555 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
556}
557
567template <typename CRCType, crcpp_uint16 CRCWidth>
568inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
569{
570 CRCType remainder = parameters.initialValue;
571
572 // Calculate the remainder on a whole number of bytes first, then call
573 // a special-case function for the remaining bits.
574 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
575 if (wholeNumberOfBytes > 0)
576 {
577 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, remainder);
578 }
579
580 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
581 if (remainingNumberOfBits != 0)
582 {
583 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
584 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
585 }
586
587 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
588
589 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
590}
591
602template <typename CRCType, crcpp_uint16 CRCWidth>
603inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
604{
605 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
606
607 // Calculate the remainder on a whole number of bytes first, then call
608 // a special-case function for the remaining bits.
609 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
610 if (wholeNumberOfBytes > 0)
611 {
612 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, parameters.initialValue);
613 }
614
615 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
616 if (remainingNumberOfBits != 0)
617 {
618 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
619 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
620 }
621
622 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
623
624 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
625}
626
636template <typename CRCType, crcpp_uint16 CRCWidth>
637inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
638{
639 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
640
641 CRCType remainder = parameters.initialValue;
642
643 // Calculate the remainder on a whole number of bytes first, then call
644 // a special-case function for the remaining bits.
645 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
646 if (wholeNumberOfBytes > 0)
647 {
648 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, remainder);
649 }
650
651 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
652 if (remainingNumberOfBits != 0)
653 {
654 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
655 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
656 }
657
658 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
659
660 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
661}
662
674template <typename CRCType, crcpp_uint16 CRCWidth>
675inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
676{
677 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
678
679 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
680
681 // Calculate the remainder on a whole number of bytes first, then call
682 // a special-case function for the remaining bits.
683 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
684 if (wholeNumberOfBytes > 0)
685 {
686 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, parameters.initialValue);
687 }
688
689 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
690 if (remainingNumberOfBits > 0)
691 {
692 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
693 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
694 }
695
696 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
697
698 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
699}
700
708template <typename IntegerType>
709inline IntegerType CRC::Reflect(IntegerType value, crcpp_uint16 numBits)
710{
711 IntegerType reversedValue(0);
712
713 for (crcpp_uint16 i = 0; i < numBits; ++i)
714 {
715 reversedValue = static_cast<IntegerType>((reversedValue << 1) | (value & 1));
716 value = static_cast<IntegerType>(value >> 1);
717 }
718
719 return reversedValue;
720}
721
731template <typename CRCType, crcpp_uint16 CRCWidth>
732inline CRCType CRC::Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
733{
734 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
735 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
736 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
737
738 if (reflectOutput)
739 {
740 remainder = Reflect(remainder, CRCWidth);
741 }
742
743 return (remainder ^ finalXOR) & BIT_MASK;
744}
745
763template <typename CRCType, crcpp_uint16 CRCWidth>
764inline CRCType CRC::UndoFinalize(CRCType crc, CRCType finalXOR, bool reflectOutput)
765{
766 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
767 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
768 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
769
770 crc = (crc & BIT_MASK) ^ finalXOR;
771
772 if (reflectOutput)
773 {
774 crc = Reflect(crc, CRCWidth);
775 }
776
777 return crc;
778}
779
790template <typename CRCType, crcpp_uint16 CRCWidth>
791inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
792{
793#ifdef CRCPP_USE_CPP11
794 // This static_assert is put here because this function will always be compiled in no matter what
795 // the template parameters are and whether or not a table lookup or bit-by-bit algorithm is used.
796 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth, "CRCType is too small to contain a CRC of width CRCWidth.");
797#else
798 // Catching this compile-time error is very important. Sadly, the compiler error will be very cryptic, but it's
799 // better than nothing.
800 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
801#endif
802
803 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
804
805 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
806 // computation from the inner loop (looping over each bit) as possible.
807 if (parameters.reflectInput)
808 {
809 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
810 while (size--)
811 {
812 remainder = static_cast<CRCType>(remainder ^ *current++);
813
814 // An optimizing compiler might choose to unroll this loop.
815 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
816 {
817#ifdef CRCPP_BRANCHLESS
818 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
819 // if (remainder & 1)
820 // remainder = (remainder >> 1) ^ polynomial;
821 // else
822 // remainder >>= 1;
823 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
824#else
825 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
826#endif
827 }
828 }
829 }
830 else if (CRCWidth >= CHAR_BIT)
831 {
832 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
833#ifndef CRCPP_BRANCHLESS
834 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
835#endif
836 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
837 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
838
839 while (size--)
840 {
841 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(*current++) << SHIFT));
842
843 // An optimizing compiler might choose to unroll this loop.
844 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
845 {
846#ifdef CRCPP_BRANCHLESS
847 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
848 // if (remainder & CRC_HIGHEST_BIT_MASK)
849 // remainder = (remainder << 1) ^ parameters.polynomial;
850 // else
851 // remainder <<= 1;
852 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
853#else
854 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
855#endif
856 }
857 }
858 }
859 else
860 {
861 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
862#ifndef CRCPP_BRANCHLESS
863 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
864#endif
865 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
866 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
867
868 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
869 remainder = static_cast<CRCType>(remainder << SHIFT);
870
871 while (size--)
872 {
873 remainder = static_cast<CRCType>(remainder ^ *current++);
874
875 // An optimizing compiler might choose to unroll this loop.
876 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
877 {
878#ifdef CRCPP_BRANCHLESS
879 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
880 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
881 // remainder = (remainder << 1) ^ polynomial;
882 // else
883 // remainder <<= 1;
884 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
885#else
886 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
887#endif
888 }
889 }
890
891 remainder = static_cast<CRCType>(remainder >> SHIFT);
892 }
893
894 return remainder;
895}
896
907template <typename CRCType, crcpp_uint16 CRCWidth>
908inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder)
909{
910 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
911
912 if (lookupTable.GetParameters().reflectInput)
913 {
914 while (size--)
915 {
916#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
917 // Disable warning about data loss when doing (remainder >> CHAR_BIT) when
918 // remainder is one byte long. The algorithm is still correct in this case,
919 // though it's possible that one additional machine instruction will be executed.
920# pragma warning (push)
921# pragma warning (disable : 4333)
922#endif
923 remainder = static_cast<CRCType>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
924#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
925# pragma warning (pop)
926#endif
927 }
928 }
929 else if (CRCWidth >= CHAR_BIT)
930 {
931 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
932 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
933
934 while (size--)
935 {
936 remainder = static_cast<CRCType>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
937 }
938 }
939 else
940 {
941 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
942 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
943
944 remainder = static_cast<CRCType>(remainder << SHIFT);
945
946 while (size--)
947 {
948 // Note: no need to mask here since remainder is guaranteed to fit in a single byte.
949 remainder = lookupTable[static_cast<unsigned char>(remainder ^ *current++)];
950 }
951
952 remainder = static_cast<CRCType>(remainder >> SHIFT);
953 }
954
955 return remainder;
956}
957
958template <typename CRCType, crcpp_uint16 CRCWidth>
959inline CRCType CRC::CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
960{
961 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
962 // computation from the inner loop (looping over each bit) as possible.
963 if (parameters.reflectInput)
964 {
965 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
966 remainder = static_cast<CRCType>(remainder ^ byte);
967
968 // An optimizing compiler might choose to unroll this loop.
969 for (crcpp_size i = 0; i < numBits; ++i)
970 {
971#ifdef CRCPP_BRANCHLESS
972 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
973 // if (remainder & 1)
974 // remainder = (remainder >> 1) ^ polynomial;
975 // else
976 // remainder >>= 1;
977 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
978#else
979 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
980#endif
981 }
982 }
983 else if (CRCWidth >= CHAR_BIT)
984 {
985 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
986#ifndef CRCPP_BRANCHLESS
987 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
988#endif
989 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
990 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
991
992 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(byte) << SHIFT));
993
994 // An optimizing compiler might choose to unroll this loop.
995 for (crcpp_size i = 0; i < numBits; ++i)
996 {
997#ifdef CRCPP_BRANCHLESS
998 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
999 // if (remainder & CRC_HIGHEST_BIT_MASK)
1000 // remainder = (remainder << 1) ^ parameters.polynomial;
1001 // else
1002 // remainder <<= 1;
1003 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
1004#else
1005 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
1006#endif
1007 }
1008 }
1009 else
1010 {
1011 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
1012#ifndef CRCPP_BRANCHLESS
1013 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
1014#endif
1015 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
1016 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
1017
1018 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
1019 remainder = static_cast<CRCType>((remainder << SHIFT) ^ byte);
1020
1021 // An optimizing compiler might choose to unroll this loop.
1022 for (crcpp_size i = 0; i < numBits; ++i)
1023 {
1024#ifdef CRCPP_BRANCHLESS
1025 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1026 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
1027 // remainder = (remainder << 1) ^ polynomial;
1028 // else
1029 // remainder <<= 1;
1030 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
1031#else
1032 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
1033#endif
1034 }
1035
1036 remainder = static_cast<CRCType>(remainder >> SHIFT);
1037 }
1038
1039 return remainder;
1040}
1041
1042#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1056{
1057 static const Parameters<crcpp_uint8, 4> parameters = { 0x3, 0x0, 0x0, true, true };
1058 return parameters;
1059}
1060
1074{
1075 static const Parameters<crcpp_uint8, 5> parameters = { 0x09, 0x09, 0x00, false, false };
1076 return parameters;
1077}
1078
1092{
1093 static const Parameters<crcpp_uint8, 5> parameters = { 0x15, 0x00, 0x00, true, true };
1094 return parameters;
1095}
1096
1110{
1111 static const Parameters<crcpp_uint8, 5> parameters = { 0x05, 0x1F, 0x1F, true, true };
1112 return parameters;
1113}
1114
1128{
1129 static const Parameters<crcpp_uint8, 6> parameters = { 0x27, 0x3F, 0x00, false, false };
1130 return parameters;
1131}
1132
1146{
1147 static const Parameters<crcpp_uint8, 6> parameters = { 0x07, 0x3F, 0x00, false, false };
1148 return parameters;
1149}
1150
1164{
1165 static const Parameters<crcpp_uint8, 6> parameters = { 0x03, 0x00, 0x00, true, true };
1166 return parameters;
1167}
1168
1183{
1184 static const Parameters<crcpp_uint8, 6> parameters = { 0x21, 0x00, 0x00, false, false };
1185 return parameters;
1186}
1187
1201{
1202 static const Parameters<crcpp_uint8, 7> parameters = { 0x09, 0x00, 0x00, false, false };
1203 return parameters;
1204}
1205#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1206
1220{
1221 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0x00, 0x00, false, false };
1222 return parameters;
1223}
1224
1225#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1239{
1240 static const Parameters<crcpp_uint8, 8> parameters = { 0x1D, 0xFF, 0x00, true, true };
1241 return parameters;
1242}
1243
1257{
1258 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0xFF, 0xFF, true, true };
1259 return parameters;
1260}
1261
1275{
1276 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0x00, 0x00, true, true };
1277 return parameters;
1278}
1279
1293{
1294 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, true, true };
1295 return parameters;
1296}
1297
1311{
1312 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, false, false };
1313 return parameters;
1314}
1315
1329{
1330 static const Parameters<crcpp_uint16, 10> parameters = { 0x233, 0x000, 0x000, false, false };
1331 return parameters;
1332}
1333
1347{
1348 static const Parameters<crcpp_uint16, 10> parameters = { 0x3D9, 0x3FF, 0x000, false, false };
1349 return parameters;
1350}
1351
1365{
1366 static const Parameters<crcpp_uint16, 11> parameters = { 0x385, 0x01A, 0x000, false, false };
1367 return parameters;
1368}
1369
1384{
1385 static const Parameters<crcpp_uint16, 11> parameters = { 0x621, 0x000, 0x000, false, false };
1386 return parameters;
1387}
1388
1402{
1403 static const Parameters<crcpp_uint16, 12> parameters = { 0xF13, 0xFFF, 0x000, false, false };
1404 return parameters;
1405}
1406
1420{
1421 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, false };
1422 return parameters;
1423}
1424
1438{
1439 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, true };
1440 return parameters;
1441}
1442
1456{
1457 static const Parameters<crcpp_uint16, 13> parameters = { 0x1CF5, 0x0000, 0x0000, false, false };
1458 return parameters;
1459}
1460
1474{
1475 static const Parameters<crcpp_uint16, 15> parameters = { 0x4599, 0x0000, 0x0000, false, false };
1476 return parameters;
1477}
1478
1492{
1493 static const Parameters<crcpp_uint16, 15> parameters = { 0x6815, 0x0000, 0x0001, false, false };
1494 return parameters;
1495}
1496#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1497
1511{
1512 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, true, true };
1513 return parameters;
1514}
1515
1529{
1530 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, false, false };
1531 return parameters;
1532}
1533
1547{
1548 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, false, false };
1549 return parameters;
1550}
1551
1565{
1566 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, true, true};
1567 return parameters;
1568}
1569
1570#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1584{
1585 static const Parameters<crcpp_uint16, 16> parameters = { 0xC867, 0xFFFF, 0x0000, false, false };
1586 return parameters;
1587}
1588
1602{
1603 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, false, false };
1604 return parameters;
1605}
1606
1620{
1621 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0001, false, false };
1622 return parameters;
1623}
1624
1638{
1639 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0000, false, false };
1640 return parameters;
1641}
1642
1656{
1657 static const Parameters<crcpp_uint16, 16> parameters = { 0x3D65, 0x0000, 0xFFFF, true, true };
1658 return parameters;
1659}
1660#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1661
1675{
1676 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, false, false };
1677 return parameters;
1678}
1679
1693{
1694 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, true, true };
1695 return parameters;
1696}
1697
1698#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1712{
1713 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0xFFFF, true, true };
1714 return parameters;
1715}
1716
1730{
1731 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, true, true };
1732 return parameters;
1733}
1734
1748{
1749 static const Parameters<crcpp_uint16, 16> parameters = { 0x8BB7, 0x0000, 0x0000, false, false };
1750 return parameters;
1751}
1752
1766{
1767 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0xFFFF, true, true };
1768 return parameters;
1769}
1770
1771#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1772
1786{
1787 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, true, true };
1788 return parameters;
1789}
1790
1804{
1805 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, false, false };
1806 return parameters;
1807}
1808
1809#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1823{
1824 static const Parameters<crcpp_uint32, 17> parameters = { 0x1685B, 0x00000, 0x00000, false, false };
1825 return parameters;
1826}
1827
1841{
1842 static const Parameters<crcpp_uint32, 21> parameters = { 0x102899, 0x000000, 0x000000, false, false };
1843 return parameters;
1844}
1845
1859{
1860 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0xB704CE, 0x000000, false, false };
1861 return parameters;
1862}
1863
1877{
1878 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xFEDCBA, 0x000000, false, false };
1879 return parameters;
1880}
1881
1895{
1896 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xABCDEF, 0x000000, false, false };
1897 return parameters;
1898}
1899
1914{
1915 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0x000000, 0x000000, false, false };
1916 return parameters;
1917}
1918
1933{
1934 static const Parameters<crcpp_uint32, 24> parameters = { 0x800063, 0x000000, 0x000000, false, false };
1935 return parameters;
1936}
1937
1952{
1953 static const Parameters<crcpp_uint32, 24> parameters = { 0xB2B117, 0x000000, 0x000000, false, false };
1954 return parameters;
1955}
1956
1970{
1971 static const Parameters<crcpp_uint32, 30> parameters = { 0x2030B9C7, 0x3FFFFFFF, 0x00000000, false, false };
1972 return parameters;
1973}
1974#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1975
1989{
1990 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
1991 return parameters;
1992}
1993
2007{
2008 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false };
2009 return parameters;
2010}
2011
2012#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2026{
2027 static const Parameters<crcpp_uint32, 32> parameters = { 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2028 return parameters;
2029}
2030#endif
2031
2045{
2046 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false };
2047 return parameters;
2048}
2049
2063{
2064 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false };
2065 return parameters;
2066}
2067
2068#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2082{
2083 static const Parameters<crcpp_uint32, 32> parameters = { 0x814141AB, 0x00000000, 0x00000000, false, false };
2084 return parameters;
2085}
2086
2100{
2101 static const Parameters<crcpp_uint64, 40> parameters = { 0x0004820009, 0x0000000000, 0xFFFFFFFFFF, false, false };
2102 return parameters;
2103}
2104
2118{
2119 static const Parameters<crcpp_uint64, 64> parameters = { 0x42F0E1EBA9EA3693, 0x0000000000000000, 0x0000000000000000, false, false };
2120 return parameters;
2121}
2122#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2123
2124#ifdef CRCPP_USE_NAMESPACE
2125}
2126#endif
2127
2128#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
2129#pragma warning(pop)
2130#endif
2131
2132#endif // CRCPP_CRC_H_
crcpp_constexpr int CRCPP_PATCH_VERSION
Definition CRC.h:148
crcpp_constexpr int CRCPP_MAJOR_VERSION
Definition CRC.h:146
crcpp_constexpr char CRCPP_COPYRIGHT[]
Definition CRC.h:150
crcpp_constexpr int CRCPP_MINOR_VERSION
Definition CRC.h:147
#define crcpp_constexpr
Compile-time expression definition.
Definition CRC.h:128
#define crcpp_uint16
Unsigned 16-bit integer definition, used primarily for parameter definitions.
Definition CRC.h:89
crcpp_constexpr int CRCPP_REVISION_VERSION
Definition CRC.h:149
#define crcpp_size
Unsigned size definition, used for specifying data sizes.
Definition CRC.h:119
static CRCType CalculateBits(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:568
CRC(const CRC &other)=delete
static const Parameters< crcpp_uint16, 12 > & CRC_12_DECT()
Returns a set of parameters for CRC-12 DECT (aka CRC-12 X-CRC).
Definition CRC.h:1419
static const Parameters< crcpp_uint32, 24 > & CRC_24_NRC()
Returns a set of parameters for CRC-24 NR-C.
Definition CRC.h:1951
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYB()
Returns a set of parameters for CRC-24 FlexRay-B.
Definition CRC.h:1894
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEB()
Returns a set of parameters for CRC-24 LTE-B/NR-B.
Definition CRC.h:1932
static const Parameters< crcpp_uint16, 10 > & CRC_10()
Returns a set of parameters for CRC-10 ITU.
Definition CRC.h:1328
static const Parameters< crcpp_uint8, 6 > & CRC_6_ITU()
Returns a set of parameters for CRC-6 ITU.
Definition CRC.h:1163
static const Parameters< crcpp_uint8, 7 > & CRC_7()
Returns a set of parameters for CRC-7 JEDEC.
Definition CRC.h:1200
static const Parameters< crcpp_uint8, 4 > & CRC_4_ITU()
Returns a set of parameters for CRC-4 ITU.
Definition CRC.h:1055
CRC & operator=(const CRC &other)=delete
static const Parameters< crcpp_uint8, 5 > & CRC_5_EPC()
Returns a set of parameters for CRC-5 EPC.
Definition CRC.h:1073
static const Parameters< crcpp_uint16, 16 > & CRC_16_XMODEM()
Returns a set of parameters for CRC-16 XMODEM (aka CRC-16 ZMODEM, CRC-16 ACORN, CRC-16 LTE).
Definition CRC.h:1803
static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Definition CRC.h:959
static const Parameters< crcpp_uint32, 30 > & CRC_30()
Returns a set of parameters for CRC-30 CDMA.
Definition CRC.h:1969
static const Parameters< crcpp_uint16, 16 > & CRC_16_DECTR()
Returns a set of parameters for CRC-16 DECT-R (aka CRC-16 R-CRC).
Definition CRC.h:1619
static const Parameters< crcpp_uint32, 17 > & CRC_17_CAN()
Returns a set of parameters for CRC-17 CAN.
Definition CRC.h:1822
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
Definition CRC.h:1219
static const Parameters< crcpp_uint32, 24 > & CRC_24()
Returns a set of parameters for CRC-24 OPENPGP.
Definition CRC.h:1858
static const Parameters< crcpp_uint16, 15 > & CRC_15_MPT1327()
Returns a set of parameters for CRC-15 MPT1327.
Definition CRC.h:1491
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
Definition CRC.h:2044
static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Undoes the process of computing the final reflection and XOR of a CRC remainder.
Definition CRC.h:764
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYA()
Returns a set of parameters for CRC-24 FlexRay-A.
Definition CRC.h:1876
static const Parameters< crcpp_uint16, 16 > & CRC_16_MAXIM()
Returns a set of parameters for CRC-16 MAXIM.
Definition CRC.h:1711
static const Parameters< crcpp_uint32, 32 > & CRC_32_C()
Returns a set of parameters for CRC-32 C (aka CRC-32 ISCSI, CRC-32 Castagnoli, CRC-32 Interlaken).
Definition CRC.h:2025
static const Parameters< crcpp_uint16, 13 > & CRC_13_BBC()
Returns a set of parameters for CRC-13 BBC.
Definition CRC.h:1455
static const Parameters< crcpp_uint8, 8 > & CRC_8_MAXIM()
Returns a set of parameters for CRC-8 MAXIM (aka CRC-8 DOW-CRC).
Definition CRC.h:1274
static const Parameters< crcpp_uint8, 8 > & CRC_8_HDLC()
Returns a set of parameters for CRC-8 HDLC (ISO/IEC 13239:2002).
Definition CRC.h:1256
static const Parameters< crcpp_uint8, 8 > & CRC_8_WCDMA()
Returns a set of parameters for CRC-8 WCDMA.
Definition CRC.h:1292
static const Parameters< crcpp_uint16, 16 > & CRC_16_BUYPASS()
Returns a set of parameters for CRC-16 BUYPASS (aka CRC-16 VERIFONE, CRC-16 UMTS).
Definition CRC.h:1528
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Computes a CRC remainder.
Definition CRC.h:791
static const Parameters< crcpp_uint8, 6 > & CRC_6_NR()
Returns a set of parameters for CRC-6 NR.
Definition CRC.h:1182
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEA()
Returns a set of parameters for CRC-24 LTE-A/NR-A.
Definition CRC.h:1913
static const Parameters< crcpp_uint16, 15 > & CRC_15()
Returns a set of parameters for CRC-15 CAN.
Definition CRC.h:1473
static const Parameters< crcpp_uint16, 16 > & CRC_16_DECTX()
Returns a set of parameters for CRC-16 DECT-X (aka CRC-16 X-CRC).
Definition CRC.h:1637
static const Parameters< crcpp_uint32, 21 > & CRC_21_CAN()
Returns a set of parameters for CRC-21 CAN.
Definition CRC.h:1840
static const Parameters< crcpp_uint16, 12 > & CRC_12_UMTS()
Returns a set of parameters for CRC-12 UMTS (aka CRC-12 3GPP).
Definition CRC.h:1437
static const Parameters< crcpp_uint16, 16 > & CRC_16_CMS()
Returns a set of parameters for CRC-16 CMS.
Definition CRC.h:1601
static const Parameters< crcpp_uint16, 10 > & CRC_10_CDMA2000()
Returns a set of parameters for CRC-10 CDMA2000.
Definition CRC.h:1346
CRC & operator=(CRC &&other)=delete
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000B()
Returns a set of parameters for CRC-6 CDMA2000-B.
Definition CRC.h:1145
static const Parameters< crcpp_uint16, 16 > & CRC_16_KERMIT()
Returns a set of parameters for CRC-16 KERMIT (aka CRC-16 CCITT, CRC-16 CCITT-TRUE).
Definition CRC.h:1692
static const Parameters< crcpp_uint16, 11 > & CRC_11()
Returns a set of parameters for CRC-11 FlexRay.
Definition CRC.h:1364
static const Parameters< crcpp_uint32, 32 > & CRC_32_Q()
Returns a set of parameters for CRC-32 Q.
Definition CRC.h:2081
static const Parameters< crcpp_uint16, 16 > & CRC_16_T10DIF()
Returns a set of parameters for CRC-16 T10-DIF.
Definition CRC.h:1747
static const Parameters< crcpp_uint16, 16 > & CRC_16_MCRF4XX()
Returns a set of parameters for CRC-16 MCRF4XX.
Definition CRC.h:1564
static const Parameters< crcpp_uint16, 12 > & CRC_12_CDMA2000()
Returns a set of parameters for CRC-12 CDMA2000.
Definition CRC.h:1401
static const Parameters< crcpp_uint16, 16 > & CRC_16_GENIBUS()
Returns a set of parameters for CRC-16 GENIBUS (aka CRC-16 EPC, CRC-16 I-CODE, CRC-16 DARC).
Definition CRC.h:1674
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
Definition CRC.h:709
static const Parameters< crcpp_uint8, 8 > & CRC_8_EBU()
Returns a set of parameters for CRC-8 EBU (aka CRC-8 AES).
Definition CRC.h:1238
static const Parameters< crcpp_uint8, 5 > & CRC_5_USB()
Returns a set of parameters for CRC-5 USB.
Definition CRC.h:1109
static const Parameters< crcpp_uint16, 16 > & CRC_16_USB()
Returns a set of parameters for CRC-16 USB.
Definition CRC.h:1765
static const Parameters< crcpp_uint16, 16 > & CRC_16_MODBUS()
Returns a set of parameters for CRC-16 MODBUS.
Definition CRC.h:1729
static const Parameters< crcpp_uint32, 32 > & CRC_32_BZIP2()
Returns a set of parameters for CRC-32 BZIP2 (aka CRC-32 AAL5, CRC-32 DECT-B, CRC-32 B-CRC).
Definition CRC.h:2006
static const Parameters< crcpp_uint16, 16 > & CRC_16_CDMA2000()
Returns a set of parameters for CRC-16 CDMA2000.
Definition CRC.h:1583
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
Definition CRC.h:1546
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
Definition CRC.h:2062
static const Parameters< crcpp_uint16, 11 > & CRC_11_NR()
Returns a set of parameters for CRC-11 NR.
Definition CRC.h:1383
static const Parameters< crcpp_uint16, 16 > & CRC_16_X25()
Returns a set of parameters for CRC-16 X-25 (aka CRC-16 IBM-SDLC, CRC-16 ISO-HDLC,...
Definition CRC.h:1785
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000A()
Returns a set of parameters for CRC-6 CDMA2000-A.
Definition CRC.h:1127
static const Parameters< crcpp_uint8, 5 > & CRC_5_ITU()
Returns a set of parameters for CRC-5 ITU.
Definition CRC.h:1091
static const Parameters< crcpp_uint8, 8 > & CRC_8_LTE()
Returns a set of parameters for CRC-8 LTE.
Definition CRC.h:1310
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:481
static const Parameters< crcpp_uint16, 16 > & CRC_16_DNP()
Returns a set of parameters for CRC-16 DNP.
Definition CRC.h:1655
CRC()=delete
static const Parameters< crcpp_uint16, 16 > & CRC_16_ARC()
Returns a set of parameters for CRC-16 ARC (aka CRC-16 IBM, CRC-16 LHA).
Definition CRC.h:1510
static const Parameters< crcpp_uint32, 32 > & CRC_32()
Returns a set of parameters for CRC-32 (aka CRC-32 ADCCP, CRC-32 PKZip).
Definition CRC.h:1988
CRC(CRC &&other)=delete
static const Parameters< crcpp_uint64, 64 > & CRC_64()
Returns a set of parameters for CRC-64 ECMA.
Definition CRC.h:2117
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
Definition CRC.h:732
static const Parameters< crcpp_uint64, 40 > & CRC_40_GSM()
Returns a set of parameters for CRC-40 GSM.
Definition CRC.h:2099
CRC parameters.
Definition CRC.h:178
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
Definition CRC.h:360
CRCType initialValue
Initial CRC value.
Definition CRC.h:180
CRCType polynomial
CRC polynomial.
Definition CRC.h:179
CRCType finalXOR
Value to XOR with the final CRC.
Definition CRC.h:181
bool reflectInput
true to reflect all input bytes
Definition CRC.h:182
bool reflectOutput
true to reflect the output CRC (reflection occurs before the final XOR)
Definition CRC.h:183
CRC lookup table. After construction, the CRC parameters are fixed.
Definition CRC.h:194
const CRCType * GetTable() const
Gets the CRC table.
Definition CRC.h:413
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Definition CRC.h:401
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Definition CRC.h:426
Table(const Parameters< CRCType, CRCWidth > &parameters)
Constructs a CRC table from a set of CRC parameters.
Definition CRC.h:373
CRCType table[1<< CHAR_BIT]
CRC lookup table.
Definition CRC.h:212
void InitTable()
Initializes a CRC table.
Definition CRC.h:437
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
Definition CRC.h:211