gem5  v19.0.0.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  * Authors: Steve Reinhardt
41  * Nathan Binkert
42  * Giacomo Travaglini
43  */
44 
45 #ifndef __BASE_BITFIELD_HH__
46 #define __BASE_BITFIELD_HH__
47 
48 #include <inttypes.h>
49 #include <cassert>
50 #include <cstddef>
51 #include <type_traits>
52 
54 extern const uint8_t reverseLookUpTable[];
55 
59 inline uint64_t
60 mask(int nbits)
61 {
62  return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
63 }
64 
69 template <class T>
70 inline
71 T
72 bits(T val, int first, int last)
73 {
74  int nbits = first - last + 1;
75  return (val >> last) & mask(nbits);
76 }
77 
81 template <class T>
82 inline
83 T
84 bits(T val, int bit)
85 {
86  return bits(val, bit, bit);
87 }
88 
93 template <class T>
94 inline
95 T
96 mbits(T val, int first, int last)
97 {
98  return val & (mask(first+1) & ~mask(last));
99 }
100 
101 inline uint64_t
102 mask(int first, int last)
103 {
104  return mbits((uint64_t)-1LL, first, last);
105 }
106 
110 template <int N>
111 inline
112 uint64_t
113 sext(uint64_t val)
114 {
115  int sign_bit = bits(val, N-1, N-1);
116  return sign_bit ? (val | ~mask(N)) : val;
117 }
118 
129 template <class T, class B>
130 inline
131 T
132 insertBits(T val, int first, int last, B bit_val)
133 {
134  T t_bit_val = bit_val;
135  T bmask = mask(first - last + 1) << last;
136  return ((t_bit_val << last) & bmask) | (val & ~bmask);
137 }
138 
142 template <class T, class B>
143 inline
144 T
145 insertBits(T val, int bit, B bit_val)
146 {
147  return insertBits(val, bit, bit, bit_val);
148 }
149 
154 template <class T, class B>
155 inline
156 void
157 replaceBits(T& val, int first, int last, B bit_val)
158 {
159  val = insertBits(val, first, last, bit_val);
160 }
161 
163 template <class T, class B>
164 inline
165 void
166 replaceBits(T& val, int bit, B bit_val)
167 {
168  val = insertBits(val, bit, bit, bit_val);
169 }
170 
183 template <class T>
184 T
185 reverseBits(T val, std::size_t size = sizeof(T))
186 {
187  static_assert(std::is_integral<T>::value, "Expecting an integer type");
188 
189  assert(size <= sizeof(T));
190 
191  T output = 0;
192  for (auto byte = 0; byte < size; byte++, val = static_cast<T>(val >> 8)) {
193  output = (output << 8) | reverseLookUpTable[val & 0xFF];
194  }
195 
196  return output;
197 }
198 
202 inline
203 int
204 findMsbSet(uint64_t val) {
205  int msb = 0;
206  if (!val)
207  return 0;
208  if (bits(val, 63,32)) { msb += 32; val >>= 32; }
209  if (bits(val, 31,16)) { msb += 16; val >>= 16; }
210  if (bits(val, 15,8)) { msb += 8; val >>= 8; }
211  if (bits(val, 7,4)) { msb += 4; val >>= 4; }
212  if (bits(val, 3,2)) { msb += 2; val >>= 2; }
213  if (bits(val, 1,1)) { msb += 1; }
214  return msb;
215 }
216 
220 inline int
221 findLsbSet(uint64_t val) {
222  int lsb = 0;
223  if (!val)
224  return sizeof(val) * 8;
225  if (!bits(val, 31,0)) { lsb += 32; val >>= 32; }
226  if (!bits(val, 15,0)) { lsb += 16; val >>= 16; }
227  if (!bits(val, 7,0)) { lsb += 8; val >>= 8; }
228  if (!bits(val, 3,0)) { lsb += 4; val >>= 4; }
229  if (!bits(val, 1,0)) { lsb += 2; val >>= 2; }
230  if (!bits(val, 0,0)) { lsb += 1; }
231  return lsb;
232 }
233 
237 template <class T>
238 inline bool
239 isPow2(T v) {
240  return (v & (v - 1)) == (T)0;
241 }
242 
248 inline int
249 popCount(uint64_t val) {
250 #ifndef __has_builtin
251  #define __has_builtin(foo) 0
252 #endif
253 #if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
254  return __builtin_popcountl(val);
255 #else
256  const uint64_t m1 = 0x5555555555555555; // ..010101b
257  const uint64_t m2 = 0x3333333333333333; // ..110011b
258  const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; // ..001111b
259  const uint64_t sum = 0x0101010101010101;
260 
261  val -= (val >> 1) & m1; // 2 bits count -> 2 bits
262  val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits
263  val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits
264  return (val * sum) >> 56; // horizontal sum
265 #endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl))
266 }
267 
278 inline uint64_t alignToPowerOfTwo(uint64_t val)
279 {
280  val--;
281  val |= val >> 1;
282  val |= val >> 2;
283  val |= val >> 4;
284  val |= val >> 8;
285  val |= val >> 16;
286  val |= val >> 32;
287  val++;
288 
289  return val;
290 };
291 
298 inline int ctz32(uint32_t value)
299 {
300  return value ? __builtin_ctzl(value) : 32;
301 }
302 
309 inline int ctz64(uint64_t value)
310 {
311  return value ? __builtin_ctzll(value) : 64;
312 }
313 
314 #endif // __BASE_BITFIELD_HH__
uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:278
int ctz32(uint32_t value)
Count trailing zeros in a 32-bit value.
Definition: bitfield.hh:298
int findLsbSet(uint64_t val)
Returns the bit position of the LSB that is set in the input.
Definition: bitfield.hh:221
const uint8_t reverseLookUpTable[]
Lookup table used for High Speed bit reversing.
Definition: bitfield.cc:43
Bitfield< 28 > v
int ctz64(uint64_t value)
Count trailing zeros in a 64-bit value.
Definition: bitfield.hh:309
static void output(const char *filename)
Definition: debug.cc:63
#define LL(N)
int64_t constant
Definition: types.hh:52
T reverseBits(T val, std::size_t size=sizeof(T))
Takes a variable lenght word and returns the mirrored version (Bit by bit, LSB=>MSB).
Definition: bitfield.hh:185
int popCount(uint64_t val)
Returns the number of set ones in the provided value.
Definition: bitfield.hh:249
Bitfield< 63 > val
Definition: misc.hh:771
Bitfield< 18 > sum
Definition: registers.hh:617
void replaceBits(T &val, int first, int last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Definition: bitfield.hh:157
T insertBits(T val, int first, int last, B bit_val)
Returns val with bits first to last set to the LSBs of bit_val.
Definition: bitfield.hh:132
#define ULL(N)
uint64_t constant
Definition: types.hh:50
int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:204
T mbits(T val, int first, int last)
Mask off the given bits in place like bits() but without shifting.
Definition: bitfield.hh:96
T bits(T val, int first, int last)
Extract the bitfield from position &#39;first&#39; to &#39;last&#39; (inclusive) from &#39;val&#39; and right justify it...
Definition: bitfield.hh:72
uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition: bitfield.hh:113
bool isPow2(T v)
Checks if a number is a power of two, or zero.
Definition: bitfield.hh:239
uint64_t mask(int nbits)
Generate a 64-bit mask of &#39;nbits&#39; 1s, right justified.
Definition: bitfield.hh:60

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13