going fucking crazy
This commit is contained in:
		
							parent
							
								
									210ada5584
								
							
						
					
					
						commit
						49223f1956
					
				| @ -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) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Brett Weiland
						Brett Weiland