diff options
author | Brett Weiland <brett_weiland@gmail.com> | 2024-04-06 18:03:56 -0500 |
---|---|---|
committer | Brett Weiland <brett_weiland@gmail.com> | 2024-04-06 18:03:56 -0500 |
commit | 210ada55845bc8ca04ee3100889a38b6cb1c62ba (patch) | |
tree | eea023992379d10651cdbfaeb2c2137b45a00027 | |
parent | 53e65de28e1d38c5f2174c519aaab9f7f4195e3b (diff) |
almost all but booth mod working, still need benchmark
-rwxr-xr-x | booth_multiplier.py | 59 | ||||
-rw-r--r-- | input.txt | 36 |
2 files changed, 54 insertions, 41 deletions
diff --git a/booth_multiplier.py b/booth_multiplier.py index 606aee5..a1ac1ab 100755 --- a/booth_multiplier.py +++ b/booth_multiplier.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 +from tabulate import tabulate + with open('input.txt') as f: input_string = f.read().split('\n') def twos_comp(num, length): if num == 0: return 0 - num ^= ((1 << length) - 1) - return num + 1 + return abs((num ^ ((1 << length) - 1)) + 1) def logical_shiftr(num, length, times): for t in range(times): @@ -18,16 +19,20 @@ def convert_if_twoscomp(num, length): return (-1 * twos_comp(num, length)) return num & (1 << length) - 1 -def debug(multiplicand_b, multiplier_b, length): - sign = 0 - multiplicand = convert_if_twoscomp(multiplicand_b, length) - multiplier = convert_if_twoscomp(multiplier_b, length) - - result = multiplicand * multiplier - result_bin = (twos_comp(result, length * 2), result) [result > 0] +def debug(results): + headers = ['multiplicand bin', 'multiplier bin', 'multiplicand dec', 'multiplier dec', 'expected bin', 'expected dec', 'booth if correct', 'booth mod if correct'] + table = [] + for [multiplicand_bin, multiplier_bin, result_booth, result_booth_mod, length] in results: + multiplicand = convert_if_twoscomp(multiplicand_bin, length) + multiplier = convert_if_twoscomp(multiplier_bin, length) + 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): @@ -65,17 +70,27 @@ def booth_mod(multiplier, multiplicand, length): -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) +if __name__ == "__main__": + headers = ['multiplicand', 'multiplier', 'result (bin)', 'result (hex)'] + table = [] + + 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)) @@ -1,20 +1,18 @@ #BEFORE TURNING IN. MAKE SURE THESE ARE RIGHT -001011 011001 -#1101101 0000111 -#1110 1111 -#0101 0000 -#111111 111111 -#101110 110111 -#111011 100011 -#00011111 01010101 -#11010111 01010101 -#01010101 11010111 -#01110111 00110011 -#00000000 01110111 -#0101010101 0101010101 -#1100111011 1001110000 -#1001101110 0101111010 -#010101010101 010101010101 -#001111100111 000000000000 -#101010101010 101010101010 -#111001110000 000011111111 +1110 1111 +0101 0000 +111111 111111 +101110 110111 +111011 100011 +00011111 01010101 +11010111 01010101 +01010101 11010111 +01110111 00110011 +00000000 01110111 +0101010101 0101010101 +1100111011 1001110000 +1001101110 0101111010 +010101010101 010101010101 +001111100111 000000000000 +101010101010 101010101010 +111001110000 000011111111 |