[libc-commits] [PATCH] D108406: [libc] Add an optimized version for memcmp
Guillaume Chatelet via Phabricator via libc-commits
libc-commits at lists.llvm.org
Thu Aug 19 13:59:05 PDT 2021
gchatelet created this revision.
gchatelet added a reviewer: sivachandra.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added a project: libc-project.
gchatelet requested review of this revision.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D108406
Files:
libc/src/string/memcmp.cpp
Index: libc/src/string/memcmp.cpp
===================================================================
--- libc/src/string/memcmp.cpp
+++ libc/src/string/memcmp.cpp
@@ -8,20 +8,46 @@
#include "src/string/memcmp.h"
#include "src/__support/common.h"
+#include "src/string/memory_utils/elements.h"
+
#include <stddef.h> // size_t
namespace __llvm_libc {
-// TODO: It is a simple implementation, an optimized version is preparing.
+static int memcmp_impl(const char *lhs, const char *rhs, size_t count) {
+#if defined(__i386__) || defined(__x86_64__)
+ using namespace ::__llvm_libc::x86;
+#else
+ using namespace ::__llvm_libc::scalar;
+#endif
+
+ if (count == 0)
+ return 0;
+ if (count == 1)
+ return ThreeWayCompare<_1>(lhs, rhs);
+ if (count == 2)
+ return ThreeWayCompare<_2>(lhs, rhs);
+ if (count == 3)
+ return ThreeWayCompare<_3>(lhs, rhs);
+ if (count <= 8)
+ return ThreeWayCompare<HeadTail<_4>>(lhs, rhs, count);
+ if (count <= 16)
+ return ThreeWayCompare<HeadTail<_8>>(lhs, rhs, count);
+ if (count <= 32)
+ return ThreeWayCompare<HeadTail<_16>>(lhs, rhs, count);
+ if (count <= 64)
+ return ThreeWayCompare<HeadTail<_32>>(lhs, rhs, count);
+ if (count <= 128)
+ return ThreeWayCompare<HeadTail<_64>>(lhs, rhs, count);
+ if (count <= 384)
+ return ThreeWayCompare<Loop<_32>>(lhs, rhs, count);
+ return ThreeWayCompare<Align<_16>::Then<Loop<_32>>>(lhs, rhs, count);
+}
+
LLVM_LIBC_FUNCTION(int, memcmp,
(const void *lhs, const void *rhs, size_t count)) {
- const unsigned char *_lhs = reinterpret_cast<const unsigned char *>(lhs);
- const unsigned char *_rhs = reinterpret_cast<const unsigned char *>(rhs);
- for (size_t i = 0; i < count; ++i)
- if (_lhs[i] != _rhs[i])
- return _lhs[i] - _rhs[i];
- // count is 0 or _lhs and _rhs are the same.
- return 0;
+ return memcmp_impl(static_cast<const char *>(lhs),
+ static_cast<const char *>(rhs), count);
}
-} // namespace __llvm_libc
+} // namespace __llvm_libc
\ No newline at end of file
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108406.367607.patch
Type: text/x-patch
Size: 2058 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210819/bad2c8b2/attachment.bin>
More information about the libc-commits
mailing list