1af68c3b8SCheng Wang //===-- Implementation of memcmp ------------------------------------------===//
2af68c3b8SCheng Wang //
3af68c3b8SCheng Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4af68c3b8SCheng Wang // See https://llvm.org/LICENSE.txt for license information.
5af68c3b8SCheng Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af68c3b8SCheng Wang //
7af68c3b8SCheng Wang //===----------------------------------------------------------------------===//
8af68c3b8SCheng Wang 
9af68c3b8SCheng Wang #include "src/string/memcmp.h"
10af68c3b8SCheng Wang #include "src/__support/common.h"
11af68c3b8SCheng Wang #include <stddef.h> // size_t
12af68c3b8SCheng Wang 
13af68c3b8SCheng Wang namespace __llvm_libc {
14af68c3b8SCheng Wang 
15af68c3b8SCheng Wang // TODO: It is a simple implementation, an optimized version is preparing.
16*a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(int, memcmp,
17*a0b65a7bSMichael Jones                    (const void *lhs, const void *rhs, size_t count)) {
18af68c3b8SCheng Wang   const unsigned char *_lhs = reinterpret_cast<const unsigned char *>(lhs);
19af68c3b8SCheng Wang   const unsigned char *_rhs = reinterpret_cast<const unsigned char *>(rhs);
20af68c3b8SCheng Wang   for (size_t i = 0; i < count; ++i)
21af68c3b8SCheng Wang     if (_lhs[i] != _rhs[i])
22af68c3b8SCheng Wang       return _lhs[i] - _rhs[i];
23af68c3b8SCheng Wang   // count is 0 or _lhs and _rhs are the same.
24af68c3b8SCheng Wang   return 0;
25af68c3b8SCheng Wang }
26af68c3b8SCheng Wang 
27af68c3b8SCheng Wang } // namespace __llvm_libc
28