0

I am doing a project for my computer architecture module and the project implementation is rather vague. Im just not sure how to implement a branch prediction algorithm in c++

This is what i have attempted so far:

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;
}

The Question is:

Using any high-level programming language of your choice, develop a programme to test the working of “Branch prediction” technique, which is used in Computer Engineering to improve performance. In developing this program, it is suggested that you consider the following possibilities:

[1] Write a main program that has at least ten different executable branches. The executed branch should be determined by a value of a global variable, say, myBranchNumber.

[2] For the ten branches suggested in [1] above, ensure that they have comparable computations.

[3] Apart from the ten branches in [1], the main program should have at least thirty (30) sequentially executed instructions.

[4] Write a function that generates random numbers within a reasonable range and update the global variable as suggested in [1].

[5] There should be a function which keeps track of the time taken to complete N runs of the program. This function should store in a file the record of predicted and executed branches as well as the time to complete the N runs.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    You are not trying to implement branch prediction algorithm nor it is requested by your teacher; it is suggested to implement something to measure effect of branch prediction. – Jean-Baptiste Yunès Apr 18 '19 at 11:32
  • 1
    See [Why is it faster to process a sorted array than an unsorted array?](https://stackoverflow.com/q/11227809/8769985) for an example of a program showing big improvements by branch prediction – francesco Apr 18 '19 at 11:51

0 Answers0