crc_memcpy.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_CRC_MEMCPY_H_
  15. #define ABSL_CRC_INTERNAL_CRC_MEMCPY_H_
  16. #include <cstddef>
  17. #include <memory>
  18. #include "absl/base/config.h"
  19. #include "absl/crc/crc32c.h"
  20. #include "absl/crc/internal/crc32_x86_arm_combined_simd.h"
  21. // Defined if the class AcceleratedCrcMemcpyEngine exists.
  22. // TODO(b/299127771): Consider relaxing the pclmul requirement once the other
  23. // intrinsics are conditionally compiled without it.
  24. #if defined(ABSL_CRC_INTERNAL_HAVE_X86_SIMD)
  25. #define ABSL_INTERNAL_HAVE_X86_64_ACCELERATED_CRC_MEMCPY_ENGINE 1
  26. #elif defined(ABSL_CRC_INTERNAL_HAVE_ARM_SIMD)
  27. #define ABSL_INTERNAL_HAVE_ARM_ACCELERATED_CRC_MEMCPY_ENGINE 1
  28. #endif
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. namespace crc_internal {
  32. class CrcMemcpyEngine {
  33. public:
  34. virtual ~CrcMemcpyEngine() = default;
  35. virtual crc32c_t Compute(void* __restrict dst, const void* __restrict src,
  36. std::size_t length, crc32c_t initial_crc) const = 0;
  37. protected:
  38. CrcMemcpyEngine() = default;
  39. };
  40. class CrcMemcpy {
  41. public:
  42. static crc32c_t CrcAndCopy(void* __restrict dst, const void* __restrict src,
  43. std::size_t length,
  44. crc32c_t initial_crc = crc32c_t{0},
  45. bool non_temporal = false) {
  46. static const ArchSpecificEngines engines = GetArchSpecificEngines();
  47. auto* engine = non_temporal ? engines.non_temporal : engines.temporal;
  48. return engine->Compute(dst, src, length, initial_crc);
  49. }
  50. // For testing only: get an architecture-specific engine for tests.
  51. static std::unique_ptr<CrcMemcpyEngine> GetTestEngine(int vector,
  52. int integer);
  53. private:
  54. struct ArchSpecificEngines {
  55. CrcMemcpyEngine* temporal;
  56. CrcMemcpyEngine* non_temporal;
  57. };
  58. static ArchSpecificEngines GetArchSpecificEngines();
  59. };
  60. // Fallback CRC-memcpy engine.
  61. class FallbackCrcMemcpyEngine : public CrcMemcpyEngine {
  62. public:
  63. FallbackCrcMemcpyEngine() = default;
  64. FallbackCrcMemcpyEngine(const FallbackCrcMemcpyEngine&) = delete;
  65. FallbackCrcMemcpyEngine operator=(const FallbackCrcMemcpyEngine&) = delete;
  66. crc32c_t Compute(void* __restrict dst, const void* __restrict src,
  67. std::size_t length, crc32c_t initial_crc) const override;
  68. };
  69. // CRC Non-Temporal-Memcpy engine.
  70. class CrcNonTemporalMemcpyEngine : public CrcMemcpyEngine {
  71. public:
  72. CrcNonTemporalMemcpyEngine() = default;
  73. CrcNonTemporalMemcpyEngine(const CrcNonTemporalMemcpyEngine&) = delete;
  74. CrcNonTemporalMemcpyEngine operator=(const CrcNonTemporalMemcpyEngine&) =
  75. delete;
  76. crc32c_t Compute(void* __restrict dst, const void* __restrict src,
  77. std::size_t length, crc32c_t initial_crc) const override;
  78. };
  79. // CRC Non-Temporal-Memcpy AVX engine.
  80. class CrcNonTemporalMemcpyAVXEngine : public CrcMemcpyEngine {
  81. public:
  82. CrcNonTemporalMemcpyAVXEngine() = default;
  83. CrcNonTemporalMemcpyAVXEngine(const CrcNonTemporalMemcpyAVXEngine&) = delete;
  84. CrcNonTemporalMemcpyAVXEngine operator=(
  85. const CrcNonTemporalMemcpyAVXEngine&) = delete;
  86. crc32c_t Compute(void* __restrict dst, const void* __restrict src,
  87. std::size_t length, crc32c_t initial_crc) const override;
  88. };
  89. // Copy source to destination and return the CRC32C of the data copied. If an
  90. // accelerated version is available, use the accelerated version, otherwise use
  91. // the generic fallback version.
  92. inline crc32c_t Crc32CAndCopy(void* __restrict dst, const void* __restrict src,
  93. std::size_t length,
  94. crc32c_t initial_crc = crc32c_t{0},
  95. bool non_temporal = false) {
  96. return CrcMemcpy::CrcAndCopy(dst, src, length, initial_crc, non_temporal);
  97. }
  98. } // namespace crc_internal
  99. ABSL_NAMESPACE_END
  100. } // namespace absl
  101. #endif // ABSL_CRC_INTERNAL_CRC_MEMCPY_H_