Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / string / memory_utils / inline_strcmp.h
blob2bcd56ad3d6b0b1a6a47969709c752b2753d20cd
1 //===-- str{,case}cmp implementation ----------------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H
10 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H
12 #include <stddef.h>
14 namespace LIBC_NAMESPACE {
16 template <typename Comp>
17 LIBC_INLINE constexpr int inline_strcmp(const char *left, const char *right,
18 Comp &&comp) {
19 // TODO: Look at benefits for comparing words at a time.
20 for (; *left && !comp(*left, *right); ++left, ++right)
22 return comp(*reinterpret_cast<const unsigned char *>(left),
23 *reinterpret_cast<const unsigned char *>(right));
26 template <typename Comp>
27 LIBC_INLINE constexpr int inline_strncmp(const char *left, const char *right,
28 size_t n, Comp &&comp) {
29 if (n == 0)
30 return 0;
32 // TODO: Look at benefits for comparing words at a time.
33 for (; n > 1; --n, ++left, ++right) {
34 char lc = *left;
35 if (!comp(lc, '\0') || comp(lc, *right))
36 break;
38 return comp(*reinterpret_cast<const unsigned char *>(left),
39 *reinterpret_cast<const unsigned char *>(right));
42 } // namespace LIBC_NAMESPACE
44 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H