crc_memcpy.h 4.2 KB

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