Advertisement
errant

Untitled

Apr 15th, 2012
1,945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. class local_update : public branch_update
  2. {
  3. public:
  4.         unsigned int index;
  5. };
  6.  
  7. class local_predictor : public branch_predictor
  8. {
  9. public:
  10. #define HISTORY_LENGTH  15
  11. #define TABLE_BITS  15
  12.     branch_info bi;
  13.     unsigned int history;
  14.     unsigned char tab[1<<TABLE_BITS];
  15.     local_update u;
  16.  
  17.     local_predictor (void) : history(0)
  18.     {
  19.         memset (tab, 0, sizeof (tab));
  20.     }
  21.  
  22.     branch_update *predict (branch_info & b)
  23.     {
  24.         bi = b;
  25.         if (b.br_flags & BR_CONDITIONAL)
  26.         {
  27.             u.index = (history << (TABLE_BITS - HISTORY_LENGTH)) ^ (b.address & ((1<<TABLE_BITS)-1));
  28.             u.direction_prediction (tab[u.index] >> 1);
  29.         }
  30.         else
  31.         {
  32.             u.direction_prediction (true);
  33.         }
  34.         u.target_prediction (0);
  35.         return &u;
  36.     }
  37.  
  38.     void update (branch_update *u, bool taken, unsigned int target)
  39.     {
  40.         if (bi.br_flags & BR_CONDITIONAL)
  41.         {
  42.             unsigned char *c = &tab[((local_update*)u)->index];
  43.             if (taken)
  44.             {
  45.                 if (*c < 3)
  46.                     (*c)++;
  47.             }
  48.             else
  49.             {
  50.                 if (*c > 0)
  51.                     (*c)--;
  52.             }
  53.  
  54.             history <<= 1;              //Bit shift _history_ to the left one to make room for the new result
  55.             history |= taken;           //Put the result (1 or 0) in the least significant bit of history (right bit)
  56.             history &= (1<<HISTORY_LENGTH)-1;  
  57.         }
  58.     }
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement