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(); } }
/// <summary> /// Converts the format to a SysDOM type descriptor. /// </summary> public static TypeDescriptor ToType(this FixFormat fmt) { if (fmt.IsSigned) { return(TypeDescriptor.GetTypeOf(SFix.FromDouble(0.0, fmt.IntWidth, fmt.FracWidth))); } else { return(TypeDescriptor.GetTypeOf(UFix.FromDouble(0.0, fmt.IntWidth, fmt.FracWidth))); } }
/// <summary> /// Extracts the fixed-point format from a SysDOM type descriptor, given that it actually describes a fixed-point number type. /// </summary> public static FixFormat GetFixFormat(this TypeDescriptor td) { if (td.CILType.Equals(typeof(Signed)) || td.CILType.Equals(typeof(SFix))) { return(SFix.GetFormat(td)); } else if (td.CILType.Equals(typeof(Unsigned)) || td.CILType.Equals(typeof(UFix))) { return(UFix.GetFormat(td)); } else { return(null); } }
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)); } } }
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(); } }
/// <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 ConvertUFix(UFix src, TypeDescriptor dstType) { Contract.Requires<ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(UFix))) return src.Resize(UFix.GetFormat(dstType).IntWidth, UFix.GetFormat(dstType).FracWidth); else if (dstType.CILType.Equals(typeof(Unsigned))) return src.UnsignedValue.Resize(UFix.GetFormat(dstType).IntWidth); else if (dstType.CILType.Equals(typeof(StdLogicVector))) return src.SLVValue[StdLogicVector.GetLength(dstType) - 1, 0]; else return ConvertUFix(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 ConvertUFix(UFix src, Type dstType) { Contract.Requires<ArgumentNullException>(dstType != null, "dstType"); if (dstType.Equals(typeof(double))) return src.DoubleValue; else if (dstType.Equals(typeof(sbyte))) return (sbyte)src.SFixValue.Resize(8, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(byte))) return (byte)src.Resize(8, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(short))) return (short)src.SFixValue.Resize(16, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(ushort))) return (byte)src.Resize(16, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(int))) return (int)src.SFixValue.Resize(32, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(uint))) return (uint)src.Resize(32, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(long))) return src.SFixValue.Resize(64, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(ulong))) return src.Resize(64, 0).UnsignedValue.ULongValue; else throw new NotImplementedException(); }
/// <summary> /// Constructs a new instance /// </summary> /// <param name="lutWidth">resolution of data table</param> /// <param name="xFracWidth">fractional width of operand</param> /// <param name="yFracWidth">fractional width of result</param> /// <param name="pipeStages">additional pipeline stages for interpolation computation</param> public SinCosLUTCore(int lutWidth, int xFracWidth, int yFracWidth, int pipeStages) { PipeStages = pipeStages; XIntWidth = 2; XFracWidth = xFracWidth; YIntWidth = 2; YFracWidth = yFracWidth; DIntWidth = 2; DFracWidth = yFracWidth; LUTWidth = lutWidth; _x = new Signal<UFix>() { InitialValue = UFix.FromDouble(0.0, LUTWidth + 1, XFracWidth - LUTWidth - 1) }; _xq = new Signal<UFix>() { InitialValue = UFix.FromDouble(0.0, LUTWidth + 1, XFracWidth - LUTWidth - 1) }; _sinRaw = new Signal<SFix>() { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth) }; _cosRaw = new Signal<SFix>() { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth) }; _sinIn = new SLVSignal(YIntWidth + YFracWidth) { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth).SLVValue }; _cosIn = new SLVSignal(YIntWidth + YFracWidth) { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth).SLVValue }; _sinOut = new SLVSignal(YIntWidth + YFracWidth) { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth).SLVValue }; _cosOut = new SLVSignal(YIntWidth + YFracWidth) { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth).SLVValue }; AddrWidth = lutWidth + 1; _sinAddr = new Signal<Unsigned>() { InitialValue = Unsigned.FromUInt(0, AddrWidth) }; _cosAddr = new Signal<Unsigned>() { InitialValue = Unsigned.FromUInt(0, AddrWidth) }; _sinData = new Signal<SFix>() { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth) }; _cosData = new Signal<SFix>() { InitialValue = SFix.FromDouble(0.0, YIntWidth, YFracWidth) }; _sinLUT = new VSignal<SFix>((1 << lutWidth) + 2, _ => new Signal<SFix>() { InitialValue = SFix.FromDouble(Math.Sin(Math.PI * 0.5 * _ / (double)(1 << lutWidth)), 2, yFracWidth) }); _sinFlipSignIn = new SLVSignal(1) { InitialValue = "0" }; _cosFlipSignIn = new SLVSignal(1) { InitialValue = "0" }; _sinFlipSignOut = new SLVSignal(1) { InitialValue = "0" }; _cosFlipSignOut = new SLVSignal(1) { InitialValue = "0" }; _mirror = UFix.FromUnsigned(Unsigned.One.Resize(XFracWidth + 2) << (xFracWidth + 1), xFracWidth - LUTWidth); _mirror2 = UFix.FromUnsigned(Unsigned.One.Resize(XFracWidth + 2) << xFracWidth, xFracWidth - LUTWidth); _sinPipe = new RegPipe(pipeStages, YIntWidth + YFracWidth); Bind(() => { _sinPipe.Clk = Clk; _sinPipe.Din = _sinIn; _sinPipe.Dout = _sinOut; }); _cosPipe = new RegPipe(pipeStages, YIntWidth + YFracWidth); Bind(() => { _cosPipe.Clk = Clk; _cosPipe.Din = _cosIn; _cosPipe.Dout = _cosOut; }); _sinFlipSignPipe = new RegPipe(2, 1); Bind(() => { _sinFlipSignPipe.Clk = Clk; _sinFlipSignPipe.Din = _sinFlipSignIn; _sinFlipSignPipe.Dout = _sinFlipSignOut; }); _cosFlipSignPipe = new RegPipe(2, 1); Bind(() => { _cosFlipSignPipe.Clk = Clk; _cosFlipSignPipe.Din = _cosFlipSignIn; _cosFlipSignPipe.Dout = _cosFlipSignOut; }); _sinUnit = new LERPUnit(lutWidth + 1, xFracWidth - 1 - lutWidth, YIntWidth, yFracWidth, 0); Bind(() => { _sinUnit.Clk = Clk; _sinUnit.X = _x; _sinUnit.Y = _sinRaw; _sinUnit.Addr = _sinAddr; _sinUnit.Data = _sinData; }); _cosUnit = new LERPUnit(lutWidth + 1, xFracWidth - 1 - lutWidth, YIntWidth, yFracWidth, 0); Bind(() => { _cosUnit.Clk = Clk; _cosUnit.X = _xq; _cosUnit.Y = _cosRaw; _cosUnit.Addr = _cosAddr; _cosUnit.Data = _cosData; }); TASite = new TransactionSite(this); }
public object Deserialize(StdLogicVector slv, TypeDescriptor targetType) { var fmt = UFix.GetFormat(targetType); return(UFix.FromUnsigned(slv.UnsignedValue, fmt.FracWidth)); }
public StdLogicVector Serialize(object value) { UFix ufix = (UFix)value; return(ufix.UnsignedValue.SLVValue); }
public override object CorrectArgument(object arg) { var org = (UFix)arg; return(UFix.FromDouble(1.0, org.Format.IntWidth, org.Format.FracWidth)); }