Skip to content

Commit 7320cac

Browse files
committed
Add Bitwise!Not operator.
[Feature]
1 parent 0ce6195 commit 7320cac

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

modules/Bitwise.tla

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ x ^^ y == \* single "^" already taken by Naturals.tla
5252

5353
-------------------------------------------------------------------------------
5454

55+
RECURSIVE NotR(_,_,_)
56+
LOCAL NotR(x,n,m) ==
57+
LET exp == 2^n
58+
xdm == (x \div exp) % 2
59+
IN IF m = 0
60+
THEN 0
61+
ELSE exp * ((xdm + 1) % 2)
62+
+ NotR(x,n+1,m \div 2)
63+
64+
Not(a) ==
65+
(***************************************************************************)
66+
(* Bitwise NOT of (non-negative) x (defined for Nat \cup {0}). *)
67+
(***************************************************************************)
68+
NotR(a,0,a)
69+
70+
-------------------------------------------------------------------------------
71+
5572
RECURSIVE shiftR(_,_)
5673
shiftR(n,pos) ==
5774
(***************************************************************************)

modules/tlc2/overrides/Bitwise.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public static IntValue Xor(IntValue x, IntValue y, IntValue n, IntValue m) {
4343
return IntValue.gen(x.val ^ y.val);
4444
}
4545

46+
@TLAPlusOperator(identifier = "Not", module = "Bitwise", warn = false)
47+
public static IntValue NotR(IntValue x) {
48+
// The TLA+ definition of Bitwise!Not has no type to work with and, thus,
49+
// only flips the bits in (Integer.numberOfLeadingZeros(x.val), 0]. Thus,
50+
// correct Java's not (~) flipping the upper bits of an 32 bit integer
51+
// by and'ing mask.
52+
final int mask = Integer.MAX_VALUE >> Integer.numberOfLeadingZeros(x.val) - 1;
53+
final int tmp = ~x.val & mask;
54+
return IntValue.gen(tmp);
55+
}
56+
4657
@TLAPlusOperator(identifier = "shiftR", module = "Bitwise", warn = false)
4758
public static IntValue shiftR(final IntValue n, final IntValue pos) {
4859
return IntValue.gen(n.val >>> pos.val);

tests/BitwiseTests.tla

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,55 @@ ASSUME \A n, m \in 0..32 : AssertEq(n ^^ m, XorPure(n,m))
114114

115115
-----------------------------------------------------------------------------
116116

117+
ASSUME(AssertEq(Not(0), 0))
118+
ASSUME(AssertEq(Not(1), 0))
119+
ASSUME(AssertEq(Not(2), 1))
120+
ASSUME(AssertEq(Not(3), 0))
121+
ASSUME(AssertEq(Not(4), 3))
122+
ASSUME(AssertEq(Not(5), 2))
123+
ASSUME(AssertEq(Not(6), 1))
124+
ASSUME(AssertEq(Not(7), 0))
125+
ASSUME(AssertEq(Not(8), 7))
126+
ASSUME(AssertEq(Not(9), 6))
127+
ASSUME(AssertEq(Not(10), 5))
128+
ASSUME(AssertEq(Not(11), 4))
129+
ASSUME(AssertEq(Not(12), 3))
130+
ASSUME(AssertEq(Not(13), 2))
131+
ASSUME(AssertEq(Not(14), 1))
132+
ASSUME(AssertEq(Not(15), 0))
133+
ASSUME(AssertEq(Not(16), 15))
134+
ASSUME(AssertEq(Not(17), 14))
135+
ASSUME(AssertEq(Not(18), 13))
136+
ASSUME(AssertEq(Not(19), 12))
137+
ASSUME(AssertEq(Not(20), 11))
138+
ASSUME(AssertEq(Not(21), 10))
139+
ASSUME(AssertEq(Not(22), 9))
140+
ASSUME(AssertEq(Not(23), 8))
141+
ASSUME(AssertEq(Not(24), 7))
142+
ASSUME(AssertEq(Not(25), 6))
143+
ASSUME(AssertEq(Not(26), 5))
144+
ASSUME(AssertEq(Not(27), 4))
145+
ASSUME(AssertEq(Not(28), 3))
146+
ASSUME(AssertEq(Not(29), 2))
147+
ASSUME(AssertEq(Not(30), 1))
148+
ASSUME(AssertEq(Not(31), 0))
149+
ASSUME(AssertEq(Not(32), 31))
150+
151+
NotPure(a) ==
152+
LET RECURSIVE NotRPure(_,_,_)
153+
NotRPure(x,n,m) ==
154+
LET exp == 2^n
155+
xdm == (x \div exp) % 2
156+
IN IF m = 0
157+
THEN 0
158+
ELSE exp * ((xdm + 1) % 2)
159+
+ NotRPure(x,n+1,m \div 2)
160+
IN NotRPure(a,0,a)
161+
162+
ASSUME \A n \in 0..64 : AssertEq(Not(n), NotPure(n))
163+
164+
-----------------------------------------------------------------------------
165+
117166
ASSUME(\A n \in ZeroToM : AssertEq(shiftR(n, 1), (n \div 2)))
118167

119168
=============================================================================

0 commit comments

Comments
 (0)