Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/36496 )

Change subject: python: Add support for Proxy division
......................................................................

python: Add support for Proxy division

Allow proxies to use python3's division operations. The dividends
and divisors can be either a proxy or a constant.

Change-Id: I96b854355b8f593edfb1ea52a52548b855b05fc0
Signed-off-by: Daniel R. Carvalho <[email protected]>
---
M src/python/m5/proxy.py
1 file changed, 57 insertions(+), 16 deletions(-)



diff --git a/src/python/m5/proxy.py b/src/python/m5/proxy.py
index 9d91b84..6019ce0 100644
--- a/src/python/m5/proxy.py
+++ b/src/python/m5/proxy.py
@@ -55,7 +55,7 @@
     def __init__(self, search_self, search_up):
         self._search_self = search_self
         self._search_up = search_up
-        self._multipliers = []
+        self._ops = []

     def __str__(self):
         if self._search_self and not self._search_up:
@@ -72,29 +72,70 @@
                 "cannot set attribute '%s' on proxy object" % attr)
         super(BaseProxy, self).__setattr__(attr, value)

-    # support for multiplying proxies by constants or other proxies to
-    # other params
+    # Support for multiplying proxies by either constants or other proxies
     def __mul__(self, other):
         if not (isinstance(other, (int, long, float)) or isproxy(other)):
             raise TypeError(
"Proxy multiplier must be a constant or a proxy to a param")
-        self._multipliers.append(other)
+        self._ops.append(["*", other])
         return self

+    # Support for multiplying constants by proxies
     __rmul__ = __mul__

-    def _mulcheck(self, result, base):
+    # Support for dividing proxies by either constants or other proxies
+    def __truediv__(self, other):
+        if not (isinstance(other, (int, long, float)) or isproxy(other)):
+            raise TypeError(
+                "Proxy divisor must be a constant or a proxy to a param")
+        self._ops.append(["/", other])
+        return self
+
+    def __floordiv__(self, other):
+        if not (isinstance(other, (int, long, float)) or isproxy(other)):
+            raise TypeError(
+                "Proxy divisor must be a constant or a proxy to a param")
+        self._ops.append(["//", other])
+        return self
+
+    # Support for dividing constants by proxies
+    def __rtruediv__(self, other):
+        if not (isinstance(other, (int, long, float))):
+            raise TypeError("Proxy dividend must be a constant")
+        self._ops.append(["r/", other])
+        return self
+
+    def __rfloordiv__(self, other):
+        if not (isinstance(other, (int, long, float))):
+            raise TypeError("Proxy dividend must be a constant")
+        self._ops.append(["r//", other])
+        return self
+
+    # After all the operators and operands have been defined, this function
+    # should be called to perform the actual operation
+    def _opcheck(self, result, base):
         from . import params
-        for multiplier in self._multipliers:
-            if isproxy(multiplier):
-                multiplier = multiplier.unproxy(base)
-                # assert that we are multiplying with a compatible
-                # param
-                if not isinstance(multiplier, params.NumericParamValue):
-                    raise TypeError(
-                        "Proxy multiplier must be a numerical param")
-                multiplier = multiplier.getValue()
-            result = result * multiplier
+        for operator, operand in self._ops:
+            # Get the operand's value
+            if isproxy(operand):
+                operand = operand.unproxy(base)
+                # assert that we are operating with a compatible param
+                if not isinstance(operand, params.NumericParamValue):
+ raise TypeError("Proxy operand must be a numerical param")
+                operand = operand.getValue()
+
+            # Apply the operation
+            if operator == "*":
+                result = result * operand
+            elif operator == "/":
+                result = result / operand
+            elif operator == "//":
+                result = result // operand
+            elif operator == "r/":
+                result = operand / result.getValue()
+            elif operator == "r//":
+                result = operand // result.getValue()
+
         return result

     def unproxy(self, base):
@@ -128,7 +169,7 @@
                 raise RuntimeError("Cycle in unproxy")
             result = result.unproxy(obj)

-        return self._mulcheck(result, base)
+        return self._opcheck(result, base)

     def getindex(obj, index):
         if index == None:

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/36496
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I96b854355b8f593edfb1ea52a52548b855b05fc0
Gerrit-Change-Number: 36496
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to