Skip to content

Commit

Permalink
Revert "[libc] New version of the mem* framework"
Browse files Browse the repository at this point in the history
This reverts commit d55f2d8.
  • Loading branch information
gchatelet committed Oct 13, 2022
1 parent d55f2d8 commit 9d1f646
Show file tree
Hide file tree
Showing 26 changed files with 1,831 additions and 1,635 deletions.
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/string_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void StringWriter::write(char new_char, size_t len) {
len = available_capacity;

if (len > 0) {
inline_memset(cur_buffer, static_cast<uint8_t>(new_char), len);
inline_memset(cur_buffer, new_char, len);
cur_buffer += len;
available_capacity -= len;
}
Expand Down
4 changes: 2 additions & 2 deletions libc/src/string/bcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, bcmp,
(const void *lhs, const void *rhs, size_t count)) {
return static_cast<int>(inline_bcmp(static_cast<const char *>(lhs),
static_cast<const char *>(rhs), count));
return inline_bcmp(static_cast<const char *>(lhs),
static_cast<const char *>(rhs), count);
}

} // namespace __llvm_libc
4 changes: 2 additions & 2 deletions libc/src/string/memcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, memcmp,
(const void *lhs, const void *rhs, size_t count)) {
return static_cast<int>(inline_memcmp(static_cast<const char *>(lhs),
static_cast<const char *>(rhs), count));
return inline_memcmp(static_cast<const char *>(lhs),
static_cast<const char *>(rhs), count);
}

} // namespace __llvm_libc
53 changes: 14 additions & 39 deletions libc/src/string/memmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,36 @@
#include "src/string/memmove.h"

#include "src/__support/common.h"
#include "src/string/memory_utils/op_aarch64.h"
#include "src/string/memory_utils/op_generic.h"
#include "src/string/memory_utils/op_x86.h"
#include "src/__support/integer_operations.h"
#include "src/string/memory_utils/elements.h"
#include <stddef.h> // size_t, ptrdiff_t

#include <stdio.h>

