1 //===-- Implementation of memcmp ------------------------------------------===//
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 #include "src/string/memcmp.h"
10 #include "src/__support/architectures.h"
11 #include "src/__support/common.h"
12 #include "src/string/memory_utils/elements.h"
13 
14 #include <stddef.h> // size_t
15 
16 namespace __llvm_libc {
17 
18 static int memcmp_impl(const char *lhs, const char *rhs, size_t count) {
19 #if defined(LLVM_LIBC_ARCH_X86)
20   using namespace ::__llvm_libc::x86;
21 #else
22   using namespace ::__llvm_libc::scalar;
23 #endif
24 
25   if (count == 0)
26     return 0;
27   if (count == 1)
28     return ThreeWayCompare<_1>(lhs, rhs);
29   if (count == 2)
30     return ThreeWayCompare<_2>(lhs, rhs);
31   if (count == 3)
32     return ThreeWayCompare<_3>(lhs, rhs);
33   if (count <= 8)
34     return ThreeWayCompare<HeadTail<_4>>(lhs, rhs, count);
35   if (count <= 16)
36     return ThreeWayCompare<HeadTail<_8>>(lhs, rhs, count);
37   if (count <= 32)
38     return ThreeWayCompare<HeadTail<_16>>(lhs, rhs, count);
39   if (count <= 64)
40     return ThreeWayCompare<HeadTail<_32>>(lhs, rhs, count);
41   if (count <= 128)
42     return ThreeWayCompare<HeadTail<_64>>(lhs, rhs, count);
43   return ThreeWayCompare<Align<_32>::Then<Loop<_32>>>(lhs, rhs, count);
44 }
45 
46 LLVM_LIBC_FUNCTION(int, memcmp,
47                    (const void *lhs, const void *rhs, size_t count)) {
48   return memcmp_impl(static_cast<const char *>(lhs),
49                      static_cast<const char *>(rhs), count);
50 }
51 
52 } // namespace __llvm_libc
53