misc: Standardize the way create() constructs SimObjects.
[gem5.git] / src / base / bitunion.test.cc
1 /*
2 * Copyright 2014 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <gtest/gtest.h>
29
30 #include <cassert>
31 #include <iostream>
32 #include <type_traits>
33
34 #include "base/bitunion.hh"
35 #include "base/cprintf.hh"
36
37 using namespace std;
38
39 namespace {
40
41 BitUnion64(SixtyFour)
42 Bitfield<39, 32> byte5;
43 Bitfield<2> bit2;
44 BitfieldRO<39, 32> byte5RO;
45 BitfieldWO<39, 32> byte5WO;
46 SubBitUnion(byte6, 47, 40)
47 Bitfield<43, 42> bits43To42;
48 Bitfield<41> bit41;
49 SignedBitfield<41> bit41Signed;
50 EndSubBitUnion(byte6)
51 SignedBitfield<47, 40> byte6Signed;
52 SignedBitfieldRO<47, 40> byte6SignedRO;
53 SignedBitfieldWO<47, 40> byte6SignedWO;
54 EndBitUnion(SixtyFour)
55
56 BitUnion64(EmptySixtyFour)
57 EndBitUnion(EmptySixtyFour)
58
59 BitUnion32(EmptyThirtyTwo)
60 EndBitUnion(EmptyThirtyTwo)
61
62 BitUnion16(EmptySixteen)
63 EndBitUnion(EmptySixteen)
64
65 BitUnion8(EmptyEight)
66 EndBitUnion(EmptyEight)
67
68 class SplitField
69 {
70 protected:
71 BitUnion64(In)
72 Bitfield<15, 12> high;
73 Bitfield<7, 4> low;
74 EndBitUnion(In)
75
76 BitUnion64(Out)
77 Bitfield<7, 4> high;
78 Bitfield<3, 0> low;
79 EndBitUnion(Out)
80 public:
81 uint64_t
82 getter(const uint64_t &storage) const
83 {
84 Out out = 0;
85 In in = storage;
86 out.high = in.high;
87 out.low = in.low;
88 return out;
89 }
90
91 void
92 setter(uint64_t &storage, uint64_t val)
93 {
94 Out out = val;
95 In in = 0;
96 in.high = out.high;
97 in.low = out.low;
98 storage = in;
99 }
100 };
101
102 BitUnion64(Split)
103 BitfieldType<SplitField> split;
104 EndBitUnion(Split)
105
106 struct ContainingStruct
107 {
108 BitUnion64(Contained)
109 Bitfield<63, 60> topNibble;
110 EndBitUnion(Contained)
111
112 Contained contained;
113 };
114
115 uint64_t
116 containingFunc(uint64_t init_val, uint64_t fieldVal)
117 {
118 BitUnion32(Contained)
119 Bitfield<16, 15> field;
120 EndBitUnion(Contained)
121
122 Contained contained = init_val;
123 contained.field = fieldVal;
124 return contained;
125 }
126
127 } // anonymous namespace
128
129 // Declare these as global so g++ doesn't ignore them. Initialize them in
130 // various ways.
131 EmptySixtyFour emptySixtyFour = 0;
132 EmptyThirtyTwo emptyThirtyTwo;
133 EmptySixteen emptySixteen;
134 EmptyEight emptyEight(0);
135
136 class BitUnionData : public testing::Test {
137 protected:
138 SixtyFour sixtyFour;
139 Split split;
140
141 void SetUp() override { sixtyFour = 0; split = 0; }
142
143 template <typename T>
144 uint64_t templatedFunction(T) { return 0; }
145
146 template <typename T>
147 uint64_t
148 templatedFunction(BitUnionType<T> u)
149 {
150 BitUnionBaseType<T> b = u;
151 return b;
152 }
153 };
154
155 TEST_F(BitUnionData, NormalBitfield)
156 {
157 EXPECT_EQ(sixtyFour.byte5, 0);
158 sixtyFour.byte5 = 0xff;
159 EXPECT_EQ(sixtyFour, 0xff00000000);
160 sixtyFour.byte5 = 0xfff;
161 EXPECT_EQ(sixtyFour, 0xff00000000);
162 EXPECT_EQ(sixtyFour.byte5, 0xff);
163 }
164
165 TEST_F(BitUnionData, SingleBitfield)
166 {
167 EXPECT_EQ(sixtyFour.bit2, 0);
168 sixtyFour.bit2 = 0x1;
169 EXPECT_EQ(sixtyFour, 0x4);
170 EXPECT_EQ(sixtyFour.bit2, 0x1);
171 }
172
173 TEST_F(BitUnionData, ReadOnlyBitfield)
174 {
175 EXPECT_EQ(sixtyFour.byte5RO, 0);
176 sixtyFour.byte5 = 0xff;
177 EXPECT_EQ(sixtyFour.byte5RO, 0xff);
178 }
179
180 TEST_F(BitUnionData, WriteOnlyBitfield)
181 {
182 sixtyFour.byte5WO = 0xff;
183 EXPECT_EQ(sixtyFour, 0xff00000000);
184 }
185
186 TEST_F(BitUnionData, SubBitUnions)
187 {
188 EXPECT_EQ(sixtyFour.byte6.bit41, 0);
189 sixtyFour.byte6 = 0x2;
190 EXPECT_EQ(sixtyFour.byte6.bit41, 1);
191 sixtyFour.byte6.bits43To42 = 0x3;
192 EXPECT_EQ(sixtyFour.byte6, 0xe);
193 sixtyFour.byte6 = 0xff;
194 sixtyFour.byte6.bit41 = 0;
195 EXPECT_EQ(sixtyFour, 0xfd0000000000);
196 }
197
198 TEST_F(BitUnionData, SignedBitfields)
199 {
200 sixtyFour.byte6 = 0xff;
201 EXPECT_EQ(sixtyFour.byte6Signed, -1);
202 EXPECT_EQ(sixtyFour.byte6SignedRO, -1);
203 sixtyFour.byte6SignedWO = 0;
204 EXPECT_EQ(sixtyFour.byte6Signed, 0);
205 EXPECT_EQ(sixtyFour.byte6SignedRO, 0);
206 EXPECT_EQ(sixtyFour.byte6, 0);
207 }
208
209 TEST_F(BitUnionData, InsideStruct)
210 {
211 ContainingStruct containing;
212 containing.contained = 0;
213 containing.contained.topNibble = 0xd;
214 EXPECT_EQ(containing.contained, 0xd000000000000000);
215 }
216
217 TEST_F(BitUnionData, InsideFunction)
218 {
219 EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
220 }
221
222 TEST_F(BitUnionData, BitfieldToBitfieldAssignment)
223 {
224 SixtyFour otherSixtyFour = 0;
225 sixtyFour.bit2 = 1;
226 otherSixtyFour.byte6.bit41 = sixtyFour.bit2;
227 EXPECT_EQ(otherSixtyFour, 0x20000000000);
228 otherSixtyFour.bit2 = sixtyFour.bit2;
229 EXPECT_EQ(otherSixtyFour, 0x20000000004);
230 }
231
232 TEST_F(BitUnionData, Operators)
233 {
234 SixtyFour otherSixtyFour = 0x4;
235 sixtyFour = otherSixtyFour;
236 EXPECT_EQ(sixtyFour, 0x4);
237 sixtyFour = 0;
238 EXPECT_TRUE(sixtyFour < otherSixtyFour);
239 EXPECT_TRUE(otherSixtyFour > sixtyFour);
240 EXPECT_TRUE(sixtyFour != otherSixtyFour);
241 sixtyFour = otherSixtyFour;
242 EXPECT_TRUE(sixtyFour == otherSixtyFour);
243 }
244
245 TEST_F(BitUnionData, Custom)
246 {
247 EXPECT_EQ(split, 0);
248 split.split = 0xfff;
249 EXPECT_EQ(split, 0xf0f0);
250 EXPECT_EQ((uint64_t)split.split, 0xff);
251 }
252
253 TEST_F(BitUnionData, Templating)
254 {
255 sixtyFour = 0xff;
256 EXPECT_EQ(templatedFunction(sixtyFour), 0xff);
257 EXPECT_EQ(templatedFunction((uint64_t)sixtyFour), 0);
258
259 BitUnion(uint64_t, Dummy64)
260 EndBitUnion(Dummy64);
261
262 BitUnion(uint32_t, Dummy32)
263 EndBitUnion(Dummy32);
264
265 bool is64;
266 is64 = std::is_same<BitUnionBaseType<Dummy64>, uint64_t>::value;
267 EXPECT_TRUE(is64);
268 is64 = std::is_same<BitUnionBaseType<Dummy32>, uint64_t>::value;
269 EXPECT_FALSE(is64);
270 }
271
272 TEST_F(BitUnionData, Output)
273 {
274 sixtyFour = 1234567812345678;
275 std::stringstream ss;
276 ss << sixtyFour;
277 EXPECT_EQ(ss.str(), "1234567812345678");
278 ss.str("");
279
280 EmptyEight eight = 65;
281 ss << eight;
282 EXPECT_EQ(ss.str(), "65");
283 ss.str("");
284 }