Given this piece of code fragment: (10%)
for (int x = 0; x < 10; x++)
if ( x % 4 >= 2)
cout << "OK" << endl;
Assuming the branch prediction by default is “TAKEN”,
What is the accuracy of branch prediction of the if statement when we use a 1-bit branch history?
What is the accuracy of branch prediction of the if statement when we use a 2-bit branch history?
If we use 1 bit branch history then we will have 2 states and they are as below
Hence we have if ( x % 4 >= 2) and the values of x are from 0 to 9
for x=0 , 0 % 4 =0 and it is not >= 2 so branch wont take as the defualt prediction is taken
means we are the taken stage hence it will fail.
Now the prediction changed to branch not taken.
for x=1 , 1 % 4 =1 and it is not >= 2 so branch wont take as we are presently at
not taken stage hence it will pass.
Now the prediction wont change and it will be still at branch not taken.
for x=2 , 2 % 4 =2 and it is >= 2 so branch will take but we are presently at
not taken stage hence it will fail.
Now the prediction will change to branch taken.
for x=3 , 3 % 4 = 3 and it is >= 2 so branch will take and we are presently at
taken stage hence it will pass.
Now the prediction wont change and it will be at branch taken.
for x=4 , 4 % 4 = 0 and it is not >= 2 so branch wont take and we are presently at
taken stage hence it will fail.
Now the prediction will change to branch not taken.
for x=5 , 5 % 4 = 1 and it is not >= 2 so branch wont take and we are presently at
not taken stage hence it will pass.
Now the prediction wont change and it will be at branch not taken.
for x=6 , 6 % 4 = 2 and it is >= 2 so branch will take and we are presently at
not taken stage hence it will fail.
Now the prediction will change to branch taken.
for x=7 , 7 % 4 = 3 and it is >= 2 so branch will take and we are presently at
taken stage hence it will pass.
Now the prediction wont change and it will be at branch taken.
for x=8 , 8 % 4 = 0 and it is not >= 2 so branch wont take and we are presently at
taken stage hence it will fail.
Now the prediction will change and it will be branch not taken.
for x=9 , 9 % 4 = 1 and it is not >= 2 so branch wont take and we are presently at
not taken stage hence it will pass.
Now the prediction wont change and it will be at branch not taken.
Out of 10 cases 5 cases are passed hence our accuracy is 50%.
If we use 2 bit branch history then we will have 4 states and they are as below
Hence we have if ( x % 4 >= 2) and the values of x are from 0 to 9
for x=0 , 0 % 4 =0 and it is not >= 2 so branch wont take as the defualt prediction is taken
means we are the taken stage S1 hence it will fail.
Now the prediction changed to branch taken S2.
for x=1 , 1 % 4 =1 and it is not >= 2 so branch wont take as we are presently at
taken stage S2 hence it will fail.
Now the prediction will change to not taken S3.
for x=2 , 2 % 4 =2 and it is >= 2 so branch will take but we are presently at
not taken S3 stage hence it will fail.
Now the prediction will change to branch taken S4.
for x=3 , 3 % 4 = 3 and it is >= 2 so branch will take and we are presently at
taken S4 stage hence it will fail.
Now the prediction will change and it will be at branch taken S1.
for x=4 , 4 % 4 = 0 and it is not >= 2 so branch wont take and we are presently at
taken S1 stage hence it will fail.
Now the prediction will change to branch taken S2.
for x=5 , 5 % 4 = 1 and it is not >= 2 so branch wont take and we are presently at
taken S2 stage hence it will fail.
Now the prediction will change and it will be at branch not taken S3.
for x=6 , 6 % 4 = 2 and it is >= 2 so branch will take and we are presently at
not taken S3 stage hence it will fail.
Now the prediction will change to branch not taken S4.
for x=7 , 7 % 4 = 3 and it is >= 2 so branch will take and we are presently at
not taken S4 stage hence it will fail.
Now the prediction will change and it will be at branch taken S1.
for x=8 , 8 % 4 = 0 and it is not >= 2 so branch wont take and we are presently at
taken S1 stage hence it will fail.
Now the prediction will change and it will be branch taken S1.
for x=9 , 9 % 4 = 1 and it is not >= 2 so branch wont take and we are presently at
taken S2 stage hence it will fail.
Now the prediction wont change and it will be at branch not taken S3.
Out of 10 cases 0 cases are passed hence our accuracy is 0%.
Given this piece of code fragment: (10%) for (int x = 0; x < 10; x++)...
3) What are the final values of a, b, c in the following code fragment (1.5 point): int a = 6 , b = 127 , c; c = ( ++a ) + ( b -- ); Answer: 4) What are the final values of a, b, c in the following code fragment (1.5 point): int a = 6 , b = 127 , c; c = (a++) + ( -- b); Answer: 5) What is displayed by this poorly...
12. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout << x << " " << y << endl; cout << *p << " " << *q << endl; 13. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p...
Predictors Given the following piece of code below, compute the accuracies of the 1 bit and 2 bit predictors. Assume that the branch is taken if the "if" condition is met. The 1 bit predictor is initialized to 1 and the 2 bit predictor is initialized to 10. i-0; do1 if (i%2--?) { #Branch 1 if (i%2-1){ #Branch 2 }while (i<10000); #Branch 3 1 bit 2 bit Branch 1 Branch 2 Branch 3
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;
8. Given the following code fragment and function definition, what is(are) the output(s)? int funct (int n) int var = 1; while(n > 0) cout << funct(7)<<" "<<funct(0); var *= n; n--; return var;
31. The following code segment is syntactically correct: int number{20}; cout << number << setbase(16) << " " << number << setbase(10) << " " << number << showpos << " " << number << endl; T__ F__ 32. The following statement wants to determine if ‘count’ is outside the range of 0 through 100: if (count < 0 && count > 100) T__ F__ 33. There is...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
In the code given below, how many times the cout statement is executed? for (int x = 0; x< 10; x++) for (int y=0; y < 10; y++) for (int z = 0; z <=10; z++) cout << X+y+z;
What does the following code output? int x = 7, y = 10; X -= 30; y *= 5; X += 32; y /= 4; cout << "y = " « y << " and x = " << x << "; " << endl; X -= 10; y /= 5; cout << "x = " << x < " and y = " << y « ";" << endl;
12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude <iostream> using namespace std; double fudge (double s) f return s 2.3; int mainO cout >> "Your original estimate" double estimate; cin >> estimate; cout << endl; for (int 1 = 0;1c3;i++); cout << "EStimate' < fudge(estimate) <<endl Hint: There are 4 syntax errors. There is one error that is syntactically correct but causes the output to not be what you would expect. Once...