summaryrefslogtreecommitdiff
path: root/src/base/sat_counter.hh
diff options
context:
space:
mode:
authorDaniel <odanrc@yahoo.com.br>2019-04-13 11:02:43 +0200
committerDaniel Carvalho <odanrc@yahoo.com.br>2019-05-14 07:55:06 +0000
commit50a533f10134e7bc377dfdb9209b0334fd01cd24 (patch)
treec92958ae7fcbe135b84ade80ffbc5fbcb94e84e4 /src/base/sat_counter.hh
parent5b3f91daf6891c68e978963701d792ebd63f23a7 (diff)
downloadgem5-50a533f10134e7bc377dfdb9209b0334fd01cd24.tar.xz
base: Add operators to SatCounter
Add shift, add and subtract assignment operators, as well as copy and move constructor and assignments to SatCounter, so that it they can be used by the prefetchers. Also add extra useful functions to calculate saturation oercentile so that the instantiator does not need to be aware of the counter's maximum value. Change-Id: I61d0cb28c8375b9d2774a39011e4a0aa6fe9ccb7 Signed-off-by: Daniel <odanrc@yahoo.com.br> Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17996 Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Maintainer: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src/base/sat_counter.hh')
-rw-r--r--src/base/sat_counter.hh114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/base/sat_counter.hh b/src/base/sat_counter.hh
index 342a3382f..3de9486db 100644
--- a/src/base/sat_counter.hh
+++ b/src/base/sat_counter.hh
@@ -56,6 +56,9 @@
class SatCounter
{
public:
+ /** The default constructor should never be used. */
+ SatCounter() = delete;
+
/**
* Constructor for the counter. The explicit keyword is used to make
* sure the user does not assign a number to the counter thinking it
@@ -75,6 +78,58 @@ class SatCounter
"Saturating counter's Initial value exceeds max value.");
}
+ /** Copy constructor. */
+ SatCounter(const SatCounter& other)
+ : initialVal(other.initialVal), maxVal(other.maxVal),
+ counter(other.counter)
+ {
+ }
+
+ /** Copy assignment. */
+ SatCounter& operator=(const SatCounter& other) {
+ if (this != &other) {
+ SatCounter temp(other);
+ this->swap(temp);
+ }
+ return *this;
+ }
+
+ /** Move constructor. */
+ SatCounter(SatCounter&& other)
+ {
+ initialVal = other.initialVal;
+ maxVal = other.maxVal;
+ counter = other.counter;
+ SatCounter temp(0);
+ other.swap(temp);
+ }
+
+ /** Move assignment. */
+ SatCounter& operator=(SatCounter&& other) {
+ if (this != &other) {
+ initialVal = other.initialVal;
+ maxVal = other.maxVal;
+ counter = other.counter;
+ SatCounter temp(0);
+ other.swap(temp);
+ }
+ return *this;
+ }
+
+ /**
+ * Swap the contents of every member of the class. Used for the default
+ * copy-assignment created by the compiler.
+ *
+ * @param other The other object to swap contents with.
+ */
+ void
+ swap(SatCounter& other)
+ {
+ std::swap(initialVal, other.initialVal);
+ std::swap(maxVal, other.maxVal);
+ std::swap(counter, other.counter);
+ }
+
/** Pre-increment operator. */
SatCounter&
operator++()
@@ -113,6 +168,49 @@ class SatCounter
return old_counter;
}
+ /** Shift-right-assignment. */
+ SatCounter&
+ operator>>=(const int& shift)
+ {
+ this->counter >>= shift;
+ return *this;
+ }
+
+ /** Shift-left-assignment. */
+ SatCounter&
+ operator<<=(const int& shift)
+ {
+ this->counter <<= shift;
+ if (this->counter > maxVal) {
+ this->counter = maxVal;
+ }
+ return *this;
+ }
+
+ /** Add-assignment. */
+ SatCounter&
+ operator+=(const int& value)
+ {
+ if (maxVal - this->counter >= value) {
+ this->counter += value;
+ } else {
+ this->counter = maxVal;
+ }
+ return *this;
+ }
+
+ /** Subtract-assignment. */
+ SatCounter&
+ operator-=(const int& value)
+ {
+ if (this->counter > value) {
+ this->counter -= value;
+ } else {
+ this->counter = 0;
+ }
+ return *this;
+ }
+
/**
* Read the counter's value.
*/
@@ -121,6 +219,22 @@ class SatCounter
/** Reset the counter to its initial value. */
void reset() { counter = initialVal; }
+ /**
+ * Calculate saturation percentile of the current counter's value
+ * with regard to its maximum possible value.
+ *
+ * @return A value between 0.0 and 1.0 to indicate which percentile of
+ * the maximum value the current value is.
+ */
+ double calcSaturation() const { return (double) counter / maxVal; }
+
+ /**
+ * Whether the counter has achieved its maximum value or not.
+ *
+ * @return True if the counter saturated.
+ */
+ bool isSaturated() const { return counter == maxVal; }
+
private:
uint8_t initialVal;
uint8_t maxVal;