00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <iostream.h>
00022 #include <stdio.h>
00023 #include "branch.h"
00024
00025
00026
00027
00028
00029
00030
00031 void Branch_Predictor::predict(int bhr,int ip,int &direction)
00032 {
00033 int index=ip>>_b;
00034
00035
00036
00037 index=index&_index_mask;
00038
00039 if(_l>0)
00040 {
00041 bhr=bhr&_low_bhr_mask;
00042 index=index ^ (bhr<<(_m-_l));
00043 }
00044
00045 assert(index<_num_of_entry);
00046
00047 int counter_value=_counter[index];
00048 direction=counter_value & _slot_mask;
00049 assert(direction<_num_of_slot);
00050
00051
00052
00053
00054
00055
00056
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void Branch_Predictor::update(int bhr,int ip,int actual_slot_taken)
00068 {
00069
00070
00071 assert(actual_slot_taken<_num_of_slot);
00072
00073 int index=ip>>_b;
00074
00075
00076
00077
00078 index=index&_index_mask;
00079 if(_l>0)
00080 {
00081 bhr=bhr&_low_bhr_mask;
00082 index=index ^ (bhr<<(_m-_l));
00083 }
00084
00085 assert(index<_num_of_entry);
00086
00087 int counter_value=_counter[index];
00088 int strong=counter_value>>_num_of_slot_bits;
00089 int slot=counter_value & _slot_mask;
00090
00091
00092
00093 if(slot==actual_slot_taken)
00094 {
00095 if(strong==0)
00096 {
00097 _counter[index]=counter_value|_strong_mask;
00098 }
00099 }
00100 else
00101 {
00102
00103 if(strong==1)
00104 {
00105
00106 _counter[index]=slot;
00107 }
00108 else
00109 {
00110 _counter[index]=actual_slot_taken;
00111 }
00112
00113
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 }