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
Merged
Changes from 2 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
26 changes: 26 additions & 0 deletions p4-16/spec/P4-16-spec.mdk
Original file line number Diff line number Diff line change
Expand Up @@ -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"?

operations are limited to a fixed range between a minimum and maximum value.
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?

as numerically close to the true answer as possible; for 8-bit binary signed
arithmetic, when the correct answer is 130, it is considerably less surprising
to get an answer of 127 from saturating arithmetic than to get an answer of
−126 from modular arithmetic. Likewise, for 8-bit binary unsigned arithmetic,
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 addition and
subtraction. For an unsigned integer with bit-width of `W`, the minimum value
is `0` and the maximum value is `2^W-1`.

All binary operations (except shifts) require both operands to have
the same exact type and width; supplying operands with different
widths produces an error at compile time. No implicit casts are
Expand Down Expand Up @@ -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.

- Saturating subtraction, denoted by `|-|`.

Bit-strings also support the following operations:

Expand Down Expand Up @@ -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?

integers. For a signed integer with bit-width of `W`, the minimum value is
`-2^(W-1)` and the maximum value is `2^(W-1)-1`.

P4 also does not support arithmetic exceptions. The runtime result of an
arithmetic operation is defined for all combinations of input
arguments.
Expand Down Expand Up @@ -2880,6 +2902,8 @@ type. The result always has the same width as the left operand.
- Multiplication, denoted by `*`. Result has the same width as the
operands. P4 architectures may impose additional restrictions---e.g., they may
only allow multiplication by a power of two.
- Saturating addition, denoted by `|+|`.
- Saturating subtraction, denoted by `|-|`.
- Arithmetic shift left and right denoted by `<<` and `>>`.
The left operand is signed and the right operand must be either an
unsigned number of type `bit<S>` or a non-negative
Expand Down Expand Up @@ -2966,6 +2990,8 @@ Note: bitwise-operations (`|`,`&`,`^`,`~`) are not
defined on expressions of type `int`. In addition, it is illegal
to apply division and modulo to negative values.

Note: saturating arithmetic is not supported for arbitrary-precision integers.

## Operations on variable-size bit types { #sec-varbit-string }

To support parsing headers with variable-length fields, P4 offers a
Expand Down