Exemplo n.º 1
0
 private SFix(Signed value, int intWidth, int fracWidth)
 {
     Contract.Requires(intWidth + fracWidth >= 0);
     Debug.Assert(value.Size <= intWidth + fracWidth);
     _value  = value;
     _format = new FixFormat(true, intWidth, fracWidth);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Subtracts <paramref name="b"/> from <paramref name="a"/>.
        /// 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)
        {
            Signed    an, bn;
            FixFormat dfmt = Equalize(a, b, out an, out bn);
            Signed    rn   = an - bn;

            return(new SFix(rn, dfmt.IntWidth, dfmt.FracWidth));
        }
Exemplo n.º 3
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 UFix operator *(UFix a, UFix b)
        {
            FixFormat dfmt = new FixFormat(true,
                                           a.Format.IntWidth + b.Format.IntWidth,
                                           a.Format.FracWidth + b.Format.FracWidth);

            return(new UFix(a._value * b._value, dfmt.IntWidth, dfmt.FracWidth));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Subtracts <paramref name="b"/> from <paramref name="a"/>.
        /// The integer and fractional width of the result is determined according to the
        /// current arithmetic sizing mode (<seealso cref="FixedPointSettings"/>).
        /// </summary>
        public static UFix operator -(UFix a, UFix b)
        {
            Unsigned  an, bn;
            FixFormat dfmt = Equalize(a, b, out an, out bn);
            Unsigned  rn   = an - bn;

            return(new UFix(rn, dfmt.IntWidth, dfmt.FracWidth));
        }
Exemplo n.º 5
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.º 6
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)));
     }
 }
 private bool IsFix(TypeDescriptor inType, out FixFormat format)
 {
     if (inType.CILType.Equals(typeof(SFix)) ||
         inType.CILType.Equals(typeof(UFix)))
     {
         format = inType.GetFixFormat();
         return true;
     }
     format = null;
     return false;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Two fixed-point formats are defined to be equal iff the have the same signedness and integer/fractional widths match.
        /// </summary>
        public override bool Equals(object obj)
        {
            FixFormat other = (FixFormat)obj;

            if (other == null)
            {
                return(false);
            }
            return(IsSigned == other.IsSigned &&
                   IntWidth == other.IntWidth &&
                   FracWidth == other.FracWidth);
        }
Exemplo n.º 9
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 UFix operator *(UFix a, UFix b)
 {
     FixFormat dfmt = new FixFormat(true,
         a.Format.IntWidth + b.Format.IntWidth,
         a.Format.FracWidth + b.Format.FracWidth);
     return new UFix(a._value * b._value, dfmt.IntWidth, dfmt.FracWidth);
 }
Exemplo n.º 10
0
        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();
            }
        }
Exemplo n.º 11
0
 private UFix(Unsigned value, int intWidth, int fracWidth)
 {
     Contract.Requires(intWidth + fracWidth >= 0);
     Debug.Assert(value.Size <= intWidth + fracWidth);
     _value = value;
     _format = new FixFormat(false, intWidth, fracWidth);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="floatWidth">total bit-width of input floating-point number
        /// (actual partitioning between exponent and mantissa bits does not matter)</param>
        /// <param name="outFormat">desired fixed-point output format</param>
        public FloatSignAsSigned(int floatWidth, FixFormat outFormat)
        {
            FloatWidth = floatWidth;
            OutFormat = outFormat;

            _outM1 = SFix.FromDouble(-1.0, outFormat.IntWidth, outFormat.FracWidth).SLVValue;
            _out0 = SFix.FromDouble(0.0, outFormat.IntWidth, outFormat.FracWidth).SLVValue;
            _out1 = SFix.FromDouble(1.0, outFormat.IntWidth, outFormat.FracWidth).SLVValue;
            _zeros = StdLogicVector._0s(floatWidth - 1);

            TASite = new TransactionSite(this);
        }
Exemplo n.º 13
0
            public override TypeDescriptor MakeWireType(TypeDescriptor ctype)
            {
                FixFormat fmt = (FixFormat)ctype.TypeParams[0];

                return(TypeDescriptor.GetTypeOf(StdLogicVector._0s(fmt.TotalWidth)));
            }
Exemplo n.º 14
0
 /// <summary>
 /// Converts <paramref name="fmt"/> to a range repesentation in the way it is typically used in VHDL designs.
 /// </summary>
 public static Range ConvertToRange(FixFormat fmt)
 {
     return(new Range(fmt.IntWidth - 1, -fmt.FracWidth, EDimDirection.Downto));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Converts <paramref name="fmt"/> to a range repesentation in the way it is typically used in VHDL designs.
 /// </summary>
 public static Range ConvertToRange(FixFormat fmt)
 {
     return new Range(fmt.IntWidth - 1, -fmt.FracWidth, EDimDirection.Downto);
 }