Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:15

0001 // Copyright 2022 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_CRC_INTERNAL_CRC_MEMCPY_H_
0016 #define ABSL_CRC_INTERNAL_CRC_MEMCPY_H_
0017 
0018 #include <cstddef>
0019 #include <memory>
0020 
0021 #include "absl/base/config.h"
0022 #include "absl/crc/crc32c.h"
0023 #include "absl/crc/internal/crc32_x86_arm_combined_simd.h"
0024 
0025 // Defined if the class AcceleratedCrcMemcpyEngine exists.
0026 // TODO(b/299127771): Consider relaxing the pclmul requirement once the other
0027 // intrinsics are conditionally compiled without it.
0028 #if defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD)
0029 #define ABSL_INTERNAL_HAVE_X86_64_ACCELERATED_CRC_MEMCPY_ENGINE 1
0030 #elif defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD)
0031 #define ABSL_INTERNAL_HAVE_ARM_ACCELERATED_CRC_MEMCPY_ENGINE 1
0032 #endif
0033 
0034 namespace absl {
0035 ABSL_NAMESPACE_BEGIN
0036 namespace crc_internal {
0037 
0038 class CrcMemcpyEngine {
0039  public:
0040   virtual ~CrcMemcpyEngine() = default;
0041 
0042   virtual crc32c_t Compute(void* __restrict dst, const void* __restrict src,
0043                            std::size_t length, crc32c_t initial_crc) const = 0;
0044 
0045  protected:
0046   CrcMemcpyEngine() = default;
0047 };
0048 
0049 class CrcMemcpy {
0050  public:
0051   static crc32c_t CrcAndCopy(void* __restrict dst, const void* __restrict src,
0052                              std::size_t length,
0053                              crc32c_t initial_crc = crc32c_t{0},
0054                              bool non_temporal = false) {
0055     static const ArchSpecificEngines engines = GetArchSpecificEngines();
0056     auto* engine = non_temporal ? engines.non_temporal : engines.temporal;
0057     return engine->Compute(dst, src, length, initial_crc);
0058   }
0059 
0060   // For testing only: get an architecture-specific engine for tests.
0061   static std::unique_ptr<CrcMemcpyEngine> GetTestEngine(int vector,
0062                                                         int integer);
0063 
0064  private:
0065   struct ArchSpecificEngines {
0066     CrcMemcpyEngine* temporal;
0067     CrcMemcpyEngine* non_temporal;
0068   };
0069 
0070   static ArchSpecificEngines GetArchSpecificEngines();
0071 };
0072 
0073 // Fallback CRC-memcpy engine.
0074 class FallbackCrcMemcpyEngine : public CrcMemcpyEngine {
0075  public:
0076   FallbackCrcMemcpyEngine() = default;
0077   FallbackCrcMemcpyEngine(const FallbackCrcMemcpyEngine&) = delete;
0078   FallbackCrcMemcpyEngine operator=(const FallbackCrcMemcpyEngine&) = delete;
0079 
0080   crc32c_t Compute(void* __restrict dst, const void* __restrict src,
0081                    std::size_t length, crc32c_t initial_crc) const override;
0082 };
0083 
0084 // CRC Non-Temporal-Memcpy engine.
0085 class CrcNonTemporalMemcpyEngine : public CrcMemcpyEngine {
0086  public:
0087   CrcNonTemporalMemcpyEngine() = default;
0088   CrcNonTemporalMemcpyEngine(const CrcNonTemporalMemcpyEngine&) = delete;
0089   CrcNonTemporalMemcpyEngine operator=(const CrcNonTemporalMemcpyEngine&) =
0090       delete;
0091 
0092   crc32c_t Compute(void* __restrict dst, const void* __restrict src,
0093                    std::size_t length, crc32c_t initial_crc) const override;
0094 };
0095 
0096 // CRC Non-Temporal-Memcpy AVX engine.
0097 class CrcNonTemporalMemcpyAVXEngine : public CrcMemcpyEngine {
0098  public:
0099   CrcNonTemporalMemcpyAVXEngine() = default;
0100   CrcNonTemporalMemcpyAVXEngine(const CrcNonTemporalMemcpyAVXEngine&) = delete;
0101   CrcNonTemporalMemcpyAVXEngine operator=(
0102       const CrcNonTemporalMemcpyAVXEngine&) = delete;
0103 
0104   crc32c_t Compute(void* __restrict dst, const void* __restrict src,
0105                    std::size_t length, crc32c_t initial_crc) const override;
0106 };
0107 
0108 // Copy source to destination and return the CRC32C of the data copied.  If an
0109 // accelerated version is available, use the accelerated version, otherwise use
0110 // the generic fallback version.
0111 inline crc32c_t Crc32CAndCopy(void* __restrict dst, const void* __restrict src,
0112                               std::size_t length,
0113                               crc32c_t initial_crc = crc32c_t{0},
0114                               bool non_temporal = false) {
0115   return CrcMemcpy::CrcAndCopy(dst, src, length, initial_crc, non_temporal);
0116 }
0117 
0118 }  // namespace crc_internal
0119 ABSL_NAMESPACE_END
0120 }  // namespace absl
0121 
0122 #endif  // ABSL_CRC_INTERNAL_CRC_MEMCPY_H_