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();
262 static const Parameters<crcpp_uint16, 10> & CRC_10();
264 static const Parameters<crcpp_uint16, 11> & CRC_11();
270 static const Parameters<crcpp_uint16, 15> & CRC_15();
272#endif
277#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
283#endif
286#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
291#endif
294#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
297 static const Parameters<crcpp_uint32, 24> & CRC_24();
303 static const Parameters<crcpp_uint32, 30> & CRC_30();
304#endif
305 static const Parameters<crcpp_uint32, 32> & CRC_32();
307#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
308 static const Parameters<crcpp_uint32, 32> & CRC_32_C();
309#endif
312#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
313 static const Parameters<crcpp_uint32, 32> & CRC_32_Q();
315 static const Parameters<crcpp_uint64, 64> & CRC_64();
316#endif
317
318#ifdef CRCPP_USE_CPP11
319 CRC() = delete;
320 CRC(const CRC & other) = delete;
321 CRC & operator=(const CRC & other) = delete;
322 CRC(CRC && other) = delete;
323 CRC & operator=(CRC && other) = delete;
324#endif
325
326private:
327#ifndef CRCPP_USE_CPP11
328 CRC();
329 CRC(const CRC & other);
330 CRC & operator=(const CRC & other);
331#endif
332
333 template <typename IntegerType>
334 static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits);
335
336 template <typename CRCType, crcpp_uint16 CRCWidth>
337 static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
338
339 template <typename CRCType, crcpp_uint16 CRCWidth>
340 static CRCType UndoFinalize(CRCType remainder, CRCType finalXOR, bool reflectOutput);
341
342 template <typename CRCType, crcpp_uint16 CRCWidth>
343 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
344
345 template <typename CRCType, crcpp_uint16 CRCWidth>
346 static CRCType CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder);
347
348 template <typename CRCType, crcpp_uint16 CRCWidth>
349 static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder);
350};
351
360template <typename CRCType, crcpp_uint16 CRCWidth>
362{
363 // This should take advantage of RVO and optimize out the copy.
364 return CRC::Table<CRCType, CRCWidth>(*this);
365}
366
373template <typename CRCType, crcpp_uint16 CRCWidth>
379
380#ifdef CRCPP_USE_CPP11
387template <typename CRCType, crcpp_uint16 CRCWidth>
389 parameters(::std::move(params))
390{
391 InitTable();
392}
393#endif
394
401template <typename CRCType, crcpp_uint16 CRCWidth>
406
413template <typename CRCType, crcpp_uint16 CRCWidth>
414inline const CRCType * CRC::Table<CRCType, CRCWidth>::GetTable() const
415{
416 return table;
417}
418
426template <typename CRCType, crcpp_uint16 CRCWidth>
427inline CRCType CRC::Table<CRCType, CRCWidth>::operator[](unsigned char index) const
428{
429 return table[index];
430}
431
437template <typename CRCType, crcpp_uint16 CRCWidth>
439{
440 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
441 static crcpp_constexpr CRCType BIT_MASK((CRCType(1) << (CRCWidth - CRCType(1))) |
442 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1)));
443
444 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
445 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
446
447 CRCType crc;
448 unsigned char byte = 0;
449
450 // Loop over each dividend (each possible number storable in an unsigned char)
451 do
452 {
453 crc = CRC::CalculateRemainder<CRCType, CRCWidth>(&byte, sizeof(byte), parameters, CRCType(0));
454
455 // This mask might not be necessary; all unit tests pass with this line commented out,
456 // but that might just be a coincidence based on the CRC parameters used for testing.
457 // In any case, this is harmless to leave in and only adds a single machine instruction per loop iteration.
458 crc &= BIT_MASK;
459
460 if (!parameters.reflectInput && CRCWidth < CHAR_BIT)
461 {
462 // Undo the special operation at the end of the CalculateRemainder()
463 // function for non-reflected CRCs < CHAR_BIT.
464 crc = static_cast<CRCType>(crc << SHIFT);
465 }
466
467 table[byte] = crc;
468 }
469 while (++byte);
470}
471
481template <typename CRCType, crcpp_uint16 CRCWidth>
482inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
483{
484 CRCType remainder = CalculateRemainder(data, size, parameters, parameters.initialValue);
485
486 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
487
488 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
489}
490
501template <typename CRCType, crcpp_uint16 CRCWidth>
502inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
503{
504 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
505
506 remainder = CalculateRemainder(data, size, parameters, remainder);
507
508 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
509
510 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
511}
512
522template <typename CRCType, crcpp_uint16 CRCWidth>
523inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
524{
525 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
526
527 CRCType remainder = CalculateRemainder(data, size, lookupTable, parameters.initialValue);
528
529 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
530
531 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
532}
533
545template <typename CRCType, crcpp_uint16 CRCWidth>
546inline CRCType CRC::Calculate(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
547{
548 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
549
550 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
551
552 remainder = CalculateRemainder(data, size, lookupTable, remainder);
553
554 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
555
556 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
557}
558
568template <typename CRCType, crcpp_uint16 CRCWidth>
569inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters)
570{
571 CRCType remainder = parameters.initialValue;
572
573 // Calculate the remainder on a whole number of bytes first, then call
574 // a special-case function for the remaining bits.
575 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
576 if (wholeNumberOfBytes > 0)
577 {
578 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, remainder);
579 }
580
581 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
582 if (remainingNumberOfBits != 0)
583 {
584 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
585 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
586 }
587
588 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
589
590 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
591}
592
603template <typename CRCType, crcpp_uint16 CRCWidth>
604inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType crc)
605{
606 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
607
608 // Calculate the remainder on a whole number of bytes first, then call
609 // a special-case function for the remaining bits.
610 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
611 if (wholeNumberOfBytes > 0)
612 {
613 remainder = CalculateRemainder(data, wholeNumberOfBytes, parameters, parameters.initialValue);
614 }
615
616 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
617 if (remainingNumberOfBits != 0)
618 {
619 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
620 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
621 }
622
623 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
624
625 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
626}
627
637template <typename CRCType, crcpp_uint16 CRCWidth>
638inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable)
639{
640 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
641
642 CRCType remainder = parameters.initialValue;
643
644 // Calculate the remainder on a whole number of bytes first, then call
645 // a special-case function for the remaining bits.
646 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
647 if (wholeNumberOfBytes > 0)
648 {
649 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, remainder);
650 }
651
652 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
653 if (remainingNumberOfBits != 0)
654 {
655 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
656 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
657 }
658
659 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
660
661 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
662}
663
675template <typename CRCType, crcpp_uint16 CRCWidth>
676inline CRCType CRC::CalculateBits(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType crc)
677{
678 const Parameters<CRCType, CRCWidth> & parameters = lookupTable.GetParameters();
679
680 CRCType remainder = UndoFinalize<CRCType, CRCWidth>(crc, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
681
682 // Calculate the remainder on a whole number of bytes first, then call
683 // a special-case function for the remaining bits.
684 crcpp_size wholeNumberOfBytes = size / CHAR_BIT;
685 if (wholeNumberOfBytes > 0)
686 {
687 remainder = CalculateRemainder(data, wholeNumberOfBytes, lookupTable, parameters.initialValue);
688 }
689
690 crcpp_size remainingNumberOfBits = size % CHAR_BIT;
691 if (remainingNumberOfBits > 0)
692 {
693 unsigned char lastByte = *(reinterpret_cast<const unsigned char *>(data) + wholeNumberOfBytes);
694 remainder = CalculateRemainderBits(lastByte, remainingNumberOfBits, parameters, remainder);
695 }
696
697 // No need to mask the remainder here; the mask will be applied in the Finalize() function.
698
699 return Finalize<CRCType, CRCWidth>(remainder, parameters.finalXOR, parameters.reflectInput != parameters.reflectOutput);
700}
701
709template <typename IntegerType>
710inline IntegerType CRC::Reflect(IntegerType value, crcpp_uint16 numBits)
711{
712 IntegerType reversedValue(0);
713
714 for (crcpp_uint16 i = 0; i < numBits; ++i)
715 {
716 reversedValue = static_cast<IntegerType>((reversedValue << 1) | (value & 1));
717 value = static_cast<IntegerType>(value >> 1);
718 }
719
720 return reversedValue;
721}
722
732template <typename CRCType, crcpp_uint16 CRCWidth>
733inline CRCType CRC::Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
734{
735 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
736 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
737 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
738
739 if (reflectOutput)
740 {
741 remainder = Reflect(remainder, CRCWidth);
742 }
743
744 return (remainder ^ finalXOR) & BIT_MASK;
745}
746
764template <typename CRCType, crcpp_uint16 CRCWidth>
765inline CRCType CRC::UndoFinalize(CRCType crc, CRCType finalXOR, bool reflectOutput)
766{
767 // For masking off the bits for the CRC (in the event that the number of bits in CRCType is larger than CRCWidth)
768 static crcpp_constexpr CRCType BIT_MASK = (CRCType(1) << (CRCWidth - CRCType(1))) |
769 ((CRCType(1) << (CRCWidth - CRCType(1))) - CRCType(1));
770
771 crc = (crc & BIT_MASK) ^ finalXOR;
772
773 if (reflectOutput)
774 {
775 crc = Reflect(crc, CRCWidth);
776 }
777
778 return crc;
779}
780
791template <typename CRCType, crcpp_uint16 CRCWidth>
792inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
793{
794#ifdef CRCPP_USE_CPP11
795 // This static_assert is put here because this function will always be compiled in no matter what
796 // the template parameters are and whether or not a table lookup or bit-by-bit algorithm is used.
797 static_assert(::std::numeric_limits<CRCType>::digits >= CRCWidth, "CRCType is too small to contain a CRC of width CRCWidth.");
798#else
799 // Catching this compile-time error is very important. Sadly, the compiler error will be very cryptic, but it's
800 // better than nothing.
801 enum { static_assert_failed_CRCType_is_too_small_to_contain_a_CRC_of_width_CRCWidth = 1 / (::std::numeric_limits<CRCType>::digits >= CRCWidth ? 1 : 0) };
802#endif
803
804 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
805
806 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
807 // computation from the inner loop (looping over each bit) as possible.
808 if (parameters.reflectInput)
809 {
810 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
811 while (size--)
812 {
813 remainder = static_cast<CRCType>(remainder ^ *current++);
814
815 // An optimizing compiler might choose to unroll this loop.
816 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
817 {
818#ifdef CRCPP_BRANCHLESS
819 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
820 // if (remainder & 1)
821 // remainder = (remainder >> 1) ^ polynomial;
822 // else
823 // remainder >>= 1;
824 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
825#else
826 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
827#endif
828 }
829 }
830 }
831 else if (CRCWidth >= CHAR_BIT)
832 {
833 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
834#ifndef CRCPP_BRANCHLESS
835 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
836#endif
837 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
838 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
839
840 while (size--)
841 {
842 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(*current++) << SHIFT));
843
844 // An optimizing compiler might choose to unroll this loop.
845 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
846 {
847#ifdef CRCPP_BRANCHLESS
848 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
849 // if (remainder & CRC_HIGHEST_BIT_MASK)
850 // remainder = (remainder << 1) ^ parameters.polynomial;
851 // else
852 // remainder <<= 1;
853 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
854#else
855 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
856#endif
857 }
858 }
859 }
860 else
861 {
862 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
863#ifndef CRCPP_BRANCHLESS
864 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
865#endif
866 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
867 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
868
869 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
870 remainder = static_cast<CRCType>(remainder << SHIFT);
871
872 while (size--)
873 {
874 remainder = static_cast<CRCType>(remainder ^ *current++);
875
876 // An optimizing compiler might choose to unroll this loop.
877 for (crcpp_size i = 0; i < CHAR_BIT; ++i)
878 {
879#ifdef CRCPP_BRANCHLESS
880 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
881 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
882 // remainder = (remainder << 1) ^ polynomial;
883 // else
884 // remainder <<= 1;
885 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
886#else
887 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
888#endif
889 }
890 }
891
892 remainder = static_cast<CRCType>(remainder >> SHIFT);
893 }
894
895 return remainder;
896}
897
908template <typename CRCType, crcpp_uint16 CRCWidth>
909inline CRCType CRC::CalculateRemainder(const void * data, crcpp_size size, const Table<CRCType, CRCWidth> & lookupTable, CRCType remainder)
910{
911 const unsigned char * current = reinterpret_cast<const unsigned char *>(data);
912
913 if (lookupTable.GetParameters().reflectInput)
914 {
915 while (size--)
916 {
917#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
918 // Disable warning about data loss when doing (remainder >> CHAR_BIT) when
919 // remainder is one byte long. The algorithm is still correct in this case,
920 // though it's possible that one additional machine instruction will be executed.
921# pragma warning (push)
922# pragma warning (disable : 4333)
923#endif
924 remainder = static_cast<CRCType>((remainder >> CHAR_BIT) ^ lookupTable[static_cast<unsigned char>(remainder ^ *current++)]);
925#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
926# pragma warning (pop)
927#endif
928 }
929 }
930 else if (CRCWidth >= CHAR_BIT)
931 {
932 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
933 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
934
935 while (size--)
936 {
937 remainder = static_cast<CRCType>((remainder << CHAR_BIT) ^ lookupTable[static_cast<unsigned char>((remainder >> SHIFT) ^ *current++)]);
938 }
939 }
940 else
941 {
942 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
943 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
944
945 remainder = static_cast<CRCType>(remainder << SHIFT);
946
947 while (size--)
948 {
949 // Note: no need to mask here since remainder is guaranteed to fit in a single byte.
950 remainder = lookupTable[static_cast<unsigned char>(remainder ^ *current++)];
951 }
952
953 remainder = static_cast<CRCType>(remainder >> SHIFT);
954 }
955
956 return remainder;
957}
958
959template <typename CRCType, crcpp_uint16 CRCWidth>
960inline CRCType CRC::CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters<CRCType, CRCWidth> & parameters, CRCType remainder)
961{
962 // Slightly different implementations based on the parameters. The current implementations try to eliminate as much
963 // computation from the inner loop (looping over each bit) as possible.
964 if (parameters.reflectInput)
965 {
966 CRCType polynomial = CRC::Reflect(parameters.polynomial, CRCWidth);
967 remainder = static_cast<CRCType>(remainder ^ byte);
968
969 // An optimizing compiler might choose to unroll this loop.
970 for (crcpp_size i = 0; i < numBits; ++i)
971 {
972#ifdef CRCPP_BRANCHLESS
973 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
974 // if (remainder & 1)
975 // remainder = (remainder >> 1) ^ polynomial;
976 // else
977 // remainder >>= 1;
978 remainder = static_cast<CRCType>((remainder >> 1) ^ ((remainder & 1) * polynomial));
979#else
980 remainder = static_cast<CRCType>((remainder & 1) ? ((remainder >> 1) ^ polynomial) : (remainder >> 1));
981#endif
982 }
983 }
984 else if (CRCWidth >= CHAR_BIT)
985 {
986 static crcpp_constexpr CRCType CRC_WIDTH_MINUS_ONE(CRCWidth - CRCType(1));
987#ifndef CRCPP_BRANCHLESS
988 static crcpp_constexpr CRCType CRC_HIGHEST_BIT_MASK(CRCType(1) << CRC_WIDTH_MINUS_ONE);
989#endif
990 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
991 static crcpp_constexpr CRCType SHIFT((CRCWidth >= CHAR_BIT) ? static_cast<CRCType>(CRCWidth - CHAR_BIT) : 0);
992
993 remainder = static_cast<CRCType>(remainder ^ (static_cast<CRCType>(byte) << SHIFT));
994
995 // An optimizing compiler might choose to unroll this loop.
996 for (crcpp_size i = 0; i < numBits; ++i)
997 {
998#ifdef CRCPP_BRANCHLESS
999 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1000 // if (remainder & CRC_HIGHEST_BIT_MASK)
1001 // remainder = (remainder << 1) ^ parameters.polynomial;
1002 // else
1003 // remainder <<= 1;
1004 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CRC_WIDTH_MINUS_ONE) & 1) * parameters.polynomial));
1005#else
1006 remainder = static_cast<CRCType>((remainder & CRC_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ parameters.polynomial) : (remainder << 1));
1007#endif
1008 }
1009 }
1010 else
1011 {
1012 static crcpp_constexpr CRCType CHAR_BIT_MINUS_ONE(CHAR_BIT - 1);
1013#ifndef CRCPP_BRANCHLESS
1014 static crcpp_constexpr CRCType CHAR_BIT_HIGHEST_BIT_MASK(CRCType(1) << CHAR_BIT_MINUS_ONE);
1015#endif
1016 // The conditional expression is used to avoid a -Wshift-count-overflow warning.
1017 static crcpp_constexpr CRCType SHIFT((CHAR_BIT >= CRCWidth) ? static_cast<CRCType>(CHAR_BIT - CRCWidth) : 0);
1018
1019 CRCType polynomial = static_cast<CRCType>(parameters.polynomial << SHIFT);
1020 remainder = static_cast<CRCType>((remainder << SHIFT) ^ byte);
1021
1022 // An optimizing compiler might choose to unroll this loop.
1023 for (crcpp_size i = 0; i < numBits; ++i)
1024 {
1025#ifdef CRCPP_BRANCHLESS
1026 // Clever way to avoid a branch at the expense of a multiplication. This code is equivalent to the following:
1027 // if (remainder & CHAR_BIT_HIGHEST_BIT_MASK)
1028 // remainder = (remainder << 1) ^ polynomial;
1029 // else
1030 // remainder <<= 1;
1031 remainder = static_cast<CRCType>((remainder << 1) ^ (((remainder >> CHAR_BIT_MINUS_ONE) & 1) * polynomial));
1032#else
1033 remainder = static_cast<CRCType>((remainder & CHAR_BIT_HIGHEST_BIT_MASK) ? ((remainder << 1) ^ polynomial) : (remainder << 1));
1034#endif
1035 }
1036
1037 remainder = static_cast<CRCType>(remainder >> SHIFT);
1038 }
1039
1040 return remainder;
1041}
1042
1043#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1057{
1058 static const Parameters<crcpp_uint8, 4> parameters = { 0x3, 0x0, 0x0, true, true };
1059 return parameters;
1060}
1061
1075{
1076 static const Parameters<crcpp_uint8, 5> parameters = { 0x09, 0x09, 0x00, false, false };
1077 return parameters;
1078}
1079
1093{
1094 static const Parameters<crcpp_uint8, 5> parameters = { 0x15, 0x00, 0x00, true, true };
1095 return parameters;
1096}
1097
1111{
1112 static const Parameters<crcpp_uint8, 5> parameters = { 0x05, 0x1F, 0x1F, true, true };
1113 return parameters;
1114}
1115
1129{
1130 static const Parameters<crcpp_uint8, 6> parameters = { 0x27, 0x3F, 0x00, false, false };
1131 return parameters;
1132}
1133
1147{
1148 static const Parameters<crcpp_uint8, 6> parameters = { 0x07, 0x3F, 0x00, false, false };
1149 return parameters;
1150}
1151
1165{
1166 static const Parameters<crcpp_uint8, 6> parameters = { 0x03, 0x00, 0x00, true, true };
1167 return parameters;
1168}
1169
1184{
1185 static const Parameters<crcpp_uint8, 6> parameters = { 0x21, 0x00, 0x00, false, false };
1186 return parameters;
1187}
1188
1202{
1203 static const Parameters<crcpp_uint8, 7> parameters = { 0x09, 0x00, 0x00, false, false };
1204 return parameters;
1205}
1206#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1207
1221{
1222 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0x00, 0x00, false, false };
1223 return parameters;
1224}
1225
1226#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1240{
1241 static const Parameters<crcpp_uint8, 8> parameters = { 0x1D, 0xFF, 0x00, true, true };
1242 return parameters;
1243}
1244
1258{
1259 static const Parameters<crcpp_uint8, 8> parameters = { 0x07, 0xFF, 0xFF, true, true };
1260 return parameters;
1261}
1262
1276{
1277 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0x00, 0x00, true, true };
1278 return parameters;
1279}
1280
1294{
1295 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, true, true };
1296 return parameters;
1297}
1298
1312{
1313 static const Parameters<crcpp_uint8, 8> parameters = { 0x9B, 0x00, 0x00, false, false };
1314 return parameters;
1315}
1316
1330{
1331 static const Parameters<crcpp_uint8, 8> parameters = { 0x31, 0xFF, 0x00, false, false };
1332 return parameters;
1333}
1334
1348{
1349 static const Parameters<crcpp_uint16, 10> parameters = { 0x233, 0x000, 0x000, false, false };
1350 return parameters;
1351}
1352
1366{
1367 static const Parameters<crcpp_uint16, 10> parameters = { 0x3D9, 0x3FF, 0x000, false, false };
1368 return parameters;
1369}
1370
1384{
1385 static const Parameters<crcpp_uint16, 11> parameters = { 0x385, 0x01A, 0x000, false, false };
1386 return parameters;
1387}
1388
1403{
1404 static const Parameters<crcpp_uint16, 11> parameters = { 0x621, 0x000, 0x000, false, false };
1405 return parameters;
1406}
1407
1421{
1422 static const Parameters<crcpp_uint16, 12> parameters = { 0xF13, 0xFFF, 0x000, false, false };
1423 return parameters;
1424}
1425
1439{
1440 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, false };
1441 return parameters;
1442}
1443
1457{
1458 static const Parameters<crcpp_uint16, 12> parameters = { 0x80F, 0x000, 0x000, false, true };
1459 return parameters;
1460}
1461
1475{
1476 static const Parameters<crcpp_uint16, 13> parameters = { 0x1CF5, 0x0000, 0x0000, false, false };
1477 return parameters;
1478}
1479
1493{
1494 static const Parameters<crcpp_uint16, 15> parameters = { 0x4599, 0x0000, 0x0000, false, false };
1495 return parameters;
1496}
1497
1511{
1512 static const Parameters<crcpp_uint16, 15> parameters = { 0x6815, 0x0000, 0x0001, false, false };
1513 return parameters;
1514}
1515#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1516
1530{
1531 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, true, true };
1532 return parameters;
1533}
1534
1548{
1549 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0x0000, false, false };
1550 return parameters;
1551}
1552
1566{
1567 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, false, false };
1568 return parameters;
1569}
1570
1584{
1585 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0x0000, true, true};
1586 return parameters;
1587}
1588
1589#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1603{
1604 static const Parameters<crcpp_uint16, 16> parameters = { 0xC867, 0xFFFF, 0x0000, false, false };
1605 return parameters;
1606}
1607
1621{
1622 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, false, false };
1623 return parameters;
1624}
1625
1639{
1640 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0001, false, false };
1641 return parameters;
1642}
1643
1657{
1658 static const Parameters<crcpp_uint16, 16> parameters = { 0x0589, 0x0000, 0x0000, false, false };
1659 return parameters;
1660}
1661
1675{
1676 static const Parameters<crcpp_uint16, 16> parameters = { 0x3D65, 0x0000, 0xFFFF, true, true };
1677 return parameters;
1678}
1679#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1680
1694{
1695 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, false, false };
1696 return parameters;
1697}
1698
1712{
1713 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, true, true };
1714 return parameters;
1715}
1716
1717#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1731{
1732 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0x0000, 0xFFFF, true, true };
1733 return parameters;
1734}
1735
1749{
1750 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0x0000, true, true };
1751 return parameters;
1752}
1753
1767{
1768 static const Parameters<crcpp_uint16, 16> parameters = { 0x8BB7, 0x0000, 0x0000, false, false };
1769 return parameters;
1770}
1771
1785{
1786 static const Parameters<crcpp_uint16, 16> parameters = { 0x8005, 0xFFFF, 0xFFFF, true, true };
1787 return parameters;
1788}
1789
1790#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1791
1805{
1806 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0xFFFF, 0xFFFF, true, true };
1807 return parameters;
1808}
1809
1823{
1824 static const Parameters<crcpp_uint16, 16> parameters = { 0x1021, 0x0000, 0x0000, false, false };
1825 return parameters;
1826}
1827
1828#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1842{
1843 static const Parameters<crcpp_uint32, 17> parameters = { 0x1685B, 0x00000, 0x00000, false, false };
1844 return parameters;
1845}
1846
1860{
1861 static const Parameters<crcpp_uint32, 21> parameters = { 0x102899, 0x000000, 0x000000, false, false };
1862 return parameters;
1863}
1864
1878{
1879 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0xB704CE, 0x000000, false, false };
1880 return parameters;
1881}
1882
1896{
1897 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xFEDCBA, 0x000000, false, false };
1898 return parameters;
1899}
1900
1914{
1915 static const Parameters<crcpp_uint32, 24> parameters = { 0x5D6DCB, 0xABCDEF, 0x000000, false, false };
1916 return parameters;
1917}
1918
1933{
1934 static const Parameters<crcpp_uint32, 24> parameters = { 0x864CFB, 0x000000, 0x000000, false, false };
1935 return parameters;
1936}
1937
1952{
1953 static const Parameters<crcpp_uint32, 24> parameters = { 0x800063, 0x000000, 0x000000, false, false };
1954 return parameters;
1955}
1956
1971{
1972 static const Parameters<crcpp_uint32, 24> parameters = { 0xB2B117, 0x000000, 0x000000, false, false };
1973 return parameters;
1974}
1975
1989{
1990 static const Parameters<crcpp_uint32, 30> parameters = { 0x2030B9C7, 0x3FFFFFFF, 0x00000000, false, false };
1991 return parameters;
1992}
1993#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
1994
2008{
2009 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2010 return parameters;
2011}
2012
2026{
2027 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false };
2028 return parameters;
2029}
2030
2031#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2045{
2046 static const Parameters<crcpp_uint32, 32> parameters = { 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true };
2047 return parameters;
2048}
2049#endif
2050
2064{
2065 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false };
2066 return parameters;
2067}
2068
2082{
2083 static const Parameters<crcpp_uint32, 32> parameters = { 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false };
2084 return parameters;
2085}
2086
2087#ifdef CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2101{
2102 static const Parameters<crcpp_uint32, 32> parameters = { 0x814141AB, 0x00000000, 0x00000000, false, false };
2103 return parameters;
2104}
2105
2119{
2120 static const Parameters<crcpp_uint64, 40> parameters = { 0x0004820009, 0x0000000000, 0xFFFFFFFFFF, false, false };
2121 return parameters;
2122}
2123
2137{
2138 static const Parameters<crcpp_uint64, 64> parameters = { 0x42F0E1EBA9EA3693, 0x0000000000000000, 0x0000000000000000, false, false };
2139 return parameters;
2140}
2141#endif // CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS
2142
2143#ifdef CRCPP_USE_NAMESPACE
2144}
2145#endif
2146
2147#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER)
2148#pragma warning(pop)
2149#endif
2150
2151#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:569
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:1438
static const Parameters< crcpp_uint32, 24 > & CRC_24_NRC()
Returns a set of parameters for CRC-24 NR-C.
Definition CRC.h:1970
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYB()
Returns a set of parameters for CRC-24 FlexRay-B.
Definition CRC.h:1913
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEB()
Returns a set of parameters for CRC-24 LTE-B/NR-B.
Definition CRC.h:1951
static const Parameters< crcpp_uint16, 10 > & CRC_10()
Returns a set of parameters for CRC-10 ITU.
Definition CRC.h:1347
static const Parameters< crcpp_uint8, 6 > & CRC_6_ITU()
Returns a set of parameters for CRC-6 ITU.
Definition CRC.h:1164
static const Parameters< crcpp_uint8, 7 > & CRC_7()
Returns a set of parameters for CRC-7 JEDEC.
Definition CRC.h:1201
static const Parameters< crcpp_uint8, 4 > & CRC_4_ITU()
Returns a set of parameters for CRC-4 ITU.
Definition CRC.h:1056
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:1074
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:1822
static CRCType CalculateRemainderBits(unsigned char byte, crcpp_size numBits, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Definition CRC.h:960
static const Parameters< crcpp_uint32, 30 > & CRC_30()
Returns a set of parameters for CRC-30 CDMA.
Definition CRC.h:1988
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:1638
static const Parameters< crcpp_uint32, 17 > & CRC_17_CAN()
Returns a set of parameters for CRC-17 CAN.
Definition CRC.h:1841
static const Parameters< crcpp_uint8, 8 > & CRC_8()
Returns a set of parameters for CRC-8 SMBus.
Definition CRC.h:1220
static const Parameters< crcpp_uint32, 24 > & CRC_24()
Returns a set of parameters for CRC-24 OPENPGP.
Definition CRC.h:1877
static const Parameters< crcpp_uint16, 15 > & CRC_15_MPT1327()
Returns a set of parameters for CRC-15 MPT1327.
Definition CRC.h:1510
static const Parameters< crcpp_uint32, 32 > & CRC_32_MPEG2()
Returns a set of parameters for CRC-32 MPEG-2.
Definition CRC.h:2063
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:765
static const Parameters< crcpp_uint32, 24 > & CRC_24_FLEXRAYA()
Returns a set of parameters for CRC-24 FlexRay-A.
Definition CRC.h:1895
static const Parameters< crcpp_uint16, 16 > & CRC_16_MAXIM()
Returns a set of parameters for CRC-16 MAXIM.
Definition CRC.h:1730
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:2044
static const Parameters< crcpp_uint16, 13 > & CRC_13_BBC()
Returns a set of parameters for CRC-13 BBC.
Definition CRC.h:1474
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:1275
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:1257
static const Parameters< crcpp_uint8, 8 > & CRC_8_WCDMA()
Returns a set of parameters for CRC-8 WCDMA.
Definition CRC.h:1293
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:1547
static CRCType CalculateRemainder(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters, CRCType remainder)
Computes a CRC remainder.
Definition CRC.h:792
static const Parameters< crcpp_uint8, 6 > & CRC_6_NR()
Returns a set of parameters for CRC-6 NR.
Definition CRC.h:1183
static const Parameters< crcpp_uint32, 24 > & CRC_24_LTEA()
Returns a set of parameters for CRC-24 LTE-A/NR-A.
Definition CRC.h:1932
static const Parameters< crcpp_uint16, 15 > & CRC_15()
Returns a set of parameters for CRC-15 CAN.
Definition CRC.h:1492
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:1656
static const Parameters< crcpp_uint32, 21 > & CRC_21_CAN()
Returns a set of parameters for CRC-21 CAN.
Definition CRC.h:1859
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:1456
static const Parameters< crcpp_uint16, 16 > & CRC_16_CMS()
Returns a set of parameters for CRC-16 CMS.
Definition CRC.h:1620
static const Parameters< crcpp_uint16, 10 > & CRC_10_CDMA2000()
Returns a set of parameters for CRC-10 CDMA2000.
Definition CRC.h:1365
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:1146
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:1711
static const Parameters< crcpp_uint16, 11 > & CRC_11()
Returns a set of parameters for CRC-11 FlexRay.
Definition CRC.h:1383
static const Parameters< crcpp_uint32, 32 > & CRC_32_Q()
Returns a set of parameters for CRC-32 Q.
Definition CRC.h:2100
static const Parameters< crcpp_uint16, 16 > & CRC_16_T10DIF()
Returns a set of parameters for CRC-16 T10-DIF.
Definition CRC.h:1766
static const Parameters< crcpp_uint16, 16 > & CRC_16_MCRF4XX()
Returns a set of parameters for CRC-16 MCRF4XX.
Definition CRC.h:1583
static const Parameters< crcpp_uint16, 12 > & CRC_12_CDMA2000()
Returns a set of parameters for CRC-12 CDMA2000.
Definition CRC.h:1420
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:1693
static IntegerType Reflect(IntegerType value, crcpp_uint16 numBits)
Reflects (i.e. reverses the bits within) an integer value.
Definition CRC.h:710
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:1239
static const Parameters< crcpp_uint8, 5 > & CRC_5_USB()
Returns a set of parameters for CRC-5 USB.
Definition CRC.h:1110
static const Parameters< crcpp_uint16, 16 > & CRC_16_USB()
Returns a set of parameters for CRC-16 USB.
Definition CRC.h:1784
static const Parameters< crcpp_uint16, 16 > & CRC_16_MODBUS()
Returns a set of parameters for CRC-16 MODBUS.
Definition CRC.h:1748
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:2025
static const Parameters< crcpp_uint16, 16 > & CRC_16_CDMA2000()
Returns a set of parameters for CRC-16 CDMA2000.
Definition CRC.h:1602
static const Parameters< crcpp_uint16, 16 > & CRC_16_CCITTFALSE()
Returns a set of parameters for CRC-16 CCITT FALSE.
Definition CRC.h:1565
static const Parameters< crcpp_uint32, 32 > & CRC_32_POSIX()
Returns a set of parameters for CRC-32 POSIX.
Definition CRC.h:2081
static const Parameters< crcpp_uint16, 11 > & CRC_11_NR()
Returns a set of parameters for CRC-11 NR.
Definition CRC.h:1402
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:1804
static const Parameters< crcpp_uint8, 6 > & CRC_6_CDMA2000A()
Returns a set of parameters for CRC-6 CDMA2000-A.
Definition CRC.h:1128
static const Parameters< crcpp_uint8, 5 > & CRC_5_ITU()
Returns a set of parameters for CRC-5 ITU.
Definition CRC.h:1092
static const Parameters< crcpp_uint8, 8 > & CRC_8_LTE()
Returns a set of parameters for CRC-8 LTE.
Definition CRC.h:1311
static const Parameters< crcpp_uint8, 8 > & CRC_8_NRSC5()
Returns a set of parameters for CRC-8 NRSC-5.
Definition CRC.h:1329
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition CRC.h:482
static const Parameters< crcpp_uint16, 16 > & CRC_16_DNP()
Returns a set of parameters for CRC-16 DNP.
Definition CRC.h:1674
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:1529
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:2007
CRC(CRC &&other)=delete
static const Parameters< crcpp_uint64, 64 > & CRC_64()
Returns a set of parameters for CRC-64 ECMA.
Definition CRC.h:2136
static CRCType Finalize(CRCType remainder, CRCType finalXOR, bool reflectOutput)
Computes the final reflection and XOR of a CRC remainder.
Definition CRC.h:733
static const Parameters< crcpp_uint64, 40 > & CRC_40_GSM()
Returns a set of parameters for CRC-40 GSM.
Definition CRC.h:2118
CRC parameters.
Definition CRC.h:178
Table< CRCType, CRCWidth > MakeTable() const
Returns a CRC lookup table construct using these CRC parameters.
Definition CRC.h:361
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:414
const Parameters< CRCType, CRCWidth > & GetParameters() const
Gets the CRC parameters used to construct the CRC table.
Definition CRC.h:402
CRCType operator[](unsigned char index) const
Gets an entry in the CRC table.
Definition CRC.h:427
Table(const Parameters< CRCType, CRCWidth > &parameters)
Constructs a CRC table from a set of CRC parameters.
Definition CRC.h:374
CRCType table[1<< CHAR_BIT]
CRC lookup table.
Definition CRC.h:212
void InitTable()
Initializes a CRC table.
Definition CRC.h:438
Parameters< CRCType, CRCWidth > parameters
CRC parameters used to construct the table.
Definition CRC.h:211