back.pysim: fix simulation of Value.xor().
authorwhitequark <whitequark@whitequark.org>
Fri, 20 Sep 2019 10:12:59 +0000 (10:12 +0000)
committerwhitequark <whitequark@whitequark.org>
Fri, 20 Sep 2019 10:12:59 +0000 (10:12 +0000)
nmigen/back/pysim.py
nmigen/test/test_sim.py

index 4ff3ed587ad6a856de0a51c8afb3c9134d5eeadd..57461ee84c2e6f1861136ddc2233b35853eeda7b 100644 (file)
@@ -137,7 +137,7 @@ class _RHSValueCompiler(_ValueCompiler):
                 return lambda state: normalize(arg(state) == mask, shape)
             if value.op == "r^":
                 # Believe it or not, this is the fastest way to compute a sideways XOR in Python.
-                return lambda state: normalize(str(arg(state)).count("1") % 2, shape)
+                return lambda state: normalize(format(arg(state), "b").count("1") % 2, shape)
         elif len(value.operands) == 2:
             lhs, rhs = map(self, value.operands)
             if value.op == "+":
index fae57189f81f95c20175d4601b4cf8bcfca5334b..7979e495a89f5af2e87a34960613302c46f6f684 100644 (file)
@@ -61,18 +61,21 @@ class SimulatorUnitTestCase(FHDLTestCase):
         stmt = lambda y, a: y.eq(a.any())
         self.assertStatement(stmt, [C(0b00, 2)], C(0))
         self.assertStatement(stmt, [C(0b01, 2)], C(1))
+        self.assertStatement(stmt, [C(0b10, 2)], C(1))
         self.assertStatement(stmt, [C(0b11, 2)], C(1))
 
     def test_all(self):
         stmt = lambda y, a: y.eq(a.all())
         self.assertStatement(stmt, [C(0b00, 2)], C(0))
         self.assertStatement(stmt, [C(0b01, 2)], C(0))
+        self.assertStatement(stmt, [C(0b10, 2)], C(0))
         self.assertStatement(stmt, [C(0b11, 2)], C(1))
 
-    def test_xor(self):
+    def test_xor_unary(self):
         stmt = lambda y, a: y.eq(a.xor())
         self.assertStatement(stmt, [C(0b00, 2)], C(0))
         self.assertStatement(stmt, [C(0b01, 2)], C(1))
+        self.assertStatement(stmt, [C(0b10, 2)], C(1))
         self.assertStatement(stmt, [C(0b11, 2)], C(0))
 
     def test_add(self):
@@ -100,7 +103,7 @@ class SimulatorUnitTestCase(FHDLTestCase):
         stmt = lambda y, a, b: y.eq(a | b)
         self.assertStatement(stmt, [C(0b1100, 4), C(0b1010, 4)], C(0b1110, 4))
 
-    def test_xor(self):
+    def test_xor_binary(self):
         stmt = lambda y, a, b: y.eq(a ^ b)
         self.assertStatement(stmt, [C(0b1100, 4), C(0b1010, 4)], C(0b0110, 4))