Skip to content

Commit 245d3c0

Browse files
committed
Floating point implementation refactoring:
* 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.
1 parent 69781de commit 245d3c0

11 files changed

Lines changed: 580 additions & 728 deletions

File tree

docs/rtllib.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Multipliers
2323
.. automodule:: pyrtl.rtllib.multipliers
2424
:members:
2525

26+
Floating Point
27+
--------------
28+
29+
.. automodule:: pyrtl.rtllib.float
30+
:members:
31+
2632
Barrel Shifter
2733
--------------
2834

pyrtl/rtllib/float/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Add, subtract, and multiply floating point numbers.
3+
4+
Several standard ``Float`` formats like :class:`Float16` and :class:`Float32` are
5+
predefined. Users may also define custom floating point formats.
6+
7+
The main operators are :func:`add`, :func:`sub`, and :func:`mult`. These operators all
8+
accept and return one of these ``Float`` formats. Inputs to an operator must share
9+
the same ``Float`` format. The operator's output will be in the same ``Float`` format as
10+
its inputs.
11+
"""
12+
13+
from .add_sub import add, sub
14+
from .mult import mult
15+
from .types import BFloat16, Float16, Float32, Float64, RoundingMode
16+
from .utils import get_default_rounding_mode, set_default_rounding_mode
17+
18+
__all__ = [
19+
"BFloat16",
20+
"Float16",
21+
"Float32",
22+
"Float64",
23+
"RoundingMode",
24+
"add",
25+
"get_default_rounding_mode",
26+
"mult",
27+
"sub",
28+
"set_default_rounding_mode",
29+
]

0 commit comments

Comments
 (0)