Skip to content

Instantly share code, notes, and snippets.

@randName
Last active December 18, 2018 12:32
Show Gist options
  • Save randName/7a478093c0b58773b15fcadcdcca205b to your computer and use it in GitHub Desktop.
Save randName/7a478093c0b58773b15fcadcdcca205b to your computer and use it in GitHub Desktop.
Generates Kogge-Stone Full Adder for SAT CNF Format
fulladder = """aS{0}:=ODD(A{0},B{0},C{1});
C{0}:=OR(AND(A{0},B{0}),AND(C{1},ODD(A{0},B{0})));"""
def koggestone(i, l, maxi):
g = "G{2}{1}:=OR(G{3}{1},AND(P{3}{1},{4}));"
p = "\nP{2}{1}:=AND(P{3}{1},P{3}{0});"
i -= 1
s = 2 ** l
if i + s >= maxi:
return ""
vals = (i, i + s, chr(l + 98), chr(l + 97), takeup(i, l))
return g.format(*vals) + (p.format(*vals) if i > s - 2 else "")
def takeup(i, l):
if i == -1:
return "Ci"
while i <= 2 ** (l - 1) - 2:
l -= 1
return "G%s%d" % (chr(l + 97), i)
if __name__ == "__main__":
h = 5
f = range(2 ** h)
for i in f:
print(fulladder.format(i, (i - 1) if i else "i"))
for i in f:
print("Ga{0}:=AND(A{0},B{0}); Pa{0}:=ODD(A{0},B{0});".format(i))
for j in range(h):
for i in f:
print(koggestone(i, j, 2 ** h))
for i in f:
print("bS{0}:=ODD(Pa{0},{1});".format(i, takeup(i - 1, 5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment