crc32c_inline.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2022 The Abseil Authors.
  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. // https://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. #ifndef ABSL_CRC_INTERNAL_CRC32C_INLINE_H_
  15. #define ABSL_CRC_INTERNAL_CRC32C_INLINE_H_
  16. #include <cstdint>
  17. #include "absl/base/config.h"
  18. #include "absl/base/internal/endian.h"
  19. #include "absl/crc/internal/crc32_x86_arm_combined_simd.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace crc_internal {
  23. // CRC32C implementation optimized for small inputs.
  24. // Either computes crc and return true, or if there is
  25. // no hardware support does nothing and returns false.
  26. inline bool ExtendCrc32cInline(uint32_t* crc, const char* p, size_t n) {
  27. #if defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) || \
  28. defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD)
  29. constexpr uint32_t kCrc32Xor = 0xffffffffU;
  30. *crc ^= kCrc32Xor;
  31. if (n & 1) {
  32. *crc = CRC32_u8(*crc, static_cast<uint8_t>(*p));
  33. n--;
  34. p++;
  35. }
  36. if (n & 2) {
  37. *crc = CRC32_u16(*crc, absl::little_endian::Load16(p));
  38. n -= 2;
  39. p += 2;
  40. }
  41. if (n & 4) {
  42. *crc = CRC32_u32(*crc, absl::little_endian::Load32(p));
  43. n -= 4;
  44. p += 4;
  45. }
  46. while (n) {
  47. *crc = CRC32_u64(*crc, absl::little_endian::Load64(p));
  48. n -= 8;
  49. p += 8;
  50. }
  51. *crc ^= kCrc32Xor;
  52. return true;
  53. #else
  54. // No hardware support, signal the need to fallback.
  55. static_cast<void>(crc);
  56. static_cast<void>(p);
  57. static_cast<void>(n);
  58. return false;
  59. #endif // defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD) ||
  60. // defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD)
  61. }
  62. } // namespace crc_internal
  63. ABSL_NAMESPACE_END
  64. } // namespace absl
  65. #endif // ABSL_CRC_INTERNAL_CRC32C_INLINE_H_