add linkly and unlikely optimisation to ltt_time_compare
authorcompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Sun, 15 Aug 2004 01:30:41 +0000 (01:30 +0000)
committercompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Sun, 15 Aug 2004 01:30:41 +0000 (01:30 +0000)
git-svn-id: http://ltt.polymtl.ca/svn@769 04897980-b3bd-0310-b5e0-8ef037075253

ltt/branches/poly/ltt/ltt-private.h
ltt/branches/poly/ltt/time.h
ltt/branches/poly/ltt/tracefile.c

index ae7cc7e8b5f47dc38686f122afed2d6ffa91a84c..ae10e90e82c9d10daf953ca67172001db99d82f7 100644 (file)
@@ -160,7 +160,8 @@ struct _LttTracefile{
   BlockEnd   * a_block_end;          //block end of the block
   void * cur_event_pos;              //the position of the current event
   void * buffer;                     //the buffer containing the block
-  double cycle_per_nsec;             //Cycles per nsec
+  double nsec_per_cycle;             //Nsec per cycle
+  //LttCycleCount cycles_per_nsec_reciprocal; // Optimisation for speed
   unsigned cur_heart_beat_number;    //current number of heart beat in the buf
   LttCycleCount cur_cycle_count;     //current cycle count of the event
   void * last_event_pos;
index 55fb5883c7f83bf8ad061e5469a1bf53a1f0033a..a257018275262b2dd2da7b87a5fc1e1598492a02 100644 (file)
@@ -62,15 +62,17 @@ static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
   return res;
 }
 
+#define likely(x) __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
 
 /* Fastest comparison : t1 > t2 */
 static inline int ltt_time_compare(LttTime t1, LttTime t2)
 {
   int ret=0;
-  if(t1.tv_sec > t2.tv_sec) ret = 1;
-  else if(t1.tv_sec < t2.tv_sec) ret = -1;
-  else if(t1.tv_nsec > t2.tv_nsec) ret = 1;
-  else if(t1.tv_nsec < t2.tv_nsec) ret = -1;
+  if(likely(t1.tv_sec > t2.tv_sec)) ret = 1;
+  else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1;
+  else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1;
+  else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1;
   
   return ret;
 }
index dd84a3966b36b4aa2edc4ffbdbcfee46fe96e37c..e5506c0deaaa307e5c9f5a415e1fd8e3fdff69e4 100644 (file)
@@ -1245,8 +1245,8 @@ int skipEvent(LttTracefile * t)
 void getCyclePerNsec(LttTracefile * t)
 {
   LttTime           lBufTotalTime; /* Total time for this buffer */
-  LttCycleCount     lBufTotalNSec; /* Total time for this buffer in nsecs */
-  LttCycleCount     lBufTotalCycle;/* Total cycles for this buffer */
+  double            lBufTotalNSec; /* Total time for this buffer in nsecs */
+  double            lBufTotalCycle;/* Total cycles for this buffer */
 
   /* Calculate the total time for this buffer */
   lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
@@ -1256,16 +1256,24 @@ void getCyclePerNsec(LttTracefile * t)
   lBufTotalCycle -= t->a_block_start->cycle_count;
 
   /* Convert the total time to nsecs */
-  lBufTotalNSec  = lBufTotalTime.tv_sec;
-  lBufTotalNSec *= NANOSECONDS_PER_SECOND; 
-  lBufTotalNSec += lBufTotalTime.tv_nsec;
+  lBufTotalNSec  = ltt_time_to_double(lBufTotalTime);
   
-  t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
+  t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
+  /* See : http://www.azillionmonkeys.com/qed/adiv.html */
+  // precalculate the reciprocal, so divisions will be really fast.
+  // 2^32-1 == 0xFFFFFFFFULL
+  //{
+  //  double int_res = lBufTotalCycle/lBufTotalNSec;
+  //  t->cycles_per_nsec_reciprocal = 
+  //            ((0xFFFF+int_res)/int_res);
+  //}
+
 }
 
 /****************************************************************************
  *Function name
  *    getEventTime    : obtain the time of an event 
+ *                      NOTE : this function _really_ is on critical path.
  *Input params 
  *    tf              : tracefile
  *Return value
@@ -1277,7 +1285,7 @@ static inline LttTime getEventTime(LttTracefile * tf)
   LttTime       time;
   LttCycleCount cycle_count;      // cycle count for the current event
   LttCycleCount lEventTotalCycle; // Total cycles from start for event
-  double        lEventNSec;       // Total nsecs from start for event
+  LttCycleCount lEventNSec;       // Total nsecs from start for event
   LttTime       lTimeOffset;      // Time offset in struct LttTime
   guint16       evId;
 
@@ -1310,8 +1318,9 @@ static inline LttTime getEventTime(LttTracefile * tf)
   lEventTotalCycle -= tf->a_block_start->cycle_count;
 
   // Convert it to nsecs
-  lEventNSec = (double)lEventTotalCycle / (double)tf->cycle_per_nsec;
-
+  lEventNSec = (double)lEventTotalCycle * (double)tf->nsec_per_cycle;
+  //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16;
+  
   // Determine offset in struct LttTime 
   lTimeOffset = ltt_time_from_double(lEventNSec);
 
This page took 0.026462 seconds and 4 git commands to generate.