gem5  v20.1.0.0
bitunion.test.cc
Go to the documentation of this file.
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)
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
149  {
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 
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 
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 }
BitUnion16
#define BitUnion16(name)
Definition: bitunion.hh:401
BitUnion
#define BitUnion(type, name)
Use this to define an arbitrary type overlayed with bitfields.
Definition: bitunion.hh:392
BitUnionData
Definition: bitunion.test.cc:136
PowerISA::xe
Bitfield< 4 > xe
Definition: miscregs.hh:91
emptySixteen
EmptySixteen emptySixteen
Definition: bitunion.test.cc:133
TEST_F
TEST_F(BitUnionData, NormalBitfield)
Definition: bitunion.test.cc:155
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
emptySixtyFour
EmptySixtyFour emptySixtyFour
Definition: bitunion.test.cc:131
BitUnion64
#define BitUnion64(name)
Use this to define conveniently sized values overlayed with bitfields.
Definition: bitunion.hh:399
BitUnionData::split
Split split
Definition: bitunion.test.cc:139
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
EndSubBitUnion
#define EndSubBitUnion(name)
This closes off the union created above and gives it a name.
Definition: bitunion.hh:360
SubBitUnion
#define SubBitUnion(name, first, last)
Regular bitfields These define macros for read/write regular bitfield based subbitfields.
Definition: bitunion.hh:375
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
bitunion.hh
BitUnionData::sixtyFour
SixtyFour sixtyFour
Definition: bitunion.test.cc:138
BitUnion8
#define BitUnion8(name)
Definition: bitunion.hh:402
cprintf.hh
emptyThirtyTwo
EmptyThirtyTwo emptyThirtyTwo
Definition: bitunion.test.cc:132
BitfieldType
Definition: bitunion.hh:113
BitfieldBackend::BitUnionOperators
Definition: bitunion.hh:247
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
ArmISA::u
Bitfield< 22 > u
Definition: miscregs_types.hh:348
BitUnion32
BitUnion32(UserDescFlags) Bitfield< 0 > seg_32bit
ArmISA::b
Bitfield< 7 > b
Definition: miscregs_types.hh:376
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
BitUnionData::SetUp
void SetUp() override
Definition: bitunion.test.cc:141
BitUnionData::templatedFunction
uint64_t templatedFunction(BitUnionType< T > u)
Definition: bitunion.test.cc:148
BitUnionData::templatedFunction
uint64_t templatedFunction(T)
Definition: bitunion.test.cc:144
EndBitUnion
EndBitUnion(UserDescFlags) struct UserDesc32
Definition: process.cc:149
emptyEight
EmptyEight emptyEight(0)
BitUnionBaseType
typename BitfieldBackend::BitUnionBaseType< T >::Type BitUnionBaseType
Definition: bitunion.hh:445

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17