RenHao

1-bit branch predictor

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