[libc-commits] [libc] [libc] Speed up memmove overlapping check (PR #70017)
Dmitry Vyukov via libc-commits
libc-commits at lists.llvm.org
Tue Oct 24 03:30:06 PDT 2023
================
@@ -89,16 +89,10 @@ template <size_t alignment, typename T> LIBC_INLINE T *assume_aligned(T *ptr) {
// Returns true iff memory regions [p1, p1 + size] and [p2, p2 + size] are
// disjoint.
LIBC_INLINE bool is_disjoint(const void *p1, const void *p2, size_t size) {
- const char *a = static_cast<const char *>(p1);
- const char *b = static_cast<const char *>(p2);
- if (a > b) {
- // Swap a and b, this compiles down to conditionnal move for aarch64, x86
- // and RISCV with zbb extension.
- const char *tmp = a;
- a = b;
- b = tmp;
- }
- return a + size <= b;
+ const size_t diff =
+ static_cast<const char *>(p1) - static_cast<const char *>(p2);
+ // This is expected to compile to conditional move.
+ return static_cast<ptrdiff_t>(diff) >= 0 ? size <= diff : size <= -diff;
----------------
dvyukov wrote:
I agree that assigning the diff initally to ptrdiff_t and not doing any cast when comparing with 0 is cleaner (I did not do it only to save on a cast).
But I am not sure about bit casts. If representations are differnet, then we are asking for random numbers, no? (like casting std::string to std::vector).
If ranges are representable (should be), then implicit casts are ok. Otherwise, there is no amount of magic we can do.
What am I missing?
https://github.com/llvm/llvm-project/pull/70017
More information about the libc-commits
mailing list