Skip to content

Commit 49eefa0

Browse files
committed
Add doctest examples for rtllib/adders.py.
1 parent 1a497bc commit 49eefa0

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

pyrtl/rtllib/adders.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ def kogge_stone(
2424
useful for performance critical designs. However, it has `O(n log(n))` area usage,
2525
and large fan out.
2626
27+
.. doctest only::
28+
29+
>>> import pyrtl
30+
>>> pyrtl.reset_working_block()
31+
32+
Example::
33+
34+
>>> a = pyrtl.Input(name="a", bitwidth=4)
35+
>>> b = pyrtl.Input(name="b", bitwidth=4)
36+
>>> output = pyrtl.Output(name="output")
37+
38+
>>> output <<= pyrtl.rtllib.adders.kogge_stone(a, b)
39+
40+
>>> sim = pyrtl.Simulation()
41+
>>> sim.step(provided_inputs={"a": 2, "b": 3})
42+
>>> sim.inspect("output")
43+
5
44+
2745
:param a: A :class:`.WireVector` to add up. Bitwidths don't need to match.
2846
:param b: A :class:`.WireVector` to add up. Bitwidths don't need to match.
2947
:param cin: An optional 1-bit carry-in :class:`.WireVector`. Can be any type that
@@ -103,6 +121,25 @@ def carrysave_adder(
103121
) -> pyrtl.WireVector:
104122
"""Adds three :class:`WireVectors<.WireVector>` up in an efficient manner.
105123
124+
.. doctest only::
125+
126+
>>> import pyrtl
127+
>>> pyrtl.reset_working_block()
128+
129+
Example::
130+
131+
>>> a = pyrtl.Input(name="a", bitwidth=4)
132+
>>> b = pyrtl.Input(name="b", bitwidth=4)
133+
>>> c = pyrtl.Input(name="c", bitwidth=4)
134+
>>> output = pyrtl.Output(name="output")
135+
136+
>>> output <<= pyrtl.rtllib.adders.carrysave_adder(a, b, c)
137+
138+
>>> sim = pyrtl.Simulation()
139+
>>> sim.step(provided_inputs={"a": 2, "b": 3, "c": 4})
140+
>>> sim.inspect("output")
141+
9
142+
106143
:param a: A :class:`.WireVector` to add up. Bitwidths don't need to match.
107144
:param b: A :class:`.WireVector` to add up. Bitwidths don't need to match.
108145
:param c: A :class:`.WireVector` to add up. Bitwidths don't need to match.
@@ -128,6 +165,24 @@ def cla_adder(
128165
calculates the carry bits faster. It is not as fast as :func:`kogge_stone`, but uses
129166
less area.
130167
168+
.. doctest only::
169+
170+
>>> import pyrtl
171+
>>> pyrtl.reset_working_block()
172+
173+
Example::
174+
175+
>>> a = pyrtl.Input(name="a", bitwidth=4)
176+
>>> b = pyrtl.Input(name="b", bitwidth=4)
177+
>>> output = pyrtl.Output(name="output")
178+
179+
>>> output <<= pyrtl.rtllib.adders.cla_adder(a, b)
180+
181+
>>> sim = pyrtl.Simulation()
182+
>>> sim.step(provided_inputs={"a": 2, "b": 3})
183+
>>> sim.inspect("output")
184+
5
185+
131186
:param a: A :class:`.WireVector` to add up. Bitwidths don't need to match.
132187
:param b: A :class:`.WireVector` to add up. Bitwidths don't need to match.
133188
:param cin: A 1-bit carry-in :class:`.WireVector`.
@@ -322,6 +377,26 @@ def fast_group_adder(
322377
323378
max(len(w) for w in wires_to_add) + ceil(len(wires_to_add))
324379
380+
.. doctest only::
381+
382+
>>> import pyrtl
383+
>>> pyrtl.reset_working_block()
384+
385+
Example::
386+
387+
>>> wires_to_add = [pyrtl.Const(n) for n in range(10)]
388+
>>> output = pyrtl.Output(name="output")
389+
390+
>>> output <<= pyrtl.rtllib.adders.fast_group_adder(wires_to_add)
391+
392+
>>> sim = pyrtl.Simulation()
393+
>>> sim.step()
394+
>>> sim.inspect("output")
395+
45
396+
>>> sum(range(10))
397+
45
398+
399+
325400
:param wires_to_add: A :class:`list` of :class:`WireVectors<.WireVector>` to add.
326401
:param reducer: The tree reducer to use. See :func:`wallace_reducer` and
327402
:func:`dada_reducer`.

tests/rtllib/test_adders.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import doctest
12
import random
23
import unittest
34

@@ -6,6 +7,15 @@
67
from pyrtl.rtllib import adders
78

89

10+
class TestDocTests(unittest.TestCase):
11+
"""Test documentation examples."""
12+
13+
def test_doctests(self):
14+
failures, tests = doctest.testmod(m=pyrtl.rtllib.adders)
15+
self.assertGreater(tests, 0)
16+
self.assertEqual(failures, 0)
17+
18+
919
class TestAdders(unittest.TestCase):
1020
@classmethod
1121
def setUpClass(cls):

0 commit comments

Comments
 (0)