aboutsummaryrefslogtreecommitdiff
path: root/external/glim/TscTimer.hpp
blob: ce22c7cb108d2fe74a59cda4eed30851c4bffdde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef _TSC_TIMER_H
#define _TSC_TIMER_H

namespace glim {

extern "C" {  // http://en.wikipedia.org/wiki/Rdtsc
#if (defined(__GNUC__) || defined(__ICC)) && defined(__i386__)
 static __inline__ unsigned long long rdTsc(void) {
  unsigned long long ret;
  __asm__ __volatile__("rdtsc": "=A" (ret));
  return ret;
 }
#elif (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C)) && defined(__x86_64__)
 static __inline__ unsigned long long rdTsc(void) {
  unsigned a, d;
  asm volatile("rdtsc" : "=a" (a), "=d" (d));
  return ((unsigned long long)a) | (((unsigned long long)d) << 32);
 }
#endif
}

//! CPU cycles timer. Fast, not safe.
//! cf. http://en.wikipedia.org/wiki/Rdtsc
struct TscTimer {
  int64_t start;
  TscTimer (): start (rdTsc()) {}
  int64_t operator()() const {return rdTsc() - start;}
};

}

#endif // _TSC_TIMER_H