def fromPy(cls, val, typeObj, vldMask=None): """ Construct value from pythonic value (int, bytes, enum.Enum member) """ assert not isinstance(val, Value) if val is None: vld = 0 val = 0 assert vldMask is None or vldMask == 0 else: allMask = typeObj.all_mask() w = typeObj.bit_length() if isinstance(val, bytes): val = int.from_bytes(val, byteorder="little", signed=bool(typeObj.signed)) else: try: val = int(val) except TypeError as e: if isinstance(val, enum.Enum): val = int(val.value) else: raise e if vldMask is None: vld = allMask else: assert vldMask <= allMask and vldMask >= 0 vld = vldMask if val < 0: assert typeObj.signed assert signFix(val & allMask, w) == val, (val, signFix(val & allMask, w)) val = signFix(val & vld, w) else: if typeObj.signed: msb = 1 << (w - 1) if msb & val: assert val < 0, val if val & allMask != val: raise ValueError("Not enought bits to represent value", val, val & allMask) val = val & vld return cls(val, typeObj, vld)
def _invert__val(self): v = self.clone() v.val = ~v.val w = v._dtype.bit_length() v.val &= mask(w) if self._dtype.signed: v.val = signFix(v.val, w) return v
def _mul__val(self, other): # [TODO] resT should be wider resT = self._dtype v = self.val * other.val v &= resT.all_mask() if resT.signed: v = signFix(v, resT.bit_length()) result = resT.fromPy(v) if not self._isFullVld() or not other._isFullVld(): result.vldMask = 0 result.updateTime = max(self.updateTime, other.updateTime) return result