Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
 /// <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)));
     }
 }
Exemplo n.º 3
0
 /// <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);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Multiplies <paramref name="a"/> and <paramref name="b"/>.
        /// 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)
        {
            var tmp = new SFix(a._value * b._value,
                               a.Format.IntWidth + b.Format.IntWidth,
                               a.Format.FracWidth + b.Format.FracWidth);

            switch (DesignContext.Instance.FixPoint.ArithSizingMode)
            {
            case EArithSizingMode.Safe:
            case EArithSizingMode.VHDLCompliant:
                return(tmp);

            case EArithSizingMode.InSizeIsOutSize:
                return(tmp.Resize(a.Format.IntWidth, a.Format.FracWidth));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 5
0
 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));
         }
     }
 }
Exemplo n.º 6
0
        public object Deserialize(StdLogicVector slv, TypeDescriptor targetType)
        {
            var fmt = SFix.GetFormat(targetType);

            return(SFix.FromSigned(slv.SignedValue, fmt.FracWidth));
        }
Exemplo n.º 7
0
        public StdLogicVector Serialize(object value)
        {
            SFix sfix = (SFix)value;

            return(sfix.SignedValue.SLVValue);
        }
Exemplo n.º 8
0
        public override object CorrectArgument(object arg)
        {
            var org = (SFix)arg;

            return(SFix.FromDouble(1.0, org.Format.IntWidth, org.Format.FracWidth));
        }