Skip to content

Instantly share code, notes, and snippets.

@corytodd
Last active August 29, 2015 14:18
Show Gist options
  • Save corytodd/4269f212de3ea3225f04 to your computer and use it in GitHub Desktop.
Save corytodd/4269f212de3ea3225f04 to your computer and use it in GitHub Desktop.
Version with some breathing room...
proc s(x, y, b: var int)=
x = x div 2
y = y div 2
b *= 2
proc n(x: var int): int =
return -(x+1)
proc a(x, y: var int): int =
var b = 1
while x > 0 and y > 0:
if (x mod 2 + y mod 2) == 2:
result += b
s(x,y,b)
proc o(x, y: var int): int =
var b = 1
while x + y > 0:
if (x mod 2 + y mod 2) >= 1:
result += b
s(x,y,b)
proc r(x, y: var int): int =
var b = 1
while x + y > 0:
if (x mod 2 + y mod 2) == 1:
result += b
s(x,y,b)
# Test harness
proc test(i,j, exp: int; p: proc; op: string){.procvar.}=
var
act: int
var x = i
var y = j
act = p(x,y)
if(exp != act):
echo("Error! ", i, op, j," ", exp," ", act)
import threadpool
var
x, exp, act: int
for i in countup(0, 0xffffffff):
x = i
exp = not x
act = n(x)
if(exp != act):
echo("Error! ~", i)
parallel:
for j in countup(0,0xffffffff):
spawn test(i,j,i and j, a," & ")
spawn test(i,j,i or j, o," | ")
spawn test(i,j,i xor j, r," ^ ")
@corytodd
Copy link
Author

corytodd commented Apr 3, 2015

Added test harness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment