/// <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 ConvertLong(long src, TypeDescriptor dstType) { Contract.Requires <ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(Signed))) { return(Signed.FromLong(src, SFix.GetFormat(dstType).IntWidth)); } else if (dstType.CILType.Equals(typeof(Unsigned))) { return(Unsigned.FromBigInt(new System.Numerics.BigInteger(src), UFix.GetFormat(dstType).IntWidth)); } else if (dstType.CILType.Equals(typeof(SFix))) { return(SFix.FromSigned(Signed.FromLong(src, SFix.GetFormat(dstType).IntWidth), SFix.GetFormat(dstType).FracWidth)); } else if (dstType.CILType.Equals(typeof(StdLogicVector))) { return(StdLogicVector.FromLong(src, StdLogicVector.GetLength(dstType))); } else { return(ConvertLong(src, dstType.CILType)); } }
/// <summary> /// Converts a double value to its binary encoding, given a floating point format. /// </summary> /// <param name="value">The value to be encoded</param> /// <param name="fmt">The floating point format to be assumed</param> /// <returns>The binary encoding</returns> public static StdLogicVector ToSLV(this double value, FloatFormat fmt) { StdLogicVector sign; StdLogicVector exponent; StdLogicVector mantissa; if (double.IsInfinity(value)) { sign = double.IsNegativeInfinity(value) ? (StdLogicVector)"1" : (StdLogicVector)"0"; exponent = StdLogicVector._1s(fmt.ExponentWidth); mantissa = StdLogicVector._0s(fmt.FractionWidth); } else if (double.IsNaN(value)) { sign = (StdLogicVector)"0"; exponent = StdLogicVector._1s(fmt.ExponentWidth); mantissa = StdLogicVector._1s(fmt.FractionWidth); } else { sign = value < 0.0 ? (StdLogicVector)"1" : (StdLogicVector)"0"; double absvalue = Math.Abs(value); int exp = 0; while (absvalue >= 2.0) { absvalue *= 0.5; exp++; } while (absvalue > 0.0 && absvalue < 1.0) { absvalue *= 2.0; exp--; } if (absvalue == 0.0) { return(StdLogicVector._0s(fmt.TotalWidth)); } else if (exp <= -fmt.Bias) { // denomalized exponent = StdLogicVector._0s(fmt.ExponentWidth); absvalue *= (double)(1L << (fmt.FractionWidth + 1)); long mant = (long)absvalue; mantissa = StdLogicVector.FromLong(mant, fmt.FractionWidth); } else { absvalue -= 1.0; absvalue *= (double)(1L << fmt.FractionWidth); long mant = (long)absvalue; mantissa = StdLogicVector.FromLong(mant, fmt.FractionWidth); exponent = StdLogicVector.FromLong(exp + fmt.Bias, fmt.ExponentWidth); } } return(sign.Concat(exponent.Concat(mantissa))); }
private void MakeVectors(int value1, int width1, int value2, int width2, out StdLogicVector r1, out StdLogicVector r2) { StdLogicVector v1 = StdLogicVector.FromLong(value1, width1); StdLogicVector v2 = StdLogicVector.FromLong(value2, width2); StdLogicVector v = v1.Concat(v2).Concat(StdLogicVector._0s(2 * MaxWidth - width1 - width2)); r1 = v[2 * MaxWidth - 1, MaxWidth]; r2 = v[MaxWidth - 1, 0]; }
private async void TestProcess() { await Tick; int curNumber = 0x12345678; do { _syncIn.Next = '1'; StdLogicVector next = StdLogicVector.FromLong(curNumber, Design.DataWidth); _parIn.Next = next; Console.WriteLine(DesignContext.Instance.CurTime + ": input number " + next.IntValue); await Tick; _syncIn.Next = '0'; while (!_syncOut.Cur) { await Tick; } Console.WriteLine(DesignContext.Instance.CurTime + ": output number " + _parOut.Cur.IntValue); curNumber *= 2; } while (true); }