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: 61e6267dafa4
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: 51269ad4a037
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Oct 26, 2019

  1. hdl.ast: simplify {bit,word}_select with constant offset.

    We don't have any other convenient shortcut for x[off*w:(off+1)*w],
    but using word_select to extract a single static range would result
    in severe bloat of emitted code through expansion to dead branches.
    Recognize and simplify this pattern.
    whitequark committed Oct 26, 2019
    Copy the full SHA
    51269ad View commit details
Showing with 21 additions and 0 deletions.
  1. +6 −0 nmigen/hdl/ast.py
  2. +15 −0 nmigen/test/test_hdl_ast.py
6 changes: 6 additions & 0 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
@@ -304,6 +304,9 @@ def bit_select(self, offset, width):
Part, out
Selected part of the ``Value``
"""
offset = Value.cast(offset)
if type(offset) is Const and isinstance(width, int):
return self[offset.value:offset.value + width]
return Part(self, offset, width, stride=1, src_loc_at=1)

def word_select(self, offset, width):
@@ -324,6 +327,9 @@ def word_select(self, offset, width):
Part, out
Selected part of the ``Value``
"""
offset = Value.cast(offset)
if type(offset) is Const and isinstance(width, int):
return self[offset.value * width:(offset.value + 1) * width]
return Part(self, offset, width, stride=width, src_loc_at=1)

def matches(self, *patterns):
15 changes: 15 additions & 0 deletions nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
@@ -533,15 +533,23 @@ def setUp(self):

def test_shape(self):
s1 = self.c.bit_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.shape(), unsigned(2))
self.assertIsInstance(s1.shape(), Shape)
s2 = self.c.bit_select(self.s, 0)
self.assertIsInstance(s2, Part)
self.assertEqual(s2.shape(), unsigned(0))

def test_stride(self):
s1 = self.c.bit_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.stride, 1)

def test_const(self):
s1 = self.c.bit_select(1, 2)
self.assertIsInstance(s1, Slice)
self.assertRepr(s1, """(slice (const 8'd0) 1:3)""")

def test_width_wrong(self):
with self.assertRaises(TypeError):
self.c.bit_select(self.s, -1)
@@ -558,13 +566,20 @@ def setUp(self):

def test_shape(self):
s1 = self.c.word_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.shape(), unsigned(2))
self.assertIsInstance(s1.shape(), Shape)

def test_stride(self):
s1 = self.c.word_select(self.s, 2)
self.assertIsInstance(s1, Part)
self.assertEqual(s1.stride, 2)

def test_const(self):
s1 = self.c.word_select(1, 2)
self.assertIsInstance(s1, Slice)
self.assertRepr(s1, """(slice (const 8'd0) 2:4)""")

def test_width_wrong(self):
with self.assertRaises(TypeError):
self.c.word_select(self.s, 0)