Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CycleCounterTSC.hh
1 #ifndef _CycleCounter_hh_64825420763_
2 #define _CycleCounter_hh_64825420763_
3 
4 
5 
6 namespace BIAS {
7 
8 
9 
10  /** @brief processor cycles Time Stamp Counter on >= Pentium like CPU's.
11 
12  High resolution CPU timing information based on x86 assembly rdtsc command.
13  Works for single core machines.
14  Please note that the cycle counter may be unreliable
15  -with speed step etc. on notebooks
16  -with multicore/multiprocessor systems as the cyclec ounters may not be in sync
17  and the schneduler may move thread execution between processing kernels.
18 
19 
20  Windows:
21  Assembler function without prolog and epilog.
22  Should work on Intel Pentium and newer AMD CPU's, see rdtsc assembler instruction.
23  Return the number of clock cycles since the CPU was powered up or reset.
24  This functions can't be class member inlined because it collides with naked fastcall modifier.
25  int64 has no ANSI equivalent.
26 
27  Linux:
28  Same approach avoiding int64 usage.
29 
30  See http://en.wikipedia.org/wiki/RDTSC
31 
32  \todo: For x64 see http://www3.intel.com/cd/ids/developer/asmo-na/eng/257129.htm
33 
34  @author Felix Woelk(Linux), Jan Woetzel(Windows) */
35 
36 #ifdef WIN32
37 #ifdef _WIN64
38  __forceinline __int64 __fastcall GetTSC(){ return __rdtsc(); };
39 #else
40  __forceinline __int64 __fastcall GetTSC(){ __asm rdtsc };
41 #endif
42 
43  /** @brief compute the difference between two int64 cycle counts using 12 bit overflow mask .
44  @author Jan Woetzel 12/2004 */
45  __forceinline __int64
46  GetTSCTicks(__int64 start, __int64 end)
47  {
48  return ((end - start) & 0xFFFFFFFFFFFF);
49  };
50 
51 #else// WIN32
52 
53 
54  inline
55  void GetTSC_hilo(unsigned int *hi, unsigned int *lo)
56  {
57 #if !defined(WIN32) && defined(__i386__)
58 
59  asm("rdtsc; movl %%edx,%0; movl %%eax,%1" /* Read cycle counter */
60  : "=r" (*hi), "=r" (*lo) /* and move results to */
61  : /* No input */ /* the two outputs */
62  : "%edx", "%eax");
63 #else
64  *hi = *lo = 0; // dummy implementation
65 #endif
66  }
67 
68 #endif // WIN32
69 
70 } // namespace
71 
72 
73 #endif
void GetTSC_hilo(unsigned int *hi, unsigned int *lo)
processor cycles Time Stamp Counter on >= Pentium like CPU's.