两位分支预测应该给出更高的百分比

时间:2013-04-04 01:34:43

标签: branch-prediction

我有一个小程序,应该计算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%

0 个答案:

没有答案