gem5  v22.1.0.0
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 <cassert>
45 #include <cstddef>
46 #include <cstdint>
47 #include <type_traits>
48 
49 namespace gem5
50 {
51 
52 extern const uint8_t reverseBitsLookUpTable[];
53 
62 constexpr uint64_t
63 mask(unsigned nbits)
64 {
65  return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
66 }
67 
74 template <class T>
75 constexpr T
76 bits(T val, unsigned first, unsigned last)
77 {
78  assert(first >= last);
79  int nbits = first - last + 1;
80  return (val >> last) & mask(nbits);
81 }
82 
88 template <class T>
89 constexpr T
90 bits(T val, unsigned bit)
91 {
92  return bits(val, bit, bit);
93 }
94 
101 template <class T>
102 constexpr T
103 mbits(T val, unsigned first, unsigned last)
104 {
105  return val & (mask(first + 1) & ~mask(last));
106 }
107 
111 constexpr uint64_t
112 mask(unsigned first, unsigned last)
113 {
114  return mbits((uint64_t)-1LL, first, last);
115 }
116 
124 template <int N>
125 constexpr uint64_t
126 sext(uint64_t val)
127 {
128  bool sign_bit = bits(val, N - 1);
129  if (sign_bit)
130  val |= ~mask(N);
131  return val;
132 }
133 
140 template <int N>
141 constexpr uint64_t
142 szext(uint64_t val)
143 {
144  bool sign_bit = bits(val, N - 1);
145  if (sign_bit)
146  val |= ~mask(N);
147  else
148  val &= mask(N);
149  return val;
150 }
151 
164 template <class T, class B>
165 constexpr T
166 insertBits(T val, unsigned first, unsigned last, B bit_val)
167 {
168  assert(first >= last);
169  T bmask = mask(first, last);
170  val &= ~bmask;
171  val |= ((T)bit_val << last) & bmask;
172  return val;
173 }
174 
180 template <class T, class B>
181 constexpr T
182 insertBits(T val, unsigned bit, B bit_val)
183 {
184  return insertBits(val, bit, bit, bit_val);
185 }
186 
195 template <class T, class B>
196 constexpr void
197 replaceBits(T& val, unsigned first, unsigned last, B bit_val)
198 {
199  val = insertBits(val, first, last, bit_val);
200 }
201 
207 template <class T, class B>
208 constexpr void
209 replaceBits(T& val, unsigned bit, B bit_val)
210 {
211  val = insertBits(val, bit, bit, bit_val);
212 }
213 
234 template <class T>
235 std::enable_if_t<std::is_integral_v<T>, T>
236 reverseBits(T val, size_t size=sizeof(T))
237 {
238  assert(size <= sizeof(T));
239 
240  if constexpr (sizeof(T) == 1) {
241  return reverseBitsLookUpTable[val];
242  } else {
243  T output = {};
244 
245  for (size_t byte = 0; byte < size; byte++) {
246  output = (output << 8) | reverseBitsLookUpTable[val & mask(8)];
247  val >>= 8;
248  }
249 
250  return output;
251  }
252 }
253 
259 constexpr int
260 findMsbSet(uint64_t val)
261 {
262  int msb = 0;
263  if (!val)
264  return 0;
265  if (bits(val, 63, 32)) {
266  msb += 32;
267  val >>= 32;
268  }
269  if (bits(val, 31, 16)) {
270  msb += 16;
271  val >>= 16;
272  }
273  if (bits(val, 15, 8)) {
274  msb += 8;
275  val >>= 8;
276  }
277  if (bits(val, 7, 4)) {
278  msb += 4;
279  val >>= 4;
280  }
281  if (bits(val, 3, 2)) {
282  msb += 2;
283  val >>= 2;
284  }
285  if (bits(val, 1, 1))
286  msb += 1;
287  return msb;
288 }
289 
295 constexpr int
296 findLsbSet(uint64_t val)
297 {
298  int lsb = 0;
299  if (!val)
300  return sizeof(val) * 8;
301  if (!bits(val, 31, 0)) {
302  lsb += 32;
303  val >>= 32;
304  }
305  if (!bits(val, 15, 0)) {
306  lsb += 16;
307  val >>= 16;
308  }
309  if (!bits(val, 7, 0)) {
310  lsb += 8;
311  val >>= 8;
312  }
313  if (!bits(val, 3, 0)) {
314  lsb += 4;
315  val >>= 4;
316  }
317  if (!bits(val, 1, 0)) {
318  lsb += 2;
319  val >>= 2;
320  }
321  if (!bits(val, 0, 0))
322  lsb += 1;
323  return lsb;
324 }
325 
333 constexpr int
334 popCount(uint64_t val)
335 {
336 #ifndef __has_builtin
337 # define __has_builtin(foo) 0
338 #endif
339 #if defined(__GNUC__) || \
340  (defined(__clang__) && __has_builtin(__builtin_popcountl))
341  return __builtin_popcountl(val);
342 #else
343  const uint64_t m1 = 0x5555555555555555ULL; // ..010101b
344  const uint64_t m2 = 0x3333333333333333ULL; // ..110011b
345  const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // ..001111b
346  const uint64_t sum = 0x0101010101010101ULL;
347 
348  val -= (val >> 1) & m1; // 2 bits count -> 2 bits
349  val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits
350  val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits
351  return (val * sum) >> 56; // horizontal sum
352 #endif // defined(__GNUC__) ||
353  //(defined(__clang__) && __has_builtin(__builtin_popcountl))
354 }
355 
368 constexpr uint64_t
370 {
371  val--;
372  val |= val >> 1;
373  val |= val >> 2;
374  val |= val >> 4;
375  val |= val >> 8;
376  val |= val >> 16;
377  val |= val >> 32;
378  val++;
379 
380  return val;
381 };
382 
391 constexpr int
392 ctz32(uint32_t value)
393 {
394  return value ? __builtin_ctzl(value) : 32;
395 }
396 
405 constexpr int
406 ctz64(uint64_t value)
407 {
408  return value ? __builtin_ctzll(value) : 64;
409 }
410 
419 constexpr inline int
420 clz32(uint32_t value)
421 {
422  return value ? __builtin_clz(value) : 32;
423 }
424 
433 constexpr inline int
434 clz64(uint64_t value)
435 {
436  return value ? __builtin_clzll(value) : 64;
437 }
438 
439 } // namespace gem5
440 
441 #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:260
constexpr int clz32(uint32_t value)
Count leading zeros in a 32-bit value.
Definition: bitfield.hh:420
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:334
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
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:236
constexpr uint64_t mask(unsigned nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
Definition: bitfield.hh:63
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:392
constexpr uint64_t szext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:142
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
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:369
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:126
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition: bitfield.hh:296
constexpr int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition: bitfield.hh:406
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:197
constexpr int clz64(uint64_t value)
Count leading zeros in a 64-bit value.
Definition: bitfield.hh:434
Bitfield< 18 > sum
Definition: misc.hh:560
Bitfield< 63 > val
Definition: misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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 Wed Dec 21 2022 10:22:28 for gem5 by doxygen 1.9.1