namespace __llvm_libc {

static inline void inline_memmove(char *dst, const char *src, size_t count) {
#if defined(LLVM_LIBC_ARCH_X86) || defined(LLVM_LIBC_ARCH_AARCH64)
#if defined(LLVM_LIBC_ARCH_X86)
static constexpr size_t kMaxSize = x86::kAvx512F ? 64
: x86::kAvx ? 32
: x86::kSse2 ? 16
: 8;
#elif defined(LLVM_LIBC_ARCH_AARCH64)
static constexpr size_t kMaxSize = aarch64::kNeon ? 16 : 8;
#endif
using namespace __llvm_libc::scalar;
if (count == 0)
return;
if (count == 1)
return generic::Memmove<1, kMaxSize>::block(dst, src);
return move<_1>(dst, src);
if (count <= 4)
return generic::Memmove<2, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_2>>(dst, src, count);
if (count <= 8)
return generic::Memmove<4, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_4>>(dst, src, count);
if (count <= 16)
return generic::Memmove<8, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_8>>(dst, src, count);
if (count <= 32)
return generic::Memmove<16, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_16>>(dst, src, count);
if (count <= 64)
return generic::Memmove<32, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_32>>(dst, src, count);
if (count <= 128)
return generic::Memmove<64, kMaxSize>::head_tail(dst, src, count);
return move<HeadTail<_64>>(dst, src, count);

if (dst < src) {
generic::Memmove<32, kMaxSize>::align_forward<Arg::Src>(dst, src, count);
return generic::Memmove<64, kMaxSize>::loop_and_tail_forward(dst, src,
count);
} else {
generic::Memmove<32, kMaxSize>::align_backward<Arg::Src>(dst, src, count);
return generic::Memmove<64, kMaxSize>::loop_and_tail_backward(dst, src,
count);
}
#elif defined(LLVM_LIBC_ARCH_ARM)
if (count == 0)
return;
using AlignedMoveLoop = Align<_16, Arg::Src>::Then<Loop<_64>>;
if (dst < src)
return generic::Memmove<1, 1>::loop_forward(dst, src, count);
else
return generic::Memmove<1, 1>::loop_backward(dst, src, count);
#else
#error "Unsupported platform"
#endif
return move<AlignedMoveLoop>(dst, src, count);
else if (dst > src)
return move_backward<AlignedMoveLoop>(dst, src, count);
}

LLVM_LIBC_FUNCTION(void *, memmove,
Expand Down
8 changes: 2 additions & 6 deletions libc/src/string/memory_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
add_header_library(
memory_utils
HDRS
utils.h
elements.h
bcmp_implementations.h
bzero_implementations.h
memcmp_implementations.h
memcpy_implementations.h
memset_implementations.h
op_aarch64.h
op_higher_order.h
op_builtin.h
op_generic.h
op_x86.h
utils.h
DEPS
libc.src.__support.CPP.bit
)
Expand Down
97 changes: 0 additions & 97 deletions libc/src/string/memory_utils/README.md

This file was deleted.

143 changes: 28 additions & 115 deletions libc/src/string/memory_utils/bcmp_implementations.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,136 +11,49 @@

#include "src/__support/architectures.h"
#include "src/__support/common.h"
#include "src/string/memory_utils/op_aarch64.h"
#include "src/string/memory_utils/op_generic.h"
#include "src/string/memory_utils/op_x86.h"
#include "src/string/memory_utils/elements.h"

#include <stddef.h> // size_t

namespace __llvm_libc {

#if defined(LLVM_LIBC_ARCH_X86) || defined(LLVM_LIBC_ARCH_AARCH64)
static inline BcmpReturnType inline_bcmp_generic_gt16(CPtr p1, CPtr p2,
size_t count) {
if (count < 256)
return generic::Bcmp<16>::loop_and_tail(p1, p2, count);
if (auto value = generic::Bcmp<64>::block(p1, p2))
return value;
align_to_next_boundary<64, Arg::P1>(p1, p2, count);
return generic::Bcmp<64>::loop_and_tail(p1, p2, count);
// Fixed-size difference between 'lhs' and 'rhs'.
template <typename Element> bool differs(const char *lhs, const char *rhs) {
return !Element::equals(lhs, rhs);
}
#endif // defined(LLVM_LIBC_ARCH_X86) || defined(LLVM_LIBC_ARCH_AARCH64)

#if defined(LLVM_LIBC_ARCH_X86)
static inline BcmpReturnType inline_bcmp_x86_sse2_gt16(CPtr p1, CPtr p2,
size_t count) {
if (count <= 32)
return x86::sse2::Bcmp<16>::head_tail(p1, p2, count);
if (count < 256)
return x86::sse2::Bcmp<16>::loop_and_tail(p1, p2, count);
if (auto value = x86::sse2::Bcmp<16>::block(p1, p2))
return value;
align_to_next_boundary<16, Arg::P1>(p1, p2, count);
return x86::sse2::Bcmp<64>::loop_and_tail(p1, p2, count);
}

static inline BcmpReturnType inline_bcmp_x86_avx2_gt16(CPtr p1, CPtr p2,
size_t count) {
if (count <= 32)
return x86::sse2::Bcmp<16>::head_tail(p1, p2, count);
if (count <= 64)
return x86::avx2::Bcmp<32>::head_tail(p1, p2, count);
if (count <= 128)
return x86::avx2::Bcmp<64>::head_tail(p1, p2, count);
if (unlikely(count >= 256)) {
if (auto value = x86::avx2::Bcmp<64>::block(p1, p2))
return value;
align_to_next_boundary<64, Arg::P1>(p1, p2, count);
}
return x86::avx2::Bcmp<64>::loop_and_tail(p1, p2, count);
}

static inline BcmpReturnType inline_bcmp_x86_avx512bw_gt16(CPtr p1, CPtr p2,
size_t count) {
if (count <= 32)
return x86::sse2::Bcmp<16>::head_tail(p1, p2, count);
if (count <= 64)
return x86::avx2::Bcmp<32>::head_tail(p1, p2, count);
if (count <= 128)
return x86::avx512bw::Bcmp<64>::head_tail(p1, p2, count);
if (unlikely(count >= 256)) {
if (auto value = x86::avx512bw::Bcmp<64>::block(p1, p2))
return value;
align_to_next_boundary<64, Arg::P1>(p1, p2, count);
}
return x86::avx512bw::Bcmp<64>::loop_and_tail(p1, p2, count);
// Runtime-size difference between 'lhs' and 'rhs'.
template <typename Element>
bool differs(const char *lhs, const char *rhs, size_t size) {
return !Element::equals(lhs, rhs, size);
}
#endif // defined(LLVM_LIBC_ARCH_X86)

static inline BcmpReturnType inline_bcmp(CPtr p1, CPtr p2, size_t count) {
static inline int inline_bcmp(const char *lhs, const char *rhs, size_t count) {
#if defined(LLVM_LIBC_ARCH_X86)
using namespace ::__llvm_libc::x86;
#elif defined(LLVM_LIBC_ARCH_AARCH64)
using namespace ::__llvm_libc::aarch64;
#else
using namespace ::__llvm_libc::scalar;
#endif
if (count == 0)
return BcmpReturnType::ZERO();
return 0;
if (count == 1)
return generic::Bcmp<1>::block(p1, p2);
return differs<_1>(lhs, rhs);
if (count == 2)
return generic::Bcmp<2>::block(p1, p2);
if (count <= 4)
return generic::Bcmp<2>::head_tail(p1, p2, count);
return differs<_2>(lhs, rhs);
if (count == 3)
return differs<_3>(lhs, rhs);
if (count <= 8)
return generic::Bcmp<4>::head_tail(p1, p2, count);
return differs<HeadTail<_4>>(lhs, rhs, count);
if (count <= 16)
return generic::Bcmp<8>::head_tail(p1, p2, count);
if constexpr (x86::kAvx512BW)
return inline_bcmp_x86_avx512bw_gt16(p1, p2, count);
else if constexpr (x86::kAvx2)
return inline_bcmp_x86_avx2_gt16(p1, p2, count);
else if constexpr (x86::kSse2)
return inline_bcmp_x86_sse2_gt16(p1, p2, count);
else
return inline_bcmp_generic_gt16(p1, p2, count);
#elif defined(LLVM_LIBC_ARCH_AARCH64)
if (likely(count <= 32)) {
if (unlikely(count >= 16)) {
return generic::Bcmp<16>::head_tail(p1, p2, count);
}
switch (count) {
case 0:
return BcmpReturnType::ZERO();
case 1:
return generic::Bcmp<1>::block(p1, p2);
case 2:
return generic::Bcmp<2>::block(p1, p2);
case 3:
return generic::Bcmp<2>::head_tail(p1, p2, count);
case 4:
return generic::Bcmp<4>::block(p1, p2);
case 5 ... 7:
return generic::Bcmp<4>::head_tail(p1, p2, count);
case 8:
return generic::Bcmp<8>::block(p1, p2);
case 9 ... 15:
return generic::Bcmp<8>::head_tail(p1, p2, count);
}
}

return differs<HeadTail<_8>>(lhs, rhs, count);
if (count <= 32)
return differs<HeadTail<_16>>(lhs, rhs, count);
if (count <= 64)
return generic::Bcmp<32>::head_tail(p1, p2, count);

// Aligned loop if > 256, otherwise normal loop
if (count > 256) {
if (auto value = generic::Bcmp<32>::block(p1, p2))
return value;
align_to_next_boundary<16, Arg::P1>(p1, p2, count);
}
return generic::Bcmp<32>::loop_and_tail(p1, p2, count);
#elif defined(LLVM_LIBC_ARCH_ARM)
if (count == 0)
return BcmpReturnType::ZERO();
return generic::Bcmp<1>::loop(p1, p2, count);
#else
#error "Unsupported platform"
#endif
return differs<HeadTail<_32>>(lhs, rhs, count);
if (count <= 128)
return differs<HeadTail<_64>>(lhs, rhs, count);
return differs<Align<_32>::Then<Loop<_32>>>(lhs, rhs, count);
}

} // namespace __llvm_libc
Expand Down

0 comments on commit 9d1f646

Please sign in to comment.