add stand-alone simulator bitmanip test
[openpower-isa.git] / src / openpower / test / bitmanip / bitmanip_cases.py
index e741ddcef6b942e0c3bc9b25ea211c9950d3aca2..f38e06665dc211dd5ba816be1ed19fa629787069 100644 (file)
@@ -1,36 +1,51 @@
-from openpower.test.common import TestAccumulatorBase
+from openpower.sv.trans.svp64 import SVP64Asm
+from openpower.test.common import TestAccumulatorBase, skip_case
 from openpower.endian import bigendian
 from openpower.simulator.program import Program
-from hashlib import sha256
+from openpower.test.state import ExpectedState
+from nmutil.sim_util import hash_256
 
 
-def hash_256(v):
-    return int.from_bytes(
-        sha256(bytes(v, encoding='utf-8')).digest(),
-        byteorder='little'
-    )
+class BitManipTestCase(TestAccumulatorBase):
+    def do_case_ternlogi(self, rt, ra, rb, imm):
+        lst = [f"ternlogi 3, 4, 5, {imm}"]
+        initial_regs = [0] * 32
+        rt %= 2 ** 64
+        ra %= 2 ** 64
+        rb %= 2 ** 64
+        initial_regs[3] = rt
+        initial_regs[4] = ra
+        initial_regs[5] = rb
+        lst = list(SVP64Asm(lst, bigendian))
+        e = ExpectedState(pc=4)
+        expected = 0
+        for i in range(64):
+            lut_index = 0
+            if rb & 2 ** i:
+                lut_index |= 2 ** 0
+            if ra & 2 ** i:
+                lut_index |= 2 ** 1
+            if rt & 2 ** i:
+                lut_index |= 2 ** 2
+            if imm & 2 ** lut_index:
+                expected |= 2 ** i
+        e.intregs[3] = expected
+        e.intregs[4] = ra
+        e.intregs[5] = rb
+        self.add_case(Program(lst, bigendian), initial_regs, expected=e)
 
+    def case_ternlogi_0(self):
+        self.do_case_ternlogi(0x8000_0000_FFFF_0000,
+                              0x8000_0000_FF00_FF00,
+                              0x8000_0000_F0F0_F0F0, 0x80)
 
-class BitManipTestCase(TestAccumulatorBase):
-    def case_ternaryi(self):
-        po = 5
-        rt = 3
-        ra = 4
-        rb = 5
-        rc = 1
-        xo = 0
+    def case_ternlogi_FF(self):
+        self.do_case_ternlogi(0, 0, 0, 0xFF)
+
+    def case_ternlogi_random(self):
         for i in range(100):
-            imm = hash_256(f"ternaryi imm {i}") & 0xFF
-            instr = po
-            instr = (instr << 5) | rt
-            instr = (instr << 5) | ra
-            instr = (instr << 5) | rb
-            instr = (instr << 8) | imm
-            instr = (instr << 2) | xo
-            instr = (instr << 1) | rc
-            lst = [f".4byte {hex(instr)}"]
-            initial_regs = [0] * 32
-            initial_regs[3] = hash_256(f"ternaryi rt {i}") % 2 ** 64
-            initial_regs[4] = hash_256(f"ternaryi ra {i}") % 2 ** 64
-            initial_regs[5] = hash_256(f"ternaryi rb {i}") % 2 ** 64
-            self.add_case(Program(lst, bigendian), initial_regs)
+            imm = hash_256(f"ternlogi imm {i}") & 0xFF
+            rt = hash_256(f"ternlogi rt {i}") % 2 ** 64
+            ra = hash_256(f"ternlogi ra {i}") % 2 ** 64
+            rb = hash_256(f"ternlogi rb {i}") % 2 ** 64
+            self.do_case_ternlogi(rt, ra, rb, imm)