gem5 v24.0.0.0
Loading...
Searching...
No Matches
bitfield.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, 2019 ARM Limited
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 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#ifndef __BASE_BITFIELD_HH__
42#define __BASE_BITFIELD_HH__
43
44#include <bitset>
45#include <cassert>
46#include <climits>
47#include <cstddef>
48#include <cstdint>
49#include <limits>
50#include <type_traits>
51
52namespace gem5
53{
54
55extern const uint8_t reverseBitsLookUpTable[];
56
65constexpr uint64_t
66mask(unsigned nbits)
67{
68 return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
69}
70
77template <class T>
78constexpr T
79bits(T val, unsigned first, unsigned last)
80{
81 assert(first >= last);
82 int nbits = first - last + 1;
83 return (val >> last) & mask(nbits);
84}
85
91template <class T>
92constexpr T
93bits(T val, unsigned bit)
94{
95 return bits(val, bit, bit);
96}
97
104template <class T>
105constexpr T
106mbits(T val, unsigned first, unsigned last)
107{
108 return val & (mask(first + 1) & ~mask(last));
109}
110
114constexpr uint64_t
115mask(unsigned first, unsigned last)
116{
117 return mbits((uint64_t)-1LL, first, last);
118}
119
127template <int N>
128constexpr uint64_t
129sext(uint64_t val)
130{
131 bool sign_bit = bits(val, N - 1);
132 if (sign_bit)
133 val |= ~mask(N);
134 return val;
135}
136
144constexpr uint64_t
145sext(uint64_t val, int N)
146{
147 bool sign_bit = bits(val, N - 1);
148 if (sign_bit)
149 val |= ~mask(N);
150 return val;
151}
152
159template <int N>
160constexpr uint64_t
161szext(uint64_t val)
162{
163 bool sign_bit = bits(val, N - 1);
164 if (sign_bit)
165 val |= ~mask(N);
166 else
167 val &= mask(N);
168 return val;
169}
170
183template <class T, class B>
184constexpr T
185insertBits(T val, unsigned first, unsigned last, B bit_val)
186{
187 assert(first >= last);
188 T bmask = mask(first, last);
189 val &= ~bmask;
190 val |= ((T)bit_val << last) & bmask;
191 return val;
192}
193
199template <class T, class B>
200constexpr T
201insertBits(T val, unsigned bit, B bit_val)
202{
203 return insertBits(val, bit, bit, bit_val);
204}
205
214template <class T, class B>
215constexpr void
216replaceBits(T& val, unsigned first, unsigned last, B bit_val)
217{
218 val = insertBits(val, first, last, bit_val);
219}
220
226template <class T, class B>
227constexpr void
228replaceBits(T& val, unsigned bit, B bit_val)
229{
230 val = insertBits(val, bit, bit, bit_val);
231}
232
253template <class T>
254std::enable_if_t<std::is_integral_v<T>, T>
255reverseBits(T val, size_t size=sizeof(T))
256{
257 assert(size <= sizeof(T));
258
259 if constexpr (sizeof(T) == 1) {
261 } else {
262 T output = {};
263
264 for (size_t byte = 0; byte < size; byte++) {
266 val >>= 8;
267 }
268
269 return output;
270 }
271}
272
278constexpr int
280{
281 int msb = 0;
282 if (!val)
283 return 0;
284 if (bits(val, 63, 32)) {
285 msb += 32;
286 val >>= 32;
287 }
288 if (bits(val, 31, 16)) {
289 msb += 16;
290 val >>= 16;
291 }
292 if (bits(val, 15, 8)) {
293 msb += 8;
294 val >>= 8;
295 }
296 if (bits(val, 7, 4)) {
297 msb += 4;
298 val >>= 4;
299 }
300 if (bits(val, 3, 2)) {
301 msb += 2;
302 val >>= 2;
303 }
304 if (bits(val, 1, 1))
305 msb += 1;
306 return msb;
307}
308
309namespace {
310template<typename T>
311constexpr bool
312hasBuiltinCtz() {
313// Since the defined(__has_builtin) in the subsequent #if statement
314// won't short-circuit the macro expansion of
315// __has_builtin(__builtin_ctz), we must explicitly define it as zero
316// if it's undefined to avoid a preprocessor error.
317#ifndef __has_builtin
318# define __has_builtin(foo) 0
319#endif
320#if defined(__has_builtin) && __has_builtin(__builtin_ctz)
321 return sizeof(unsigned long long) >= sizeof(T);
322#else
323 return false;
324#endif
325}
326
327[[maybe_unused]]
328constexpr int
329findLsbSetFallback(uint64_t val) {
330 int lsb = 0;
331 if (!val) {
332 return sizeof(val) * 8;
333 }
334 if (!bits(val, 31, 0)) {
335 lsb += 32;
336 val >>= 32;
337 }
338 if (!bits(val, 15, 0)) {
339 lsb += 16;
340 val >>= 16;
341 }
342 if (!bits(val, 7, 0)) {
343 lsb += 8;
344 val >>= 8;
345 }
346 if (!bits(val, 3, 0)) {
347 lsb += 4;
348 val >>= 4;
349 }
350 if (!bits(val, 1, 0)) {
351 lsb += 2;
352 val >>= 2;
353 }
354 if (!bits(val, 0, 0)) {
355 lsb += 1;
356 }
357 return lsb;
358}
359} // anonymous namespace
360
368constexpr int
369findLsbSet(uint64_t val) {
370 if (val == 0) return 64;
371
372 if constexpr (hasBuiltinCtz<decltype(val)>()) {
373 return __builtin_ctzll(val);
374 } else {
375 return findLsbSetFallback(val);
376 }
377}
378
379
380template<size_t N>
381constexpr int
382findLsbSet(std::bitset<N> bs)
383{
384 if constexpr (N <= 64) {
385 return findLsbSet(bs.to_ullong());
386 } else {
387 if (bs.none()) return N;
388 // Mask of ones
389 constexpr std::bitset<N> mask(std::numeric_limits<uint64_t>::max());
390 // Is the lsb set in the rightmost 64 bits ?
391 auto nextQword{bs & mask};
392 int i{0};
393 while (nextQword.none()) {
394 // If no, shift by 64 bits and repeat
395 i += 64;
396 bs >>= 64;
397 nextQword = bs & mask;
398 }
399 // If yes, account for the bumber of 64-bit shifts and add the
400 // remaining using the uint64_t implementation. Store in intermediate
401 // variable to ensure valid conversion from ullong to uint64_t.
402 uint64_t remaining{nextQword.to_ullong()};
403 return i + findLsbSet(remaining);
404 }
405}
406
414constexpr int
415popCount(uint64_t val)
416{
417#ifndef __has_builtin
418# define __has_builtin(foo) 0
419#endif
420#if defined(__GNUC__) || \
421 (defined(__clang__) && __has_builtin(__builtin_popcountl))
422 return __builtin_popcountl(val);
423#else
424 const uint64_t m1 = 0x5555555555555555ULL; // ..010101b
425 const uint64_t m2 = 0x3333333333333333ULL; // ..110011b
426 const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // ..001111b
427 const uint64_t sum = 0x0101010101010101ULL;
428
429 val -= (val >> 1) & m1; // 2 bits count -> 2 bits
430 val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits
431 val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits
432 return (val * sum) >> 56; // horizontal sum
433#endif // defined(__GNUC__) ||
434 //(defined(__clang__) && __has_builtin(__builtin_popcountl))
435}
436
449constexpr uint64_t
451{
452 val--;
453 val |= val >> 1;
454 val |= val >> 2;
455 val |= val >> 4;
456 val |= val >> 8;
457 val |= val >> 16;
458 val |= val >> 32;
459 val++;
460
461 return val;
462};
463
472constexpr int
473ctz32(uint32_t value)
474{
475 return value ? __builtin_ctzl(value) : 32;
476}
477
486constexpr int
487ctz64(uint64_t value)
488{
489 return value ? __builtin_ctzll(value) : 64;
490}
491
500constexpr inline int
501clz32(uint32_t value)
502{
503 return value ? __builtin_clz(value) : 32;
504}
505
514constexpr inline int
515clz64(uint64_t value)
516{
517 return value ? __builtin_clzll(value) : 64;
518}
519
520} // namespace gem5
521
522#endif // __BASE_BITFIELD_HH__
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 void replaceBits(T &val, unsigned first, unsigned last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Definition bitfield.hh:216
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< 18 > sum
Definition misc.hh:1198
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
const uint8_t reverseBitsLookUpTable[]
Lookup table used for High Speed bit reversing.
Definition bitfield.cc:44
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