diff options
author | Hans Wennborg <hans@hanshq.net> | 2019-10-09 09:06:30 +0000 |
---|---|---|
committer | Hans Wennborg <hans@hanshq.net> | 2019-10-09 09:06:30 +0000 |
commit | 1e1e3ba2526ecab1ad3c18c97abef36df00b3813 (patch) | |
tree | 58e8e645b1d85f51ab432d8ab7e279df0b3caf17 /llvm/unittests/Support | |
parent | f8d482c07be46b2ed0c280dfda1bb86cab5de386 (diff) | |
download | bcm5719-llvm-1e1e3ba2526ecab1ad3c18c97abef36df00b3813.tar.gz bcm5719-llvm-1e1e3ba2526ecab1ad3c18c97abef36df00b3813.zip |
Unify the two CRC implementations
David added the JamCRC implementation in r246590. More recently, Eugene
added a CRC-32 implementation in r357901, which falls back to zlib's
crc32 function if present.
These checksums are essentially the same, so having multiple
implementations seems unnecessary. This replaces the CRC-32
implementation with the simpler one from JamCRC, and implements the
JamCRC interface in terms of CRC-32 since this means it can use zlib's
implementation when available, saving a few bytes and potentially making
it faster.
JamCRC took an ArrayRef<char> argument, and CRC-32 took a StringRef.
This patch changes it to ArrayRef<uint8_t> which I think is the best
choice, and simplifies a few of the callers nicely.
Differential revision: https://reviews.llvm.org/D68570
llvm-svn: 374148
Diffstat (limited to 'llvm/unittests/Support')
-rw-r--r-- | llvm/unittests/Support/CRCTest.cpp | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/llvm/unittests/Support/CRCTest.cpp b/llvm/unittests/Support/CRCTest.cpp index 71afb0a4789..8e2cb2c5e78 100644 --- a/llvm/unittests/Support/CRCTest.cpp +++ b/llvm/unittests/Support/CRCTest.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/CRC.h" +#include "llvm/ADT/StringExtras.h" #include "gtest/gtest.h" using namespace llvm; @@ -18,12 +19,26 @@ using namespace llvm; namespace { TEST(CRCTest, CRC32) { - EXPECT_EQ(0x414FA339U, - llvm::crc32( - 0, StringRef("The quick brown fox jumps over the lazy dog"))); + EXPECT_EQ(0x414FA339U, llvm::crc32(arrayRefFromStringRef( + "The quick brown fox jumps over the lazy dog"))); + // CRC-32/ISO-HDLC test vector // http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32c - EXPECT_EQ(0xCBF43926U, llvm::crc32(0, StringRef("123456789"))); + EXPECT_EQ(0xCBF43926U, llvm::crc32(arrayRefFromStringRef("123456789"))); + + // Check the CRC-32 of each byte value, exercising all of CRCTable. + for (int i = 0; i < 256; i++) { + // Compute CRCTable[i] using Hacker's Delight (2nd ed.) Figure 14-7. + uint32_t crc = i; + for (int j = 7; j >= 0; j--) { + uint32_t mask = -(crc & 1); + crc = (crc >> 1) ^ (0xEDB88320 & mask); + } + + // CRCTable[i] is the CRC-32 of i without the initial and final bit flips. + uint8_t byte = i; + EXPECT_EQ(crc, ~llvm::crc32(0xFFFFFFFFU, byte)); + } } } // end anonymous namespace |