add trunc_div and trunch_rem to decoder helpers
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 19 Jun 2020 14:03:28 +0000 (15:03 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 19 Jun 2020 14:03:28 +0000 (15:03 +0100)
https://bugs.libre-soc.org/show_bug.cgi?id=324

src/soc/decoder/helpers.py
src/soc/decoder/isa/caller.py
src/soc/decoder/power_pseudo.py
src/soc/decoder/pseudo/pywriter.py

index e8fd6cb7e1a5c7ca2c535a6213e2a791684577ee..ef6610e5c89d7d0e0316c216bf51faf8486f46df 100644 (file)
@@ -1,11 +1,38 @@
 import unittest
 from soc.decoder.selectable_int import SelectableInt
 
 import unittest
 from soc.decoder.selectable_int import SelectableInt
 
+"""
+Links:
+* https://bugs.libre-soc.org/show_bug.cgi?id=324 - add trunc_div and trunc_rem
+"""
 
 def exts(value, bits):
     sign = 1 << (bits - 1)
     return (value & (sign - 1)) - (value & sign)
 
 
 def exts(value, bits):
     sign = 1 << (bits - 1)
     return (value & (sign - 1)) - (value & sign)
 
+def trunc_div(n, d):
+    f = getattr(n, "trunc_div", None)
+    if f is not None:
+        return f(d)
+    fr = getattr(d, "rtrunc_div", None)
+    if fr is not None:
+        return fr(n)
+    abs_n = abs(n)
+    abs_d = abs(d)
+    abs_q = n // d
+    if (n < 0) == (d < 0):
+        return abs_q
+    return -abs_q
+
+def trunc_rem(n, d):
+    f = getattr(n, "trunc_rem", None)
+    if f is not None:
+        return f(d)
+    fr = getattr(d, "rtrunc_rem", None)
+    if fr is not None:
+        return fr(n)
+    return n - d * trunc_div(n, d)
+
 
 def EXTS(value):
     """ extends sign bit out from current MSB to all 256 bits
 
 def EXTS(value):
     """ extends sign bit out from current MSB to all 256 bits
index 79e8b13ec5522791e75bd1184f228005a29c6520..7bf411fd7cd541e6c96e16137b4bd1674fac422a 100644 (file)
@@ -10,7 +10,7 @@ from soc.decoder.orderedset import OrderedSet
 from soc.decoder.selectable_int import (FieldSelectableInt, SelectableInt,
                                         selectconcat)
 from soc.decoder.power_enums import spr_dict, XER_bits, insns, InternalOp
 from soc.decoder.selectable_int import (FieldSelectableInt, SelectableInt,
                                         selectconcat)
 from soc.decoder.power_enums import spr_dict, XER_bits, insns, InternalOp
-from soc.decoder.helpers import exts
+from soc.decoder.helpers import exts, trunc_div, trunc_rem
 from collections import namedtuple
 import math
 import sys
 from collections import namedtuple
 import math
 import sys
index e2b6440a6065c28aa4d1c896db211fddac0d8419..2e7b242286f1c4c1567dc8236cadfb7edb13408e 100644 (file)
@@ -255,12 +255,15 @@ def test():
         print("args", args)
         print("-->", " ".join(map(str, args)))
 
         print("args", args)
         print("-->", " ".join(map(str, args)))
 
-    from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
+    from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
+                                     trunc_div, trunc_rem)
 
     d = {}
     d["print"] = print_
     d["EXTS64"] = EXTS64
     d["EXTZ64"] = EXTZ64
 
     d = {}
     d["print"] = print_
     d["EXTS64"] = EXTS64
     d["EXTZ64"] = EXTZ64
+    d["trunc_div"] = trunc_div
+    d["trunc_rem"] = trunc_rem
     d["SelectableInt"] = SelectableInt
     d["concat"] = selectconcat
     d["GPR"] = gsc.gpr
     d["SelectableInt"] = SelectableInt
     d["concat"] = selectconcat
     d["GPR"] = gsc.gpr
index 9acec8cea994809343daa8b74d8ce081c9335520..bac8c4592b8709ffea6928dc19f4d8d33cb29e65 100644 (file)
@@ -20,7 +20,8 @@ header = """\
 
 from soc.decoder.isa.caller import inject, instruction_info
 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
 
 from soc.decoder.isa.caller import inject, instruction_info
 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
-                                 ne, eq, gt, ge, lt, le, length)
+                                 ne, eq, gt, ge, lt, le, length,
+                                 trunc_div, trunc_rem)
 from soc.decoder.selectable_int import SelectableInt
 from soc.decoder.selectable_int import selectconcat as concat
 from soc.decoder.orderedset import OrderedSet
 from soc.decoder.selectable_int import SelectableInt
 from soc.decoder.selectable_int import selectconcat as concat
 from soc.decoder.orderedset import OrderedSet