|
11 | 11 |
|
12 | 12 | #include "src/__support/architectures.h"
|
13 | 13 | #include "src/__support/common.h"
|
14 |
| -#include "src/string/memory_utils/elements.h" |
| 14 | +#include "src/string/memory_utils/op_aarch64.h" |
| 15 | +#include "src/string/memory_utils/op_builtin.h" |
| 16 | +#include "src/string/memory_utils/op_generic.h" |
| 17 | +#include "src/string/memory_utils/op_x86.h" |
15 | 18 |
|
16 | 19 | #include <stddef.h> // size_t
|
17 | 20 |
|
18 | 21 | namespace __llvm_libc {
|
19 | 22 |
|
20 |
| -// Fixed-size difference between 'lhs' and 'rhs'. |
21 |
| -template <typename Element> bool differs(const char *lhs, const char *rhs) { |
22 |
| - return !Element::equals(lhs, rhs); |
| 23 | +[[maybe_unused]] static inline BcmpReturnType |
| 24 | +inline_bcmp_embedded_tiny(CPtr p1, CPtr p2, size_t count) { |
| 25 | +#pragma nounroll |
| 26 | + for (size_t offset = 0; offset < count; ++offset) |
| 27 | + if (auto value = generic::Bcmp<1>::block(p1 + offset, p2 + offset)) |
| 28 | + return value; |
| 29 | + return BcmpReturnType::ZERO(); |
23 | 30 | }
|
24 |
| -// Runtime-size difference between 'lhs' and 'rhs'. |
25 |
| -template <typename Element> |
26 |
| -bool differs(const char *lhs, const char *rhs, size_t size) { |
27 |
| - return !Element::equals(lhs, rhs, size); |
| 31 | + |
| 32 | +#if defined(LLVM_LIBC_ARCH_X86) || defined(LLVM_LIBC_ARCH_AARCH64) |
| 33 | +[[maybe_unused]] static inline BcmpReturnType |
| 34 | +inline_bcmp_generic_gt16(CPtr p1, CPtr p2, size_t count) { |
| 35 | + if (count < 256) |
| 36 | + return generic::Bcmp<16>::loop_and_tail(p1, p2, count); |
| 37 | + if (auto value = generic::Bcmp<64>::block(p1, p2)) |
| 38 | + return value; |
| 39 | + align_to_next_boundary<64, Arg::P1>(p1, p2, count); |
| 40 | + return generic::Bcmp<64>::loop_and_tail(p1, p2, count); |
28 | 41 | }
|
| 42 | +#endif // defined(LLVM_LIBC_ARCH_X86) || defined(LLVM_LIBC_ARCH_AARCH64) |
29 | 43 |
|
30 |
| -static inline int inline_bcmp(const char *lhs, const char *rhs, size_t count) { |
31 | 44 | #if defined(LLVM_LIBC_ARCH_X86)
|
32 |
| - using namespace ::__llvm_libc::x86; |
33 |
| -#elif defined(LLVM_LIBC_ARCH_AARCH64) |
34 |
| - using namespace ::__llvm_libc::aarch64; |
35 |
| -#else |
36 |
| - using namespace ::__llvm_libc::scalar; |
37 |
| -#endif |
| 45 | +[[maybe_unused]] static inline BcmpReturnType |
| 46 | +inline_bcmp_x86_sse2_gt16(CPtr p1, CPtr p2, size_t count) { |
| 47 | + if (count <= 32) |
| 48 | + return x86::sse2::Bcmp<16>::head_tail(p1, p2, count); |
| 49 | + if (count < 256) |
| 50 | + return x86::sse2::Bcmp<16>::loop_and_tail(p1, p2, count); |
| 51 | + if (auto value = x86::sse2::Bcmp<16>::block(p1, p2)) |
| 52 | + return value; |
| 53 | + align_to_next_boundary<16, Arg::P1>(p1, p2, count); |
| 54 | + return x86::sse2::Bcmp<64>::loop_and_tail(p1, p2, count); |
| 55 | +} |
| 56 | + |
| 57 | +[[maybe_unused]] static inline BcmpReturnType |
| 58 | +inline_bcmp_x86_avx2_gt16(CPtr p1, CPtr p2, size_t count) { |
| 59 | + if (count <= 32) |
| 60 | + return x86::sse2::Bcmp<16>::head_tail(p1, p2, count); |
| 61 | + if (count <= 64) |
| 62 | + return x86::avx2::Bcmp<32>::head_tail(p1, p2, count); |
| 63 | + if (count <= 128) |
| 64 | + return x86::avx2::Bcmp<64>::head_tail(p1, p2, count); |
| 65 | + if (unlikely(count >= 256)) { |
| 66 | + if (auto value = x86::avx2::Bcmp<64>::block(p1, p2)) |
| 67 | + return value; |
| 68 | + align_to_next_boundary<64, Arg::P1>(p1, p2, count); |
| 69 | + } |
| 70 | + return x86::avx2::Bcmp<64>::loop_and_tail(p1, p2, count); |
| 71 | +} |
| 72 | + |
| 73 | +[[maybe_unused]] static inline BcmpReturnType |
| 74 | +inline_bcmp_x86_avx512bw_gt16(CPtr p1, CPtr p2, size_t count) { |
| 75 | + if (count <= 32) |
| 76 | + return x86::sse2::Bcmp<16>::head_tail(p1, p2, count); |
| 77 | + if (count <= 64) |
| 78 | + return x86::avx2::Bcmp<32>::head_tail(p1, p2, count); |
| 79 | + if (count <= 128) |
| 80 | + return x86::avx512bw::Bcmp<64>::head_tail(p1, p2, count); |
| 81 | + if (unlikely(count >= 256)) { |
| 82 | + if (auto value = x86::avx512bw::Bcmp<64>::block(p1, p2)) |
| 83 | + return value; |
| 84 | + align_to_next_boundary<64, Arg::P1>(p1, p2, count); |
| 85 | + } |
| 86 | + return x86::avx512bw::Bcmp<64>::loop_and_tail(p1, p2, count); |
| 87 | +} |
| 88 | + |
| 89 | +[[maybe_unused]] static inline BcmpReturnType inline_bcmp_x86(CPtr p1, CPtr p2, |
| 90 | + size_t count) { |
38 | 91 | if (count == 0)
|
39 |
| - return 0; |
| 92 | + return BcmpReturnType::ZERO(); |
40 | 93 | if (count == 1)
|
41 |
| - return differs<_1>(lhs, rhs); |
| 94 | + return generic::Bcmp<1>::block(p1, p2); |
42 | 95 | if (count == 2)
|
43 |
| - return differs<_2>(lhs, rhs); |
44 |
| - if (count == 3) |
45 |
| - return differs<_3>(lhs, rhs); |
| 96 | + return generic::Bcmp<2>::block(p1, p2); |
| 97 | + if (count <= 4) |
| 98 | + return generic::Bcmp<2>::head_tail(p1, p2, count); |
46 | 99 | if (count <= 8)
|
47 |
| - return differs<HeadTail<_4>>(lhs, rhs, count); |
| 100 | + return generic::Bcmp<4>::head_tail(p1, p2, count); |
48 | 101 | if (count <= 16)
|
49 |
| - return differs<HeadTail<_8>>(lhs, rhs, count); |
50 |
| - if (count <= 32) |
51 |
| - return differs<HeadTail<_16>>(lhs, rhs, count); |
| 102 | + return generic::Bcmp<8>::head_tail(p1, p2, count); |
| 103 | + if constexpr (x86::kAvx512BW) |
| 104 | + return inline_bcmp_x86_avx512bw_gt16(p1, p2, count); |
| 105 | + else if constexpr (x86::kAvx2) |
| 106 | + return inline_bcmp_x86_avx2_gt16(p1, p2, count); |
| 107 | + else if constexpr (x86::kSse2) |
| 108 | + return inline_bcmp_x86_sse2_gt16(p1, p2, count); |
| 109 | + else |
| 110 | + return inline_bcmp_generic_gt16(p1, p2, count); |
| 111 | +} |
| 112 | +#endif // defined(LLVM_LIBC_ARCH_X86) |
| 113 | + |
| 114 | +#if defined(LLVM_LIBC_ARCH_AARCH64) |
| 115 | +[[maybe_unused]] static inline BcmpReturnType |
| 116 | +inline_bcmp_aarch64(CPtr p1, CPtr p2, size_t count) { |
| 117 | + if (likely(count <= 32)) { |
| 118 | + if (unlikely(count >= 16)) { |
| 119 | + return generic::Bcmp<16>::head_tail(p1, p2, count); |
| 120 | + } |
| 121 | + switch (count) { |
| 122 | + case 0: |
| 123 | + return BcmpReturnType::ZERO(); |
| 124 | + case 1: |
| 125 | + return generic::Bcmp<1>::block(p1, p2); |
| 126 | + case 2: |
| 127 | + return generic::Bcmp<2>::block(p1, p2); |
| 128 | + case 3: |
| 129 | + return generic::Bcmp<2>::head_tail(p1, p2, count); |
| 130 | + case 4: |
| 131 | + return generic::Bcmp<4>::block(p1, p2); |
| 132 | + case 5: |
| 133 | + case 6: |
| 134 | + case 7: |
| 135 | + return generic::Bcmp<4>::head_tail(p1, p2, count); |
| 136 | + case 8: |
| 137 | + return generic::Bcmp<8>::block(p1, p2); |
| 138 | + case 9: |
| 139 | + case 10: |
| 140 | + case 11: |
| 141 | + case 12: |
| 142 | + case 13: |
| 143 | + case 14: |
| 144 | + case 15: |
| 145 | + return generic::Bcmp<8>::head_tail(p1, p2, count); |
| 146 | + } |
| 147 | + } |
| 148 | + |
52 | 149 | if (count <= 64)
|
53 |
| - return differs<HeadTail<_32>>(lhs, rhs, count); |
54 |
| - if (count <= 128) |
55 |
| - return differs<HeadTail<_64>>(lhs, rhs, count); |
56 |
| - return differs<Align<_32>::Then<Loop<_32>>>(lhs, rhs, count); |
| 150 | + return generic::Bcmp<32>::head_tail(p1, p2, count); |
| 151 | + |
| 152 | + // Aligned loop if > 256, otherwise normal loop |
| 153 | + if (count > 256) { |
| 154 | + if (auto value = generic::Bcmp<32>::block(p1, p2)) |
| 155 | + return value; |
| 156 | + align_to_next_boundary<16, Arg::P1>(p1, p2, count); |
| 157 | + } |
| 158 | + return generic::Bcmp<32>::loop_and_tail(p1, p2, count); |
| 159 | +} |
| 160 | +#endif // defined(LLVM_LIBC_ARCH_AARCH64) |
| 161 | + |
| 162 | +static inline BcmpReturnType inline_bcmp(CPtr p1, CPtr p2, size_t count) { |
| 163 | +#if defined(LLVM_LIBC_ARCH_X86) |
| 164 | + return inline_bcmp_x86(p1, p2, count); |
| 165 | +#elif defined(LLVM_LIBC_ARCH_AARCH64) |
| 166 | + return inline_bcmp_aarch64(p1, p2, count); |
| 167 | +#elif defined(LLVM_LIBC_ARCH_ARM) |
| 168 | + return inline_bcmp_embedded_tiny(p1, p2, count); |
| 169 | +#else |
| 170 | +#error "Unsupported platform" |
| 171 | +#endif |
| 172 | +} |
| 173 | + |
| 174 | +static inline int inline_bcmp(const void *p1, const void *p2, size_t count) { |
| 175 | + return static_cast<int>(inline_bcmp(reinterpret_cast<CPtr>(p1), |
| 176 | + reinterpret_cast<CPtr>(p2), count)); |
57 | 177 | }
|
58 | 178 |
|
59 | 179 | } // namespace __llvm_libc
|
|
0 commit comments