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/common.h" 11 #include <stddef.h> // size_t 12 13 namespace __llvm_libc { 14 15 // TODO: It is a simple implementation, an optimized version is preparing. 16 LLVM_LIBC_FUNCTION(int, memcmp, 17 (const void *lhs, const void *rhs, size_t count)) { 18 const unsigned char *_lhs = reinterpret_cast<const unsigned char *>(lhs); 19 const unsigned char *_rhs = reinterpret_cast<const unsigned char *>(rhs); 20 for (size_t i = 0; i < count; ++i) 21 if (_lhs[i] != _rhs[i]) 22 return _lhs[i] - _rhs[i]; 23 // count is 0 or _lhs and _rhs are the same. 24 return 0; 25 } 26 27 } // namespace __llvm_libc 28