Skip to content

Commit

Permalink
Minor improvements to one_hot_to_binary's documentation:
Browse files Browse the repository at this point in the history
- Add `one_hot_to_binary` to helpers.rst, in a new "Encoders and Decoders" section.
- Fix broken link to `as_wires` and add a link to `Const`.
- Use two spaces before inline comments for consistency with the rest of the file
- Minor formatting changes
  • Loading branch information
fdxmw committed Dec 16, 2024
1 parent 4c72f2d commit d384107
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ addition, subtraction, multiplication, comparison, and many others).
.. autofunction:: pyrtl.corecircuits.shift_right_arithmetic
.. autofunction:: pyrtl.corecircuits.shift_left_logical
.. autofunction:: pyrtl.corecircuits.shift_right_logical

Encoders and Decoders
---------------------

.. autofunction:: pyrtl.helperfuncs.one_hot_to_binary

19 changes: 10 additions & 9 deletions pyrtl/helperfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,20 +1688,21 @@ def __len__(self):
def one_hot_to_binary(w) -> WireVector:
'''Takes a one-hot input and returns the bit position of the high bit in binary.
:param w: WireVector or a WireVector-like object or something that can be converted
into a Const (in accordance with the :py:func:`as_wires()` required input). Example
inputs: 0b0010, 64, 0b01.
:param w: WireVector, WireVector-like object, or something that can be converted
into a :py:class:`.Const` (in accordance with the :py:func:`.as_wires()`
required input). Example inputs: ``0b0010``, ``64``, ``0b01``.
:return: The bit position of the high bit in binary as a WireVector.
If the input contains multiple 1s, the bit position of the first 1 will
be returned. If the input contains no 1s, 0 will be returned.
If the input contains multiple ``1``s, the bit position of the first ``1`` will be
returned. If the input contains no ``1``s, ``0`` will be returned.
Examples::
one_hot_to_binary(0b0010) # returns 1
one_hot_to_binary(64) # returns 6
one_hot_to_binary(0b1100) # returns 2, the bit position of the first 1
one_hot_to_binary(0) # returns 0
one_hot_to_binary(0b0010) # returns 1
one_hot_to_binary(64) # returns 6
one_hot_to_binary(0b1100) # returns 2, the bit position of the first 1
one_hot_to_binary(0) # returns 0
'''

w = as_wires(w)
Expand Down

0 comments on commit d384107

Please sign in to comment.