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

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13