From 17cea64cb78417105eb8a81bbf62e2e5d1fb8191 Mon Sep 17 00:00:00 2001 From: Calin Cascaval Date: Sun, 15 Apr 2018 20:17:43 -0700 Subject: [PATCH 1/4] adds saturating addition and subtraction operators 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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/p4-16/spec/P4-16-spec.mdk b/p4-16/spec/P4-16-spec.mdk index ab7a12cc08..1cdd22730c 100644 --- a/p4-16/spec/P4-16-spec.mdk +++ b/p4-16/spec/P4-16-spec.mdk @@ -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 +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 +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 multiplication and +addition. 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 @@ -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 `|+|`. +- Saturating subtraction, denoted by `|-|`. Bit-strings also support the following operations: @@ -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 +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. @@ -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` or a non-negative @@ -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 From 4d41170c1237682dd39434ad20282eb62310525d Mon Sep 17 00:00:00 2001 From: Calin Cascaval Date: Mon, 16 Apr 2018 09:53:01 -0700 Subject: [PATCH 2/4] address reviewer's comments --- p4-16/spec/P4-16-spec.mdk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p4-16/spec/P4-16-spec.mdk b/p4-16/spec/P4-16-spec.mdk index 1cdd22730c..c85fb271bf 100644 --- a/p4-16/spec/P4-16-spec.mdk +++ b/p4-16/spec/P4-16-spec.mdk @@ -2789,9 +2789,9 @@ 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 multiplication and -addition. For an unsigned integer with bit-width of `W`, the minimum value is `0` -and the maximum value is `2^W-1`. +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 From a23ab7a4788f7b62b0a3ed900be2f1711f60db44 Mon Sep 17 00:00:00 2001 From: Calin Cascaval Date: Mon, 16 Apr 2018 14:12:14 -0700 Subject: [PATCH 3/4] LDWG discussion --- p4-16/spec/P4-16-spec.mdk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p4-16/spec/P4-16-spec.mdk b/p4-16/spec/P4-16-spec.mdk index c85fb271bf..e55c0e305f 100644 --- a/p4-16/spec/P4-16-spec.mdk +++ b/p4-16/spec/P4-16-spec.mdk @@ -2777,12 +2777,12 @@ 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 +P4 target architectures may optionally support saturating arithmetic. All saturating 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 +[Saturating Arithmetic](https://en.wikipedia.org/wiki/Saturation_arithmetic) saturating arithmetic is 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 From 3239a48f79d253902ded64e3e548ac5db83a21c7 Mon Sep 17 00:00:00 2001 From: Calin Cascaval Date: Sat, 21 Apr 2018 12:27:59 -0700 Subject: [PATCH 4/4] add precedence for saturating operations and more optional language --- p4-16/spec/P4-16-spec.mdk | 7 +++++-- p4-16/spec/grammar.mdk | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/p4-16/spec/P4-16-spec.mdk b/p4-16/spec/P4-16-spec.mdk index e55c0e305f..e6e66e479c 100644 --- a/p4-16/spec/P4-16-spec.mdk +++ b/p4-16/spec/P4-16-spec.mdk @@ -2792,6 +2792,8 @@ 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`. +The precedence of saturating addition and subtraction operations is the +same as for modulo arithmetic addition and subtraction. All binary operations (except shifts) require both operands to have the same exact type and width; supplying operands with different @@ -2870,8 +2872,9 @@ 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 -integers. For a signed integer with bit-width of `W`, the minimum value is +P4 supports saturating arithmetic (addition and subtraction) for signed +integers. Targets may optionally reject programs using saturating arithmetic. +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 diff --git a/p4-16/spec/grammar.mdk b/p4-16/spec/grammar.mdk index aa37dd8e72..b0fb527f5e 100644 --- a/p4-16/spec/grammar.mdk +++ b/p4-16/spec/grammar.mdk @@ -549,7 +549,7 @@ lvalue %left '^' %left '&' %left SHL -%left PP '+' '-' +%left PP '+' '-' '|+|' '|-|' %left '*' '/' '%' %right PREFIX %nonassoc ']' '(' '[' @@ -580,6 +580,8 @@ expression | expression '%' expression | expression '+' expression | expression '-' expression + | expression '|+|' expression + | expression '|-|' expression | expression SHL expression // << | expression '>''>' expression // check that >> are adjacent | expression LE expression // <=