This preview shows pages 1–3. Sign up to view the full content.
Homework 2
Prof. Milos Prvulovic
CS 4290/6290  Spring 2009
Handed Out: 3 Feb 2009 (Tue)
Due: 10 Feb 2009 (Tue)
This homework counts as 5% of the overall class grade. Submit your answers in TSquare as a
single PDF document. You can either handwrite your answers and scan them into a PDF
document, or write your answers in a text processor (e.g. LaTeX) and generate a PDF document
from that (e.g. pdflatex). No printed submissions will be accepted. In case you handwrite your
answers, keep in mind that illegible answers will be considered to be wrong answers.
1.
Consider the following MIPS code. Note that R0 (register 0) in the MIPS ISA always has
a value of zero. Also note that MIPS instructions always start at wordaligned addresses,
so the two least significant bits of the instruction’s address are always zero. As a result,
the two least significant bits are never used to index branch predictor tables. Instead,
these bits are ignored and the index for the predictor consists of the least significant
remaining bits when indexing into a branch predictor table.
0x100
li
R2,0
; v=0
0x104
li
R3,100
; Loop bound for LoopI
0x108
li
R4,0
; i=0
LoopI:
0x10C
beq
R4,R3,EndLoopI ; Exit LoopI if i==100
0x110
li
R5,0
; j=0
LoopJ:
0x114
beq
R5,R3,EndLoopJ ; Exit LoopJ if j==100
0x118
add
R6,R5,R4
; j+i
0x11C
andi R6,R6,1
; (j+i)%2
0x120
bne
R6,R0,EndIf
; Skip if (j+1)%2!=0
0x124
add R2,R2,R5
; v+=j
EndIf:
0x128
addi R5,R5,1
; j++
0x12C
beq
R0,R0,LoopJ
; Go back to LoopJ
EndLoopJ:
0x130
addi R4,R4,1
; i++
0x134
beq
R0,R0,LoopI
; Go back to LoopI
EndLoopI:
For this code, determine the
exact
number of branch mispredictions that occur during its
execution (show your work), if we use:
a)
An alwaystaken predictor.
This code can be written in C as follows:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documentv=0;
for(i=0;i!=100;i++)
for(j=0;j!=100;j++)
if((j+i)%2)==0)
v+=j;
The branch at 0x10C is executed 101 times. It is not taken 100 times (in each iteration
of the outer loop) and taken once (to exit of the outer loop). Correspondingly, the
branch at 0x134 is taken 100 times (at the end of each iteration of the outer loop).
The branch at 0x114 is executed 101 times in each of the 100 iterations of the outer
loop. It is NT 100 times, then T once, then NT 100 times, then T once, etc. Overall,
this branch is not taken 10,000 times and taken 100 times. Correspondingly, the
branch at 0x12C is taken 10,000 times (at the end of each iteration of the inner loop).
The branch at 0x120 is NT when (j+i) is an even number and taken when (i+j) is an
odd number. When loop counter i is even, j takes values from 0 to 99 and this branch
has a pattern of (NT,T)
50
(NT, then T, repeat 50 times). When I is odd, this branch has
a pattern of (T,NT)
50
. Because i takes values from 0 to 99, the overall pattern of this
branch is ((NT,T)
50
(T,NT)
50
)
50
. Overall, this branch executes 10,000 times and is
taken 5,000 times and not taken 5,000.
In summary, the number of mispredictions with the alwaystaken predictor is 15,100
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 Staff

Click to edit the document details