summaryrefslogtreecommitdiff
path: root/booth_multiplier.py
diff options
context:
space:
mode:
Diffstat (limited to 'booth_multiplier.py')
-rwxr-xr-xbooth_multiplier.py17
1 files changed, 4 insertions, 13 deletions
diff --git a/booth_multiplier.py b/booth_multiplier.py
index 435847f..517c613 100755
--- a/booth_multiplier.py
+++ b/booth_multiplier.py
@@ -40,16 +40,12 @@ def debug(results):
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
@@ -60,26 +56,21 @@ def booth(multiplier, multiplicand, length):
def booth_mod(multiplier, multiplicand, length):
multiplicand_twos_comp = twos_comp(multiplicand, length)
result = multiplier << 1 # extended bit
+ operations = 0
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)
+ if result & (1 << length):
+ print("{}: overflow".format(bin(multiplicand)))
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
@@ -101,7 +92,7 @@ if __name__ == "__main__":
result_booth = booth(multiplier, multiplicand, length)
- result_mod_booth = booth_mod(multiplier, multiplicand, length + 1)
+ result_mod_booth = booth_mod(multiplier, multiplicand, length)
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)