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