/// <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(); }