diff options
author | Andreas Sandberg <andreas.sandberg@arm.com> | 2019-01-27 12:01:08 +0000 |
---|---|---|
committer | Andreas Sandberg <andreas.sandberg@arm.com> | 2019-02-22 10:47:36 +0000 |
commit | 8e5d168332c4ac3851aee4f815cff0b62b37cc40 (patch) | |
tree | d8e74ce05fdea4870e0f71c46346c92af1873c2f /src/python/m5/params.py | |
parent | 466d30cbbde67df7490a02149a92fa3e9be3c492 (diff) | |
download | gem5-8e5d168332c4ac3851aee4f815cff0b62b37cc40.tar.xz |
python: Add missing operators to NumericParamValue
Add missing operators to NumericParamValue and ensure that they are
able to work on the underlying value if the right hand side is a
param.
Change-Id: I2bd86662aee9891bbd89aed7ebe20b827b5528bd
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/16001
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Diffstat (limited to 'src/python/m5/params.py')
-rw-r--r-- | src/python/m5/params.py | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/src/python/m5/params.py b/src/python/m5/params.py index 5eabce1cd..0c189c983 100644 --- a/src/python/m5/params.py +++ b/src/python/m5/params.py @@ -454,6 +454,10 @@ class String(ParamValue,str): # operations in a type-safe way. e.g., a Latency times an int returns # a new Latency object. class NumericParamValue(ParamValue): + @staticmethod + def unwrap(v): + return v.value if isinstance(v, NumericParamValue) else v + def __str__(self): return str(self.value) @@ -472,24 +476,70 @@ class NumericParamValue(ParamValue): def __mul__(self, other): newobj = self.__class__(self) - newobj.value *= other + newobj.value *= NumericParamValue.unwrap(other) newobj._check() return newobj __rmul__ = __mul__ - def __div__(self, other): + def __truediv__(self, other): + newobj = self.__class__(self) + newobj.value /= NumericParamValue.unwrap(other) + newobj._check() + return newobj + + def __floordiv__(self, other): + newobj = self.__class__(self) + newobj.value //= NumericParamValue.unwrap(other) + newobj._check() + return newobj + + + def __add__(self, other): newobj = self.__class__(self) - newobj.value /= other + newobj.value += NumericParamValue.unwrap(other) newobj._check() return newobj def __sub__(self, other): newobj = self.__class__(self) - newobj.value -= other + newobj.value -= NumericParamValue.unwrap(other) newobj._check() return newobj + def __iadd__(self, other): + self.value += NumericParamValue.unwrap(other) + self._check() + return self + + def __isub__(self, other): + self.value -= NumericParamValue.unwrap(other) + self._check() + return self + + def __imul__(self, other): + self.value *= NumericParamValue.unwrap(other) + self._check() + return self + + def __itruediv__(self, other): + self.value /= NumericParamValue.unwrap(other) + self._check() + return self + + def __ifloordiv__(self, other): + self.value //= NumericParamValue.unwrap(other) + self._check() + return self + + def __lt__(self, other): + return self.value < NumericParamValue.unwrap(other) + + # Python 2.7 pre __future__.division operators + # TODO: Remove these when after "import division from __future__" + __div__ = __truediv__ + __idiv__ = __itruediv__ + def config_value(self): return self.value |