Skip to content

Allow division and modulo operations on fixed-width bit (unsinged) types - #1414

Merged
jonathan-dilorenzo merged 4 commits into
p4lang:mainfrom
jaehyun1ee:bit-div-mod
Sep 17, 2026
Merged

jonathan-dilorenzo merged 4 commits into
p4lang:mainfrom
jaehyun1ee:bit-div-mod

Conversation

@jaehyun1ee

Copy link
Copy Markdown
Contributor

This is a follow-up PR from #1327.

The current specification only allows division and modulo for arbitrary-precision integer types (int), but not for bit<W> or int<S> types. Yet, there were uses of division and modulo on bit<W> and int<S> types in p4c tests (where p4c allows such, under certain p4c-specific restrictions).

This was first discussed in Nov-2025 LDWG meeting, where @jonathan-dilorenzo summarized as:

Allow any bit divisions and modulo, noting that targets may restrict this behavior arbitrarily. Suggestion from P4 LDWG is to send a PR to support this.

However, no follow-up PR was made, thus this has gone through another round of discussion in May-2026 LDWG meeting. The consensus is the same: allow division and modulo for bit<W> types, yet disallow for int<S> types since there is no clear consensus on how division and modulo should be defined regarding signedness and rounding.

This PR adds division and modulo to 8.7. Operations on fixed-width bit types (unsigned integers), also mentioning that P4 architectures may restrict division and modulo.

Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
Comment thread p4-16/spec/P4-16-spec.adoc Outdated
and is computed by truncating the result to the output's width.
P4 architectures may impose additional restrictions --- e.g., they may only
allow multiplication by a non-negative integer power of two.
* Truncating integer division between positive values, denoted by `/`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The numerator for division or modulo should be allowed to be 0, not only positive, yes? I agree it is reasonable to require the denominator to be positive, and not attempt to define the result of such an operation with a denominator of 0.

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.

Thank you for the suggestion, I made the following edit:

* Truncating integer division, denoted by `/`.
  The denominator must be positive.

Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
@jonathan-dilorenzo

Copy link
Copy Markdown
Collaborator

LGTM, anything else you want to change here @jafingerhut or shall we resolve the conversation and merge this?

Comment thread p4-16/spec/P4-16-spec.adoc Outdated
The denominator must be positive.
The result has the same width as the operands. Similar to multiplication,
P4 architectures may impose additional restrictions.
* Modulo between positive values, denoted by `%`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Allow the numerator to be 0 here.

@jafingerhut jafingerhut Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Recommendation: Change "the denominator must be positive." with "The denominator must not be 0." The same restriction should exist for the modulo operator (the current language suggests that the numerator cannot be 0, but we should allow that).

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.

Thanks! Addressed as follows:

* Truncating integer division, denoted by `/`.
  The denominator must not be zero.
  The result has the same width as the operands. Similar to multiplication,
  P4 architectures may impose additional restrictions.
* Modulo, denoted by `%`.
  The denominator must not be zero.
  The result has the same width as the operands. Similar to multiplication,
  P4 architectures may impose additional restrictions.

Comment thread p4-16/spec/P4-16-spec.adoc Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ideally allow 0 for numerator both here and in truncating integer division.

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.

Also addressed here:

- * Truncating integer division between positive values, denoted by `/`.
- * Modulo between positive values, denoted by `%`.
+ * Truncating integer division, denoted by `/`.
+   The numerator must be non-negative and the denominator must be positive.
+ * Modulo, denoted by `%`.
+   The numerator must be non-negative and the denominator must be positive.

@jafingerhut jafingerhut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, subject to Jonathan's final review on small changes discussed in the 2026-Sep-14 LDWG meeting.

… type

Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
@jaehyun1ee

Copy link
Copy Markdown
Contributor Author

Thank you for the comments! @jonathan-dilorenzo, can you take a look?

@jonathan-dilorenzo

Copy link
Copy Markdown
Collaborator

Love it! Thanks @jaehyun1ee and @jafingerhut!

@jonathan-dilorenzo
jonathan-dilorenzo merged commit 0370191 into p4lang:main Sep 17, 2026
4 checks passed
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.

3 participants