bitfield.hh (11932:98961d1b51ca) bitfield.hh (12226:36dff288b076)
1/*
2 * Copyright (c) 2017 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

--- 25 unchanged lines hidden (view full) ---

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
1/*
2 * Copyright (c) 2017 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

--- 25 unchanged lines hidden (view full) ---

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
42 */
43
44#ifndef __BASE_BITFIELD_HH__
45#define __BASE_BITFIELD_HH__
46
47#include <inttypes.h>
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>
48
52
53/** Lookup table used for High Speed bit reversing */
54extern const uint8_t reverseLookUpTable[];
55
49/**
50 * Generate a 64-bit mask of 'nbits' 1s, right justified.
51 */
52inline uint64_t
53mask(int nbits)
54{
55 return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
56}
57
56/**
57 * Generate a 64-bit mask of 'nbits' 1s, right justified.
58 */
59inline uint64_t
60mask(int nbits)
61{
62 return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
63}
64
58
59
60/**
61 * Extract the bitfield from position 'first' to 'last' (inclusive)
62 * from 'val' and right justify it. MSB is numbered 63, LSB is 0.
63 */
64template <class T>
65inline
66T
67bits(T val, int first, int last)

--- 82 unchanged lines hidden (view full) ---

150/** Overloaded function to allow to access only 1 bit*/
151template <class T, class B>
152inline
153void
154replaceBits(T& val, int bit, B bit_val)
155{
156 val = insertBits(val, bit, bit, bit_val);
157}
65/**
66 * Extract the bitfield from position 'first' to 'last' (inclusive)
67 * from 'val' and right justify it. MSB is numbered 63, LSB is 0.
68 */
69template <class T>
70inline
71T
72bits(T val, int first, int last)

--- 82 unchanged lines hidden (view full) ---

155/** Overloaded function to allow to access only 1 bit*/
156template <class T, class B>
157inline
158void
159replaceBits(T& val, int bit, B bit_val)
160{
161 val = insertBits(val, bit, bit, bit_val);
162}
163
158/**
164/**
165 * Takes a variable lenght word and returns the mirrored version
166 * (Bit by bit, LSB=>MSB).
167 *
168 * algorithm from
169 * http://graphics.stanford.edu/~seander/bithacks.html
170 * #ReverseBitsByLookupTable
171 *
172 * @param val: variable lenght word
173 * @param size: number of bytes to mirror
174 * @return mirrored word
175 */
176template <class T>
177T
178reverseBits(T val, std::size_t size = sizeof(T))
179{
180 static_assert(std::is_integral<T>::value, "Expecting an integer type");
181
182 assert(size <= sizeof(T));
183
184 T output = 0;
185 for (auto byte = 0; byte < size; byte++, val >>= 8) {
186 output = (output << 8) | reverseLookUpTable[val & 0xFF];
187 }
188
189 return output;
190}
191
192/**
159 * Returns the bit position of the MSB that is set in the input
160 */
161inline
162int
163findMsbSet(uint64_t val) {
164 int msb = 0;
165 if (!val)
166 return 0;

--- 85 unchanged lines hidden ---
193 * Returns the bit position of the MSB that is set in the input
194 */
195inline
196int
197findMsbSet(uint64_t val) {
198 int msb = 0;
199 if (!val)
200 return 0;

--- 85 unchanged lines hidden ---