我如何使用if和if else语句在c ++中实现1位分支预测?

时间:2019-04-18 11:26:24

标签: c++ branch branch-prediction

我正在为我的计算机体系结构模块做一个项目,该项目的实现相当模糊。我只是不确定如何在c ++中实现分支预测算法

这是我到目前为止尝试过的:

int random(int& branchNum); //passed by reference to update the global variable when random function is called

int main()
{
    //branch prediction

    //global variable as per request from document
    int myBranchNumber;

    cout << "Enter a branch number from 0 to 100" << endl;
    cin << myBranchNumber;

    //10 different executable branches

    if (myBranchNumber >= 90 && myBranchNumber <= 100) {
        cout << "Scored an A+ of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 80 && myBranchNumber >= 89) {
        cout << "Scored an A- of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 70 && myBranchNumber <= 79) {
        cout << "Scored an B+ of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 60 && myBranchNumber <= 69) {
        cout << "Scored an B+ of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 50 && myBranchNumber <= 59) {
        cout << "Scored an D of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 40 && myBranchNumber <= 49) {

        cout << "Scored an E of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 30 && myBranchNumber <= 39) {
        cout << "Scored an F of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 20 && myBranchNumber <= 29) {
        cout << "Scored an G of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 10 && myBranchNumber <= 19) {
        cout << "Scored an H of " << myBranchNumber << endl;
    }

    else if (myBranchNumber >= 0 && myBranchNumber <= 9) {
        cout << "Scored an I of " << myBranchNumber << endl;
    }

    //4 random numbers in a reasonable range and range selected is 0-100
    random(myBranchNumber);

    cout << "random number is : " << random(myBranchNumber) << endl;
    cout << "my branch number is now:" << myBranchNumber;
    //function keep track of time taken to complete N runs
    return 0;
}

//function for randomising
int random(int& branchNum)
{
    srand(time(NULL)); //srand is declared once only and we using randomising

    branchNum = (rand() % 100) + 1;
    return branchNum;
}

问题是:

使用您选择的任何高级编程语言,开发一个程序来测试“分支预测”技术的工作,该技术在计算机工程中用于提高性能。在开发此程序时,建议您考虑以下可能性:

[1]编写一个具有至少十个不同的可执行分支的主程序。执行的分支应由全局变量的值(例如myBranchNumber)确定。

[2]对于上面[1]中建议的十个分支,请确保它们具有可比较的计算。

[3]除了[1]中的十个分支外,主程序还应至少具有三十(30)个顺序执行的指令。

[4]编写一个函数,该函数会在合理范围内生成随机数,并按照[1]中的建议更新全局变量。

[5]应该有一个函数来跟踪完成该程序的N次运行所花费的时间。此功能应将预测和已执行分支的记录以及完成N次运行的时间存储在文件中。

0 个答案:

没有答案