1 //===-- Memcmp implementation for aarch64 -----------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #ifndef LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_MEMCMP_H 9 #define LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_MEMCMP_H 10 11 #include "src/__support/macros/attributes.h" // LIBC_INLINE 12 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY 13 #include "src/string/memory_utils/op_aarch64.h" 14 #include "src/string/memory_utils/op_generic.h" 15 #include "src/string/memory_utils/utils.h" // MemcmpReturnType 16 17 namespace LIBC_NAMESPACE_DECL { 18 19 [[maybe_unused]] LIBC_INLINE MemcmpReturnType 20 inline_memcmp_generic_gt16(CPtr p1, CPtr p2, size_t count) { 21 if (LIBC_UNLIKELY(count >= 384)) { 22 if (auto value = generic::Memcmp<uint8x16_t>::block(p1, p2)) 23 return value; 24 align_to_next_boundary<16, Arg::P1>(p1, p2, count); 25 } 26 return generic::Memcmp<uint8x16_t>::loop_and_tail(p1, p2, count); 27 } 28 29 [[maybe_unused]] LIBC_INLINE MemcmpReturnType 30 inline_memcmp_aarch64_neon_gt16(CPtr p1, CPtr p2, size_t count) { 31 if (LIBC_UNLIKELY(count >= 128)) { // [128, ∞] 32 if (auto value = generic::Memcmp<uint8x16_t>::block(p1, p2)) 33 return value; 34 align_to_next_boundary<16, Arg::P1>(p1, p2, count); 35 return generic::Memcmp<uint8x16x2_t>::loop_and_tail(p1, p2, count); 36 } 37 if (generic::Bcmp<uint8x16_t>::block(p1, p2)) // [16, 16] 38 return generic::Memcmp<uint8x16_t>::block(p1, p2); 39 if (count < 32) // [17, 31] 40 return generic::Memcmp<uint8x16_t>::tail(p1, p2, count); 41 if (generic::Bcmp<uint8x16_t>::block(p1 + 16, p2 + 16)) // [32, 32] 42 return generic::Memcmp<uint8x16_t>::block(p1 + 16, p2 + 16); 43 if (count < 64) // [33, 63] 44 return generic::Memcmp<uint8x16x2_t>::tail(p1, p2, count); 45 // [64, 127] 46 return generic::Memcmp<uint8x16_t>::loop_and_tail(p1 + 32, p2 + 32, 47 count - 32); 48 } 49 50 LIBC_INLINE MemcmpReturnType inline_memcmp_aarch64(CPtr p1, CPtr p2, 51 size_t count) { 52 if (count == 0) 53 return MemcmpReturnType::zero(); 54 if (count == 1) 55 return generic::Memcmp<uint8_t>::block(p1, p2); 56 if (count == 2) 57 return generic::Memcmp<uint16_t>::block(p1, p2); 58 if (count == 3) 59 return generic::MemcmpSequence<uint16_t, uint8_t>::block(p1, p2); 60 if (count <= 8) 61 return generic::Memcmp<uint32_t>::head_tail(p1, p2, count); 62 if (count <= 16) 63 return generic::Memcmp<uint64_t>::head_tail(p1, p2, count); 64 if constexpr (aarch64::kNeon) 65 return inline_memcmp_aarch64_neon_gt16(p1, p2, count); 66 else 67 return inline_memcmp_generic_gt16(p1, p2, count); 68 } 69 } // namespace LIBC_NAMESPACE_DECL 70 71 #endif // LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_MEMCMP_H 72