def mult(op0,op1,outpar): multiplicand=int(op0,2) multiplicand=twos_comp(multiplicand,len(op0)) multiplier=int(op1,2) multiplier=twos_comp(multiplier,len(op1)) product=multiplicand*multiplier return printer_2s(product,outpar)+'\n'
def binStimGen(sign, bitDyn, nBit): if (sign): num = random.randint(-2**(bitDyn - 1), 2**(bitDyn - 1) - 1) else: num = random.randint(0, 2**(bitDyn) - 1) string = printer_2s(num, nBit) return string
def mult_trunc_pp (op0,op1,outpar,lsbs): if len(op0)!=len(op1): DADDALEVELS=int(input("How many DADDALEVELS should I set?\n")) else: DADDALEVELS=len(op0)/2 + 1 multiplicand=int(op0,2) multiplicand=twos_comp(multiplicand,len(op0)) multiplier=int(op1,2) multiplier=twos_comp(multiplier,len(op1)) # Initialize pp matrix pp=[] i=0 inv=0 # Calculating partial products while i < DADDALEVELS-1: recode_ctr=recode_calculator(op1,i) pp.append((pp_calculator(multiplicand,recode_ctr)<<(2*i))+inv) inv=inv_evaluator(recode_ctr)<<(2*i) i+=1 pp.append(inv) # Truncating LSBs if requested ending_str='0'*lsbs i=0 while i<DADDALEVELS: original_str=printer_2s(pp[i],outpar) new_str=original_str[0:len(original_str)-lsbs]+ending_str new_num=int(new_str,2) pp[i]=twos_comp(new_num,len(new_str)) i+=1 # Summing pp i=0 product=0 while i<DADDALEVELS: product=pp[i]+product i+=1 # Writing result return printer_2s(product,outpar)+'\n'
def binStimGen(fname, lineNum, stimInLine, bitDyn, nBit, delimiter): with open(fname, "w") as fout_pointer: i = 0 while i < lineNum: j = 0 string = "" while j < stimInLine: num = random.randint(-2**(bitDyn - 1), 2**(bitDyn - 1) - 1) string += printer_2s(num, nBit) string += delimiter j += 1 fout_pointer.write(string + "\n") i += 1
inv=0 # Calculating partial products while i < DADDALEVELS-1: recode_ctr=recode_calculator(nums[0],i) pp.append((pp_calculator(multiplicand,recode_ctr)<<(2*i))+inv) inv=inv_evaluator(recode_ctr)<<(2*i) i+=1 pp.append(inv) # Truncating LSBs if requested if len(sys.argv)>1: APPROX=int(sys.argv[1]) ending_str='0'*APPROX i=0 while i<DADDALEVELS: original_str=printer_2s(pp[i],OUTPAR) new_str=original_str[0:len(original_str)-APPROX]+ending_str new_num=int(new_str,2) pp[i]=twos_comp(new_num,len(new_str)) i+=1 # Summing pp i=0 product=0 while i<DADDALEVELS: product=pp[i]+product i+=1 # Writing result out_file.write(printer_2s(product,OUTPAR)+'\n')
#!/bin/python3 FIN_NAME = "common/samplesE.txt" FOUT_NAME = "common/pyresult.txt" OUTPAR = 47 in_file = open(FIN_NAME, "r") out_file = open(FOUT_NAME, "w") from bin_lib import twos_comp, printer_2s for line in in_file: nums = line.split() multiplicand = int(nums[0], 2) multiplicand = twos_comp(multiplicand, len(nums[0])) multiplier = int(nums[1], 2) multiplier = twos_comp(multiplier, len(nums[1])) product = multiplicand * multiplier out_file.write(printer_2s(product, OUTPAR) + '\n')
def integerTo2sFileConverter(infile,outfile,outpar): with open(infile,"r") as fin_pointer, open(outfile,"w") as fout_pointer: for line in fin_pointer: fout_pointer.write(printer_2s(int(line),outpar)+'\n')