crc_memcpy_fallback.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <cstring>
  15. #include <memory>
  16. #include "absl/base/config.h"
  17. #include "absl/crc/crc32c.h"
  18. #include "absl/crc/internal/crc_memcpy.h"
  19. #include "absl/strings/string_view.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace crc_internal {
  23. absl::crc32c_t FallbackCrcMemcpyEngine::Compute(void* __restrict dst,
  24. const void* __restrict src,
  25. std::size_t length,
  26. crc32c_t initial_crc) const {
  27. constexpr size_t kBlockSize = 8192;
  28. absl::crc32c_t crc = initial_crc;
  29. const char* src_bytes = reinterpret_cast<const char*>(src);
  30. char* dst_bytes = reinterpret_cast<char*>(dst);
  31. // Copy + CRC loop - run 8k chunks until we are out of full chunks. CRC
  32. // then copy was found to be slightly more efficient in our test cases.
  33. std::size_t offset = 0;
  34. for (; offset + kBlockSize < length; offset += kBlockSize) {
  35. crc = absl::ExtendCrc32c(crc,
  36. absl::string_view(src_bytes + offset, kBlockSize));
  37. memcpy(dst_bytes + offset, src_bytes + offset, kBlockSize);
  38. }
  39. // Save some work if length is 0.
  40. if (offset < length) {
  41. std::size_t final_copy_size = length - offset;
  42. crc = absl::ExtendCrc32c(
  43. crc, absl::string_view(src_bytes + offset, final_copy_size));
  44. memcpy(dst_bytes + offset, src_bytes + offset, final_copy_size);
  45. }
  46. return crc;
  47. }
  48. // Compile the following only if we don't have
  49. #if !defined(ABSL_INTERNAL_HAVE_X86_64_ACCELERATED_CRC_MEMCPY_ENGINE) && \
  50. !defined(ABSL_INTERNAL_HAVE_ARM_ACCELERATED_CRC_MEMCPY_ENGINE)
  51. CrcMemcpy::ArchSpecificEngines CrcMemcpy::GetArchSpecificEngines() {
  52. CrcMemcpy::ArchSpecificEngines engines;
  53. engines.temporal = new FallbackCrcMemcpyEngine();
  54. engines.non_temporal = new FallbackCrcMemcpyEngine();
  55. return engines;
  56. }
  57. std::unique_ptr<CrcMemcpyEngine> CrcMemcpy::GetTestEngine(int /*vector*/,
  58. int /*integer*/) {
  59. return std::make_unique<FallbackCrcMemcpyEngine>();
  60. }
  61. #endif // !ABSL_INTERNAL_HAVE_X86_64_ACCELERATED_CRC_MEMCPY_ENGINE &&
  62. // !ABSL_INTERNAL_HAVE_ARM_ACCELERATED_CRC_MEMCPY_ENGINE
  63. } // namespace crc_internal
  64. ABSL_NAMESPACE_END
  65. } // namespace absl