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_CCSRC_UTILS_SYSTEM_CRC32C_H_ 18 #define MINDSPORE_CCSRC_UTILS_SYSTEM_CRC32C_H_ 19 20 #include <stddef.h> 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 29 // Align n to (1 << m) byte boundary 30 #define MEM_ALIGN(n, m) ((n + ((1 << (m)) - 1)) & ~((1 << (m)) - 1)) 31 32 // Masked for crc. 33 static constexpr uint32 kMaskDelta = 0xa282ead8ul; 34 static const int kRightShift = 15; 35 static const int kLeftShift = (32 - kRightShift); 36 // Provide the Crc32c function 37 class Crc32c { 38 public: 39 Crc32c() = default; 40 ~Crc32c() = default; 41 42 // Calculate the crc32c value, use the 8 table method 43 static uint32 MakeCrc32c(uint32 init_crc, const char *data, size_t size); 44 45 // retrun the crc32c value(need mask) GetMaskCrc32cValue(const char * data,size_t n)46 static uint32 GetMaskCrc32cValue(const char *data, size_t n) { 47 auto crc = MakeCrc32c(0, data, n); 48 // Rotate right by kRightShift bits and add kMaskDelta(a constant). 49 return ((crc >> kRightShift) | (crc << kLeftShift)) + kMaskDelta; 50 } 51 }; 52 53 } // namespace system 54 } // namespace mindspore 55 56 #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_CRC32C_H_ 57