private static IntX Trim(IntX value, int size) { IntX rem = new IntX(1) << size; switch (DesignContext.Instance.FixPoint.OverflowMode) { case EOverflowMode.Wrap: value = IntX.Modulo(value, rem, DivideMode.AutoNewton); if (value < 0) { value += rem; } break; case EOverflowMode.Saturate: if (value > rem) { value = rem; } else if (value < 0) { value = 0; } break; case EOverflowMode.Fail: if (value > rem || value < 0) { throw new ArgumentException(); } break; } return(value); }
private static IntX Trim(IntX value, int size) { if (value == null) { return(null); } if (size == 0) { if (value != 0 && DesignContext.Instance.FixPoint.OverflowMode == EOverflowMode.Fail) { throw new ArgumentException(); } return(0); } IntX rem = new IntX(1) << (int)size; IntX rem2 = rem >> 1; switch (DesignContext.Instance.FixPoint.OverflowMode) { case EOverflowMode.Wrap: value = IntX.Modulo(value, rem, DivideMode.AutoNewton); if (value >= rem2) { value -= rem; } else if (value < -rem2) { value += rem; } return(value); case EOverflowMode.Fail: if (value > rem2 - 1 || value < -rem2) { throw new ArgumentException(); } return(value); case EOverflowMode.Saturate: if (value > rem2 - 1) { return(rem2 - 1); } else if (value < -rem2) { return(-rem2); } else { return(value); } default: throw new NotImplementedException(); } }
public Unsigned Resize(int newWidth) { if (newWidth < Size) { IntX one = new IntX(1); IntX rem = (one << newWidth); IntX newValue = IntX.Modulo(_value, rem, DivideMode.AutoNewton); return(new Unsigned(newValue, newWidth)); } else { return(new Unsigned(_value, newWidth)); } }