28#include <gtest/gtest.h>
42 Bitfield<39, 32> byte5;
44 BitfieldRO<39, 32> byte5RO;
45 BitfieldWO<39, 32> byte5WO;
47 Bitfield<43, 42> bits43To42;
49 SignedBitfield<41> bit41Signed;
51 SignedBitfield<47, 40> byte6Signed;
52 SignedBitfieldRO<47, 40> byte6SignedRO;
53 SignedBitfieldWO<47, 40> byte6SignedWO;
72 Bitfield<15, 12> high;
82 getter(
const uint64_t &storage)
const
92 setter(uint64_t &storage, uint64_t
val)
106struct ContainingStruct
109 Bitfield<63, 60> topNibble;
116containingFunc(uint64_t init_val, uint64_t fieldVal)
119 Bitfield<16, 15> field;
122 Contained contained = init_val;
123 contained.field = fieldVal;
144 template <
typename T>
147 template <
typename T>
158 EXPECT_EQ(sixtyFour.byte5, 0);
159 sixtyFour.byte5 = 0xff;
160 EXPECT_EQ(sixtyFour, 0xff00000000);
161 sixtyFour.byte5 = 0xfff;
162 EXPECT_EQ(sixtyFour, 0xff00000000);
163 EXPECT_EQ(sixtyFour.byte5, 0xff);
168 EXPECT_EQ(sixtyFour.bit2, 0);
169 sixtyFour.bit2 = 0x1;
170 EXPECT_EQ(sixtyFour, 0x4);
171 EXPECT_EQ(sixtyFour.bit2, 0x1);
176 EXPECT_EQ(sixtyFour.byte5RO, 0);
177 sixtyFour.byte5 = 0xff;
178 EXPECT_EQ(sixtyFour.byte5RO, 0xff);
183 sixtyFour.byte5WO = 0xff;
184 EXPECT_EQ(sixtyFour, 0xff00000000);
189 EXPECT_EQ(sixtyFour.byte6.bit41, 0);
190 sixtyFour.byte6 = 0x2;
191 EXPECT_EQ(sixtyFour.byte6.bit41, 1);
192 sixtyFour.byte6.bits43To42 = 0x3;
193 EXPECT_EQ(sixtyFour.byte6, 0xe);
194 sixtyFour.byte6 = 0xff;
195 sixtyFour.byte6.bit41 = 0;
196 EXPECT_EQ(sixtyFour, 0xfd0000000000);
201 sixtyFour.byte6 = 0xff;
202 EXPECT_EQ(sixtyFour.byte6Signed, -1);
203 EXPECT_EQ(sixtyFour.byte6SignedRO, -1);
204 sixtyFour.byte6SignedWO = 0;
205 EXPECT_EQ(sixtyFour.byte6Signed, 0);
206 EXPECT_EQ(sixtyFour.byte6SignedRO, 0);
207 EXPECT_EQ(sixtyFour.byte6, 0);
212 ContainingStruct containing;
213 containing.contained = 0;
214 containing.contained.topNibble = 0xd;
215 EXPECT_EQ(containing.contained, 0xd000000000000000);
220 EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
225 SixtyFour otherSixtyFour = 0;
227 otherSixtyFour.byte6.bit41 = sixtyFour.bit2;
228 EXPECT_EQ(otherSixtyFour, 0x20000000000);
229 otherSixtyFour.bit2 = sixtyFour.bit2;
230 EXPECT_EQ(otherSixtyFour, 0x20000000004);
235 SixtyFour otherSixtyFour = 0x4;
236 sixtyFour = otherSixtyFour;
237 EXPECT_EQ(sixtyFour, 0x4);
239 EXPECT_TRUE(sixtyFour < otherSixtyFour);
240 EXPECT_TRUE(otherSixtyFour > sixtyFour);
241 EXPECT_TRUE(sixtyFour != otherSixtyFour);
242 sixtyFour = otherSixtyFour;
243 EXPECT_TRUE(sixtyFour == otherSixtyFour);
250 EXPECT_EQ(split, 0xf0f0);
251 EXPECT_EQ((uint64_t)split.split, 0xff);
257 EXPECT_EQ(templatedFunction(sixtyFour), 0xff);
258 EXPECT_EQ(templatedFunction((uint64_t)sixtyFour), 0);
267 is64 = std::is_same_v<BitUnionBaseType<Dummy64>, uint64_t>;
269 is64 = std::is_same_v<BitUnionBaseType<Dummy32>, uint64_t>;
275 sixtyFour = 1234567812345678;
276 std::stringstream
ss;
278 EXPECT_EQ(
ss.str(),
"1234567812345678");
281 EmptyEight eight = 65;
283 EXPECT_EQ(
ss.str(),
"65");
EmptyThirtyTwo emptyThirtyTwo
EmptySixteen emptySixteen
EmptySixtyFour emptySixtyFour
TEST_F(BitUnionData, NormalBitfield)
uint64_t templatedFunction(BitUnionType< T > u)
uint64_t templatedFunction(T)
typename bitfield_backend::BitUnionBaseType< T >::Type BitUnionBaseType
#define BitUnion64(name)
Use this to define conveniently sized values overlayed with bitfields.
#define EndBitUnion(name)
This closes off the class and union started by the above macro.
#define SubBitUnion(name, first, last)
Regular bitfields These define macros for read/write regular bitfield based subbitfields.
#define EndSubBitUnion(name)
This closes off the union created above and gives it a name.
#define BitUnion(type, name)
Use this to define an arbitrary type overlayed with bitfields.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.