You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

crc32c.h 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef MINDSPORE_CCSRC_UTILS_SYSTEM_CRC32C_H_
  17. #define MINDSPORE_CCSRC_UTILS_SYSTEM_CRC32C_H_
  18. #include <stddef.h>
  19. #include <cstdint>
  20. #include "utils/system/base.h"
  21. #include "utils/system/env.h"
  22. #include "utils/log_adapter.h"
  23. namespace mindspore {
  24. namespace system {
  25. // Align n to (1 << m) byte boundary
  26. #define MEM_ALIGN(n, m) ((n + ((1 << (m)) - 1)) & ~((1 << (m)) - 1))
  27. // Masked for crc.
  28. static constexpr uint32 kMaskDelta = 0xa282ead8ul;
  29. static const int kRightShift = 15;
  30. static const int kLeftShift = (32 - kRightShift);
  31. // Provide the Crc32c function
  32. class Crc32c {
  33. public:
  34. Crc32c() = default;
  35. ~Crc32c() = default;
  36. // Calculate the crc32c value, use the 8 table method
  37. static uint32 MakeCrc32c(uint32 init_crc, const char *data, size_t size);
  38. // retrun the crc32c value(need mask)
  39. static uint32 GetMaskCrc32cValue(const char *data, size_t n) {
  40. auto crc = MakeCrc32c(0, data, n);
  41. // Rotate right by kRightShift bits and add kMaskDelta(a constant).
  42. return ((crc >> kRightShift) | (crc << kLeftShift)) + kMaskDelta;
  43. }
  44. };
  45. } // namespace system
  46. } // namespace mindspore
  47. #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_CRC32C_H_