multiply.py: add method #2 support
This commit is contained in:
parent
3338a75114
commit
24b62d109a
|
@ -1,3 +1,9 @@
|
||||||
|
import bitutils as bu
|
||||||
|
|
||||||
|
# this needs to be replaced at some point, because:
|
||||||
|
# - it uses old notation for align operation, which contradicts with
|
||||||
|
# modern instruction sets, thus making it easily confusable with
|
||||||
|
# the bu.al() operation which aligns bits to the left
|
||||||
def align_binary_to_right(value, size):
|
def align_binary_to_right(value, size):
|
||||||
if "b" in value:
|
if "b" in value:
|
||||||
result = value.split("b")[1]
|
result = value.split("b")[1]
|
||||||
|
@ -15,19 +21,62 @@ def multiply(n, x, y, method):
|
||||||
- get just the end result of binary multiplication;
|
- get just the end result of binary multiplication;
|
||||||
|
|
||||||
it takes 4 arguments:
|
it takes 4 arguments:
|
||||||
n - (int) base register bit depth
|
n - (int) base register bit length
|
||||||
x - (int) value for X operand
|
x - (int) value for X operand
|
||||||
y - (int) value for Y operand
|
y - (int) value for Y operand
|
||||||
method - (int) which method to use to perform multiplication
|
method - (int) which method to use to perform multiplication
|
||||||
|
|
||||||
it returns 2 items:
|
it returns 2 items:
|
||||||
- (list) table with step-by-step operations and descriptions
|
- (list) table with step-by-step operations and descriptions (table format
|
||||||
- (str) binary representation of the result
|
depends on the chosen method)
|
||||||
|
- (str) binary representation of the result (method-independant)
|
||||||
|
|
||||||
Methods fully supported: №4
|
Methods fully supported:
|
||||||
|
- №2 (passed mult-test.py with 10 bits)
|
||||||
|
- №4 (passed mult-test.py with 12 bits)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if method == 4:
|
if method == 2:
|
||||||
|
# every table line has registers like so: RG1, RG3, RG2
|
||||||
|
data_table = [[["0", "0"*(2*n), "0"*n + bu.ar(bin(y)[2:], n), bu.ar(bin(x)[2:], n), "-"]]*2]
|
||||||
|
|
||||||
|
# iteration number
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while int('0b' + data_table[-1][-1][3], 2) != 0:
|
||||||
|
data_table.append([])
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if data_table[-2][-1][3][-1] == "1":
|
||||||
|
data_table[-1].append([
|
||||||
|
i,
|
||||||
|
#al(bin(int("0b"+data_table[-2][-1][1], 2) + int("0b"+data_table[-2][-1][2], 2))[-(2*n+1):], 2*n+1), # RG1 + RG3
|
||||||
|
bu.rsum(data_table[-2][-1][1], data_table[-2][-1][2], 2*n),
|
||||||
|
data_table[-2][-1][2],
|
||||||
|
data_table[-2][-1][3],
|
||||||
|
"RG1+RG3"
|
||||||
|
])
|
||||||
|
|
||||||
|
data_table[-1].append([
|
||||||
|
i,
|
||||||
|
data_table[-1][-1][1],
|
||||||
|
bu.l(data_table[-1][-1][2]), # l(RG3).0
|
||||||
|
bu.r(data_table[-1][-1][3]), # 0.r(RG2)
|
||||||
|
"0.r(RG2), l(RG3).0"
|
||||||
|
])
|
||||||
|
|
||||||
|
else:
|
||||||
|
data_table[-1].append([
|
||||||
|
i,
|
||||||
|
data_table[-2][-1][1],
|
||||||
|
bu.l(data_table[-2][-1][2]), # l(RG3).0
|
||||||
|
bu.r(data_table[-2][-1][3]), # 0.r(RG2)
|
||||||
|
"0.r(RG2), l(RG3).0"
|
||||||
|
])
|
||||||
|
|
||||||
|
return data_table, data_table[-1][-1][1]
|
||||||
|
|
||||||
|
elif method == 4:
|
||||||
# every table line has registers like so: RG1, RG3, RG2
|
# every table line has registers like so: RG1, RG3, RG2
|
||||||
data_table = [[["0", "0"*(2*n+1), "0" + al(bin(y)[2:], n) + "0"*n, al(bin(x)[2:], n), "-"]]*2]
|
data_table = [[["0", "0"*(2*n+1), "0" + al(bin(y)[2:], n) + "0"*n, al(bin(x)[2:], n), "-"]]*2]
|
||||||
|
|
||||||
|
@ -66,6 +115,19 @@ def multiply(n, x, y, method):
|
||||||
|
|
||||||
return data_table, data_table[-1][-1][1][:-1]
|
return data_table, data_table[-1][-1][1][:-1]
|
||||||
|
|
||||||
|
def table_to_text(dt):
|
||||||
|
from lib.prettytable import PrettyTable
|
||||||
|
pt = PrettyTable()
|
||||||
|
pt.field_names = ["Iteration", "RG1", "RG3", "RG2", "Operations"]
|
||||||
|
|
||||||
|
for i in dt:
|
||||||
|
for j in range(len(i)):
|
||||||
|
if j+1 == len(i):
|
||||||
|
pt.add_row(i[j], divider = True)
|
||||||
|
else:
|
||||||
|
pt.add_row(i[j])
|
||||||
|
|
||||||
|
return pt.get_string()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# a fully functional reference
|
# a fully functional reference
|
||||||
|
@ -87,6 +149,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
dt, result = multiply(n, x, y, method)
|
dt, result = multiply(n, x, y, method)
|
||||||
|
|
||||||
|
'''
|
||||||
from lib.prettytable import PrettyTable
|
from lib.prettytable import PrettyTable
|
||||||
pt = PrettyTable()
|
pt = PrettyTable()
|
||||||
pt.field_names = ["Iteration", "RG1", "RG3", "RG2", "Operations"]
|
pt.field_names = ["Iteration", "RG1", "RG3", "RG2", "Operations"]
|
||||||
|
@ -99,4 +162,6 @@ if __name__ == "__main__":
|
||||||
pt.add_row(i[j])
|
pt.add_row(i[j])
|
||||||
|
|
||||||
print(pt)
|
print(pt)
|
||||||
|
'''
|
||||||
|
print(table_to_text(dt))
|
||||||
print(f"Result: {result}")
|
print(f"Result: {result}")
|
||||||
|
|
Loading…
Reference in New Issue