Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions docs/src/explanations/width-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ The width of a conditionally valid expression is the width of its input expressi

Hardware operators have output widths as defined by the following set of rules:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PDF link above for firrtl spec is a 404, just noting since noticed 👍 .


| operation | bit width |
| --------- | --------- |
| `z = x + y` *or* `z = x +% y` | `w(z) = max(w(x), w(y))` |
| `z = x +& y` | `w(z) = max(w(x), w(y)) + 1` |
| `z = x - y` *or* `z = x -% y` | `w(z) = max(w(x), w(y))` |
| `z = x -& y` | `w(z) = max(w(x), w(y)) + 1` |
| `z = x & y` | `w(z) = max(w(x), w(y))` |
| `z = Mux(c, x, y)` | `w(z) = max(w(x), w(y))` |
| `z = w * y` | `w(z) = w(x) + w(y)` |
| `z = x << n` | `w(z) = w(x) + maxNum(n)` |
| `z = x >> n` | `w(z) = w(x) - minNum(n)` |
| `z = Cat(x, y)` | `w(z) = w(x) + w(y)` |
| `z = Fill(n, x)` | `w(z) = w(x) * maxNum(n)` |

>where for instance `w(z)` is the bit width of wire `z`, and the `&`
rule applies to all bitwise logical operations.
| operation | bit width |
| --------- | --------- |
| `z = x + y` *or* `z = x +% y` | `w(z) = max(w(x), w(y))` |
| `z = x +& y` | `w(z) = max(w(x), w(y)) + 1` |
| `z = x - y` *or* `z = x -% y` | `w(z) = max(w(x), w(y))` |
| `z = x -& y` | `w(z) = max(w(x), w(y)) + 1` |
| `z = x & y` | `w(z) = max(w(x), w(y))` |
| `z = Mux(c, x, y)` | `w(z) = max(w(x), w(y))` |
| `z = w * y` | `w(z) = w(x) + w(y)` |
| `z = x << n` | `w(z) = w(x) + maxNum(n)` |
| `z = x >> n` | `w(z) = w(x) - minNum(n)` |
| `z = Cat(x, y)` | `w(z) = w(x) + w(y)` |
| `z = Fill(n, x)` | `w(z) = w(x) * n` |

>where for instance `w(z)` is the bit width of wire `z`, `maxNum(n)` is the maximum number representable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define minNum while visiting?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n for these shifts must be unsigned, is that right? Curious if minNum(n) is ever non-zero.

If n is 5 is maxNum(n) 5 or is it 7 (largest 3-bit number, where 3 is width of n)?

Copy link
Copy Markdown

@yulan-dacosta yulan-dacosta Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If n is 5 is maxNum(n) 5 or is it 7 (largest 3-bit number, where 3 is width of n)?

Is there a way to check this? Cause I think when using Fill(6, 1.U), I saw a width of z to be 7.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @dtzSiFive comment I think we should use a different thing for the last number in Fill, as there it's an Int n not a UInt-that-has-a-width n

>by `n`, and the `&` rule applies to all bitwise logical operations.

Given a path of connections that begins with an unspecified width element (most commonly a top-level input), then the compiler will throw an exception indicating a certain width was uninferrable.

Expand Down