• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
17 #define TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
18 
19 #include <stddef.h>
20 
21 // NOLINTNEXTLINE
22 #include "tensorflow/core/platform/platform.h"
23 // NOLINTNEXTLINE
24 #include "tensorflow/core/platform/cord.h"
25 #include "tensorflow/core/platform/types.h"
26 
27 namespace tensorflow {
28 namespace crc32c {
29 
30 // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
31 // crc32c of some string A.  Extend() is often used to maintain the
32 // crc32c of a stream of data.
33 extern uint32 Extend(uint32 init_crc, const char* data, size_t n);
34 
35 #if defined(TF_CORD_SUPPORT)
36 extern uint32 Extend(uint32 init_crc, const absl::Cord& cord);
37 #endif
38 
39 // Return the crc32c of data[0,n-1]
Value(const char * data,size_t n)40 inline uint32 Value(const char* data, size_t n) { return Extend(0, data, n); }
41 
42 #if defined(TF_CORD_SUPPORT)
Value(const absl::Cord & cord)43 inline uint32 Value(const absl::Cord& cord) { return Extend(0, cord); }
44 #endif
45 
46 static const uint32 kMaskDelta = 0xa282ead8ul;
47 
48 // Return a masked representation of crc.
49 //
50 // Motivation: it is problematic to compute the CRC of a string that
51 // contains embedded CRCs.  Therefore we recommend that CRCs stored
52 // somewhere (e.g., in files) should be masked before being stored.
Mask(uint32 crc)53 inline uint32 Mask(uint32 crc) {
54   // Rotate right by 15 bits and add a constant.
55   return ((crc >> 15) | (crc << 17)) + kMaskDelta;
56 }
57 
58 // Return the crc whose masked representation is masked_crc.
Unmask(uint32 masked_crc)59 inline uint32 Unmask(uint32 masked_crc) {
60   uint32 rot = masked_crc - kMaskDelta;
61   return ((rot >> 17) | (rot << 15));
62 }
63 
64 }  // namespace crc32c
65 }  // namespace tensorflow
66 
67 #endif  // TENSORFLOW_CORE_LIB_HASH_CRC32C_H_
68