xref: /llvm-project/libc/test/src/string/memory_utils/utils_test.cpp (revision 9d1f6466e56409b93ca0786702fd0db155d5edcd)
1 //===-- Unittests for memory_utils ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/CPP/array.h"
10 #include "src/string/memory_utils/utils.h"
11 #include "utils/UnitTest/Test.h"
12 
13 namespace __llvm_libc {
14 
15 TEST(LlvmLibcUtilsTest, IsPowerOfTwoOrZero) {
16   static const cpp::array<bool, 65> kExpectedValues{
17       1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 0-15
18       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
19       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47
20       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63
21       1                                               // 64
22   };
23   for (size_t i = 0; i < kExpectedValues.size(); ++i)
24     EXPECT_EQ(is_power2_or_zero(i), kExpectedValues[i]);
25 }
26 
27 TEST(LlvmLibcUtilsTest, IsPowerOfTwo) {
28   static const cpp::array<bool, 65> kExpectedValues{
29       0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // 0-15
30       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
31       1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47
32       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 48-63
33       1                                               // 64
34   };
35   for (size_t i = 0; i < kExpectedValues.size(); ++i)
36     EXPECT_EQ(is_power2(i), kExpectedValues[i]);
37 }
38 
39 TEST(LlvmLibcUtilsTest, Log2) {
40   static const cpp::array<size_t, 65> kExpectedValues{
41       0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 0-15
42       4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 16-31
43       5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 32-47
44       5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 48-63
45       6                                               // 64
46   };
47   for (size_t i = 0; i < kExpectedValues.size(); ++i)
48     EXPECT_EQ(log2(i), kExpectedValues[i]);
49 }
50 
51 TEST(LlvmLibcUtilsTest, LEPowerOf2) {
52   static const cpp::array<size_t, 65> kExpectedValues{
53       0,  1,  2,  2,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,  8,  // 0-15
54       16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, // 16-31
55       32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // 32-47
56       32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // 48-63
57       64                                                              // 64
58   };
59   for (size_t i = 0; i < kExpectedValues.size(); ++i)
60     EXPECT_EQ(le_power2(i), kExpectedValues[i]);
61 }
62 
63 TEST(LlvmLibcUtilsTest, GEPowerOf2) {
64   static const cpp::array<size_t, 66> kExpectedValues{
65       0,  1,  2,  4,  4,  8,  8,  8,  8,  16, 16, 16, 16, 16, 16, 16, // 0-15
66       16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // 16-31
67       32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 32-47
68       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 48-63
69       64, 128                                                         // 64-65
70   };
71   for (size_t i = 0; i < kExpectedValues.size(); ++i)
72     EXPECT_EQ(ge_power2(i), kExpectedValues[i]);
73 }
74 
75 using I = intptr_t;
76 
77 // Converts an offset into a pointer.
78 const void *forge(size_t offset) {
79   return reinterpret_cast<const void *>(offset);
80 }
81 
82 TEST(LlvmLibcUtilsTest, OffsetToNextAligned) {
83   EXPECT_EQ(offset_to_next_aligned<16>(forge(0)), I(0));
84   EXPECT_EQ(offset_to_next_aligned<16>(forge(1)), I(15));
85   EXPECT_EQ(offset_to_next_aligned<16>(forge(16)), I(0));
86   EXPECT_EQ(offset_to_next_aligned<16>(forge(15)), I(1));
87   EXPECT_EQ(offset_to_next_aligned<32>(forge(16)), I(16));
88 }
89 
90 TEST(LlvmLibcUtilsTest, OffsetFromLastAligned) {
91   EXPECT_EQ(offset_from_last_aligned<16>(forge(0)), I(0));
92   EXPECT_EQ(offset_from_last_aligned<16>(forge(1)), I(1));
93   EXPECT_EQ(offset_from_last_aligned<16>(forge(16)), I(0));
94   EXPECT_EQ(offset_from_last_aligned<16>(forge(15)), I(15));
95   EXPECT_EQ(offset_from_last_aligned<32>(forge(16)), I(16));
96 }
97 
98 TEST(LlvmLibcUtilsTest, OffsetToNextCacheLine) {
99   EXPECT_GT(LLVM_LIBC_CACHELINE_SIZE, 0);
100   EXPECT_EQ(offset_to_next_cache_line(forge(0)), I(0));
101   EXPECT_EQ(offset_to_next_cache_line(forge(1)),
102             I(LLVM_LIBC_CACHELINE_SIZE - 1));
103   EXPECT_EQ(offset_to_next_cache_line(forge(LLVM_LIBC_CACHELINE_SIZE)), I(0));
104   EXPECT_EQ(offset_to_next_cache_line(forge(LLVM_LIBC_CACHELINE_SIZE - 1)),
105             I(1));
106 }
107 
108 TEST(LlvmLibcUtilsTest, Adjust1) {
109   char a;
110   const size_t base_size = 10;
111   for (size_t I = -2; I < 2; ++I) {
112     auto *ptr = &a;
113     size_t size = base_size;
114     adjust(I, ptr, size);
115     EXPECT_EQ(intptr_t(ptr), intptr_t(&a + I));
116     EXPECT_EQ(size, base_size - I);
117   }
118 }
119 
120 TEST(LlvmLibcUtilsTest, Adjust2) {
121   char a, b;
122   const size_t base_size = 10;
123   for (size_t I = -2; I < 2; ++I) {
124     auto *p1 = &a;
125     auto *p2 = &b;
126     size_t size = base_size;
127     adjust(I, p1, p2, size);
128     EXPECT_EQ(intptr_t(p1), intptr_t(&a + I));
129     EXPECT_EQ(intptr_t(p2), intptr_t(&b + I));
130     EXPECT_EQ(size, base_size - I);
131   }
132 }
133 
134 TEST(LlvmLibcUtilsTest, Align1) {
135   char a;
136   const size_t base_size = 10;
137   {
138     auto *ptr = &a;
139     size_t size = base_size;
140     align<128>(ptr, size);
141     EXPECT_TRUE(uintptr_t(ptr) % 128 == 0);
142     EXPECT_GE(ptr, &a);
143     EXPECT_EQ(size_t(ptr - &a), base_size - size);
144   }
145 }
146 
147 TEST(LlvmLibcUtilsTest, Align2) {
148   char a, b;
149   const size_t base_size = 10;
150   {
151     auto *p1 = &a;
152     auto *p2 = &b;
153     size_t size = base_size;
154     align<128, Arg::_1>(p1, p2, size);
155     EXPECT_TRUE(uintptr_t(p1) % 128 == 0);
156     EXPECT_GE(p1, &a);
157     EXPECT_GE(p2, &b);
158     EXPECT_EQ(size_t(p1 - &a), base_size - size);
159     EXPECT_EQ(size_t(p2 - &b), base_size - size);
160   }
161   {
162     auto *p1 = &a;
163     auto *p2 = &b;
164     size_t size = base_size;
165     align<128, Arg::_2>(p1, p2, size);
166     EXPECT_TRUE(uintptr_t(p2) % 128 == 0);
167     EXPECT_GE(p1, &a);
168     EXPECT_GE(p2, &b);
169     EXPECT_EQ(size_t(p1 - &a), base_size - size);
170     EXPECT_EQ(size_t(p2 - &b), base_size - size);
171   }
172 }
173 
174 } // namespace __llvm_libc
175