Created
October 19, 2019 18:01
-
-
Save gynvael/b782fb5427801400f73709abbb6ea30d to your computer and use it in GitHub Desktop.
Gynvael's test code for the circuit described in the "Designing adder circuit for Fibonacci representation" article by Tomasz Idziaszek
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
N=15 | |
fib = [0, 1] | |
for i in xrange(2, N + 2): | |
fib.append(fib[i-1] + fib[i-2]) | |
del fib[0] | |
del fib[0] | |
print fib | |
def i2fib(v): | |
n = [0]*N | |
for i in xrange(N-1, -1, -1): | |
if v >= fib[i]: | |
n[i] = 1 | |
v -= fib[i] | |
if v == 0: | |
break | |
s = ''.join(["01"[i] for i in n[::-1]]) | |
if '11' in s: | |
return None | |
return n | |
def NOT(v): | |
return v ^ 1 | |
def fix(n): | |
t = n[0] & n[1] & NOT(n[2]) | |
return [ | |
t ^ n[0], | |
t ^ n[1], | |
t ^ n[2] | |
] | |
def h(a, b): | |
return [ | |
a & b, # A | |
a ^ b # B | |
] | |
def fibadder(a, b): | |
t0 = a[2] & NOT(b[1]) | |
t1 = a[2] | (b[2] & a[1]) | |
t2 = (b[1] & a[2]) | (b[2] & a[1]) | |
A = [ | |
a[0] | (b[0] & t0), | |
a[1] & NOT(t2), | |
0, | |
0 | |
] | |
B = [ | |
b[0] ^ t0, | |
b[1] ^ t2, | |
b[2] ^ t2, | |
b[3] | t1 | |
] | |
return (A, B) | |
def g(n, i): | |
if i < 0 or i >= len(n): | |
return 0 | |
return n[i] | |
def s(n, i, v): | |
if i < 0 or i >= len(n): | |
return | |
n[i] = v | |
def add(n, m): | |
a = {i: 0 for i in xrange(-2, N + 1)} | |
b = {i: 0 for i in xrange(-2, N + 1)} | |
for i in xrange(N): | |
a[i], b[i] = h(n[i], m[i]) | |
""" | |
print "n", n | |
print "m", m | |
print "a", a | |
print "b", b | |
""" | |
c = [0] * (N + 2) | |
for i in xrange(N - 3, -3, -1): | |
u = [a[j] for j in xrange(i, i + 4)] | |
v = [b[j] for j in xrange(i, i + 4)] | |
A, B = fibadder(u, v) | |
a[i+0] = A[0] | |
a[i+1] = A[1] | |
a[i+2] = A[2] | |
a[i+3] = A[3] | |
b[i+0] = B[0] | |
b[i+1] = B[1] | |
b[i+2] = B[2] | |
b[i+3] = B[3] | |
c[i+3] = B[3] | |
c[i+2] = B[2] | |
negc = B[1] | |
for i in xrange(N - 2, -2, -1): | |
p = [g(c, j) for j in xrange(i, i+3)] | |
if i == -1: | |
p[0] = negc | |
r = fix(p) | |
s(c, i, r[0]) | |
s(c, i+1, r[1]) | |
s(c, i+2, r[2]) | |
c[0] = r[0] | r[1] | |
for i in xrange(2, N + 2 - 2): | |
r = fix([g(c, j) for j in xrange(i, i+3)]) | |
s(c, i, r[0]) | |
s(c, i+1, r[1]) | |
s(c, i+2, r[2]) | |
return c[:N] | |
def inc(n): | |
a = n[:] | |
a[0] = NOT(n[0]) | |
a[1] = n[0] | n[1] | |
a.append(0) | |
for i in xrange(0, N - 2 + 1): | |
a[i:i+3] = fix(a[i:i+3]) | |
n[:] = a[:N] | |
def fib2i(n): | |
g = [fib[i] for i, v in enumerate(n) if v] | |
return sum(g) | |
def printfib(n): | |
if n is None: | |
print "None" | |
return False | |
g = [fib[i] for i, v in enumerate(n) if v] | |
s = ''.join(["01"[i] for i in n[::-1]]) | |
if '11' in s: | |
print "ERROR", | |
print sum(g), s, g | |
return True | |
""" | |
# Conversion bin to fib test | |
for i in xrange(0, 100): | |
if not printfib(i2fib(i)): | |
break | |
""" | |
# Testing "increment by 1" operation. | |
""" | |
n = i2fib(0) | |
for i in xrange(100): | |
printfib(n) | |
inc(n) | |
""" | |
# Test adding | |
for i in xrange(0, 10): | |
#sys.stdout.write('.') | |
#sys.stdout.flush() | |
for j in xrange(0, 10): | |
n = i2fib(i) | |
m = i2fib(j) | |
k = add(n, m) | |
print "-" * 70, i, j, i+j | |
print " ", | |
printfib(n) | |
print "+", | |
printfib(m) | |
print "=", | |
printfib(k) | |
if fib2i(k) != i + j: | |
print "ERROR" | |
print "n =", | |
printfib(n) | |
print "m =", | |
printfib(m) | |
print "k =", | |
printfib(k) | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment