1

我有一个小程序,它应该计算 2 位分支预测器的成功预测百分比。我已经完成了所有工作,但我的输出不是我所期望的,百分比停止在 91% 左右,而不是我认为应该是 98% 或 99%。我认为问题可能在于如何将掩码应用于地址。有人可以查看我的代码并验证这是否是问题所在。

该程序遍历一个文件,该文件具有 gcc 编译器运行的分支历史记录,该历史记录由大约 1792 个地址和一个数字列组成,其中 1 表示已采用分支,0 表示未采用分支。

static void twoBitPredictor_v1(StreamWriter sw)
    {
        uint hxZero = 0x000000000;
        uint uMask1 = 0x00000000;
        int nCorrectPrediction = 0;
        uint uSize2;
        int nSize;
        int nTotalReads;
        int nTableMin = 2;
        int nTableMax = 16;
        int nTaken = 0;
        uint[] uArrBt1;

        sw.WriteLine("\n\nTwo-Bit Predictor Results Ver. 1\n");
        sw.WriteLine("-------------------------\n");
        sw.WriteLine("Total" + "\t" + "Correct");
        sw.WriteLine("Reads" + "\t" + "Prediction" + "\t" + "Percentage");
        System.Console.WriteLine("\n\nTwo-Bit Predictor Results Ver. 1\n");
        System.Console.WriteLine("-------------------------\n");
        System.Console.WriteLine("Total" + "\t" + "Correct");
        System.Console.WriteLine("Reads" + "\t" + "Prediction" + "\t" + "Percentage");

        for (int _i = nTableMin; _i <= nTableMax; _i++)
        {
            StreamReader sr2 = new StreamReader(@"C:\Temp\gccHist.txt");
            nSize = _i;
            uSize2 = (uint)(Math.Pow(2, nSize));
            uArrBt1 = new uint[2 * uSize2];

            for (int i = 0; i < uSize2; i++)
                uArrBt1[i] = hxZero;

            nCorrectPrediction = 0;
            nTotalReads = 0;

            while (!sr2.EndOfStream)
            {
                String[] strLineRead = sr2.ReadLine().Split(',');
                uint uBRAddress = Convert.ToUInt32(strLineRead[0], 16);
                uint bBranchTaken = Convert.ToUInt32(strLineRead[2]);

   >>>>>   In the line below is where I think lies the problem but not sure how to correct it.
                uMask1 = uBRAddress & (0xffffffff >> 32 - nSize);
                int _mask = Convert.ToInt32(uMask1);
                nTaken = Convert.ToInt32(uArrBt1[2 * _mask]);

                switch (Convert.ToInt32(uArrBt1[_mask]))
                {
                    case 0:

                        if (bBranchTaken == 0) // Branch Not Taken
                            nCorrectPrediction++;
                        else
                            uArrBt1[_mask] = 1;

                        break;

                    case 1:

                        if (bBranchTaken == 0)
                        {
                            uArrBt1[_mask] = 0;
                            nCorrectPrediction++;
                        }
                        else
                            uArrBt1[_mask] = 3;

                        break;

                    case 2:

                        if (bBranchTaken == 0)
                        {
                            uArrBt1[_mask] = 3;
                            nCorrectPrediction++;
                        }
                        else
                            uArrBt1[_mask] = 0;

                        break;

                    case 3:

                        if (bBranchTaken == 0)
                            uArrBt1[_mask] = 2;
                        else                                
                            nCorrectPrediction++;

                        break;                    
                }
                nTotalReads++;
            }
            sr2.Close();
            double percentage = ((double)nCorrectPrediction / (double)nTotalReads) * 100;
            sw.WriteLine(nTotalReads + "\t" + nCorrectPrediction + "\t\t" + Math.Round(percentage, 2) + "%");
            System.Console.WriteLine(nTotalReads + "\t" + nCorrectPrediction + "\t\t" + Math.Round(percentage, 2) + "%");
        }
    }

这是输出:

Two-Bit Predictor Results Ver. 1
-------------------------

Total   Correct
Reads   Prediction      Percentage
1792    997             55.64%
1792    997             55.64%
1792    1520            84.82%
1792    1522            84.93%
1792    1521            84.88%
1792    1639            91.46%
1792    1651            92.13%
1792    1649            92.02%
1792    1649            92.02%
1792    1648            91.96%
1792    1646            91.85%
1792    1646            91.85%
1792    1646            91.85%
1792    1646            91.85%
1792    1646            91.85%
4

0 回答 0