summaryrefslogtreecommitdiff
path: root/libc/test/src/string/memory_utils/utils_test.cpp
diff options
context:
space:
mode:
authorGuillaume Chatelet <gchatelet@google.com>2022-10-04 08:49:02 +0000
committerGuillaume Chatelet <gchatelet@google.com>2022-10-12 15:26:26 +0000
commit4c19439d249256db720e323a446e39d05496732f (patch)
tree2dab116d139e6097be5dfc61c68e55ee0ae00499 /libc/test/src/string/memory_utils/utils_test.cpp
parentd71ad4108056d685f48407447095d8d92fd7685d (diff)
[libc] New version of the mem* framework
This version is more composable and also simpler at the expense of being more explicit and more verbose. This patch is not meant to be submitted but gives an idea of the change. Codegen can be checked in https://godbolt.org/z/6z1dEoWbs by removing the "static inline" before individual functions. Unittests are coming. Suggested review order: - utils - op_base - op_builtin - op_generic - op_x86 / op_aarch64 - *_implementations.h Differential Revision: https://reviews.llvm.org/D135134
Diffstat (limited to 'libc/test/src/string/memory_utils/utils_test.cpp')
-rw-r--r--libc/test/src/string/memory_utils/utils_test.cpp79
1 files changed, 26 insertions, 53 deletions
diff --git a/libc/test/src/string/memory_utils/utils_test.cpp b/libc/test/src/string/memory_utils/utils_test.cpp
index a20c0900b723..5c7920c4960b 100644
--- a/libc/test/src/string/memory_utils/utils_test.cpp
+++ b/libc/test/src/string/memory_utils/utils_test.cpp
@@ -72,55 +72,41 @@ TEST(LlvmLibcUtilsTest, GEPowerOf2) {
EXPECT_EQ(ge_power2(i), kExpectedValues[i]);
}
-using I = intptr_t;
+using UINT = uintptr_t;
// Converts an offset into a pointer.
const void *forge(size_t offset) {
return reinterpret_cast<const void *>(offset);
}
-TEST(LlvmLibcUtilsTest, OffsetToNextAligned) {
- EXPECT_EQ(offset_to_next_aligned<16>(forge(0)), I(0));
- EXPECT_EQ(offset_to_next_aligned<16>(forge(1)), I(15));
- EXPECT_EQ(offset_to_next_aligned<16>(forge(16)), I(0));
- EXPECT_EQ(offset_to_next_aligned<16>(forge(15)), I(1));
- EXPECT_EQ(offset_to_next_aligned<32>(forge(16)), I(16));
+TEST(LlvmLibcUtilsTest, DistanceToNextAligned) {
+ EXPECT_EQ(distance_to_next_aligned<16>(forge(0)), UINT(16));
+ EXPECT_EQ(distance_to_next_aligned<16>(forge(1)), UINT(15));
+ EXPECT_EQ(distance_to_next_aligned<16>(forge(16)), UINT(16));
+ EXPECT_EQ(distance_to_next_aligned<16>(forge(15)), UINT(1));
+ EXPECT_EQ(distance_to_next_aligned<32>(forge(16)), UINT(16));
}
-TEST(LlvmLibcUtilsTest, OffsetFromLastAligned) {
- EXPECT_EQ(offset_from_last_aligned<16>(forge(0)), I(0));
- EXPECT_EQ(offset_from_last_aligned<16>(forge(1)), I(1));
- EXPECT_EQ(offset_from_last_aligned<16>(forge(16)), I(0));
- EXPECT_EQ(offset_from_last_aligned<16>(forge(15)), I(15));
- EXPECT_EQ(offset_from_last_aligned<32>(forge(16)), I(16));
+TEST(LlvmLibcUtilsTest, DistanceToAlignUp) {
+ EXPECT_EQ(distance_to_align_up<16>(forge(0)), UINT(0));
+ EXPECT_EQ(distance_to_align_up<16>(forge(1)), UINT(15));
+ EXPECT_EQ(distance_to_align_up<16>(forge(16)), UINT(0));
+ EXPECT_EQ(distance_to_align_up<16>(forge(15)), UINT(1));
+ EXPECT_EQ(distance_to_align_up<32>(forge(16)), UINT(16));
}
-TEST(LlvmLibcUtilsTest, OffsetToNextCacheLine) {
- EXPECT_GT(LLVM_LIBC_CACHELINE_SIZE, 0);
- EXPECT_EQ(offset_to_next_cache_line(forge(0)), I(0));
- EXPECT_EQ(offset_to_next_cache_line(forge(1)),
- I(LLVM_LIBC_CACHELINE_SIZE - 1));
- EXPECT_EQ(offset_to_next_cache_line(forge(LLVM_LIBC_CACHELINE_SIZE)), I(0));
- EXPECT_EQ(offset_to_next_cache_line(forge(LLVM_LIBC_CACHELINE_SIZE - 1)),
- I(1));
-}
-
-TEST(LlvmLibcUtilsTest, Adjust1) {
- char a;
- const size_t base_size = 10;
- for (size_t I = -2; I < 2; ++I) {
- auto *ptr = &a;
- size_t size = base_size;
- adjust(I, ptr, size);
- EXPECT_EQ(intptr_t(ptr), intptr_t(&a + I));
- EXPECT_EQ(size, base_size - I);
- }
+TEST(LlvmLibcUtilsTest, DistanceToAlignDown) {
+ EXPECT_EQ(distance_to_align_down<16>(forge(0)), UINT(0));
+ EXPECT_EQ(distance_to_align_down<16>(forge(1)), UINT(1));
+ EXPECT_EQ(distance_to_align_down<16>(forge(16)), UINT(0));
+ EXPECT_EQ(distance_to_align_down<16>(forge(15)), UINT(15));
+ EXPECT_EQ(distance_to_align_down<32>(forge(16)), UINT(16));
}
TEST(LlvmLibcUtilsTest, Adjust2) {
char a, b;
const size_t base_size = 10;
- for (size_t I = -2; I < 2; ++I) {
+ for (ptrdiff_t I = -2; I < 2; ++I) {
auto *p1 = &a;
auto *p2 = &b;
size_t size = base_size;
@@ -131,19 +117,6 @@ TEST(LlvmLibcUtilsTest, Adjust2) {
}
}
-TEST(LlvmLibcUtilsTest, Align1) {
- char a;
- const size_t base_size = 10;
- {
- auto *ptr = &a;
- size_t size = base_size;
- align<128>(ptr, size);
- EXPECT_TRUE(uintptr_t(ptr) % 128 == 0);
- EXPECT_GE(ptr, &a);
- EXPECT_EQ(size_t(ptr - &a), base_size - size);
- }
-}
-
TEST(LlvmLibcUtilsTest, Align2) {
char a, b;
const size_t base_size = 10;
@@ -151,10 +124,10 @@ TEST(LlvmLibcUtilsTest, Align2) {
auto *p1 = &a;
auto *p2 = &b;
size_t size = base_size;
- align<128, Arg::_1>(p1, p2, size);
+ align_to_next_boundary<128, Arg::P1>(p1, p2, size);
EXPECT_TRUE(uintptr_t(p1) % 128 == 0);
- EXPECT_GE(p1, &a);
- EXPECT_GE(p2, &b);
+ EXPECT_GT(p1, &a);
+ EXPECT_GT(p2, &b);
EXPECT_EQ(size_t(p1 - &a), base_size - size);
EXPECT_EQ(size_t(p2 - &b), base_size - size);
}
@@ -162,10 +135,10 @@ TEST(LlvmLibcUtilsTest, Align2) {
auto *p1 = &a;
auto *p2 = &b;
size_t size = base_size;
- align<128, Arg::_2>(p1, p2, size);
+ align_to_next_boundary<128, Arg::P2>(p1, p2, size);
EXPECT_TRUE(uintptr_t(p2) % 128 == 0);
- EXPECT_GE(p1, &a);
- EXPECT_GE(p2, &b);
+ EXPECT_GT(p1, &a);
+ EXPECT_GT(p2, &b);
EXPECT_EQ(size_t(p1 - &a), base_size - size);
EXPECT_EQ(size_t(p2 - &b), base_size - size);
}