Skip to content
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

Merged
merged 4 commits into from
Apr 21, 2018

Conversation

cc10512
Copy link
Contributor

@cc10512 cc10512 commented Apr 16, 2018

This PR introduces saturating arithmetic operators for signed and
unsigned integers of fixed width (per our discussion in the WG).

This PR introduces saturating arithmetic operators for signed and
unsigned integers of fixed width (per our discussion in the WG).
@cc10512 cc10512 requested review from jnfoster and mihaibudiu April 16, 2018 03:30
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`
Copy link
Collaborator

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 `|+|`.
Copy link
Contributor

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.

Copy link
Contributor Author

@cc10512 cc10512 Apr 16, 2018

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.

@@ -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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert to Markdown URL?

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

"may also" -> "may optionally"?

@cc10512 cc10512 merged commit 777db43 into p4lang:master Apr 21, 2018
@cc10512 cc10512 deleted the cc/saturating-take-2 branch April 21, 2018 19:55
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.

5 participants