LLVM 24.0.0git
CRC.h
Go to the documentation of this file.
1//===-- llvm/Support/CRC.h - Cyclic Redundancy Check-------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains implementations of CRC functions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_CRC_H
14#define LLVM_SUPPORT_CRC_H
15
18#include <cassert>
19
20namespace llvm {
21template <typename T> class ArrayRef;
22
23// Compute the CRC-32 of Data.
25
26// Compute the running CRC-32 of Data, with CRC being the previous value of the
27// checksum.
28LLVM_ABI uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
29
30// Calculate bit-reflected CRC for given initial CRC, Data and Polynomial.
31// It processes the lower DataBytes of Data from LSB onwards.
32// DataBytes must be 1/2/4/8.
33// Poly must already be in the bit-reflected form.
35 unsigned DataBytes,
36 uint32_t Poly) {
37 assert(
38 (DataBytes == 1 || DataBytes == 2 || DataBytes == 4 || DataBytes == 8) &&
39 "Data Bytes out of range.");
40 uint32_t Result = Crc;
41 // Process each byte
42 for (unsigned I = 0; I != DataBytes; ++I) {
43 uint8_t Byte = static_cast<uint8_t>((Data >> (I * 8)) & 0xFF);
44 Result ^= Byte;
45 for (int J = 0; J != 8; ++J) {
46 Result = (Result >> 1) ^ ((Result & 1) ? Poly : 0);
47 }
48 }
49
50 return Result;
51}
52
53// Class for computing the JamCRC.
54//
55// We will use the "Rocksoft^tm Model CRC Algorithm" to describe the properties
56// of this CRC:
57// Width : 32
58// Poly : 04C11DB7
59// Init : FFFFFFFF
60// RefIn : True
61// RefOut : True
62// XorOut : 00000000
63// Check : 340BC6D9 (result of CRC for "123456789")
64//
65// In other words, this is the same as CRC-32, except that XorOut is 0 instead
66// of FFFFFFFF.
67//
68// N.B. We permit flexibility of the "Init" value. Some consumers of this need
69// it to be zero.
70class JamCRC {
71public:
72 JamCRC(uint32_t Init = 0xFFFFFFFFU) : CRC(Init) {}
73
74 // Update the CRC calculation with Data.
76
77 uint32_t getCRC() const { return CRC; }
78
79private:
80 uint32_t CRC;
81};
82
83} // end namespace llvm
84
85#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#define LLVM_ABI
Definition Compiler.h:215
#define I(x, y, z)
Definition MD5.cpp:57
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
uint32_t getCRC() const
Definition CRC.h:77
JamCRC(uint32_t Init=0xFFFFFFFFU)
Definition CRC.h:72
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Definition CRC.cpp:103
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI uint32_t crc32(ArrayRef< uint8_t > Data)
Definition CRC.cpp:101
constexpr uint32_t calculateReflectedCRC32(uint32_t Crc, uint64_t Data, unsigned DataBytes, uint32_t Poly)
Definition CRC.h:34