def farith(s, inst): """ RD = RN <OP> RM BN = RD[31] if (RD[30:0] == 0) { BZ=1 } else { BZ=0 } if (UnbiasedExponent(RD) > 127) { BUV=1 } else { BV=0 } if (UbiasedExponent(RD) < -126) { BUS=1 } else { BUS=BUS } if (RM or RN == NAN) { BIS=1 } else { BIS=BIS } BVS = BVS | BV; """ if is16bit: inst.bits &= 0xffff rd = bits2float(s.rf[inst.rd]) rn = bits2float(s.rf[inst.rn]) rm = bits2float(s.rf[inst.rm]) # Binary operations. if name == 'add': result = rn + rm elif name == 'sub': result = rn - rm elif name == 'mul': result = rn * rm elif name == 'madd': result = rd + (rn * rm) elif name == 'msub': result = rd - (rn * rm) # Unary operations. elif name == 'float': result = float(s.rf[inst.rn]) elif name == 'fix': if math.isnan(rn): # FXIME result = 0xffffffff else: result = int(rn) elif name == 'abs': result = abs(rn) # 'result' is always a Python float. # RD = RN <OP> RM s.rf[inst.rd] = trim_32(result if name == 'fix' else float2bits(result)) # BN = RD[31] s.BN = True if result < 0.0 else False # if (RD[30:0] == 0) { BZ=1 } else { BZ=0 } s.BZ = True if abs(result) < 0.0001 else False # if (UnbiasedExponent(RD) > 127) { BUV=1 } else { BV=0 } s.BUV = True if get_exponent(s.rf[inst.rd]) > 127 else False # if (UbiasedExponent(RD) < -126) { BUS=1 } else { BUS=BUS } if get_exponent(s.rf[inst.rd]) > 127: s.BUS = True # if (RM or RN == NAN) { BIS=1 } else { BIS=BIS } if ((is_unary and math.isnan(rn)) or (not is_unary) and (math.isnan(rn) or math.isnan(rm))): s.BIS = True # BVS = BVS | BV; s.BVS = s.BVS or s.BV s.debug_flags() s.pc += 2 if is16bit else 4
def test_round_trip(): fuzzy_equals(bits2float(float2bits(0)), 0) fuzzy_equals(bits2float(float2bits(1.0)), 1.0) fuzzy_equals(bits2float(float2bits(-1.0)), -1.0) fuzzy_equals(bits2float(float2bits(-235.711)), -235.711) fuzzy_equals(bits2float(float2bits(235.711)), 235.711) assert math.isnan(bits2float(float2bits(float('nan')))) assert math.isinf(bits2float(float2bits(float('inf')))) assert math.isinf(bits2float(float2bits(float('-inf')))) assert bits2float(float2bits(float('inf'))) > 0 assert bits2float(float2bits(float('-inf'))) < 0
def fp_check(self, state, memory=[]): """Check all registers and flags against an expected state. For registers, convert the contents to a Python float and check that the state and expected state do not differ by more than self.epsilon. """ for index, expected in self.expected_registers: got = state.rf[index] if index > 63: reg_name = (key for key, value in reg_map.items() if value==index).next() else: reg_name = index if abs(bits2float(expected) - bits2float(got)) > self.epsilon: raise ValueError("Register %s differs by more than %.4f. Expected: %s got: %s" % (reg_name, self.epsilon, bits2float(expected), bits2float(got))) self.check_flags(state) self.check_memory(memory, state)