Add binary multiplication functions #3
212
bitutilities.py
212
bitutilities.py
|
@ -1,5 +1,6 @@
|
|||
from collections import deque
|
||||
from typing_extensions import Self
|
||||
from lib.prettytable import PrettyTable
|
||||
|
||||
|
||||
class BasicRegister:
|
||||
|
@ -13,10 +14,10 @@ class BasicRegister:
|
|||
self.memory: deque[bool] = deque(memory)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Memory: {[int(value) for value in self.memory]}"
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Memory: {[int(value) for value in self.memory]}"
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.memory)
|
||||
|
@ -70,6 +71,32 @@ class BasicRegister:
|
|||
return shifted_bits
|
||||
|
||||
|
||||
class Counter:
|
||||
"""
|
||||
The Counter represents a hardware register specifically designed for countdowns.
|
||||
|
||||
:param int value: Initial numeric value this Counter holds.
|
||||
"""
|
||||
|
||||
def __init__(self, value: int):
|
||||
self.memory: deque[bool] = deque([i == "1" for i in bin(value)[2:]])
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.memory)
|
||||
|
||||
def decrement(self):
|
||||
self.memory = binary_subtraction(self, BasicRegister([False] * (len(self.memory) - 1) + [True])).memory
|
||||
|
||||
def non_zero(self) -> bool:
|
||||
return any(self.memory)
|
||||
|
||||
|
||||
def get_memory(variable_name: str) -> list[bool]:
|
||||
"""
|
||||
Reads user input to be used as a memory array.
|
||||
|
@ -101,16 +128,12 @@ def binary_sum_with_carry(first_term: BasicRegister, second_term: BasicRegister)
|
|||
result_term = BasicRegister([False] * len(first_term))
|
||||
|
||||
carry = False
|
||||
for i in range(len(first_term) - 1, 0, -1):
|
||||
for i in range(len(first_term) - 1, -1, -1):
|
||||
current_bit_sum = first_term.memory[i] + second_term.memory[i] + carry
|
||||
carry = bool(current_bit_sum & 2)
|
||||
result_term.memory[i] = bool(current_bit_sum & 1)
|
||||
|
||||
final_bit_sum = first_term.memory[0] + second_term.memory[0] + carry
|
||||
final_carry = bool(final_bit_sum & 2)
|
||||
result_term.memory[0] = bool(final_bit_sum & 1)
|
||||
|
||||
return result_term, final_carry
|
||||
return result_term, carry
|
||||
|
||||
|
||||
def binary_sum(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
|
@ -182,3 +205,176 @@ def align_registers(*registers: BasicRegister) -> tuple[BasicRegister, ...]:
|
|||
"""
|
||||
required_size: int = max(map(len, registers))
|
||||
return tuple(reg.adjusted_by_size(required_size) for reg in registers)
|
||||
|
||||
|
||||
def format_device_state_table(table) -> str:
|
||||
pt = PrettyTable()
|
||||
pt.field_names = table[0]
|
||||
|
||||
for block in table[1:]:
|
||||
for line in block[:-1]:
|
||||
pt.add_row(line)
|
||||
pt.add_row(block[-1], divider = True)
|
||||
|
||||
return pt.get_string()
|
||||
|
||||
|
||||
def binary_multiplication_method_1(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using first method.
|
||||
|
||||
:param BasicRegister first_term: First register to multiply.
|
||||
:param BasicRegister second_term: Second register to multiply.
|
||||
|
||||
:return: Register containing the product.
|
||||
:rtype: BasicRegister
|
||||
"""
|
||||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * n)
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister(second_term.memory)
|
||||
ct = Counter(n)
|
||||
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "CT", "MicroOperations"]]
|
||||
|
||||
i = 0
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "-"])))
|
||||
|
||||
while ct.non_zero():
|
||||
i += 1
|
||||
data_table.append([])
|
||||
|
||||
if rg2.memory[n-1]:
|
||||
rg1 = binary_sum(rg1, rg3)
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "RG1 := RG1 + RG3"])))
|
||||
|
||||
rg2.right_shift(rg1.memory[n-1])
|
||||
rg1.right_shift()
|
||||
ct.decrement()
|
||||
|
||||
print(ct.memory)
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "RG2 := RG1[1].r(RG2)\nRG1 := 0.r(RG1)\nCT := CT - 1"])))
|
||||
|
||||
return BasicRegister(list(rg1.memory) + list(rg2.memory)), data_table
|
||||
|
||||
def binary_multiplication_method_2(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using second method.
|
||||
|
||||
:param BasicRegister first_term: First register to multiply.
|
||||
:param BasicRegister second_term: Second register to multiply.
|
||||
|
||||
:return: Register containing the product.
|
||||
:rtype: BasicRegister
|
||||
"""
|
||||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * (2*n))
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister([False] * n + list(second_term.memory))
|
||||
|
||||
i = 0
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
|
||||
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "-"])))
|
||||
|
||||
while any(rg2.memory):
|
||||
i += 1
|
||||
data_table.append([])
|
||||
|
||||
if rg2.memory[n-1]:
|
||||
rg1 = binary_sum(rg1, rg3)
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG1 := RG1 + RG3"])))
|
||||
|
||||
rg2.right_shift()
|
||||
rg3.left_shift()
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG2 := 0.r(RG2)\nRG3 := l(RG3).0"])))
|
||||
|
||||
return rg1, data_table
|
||||
|
||||
def binary_multiplication_method_3(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using third method.
|
||||
|
||||
:param BasicRegister first_term: First register to multiply.
|
||||
:param BasicRegister second_term: Second register to multiply.
|
||||
|
||||
:return: Register containing the product.
|
||||
:rtype: BasicRegister
|
||||
"""
|
||||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
data_table = [["iter", "RG2", "RG1", "RG3", "CT", "MicroOperations"]]
|
||||
|
||||
rg1 = BasicRegister([False] * n)
|
||||
rg2 = BasicRegister(list(first_term.memory) + [False])
|
||||
rg3 = BasicRegister([False] * (n+1) + list(second_term.memory))
|
||||
ct = Counter(n)
|
||||
|
||||
i = 0
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "-"])))
|
||||
|
||||
while ct.non_zero():
|
||||
i += 1
|
||||
data_table.append([])
|
||||
|
||||
if rg2.memory[0]:
|
||||
result: list[bool] = list(binary_sum(BasicRegister(rg2.memory + rg1.memory), rg3).memory)
|
||||
rg2 = BasicRegister(result[:n+1])
|
||||
rg1 = BasicRegister(result[n+1:])
|
||||
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := RG2.RG1 + RG3"])))
|
||||
|
||||
rg2.left_shift(rg1.memory[0])
|
||||
rg1.left_shift()
|
||||
ct.decrement()
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := l(RG2.RG1).0\nCT := CT - 1"])))
|
||||
|
||||
return BasicRegister((list(rg2.memory) + list(rg1.memory))[:-1]), data_table
|
||||
|
||||
def binary_multiplication_method_4(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using fourth method.
|
||||
|
||||
:param BasicRegister first_term: First register to multiply.
|
||||
:param BasicRegister second_term: Second register to multiply.
|
||||
|
||||
:return: Register containing the product.
|
||||
:rtype: BasicRegister
|
||||
"""
|
||||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * (2*n+1))
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister([False] + list(second_term.memory) + [False] * n)
|
||||
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
|
||||
|
||||
i = 0
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "-"])))
|
||||
|
||||
while any(rg2.memory):
|
||||
i += 1
|
||||
data_table.append([])
|
||||
|
||||
if rg2.memory[0]:
|
||||
rg1 = binary_sum(rg1, rg3)
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG1 := RG1 + RG3"])))
|
||||
|
||||
rg2.left_shift()
|
||||
rg3.right_shift()
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG2 := l(RG2).0\nRG3 := 0.r(RG3)"])))
|
||||
|
||||
return BasicRegister(list(rg1.memory)[:-1]), data_table
|
||||
|
|
File diff suppressed because it is too large
Load Diff
16
main.py
16
main.py
|
@ -16,7 +16,21 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg
|
|||
case "s":
|
||||
print(f"Subtraction:\n{bu.binary_subtraction(first_register, second_register)}")
|
||||
case "m":
|
||||
pass
|
||||
match input("Choose method to use (1-4):\n>>> "):
|
||||
case "1":
|
||||
result, data_table = bu.binary_multiplication_method_1(first_register, second_register)
|
||||
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
|
||||
case "2":
|
||||
result, data_table = bu.binary_multiplication_method_2(first_register, second_register)
|
||||
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
|
||||
case "3":
|
||||
result, data_table = bu.binary_multiplication_method_3(first_register, second_register)
|
||||
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
|
||||
case "4":
|
||||
result, data_table = bu.binary_multiplication_method_4(first_register, second_register)
|
||||
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
|
||||
case _:
|
||||
print("Such method does not exist, try again.")
|
||||
case "d":
|
||||
pass
|
||||
case "q":
|
||||
|
|
Loading…
Reference in New Issue