gem5 v24.0.0.0
Loading...
Searching...
No Matches
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 EXPECT_EQ(1, findLsbSetFallback(val));
320}
321
322TEST(BitfieldTest, FindLsbZero)
323{
324 EXPECT_EQ(64, findLsbSet(0));
325}
326
327TEST(BitfieldTest, FindLsbGeneralized)
328{
329 static constexpr size_t N{1000};
330 std::bitset<N> bs{0};
331 EXPECT_EQ(findLsbSet(bs), N);
332 for (size_t i{0}; i < N ; ++i) {
333 bs = std::bitset<N>{1} << i;
334 ASSERT_EQ(findLsbSet(bs), i);
335 }
336
337 const auto leadingOne = std::bitset<N>{1} << (N-1);
338 for (size_t i{0}; i < N ; ++i) {
339 bs = leadingOne | (std::bitset<N>{1} << i);
340 ASSERT_EQ(findLsbSet(bs), i);
341 }
342}
343
344/*
345 * The following tests "popCount(X)". popCount counts the number of bits set to
346 * one.
347 */
348TEST(BitfieldTest, PopCountNoBits)
349{
350 EXPECT_EQ(0, popCount(0));
351}
352
353TEST(BitfieldTest, PopCountOneBit)
354{
355 int64_t val = (1 << 9);
356 EXPECT_EQ(1, popCount(val));
357}
358
359TEST(BitfieldTest, PopCountManyBits)
360{
361 int64_t val = (1 << 22) + (1 << 21) + (1 << 15) + (1 << 9) + 1;
362 EXPECT_EQ(5, popCount(val));
363}
364
365TEST(BitfieldTest, PopCountAllOnes)
366{
367 int64_t val = 0xFFFFFFFFFFFFFFFF;
368 EXPECT_EQ(64, popCount(val));
369}
370
371/*
372 * The following tests the "alignToPowerOfTwo(x)" function which rounds
373 * uint64_t x up to the nearest power of two. If x is already a power
374 * of two, that power is returned.
375 */
376TEST(BitfieldTest, AlignToPowerOfTwo0)
377{
378 EXPECT_EQ(0, alignToPowerOfTwo(0));
379}
380
381TEST(BitfieldTest, AlignToPowerOfTwo3)
382{
383 EXPECT_EQ(4, alignToPowerOfTwo(3));
384}
385
386TEST(BitfieldTest, AlignToPowerOfTwo5)
387{
388 EXPECT_EQ(8, alignToPowerOfTwo(5));
389}
390
391TEST(BitfieldTest, AlignToPowerOfTwo10)
392{
393 EXPECT_EQ(16, alignToPowerOfTwo(10));
394}
395
396TEST(BitfieldTest, AlignToPowerOfTwo16)
397{
398 EXPECT_EQ(16, alignToPowerOfTwo(16));
399}
400
401TEST(BitfieldTest, AlignToPowerOfTwo31)
402{
403 EXPECT_EQ(32, alignToPowerOfTwo(31));
404}
405
406/*
407 * The following tests test ctz32/64. The value returned in all cases should
408 * be equal to the number of trailing zeros (i.e., the number before the first
409 * bit set to one).
410 */
411
412TEST(BitfieldTest, CountTrailingZeros32BitsNoTrailing)
413{
414 int32_t value = 1;
415 EXPECT_EQ(0, ctz32(value));
416}
417
418TEST(BitfieldTest, CountTrailingZeros32Bits)
419{
420 uint32_t value = (1 << 30) + (1 << 29);
421 EXPECT_EQ(29, ctz32(value));
422}
423
424TEST(BitfieldTest, CountTrailingZeros64BitsNoTrailing)
425{
426 uint64_t value = (1 << 29) + 1;
427 EXPECT_EQ(0, ctz64(value));
428}
429
430TEST(BitfieldTest, CountTrailingZeros64Bits)
431{
432 uint64_t value = 1ULL << 63;
433 EXPECT_EQ(63, ctz64(value));
434}
435
436TEST(BitfieldTest, CountTrailingZero64AllZeros)
437{
438 uint64_t value = 0;
439 EXPECT_EQ(64, ctz64(value));
440}
441
442/*
443 * The following tests test clz32/64. The value returned in all cases should
444 * be equal to the number of leading zeros (i.e., the number of zeroes before
445 * the first bit set to one counting from the MSB).
446 */
447
448TEST(BitfieldTest, CountLeadingZeros32BitsNoTrailing)
449{
450 int32_t value = 1;
451 EXPECT_EQ(31, clz32(value));
452}
453
454TEST(BitfieldTest, CountLeadingZeros32Bits)
455{
456 uint32_t value = (1 << 30) + (1 << 29);
457 EXPECT_EQ(1, clz32(value));
458}
459
460TEST(BitfieldTest, CountLeadingZeros64BitsNoTrailing)
461{
462 uint64_t value = (1 << 29) + 1;
463 EXPECT_EQ(34, clz64(value));
464}
465
466TEST(BitfieldTest, CountLeadingZeros64Bits)
467{
468 uint64_t value = 1ULL << 63;
469 EXPECT_EQ(0, clz64(value));
470}
471
472TEST(BitfieldTest, CountLeadingZero64AllZeros)
473{
474 uint64_t value = 0;
475 EXPECT_EQ(64, clz64(value));
476}
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:279
constexpr int clz32(uint32_t value)
Count leading zeros in a 32-bit value.
Definition bitfield.hh:501
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:79
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition bitfield.hh:415
constexpr T mbits(T val, unsigned first, unsigned last)
Mask off the given bits in place like bits() but without shifting.
Definition bitfield.hh:106
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition bitfield.hh:473
constexpr uint64_t szext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition bitfield.hh:161
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:185
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition bitfield.hh:450
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition bitfield.hh:129
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input That function will either use a builtin ...
Definition bitfield.hh:369
constexpr int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition bitfield.hh:487
constexpr int clz64(uint64_t value)
Count leading zeros in a 64-bit value.
Definition bitfield.hh:515
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:255
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 3 > x
Definition pagetable.hh:73
Bitfield< 63 > val
Definition misc.hh:804
Bitfield< 14 > bs
Definition misc.hh:686
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
static void output(const char *filename)
Definition debug.cc:60

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0