gem5  v21.1.0.2
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<T>::value && sizeof(T) != 1, T>
236 reverseBits(T val, size_t size=sizeof(T))
237 {
238  assert(size <= sizeof(T));
239 
240  T output = {};
241  for (size_t byte = 0; byte < size; byte++) {
242  output = (output << 8) | reverseBitsLookUpTable[val & mask(8)];
243  val >>= 8;
244  }
245 
246  return output;
247 }
248 
249 template <class T>
250 std::enable_if_t<std::is_integral<T>::value && sizeof(T) == 1, T>
251 reverseBits(T val, size_t size=sizeof(T))
252 {
253  assert(size == 1);
254  return reverseBitsLookUpTable[val];
255 }
256 
262 constexpr int
263 findMsbSet(uint64_t val)
264 {
265  int msb = 0;
266  if (!val)
267  return 0;
268  if (bits(val, 63, 32)) {
269  msb += 32;
270  val >>= 32;
271  }
272  if (bits(val, 31, 16)) {
273  msb += 16;
274  val >>= 16;
275  }
276  if (bits(val, 15, 8)) {
277  msb += 8;
278  val >>= 8;
279  }
280  if (bits(val, 7, 4)) {
281  msb += 4;
282  val >>= 4;
283  }
284  if (bits(val, 3, 2)) {
285  msb += 2;
286  val >>= 2;
287  }
288  if (bits(val, 1, 1))
289  msb += 1;
290  return msb;
291 }
292 
298 constexpr int
299 findLsbSet(uint64_t val)
300 {
301  int lsb = 0;
302  if (!val)
303  return sizeof(val) * 8;
304  if (!bits(val, 31, 0)) {
305  lsb += 32;
306  val >>= 32;
307  }
308  if (!bits(val, 15, 0)) {
309  lsb += 16;
310  val >>= 16;
311  }
312  if (!bits(val, 7, 0)) {
313  lsb += 8;
314  val >>= 8;
315  }
316  if (!bits(val, 3, 0)) {
317  lsb += 4;
318  val >>= 4;
319  }
320  if (!bits(val, 1, 0)) {
321  lsb += 2;
322  val >>= 2;
323  }
324  if (!bits(val, 0, 0))
325  lsb += 1;
326  return lsb;
327 }
328 
336 constexpr int
337 popCount(uint64_t val)
338 {
339 #ifndef __has_builtin
340 # define __has_builtin(foo) 0
341 #endif
342 #if defined(__GNUC__) || \
343  (defined(__clang__) && __has_builtin(__builtin_popcountl))
344  return __builtin_popcountl(val);
345 #else
346  const uint64_t m1 = 0x5555555555555555ULL; // ..010101b
347  const uint64_t m2 = 0x3333333333333333ULL; // ..110011b
348  const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // ..001111b
349  const uint64_t sum = 0x0101010101010101ULL;
350 
351  val -= (val >> 1) & m1; // 2 bits count -> 2 bits
352  val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits
353  val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits
354  return (val * sum) >> 56; // horizontal sum
355 #endif // defined(__GNUC__) ||
356  //(defined(__clang__) && __has_builtin(__builtin_popcountl))
357 }
358 
371 constexpr uint64_t
373 {
374  val--;
375  val |= val >> 1;
376  val |= val >> 2;
377  val |= val >> 4;
378  val |= val >> 8;
379  val |= val >> 16;
380  val |= val >> 32;
381  val++;
382 
383  return val;
384 };
385 
394 constexpr int
395 ctz32(uint32_t value)
396 {
397  return value ? __builtin_ctzl(value) : 32;
398 }
399 
408 constexpr int
409 ctz64(uint64_t value)
410 {
411  return value ? __builtin_ctzll(value) : 64;
412 }
413 
414 } // namespace gem5
415 
416 #endif // __BASE_BITFIELD_HH__
gem5::output
static void output(const char *filename)
Definition: debug.cc:66
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:197
gem5::sext
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:126
gem5::reverseBits
std::enable_if_t< std::is_integral< T >::value &&sizeof(T) !=1, T > reverseBits(T val, size_t size=sizeof(T))
Takes a value and returns the bit reversed version.
Definition: bitfield.hh:236
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
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:409
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:299
gem5::popCount
constexpr int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition: bitfield.hh:337
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:166
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:142
gem5::ctz32
constexpr int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:395
gem5::findMsbSet
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:263
gem5::RiscvISA::sum
Bitfield< 18 > sum
Definition: misc.hh:545
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:372
gem5::reverseBitsLookUpTable
const uint8_t reverseBitsLookUpTable[]
Lookup table used for High Speed bit reversing.
Definition: bitfield.cc:44

Generated on Tue Sep 21 2021 12:24:52 for gem5 by doxygen 1.8.17