base: Add addIntlvBits to AddrRange
[gem5.git] / src / base / addr_range.test.cc
1 /*
2 * Copyright (c) 2018-2019 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Nikos Nikoleris
38 */
39
40 #include <gtest/gtest.h>
41
42 #include "base/addr_range.hh"
43 #include "base/bitfield.hh"
44
45 TEST(AddrRangeComp, AddrRangeIsSubset)
46 {
47 AddrRange r, r1, r2;
48
49 // Test non-interleaved ranges
50 r1 = AddrRange(0x0, 0x7f);
51 r2 = AddrRange(0x80, 0xff);
52
53 r = AddrRange(0x0, 0xf);
54 EXPECT_TRUE(r.isSubset(r1));
55 EXPECT_FALSE(r.isSubset(r2));
56
57 r = AddrRange(0x80, 0x8f);
58 EXPECT_FALSE(r.isSubset(r1));
59 EXPECT_TRUE(r.isSubset(r2));
60
61 // Test interleaved ranges
62 r1 = AddrRange(0x0, 0xff, 6, 0, 1, 0);
63 r2 = AddrRange(0x0, 0xff, 6, 0, 1, 1);
64
65 r = AddrRange(0x0, 0xf);
66 EXPECT_TRUE(r.isSubset(r1));
67 EXPECT_FALSE(r.isSubset(r2));
68
69 r = AddrRange(0x40, 0x4f);
70 EXPECT_FALSE(r.isSubset(r1));
71 EXPECT_TRUE(r.isSubset(r2));
72
73 r = AddrRange(0xbf, 0xc0);
74 EXPECT_FALSE(r.isSubset(r1));
75 EXPECT_FALSE(r.isSubset(r2));
76
77 // Test interleaved ranges with hashing
78 r1 = AddrRange(0x0, 0xff, 6, 7, 1, 0);
79 r2 = AddrRange(0x0, 0xff, 6, 7, 1, 1);
80
81 r = AddrRange(0x0, 0xf);
82 EXPECT_TRUE(r.isSubset(r1));
83 EXPECT_FALSE(r.isSubset(r2));
84
85 r = AddrRange(0x40, 0x4f);
86 EXPECT_FALSE(r.isSubset(r1));
87 EXPECT_TRUE(r.isSubset(r2));
88
89 r = AddrRange(0xbf, 0xc0);
90 EXPECT_FALSE(r.isSubset(r1));
91 EXPECT_FALSE(r.isSubset(r2));
92 }
93
94 class AddrRangeBase : public testing::Test {
95 protected:
96
97 virtual int getIndex(Addr addr) = 0;
98
99 void testContains()
100 {
101 for (Addr addr = start; addr <= end; addr++) {
102 int i = getIndex(addr);
103 ASSERT_TRUE(range[i].contains(addr));
104 for (int j = 1; j < intlvSize; j++) {
105 ASSERT_FALSE(range[(i + j) % intlvSize].contains(addr));
106 }
107 }
108 }
109
110 void testGetOffset()
111 {
112 Addr offsets[intlvSize] = {0, 0, 0, 0};
113 for (Addr addr = start; addr <= end; addr++) {
114 int i = getIndex(addr);
115 Addr offset = range[i].getOffset(addr);
116 ASSERT_EQ(offsets[i], offset);
117 offsets[i]++;
118 }
119 for (Addr offset: offsets) {
120 ASSERT_EQ(offset, (end - start + 1) / intlvSize);
121 }
122 }
123
124 void testAddRemoveIntlvBits()
125 {
126 for (Addr addr = start; addr <= end; addr++) {
127 AddrRange &r = range[getIndex(addr)];
128 Addr ch_addr = r.removeIntlvBits(addr);
129 Addr pa = r.addIntlvBits(ch_addr);
130 ASSERT_EQ(addr, pa);
131 }
132 }
133
134 static const Addr end = 0x1ffff;
135 static const Addr start = 0x0;
136 static const int intlvSize = 4;
137
138 AddrRange range[intlvSize];
139 };
140
141
142 class AddrRangeCont : public AddrRangeBase {
143 protected:
144 void SetUp() override
145 {
146 std::vector<Addr> masks = {
147 1UL << xorBits0[0] | 1UL << xorBits0[1],
148 1UL << xorBits1[0] | 1UL << xorBits1[1]
149 };
150 for (auto i = 0; i < intlvSize; i++) {
151 range[i] = AddrRange(start, end, masks, i);
152 }
153 }
154
155 int getIndex(Addr addr) override
156 {
157 return bits(addr, xorBits1[1], xorBits0[1]) ^
158 bits(addr, xorBits1[0], xorBits0[0]);
159 }
160
161 const int xorBits0[2] = {8, 14};
162 const int xorBits1[2] = {9, 15};
163 };
164
165 TEST_F(AddrRangeCont, AddrRangeContains)
166 {
167 testContains();
168 }
169
170 TEST_F(AddrRangeCont, AddrRangeGetOffset)
171 {
172 testGetOffset();
173 }
174
175 TEST_F(AddrRangeCont, AddrRangeAddRemoveIntlvBits)
176 {
177 testAddRemoveIntlvBits();
178 }
179
180
181 class AddrRangeContLegacy : public AddrRangeCont {
182 protected:
183 void SetUp() override
184 {
185 // Test interleaved ranges with hashing
186 for (auto i = 0; i < intlvSize; i++) {
187 range[i] = AddrRange(start, end, xorBits1[0], xorBits1[1],
188 2, i);
189 }
190 }
191 };
192
193 TEST_F(AddrRangeContLegacy, AddrRangeContains)
194 {
195 testContains();
196 }
197
198 TEST_F(AddrRangeContLegacy, AddrRangeGetOffset)
199 {
200 testGetOffset();
201 }
202
203 TEST_F(AddrRangeContLegacy, AddrRangeAddRemoveIntlvBits)
204 {
205 testAddRemoveIntlvBits();
206 }
207
208 class AddrRangeArb : public AddrRangeBase {
209 protected:
210 void SetUp() override
211 {
212 std::vector<Addr> masks = {
213 1UL << xorBits0[0] | 1UL << xorBits0[1],
214 1UL << xorBits1[0] | 1UL << xorBits1[1]
215 };
216 for (auto i = 0; i < intlvSize; i++) {
217 range[i] = AddrRange(start, end, masks, i);
218 }
219 }
220
221 int getIndex(Addr addr) override
222 {
223 return (bits(addr, xorBits0[0]) ^ bits(addr, xorBits0[1])) |
224 (bits(addr, xorBits1[0]) ^ bits(addr, xorBits1[1])) << 1;
225 }
226
227 const int xorBits0[2] = {11, 12};
228 const int xorBits1[2] = {8, 15};
229 };
230
231 TEST_F(AddrRangeArb, AddrRangeContains)
232 {
233 testContains();
234 }
235
236 TEST_F(AddrRangeArb, AddrRangeGetOffset)
237 {
238 testGetOffset();
239 }
240
241 TEST_F(AddrRangeArb, AddrRangeAddRemoveIntlvBits)
242 {
243 testAddRemoveIntlvBits();
244 }