public override object CreateInstance(ETypeCreationOptions options, object template) { if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) || options.HasFlag(ETypeCreationOptions.NonZero)) { if (template == null) { return(Unsigned.One); } else { var unsigned = (Unsigned)template; return(Unsigned.FromUInt(1, unsigned.Size)); } } else { if (template == null) { return(Unsigned.Zero); } else { var unsigned = (Unsigned)template; return(Unsigned.FromUInt(0, unsigned.Size)); } } }
private UFix(Unsigned value, int intWidth, int fracWidth) { Contract.Requires(intWidth + fracWidth >= 0); Debug.Assert(value.Size <= intWidth + fracWidth); _value = value; _format = new FixFormat(false, intWidth, fracWidth); }
/// <summary> /// Subtracts <paramref name="b"/> from <paramref name="a"/>. /// The integer and fractional width of the result is determined according to the /// current arithmetic sizing mode (<seealso cref="FixedPointSettings"/>). /// </summary> public static UFix operator -(UFix a, UFix b) { Unsigned an, bn; FixFormat dfmt = Equalize(a, b, out an, out bn); Unsigned rn = an - bn; return(new UFix(rn, dfmt.IntWidth, dfmt.FracWidth)); }
private static FixFormat Equalize(UFix a, UFix b, out Unsigned an, out Unsigned bn) { an = a._value; bn = b._value; FixFormat dfmt; switch (DesignContext.Instance.FixPoint.ArithSizingMode) { case EArithSizingMode.Safe: dfmt = new FixFormat(false, Math.Max(a.Format.IntWidth, b.Format.IntWidth) + 1, Math.Max(a.Format.FracWidth, b.Format.FracWidth)); an = an.Resize(dfmt.TotalWidth - 1); bn = bn.Resize(dfmt.TotalWidth - 1); if (a.Format.FracWidth > b.Format.FracWidth) { bn = bn << (int)(a.Format.FracWidth - b.Format.FracWidth); } else if (b.Format.FracWidth > a.Format.FracWidth) { an = an << (int)(b.Format.FracWidth - a.Format.FracWidth); } return(dfmt); case EArithSizingMode.VHDLCompliant: dfmt = new FixFormat(false, Math.Max(a.Format.IntWidth, b.Format.IntWidth) + 1, Math.Max(a.Format.FracWidth, b.Format.FracWidth)); an = an.Resize(dfmt.TotalWidth); bn = bn.Resize(dfmt.TotalWidth); if (a.Format.FracWidth > b.Format.FracWidth) { bn = bn << (int)(a.Format.FracWidth - b.Format.FracWidth); } else if (b.Format.FracWidth > a.Format.FracWidth) { an = an << (int)(b.Format.FracWidth - a.Format.FracWidth); } return(dfmt); case EArithSizingMode.InSizeIsOutSize: dfmt = a.Format; bn = bn.Resize(dfmt.TotalWidth); if (a.Format.FracWidth > b.Format.FracWidth) { bn = bn << (int)(a.Format.FracWidth - b.Format.FracWidth); } else if (b.Format.FracWidth > a.Format.FracWidth) { an = an << (int)(b.Format.FracWidth - a.Format.FracWidth); } return(dfmt); default: throw new NotImplementedException(); } }
public override TypeDescriptor MakeHardwareType(TypeDescriptor ctype) { if (_isSigned) { return(TypeDescriptor.GetTypeOf(Signed.FromInt(0, _size))); } else { return(TypeDescriptor.GetTypeOf(Unsigned.FromUInt(0, _size))); } }
public override bool Equals(object obj) { if (obj is Unsigned) { Unsigned other = (Unsigned)obj; return(Size == other.Size && object.Equals(_value, other._value)); } else { return(false); } }
public override object ConvertValueToHardwareType(object value) { if (_isSigned) { long longVal = (long)Convert.ChangeType(value, typeof(long)); return(Signed.FromLong(longVal, _size)); } else { ulong ulongVal = (ulong)Convert.ChangeType(value, typeof(ulong)); return(Unsigned.FromULong(ulongVal, _size)); } }
public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder) { if (args[0].Sample == null) { return(false); } ISized one, rsample; if (IsSigned) { Signed sample = (Signed)args[0].Sample; Signed sone = Signed.FromLong(1, (int)sample.Size); one = sone; rsample = sample + sone; } else { Unsigned sample = (Unsigned)args[0].Sample; Unsigned uone = Unsigned.FromULong(1, (int)sample.Size); one = uone; rsample = sample + uone; } LiteralReference oneLit = LiteralReference.CreateConstant(one); Expression inc; if (IsDecrement) { inc = args[0].Expr - oneLit; } else { inc = args[0].Expr + oneLit; } inc.ResultType = TypeDescriptor.GetTypeOf(rsample); inc = IntrinsicFunctions.Resize(inc, (int)one.Size, TypeDescriptor.GetTypeOf(one)); stack.Push(new StackElement(inc, one, Analysis.Msil.EVariability.ExternVariable)); return(true); }
public override object CreateInstance(ETypeCreationOptions options, object template) { if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral)) { if (template == null) { return(UFix.One); } else { var ufix = (UFix)template; return(UFix.FromDouble(1.0, ufix.Format.IntWidth, ufix.Format.FracWidth)); } } else if (options.HasFlag(ETypeCreationOptions.NonZero)) { if (template == null) { return(UFix.One); } else { var ufix = (UFix)template; return(UFix.FromUnsigned(Unsigned.FromUInt(1, ufix.Format.TotalWidth), ufix.Format.FracWidth)); } } else { if (template == null) { return(UFix.Zero); } else { var ufix = (UFix)template; return(UFix.FromDouble(0.0, ufix.Format.IntWidth, ufix.Format.FracWidth)); } } }
public StdLogicVector Serialize(object value) { Unsigned number = (Unsigned)value; return(number.SLVValue); }