def fixed_to_real(value, point): if point < 0: return vtypes.SystemTask('itor', fixed_to_int(value, point)) if point == 0: return vtypes.SystemTask('itor', value) if isinstance(value, float): raise TypeError("value is already float.") if isinstance(value, (int, bool)) and isinstance(point, int): mag = 2 ** point return float(value) / mag signed = vtypes.get_signed(value) width = value.bit_length() msb = (value[width - 1] if isinstance(value, vtypes._Variable) else (value >> (width - 1)) & 0x1) v0 = (vtypes.SystemTask('itor', fixed_to_int(value, point)) + vtypes.SystemTask('itor', fixed_to_int_low(value, point)) / vtypes.SystemTask('itor', vtypes.Int(2) ** point)) nv = vtypes.Unot(value) + 1 v1 = ((vtypes.SystemTask('itor', fixed_to_int(nv, point)) + vtypes.SystemTask('itor', fixed_to_int_low(nv, point)) / vtypes.SystemTask('itor', vtypes.Int(2) ** point))) * vtypes.SystemTask('itor', -1) return vtypes.Mux(signed and msb == 0, v0, v1)
def __init__(self, left, right): lpoint = left.point if isinstance(left, _FixedBase) else 0 rpoint = right.point if isinstance(right, _FixedBase) else 0 lsigned = vtypes.get_signed(left) rsigned = vtypes.get_signed(right) point = _max_mux(lpoint, rpoint) signed = lsigned and rsigned if not self.overwrite_signed else False lwidth = vtypes.get_width(left) rwidth = vtypes.get_width(right) if lpoint <= rpoint: ldata, rdata = adjust(left, right, lpoint, rpoint, signed) shift_size = point else: ldata = left rdata = right shift_size = point - (lpoint - rpoint) try: lmsb = ldata[lwidth - 1] except: lmsb = (ldata >> (lwidth - 1) & vtypes.Int(1, 1, base=2)) try: rmsb = rdata[rwidth - 1] except: rmsb = (rdata >> (rwidth - 1) & vtypes.Int(1, 1, base=2)) abs_ldata = (ldata if not lsigned else vtypes.Mux( vtypes.Ulnot(lmsb), ldata, vtypes.Unot(ldata) + 1)) abs_rdata = (rdata if not rsigned else vtypes.Mux( vtypes.Ulnot(rmsb), rdata, vtypes.Unot(rdata) + 1)) abs_data = vtypes.Divide(abs_ldata, abs_rdata) data = (abs_data if not signed else vtypes.Mux( vtypes.Eq(lmsb, rmsb), abs_data, vtypes.Unot(abs_data) + 1)) if shift_size > 0: data = vtypes.Sll(data, shift_size) _FixedSkipUnaryOperator.__init__(self, data, point, signed)
def _binary_op_div(self, op, r): lvalue = self.value lpoint = self.point lsigned = self.signed if not isinstance(r, Fixed): rvalue = r rsigned = vtypes.get_signed(r) rpoint = 0 else: rvalue = r.value rsigned = r.signed rpoint = r.point point = _max_mux(lpoint, rpoint) signed = lsigned and rsigned lwidth = lvalue.bit_length() rwidth = rvalue.bit_length() ldata, rdata = adjust(lvalue, rvalue, lpoint, rpoint, signed) try: lmsb = ldata[lwidth - 1] except: lmsb = (ldata >> (lwidth - 1) & 0x1) try: rmsb = rdata[lwidth - 1] except: rmsb = (rdata >> (rwidth - 1) & 0x1) abs_ldata = (ldata if not lsigned else vtypes.Mux(lmsb == 0, ldata, vtypes.Unot(ldata) + 1)) abs_rdata = (rdata if not rsigned else vtypes.Mux(rmsb == 0, rdata, vtypes.Unot(rdata) + 1)) abs_data = op(abs_ldata, abs_rdata) data = (abs_data if not signed else vtypes.Mux(vtypes.Ors(vtypes.Ands(lmsb, rmsb), vtypes.Ands(vtypes.Not(lmsb), vtypes.Not(rmsb))), abs_data, vtypes.Unot(abs_data) + 1)) return Fixed(data, point, signed)
def visit_Unot(self, node): return vtypes.Unot(self.visit(node.right))