summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBradley Wang <radwang@ucdavis.edu>2018-07-19 18:28:49 -0700
committerBradley Wang <radwang@ucdavis.edu>2018-08-10 23:41:23 +0000
commit1bab261961536d81ca325762385ead702b2318ff (patch)
tree5f9dbf0bc77f4b1b1e1976b42f91e6316daf288c /src
parentcd913125d11b01a584ad08b32d988b786c42fe39 (diff)
downloadgem5-1bab261961536d81ca325762385ead702b2318ff.tar.xz
cpu: Add hash functionality for RegId class
Having a hash function defined within the header will allow all classes using RegId to use the class as a Key in a STL unordered_map. Change-Id: I32fd302a087c74e844dcbfce93fef9d0ed98d6bf Signed-off-by: Bradley Wang <radwang@ucdavis.edu> Reviewed-on: https://gem5-review.googlesource.com/11870 Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com> Maintainer: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src')
-rw-r--r--src/cpu/reg_class.hh30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/cpu/reg_class.hh b/src/cpu/reg_class.hh
index def275ee4..617d17e35 100644
--- a/src/cpu/reg_class.hh
+++ b/src/cpu/reg_class.hh
@@ -82,6 +82,7 @@ class RegId {
RegIndex regIdx;
ElemIndex elemIdx;
static constexpr size_t Scale = TheISA::NumVecElemPerVecReg;
+ friend struct std::hash<RegId>;
public:
RegId() {};
RegId(RegClass reg_class, RegIndex reg_idx)
@@ -201,4 +202,33 @@ class RegId {
return os << rid.className() << "{" << rid.index() << "}";
}
};
+
+namespace std
+{
+template<>
+struct hash<RegId>
+{
+ size_t operator()(const RegId& reg_id) const
+ {
+ // Extract unique integral values for the effective fields of a RegId.
+ const size_t flat_index = static_cast<size_t>(reg_id.flatIndex());
+ const size_t class_num = static_cast<size_t>(reg_id.regClass);
+
+ const size_t shifted_class_num = class_num << (sizeof(RegIndex) << 3);
+
+ // Concatenate the class_num to the end of the flat_index, in order to
+ // maximize information retained.
+ const size_t concatenated_hash = flat_index | shifted_class_num;
+
+ // If RegIndex is larger than size_t, then class_num will not be
+ // considered by this hash function, so we may wish to perform a
+ // different operation to include that information in the hash.
+ static_assert(sizeof(RegIndex) < sizeof(size_t),
+ "sizeof(RegIndex) should be less than sizeof(size_t)");
+
+ return concatenated_hash;
+ }
+};
+}
+
#endif // __CPU__REG_CLASS_HH__