Skip to content

Commit

Permalink
Document and add a test for bit-shift operator behavior.
Browse files Browse the repository at this point in the history
This documents the current requirement that the right-hand operand of
<< and >> have type bit<32>.  I tend to consider that a bug (see
#1158), so I
hope that it will be relaxed in the future (and the documentation
updated).

Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
blp authored and ryzhyk committed Jul 19, 2022
1 parent 675e45b commit 588d489
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/language_reference/language_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ expr ::= term
| expr "?" (*try*)
```

In shift expressions `lhs "<<" rhs` and `lhs ">>" rhs`, `lhs` may have
any integer type. The value of `rhs`, which must have type `bit<32>`,
is taken modulo the bit-width of `lhs` rounded up to a power of 2, so
that, for example, shifting a value of type `bit<10>` left or right by
10 to 15 bits, inclusive, always yields a value of 0, and shifting by
16 bits has no effect.

The following table lists operators order by decreasing priority.

|**priority** | **operators** |
Expand Down
7 changes: 7 additions & 0 deletions test/datalog_tests/bit_shift.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
relation X(x: bit<10>, y: bit<10>)
output relation Y(x: bit<10>, y: bit<10>, z: bit<10>)
Y(x, y, x << (y as u32)) :- X(x, y).

X(x, y) :-
var x = FlatMap([0, 1, 2, 3]),
var y = FlatMap([0, 1, 9, 10, 11, 15, 16, 17]).
33 changes: 33 additions & 0 deletions test/datalog_tests/bit_shift.dump.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Y:
Y{.x = 0, .y = 0, .z = 0}: +1
Y{.x = 0, .y = 1, .z = 0}: +1
Y{.x = 0, .y = 9, .z = 0}: +1
Y{.x = 0, .y = 10, .z = 0}: +1
Y{.x = 0, .y = 11, .z = 0}: +1
Y{.x = 0, .y = 15, .z = 0}: +1
Y{.x = 0, .y = 16, .z = 0}: +1
Y{.x = 0, .y = 17, .z = 0}: +1
Y{.x = 1, .y = 0, .z = 1}: +1
Y{.x = 1, .y = 1, .z = 2}: +1
Y{.x = 1, .y = 9, .z = 512}: +1
Y{.x = 1, .y = 10, .z = 0}: +1
Y{.x = 1, .y = 11, .z = 0}: +1
Y{.x = 1, .y = 15, .z = 0}: +1
Y{.x = 1, .y = 16, .z = 1}: +1
Y{.x = 1, .y = 17, .z = 2}: +1
Y{.x = 2, .y = 0, .z = 2}: +1
Y{.x = 2, .y = 1, .z = 4}: +1
Y{.x = 2, .y = 9, .z = 0}: +1
Y{.x = 2, .y = 10, .z = 0}: +1
Y{.x = 2, .y = 11, .z = 0}: +1
Y{.x = 2, .y = 15, .z = 0}: +1
Y{.x = 2, .y = 16, .z = 2}: +1
Y{.x = 2, .y = 17, .z = 4}: +1
Y{.x = 3, .y = 0, .z = 3}: +1
Y{.x = 3, .y = 1, .z = 6}: +1
Y{.x = 3, .y = 9, .z = 512}: +1
Y{.x = 3, .y = 10, .z = 0}: +1
Y{.x = 3, .y = 11, .z = 0}: +1
Y{.x = 3, .y = 15, .z = 0}: +1
Y{.x = 3, .y = 16, .z = 3}: +1
Y{.x = 3, .y = 17, .z = 6}: +1

0 comments on commit 588d489

Please sign in to comment.