Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

branch.cpp

Go to the documentation of this file.
00001 /*
00002         Copyright (C) 1997 CFU Corporation
00003 
00004         File: branch.cpp
00005         Description: branch predictors
00006         Author: Chao-yinig Fu
00007         Date: Oct. 26, 2000
00008 
00009         ^..^
00010         (00)
00011 
00012          $__$
00013         <~00~>
00014          (oo)
00015           ||
00016 
00017         ```````
00018         C ^|^ D
00019          \ O /
00020 */
00021 #include <iostream.h>
00022 #include <stdio.h>
00023 #include "branch.h"
00024 
00025 //
00026 // Return which slot will be taken
00027 //      0, fall through
00028 //      1, first slot
00029 //      2, second slot
00030 //      .....
00031 void Branch_Predictor::predict(int bhr,int ip,int &direction)
00032 {
00033         int index=ip>>_b;       // take out the _b bits
00034         //index=((index&_ip_mask)^(bhr<<_l))&_index_mask;
00035         //index=((index)^(bhr<<_l))&_index_mask;
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         if(ip==6676)
00053         {
00054                 fprintf(stderr,"ZZZ predict ip=%d index=%d slot=%d\n",ip,index,direction);
00055         }
00056         */
00057 }
00058 
00059 //
00060 // Parameter
00061 // ip => the ip of the op
00062 // actual_slot_taken => 0, if fall through
00063 //                      1, the first slot is taken
00064 //                      2, the second slot is taken
00065 //                      ...
00066 //
00067 void Branch_Predictor::update(int bhr,int ip,int actual_slot_taken)
00068 {
00069         //if(ip!=5552)return;
00070 
00071         assert(actual_slot_taken<_num_of_slot);
00072 
00073         int index=ip>>_b;       // take out the _b bits
00074         //index=index&_index_mask;
00075         //index=((index&_ip_mask)^(bhr<<_l))&_index_mask;
00076         //index=((index)^(bhr<<_l))&_index_mask;
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         //fprintf(stderr,"old strong=%d slot=%d\n",strong,slot);
00092 
00093         if(slot==actual_slot_taken)     // correct prediction
00094         {
00095                 if(strong==0)
00096                 {
00097                         _counter[index]=counter_value|_strong_mask;
00098                 }
00099         }
00100         else    // incorrect prediction
00101         {
00102 
00103                 if(strong==1)
00104                 {
00105                         // change strong to weak but with same slot
00106                         _counter[index]=slot;
00107                 }
00108                 else
00109                 {       // change to actual_slot_taken
00110                         _counter[index]=actual_slot_taken;
00111                 }
00112 
00113                 //fprintf(stderr,"update ip%d index=%d actual_slot=%d old_counter=%d new_counter=%d\n",ip,index,actual_slot_taken,counter_value,_counter[index]);
00114         }
00115 
00116         /*
00117         if(ip==6676)
00118         {
00119                 fprintf(stderr,"ZZZ update ip%d index=%d actual_slot=%d new_counter=%d\n",ip,index,actual_slot_taken,_counter[index]);
00120         }
00121         */
00122 }

Generated on Mon Jul 21 20:24:01 2003 for TINKER LEGO DOC by doxygen 1.3.2