Skip to content

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

Open
jaehyun1ee wants to merge 3 commits into
p4lang:mainfrom
jaehyun1ee:bit-div-mod
Open

Allow division and modulo operations on fixed-width bit (unsinged) types#1414
jaehyun1ee wants to merge 3 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>
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.

2 participants