non_temporal_memcpy.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_NON_TEMPORAL_MEMCPY_H_
  15. #define ABSL_CRC_INTERNAL_NON_TEMPORAL_MEMCPY_H_
  16. #ifdef _MSC_VER
  17. #include <intrin.h>
  18. #endif
  19. #ifdef __SSE__
  20. #include <xmmintrin.h>
  21. #endif
  22. #ifdef __SSE2__
  23. #include <emmintrin.h>
  24. #endif
  25. #ifdef __SSE3__
  26. #include <pmmintrin.h>
  27. #endif
  28. #ifdef __AVX__
  29. #include <immintrin.h>
  30. #endif
  31. #ifdef __aarch64__
  32. #include "absl/crc/internal/non_temporal_arm_intrinsics.h"
  33. #endif
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <cstdint>
  37. #include <cstring>
  38. #include "absl/base/config.h"
  39. #include "absl/base/optimization.h"
  40. namespace absl {
  41. ABSL_NAMESPACE_BEGIN
  42. namespace crc_internal {
  43. // This non-temporal memcpy does regular load and non-temporal store memory
  44. // copy. It is compatible to both 16-byte aligned and unaligned addresses. If
  45. // data at the destination is not immediately accessed, using non-temporal
  46. // memcpy can save 1 DRAM load of the destination cacheline.
  47. constexpr size_t kCacheLineSize = ABSL_CACHELINE_SIZE;
  48. // If the objects overlap, the behavior is undefined.
  49. inline void *non_temporal_store_memcpy(void *__restrict dst,
  50. const void *__restrict src, size_t len) {
  51. #if defined(__SSE3__) || defined(__aarch64__) || \
  52. (defined(_MSC_VER) && defined(__AVX__))
  53. // This implementation requires SSE3.
  54. // MSVC cannot target SSE3 directly, but when MSVC targets AVX,
  55. // SSE3 support is implied.
  56. uint8_t *d = reinterpret_cast<uint8_t *>(dst);
  57. const uint8_t *s = reinterpret_cast<const uint8_t *>(src);
  58. // memcpy() the misaligned header. At the end of this if block, <d> is
  59. // aligned to a 64-byte cacheline boundary or <len> == 0.
  60. if (reinterpret_cast<uintptr_t>(d) & (kCacheLineSize - 1)) {
  61. uintptr_t bytes_before_alignment_boundary =
  62. kCacheLineSize -
  63. (reinterpret_cast<uintptr_t>(d) & (kCacheLineSize - 1));
  64. size_t header_len = (std::min)(bytes_before_alignment_boundary, len);
  65. assert(bytes_before_alignment_boundary < kCacheLineSize);
  66. memcpy(d, s, header_len);
  67. d += header_len;
  68. s += header_len;
  69. len -= header_len;
  70. }
  71. if (len >= kCacheLineSize) {
  72. _mm_sfence();
  73. __m128i *dst_cacheline = reinterpret_cast<__m128i *>(d);
  74. const __m128i *src_cacheline = reinterpret_cast<const __m128i *>(s);
  75. constexpr int kOpsPerCacheLine = kCacheLineSize / sizeof(__m128i);
  76. size_t loops = len / kCacheLineSize;
  77. while (len >= kCacheLineSize) {
  78. __m128i temp1, temp2, temp3, temp4;
  79. temp1 = _mm_lddqu_si128(src_cacheline + 0);
  80. temp2 = _mm_lddqu_si128(src_cacheline + 1);
  81. temp3 = _mm_lddqu_si128(src_cacheline + 2);
  82. temp4 = _mm_lddqu_si128(src_cacheline + 3);
  83. _mm_stream_si128(dst_cacheline + 0, temp1);
  84. _mm_stream_si128(dst_cacheline + 1, temp2);
  85. _mm_stream_si128(dst_cacheline + 2, temp3);
  86. _mm_stream_si128(dst_cacheline + 3, temp4);
  87. src_cacheline += kOpsPerCacheLine;
  88. dst_cacheline += kOpsPerCacheLine;
  89. len -= kCacheLineSize;
  90. }
  91. d += loops * kCacheLineSize;
  92. s += loops * kCacheLineSize;
  93. _mm_sfence();
  94. }
  95. // memcpy the tail.
  96. if (len) {
  97. memcpy(d, s, len);
  98. }
  99. return dst;
  100. #else
  101. // Fallback to regular memcpy.
  102. return memcpy(dst, src, len);
  103. #endif // __SSE3__ || __aarch64__ || (_MSC_VER && __AVX__)
  104. }
  105. inline void *non_temporal_store_memcpy_avx(void *__restrict dst,
  106. const void *__restrict src,
  107. size_t len) {
  108. #ifdef __AVX__
  109. uint8_t *d = reinterpret_cast<uint8_t *>(dst);
  110. const uint8_t *s = reinterpret_cast<const uint8_t *>(src);
  111. // memcpy() the misaligned header. At the end of this if block, <d> is
  112. // aligned to a 64-byte cacheline boundary or <len> == 0.
  113. if (reinterpret_cast<uintptr_t>(d) & (kCacheLineSize - 1)) {
  114. uintptr_t bytes_before_alignment_boundary =
  115. kCacheLineSize -
  116. (reinterpret_cast<uintptr_t>(d) & (kCacheLineSize - 1));
  117. size_t header_len = (std::min)(bytes_before_alignment_boundary, len);
  118. assert(bytes_before_alignment_boundary < kCacheLineSize);
  119. memcpy(d, s, header_len);
  120. d += header_len;
  121. s += header_len;
  122. len -= header_len;
  123. }
  124. if (len >= kCacheLineSize) {
  125. _mm_sfence();
  126. __m256i *dst_cacheline = reinterpret_cast<__m256i *>(d);
  127. const __m256i *src_cacheline = reinterpret_cast<const __m256i *>(s);
  128. constexpr int kOpsPerCacheLine = kCacheLineSize / sizeof(__m256i);
  129. size_t loops = len / kCacheLineSize;
  130. while (len >= kCacheLineSize) {
  131. __m256i temp1, temp2;
  132. temp1 = _mm256_lddqu_si256(src_cacheline + 0);
  133. temp2 = _mm256_lddqu_si256(src_cacheline + 1);
  134. _mm256_stream_si256(dst_cacheline + 0, temp1);
  135. _mm256_stream_si256(dst_cacheline + 1, temp2);
  136. src_cacheline += kOpsPerCacheLine;
  137. dst_cacheline += kOpsPerCacheLine;
  138. len -= kCacheLineSize;
  139. }
  140. d += loops * kCacheLineSize;
  141. s += loops * kCacheLineSize;
  142. _mm_sfence();
  143. }
  144. // memcpy the tail.
  145. if (len) {
  146. memcpy(d, s, len);
  147. }
  148. return dst;
  149. #else
  150. // Fallback to regular memcpy when AVX is not available.
  151. return memcpy(dst, src, len);
  152. #endif // __AVX__
  153. }
  154. } // namespace crc_internal
  155. ABSL_NAMESPACE_END
  156. } // namespace absl
  157. #endif // ABSL_CRC_INTERNAL_NON_TEMPORAL_MEMCPY_H_