Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/nmigen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0a00d533844d
Choose a base ref
...
head repository: m-labs/nmigen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 32310aecad7c
Choose a head ref
  • 1 commit
  • 4 files changed
  • 1 contributor

Commits on Sep 13, 2019

  1. hdl.ast: add Value.xor, mapping to $reduce_xor.

    Fixes #147.
    whitequark committed Sep 13, 2019
    Copy the full SHA
    32310ae View commit details
Showing with 23 additions and 0 deletions.
  1. +3 −0 nmigen/back/pysim.py
  2. +10 −0 nmigen/hdl/ast.py
  3. +4 −0 nmigen/test/test_hdl_ast.py
  4. +6 −0 nmigen/test/test_sim.py
3 changes: 3 additions & 0 deletions nmigen/back/pysim.py
Original file line number Diff line number Diff line change
@@ -135,6 +135,9 @@ def on_Operator(self, value):
val, = value.operands
mask = (1 << len(val)) - 1
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)
elif len(value.operands) == 2:
lhs, rhs = map(self, value.operands)
if value.op == "+":
10 changes: 10 additions & 0 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
@@ -157,6 +157,16 @@ def all(self):
"""
return Operator("r&", [self])

def xor(self):
"""Compute pairwise exclusive-or of every bit.
Returns
-------
Value, out
``1`` if an odd number of bits are set, ``0`` if an even number of bits are set.
"""
return Operator("r^", [self])

def implies(premise, conclusion):
"""Implication.
4 changes: 4 additions & 0 deletions nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
@@ -259,6 +259,10 @@ def test_all(self):
v = Const(0b101).all()
self.assertEqual(repr(v), "(r& (const 3'd5))")

def test_xor(self):
v = Const(0b101).xor()
self.assertEqual(repr(v), "(r^ (const 3'd5))")

def test_hash(self):
with self.assertRaises(TypeError):
hash(Const(0) + Const(0))
6 changes: 6 additions & 0 deletions nmigen/test/test_sim.py
Original file line number Diff line number Diff line change
@@ -69,6 +69,12 @@ def test_all(self):
self.assertStatement(stmt, [C(0b01, 2)], C(0))
self.assertStatement(stmt, [C(0b11, 2)], C(1))

def test_xor(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(0b11, 2)], C(0))

def test_add(self):
stmt = lambda y, a, b: y.eq(a + b)
self.assertStatement(stmt, [C(0, 4), C(1, 4)], C(1, 4))