diff options
author | Brett Weiland <brett_weiland@gmail.com> | 2024-04-06 23:10:38 -0500 |
---|---|---|
committer | Brett Weiland <brett_weiland@gmail.com> | 2024-04-06 23:10:38 -0500 |
commit | 003b0887daa5cceb093a5c4a7bd80e8fed6619e8 (patch) | |
tree | 9d4bf10a0f9e7251d9c110f4330e8dbd215e6ed8 | |
parent | 49223f19563303fed2b63af44aeff021d8b4790e (diff) |
I cant figure out what is wrong.
-rwxr-xr-x | booth_multiplier.py | 17 | ||||
-rw-r--r-- | input.txt | 2 |
2 files changed, 5 insertions, 14 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) @@ -5,7 +5,7 @@ 111011 100011 00011111 01010101 11010111 01010101 -01010101 11010111 +01010101 11010111 01110111 00110011 00000000 01110111 0101010101 0101010101 |