1 //===-- Implementation of bcmp --------------------------------------------===// 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 9 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_BCMP_IMPLEMENTATIONS_H 10 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_BCMP_IMPLEMENTATIONS_H 11 12 #include "src/__support/architectures.h" 13 #include "src/__support/common.h" 14 #include "src/string/memory_utils/elements.h" 15 16 #include <stddef.h> // size_t 17 18 namespace __llvm_libc { 19 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 } 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); 28 } 29 30 static inline int inline_bcmp(const char *lhs, const char *rhs, size_t count) { 31 #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 38 if (count == 0) 39 return 0; 40 if (count == 1) 41 return differs<_1>(lhs, rhs); 42 if (count == 2) 43 return differs<_2>(lhs, rhs); 44 if (count == 3) 45 return differs<_3>(lhs, rhs); 46 if (count <= 8) 47 return differs<HeadTail<_4>>(lhs, rhs, count); 48 if (count <= 16) 49 return differs<HeadTail<_8>>(lhs, rhs, count); 50 if (count <= 32) 51 return differs<HeadTail<_16>>(lhs, rhs, count); 52 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); 57 } 58 59 } // namespace __llvm_libc 60 61 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_BCMP_IMPLEMENTATIONS_H 62