/// <summary> /// Converts a binary encoding, given as an StdLogicVector with respect to a floating point format to its /// double representation. /// </summary> /// <param name="slv">The binary encoding</param> /// <param name="fmt">The floating point format to be assumed</param> /// <returns>The double representation</returns> public static double ToFloat(this StdLogicVector slv, FloatFormat fmt) { if (slv.Size != fmt.TotalWidth) { throw new ArgumentException("Vector does not match specified floating point format"); } slv = slv.ProperValue; StdLogicVector mantissa = slv[fmt.FractionWidth - 1, 0]; StdLogicVector exponent = slv[fmt.FractionWidth + fmt.ExponentWidth - 1, fmt.FractionWidth]; StdLogic sign = slv[fmt.FractionWidth + fmt.ExponentWidth]; int exp = (int)exponent.ULongValue - fmt.Bias; if (exponent.Equals(StdLogicVector._0s(fmt.ExponentWidth))) { // denormalized long mant = mantissa.LongValue; double result = (double)mant * Math.Pow(2.0, exp - 1); return(result); } else if (exponent.Equals(StdLogicVector._1s(fmt.ExponentWidth))) { // Infinity / NaN if (mantissa.Equals(StdLogicVector._0s(fmt.FractionWidth))) { // infinity if (sign == '1') { return(double.NegativeInfinity); } else { return(double.PositiveInfinity); } } else { // NaN return(double.NaN); } } else { // normalized StdLogicVector number = StdLogicVector._1s(1).Concat(mantissa); ulong mant = number.ULongValue; double result = (double)mant * Math.Pow(2.0, exp - fmt.FractionWidth); if (sign == '1') { result = -result; } return(result); } }