[llvm] r357901 - [Support] Add zlib independent CRC32

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 8 04:25:48 PDT 2019


Author: evgeny777
Date: Mon Apr  8 04:25:48 2019
New Revision: 357901

URL: http://llvm.org/viewvc/llvm-project?rev=357901&view=rev
Log:
[Support] Add zlib independent CRC32

Differential revision: https://reviews.llvm.org/D59816

Added:
    llvm/trunk/include/llvm/Support/CRC.h
    llvm/trunk/lib/Support/CRC.cpp
    llvm/trunk/unittests/Support/CRCTest.cpp
Modified:
    llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp
    llvm/trunk/lib/Support/CMakeLists.txt
    llvm/trunk/unittests/Support/CMakeLists.txt

Added: llvm/trunk/include/llvm/Support/CRC.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CRC.h?rev=357901&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/CRC.h (added)
+++ llvm/trunk/include/llvm/Support/CRC.h Mon Apr  8 04:25:48 2019
@@ -0,0 +1,25 @@
+//===-- llvm/Support/CRC.h - Cyclic Redundancy Check-------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains basic functions for calculating Cyclic Redundancy Check
+// or CRC.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_CRC_H
+#define LLVM_SUPPORT_CRC_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/DataTypes.h"
+
+namespace llvm {
+/// zlib independent CRC32 calculation.
+uint32_t crc32(uint32_t CRC, StringRef S);
+} // end namespace llvm
+
+#endif

Modified: llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp?rev=357901&r1=357900&r2=357901&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp (original)
+++ llvm/trunk/lib/DebugInfo/Symbolize/Symbolize.cpp Mon Apr  8 04:25:48 2019
@@ -23,6 +23,7 @@
 #include "llvm/Object/COFF.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/MachOUniversal.h"
+#include "llvm/Support/CRC.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Compression.h"
 #include "llvm/Support/DataExtractor.h"
@@ -163,7 +164,7 @@ bool checkFileCRC(StringRef Path, uint32
       MemoryBuffer::getFileOrSTDIN(Path);
   if (!MB)
     return false;
-  return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
+  return CRCHash == crc32(0, MB.get()->getBuffer());
 }
 
 bool findDebugBinary(const std::string &OrigPath,

Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=357901&r1=357900&r2=357901&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Mon Apr  8 04:25:48 2019
@@ -76,6 +76,7 @@ add_llvm_library(LLVMSupport
   CodeGenCoverage.cpp
   CommandLine.cpp
   Compression.cpp
+  CRC.cpp
   ConvertUTF.cpp
   ConvertUTFWrapper.cpp
   CrashRecoveryContext.cpp

Added: llvm/trunk/lib/Support/CRC.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CRC.cpp?rev=357901&view=auto
==============================================================================
--- llvm/trunk/lib/Support/CRC.cpp (added)
+++ llvm/trunk/lib/Support/CRC.cpp Mon Apr  8 04:25:48 2019
@@ -0,0 +1,68 @@
+//===--- CRC.cpp - Cyclic Redundancy Check implementation -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements llvm::crc32 function.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CRC.h"
+#include "llvm/Config/config.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Threading.h"
+#include <array>
+
+using namespace llvm;
+
+#if LLVM_ENABLE_ZLIB == 0 || !HAVE_ZLIB_H
+using CRC32Table = std::array<uint32_t, 256>;
+
+static void initCRC32Table(CRC32Table &Tbl) {
+  auto Shuffle = [](uint32_t V) {
+    return (V & 1) ? (V >> 1) ^ 0xEDB88320U : V >> 1;
+  };
+
+  for (size_t I = 0; I < Tbl.size(); ++I) {
+    uint32_t V = Shuffle(I);
+    V = Shuffle(V);
+    V = Shuffle(V);
+    V = Shuffle(V);
+    V = Shuffle(V);
+    V = Shuffle(V);
+    V = Shuffle(V);
+    Tbl[I] = Shuffle(V);
+  }
+}
+
+uint32_t llvm::crc32(uint32_t CRC, StringRef S) {
+  static llvm::once_flag InitFlag;
+  static CRC32Table Tbl;
+  llvm::call_once(InitFlag, initCRC32Table, Tbl);
+
+  const uint8_t *P = reinterpret_cast<const uint8_t *>(S.data());
+  size_t Len = S.size();
+  CRC ^= 0xFFFFFFFFU;
+  for (; Len >= 8; Len -= 8) {
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+  }
+  while (Len--)
+    CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
+  return CRC ^ 0xFFFFFFFFU;
+}
+#else
+#include <zlib.h>
+uint32_t llvm::crc32(uint32_t CRC, StringRef S) {
+  return ::crc32(CRC, (const Bytef *)S.data(), S.size());
+}
+#endif

Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=357901&r1=357900&r2=357901&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Mon Apr  8 04:25:48 2019
@@ -18,6 +18,7 @@ add_llvm_unittest(SupportTests
   CommandLineTest.cpp
   CompressionTest.cpp
   ConvertUTFTest.cpp
+  CRCTest.cpp
   DataExtractorTest.cpp
   DebugTest.cpp
   DebugCounterTest.cpp

Added: llvm/trunk/unittests/Support/CRCTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CRCTest.cpp?rev=357901&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/CRCTest.cpp (added)
+++ llvm/trunk/unittests/Support/CRCTest.cpp Mon Apr  8 04:25:48 2019
@@ -0,0 +1,29 @@
+//===- llvm/unittest/Support/CRCTest.cpp - CRC tests ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements unit tests for CRC calculation functions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CRC.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(CRCTest, CRC32) {
+  EXPECT_EQ(0x414FA339U,
+            llvm::crc32(
+                0, StringRef("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")));
+}
+
+} // end anonymous namespace




More information about the llvm-commits mailing list