Skip to content
Snippets Groups Projects
Commit 874f0484 authored by Samuel Gruetter's avatar Samuel Gruetter
Browse files

make BitsTricks test more termination-friendly

counting from 31 down to 0 instead of from 0 up to 31
parent be71561e
No related branches found
No related tags found
No related merge requests found
...@@ -47,20 +47,17 @@ object BitsTricks { ...@@ -47,20 +47,17 @@ object BitsTricks {
x ^ y ^ x x ^ y ^ x
} ensuring(res => res == y) } ensuring(res => res == y)
def turnOffRightmostOneRec(x: Int, indexFromLeft: Int): Int = {
def turnOffRightmostOneRec(x: Int, index: Int): Int = { require(0 <= indexFromLeft && indexFromLeft < 32)
require(index >= 0 && index < 32) if(bitAt(x, 31 - indexFromLeft)) toggleBitN(x, 31 - indexFromLeft)//(x ^ (1 << (31 - indexFromLeft)))
if(bitAt(x, index)) toggleBitN(x, index)//(x ^ (1 << index)) else if(indexFromLeft == 0) x
else if(index == 31) x else turnOffRightmostOneRec(x, indexFromLeft - 1)
else turnOffRightmostOneRec(x, index + 1)
} }
/* // proves in 10s
* loops forever on the proof
*/
def turnOffRightmostOne(x: Int): Int = { def turnOffRightmostOne(x: Int): Int = {
x & (x - 1) x & (x - 1)
} //ensuring(_ == turnOffRightmostOneRec(x, 0)) } //ensuring(_ == turnOffRightmostOneRec(x, 31))
// 010100 -> 010111 // 010100 -> 010111
def rightPropagateRightmostOne(x: Int): Int = { def rightPropagateRightmostOne(x: Int): Int = {
...@@ -73,10 +70,9 @@ object BitsTricks { ...@@ -73,10 +70,9 @@ object BitsTricks {
} ensuring(b => b) } ensuring(b => b)
def isRotationLeft(x: Int, y: Int, n: Int, i: Int): Boolean = { def isRotationLeft(x: Int, y: Int, n: Int, i: Int): Boolean = {
require(i >= 0 && i <= 32 && n >= 0 && n < 32) require(0 <= i && i < 32 && 0 <= n && n < 32)
if(i == 32) val isOk = bitAt(x, i) == bitAt(y, (i + n) % 32)
true if (i == 0) isOk else isOk && isRotationLeft(x, y, n, i-1)
else bitAt(x, i) == bitAt(y, (i + n) % 32) && isRotationLeft(x, y, n, i+1)
} }
//rotateLeft proves in 1 minute (on very powerful computer) //rotateLeft proves in 1 minute (on very powerful computer)
...@@ -84,7 +80,7 @@ object BitsTricks { ...@@ -84,7 +80,7 @@ object BitsTricks {
require(n >= 0 && n < 32) require(n >= 0 && n < 32)
val front = x >>> (32 - n) val front = x >>> (32 - n)
(x << n) | front (x << n) | front
} //ensuring(res => isRotationLeft(x, res, n, 0)) } //ensuring(res => isRotationLeft(x, res, n, 31))
//careful with overflows, case definition, truncated //careful with overflows, case definition, truncated
def safeMean(x: Int, y: Int): Int = { def safeMean(x: Int, y: Int): Int = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment