-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds saturating addition and subtraction operators #609
Conversation
This PR introduces saturating arithmetic operators for signed and unsigned integers of fixed width (per our discussion in the WG).
p4-16/spec/P4-16-spec.mdk
Outdated
when the correct answer is 258, it is less surprising to get an answer of 255 | ||
from saturating arithmetic than to get an answer of 2 from modular arithmetic. | ||
At this time, P4 defines saturating operations only for multiplication and | ||
addition. For an unsigned integer with bit-width of `W`, the minimum value is `0` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should "multiplication and addition" be replaced with "addition and subtraction"? Those are the operators defined later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on replacing multiplication with subtraction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
@@ -2815,6 +2831,8 @@ to bit-strings of the same width: | |||
by `|`. | |||
- Bitwise "complement" of a single bit-string, denoted by `~`. | |||
- Bitwise "xor" of two bit-strings of the same width, denoted by `^`. | |||
- Saturating addition, denoted by `|+|`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another possibility is [+]
, which suggests confinement to a range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend using |+|
. []
are too overloaded.
p4-16/spec/P4-16-spec.mdk
Outdated
@@ -2852,6 +2870,10 @@ operations simply "wrap around", similar to C operations on unsigned values. | |||
Hence, attempting to represent large values using `W` bits will only keep | |||
the least-significant `W` bits of the value. | |||
|
|||
P4 also supports saturating arithmetic (addition and subtraction) for signed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For signed integers I suspect we want this to work even when adding negative values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is nothing in the text that suggests it should not. The minimum value is -2^(W-1)
. Do you think we need to add additional clarification?
p4-16/spec/P4-16-spec.mdk
Outdated
Saturating arithmetic has advantages, in particular when used as counters. The | ||
the result of a saturating counter max-ing out is much closer to the real | ||
result than a counter that overflows and wraps around. According to Wikipedia | ||
(https://en.wikipedia.org/wiki/Saturation_arithmetic) saturating arithmetic is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert to Markdown URL?
p4-16/spec/P4-16-spec.mdk
Outdated
@@ -2777,6 +2777,22 @@ keep the least-significant W bits of the value). In particular, P4 | |||
does not have arithmetic exceptions---the result of an arithmetic | |||
operation is defined for all possible inputs. | |||
|
|||
P4 target architectures may also support saturating arithmetic. All saturating |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"may also" -> "may optionally"?
This PR introduces saturating arithmetic operators for signed and
unsigned integers of fixed width (per our discussion in the WG).