|
12 | 12 | #include "src/string/memory_utils/op_generic.h"
|
13 | 13 | #include "src/string/memory_utils/op_x86.h"
|
14 | 14 | #include "src/string/memory_utils/utils.h" // Ptr, CPtr
|
| 15 | +#include "third_party/llvm/llvm-project/libc/src/string/memory_utils/inline_memcpy.h" |
15 | 16 |
|
16 | 17 | #include <stddef.h> // size_t
|
17 | 18 |
|
18 | 19 | namespace LIBC_NAMESPACE {
|
19 | 20 | namespace x86 {
|
| 21 | +// Size of one cache line for software prefetching |
| 22 | +LIBC_INLINE_VAR constexpr size_t kOneCachelineSize = 64; |
| 23 | +LIBC_INLINE_VAR constexpr size_t kTwoCachelinesSize = kOneCachelineSize * 2; |
| 24 | +LIBC_INLINE_VAR constexpr size_t kFiveCachelinesSize = kOneCachelineSize * 5; |
| 25 | + |
20 | 26 | LIBC_INLINE_VAR constexpr bool kUseSoftwarePrefetchingMemset =
|
21 | 27 | LLVM_LIBC_IS_DEFINED(LIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING);
|
22 | 28 |
|
23 | 29 | } // namespace x86
|
24 | 30 |
|
25 | 31 | #if defined(__AVX512F__)
|
26 |
| - using uint128_t = generic_v128; |
27 |
| - using uint256_t = generic_v256; |
28 |
| - using uint512_t = generic_v512; |
| 32 | +using uint128_t = generic_v128; |
| 33 | +using uint256_t = generic_v256; |
| 34 | +using uint512_t = generic_v512; |
29 | 35 | #elif defined(__AVX__)
|
30 |
| - using uint128_t = generic_v128; |
31 |
| - using uint256_t = generic_v256; |
32 |
| - using uint512_t = cpp::array<generic_v256, 2>; |
| 36 | +using uint128_t = generic_v128; |
| 37 | +using uint256_t = generic_v256; |
| 38 | +using uint512_t = cpp::array<generic_v256, 2>; |
33 | 39 | #elif defined(__SSE2__)
|
34 |
| - using uint128_t = generic_v128; |
35 |
| - using uint256_t = cpp::array<generic_v128, 2>; |
36 |
| - using uint512_t = cpp::array<generic_v128, 4>; |
| 40 | +using uint128_t = generic_v128; |
| 41 | +using uint256_t = cpp::array<generic_v128, 2>; |
| 42 | +using uint512_t = cpp::array<generic_v128, 4>; |
37 | 43 | #else
|
38 |
| - using uint128_t = cpp::array<uint64_t, 2>; |
39 |
| - using uint256_t = cpp::array<uint64_t, 4>; |
40 |
| - using uint512_t = cpp::array<uint64_t, 8>; |
| 44 | +using uint128_t = cpp::array<uint64_t, 2>; |
| 45 | +using uint256_t = cpp::array<uint64_t, 4>; |
| 46 | +using uint512_t = cpp::array<uint64_t, 8>; |
41 | 47 | #endif
|
42 | 48 |
|
43 |
| - [[maybe_unused]] LIBC_INLINE static void |
44 |
| - inline_memset_x86_sw_prefetching(Ptr dst, uint8_t value, size_t count) { |
45 |
| - // Prefetch one cacheline |
46 |
| - sw_prefetch::PrefetchW(dst + sw_prefetch::kCachelineSize); |
47 |
| - if (count <= 128) |
48 |
| - return generic::Memset<uint512_t>::head_tail(dst, value, count); |
49 |
| - // Prefetch the next cacheline |
50 |
| - sw_prefetch::PrefetchW(dst + sw_prefetch::kCachelineSize * 2); |
51 |
| - // Aligned loop |
52 |
| - generic::Memset<uint256_t>::block(dst, value); |
53 |
| - align_to_next_boundary<32>(dst, count); |
54 |
| - if (count <= 192) { |
55 |
| - return generic::Memset<uint256_t>::loop_and_tail(dst, value, count); |
56 |
| - } else { |
57 |
| - generic::Memset<uint512_t>::block(dst, value); |
58 |
| - generic::Memset<uint256_t>::block(dst + sizeof(uint512_t), value); |
59 |
| - return generic::Memset<uint256_t>::loop_and_tail_prefetch<320, 128>( |
60 |
| - dst, value, count); |
| 49 | +[[maybe_unused]] LIBC_INLINE static void |
| 50 | +inline_memset_x86_gt64_sw_prefetching(Ptr dst, uint8_t value, size_t count) { |
| 51 | + size_t prefetch_distance = x86::kFiveCachelinesSize; |
| 52 | + size_t prefetch_degree = x86::kTwoCachelinesSize; |
| 53 | + size_t SIZE = sizeof(uint256_t); |
| 54 | + // Prefetch one cache line |
| 55 | + prefetch_for_write(dst + x86::kOneCachelineSize); |
| 56 | + if (count <= 128) |
| 57 | + return generic::Memset<uint512_t>::head_tail(dst, value, count); |
| 58 | + // Prefetch the second cache line |
| 59 | + prefetch_for_write(dst + x86::kTwoCachelinesSize); |
| 60 | + // Aligned loop |
| 61 | + generic::Memset<uint256_t>::block(dst, value); |
| 62 | + align_to_next_boundary<32>(dst, count); |
| 63 | + if (count <= 192) { |
| 64 | + return generic::Memset<uint256_t>::loop_and_tail(dst, value, count); |
| 65 | + } else { |
| 66 | + generic::Memset<uint512_t>::block(dst, value); |
| 67 | + generic::Memset<uint256_t>::block_offset(dst, value, SIZE); |
| 68 | + size_t offset = 96; |
| 69 | + while (offset + prefetch_degree + SIZE <= count) { |
| 70 | + for (size_t i = 0; i < prefetch_degree / x86::kOneCachelineSize; ++i) |
| 71 | + prefetch_for_write(dst + offset + prefetch_distance + |
| 72 | + x86::kOneCachelineSize * i); |
| 73 | + for (size_t i = 0; i < prefetch_degree; i += SIZE, offset += SIZE) |
| 74 | + generic::Memset<uint256_t>::block_offset(dst, value, offset); |
61 | 75 | }
|
| 76 | + generic::Memset<uint256_t>::loop_and_tail_offset(dst, value, count, offset); |
62 | 77 | }
|
| 78 | +} |
| 79 | + |
| 80 | +[[maybe_unused]] LIBC_INLINE static void |
| 81 | +inline_memset_x86(Ptr dst, uint8_t value, size_t count) { |
| 82 | + if (count == 0) |
| 83 | + return; |
| 84 | + if (count == 1) |
| 85 | + return generic::Memset<uint8_t>::block(dst, value); |
| 86 | + if (count == 2) |
| 87 | + return generic::Memset<uint16_t>::block(dst, value); |
| 88 | + if (count == 3) |
| 89 | + return generic::MemsetSequence<uint16_t, uint8_t>::block(dst, value); |
| 90 | + if (count <= 8) |
| 91 | + return generic::Memset<uint32_t>::head_tail(dst, value, count); |
| 92 | + if (count <= 16) |
| 93 | + return generic::Memset<uint64_t>::head_tail(dst, value, count); |
| 94 | + if (count <= 32) |
| 95 | + return generic::Memset<uint128_t>::head_tail(dst, value, count); |
| 96 | + if (count <= 64) |
| 97 | + return generic::Memset<uint256_t>::head_tail(dst, value, count); |
| 98 | + if constexpr (x86::kUseSoftwarePrefetchingMemset) |
| 99 | + return inline_memset_x86_gt64_sw_prefetching(dst, value, count); |
| 100 | + if (count <= 128) |
| 101 | + return generic::Memset<uint512_t>::head_tail(dst, value, count); |
| 102 | + // Aligned loop |
| 103 | + generic::Memset<uint256_t>::block(dst, value); |
| 104 | + align_to_next_boundary<32>(dst, count); |
| 105 | + return generic::Memset<uint256_t>::loop_and_tail(dst, value, count); |
| 106 | +} |
63 | 107 |
|
64 |
| - [[maybe_unused]] LIBC_INLINE static void |
65 |
| - inline_memset_x86(Ptr dst, uint8_t value, size_t count) { |
66 |
| - if (count == 0) |
67 |
| - return; |
68 |
| - if (count == 1) |
69 |
| - return generic::Memset<uint8_t>::block(dst, value); |
70 |
| - if (count == 2) |
71 |
| - return generic::Memset<uint16_t>::block(dst, value); |
72 |
| - if (count == 3) |
73 |
| - return generic::MemsetSequence<uint16_t, uint8_t>::block(dst, value); |
74 |
| - if (count <= 8) |
75 |
| - return generic::Memset<uint32_t>::head_tail(dst, value, count); |
76 |
| - if (count <= 16) |
77 |
| - return generic::Memset<uint64_t>::head_tail(dst, value, count); |
78 |
| - if (count <= 32) |
79 |
| - return generic::Memset<uint128_t>::head_tail(dst, value, count); |
80 |
| - if (count <= 64) |
81 |
| - return generic::Memset<uint256_t>::head_tail(dst, value, count); |
82 |
| - if constexpr (x86::kUseSoftwarePrefetchingMemset) { |
83 |
| - return inline_memset_x86_sw_prefetching(dst, value, count); |
84 |
| - } |
85 |
| - if (count <= 128) |
86 |
| - return generic::Memset<uint512_t>::head_tail(dst, value, count); |
87 |
| - // Aligned loop |
88 |
| - generic::Memset<uint256_t>::block(dst, value); |
89 |
| - align_to_next_boundary<32>(dst, count); |
90 |
| - return generic::Memset<uint256_t>::loop_and_tail(dst, value, count); |
91 |
| - } |
92 | 108 | } // namespace LIBC_NAMESPACE
|
93 | 109 |
|
94 | 110 | #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_MEMSET_H
|
0 commit comments