summaryrefslogtreecommitdiff
path: root/libc/test/src/string/memory_utils/utils_test.cpp
diff options
context:
space:
mode:
authorGuillaume Chatelet <gchatelet@google.com>2022-10-14 12:42:34 +0000
committerGuillaume Chatelet <gchatelet@google.com>2022-10-14 12:42:54 +0000
commit9721687835a7df5da0c9482cf684c11b8ba97f75 (patch)
treec83ed6b14aec725071137fdd4bdb82cacaa595b8 /libc/test/src/string/memory_utils/utils_test.cpp
parent5a859432f31716f780cd662741864ef1a77fc28a (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. It also provides minimal implementations for ARM platforms. Codegen can be checked here https://godbolt.org/z/x19zvE59v 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);
}