xref: /aosp_15_r20/external/llvm-libc/src/string/memory_utils/aarch64/inline_bcmp.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Bcmp 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_AARCH64_INLINE_BCMP_H
9 #define LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_BCMP_H
10 
11 #include "src/__support/macros/attributes.h"   // LIBC_INLINE
12 #include "src/__support/macros/config.h"
13 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
14 #include "src/string/memory_utils/op_aarch64.h"
15 #include "src/string/memory_utils/op_generic.h"
16 #include "src/string/memory_utils/utils.h" // Ptr, CPtr
17 
18 #include <stddef.h> // size_t
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
inline_bcmp_aarch64(CPtr p1,CPtr p2,size_t count)22 [[maybe_unused]] LIBC_INLINE BcmpReturnType inline_bcmp_aarch64(CPtr p1,
23                                                                 CPtr p2,
24                                                                 size_t count) {
25   if (LIBC_LIKELY(count <= 32)) {
26     if (LIBC_UNLIKELY(count >= 16)) {
27       return aarch64::Bcmp<16>::head_tail(p1, p2, count);
28     }
29     switch (count) {
30     case 0:
31       return BcmpReturnType::zero();
32     case 1:
33       return generic::Bcmp<uint8_t>::block(p1, p2);
34     case 2:
35       return generic::Bcmp<uint16_t>::block(p1, p2);
36     case 3:
37       return generic::Bcmp<uint16_t>::head_tail(p1, p2, count);
38     case 4:
39       return generic::Bcmp<uint32_t>::block(p1, p2);
40     case 5:
41     case 6:
42     case 7:
43       return generic::Bcmp<uint32_t>::head_tail(p1, p2, count);
44     case 8:
45       return generic::Bcmp<uint64_t>::block(p1, p2);
46     case 9:
47     case 10:
48     case 11:
49     case 12:
50     case 13:
51     case 14:
52     case 15:
53       return generic::Bcmp<uint64_t>::head_tail(p1, p2, count);
54     }
55   }
56 
57   if (count <= 64)
58     return aarch64::Bcmp<32>::head_tail(p1, p2, count);
59 
60   // Aligned loop if > 256, otherwise normal loop
61   if (LIBC_UNLIKELY(count > 256)) {
62     if (auto value = aarch64::Bcmp<32>::block(p1, p2))
63       return value;
64     align_to_next_boundary<16, Arg::P1>(p1, p2, count);
65   }
66   return aarch64::Bcmp<32>::loop_and_tail(p1, p2, count);
67 }
68 
69 } // namespace LIBC_NAMESPACE_DECL
70 
71 #endif // LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_BCMP_H
72