almost all but booth mod working, still need benchmark

This commit is contained in:
Brett Weiland 2024-04-06 18:03:56 -05:00
parent 53e65de28e
commit 210ada5584
2 changed files with 54 additions and 41 deletions

View File

@ -1,12 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from tabulate import tabulate
with open('input.txt') as f: with open('input.txt') as f:
input_string = f.read().split('\n') input_string = f.read().split('\n')
def twos_comp(num, length): def twos_comp(num, length):
if num == 0: if num == 0:
return 0 return 0
num ^= ((1 << length) - 1) return abs((num ^ ((1 << length) - 1)) + 1)
return num + 1
def logical_shiftr(num, length, times): def logical_shiftr(num, length, times):
for t in range(times): for t in range(times):
@ -18,16 +19,20 @@ def convert_if_twoscomp(num, length):
return (-1 * twos_comp(num, length)) return (-1 * twos_comp(num, length))
return num & (1 << length) - 1 return num & (1 << length) - 1
def debug(multiplicand_b, multiplier_b, length): def debug(results):
sign = 0 headers = ['multiplicand bin', 'multiplier bin', 'multiplicand dec', 'multiplier dec', 'expected bin', 'expected dec', 'booth if correct', 'booth mod if correct']
multiplicand = convert_if_twoscomp(multiplicand_b, length) table = []
multiplier = convert_if_twoscomp(multiplier_b, length) for [multiplicand_bin, multiplier_bin, result_booth, result_booth_mod, length] in results:
multiplicand = convert_if_twoscomp(multiplicand_bin, length)
result = multiplicand * multiplier multiplier = convert_if_twoscomp(multiplier_bin, length)
result_bin = (twos_comp(result, length * 2), result) [result > 0] expected = multiplicand * multiplier
expected_bin = (twos_comp(expected, length * 2), expected) [expected > 0]
success_b = [bin(result_booth), "PASS"] [result_booth == expected_bin]
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("expected result:\t{}\t*\t{}\t=\t{}\t=\t{}".format(multiplicand, multiplier, bin(result_bin), multiplicand * multiplier))
#print("booths result:\t\t{}\t*\t{}\t=\t{}".format(bin(multiplicand), bin(multiplier), bin(result_booth)))
def booth(multiplier, multiplicand, length): def booth(multiplier, multiplicand, length):
@ -65,17 +70,27 @@ def booth_mod(multiplier, multiplicand, length):
for operation in input_string: if __name__ == "__main__":
if operation == '' or operation[0] == '#': headers = ['multiplicand', 'multiplier', 'result (bin)', 'result (hex)']
continue table = []
length = len(operation.split(" ")[0])
multiplicand = int(operation.split(" ")[0], 2)
multiplier = int(operation.split(" ")[1], 2)
debug_results = []
for operation in input_string:
if operation == '' or operation[0] == '#':
continue
length = len(operation.split(" ")[0])
multiplicand = int(operation.split(" ")[0], 2)
multiplier = int(operation.split(" ")[1], 2)
result_booth = booth(multiplier, multiplicand, length)
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)
print(tabulate(table, headers))
#generate table
debug(multiplicand, multiplier, length)
result_booth = booth(multiplier, multiplicand, length)
result_mod_booth = booth_mod(multiplier, multiplicand, length)
print("booths:\t\t\t", bin(result_booth))
print("modified booths:\t", bin(result_mod_booth))

View File

@ -1,20 +1,18 @@
#BEFORE TURNING IN. MAKE SURE THESE ARE RIGHT #BEFORE TURNING IN. MAKE SURE THESE ARE RIGHT
001011 011001 1110 1111
#1101101 0000111 0101 0000
#1110 1111 111111 111111
#0101 0000 101110 110111
#111111 111111 111011 100011
#101110 110111 00011111 01010101
#111011 100011 11010111 01010101
#00011111 01010101 01010101 11010111
#11010111 01010101 01110111 00110011
#01010101 11010111 00000000 01110111
#01110111 00110011 0101010101 0101010101
#00000000 01110111 1100111011 1001110000
#0101010101 0101010101 1001101110 0101111010
#1100111011 1001110000 010101010101 010101010101
#1001101110 0101111010 001111100111 000000000000
#010101010101 010101010101 101010101010 101010101010
#001111100111 000000000000 111001110000 000011111111
#101010101010 101010101010
#111001110000 000011111111