def test_exp(): test_vectors = [] eps = (2**-8) #Corner case test vectors test_vectors.append( [float2bfbin(2.0), float2bfbin(0), float2bfbin(math.exp(2)), 3]) test_vectors.append( [float2bfbin(-2.0), float2bfbin(0), float2bfbin(math.exp(-2)), 3]) test_vectors.append( [float2bfbin(1.0), float2bfbin(0), float2bfbin(math.exp(1.0)), 3]) test_vectors.append( [float2bfbin(0.0), float2bfbin(0), float2bfbin(math.exp(0)), 3]) test_vectors.append( [float2bfbin(-10.0), float2bfbin(0), float2bfbin(math.exp(-10)), 3]) test_vectors.append( [float2bfbin(5.0), float2bfbin(0), float2bfbin(math.exp(5)), 3]) #Random test vectors for vector_count in range(200): num = get_random_float(4) num_bfloat_str = float2bfbin(num) num_bfp = bfbin2float(num_bfloat_str) res_bfp = bfbin2float(float2bfbin(math.exp(num_bfp))) res_bfloat_str = float2bfbin(res_bfp) max_error = 2.0 * res_bfp * (math.fabs(2**(2 * num * eps) - 1) + eps) test_vectors.append( [num_bfloat_str, float2bfbin(0), res_bfloat_str, max_error]) #result = exp(op_a) for test_vector in test_vectors: op_a = Data(int(test_vector[0], 2)) op_b = Data(int(test_vector[1], 2)) exp_res = int(test_vector[2], 2) max_error = test_vector[3] exp = FExp() result = exp(op_a) golden_res = bfbin2float(test_vector[2]) actual_res = bfbin2float("{:016b}".format(int(result))) delta = math.fabs(golden_res - actual_res) #print("ln", bfbin2float(test_vector[0]), bfbin2float(test_vector[1]), # golden_res, actual_res, delta, max_error) assert delta <= max_error
def test_get_float_frac(args): fp0 = args[0] if is_nan_or_inf(fp0): pytest.skip("skipping nan") in0 = BFloat(fp0) in1 = Data(args[1]) #output = frac(input1) SIGNED inst = asm.fgetffrac() fstr = ''.join(["{:01b}".format(int(fp0.sign)), "{:08b}".format(int(fp0.exp)), "{:07b}".format(int(fp0.frac))]) frac = bfbin2float(fstr)-int(bfbin2float(fstr)) out = int(frac*(2**7)) res, res_p, _ = pe(inst, in0, in1) assert res==out rtl_tester(inst, in0, in1, res=out)
def test_ln(): test_vectors = [] eps = (2**-8) #Corner case test vectors test_vectors.append( [float2bfbin(2.0), float2bfbin(0), float2bfbin(math.log(2)), 2 * eps]) #Random test vectors for vector_count in range(200): num = math.fabs(get_random_float()) num_bfloat_str = float2bfbin(num) num_bfp = bfbin2float(num_bfloat_str) res = bfbin2float(float2bfbin(math.log(num_bfp))) res_bfloat_str = float2bfbin(res) num_exp = int(num_bfloat_str[1:9], 2) - 127 res_exp = int(res_bfloat_str[1:9], 2) - 127 max_error = 2 * (eps + math.fabs(eps * num_exp) + eps) test_vectors.append( [num_bfloat_str, float2bfbin(0), res_bfloat_str, max_error]) #result = ln(op_a) for test_vector in test_vectors: op_a = Data(int(test_vector[0], 2)) op_b = Data(int(test_vector[1], 2)) exp_res = int(test_vector[2], 2) max_error = test_vector[3] ln = FLN() result = ln(op_a) golden_res = bfbin2float(test_vector[2]) actual_res = bfbin2float("{:016b}".format(int(result))) delta = math.fabs(golden_res - actual_res) #print("ln", bfbin2float(test_vector[0]), bfbin2float(test_vector[1]), # golden_res, actual_res, delta, max_error) assert delta <= max_error
def test_div(): test_vectors = [] eps = (2**-8) #Corner case test vectors #Random test vectors for vector_count in range(200): divident_sp = get_random_float() divisor_sp = get_random_float() divident_bfloat_str = float2bfbin(divident_sp) divisor_bfloat_str = float2bfbin(divisor_sp) divident_bfp = bfbin2float(divident_bfloat_str) divisor_bfp = bfbin2float(divisor_bfloat_str) quotient = bfbin2float(float2bfbin(divident_bfp / divisor_bfp)) quotient_bfloat_str = float2bfbin(quotient) divisor_exp = int(divisor_bfloat_str[1:9], 2) - 127 max_error = 2 * math.fabs(eps * (divident_bfp / divisor_bfp)) test_vectors.append([ divident_bfloat_str, divisor_bfloat_str, quotient_bfloat_str, max_error ]) #result = op_a / op_b for test_vector in test_vectors: op_a = Data(int(test_vector[0], 2)) op_b = Data(int(test_vector[1], 2)) exp_res = int(test_vector[2], 2) max_error = test_vector[3] div = FDiv() result = div(op_a, op_b) golden_res = bfbin2float(test_vector[2]) actual_res = bfbin2float("{:016b}".format(int(result))) delta = math.fabs(golden_res - actual_res) #print("div", bfbin2float(test_vector[0]), bfbin2float(test_vector[1]), # golden_res, actual_res, delta, max_error) assert delta <= max_error
def test_get_float_int(args): fp0 = args[0] if is_nan_or_inf(fp0): pytest.skip("skipping nan") in0 = BFloat(fp0) in1 = Data(args[1]) #output = int(input1) SIGNED, VALID for input1<256.0 inst = asm.fgetfint() fstr = ''.join(["{:01b}".format(int(fp0.sign)), "{:08b}".format(int(fp0.exp)), "{:07b}".format(int(fp0.frac))]) out = int(bfbin2float(fstr)) if (abs(out) < ((2**8)-1)): out_overflow = 0 else: out_overflow = 1 res, res_p, _ = pe(inst, in0, in1) #assert V==out_overflow if (out_overflow==0): assert res==out rtl_tester(inst, in0, in1, res=out)