private SFix(Signed value, int intWidth, int fracWidth) { Contract.Requires(intWidth + fracWidth >= 0); Debug.Assert(value.Size <= intWidth + fracWidth); _value = value; _format = new FixFormat(true, intWidth, fracWidth); }
public override object CreateInstance(ETypeCreationOptions options, object template) { if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) || options.HasFlag(ETypeCreationOptions.NonZero)) { if (template == null) { return(Signed.One); } else { var signed = (Signed)template; return(Signed.FromInt(1, signed.Size)); } } else { if (template == null) { return(Signed.Zero); } else { var signed = (Signed)template; return(Signed.FromInt(0, signed.Size)); } } }
/// <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 SFix operator -(SFix a, SFix b) { Signed an, bn; FixFormat dfmt = Equalize(a, b, out an, out bn); Signed rn = an - bn; return(new SFix(rn, dfmt.IntWidth, dfmt.FracWidth)); }
private static FixFormat Equalize(SFix a, SFix b, out Signed an, out Signed bn) { an = a._value; bn = b._value; FixFormat dfmt; switch (DesignContext.Instance.FixPoint.ArithSizingMode) { case EArithSizingMode.Safe: dfmt = new FixFormat(true, 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(true, 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 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 Equals(object obj) { if (obj is Signed) { Signed other = (Signed)obj; return((Size == other.Size) && object.Equals(_value, other._value)); } else { return(false); } }
/// <summary> /// Negates <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 SFix operator -(SFix a) { Signed an = a.SignedValue; Signed rn = -an; switch (DesignContext.Instance.FixPoint.ArithSizingMode) { case EArithSizingMode.InSizeIsOutSize: return(new SFix(rn, a._format.IntWidth, a._format.FracWidth)); case EArithSizingMode.Safe: case EArithSizingMode.VHDLCompliant: return(new SFix(rn, a._format.IntWidth + 1, a._format.FracWidth)); default: throw new NotImplementedException(); } }
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(SFix.One); } else { var sfix = (SFix)template; return(SFix.FromDouble(1.0, sfix.Format.IntWidth, sfix.Format.FracWidth)); } } else if (options.HasFlag(ETypeCreationOptions.NonZero)) { if (template == null) { return(SFix.One); } else { var sfix = (SFix)template; return(SFix.FromSigned(Signed.FromInt(1, sfix.Format.TotalWidth), sfix.Format.FracWidth)); } } else { if (template == null) { return(SFix.Zero); } else { var sfix = (SFix)template; return(SFix.FromDouble(0.0, sfix.Format.IntWidth, sfix.Format.FracWidth)); } } }
/// <summary> /// Converts <paramref name="src"/> to <paramref name="dstType"/> datatype, possibly with loss of precision or overflow. /// </summary> /// <remarks>Currently, conversions between all primitive numeric CIL types, enum types, and System#-intrinsic datatypes /// Signed, Unsigned, SFix, UFix and StdLogicVector are supported.</remarks> /// <exception cref="ArgumentNullException">if <paramref name="dstType"/> is null</exception> /// <exception cref="NotImplementedException">if there is no known conversion to <paramref name="dstType"/></exception> public static object ConvertSigned(Signed src, TypeDescriptor dstType) { Contract.Requires<ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(Signed))) return src.Resize(SFix.GetFormat(dstType).IntWidth); else if (dstType.CILType.Equals(typeof(SFix))) return SFix.FromSigned(src.Resize(SFix.GetFormat(dstType).TotalWidth), SFix.GetFormat(dstType).FracWidth); else return ConvertSigned(src, dstType.CILType); }
/// <summary> /// Converts <paramref name="src"/> to <paramref name="dstType"/> datatype, possibly with loss of precision or overflow. /// </summary> /// <remarks>Currently, conversions between all primitive numeric CIL types and enum types are supported.</remarks> /// <exception cref="ArgumentNullException">if <paramref name="dstType"/> is null</exception> /// <exception cref="NotImplementedException">if there is no known conversion to <paramref name="dstType"/></exception> public static object ConvertSigned(Signed src, Type dstType) { Contract.Requires<ArgumentNullException>(dstType != null, "dstType"); if (dstType.Equals(typeof(double))) return SFix.FromSigned(src, 0).DoubleValue; else return ConvertValue(src.LongValue, dstType); }
public StdLogicVector Serialize(object value) { Signed sval = (Signed)value; return(sval.SLVValue); }