2023-04-01 19:18:10 +03:00
|
|
|
def align_binary_to_right(value, size):
|
|
|
|
|
if "b" in value:
|
|
|
|
|
result = value.split("b")[1]
|
|
|
|
|
else:
|
|
|
|
|
result = str(value)
|
|
|
|
|
|
|
|
|
|
return result.rjust(size, "0")
|
|
|
|
|
|
|
|
|
|
al = align_binary_to_right
|
|
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
def multiply(n, x, y, method):
|
|
|
|
|
'''
|
|
|
|
|
this method can be used to:
|
|
|
|
|
- get step-by-step binary multiplication data using different methods;
|
|
|
|
|
- get just the end result of binary multiplication;
|
|
|
|
|
|
|
|
|
|
it takes 4 arguments:
|
|
|
|
|
n - (int) base register bit depth
|
|
|
|
|
x - (int) value for X operand
|
|
|
|
|
y - (int) value for Y operand
|
|
|
|
|
method - (int) which method to use to perform multiplication
|
|
|
|
|
|
|
|
|
|
it returns 2 items:
|
|
|
|
|
- (list) table with step-by-step operations and descriptions
|
|
|
|
|
- (str) binary representation of the result
|
|
|
|
|
|
|
|
|
|
Methods fully supported: №4
|
|
|
|
|
'''
|
2023-04-01 19:29:57 +03:00
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
if method == 4:
|
2023-04-01 19:18:10 +03:00
|
|
|
# every table line has registers like so: RG1, RG3, RG2
|
2023-04-01 20:53:36 +03:00
|
|
|
data_table = [[["0", "0"*(2*n+1), "0" + al(bin(y)[2:], n) + "0"*n, al(bin(x)[2:], n), "-"]]*2]
|
|
|
|
|
|
|
|
|
|
# iteration number
|
|
|
|
|
i = 0
|
2023-04-01 19:29:57 +03:00
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
while int('0b' + data_table[-1][-1][3], 2) != 0:
|
2023-04-01 19:18:10 +03:00
|
|
|
data_table.append([])
|
2023-04-01 20:53:36 +03:00
|
|
|
i += 1
|
2023-04-01 19:18:10 +03:00
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
if data_table[-2][-1][3][0] == "1":
|
2023-04-01 19:18:10 +03:00
|
|
|
data_table[-1].append([
|
2023-04-01 20:53:36 +03:00
|
|
|
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
|
2023-04-01 19:18:10 +03:00
|
|
|
data_table[-2][-1][2],
|
2023-04-01 20:53:36 +03:00
|
|
|
data_table[-2][-1][3],
|
2023-04-01 19:18:10 +03:00
|
|
|
"RG1+RG3"
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
data_table[-1].append([
|
2023-04-01 20:53:36 +03:00
|
|
|
i,
|
|
|
|
|
data_table[-1][-1][1],
|
|
|
|
|
'0' + data_table[-1][-1][2][:-1], # 0.r(RG3)
|
|
|
|
|
data_table[-1][-1][3][1:] + '0', # l(RG2).0
|
2023-04-01 19:18:10 +03:00
|
|
|
"0.r(RG3), l(RG2).0"
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
data_table[-1].append([
|
2023-04-01 20:53:36 +03:00
|
|
|
i,
|
|
|
|
|
data_table[-2][-1][1],
|
|
|
|
|
'0' + data_table[-2][-1][2][:-1], # 0.r(RG3)
|
|
|
|
|
data_table[-2][-1][3][1:] + '0', # l(RG2).0
|
2023-04-01 19:18:10 +03:00
|
|
|
"0.r(RG3), l(RG2).0"
|
|
|
|
|
])
|
|
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
return data_table, data_table[-1][-1][1][:-1]
|
|
|
|
|
|
2023-04-01 19:18:10 +03:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-01 20:53:36 +03:00
|
|
|
# a fully functional reference
|
|
|
|
|
# implementation for this library
|
|
|
|
|
# is provided below
|
|
|
|
|
|
|
|
|
|
raw_x = input("X: ")
|
|
|
|
|
raw_y = input("Y: ")
|
|
|
|
|
|
|
|
|
|
if len(raw_x) == len(raw_y):
|
|
|
|
|
n = len(raw_x)
|
|
|
|
|
else:
|
|
|
|
|
n = int(input("n: "))
|
|
|
|
|
|
|
|
|
|
x = int("0b" + raw_x, 2)
|
|
|
|
|
y = int("0b" + raw_y, 2)
|
|
|
|
|
|
2023-04-01 19:18:10 +03:00
|
|
|
method = int(input("Method: "))
|
2023-04-01 20:53:36 +03:00
|
|
|
|
|
|
|
|
dt, result = multiply(n, x, y, method)
|
2023-04-01 19:18:10 +03:00
|
|
|
|
|
|
|
|
from lib.prettytable import PrettyTable
|
|
|
|
|
pt = PrettyTable()
|
2023-04-01 20:53:36 +03:00
|
|
|
pt.field_names = ["Iteration", "RG1", "RG3", "RG2", "Operations"]
|
2023-04-01 19:29:57 +03:00
|
|
|
|
2023-04-01 20:53:36 +03:00
|
|
|
for i in dt:
|
2023-04-01 19:18:10 +03:00
|
|
|
for j in range(len(i)):
|
|
|
|
|
if j+1 == len(i):
|
|
|
|
|
pt.add_row(i[j], divider = True)
|
|
|
|
|
else:
|
|
|
|
|
pt.add_row(i[j])
|
|
|
|
|
|
|
|
|
|
print(pt)
|
2023-04-01 20:53:36 +03:00
|
|
|
print(f"Result: {result}")
|