pstore2
crc32.hpp
Go to the documentation of this file.
1 //===- include/pstore/core/crc32.hpp ----------------------*- mode: C++ -*-===//
2 //* _________ *
3 //* ___ _ __ ___|___ /___ \ *
4 //* / __| '__/ __| |_ \ __) | *
5 //* | (__| | | (__ ___) / __/ *
6 //* \___|_| \___|____/_____| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
17 
18 #ifndef PSTORE_CORE_CRC32_HPP
19 #define PSTORE_CORE_CRC32_HPP
20 
21 #include <array>
22 #include <cstdint>
23 #include <cstdlib>
24 
25 namespace pstore {
26 
27  namespace details {
28  extern std::array<std::uint32_t, 256> const crc32_tab;
29  } // end namespace details
30 
31  template <typename SpanType>
32  std::uint32_t crc32 (SpanType buf) noexcept {
33  auto * p = reinterpret_cast<std::uint8_t const *> (buf.data ());
34  auto crc = std::uint32_t{0};
35  auto size = buf.size_bytes ();
36  while (size--) {
37  crc = details::crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
38  }
39  return crc ^ ~0U;
40  }
41 
42 } // end namespace pstore
43 #endif // PSTORE_CORE_CRC32_HPP
Definition: print.cpp:18
Definition: nonpod2.cpp:40