Skip to content

Floating point implementation refactoring#481

Open
fdxmw wants to merge 2 commits intoUCSBarchlab:developmentfrom
fdxmw:float
Open

Floating point implementation refactoring#481
fdxmw wants to merge 2 commits intoUCSBarchlab:developmentfrom
fdxmw:float

Conversation

@fdxmw
Copy link
Copy Markdown
Member

@fdxmw fdxmw commented Apr 24, 2026

This builds on the wire_struct improvements in #480.

  • Define canonical wire_struct Float types, and use them as the source of
    truth for the number of exponent and mantissa bits. See Float16 for an
    example. All operators are generic, and accept any of these Float types as
    input, and produce the same Float type as output. So if you add a Float16
    with a Float16, you get a Float16. If the operand types do not match, a
    PyrtlError is raised.

    This lets us remove _fp_wire_struct, FPTypeProperties,
    FloatingPointType, PyrtlFloatConfig because operands now carry their own
    metadata and describe themselves. For example, we can inspect the operand's
    mantissa bitwidth directly with operand.mantissa.bitwidth, rather than
    passing around another data structure.

  • Remove the FloatOperations classes. They are no longer needed because the
    top-level add, sub, and mul functions are generic, and work with any
    matching Float types. The default rounding mode can be set with
    set_default_rounding_mode, and can be changed for individual operations.

  • Rename the package from pyrtlfloat to just float. The full package name
    was pyrtl.rtllib.pyrtlfloat, which was redundant.

  • Rename mul to mult for symmetry with signed_mult.

  • Rename make_inf and friends to make_inf_like. These functions now take an
    operand that specifies the desired output bitwidths, similar to
    np.ones_like.

  • Remove make_zero. It is clearer to directly set the exponent and mantissa
    to 0.

  • Use dataclass to simplify some class definitions.

  • Use absolute imports (https://peps.python.org/pep-0008/#imports).

  • Rename some variables to improve readability.

  • Update rtllib.rst to generate Sphinx documentation for the float package.

* The concatenated value can be set with the special `kwarg` "_value". This is
  useful when the `wire_struct`'s class name is not known.

* Changes to make `wire_structs` documentable with Sphinx. The generated class
  copies documentation from the decorated class (`__doc__`), and the generated
  class's name now matches the decorated class's name. Previously all
  `wire_structs` were named `_WireStruct`.

* Add a new `class_name` argument to `wire_matrix`. This lets users optionally
  set the name of the generated class. Previously all `wire_matrix` were named
  `_WireMatrix`.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.9%. Comparing base (4ace6d3) to head (991bfd0).

Additional details and impacted files
@@              Coverage Diff              @@
##           development    #481     +/-   ##
=============================================
- Coverage         92.0%   91.9%   -0.0%     
=============================================
  Files               31      30      -1     
  Lines             7508    7437     -71     
=============================================
- Hits              6901    6834     -67     
+ Misses             607     603      -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fdxmw
Copy link
Copy Markdown
Member Author

fdxmw commented Apr 24, 2026

@gaborszita Here's the refactoring changes I had in mind for pyrtlfloat, please take a look when you get a chance and let me know what you think. The biggest change is that all operators now use wire_structs as their inputs and outputs, which lets us remove a bunch of data structures and code.

I also tried to clean up the documentation a bit, you can see what it currently looks like at https://pyrtl--481.org.readthedocs.build/en/481/rtllib.html#module-pyrtl.rtllib.float

* Define canonical `wire_struct` `Float` types, and use them as the source of
  truth for the number of exponent and mantissa bits. See `Float16` for an
  example. All operators are generic, and accept any of these `Float` types as
  input, and produce the same `Float` type as output. So if you add a `Float16`
  with a `Float16`, you get a `Float16`. If the operand types do not match, a
  `PyrtlError` is raised.

  This lets us remove `_fp_wire_struct`, `FPTypeProperties`,
  `FloatingPointType`, `PyrtlFloatConfig` because operands now carry their own
  metadata and describe themselves. For example, we can inspect the operand's
  mantissa bitwidth directly with `operand.mantissa.bitwidth`, rather than
  passing around another data structure.

* Remove the `FloatOperations` classes. They are no longer needed because the
  top-level `add`, `sub`, and `mul` functions are generic, and work with any
  matching `Float` types. The default rounding mode can be set with
  `set_default_rounding_mode`, and can be changed for individual operations.

* Rename the package from `pyrtlfloat` to just `float`. The full package name
  was `pyrtl.rtllib.pyrtlfloat`, which was redundant.

* Rename `mul` to `mult` for symmetry with `signed_mult`.

* Rename `make_inf` and friends to `make_inf_like`. These functions now take an
  operand that specifies the desired output bitwidths, similar to
  `np.ones_like`.

* Remove `make_zero`. It is clearer to directly set the exponent and mantissa
  to `0`.

* Use `dataclass` to simplify some class definitions.

* Use absolute imports (https://peps.python.org/pep-0008/#imports).

* Rename some variables to improve readability.

* Update `rtllib.rst` to generate Sphinx documentation for the `float` package.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant