/// <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 ConvertSFix(SFix 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.Resize(8, 0).SignedValue.LongValue); } else if (dstType.Equals(typeof(byte))) { return((byte)src.UFixValue.Resize(8, 0).UnsignedValue.ULongValue); } else if (dstType.Equals(typeof(short))) { return((short)src.Resize(16, 0).SignedValue.LongValue); } else if (dstType.Equals(typeof(ushort))) { return((byte)src.UFixValue.Resize(16, 0).UnsignedValue.ULongValue); } else if (dstType.Equals(typeof(int))) { return((int)src.Resize(32, 0).SignedValue.LongValue); } else if (dstType.Equals(typeof(uint))) { return((uint)src.UFixValue.Resize(32, 0).UnsignedValue.ULongValue); } else if (dstType.Equals(typeof(long))) { return(src.Resize(64, 0).SignedValue.LongValue); } else if (dstType.Equals(typeof(ulong))) { return(src.UFixValue.Resize(64, 0).UnsignedValue.ULongValue); } else { 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 ConvertSFix(SFix src, TypeDescriptor dstType) { Contract.Requires <ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(SFix))) { return(src.Resize(SFix.GetFormat(dstType).IntWidth, SFix.GetFormat(dstType).FracWidth)); } else if (dstType.CILType.Equals(typeof(Signed))) { return(src.SignedValue.Resize(SFix.GetFormat(dstType).IntWidth)); } else if (dstType.CILType.Equals(typeof(StdLogicVector))) { return(src.SLVValue[StdLogicVector.GetLength(dstType) - 1, 0]); } else { return(ConvertSFix(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, 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 ConvertSFix(SFix src, TypeDescriptor dstType) { Contract.Requires<ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(SFix))) return src.Resize(SFix.GetFormat(dstType).IntWidth, SFix.GetFormat(dstType).FracWidth); else if (dstType.CILType.Equals(typeof(Signed))) return src.SignedValue.Resize(SFix.GetFormat(dstType).IntWidth); else if (dstType.CILType.Equals(typeof(StdLogicVector))) return src.SLVValue[StdLogicVector.GetLength(dstType) - 1, 0]; else return ConvertSFix(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 ConvertSFix(SFix 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.Resize(8, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(byte))) return (byte)src.UFixValue.Resize(8, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(short))) return (short)src.Resize(16, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(ushort))) return (byte)src.UFixValue.Resize(16, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(int))) return (int)src.Resize(32, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(uint))) return (uint)src.UFixValue.Resize(32, 0).UnsignedValue.ULongValue; else if (dstType.Equals(typeof(long))) return src.Resize(64, 0).SignedValue.LongValue; else if (dstType.Equals(typeof(ulong))) return src.UFixValue.Resize(64, 0).UnsignedValue.ULongValue; else throw new NotImplementedException(); }