Repository navigation
Expand file tree
/
Copy pathbp.c
More file actions
88 lines (70 loc) · 2.04 KB
/
Copy pathbp.c
File metadata and controls
88 lines (70 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* EECE 476
* Edward Han 62932082
* Nick Adams 61406088
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "tracebp.h"
int main(int argc, char* argv[])
{
int num_taken = 0;
int num_not_taken = 0;
int num_mispredictions = 0;
int bp_phtsize = 131072/2;
int bp_history[3];
int bp_pht[bp_phtsize];
memset( bp_pht, 0, sizeof(bp_pht) ); // initialize to 0
memset( bp_history, 0, sizeof(bp_history) ); // initialize to 0
// open the trace file
char trace_filename[MAX_STR];
if (argc > 1) {
sscanf(argv[1], "%s", trace_filename);
} else {
printf("Usage: %s <trace_file>\n", argv[0]);
return 1;
}
initInstructionStream( trace_filename );
instruction prev_instruction;
prev_instruction.op[0] = 0;
instruction current_instruction;
while( getNextInstruction( ¤t_instruction ) ) {
// Was the prev instruction a conditional branch?
// Each instruction in this architecture is 8 bytes long
if( isBranch(prev_instruction.op) ) {
unsigned index = ((unsigned)(long)current_instruction.pc >> 3) & ((1<<13)-1);
assert(index < bp_phtsize/8);
index = index + bp_phtsize/8 * (4 * bp_history[0] + 2 * bp_history[1] + bp_history[2]);
int prediction = bp_pht[ index ];
// compute actual branch outcome
int br_taken = (current_instruction.pc != prev_instruction.pc + 8);
if( br_taken )
++num_taken;
else
++num_not_taken;
bp_history[0] = bp_history[1];
bp_history[1] = bp_history[2];
//bp_history[2] = bp_history[3];
if((prediction > 1) != br_taken ) {
num_mispredictions++;
}
bp_history[2] = br_taken;
if (br_taken) {
if (bp_pht[index] < 3)
bp_pht[index]++;
} else {
if (bp_pht[index] > 0)
bp_pht[index]--;
}
}
prev_instruction = current_instruction;
}
closeInstructionStream();
// OFFICIAL PRINT STATEMENTS FOR AUTO-MARKING
// DO NOT PRINT ANYTHING ELSE BEFORE THESE LINES!!
// DO NOT REMOVE THESE LINES!!!
printBP( num_taken, num_not_taken, num_mispredictions );
// Print your own information and statistics after this line
return 0;
}