def test_low_bw_error(self): with self.assertRaises(pyrtl.PyrtlError): libutils.twos_comp_repr(-40, 6) with self.assertRaises(pyrtl.PyrtlError): libutils.rev_twos_comp_repr(88, 6) with self.assertRaises(pyrtl.PyrtlError): libutils.rev_twos_comp_repr(8, 4)
def mult_t_base(self, len_a, len_b, **mult_args): # Creating the logic nets a, b = pyrtl.Input(len_a, "a"), pyrtl.Input(len_b, "b") product = pyrtl.Output(name="product") product <<= multipliers.signed_tree_multiplier(a, b, **mult_args) self.assertEqual(len(product), len_a + len_b) # creating the testing values and the correct results bound_a = 2**(len_a - 1) - 1 bound_b = 2**(len_b - 1) - 1 xvals = [int(random.uniform(-bound_a, bound_a)) for i in range(20)] yvals = [int(random.uniform(-bound_b, bound_b)) for i in range(20)] true_result = [i * j for i, j in zip(xvals, yvals)] # Setting up and running the tests sim_trace = pyrtl.SimulationTrace() sim = pyrtl.Simulation(tracer=sim_trace) for cycle in range(len(xvals)): sim.step({ a: libutils.twos_comp_repr(xvals[cycle], len_a), b: libutils.twos_comp_repr(yvals[cycle], len_b) }) # Extracting the values and verifying correctness multiplier_result = [ libutils.rev_twos_comp_repr(p, len(product)) for p in sim_trace.trace[product.name] ] self.assertEqual(multiplier_result, true_result)
def mult_t_base(self, len_a, len_b, **mult_args): # Creating the logic nets a, b = pyrtl.Input(len_a, "a"), pyrtl.Input(len_b, "b") product = pyrtl.Output(name="product") product <<= multipliers.signed_tree_multiplier(a, b, **mult_args) self.assertEquals(len(product), len_a + len_b) # creating the testing values and the correct results bound_a = 2**(len_a-1) - 1 bound_b = 2**(len_b-1) - 1 xvals = [int(random.uniform(-bound_a, bound_a)) for i in range(20)] yvals = [int(random.uniform(-bound_b, bound_b)) for i in range(20)] true_result = [i * j for i, j in zip(xvals, yvals)] # Setting up and running the tests sim_trace = pyrtl.SimulationTrace() sim = pyrtl.Simulation(tracer=sim_trace) for cycle in range(len(xvals)): sim.step({ a: libutils.twos_comp_repr(xvals[cycle], len_a), b: libutils.twos_comp_repr(yvals[cycle], len_b) }) # Extracting the values and verifying correctness multiplier_result = [libutils.rev_twos_comp_repr(p, len(product)) for p in sim_trace.trace[product]] self.assertEqual(multiplier_result, true_result)
def test_low_bw_error(self): with self.assertRaises(pyrtl.PyrtlError): libutils.twos_comp_repr(-40, 6) with self.assertRaises(pyrtl.PyrtlError): libutils.rev_twos_comp_repr(88, 6) with self.assertRaises(pyrtl.PyrtlError): libutils.rev_twos_comp_repr(8, 4)
def test_twos_comp_sim(self): self.out <<= self.in1 + self.in2 sim_trace = pyrtl.SimulationTrace() sim = pyrtl.Simulation(tracer=sim_trace) for i in range(10): sim.step({'in1': i, 'in2': libutils.twos_comp_repr(-2 * i, 8)}) self.assertEquals( -i, libutils.rev_twos_comp_repr(sim.inspect('out'), 8))
def test_twos_comp_sim(self): self.out <<= self.in1 + self.in2 sim_trace = pyrtl.SimulationTrace() sim = pyrtl.Simulation(tracer=sim_trace) for i in range(10): sim.step({ 'in1': i, 'in2': libutils.twos_comp_repr(-2*i, 8) }) self.assertEquals(-i, libutils.rev_twos_comp_repr(sim.inspect('out'), 8))
def test_inverse_functionality(self): for i in range(20): self.assertEqual(i * 3, libutils.rev_twos_comp_repr( libutils.twos_comp_repr(i * 3, 16), 16))
def test_inverse_functionality(self): for i in range(20): self.assertEquals(i*3, libutils.rev_twos_comp_repr( libutils.twos_comp_repr(i*3, 16), 16))