summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@gmail.com>2024-04-06 20:32:45 -0500
committerBrett Weiland <brett_weiland@gmail.com>2024-04-06 20:32:45 -0500
commit49223f19563303fed2b63af44aeff021d8b4790e (patch)
tree59bb7c8af9d68eb70cd8ef92c46199ee88553b11
parent210ada55845bc8ca04ee3100889a38b6cb1c62ba (diff)
going fucking crazy
-rwxr-xr-xbooth_multiplier.py22
-rw-r--r--input.txt1
2 files changed, 19 insertions, 4 deletions
diff --git a/booth_multiplier.py b/booth_multiplier.py
index a1ac1ab..435847f 100755
--- a/booth_multiplier.py
+++ b/booth_multiplier.py
@@ -10,8 +10,12 @@ def twos_comp(num, length):
return abs((num ^ ((1 << length) - 1)) + 1)
def logical_shiftr(num, length, times):
+ #print("SHIFT START___")
+ #print(bin(num))
for t in range(times):
num = (num >> 1) | ((1 << length) & num)
+ #print(bin(num))
+ #print("SHIFT END___")
return num
def convert_if_twoscomp(num, length):
@@ -31,17 +35,21 @@ def debug(results):
success_bm = [bin(result_booth_mod), "PASS"] [result_booth_mod == expected_bin]
table.append([bin(multiplicand_bin), bin(multiplier_bin), multiplicand, multiplier, bin(expected_bin), expected, success_b, success_bm])
- print("CHECKS: \n", tabulate(table, headers), "\n")
+ print("\nCHECKS: \n", tabulate(table, headers), "\n")
def booth(multiplier, multiplicand, length):
+ print("\n\n")
multiplicand_twos_comp = twos_comp(multiplicand, length)
+ print(multiplicand_twos_comp)
result = multiplier << 1 # extended bit
for i in range(length):
op = result & 0b11
if op == 0b01:
+ print(bin(result))
result += multiplicand << (length + 1)
+ print(bin(multiplicand << (length + 1)))
if op == 0b10:
result += multiplicand_twos_comp << (length + 1)
result &= (1 << (length * 2) + 1) - 1 # get rid of any overflows
@@ -53,17 +61,25 @@ def booth_mod(multiplier, multiplicand, length):
multiplicand_twos_comp = twos_comp(multiplicand, length)
result = multiplier << 1 # extended bit
for i in range(int(length / 2)):
+ print("before op " ,bin(result))
match result & 0b111:
case 0b010 | 0b001: # add
+ print("add")
result += multiplicand << (length + 1)
case 0b011: # add * 2
+ print("add x2")
result += multiplicand << (length + 2) # extra shift is multiplying multiplicand by 2
+ print(bin(multiplicand << (length + 2)))
+ print(bin(result))
case 0b100: # sub * 2
+ print("sub x2")
result += multiplicand_twos_comp << (length + 2)
case 0b101 | 0b110: # sub
+ print("sub")
result += multiplicand_twos_comp << (length + 1)
- result &= (1 << (length * 2) + 1) - 1 # get rid of any overflows
+ result &= (1 << ((length * 2) + 1)) - 1 # get rid of any overflows
result = logical_shiftr(result, length * 2, 2)
+ print("after op " ,bin(result))
result = result >> 1
return result
@@ -85,7 +101,7 @@ if __name__ == "__main__":
result_booth = booth(multiplier, multiplicand, length)
- result_mod_booth = booth_mod(multiplier, multiplicand, length)
+ result_mod_booth = booth_mod(multiplier, multiplicand, length + 1)
table.append([bin(multiplicand), bin(multiplier), bin(result_booth), hex(result_booth)])
debug_results.append([multiplicand, multiplier, result_booth, result_mod_booth, length])
debug(debug_results)
diff --git a/input.txt b/input.txt
index e8ec5e8..3e4755c 100644
--- a/input.txt
+++ b/input.txt
@@ -1,4 +1,3 @@
-#BEFORE TURNING IN. MAKE SURE THESE ARE RIGHT
1110 1111
0101 0000
111111 111111