summaryrefslogtreecommitdiff
path: root/src/base/bitfield.hh
diff options
context:
space:
mode:
authorGiacomo Travaglini <giacomo.travaglini@arm.com>2017-10-18 01:04:25 +0100
committerGiacomo Travaglini <giacomo.travaglini@arm.com>2017-10-20 15:33:40 +0000
commit4b3fee098435f1980d0d101ce5a416935d3d6a8e (patch)
tree0e32f298f4c6d79c01c4ed142d61704473e53828 /src/base/bitfield.hh
parent2a4d6925bc678e3cfa93e3560ec9a4c7f8e6ec2b (diff)
downloadgem5-4b3fee098435f1980d0d101ce5a416935d3d6a8e.tar.xz
base: Function for mirroring bits in variable length word
This patch introduces a high-speed template function for mirroring the bits (MSB=>LSB) in a variable length word. The function is achieving high performances since it is using a look-up table. Change-Id: Ib0d0480e68d902f25655f74d243de305103eff75 Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com> Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com> Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-on: https://gem5-review.googlesource.com/5261 Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Maintainer: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src/base/bitfield.hh')
-rw-r--r--src/base/bitfield.hh38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh
index 885b42288..23c8b4b12 100644
--- a/src/base/bitfield.hh
+++ b/src/base/bitfield.hh
@@ -39,12 +39,19 @@
*
* Authors: Steve Reinhardt
* Nathan Binkert
+ * Giacomo Travaglini
*/
#ifndef __BASE_BITFIELD_HH__
#define __BASE_BITFIELD_HH__
#include <inttypes.h>
+#include <cassert>
+#include <cstddef>
+#include <type_traits>
+
+/** Lookup table used for High Speed bit reversing */
+extern const uint8_t reverseLookUpTable[];
/**
* Generate a 64-bit mask of 'nbits' 1s, right justified.
@@ -55,8 +62,6 @@ mask(int nbits)
return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
}
-
-
/**
* Extract the bitfield from position 'first' to 'last' (inclusive)
* from 'val' and right justify it. MSB is numbered 63, LSB is 0.
@@ -155,6 +160,35 @@ replaceBits(T& val, int bit, B bit_val)
{
val = insertBits(val, bit, bit, bit_val);
}
+
+/**
+ * Takes a variable lenght word and returns the mirrored version
+ * (Bit by bit, LSB=>MSB).
+ *
+ * algorithm from
+ * http://graphics.stanford.edu/~seander/bithacks.html
+ * #ReverseBitsByLookupTable
+ *
+ * @param val: variable lenght word
+ * @param size: number of bytes to mirror
+ * @return mirrored word
+ */
+template <class T>
+T
+reverseBits(T val, std::size_t size = sizeof(T))
+{
+ static_assert(std::is_integral<T>::value, "Expecting an integer type");
+
+ assert(size <= sizeof(T));
+
+ T output = 0;
+ for (auto byte = 0; byte < size; byte++, val >>= 8) {
+ output = (output << 8) | reverseLookUpTable[val & 0xFF];
+ }
+
+ return output;
+}
+
/**
* Returns the bit position of the MSB that is set in the input
*/