gem5  v20.1.0.0
bitfield.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The Regents of the University of California
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 
38 #include <gtest/gtest.h>
39 
40 #include "base/bitfield.hh"
41 
42 /*
43  * The following tests the "mask(N)" function. It is assumed that the mask
44  * returned is a 64 bit value with the N LSBs set to one.
45  */
46 TEST(BitfieldTest, Mask0Bits)
47 {
48  EXPECT_EQ(0x0, mask(0));
49 }
50 
51 TEST(BitfieldTest, Mask1Bit)
52 {
53  EXPECT_EQ(0x1, mask(1));
54 }
55 
56 TEST(BitfieldTest, Mask8Bits)
57 {
58  EXPECT_EQ(0xFF, mask(8));
59 }
60 
61 TEST(BitfieldTest, Mask16Bits)
62 {
63  EXPECT_EQ(0xFFFF, mask(16));
64 }
65 
66 TEST(BitfieldTest, Mask32Bits)
67 {
68  EXPECT_EQ(0xFFFFFFFF, mask(32));
69 }
70 
71 TEST(BitfieldTest, MaskAllBits)
72 {
73  EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(64));
74 }
75 
76 TEST(BitfieldTest, MaskAllBitsGreaterThan64)
77 {
78  /* We cannot create a mask greater than 64 bits. It should default to 64
79  * bits if this occurs.
80  */
81  EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(70));
82 }
83 
84 /*
85  * The following tests "mask(X, Y)". mask will create a 64 bit value with bits
86  * X to Y (inclusive) set to one.
87  */
88 TEST(BitfieldTest, MaskOneBit)
89 {
90  EXPECT_EQ(1, mask(0,0));
91 }
92 
93 TEST(BitfieldTest, MaskTwoBits)
94 {
95  EXPECT_EQ((1 << 1) + 1, mask(1,0));
96 }
97 
98 TEST(BitfieldTest, MaskThreeBits)
99 {
100  EXPECT_EQ((1 << 5) + (1 << 4) + (1 << 3), mask(5,3));
101 }
102 
103 TEST(BitfieldTest, MaskEntireRange)
104 {
105  EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(63,0));
106 }
107 
108 TEST(BitfieldTest, MaskOutsideOfRange)
109 {
110  // Masking >64 bits is not possible. The maximum is a 64 bit mask.
111  EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(100, 0));
112 }
113 
114 /*
115  * The following tests "bits". This function extracts bit/bits from the input
116  * value and return them as the LSBs. The remaining bits are set to zero.
117  */
118 TEST(BitfieldTest, ExtractOneBit)
119 {
120  int32_t x = 1 << 31;
121  EXPECT_EQ(1, bits(x, 31));
122 }
123 
124 TEST(BitfieldTest, Extract63rdBit)
125 {
126  int64_t x = 1ULL << 63;
127  EXPECT_EQ(1, bits(x, 63));
128 }
129 
130 TEST(BitfieldTest, ExtractFirstBit)
131 {
132  int64_t x = 1;
133  EXPECT_EQ(1, bits(x, 0));
134 }
135 
136 TEST(BitfieldTest, ExtractFirstBitFirstBitZero)
137 {
138  int64_t x = 1 << 1;
139  EXPECT_EQ(0, bits(x, 0));
140 }
141 
142 TEST(BitfieldTest, ExtractThreeBits)
143 {
144  uint64_t x = 1 << 31;
145  EXPECT_EQ((1 << 2), bits(x, 31, 29));
146 }
147 
148 
149 /*
150  * The following tests "mbits(X, Y, Z)". mbits returns a value with bits Y to
151  * Z from X (in position Y to Z).
152  */
153 TEST(BitfieldTest, MbitsStandardCase)
154 {
155  uint64_t x = (1 << 10) + (1 << 1);
156  EXPECT_EQ((1 << 10), mbits(x, 10, 8));
157 }
158 
159 TEST(BitfieldTest, MbitsEntireRange)
160 {
161  uint64_t x = (1ULL << 63) + 1;
162  EXPECT_EQ((1ULL << 63) + 1, mbits(x, 63, 0));
163 }
164 
165 /*
166  * The following tests the "sext<N>(X)" function. sext carries out a sign
167  * extention from N bits to 64 bits on value X.
168  */
169 TEST(BitfieldTest, SignExtendPositiveInput)
170 {
171  int8_t val = 14;
172  int64_t output = 14;
173  EXPECT_EQ(output, sext<8>(val));
174 }
175 
176 TEST(BitfieldTest, SignExtendNegativeInput)
177 {
178  int8_t val = -14;
179  uint64_t output = -14;
180  EXPECT_EQ(output, sext<8>(val));
181 }
182 
183 TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
184 {
185  EXPECT_EQ((1 << 10), sext<8>(1 << 10));
186 }
187 
188 TEST(BitfieldTest, SignExtendNegativeInputOutsideRange)
189 {
190  uint64_t val = 0x4800000010000008;
191  uint64_t output = 0xF800000010000008;
192  EXPECT_EQ(output, sext<60>(val));
193 }
194 
195 /* The following tests "insertBits(A, B, C, D)". insertBits returns A
196  * with bits B to C set to D's (B - C) LSBs. "insertBits(A, B, D)" overrides
197  * the function to insert only B's LSB to position B.
198  */
199 TEST(BitfieldTest, InsertOneBitTo3)
200 {
201  int64_t val = 0;
202  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
203  EXPECT_EQ((1 << 3), insertBits(val, 3, bits));
204 }
205 
206 TEST(BitfieldTest, InsertOneBitTo18)
207 {
208  int64_t val = 0;
209  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
210  EXPECT_EQ((1 << 18), insertBits(val, 18, bits));
211 }
212 
213 TEST(BitfieldTest, InsertOneBitTo3LsbZero)
214 {
215  int64_t val = 0;
216  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
217  EXPECT_EQ(0, insertBits(val, 3, bits));
218 }
219 
220 TEST(BitfieldTest, InsertOneBitTo18LsbZero)
221 {
222  int64_t val = 0;
223  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
224  EXPECT_EQ(0, insertBits(val, 18, bits));
225 }
226 
227 TEST(BitfieldTest, InsertOnBitTo8LsbZero)
228 {
229  int64_t val = (1 << 8);
230  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
231  EXPECT_EQ(0, insertBits(val, 8, bits));
232 }
233 
234 TEST(BitfieldTest, InsertMultipleBits)
235 {
236  int64_t val = (1ULL << 63);
237  int64_t bits = (1 << 2) + 1;
238  EXPECT_EQ(val + (1 << 5) + (1 << 3), insertBits(val, 5, 3, bits));
239 }
240 
241 TEST(BitfieldTest, InsertMultipleBitsOverwrite)
242 {
243  int64_t val = (1 << 29);
244  int64_t bits = (1 << 2) + 1;
245  EXPECT_EQ((1 << 30) + (1 << 28), insertBits(val, 30, 28, bits));
246 }
247 
248 // The following tests the "reverseBits" function.
249 TEST(BitfieldTest, ReverseBits8Bit)
250 {
251  uint8_t value = (1 << 7);
252  EXPECT_EQ(1, reverseBits(value));
253 }
254 
255 TEST(BitfieldTest, ReverseBits64Bit)
256 {
257  uint64_t value = 0xF0F0F0F0F0F0F0F1;
258  EXPECT_EQ(0x8F0F0F0F0F0F0F0F, reverseBits(value));
259 }
260 
261 /* The following tests "findMsb" and "findLsb". These return the most position
262  * of the MSBs/LSBs of the input value.
263  */
264 TEST(BitfieldTest, FindMsb29)
265 {
266  uint64_t val = (1 << 29) + (1 << 1);
267  EXPECT_EQ(29, findMsbSet(val));
268 }
269 
270 TEST(BitfieldTest, FindMsb63)
271 {
272  uint64_t val = (1ULL << 63) + (1ULL << 60) + (1 << 1);
273  EXPECT_EQ(63, findMsbSet(val));
274 }
275 
276 
277 TEST(BitfieldTest, FindMsbZero)
278 {
279  EXPECT_EQ(0, findMsbSet(0));
280 }
281 
282 TEST(BitfieldTest, FindLsb)
283 {
284  uint64_t val = (1ULL << 63) + (1 << 1);
285  EXPECT_EQ(1, findLsbSet(val));
286 }
287 
288 TEST(BitfieldTest, FindLsbZero)
289 {
290  EXPECT_EQ(64, findLsbSet(0));
291 }
292 
293 /* The following tests a simple function that verifies whether a value is a
294  * a power of two or not.
295  */
296 TEST(BitfieldTest, IsPow2)
297 {
298  EXPECT_TRUE(isPow2(32));
299 }
300 
301 TEST(BitfieldTest, IsNotPow2)
302 {
303  EXPECT_FALSE(isPow2(36));
304 }
305 
306 TEST(BitfieldTest, IsPow2Zero)
307 {
308  EXPECT_TRUE(isPow2(0));
309 }
310 
311 /*
312  * The following tests "popCount(X)". popCount counts the number of bits set to
313  * one.
314  */
315 TEST(BitfieldTest, PopCountNoBits)
316 {
317  EXPECT_EQ(0, popCount(0));
318 }
319 
320 TEST(BitfieldTest, PopCountOneBit)
321 {
322  int64_t val = (1 << 9);
323  EXPECT_EQ(1, popCount(val));
324 }
325 
326 TEST(BitfieldTest, PopCountManyBits)
327 {
328  int64_t val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
329  EXPECT_EQ(5, popCount(val));
330 }
331 
332 TEST(BitfieldTest, PopCountAllOnes)
333 {
334  int64_t val = 0xFFFFFFFFFFFFFFFF;
335  EXPECT_EQ(64, popCount(val));
336 }
337 
338 /*
339  * The following tests the "alignToPowerOfTwo(x)" function which rounds
340  * uint64_t x up to the nearest power of two. If x is already a power
341  * of two, that power is returned.
342  */
343 TEST(BitfieldTest, AlignToPowerOfTwo0)
344 {
346 }
347 
348 TEST(BitfieldTest, AlignToPowerOfTwo3)
349 {
351 }
352 
353 TEST(BitfieldTest, AlignToPowerOfTwo5)
354 {
356 }
357 
358 TEST(BitfieldTest, AlignToPowerOfTwo10)
359 {
360  EXPECT_EQ(16, alignToPowerOfTwo(10));
361 }
362 
363 TEST(BitfieldTest, AlignToPowerOfTwo16)
364 {
365  EXPECT_EQ(16, alignToPowerOfTwo(16));
366 }
367 
368 TEST(BitfieldTest, AlignToPowerOfTwo31)
369 {
370  EXPECT_EQ(32, alignToPowerOfTwo(31));
371 }
372 
373 /*
374  * The following tests test ctz32/64. The value returned in all cases should
375  * be equal to the number of trailing zeros (i.e., the number before the first
376  * bit set to one).
377  */
378 
379 TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
380 {
381  int32_t value = 1;
382  EXPECT_EQ(0, ctz32(value));
383 }
384 
385 TEST(BitfieldTest, CountTrailingZeros32Bits)
386 {
387  uint32_t value = (1 << 30) + (1 << 29);
388  EXPECT_EQ(29, ctz32(value));
389 }
390 
391 TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
392 {
393  uint64_t value = (1 << 29) + 1;
394  EXPECT_EQ(0, ctz64(value));
395 }
396 
397 TEST(BitfieldTest, CountTrailingZeros64Bits)
398 {
399  uint64_t value = 1ULL << 63;
400  EXPECT_EQ(63, ctz64(value));
401 }
402 
403 TEST(BitfieldTest, CountTrailingZero64AllZeros)
404 {
405  uint64_t value = 0;
406  EXPECT_EQ(64, ctz64(value));
407 }
output
static void output(const char *filename)
Definition: debug.cc:60
alignToPowerOfTwo
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:316
findMsbSet
int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:234
mbits
T mbits(T val, int first, int last)
Mask off the given bits in place like bits() but without shifting.
Definition: bitfield.hh:104
popCount
int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition: bitfield.hh:285
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
bitfield.hh
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
RiscvISA::x
Bitfield< 3 > x
Definition: pagetable.hh:69
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
TEST
TEST(BitfieldTest, Mask0Bits)
Definition: bitfield.test.cc:46
ctz64
int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition: bitfield.hh:351
insertBits
T insertBits(T val, int first, int last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
Definition: bitfield.hh:147
reverseBits
T reverseBits(T val, std::size_t size=sizeof(T))
Takes a variable lenght word and returns the mirrored version (Bit by bit, LSB=>MSB).
Definition: bitfield.hh:213
ctz32
int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:338
findLsbSet
int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition: bitfield.hh:253
isPow2
bool isPow2(T v)
Checks if a number is a power of two, or zero.
Definition: bitfield.hh:273
ULL
#define ULL(N)
uint64_t constant
Definition: types.hh:50
ArmISA::mask
Bitfield< 28, 24 > mask
Definition: miscregs_types.hh:711
bits
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:75

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