Skip to content

Commit e3ad67a

Browse files
authored
Merge pull request #46936 from Dr15Jones/crc32
Improved calculation of CRC32
2 parents 5ad6521 + d8046a1 commit e3ad67a

File tree

7 files changed

+45
-59
lines changed

7 files changed

+45
-59
lines changed

DataFormats/Provenance/src/BranchID.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#include "DataFormats/Provenance/interface/BranchID.h"
2-
#include "FWCore/Utilities/interface/CRC32Calculator.h"
2+
#include "FWCore/Utilities/interface/calculateCRC32.h"
33
#include <ostream>
44

55
namespace edm {
66

7-
BranchID::value_type BranchID::toID(std::string const& branchName) {
8-
cms::CRC32Calculator crc32(branchName);
9-
return crc32.checksum();
10-
}
7+
BranchID::value_type BranchID::toID(std::string const& branchName) { return cms::calculateCRC32(branchName); }
118

129
std::ostream& operator<<(std::ostream& os, BranchID const& id) {
1310
os << id.id();

EventFilter/L1TRawToDigi/src/AMC13Spec.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#include "FWCore/Framework/interface/Event.h"
55
#include "FWCore/MessageLogger/interface/MessageLogger.h"
6-
#include "FWCore/Utilities/interface/CRC32Calculator.h"
6+
#include "FWCore/Utilities/interface/calculateCRC32.h"
77

88
#include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
99

10-
#define EDM_ML_DEBUG 1
10+
//#define EDM_ML_DEBUG 1
1111

1212
namespace amc13 {
1313
Header::Header(unsigned int namc, unsigned int orbit) {
@@ -38,8 +38,8 @@ namespace amc13 {
3838
}
3939

4040
void Trailer::writeCRC(const uint64_t* start, uint64_t* end) {
41-
std::string dstring(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(end) + 4);
42-
auto crc = cms::CRC32Calculator(dstring).checksum();
41+
std::string_view dstring(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(end) + 4);
42+
auto crc = cms::calculateCRC32(dstring);
4343

4444
*end = ((*end) & ~(uint64_t(CRC_mask) << CRC_shift)) | (static_cast<uint64_t>(crc & CRC_mask) << CRC_shift);
4545
}
@@ -128,8 +128,8 @@ namespace amc13 {
128128

129129
int crc = 0;
130130
if (check_crc) {
131-
std::string check(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(data) - 4);
132-
crc = cms::CRC32Calculator(check).checksum();
131+
std::string_view check(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(data) - 4);
132+
crc = cms::calculateCRC32(check);
133133

134134
LogDebug("L1T") << "checking data checksum of " << std::hex << crc << std::dec;
135135
}
@@ -156,8 +156,8 @@ namespace amc13 {
156156
t = Trailer(data++);
157157

158158
if (check_crc) {
159-
std::string check(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(data) - 4);
160-
crc = cms::CRC32Calculator(check).checksum();
159+
std::string_view check(reinterpret_cast<const char*>(start), reinterpret_cast<const char*>(data) - 4);
160+
crc = cms::calculateCRC32(check);
161161

162162
LogDebug("L1T") << "checking data checksum of " << std::hex << crc << std::dec;
163163
} else {

EventFilter/L1TRawToDigi/src/AMCSpec.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <iomanip>
22

33
#include "FWCore/MessageLogger/interface/MessageLogger.h"
4-
#include "FWCore/Utilities/interface/CRC32Calculator.h"
4+
#include "FWCore/Utilities/interface/calculateCRC32.h"
55

66
#include "EventFilter/L1TRawToDigi/interface/AMCSpec.h"
77

8-
#define EDM_ML_DEBUG 1
8+
//#define EDM_ML_DEBUG 1
99

1010
namespace amc {
1111
BlockHeader::BlockHeader(unsigned int amc_no, unsigned int board_id, unsigned int size, unsigned int block) {
@@ -92,8 +92,8 @@ namespace amc {
9292
}
9393

9494
void Trailer::writeCRC(const uint64_t *start, uint64_t *end) {
95-
std::string dstring(reinterpret_cast<const char *>(start), reinterpret_cast<const char *>(end) + 4);
96-
auto crc = cms::CRC32Calculator(dstring).checksum();
95+
std::string_view dstring(reinterpret_cast<const char *>(start), reinterpret_cast<const char *>(end) + 4);
96+
auto crc = cms::calculateCRC32(dstring);
9797

9898
*end = ((*end) & ~(uint64_t(CRC_mask) << CRC_shift)) | (static_cast<uint64_t>(crc & CRC_mask) << CRC_shift);
9999
}
@@ -133,8 +133,8 @@ namespace amc {
133133
header_ = Header(payload_.data());
134134
trailer_ = Trailer(&payload_.back());
135135

136-
std::string check(reinterpret_cast<const char *>(payload_.data()), payload_.size() * 8 - 4);
137-
auto crc = cms::CRC32Calculator(check).checksum();
136+
std::string_view check(reinterpret_cast<const char *>(payload_.data()), payload_.size() * 8 - 4);
137+
auto crc = cms::calculateCRC32(check);
138138

139139
trailer_.check(crc, lv1, header_.getSize(), mtf7_mode);
140140
}

FWCore/Utilities/interface/CRC32Calculator.h renamed to FWCore/Utilities/interface/calculateCRC32.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef FWCore_Utilities_CRC32Calculator_h
2-
#define FWCore_Utilities_CRC32Calculator_h
1+
#ifndef FWCore_Utilities_calculateCRC32_h
2+
#define FWCore_Utilities_calculateCRC32_h
33

44
/*
55
Code to calculate a CRC32 checksum on a string. This code is based
@@ -55,18 +55,10 @@ from the original code follow below to attribute the source.
5555

5656
#include <cstdint>
5757

58-
#include <string>
58+
#include <string_view>
5959

6060
namespace cms {
6161

62-
class CRC32Calculator {
63-
public:
64-
CRC32Calculator(std::string const& message);
65-
66-
std::uint32_t checksum() { return checksum_; }
67-
68-
private:
69-
std::uint32_t checksum_;
70-
};
62+
std::uint32_t calculateCRC32(std::string_view message);
7163
} // namespace cms
7264
#endif
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
#include "FWCore/Utilities/interface/CRC32Calculator.h"
2+
#include "FWCore/Utilities/interface/calculateCRC32.h"
33

44
namespace cms {
55

66
namespace {
77

8-
const std::uint32_t CRC32_XINIT = 0xFFFFFFFFL;
9-
const std::uint32_t CRC32_XOROT = 0xFFFFFFFFL;
8+
constexpr std::uint32_t CRC32_XINIT = 0xFFFFFFFFL;
9+
constexpr std::uint32_t CRC32_XOROT = 0xFFFFFFFFL;
1010

11-
const std::uint32_t crctable[256] = {
11+
constexpr std::uint32_t crctable[256] = {
1212
0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL, 0x076DC419L, 0x706AF48FL, 0xE963A535L, 0x9E6495A3L,
1313
0x0EDB8832L, 0x79DCB8A4L, 0xE0D5E91EL, 0x97D2D988L, 0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L, 0x90BF1D91L,
1414
0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL, 0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L,
@@ -43,19 +43,16 @@ namespace cms {
4343
0xB3667A2EL, 0xC4614AB8L, 0x5D681B02L, 0x2A6F2B94L, 0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL, 0x2D02EF8DL};
4444
} // namespace
4545

46-
CRC32Calculator::CRC32Calculator(std::string const& message) {
46+
std::uint32_t calculateCRC32(std::string_view message) {
4747
/* initialize value */
48-
checksum_ = CRC32_XINIT;
48+
uint32_t checksum = CRC32_XINIT;
4949

5050
/* process each byte prior to checksum field */
51-
auto length = message.length();
52-
char const* p = message.data();
53-
for (size_t j = 0; j < length; j++) {
54-
unsigned char uc = *p++;
55-
checksum_ = cms::crctable[(checksum_ ^ uc) & 0xFFL] ^ (checksum_ >> 8);
51+
for (auto uc : message) {
52+
checksum = cms::crctable[(checksum ^ uc) & 0xFFL] ^ (checksum >> 8);
5653
}
5754

5855
/* return XOR out value */
59-
checksum_ = checksum_ ^ CRC32_XOROT;
56+
return checksum ^ CRC32_XOROT;
6057
}
6158
} // namespace cms

FWCore/Utilities/test/test_catch2_CRC32Calculator.cc

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "catch.hpp"
2+
#include "FWCore/Utilities/interface/calculateCRC32.h"
3+
4+
TEST_CASE("test cms::calculateCRC32", "[calculateCRC32]") {
5+
SECTION("known") {
6+
auto crc32 = cms::calculateCRC32("type_label_instance_process");
7+
8+
// This known result was calculated using python as a cross check
9+
unsigned int knownResult = 1215348599;
10+
REQUIRE(crc32 == knownResult);
11+
}
12+
SECTION("empty") {
13+
auto emptyString_crc32 = cms::calculateCRC32("");
14+
REQUIRE(emptyString_crc32 == 0);
15+
}
16+
}

0 commit comments

Comments
 (0)