RenHao

2-bit branch predictor

Dec 20th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*  For 2-bit Counter */
  5. #define k 3
  6. #define entry pow(2,k)  //   entry = 2^k
  7.  
  8. /*   Parameters   */
  9. //2 bits
  10. #define Strongly_t  3
  11. #define Weakly_t    2
  12. #define Weakly_nt   1
  13. #define Strongly_nt 0
  14. //1 bits
  15. #define Taken    1
  16. #define NonTaken 0
  17.  
  18. /*      Declare Variables      */
  19. int Branch_Prediction_Buffer[(int)entry];
  20. int Hit = 0 , Total = 0;       //   Record for success of conditional branch
  21. int mapping = 0;
  22.  
  23. /*   Functions    */
  24. unsigned long Hex2Dec(char *ch)
  25. {
  26.     int i,j=0, temp = 0;
  27.     unsigned long   sum = 0 ;
  28.     if( ch[1] == 10 )   return 0;
  29.     for(i=7 ; i>=0 ; i--)
  30.     {
  31.             temp = ( ch[i] >= '0' && ch[i] <= '9' )? ch[i] - '0' : ( ch[i] >= 'a' && ch[i] <= 'e')? (ch[i] - 'a') +10 : 0;
  32.             sum += temp << (j++*4);
  33.     }
  34.     return sum;
  35. }
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39.   char now_buf[20] = {0} , next_buf[20] = {0} , type_buf[10] = {0} ;
  40.   int type ;
  41.   int i =1, PCnow = 0, PCnext = 1;
  42.   int Evaluation = 0 , Predict ;
  43.   float Prediction_Rate;
  44.  
  45.   /*  Initialize Buffer  */
  46.   int j;
  47.   for(j=0;j<entry;j++)   Branch_Prediction_Buffer[j] = Weakly_nt;
  48.  
  49.   /* Open files */
  50.   FILE *Now_ins, *Next_ins, *Ins_type;
  51.   Now_ins = fopen("Path_pc_now.txt","r");
  52.   Next_ins = fopen("Path_pc_next.txt","r");
  53.   Ins_type = fopen("Path_type.txt","r");   // 1 is for conditional branch , SKIP if type is not 1
  54.  
  55.   /*    Main loop   */
  56.   while(1)
  57.   {
  58.       /*    Read data   */
  59.       fgets(now_buf , 20 , Now_ins);
  60.       fgets(type_buf , 10 , Ins_type);
  61.       fgets(next_buf , 20 , Next_ins);
  62.      
  63.       /*  Convert to dec  */
  64.       type = atoi(type_buf);
  65.       PCnow = Hex2Dec(now_buf);
  66.       PCnext = Hex2Dec(next_buf);
  67.      
  68.       /*  Last data  */
  69.       if(PCnext == 0)      break;
  70.      
  71.       /*  Prediction  */
  72.        if(type != 1)    ;
  73.        else
  74.        {
  75.               Evaluation = (PCnext - PCnow == 4)? NonTaken : Taken;
  76.               mapping = (PCnow>>2) % (1<<k);
  77.               Total++;
  78.              
  79.               Predict = Branch_Prediction_Buffer[mapping] >> 1;
  80.               printf("i=%3d state=%d mapping=%d pc:%d p:%d e:%d\n",i,Branch_Prediction_Buffer[mapping],mapping,PCnow,Predict,Evaluation);
  81.               //printf("pc:%d p:%d e:%d\n",PCnow,Predict,Evaluation);
  82.               if( Predict == Evaluation )
  83.               {
  84.                   Hit++;
  85.                   if( Predict == Taken )  Branch_Prediction_Buffer[mapping]++;
  86.                   else                    Branch_Prediction_Buffer[mapping]--;
  87.               }
  88.               else
  89.               {
  90.                   if( Predict == Taken )  Branch_Prediction_Buffer[mapping]--;
  91.                   else                    Branch_Prediction_Buffer[mapping]++;
  92.               }
  93.              
  94.               if( Branch_Prediction_Buffer[mapping] > Strongly_t )        Branch_Prediction_Buffer[mapping] = Strongly_t;
  95.               else if( Branch_Prediction_Buffer[mapping] < Strongly_nt )  Branch_Prediction_Buffer[mapping] = Strongly_nt;
  96.        }
  97.        i++;
  98.        
  99.   }
  100.   printf("Hit:%d Missed:%d  Prediction Rate:%.2f%%\n",Hit,Total-Hit,(float)Hit/Total*100);
  101.  
  102.   /*  Close files  */
  103.   fclose(Now_ins);
  104.   fclose(Next_ins);
  105.   fclose(Ins_type);
  106.   system("PAUSE"); 
  107.   return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment