crc_memcpy_fallback.cc 2.7 KB

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