Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Minimal Branch Predictor

By: errant on Apr 17th, 2012  |  syntax: C++  |  size: 0.91 KB  |  hits: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class local_update : public branch_update
  2. {
  3. public:
  4.         unsigned int counter;
  5. };
  6.  
  7. class local_predictor : public branch_predictor
  8. {
  9. public:
  10.         local_update u;
  11.         branch_info binfo;
  12.        
  13.  
  14.         local_predictor (void)
  15.         {
  16.        
  17.         }
  18.  
  19.         branch_update *predict (branch_info & b)
  20.         {
  21.                 binfo = b;
  22.                 if (b.br_flags & BR_CONDITIONAL)
  23.                         {                              
  24.                                 u.direction_prediction ((u.counter) >> 1);             
  25.                         }
  26.                         else u.direction_prediction(true);                     
  27.                         u.target_prediction (0);
  28.                         return &u;
  29.         }
  30.  
  31.         void update (branch_update *u, bool taken, unsigned int target)
  32.         {
  33.  
  34.                 unsigned int *c = &((local_update*)u)->counter;
  35.                 if (binfo.br_flags & BR_CONDITIONAL)
  36.                         {
  37.  
  38.                         if (taken)
  39.                                 {
  40.                                         if (*c < 3)
  41.                                                 (*c)++;
  42.                                 }
  43.                                 else
  44.                                 {
  45.                                         if (*c > 0)
  46.                                                 (*c)--;
  47.                                 }
  48.                         }
  49.         }
  50.  
  51. };