gem5  v21.1.0.2
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 using namespace gem5;
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. It does not zero bits past the
170  * sign bit if it was zero.
171  */
172 TEST(BitfieldTest, SignExtendPositiveInput)
173 {
174  int8_t val = 14;
175  int64_t output = 14;
176  EXPECT_EQ(output, sext<8>(val));
177 }
178 
179 TEST(BitfieldTest, SignExtendNegativeInput)
180 {
181  int8_t val = -14;
182  uint64_t output = -14;
183  EXPECT_EQ(output, sext<8>(val));
184 }
185 
186 TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
187 {
188  EXPECT_EQ((1 << 10), sext<8>(1 << 10));
189 }
190 
191 TEST(BitfieldTest, SignExtendNegativeInputOutsideRange)
192 {
193  uint64_t val = 0x4800000010000008;
194  uint64_t output = 0xF800000010000008;
195  EXPECT_EQ(output, sext<60>(val));
196 }
197 /*
198  * The following tests the "szext<N>(X)" function. szext carries out a sign
199  * extention from N bits to 64 bits on value X. Will zero bits past the sign
200  * bit if it was zero.
201  */
202 TEST(BitfieldTest, SignZeroExtendPositiveInput)
203 {
204  int8_t val = 14;
205  int64_t output = 14;
206  EXPECT_EQ(output, szext<8>(val));
207 }
208 
209 TEST(BitfieldTest, SignZeroExtendNegativeInput)
210 {
211  int8_t val = -14;
212  uint64_t output = -14;
213  EXPECT_EQ(output, szext<8>(val));
214 }
215 
216 TEST(BitfieldTest, SignZeroExtendPositiveInputOutsideRange)
217 {
218  EXPECT_EQ(0, szext<8>(1 << 10));
219 }
220 
221 TEST(BitfieldTest, SignZeroExtendNegativeInputOutsideRange)
222 {
223  uint64_t val = 0x4800000010000008;
224  uint64_t output = 0xF800000010000008;
225  EXPECT_EQ(output, szext<60>(val));
226 }
227 
228 /* The following tests "insertBits(A, B, C, D)". insertBits returns A
229  * with bits B to C set to D's (B - C) LSBs. "insertBits(A, B, D)" overrides
230  * the function to insert only B's LSB to position B.
231  */
232 TEST(BitfieldTest, InsertOneBitTo3)
233 {
234  int64_t val = 0;
235  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
236  EXPECT_EQ((1 << 3), insertBits(val, 3, bits));
237 }
238 
239 TEST(BitfieldTest, InsertOneBitTo18)
240 {
241  int64_t val = 0;
242  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1) + 1;
243  EXPECT_EQ((1 << 18), insertBits(val, 18, bits));
244 }
245 
246 TEST(BitfieldTest, InsertOneBitTo3LsbZero)
247 {
248  int64_t val = 0;
249  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
250  EXPECT_EQ(0, insertBits(val, 3, bits));
251 }
252 
253 TEST(BitfieldTest, InsertOneBitTo18LsbZero)
254 {
255  int64_t val = 0;
256  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
257  EXPECT_EQ(0, insertBits(val, 18, bits));
258 }
259 
260 TEST(BitfieldTest, InsertOnBitTo8LsbZero)
261 {
262  int64_t val = (1 << 8);
263  int64_t bits = (1 << 3) + (1 << 2) + (1 << 1);
264  EXPECT_EQ(0, insertBits(val, 8, bits));
265 }
266 
267 TEST(BitfieldTest, InsertMultipleBits)
268 {
269  int64_t val = (1ULL << 63);
270  int64_t bits = (1 << 2) + 1;
271  EXPECT_EQ(val + (1 << 5) + (1 << 3), insertBits(val, 5, 3, bits));
272 }
273 
274 TEST(BitfieldTest, InsertMultipleBitsOverwrite)
275 {
276  int64_t val = (1 << 29);
277  int64_t bits = (1 << 2) + 1;
278  EXPECT_EQ((1 << 30) + (1 << 28), insertBits(val, 30, 28, bits));
279 }
280 
281 // The following tests the "reverseBits" function.
282 TEST(BitfieldTest, ReverseBits8Bit)
283 {
284  uint8_t value = (1 << 7);
285  EXPECT_EQ(1, reverseBits(value));
286 }
287 
288 TEST(BitfieldTest, ReverseBits64Bit)
289 {
290  uint64_t value = 0xF0F0F0F0F0F0F0F1;
291  EXPECT_EQ(0x8F0F0F0F0F0F0F0F, reverseBits(value));
292 }
293 
294 /* The following tests "findMsb" and "findLsb". These return the most position
295  * of the MSBs/LSBs of the input value.
296  */
297 TEST(BitfieldTest, FindMsb29)
298 {
299  uint64_t val = (1 << 29) + (1 << 1);
300  EXPECT_EQ(29, findMsbSet(val));
301 }
302 
303 TEST(BitfieldTest, FindMsb63)
304 {
305  uint64_t val = (1ULL << 63) + (1ULL << 60) + (1 << 1);
306  EXPECT_EQ(63, findMsbSet(val));
307 }
308 
309 
310 TEST(BitfieldTest, FindMsbZero)
311 {
312  EXPECT_EQ(0, findMsbSet(0));
313 }
314 
315 TEST(BitfieldTest, FindLsb)
316 {
317  uint64_t val = (1ULL << 63) + (1 << 1);
318  EXPECT_EQ(1, findLsbSet(val));
319 }
320 
321 TEST(BitfieldTest, FindLsbZero)
322 {
323  EXPECT_EQ(64, findLsbSet(0));
324 }
325 
326 /*
327  * The following tests "popCount(X)". popCount counts the number of bits set to
328  * one.
329  */
330 TEST(BitfieldTest, PopCountNoBits)
331 {
332  EXPECT_EQ(0, popCount(0));
333 }
334 
335 TEST(BitfieldTest, PopCountOneBit)
336 {
337  int64_t val = (1 << 9);
338  EXPECT_EQ(1, popCount(val));
339 }
340 
341 TEST(BitfieldTest, PopCountManyBits)
342 {
343  int64_t val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
344  EXPECT_EQ(5, popCount(val));
345 }
346 
347 TEST(BitfieldTest, PopCountAllOnes)
348 {
349  int64_t val = 0xFFFFFFFFFFFFFFFF;
350  EXPECT_EQ(64, popCount(val));
351 }
352 
353 /*
354  * The following tests the "alignToPowerOfTwo(x)" function which rounds
355  * uint64_t x up to the nearest power of two. If x is already a power
356  * of two, that power is returned.
357  */
358 TEST(BitfieldTest, AlignToPowerOfTwo0)
359 {
360  EXPECT_EQ(0, alignToPowerOfTwo(0));
361 }
362 
363 TEST(BitfieldTest, AlignToPowerOfTwo3)
364 {
365  EXPECT_EQ(4, alignToPowerOfTwo(3));
366 }
367 
368 TEST(BitfieldTest, AlignToPowerOfTwo5)
369 {
370  EXPECT_EQ(8, alignToPowerOfTwo(5));
371 }
372 
373 TEST(BitfieldTest, AlignToPowerOfTwo10)
374 {
375  EXPECT_EQ(16, alignToPowerOfTwo(10));
376 }
377 
378 TEST(BitfieldTest, AlignToPowerOfTwo16)
379 {
380  EXPECT_EQ(16, alignToPowerOfTwo(16));
381 }
382 
383 TEST(BitfieldTest, AlignToPowerOfTwo31)
384 {
385  EXPECT_EQ(32, alignToPowerOfTwo(31));
386 }
387 
388 /*
389  * The following tests test ctz32/64. The value returned in all cases should
390  * be equal to the number of trailing zeros (i.e., the number before the first
391  * bit set to one).
392  */
393 
394 TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
395 {
396  int32_t value = 1;
397  EXPECT_EQ(0, ctz32(value));
398 }
399 
400 TEST(BitfieldTest, CountTrailingZeros32Bits)
401 {
402  uint32_t value = (1 << 30) + (1 << 29);
403  EXPECT_EQ(29, ctz32(value));
404 }
405 
406 TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
407 {
408  uint64_t value = (1 << 29) + 1;
409  EXPECT_EQ(0, ctz64(value));
410 }
411 
412 TEST(BitfieldTest, CountTrailingZeros64Bits)
413 {
414  uint64_t value = 1ULL << 63;
415  EXPECT_EQ(63, ctz64(value));
416 }
417 
418 TEST(BitfieldTest, CountTrailingZero64AllZeros)
419 {
420  uint64_t value = 0;
421  EXPECT_EQ(64, ctz64(value));
422 }
gem5::output
static void output(const char *filename)
Definition: debug.cc:66
gem5::reverseBits
std::enable_if_t< std::is_integral< T >::value &&sizeof(T) !=1, T > reverseBits(T val, size_t size=sizeof(T))
Takes a value and returns the bit reversed version.
Definition: bitfield.hh:236
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
gem5::mbits
constexpr T mbits(T val, unsigned first, unsigned last)
Mask off the given bits in place like bits() but without shifting.
Definition: bitfield.hh:103
gem5::ctz64
constexpr int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition: bitfield.hh:409
gem5::mask
constexpr uint64_t mask(unsigned nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
Definition: bitfield.hh:63
bitfield.hh
gem5::findLsbSet
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition: bitfield.hh:299
gem5::popCount
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition: bitfield.hh:337
gem5::insertBits
constexpr T insertBits(T val, unsigned first, unsigned last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
Definition: bitfield.hh:166
gem5::bits
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:76
TEST
TEST(BitfieldTest, Mask0Bits)
Definition: bitfield.test.cc:48
gem5::RiscvISA::x
Bitfield< 3 > x
Definition: pagetable.hh:73
gem5::ctz32
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:395
gem5::findMsbSet
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:263
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:372

Generated on Tue Sep 21 2021 12:24:52 for gem5 by doxygen 1.8.17