1 /**
2 * Copyright 2019 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MINDSPORE_CORE_UTILS_SYSTEM_CRC32C_H_
18 #define MINDSPORE_CORE_UTILS_SYSTEM_CRC32C_H_
19
20 #include <cstddef>
21 #include <cstdint>
22 #include "utils/system/base.h"
23 #include "utils/system/env.h"
24 #include "utils/log_adapter.h"
25
26 namespace mindspore {
27 namespace system {
28 // Align n to (1 << m) byte boundary
MemAlign(uintptr_t n,size_t m)29 inline uintptr_t MemAlign(uintptr_t n, size_t m) { return ((n) + ((1 << (m)) - 1)) & (~((1 << (m)) - 1)); }
30
31 // Masked for crc.
32 static constexpr uint32 kMaskDelta = 0xa282ead8ul;
33 static const int kRightShift = 15;
34 static const int kLeftShift = (32 - kRightShift);
35 // Provide the Crc32c function
36 class MS_CORE_API Crc32c {
37 public:
38 Crc32c() = default;
39 ~Crc32c() = default;
40
41 // Calculate the crc32c value, use the 8 table method
42 static uint32 MakeCrc32c(uint32 init_crc, const char *data, size_t size);
43
44 // return the crc32c value(need mask)
GetMaskCrc32cValue(const char * data,size_t n)45 static uint32 GetMaskCrc32cValue(const char *data, size_t n) {
46 auto crc = MakeCrc32c(0, data, n);
47 // Rotate right by kRightShift bits and add kMaskDelta(a constant).
48 return ((crc >> kRightShift) | (crc << kLeftShift)) + kMaskDelta;
49 }
50 };
51 } // namespace system
52 } // namespace mindspore
53
54 #endif // MINDSPORE_CORE_UTILS_SYSTEM_CRC32C_H_
55