gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
141 constexpr uint64_t
142 sext(uint64_t val, int N)
143 {
144  bool sign_bit = bits(val, N - 1);
145  if (sign_bit)
146  val |= ~mask(N);
147  return val;
148 }
149 
156 template <int N>
157 constexpr uint64_t
158 szext(uint64_t val)
159 {
160  bool sign_bit = bits(val, N - 1);
161  if (sign_bit)
162  val |= ~mask(N);
163  else
164  val &= mask(N);
165  return val;
166 }
167 
180 template <class T, class B>
181 constexpr T
182 insertBits(T val, unsigned first, unsigned last, B bit_val)
183 {
184  assert(first >= last);
185  T bmask = mask(first, last);
186  val &= ~bmask;
187  val |= ((T)bit_val << last) & bmask;
188  return val;
189 }
190 
196 template <class T, class B>
197 constexpr T
198 insertBits(T val, unsigned bit, B bit_val)
199 {
200  return insertBits(val, bit, bit, bit_val);
201 }
202 
211 template <class T, class B>
212 constexpr void
213 replaceBits(T& val, unsigned first, unsigned last, B bit_val)
214 {
215  val = insertBits(val, first, last, bit_val);
216 }
217 
223 template <class T, class B>
224 constexpr void
225 replaceBits(T& val, unsigned bit, B bit_val)
226 {
227  val = insertBits(val, bit, bit, bit_val);
228 }
229 
250 template <class T>
251 std::enable_if_t<std::is_integral_v<T>, T>
252 reverseBits(T val, size_t size=sizeof(T))
253 {
254  assert(size <= sizeof(T));
255 
256  if constexpr (sizeof(T) == 1) {
257  return reverseBitsLookUpTable[val];
258  } else {
259  T output = {};
260 
261  for (size_t byte = 0; byte < size; byte++) {
262  output = (output << 8) | reverseBitsLookUpTable[val & mask(8)];
263  val >>= 8;
264  }
265 
266  return output;
267  }
268 }
269 
275 constexpr int
276 findMsbSet(uint64_t val)
277 {
278  int msb = 0;
279  if (!val)
280  return 0;
281  if (bits(val, 63, 32)) {
282  msb += 32;
283  val >>= 32;
284  }
285  if (bits(val, 31, 16)) {
286  msb += 16;
287  val >>= 16;
288  }
289  if (bits(val, 15, 8)) {
290  msb += 8;
291  val >>= 8;
292  }
293  if (bits(val, 7, 4)) {
294  msb += 4;
295  val >>= 4;
296  }
297  if (bits(val, 3, 2)) {
298  msb += 2;
299  val >>= 2;
300  }
301  if (bits(val, 1, 1))
302  msb += 1;
303  return msb;
304 }
305 
311 constexpr int
312 findLsbSet(uint64_t val)
313 {
314  int lsb = 0;
315  if (!val)
316  return sizeof(val) * 8;
317  if (!bits(val, 31, 0)) {
318  lsb += 32;
319  val >>= 32;
320  }
321  if (!bits(val, 15, 0)) {
322  lsb += 16;
323  val >>= 16;
324  }
325  if (!bits(val, 7, 0)) {
326  lsb += 8;
327  val >>= 8;
328  }
329  if (!bits(val, 3, 0)) {
330  lsb += 4;
331  val >>= 4;
332  }
333  if (!bits(val, 1, 0)) {
334  lsb += 2;
335  val >>= 2;
336  }
337  if (!bits(val, 0, 0))
338  lsb += 1;
339  return lsb;
340 }
341 
349 constexpr int
350 popCount(uint64_t val)
351 {
352 #ifndef __has_builtin
353 # define __has_builtin(foo) 0
354 #endif
355 #if defined(__GNUC__) || \
356  (defined(__clang__) && __has_builtin(__builtin_popcountl))
357  return __builtin_popcountl(val);
358 #else
359  const uint64_t m1 = 0x5555555555555555ULL; // ..010101b
360  const uint64_t m2 = 0x3333333333333333ULL; // ..110011b
361  const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // ..001111b
362  const uint64_t sum = 0x0101010101010101ULL;
363 
364  val -= (val >> 1) & m1; // 2 bits count -> 2 bits
365  val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits
366  val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits
367  return (val * sum) >> 56; // horizontal sum
368 #endif // defined(__GNUC__) ||
369  //(defined(__clang__) && __has_builtin(__builtin_popcountl))
370 }
371 
384 constexpr uint64_t
386 {
387  val--;
388  val |= val >> 1;
389  val |= val >> 2;
390  val |= val >> 4;
391  val |= val >> 8;
392  val |= val >> 16;
393  val |= val >> 32;
394  val++;
395 
396  return val;
397 };
398 
407 constexpr int
408 ctz32(uint32_t value)
409 {
410  return value ? __builtin_ctzl(value) : 32;
411 }
412 
421 constexpr int
422 ctz64(uint64_t value)
423 {
424  return value ? __builtin_ctzll(value) : 64;
425 }
426 
435 constexpr inline int
436 clz32(uint32_t value)
437 {
438  return value ? __builtin_clz(value) : 32;
439 }
440 
449 constexpr inline int
450 clz64(uint64_t value)
451 {
452  return value ? __builtin_clzll(value) : 64;
453 }
454 
455 } // namespace gem5
456 
457 #endif // __BASE_BITFIELD_HH__
gem5::output
static void output(const char *filename)
Definition: debug.cc:60
gem5::replaceBits
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:213
gem5::sext
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:126
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:776
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:422
gem5::mask
constexpr uint64_t mask(unsigned nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
Definition: bitfield.hh:63
gem5::findLsbSet
constexpr int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition: bitfield.hh:312
gem5::popCount
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition: bitfield.hh:350
gem5::reverseBits
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
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:182
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
gem5::szext
constexpr uint64_t szext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:158
gem5::ctz32
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:408
gem5::clz64
constexpr int clz64(uint64_t value)
Count leading zeros in a 64-bit value.
Definition: bitfield.hh:450
gem5::findMsbSet
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:276
gem5::RiscvISA::sum
Bitfield< 18 > sum
Definition: misc.hh:740
gem5::clz32
constexpr int clz32(uint32_t value)
Count leading zeros in a 32-bit value.
Definition: bitfield.hh:436
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:385
gem5::reverseBitsLookUpTable
const uint8_t reverseBitsLookUpTable[]
Lookup table used for High Speed bit reversing.
Definition: bitfield.cc:44

Generated on Sun Jul 30 2023 01:56:50 for gem5 by doxygen 1.8.17