gem5 v23.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
38#include <gtest/gtest.h>
39
40#include "base/bitfield.hh"
41
42using 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 */
48TEST(BitfieldTest, Mask0Bits)
49{
50 EXPECT_EQ(0x0, mask(0));
51}
52
53TEST(BitfieldTest, Mask1Bit)
54{
55 EXPECT_EQ(0x1, mask(1));
56}
57
58TEST(BitfieldTest, Mask8Bits)
59{
60 EXPECT_EQ(0xFF, mask(8));
61}
62
63TEST(BitfieldTest, Mask16Bits)
64{
65 EXPECT_EQ(0xFFFF, mask(16));
66}
67
68TEST(BitfieldTest, Mask32Bits)
69{
70 EXPECT_EQ(0xFFFFFFFF, mask(32));
71}
72
73TEST(BitfieldTest, MaskAllBits)
74{
75 EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(64));
76}
77
78TEST(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 */
90TEST(BitfieldTest, MaskOneBit)
91{
92 EXPECT_EQ(1, mask(0, 0));
93}
94
95TEST(BitfieldTest, MaskTwoBits)
96{
97 EXPECT_EQ((1 << 1) + 1, mask(1, 0));
98}
99
100TEST(BitfieldTest, MaskThreeBits)
101{
102 EXPECT_EQ((1 << 5) + (1 << 4) + (1 << 3), mask(5, 3));
103}
104
105TEST(BitfieldTest, MaskEntireRange)
106{
107 EXPECT_EQ(0xFFFFFFFFFFFFFFFF, mask(63, 0));
108}
109
110TEST(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 */
120TEST(BitfieldTest, ExtractOneBit)
121{
122 int32_t x = 1 << 31;
123 EXPECT_EQ(1, bits(x, 31));
124}
125
126TEST(BitfieldTest, Extract63rdBit)
127{
128 int64_t x = 1ULL << 63;
129 EXPECT_EQ(1, bits(x, 63));
130}
131
132TEST(BitfieldTest, ExtractFirstBit)
133{
134 int64_t x = 1;
135 EXPECT_EQ(1, bits(x, 0));
136}
137
138TEST(BitfieldTest, ExtractFirstBitFirstBitZero)
139{
140 int64_t x = 1 << 1;
141 EXPECT_EQ(0, bits(x, 0));
142}
143
144TEST(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 */
155TEST(BitfieldTest, MbitsStandardCase)
156{
157 uint64_t x = (1 << 10) + (1 << 1);
158 EXPECT_EQ((1 << 10), mbits(x, 10, 8));
159}
160
161TEST(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 */
172TEST(BitfieldTest, SignExtendPositiveInput)
173{
174 int8_t val = 14;
175 int64_t output = 14;
176 EXPECT_EQ(output, sext<8>(val));
177}
178
179TEST(BitfieldTest, SignExtendNegativeInput)
180{
181 int8_t val = -14;
182 uint64_t output = -14;
183 EXPECT_EQ(output, sext<8>(val));
184}
185
186TEST(BitfieldTest, SignExtendPositiveInputOutsideRange)
187{
188 EXPECT_EQ((1 << 10), sext<8>(1 << 10));
189}
190
191TEST(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 */
202TEST(BitfieldTest, SignZeroExtendPositiveInput)
203{
204 int8_t val = 14;
205 int64_t output = 14;
206 EXPECT_EQ(output, szext<8>(val));
207}
208
209TEST(BitfieldTest, SignZeroExtendNegativeInput)
210{
211 int8_t val = -14;
212 uint64_t output = -14;
213 EXPECT_EQ(output, szext<8>(val));
214}
215
216TEST(BitfieldTest, SignZeroExtendPositiveInputOutsideRange)
217{
218 EXPECT_EQ(0, szext<8>(1 << 10));
219}
220
221TEST(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 */
232TEST(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
239TEST(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
246TEST(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
253TEST(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
260TEST(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
267TEST(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
274TEST(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.
282TEST(BitfieldTest, ReverseBits8Bit)
283{
284 uint8_t value = (1 << 7);
285 EXPECT_EQ(1, reverseBits(value));
286}
287
288TEST(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 */
297TEST(BitfieldTest, FindMsb29)
298{
299 uint64_t val = (1 << 29) + (1 << 1);
300 EXPECT_EQ(29, findMsbSet(val));
301}
302
303TEST(BitfieldTest, FindMsb63)
304{
305 uint64_t val = (1ULL << 63) + (1ULL << 60) + (1 << 1);
306 EXPECT_EQ(63, findMsbSet(val));
307}
308
309
310TEST(BitfieldTest, FindMsbZero)
311{
312 EXPECT_EQ(0, findMsbSet(0));
313}
314
315TEST(BitfieldTest, FindLsb)
316{
317 uint64_t val = (1ULL << 63) + (1 << 1);
318 EXPECT_EQ(1, findLsbSet(val));
319}
320
321TEST(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 */
330TEST(BitfieldTest, PopCountNoBits)
331{
332 EXPECT_EQ(0, popCount(0));
333}
334
335TEST(BitfieldTest, PopCountOneBit)
336{
337 int64_t val = (1 << 9);
338 EXPECT_EQ(1, popCount(val));
339}
340
341TEST(BitfieldTest, PopCountManyBits)
342{
343 int64_t val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
344 EXPECT_EQ(5, popCount(val));
345}
346
347TEST(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 */
358TEST(BitfieldTest, AlignToPowerOfTwo0)
359{
360 EXPECT_EQ(0, alignToPowerOfTwo(0));
361}
362
363TEST(BitfieldTest, AlignToPowerOfTwo3)
364{
365 EXPECT_EQ(4, alignToPowerOfTwo(3));
366}
367
368TEST(BitfieldTest, AlignToPowerOfTwo5)
369{
370 EXPECT_EQ(8, alignToPowerOfTwo(5));
371}
372
373TEST(BitfieldTest, AlignToPowerOfTwo10)
374{
375 EXPECT_EQ(16, alignToPowerOfTwo(10));
376}
377
378TEST(BitfieldTest, AlignToPowerOfTwo16)
379{
380 EXPECT_EQ(16, alignToPowerOfTwo(16));
381}
382
383TEST(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
394TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
395{
396 int32_t value = 1;
397 EXPECT_EQ(0, ctz32(value));
398}
399
400TEST(BitfieldTest, CountTrailingZeros32Bits)
401{
402 uint32_t value = (1 << 30) + (1 << 29);
403 EXPECT_EQ(29, ctz32(value));
404}
405
406TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
407{
408 uint64_t value = (1 << 29) + 1;
409 EXPECT_EQ(0, ctz64(value));
410}
411
412TEST(BitfieldTest, CountTrailingZeros64Bits)
413{
414 uint64_t value = 1ULL << 63;
415 EXPECT_EQ(63, ctz64(value));
416}
417
418TEST(BitfieldTest, CountTrailingZero64AllZeros)
419{
420 uint64_t value = 0;
421 EXPECT_EQ(64, ctz64(value));
422}
423
424/*
425 * The following tests test clz32/64. The value returned in all cases should
426 * be equal to the number of leading zeros (i.e., the number of zeroes before
427 * the first bit set to one counting from the MSB).
428 */
429
430TEST(BitfieldTest, CountLeadingZeros32BitsNoTrailing)
431{
432 int32_t value = 1;
433 EXPECT_EQ(31, clz32(value));
434}
435
436TEST(BitfieldTest, CountLeadingZeros32Bits)
437{
438 uint32_t value = (1 << 30) + (1 << 29);
439 EXPECT_EQ(1, clz32(value));
440}
441
442TEST(BitfieldTest, CountLeadingZeros64BitsNoTrailing)
443{
444 uint64_t value = (1 << 29) + 1;
445 EXPECT_EQ(34, clz64(value));
446}
447
448TEST(BitfieldTest, CountLeadingZeros64Bits)
449{
450 uint64_t value = 1ULL << 63;
451 EXPECT_EQ(0, clz64(value));
452}
453
454TEST(BitfieldTest, CountLeadingZero64AllZeros)
455{
456 uint64_t value = 0;
457 EXPECT_EQ(64, clz64(value));
458}
TEST(BitfieldTest, Mask0Bits)
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition bitfield.hh:276
constexpr int clz32(uint32_t value)
Count leading zeros in a 32-bit value.
Definition bitfield.hh:436
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
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition bitfield.hh:350
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
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition bitfield.hh:408
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:182
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition bitfield.hh:385
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition bitfield.hh:312
constexpr int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition bitfield.hh:422
constexpr int clz64(uint64_t value)
Count leading zeros in a 64-bit value.
Definition bitfield.hh:450
std::enable_if_t< std::is_integral_v< T >, T > reverseBits(T val, size_t size=sizeof(T))
Takes a value and returns the bit reversed version.
Definition bitfield.hh:252
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 3 > x
Definition pagetable.hh:73
Bitfield< 63 > val
Definition misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
static void output(const char *filename)
Definition debug.cc:60

Generated on Mon Jul 10 2023 14:24:28 for gem5 by doxygen 1.9.7