StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tsc.h
1 /*
2  Copyright (C) 2009 Matthias Kretz <kretz@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) version 3.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 
19 */
20 
21 #ifndef TSC_H
22 #define TSC_H
23 
24 #ifdef _MSC_VER
25 #include <intrin.h>
26 #pragma intrinsic(__rdtsc)
27 #endif
28 
30 {
31  public:
32  void Start();
33  void Stop();
34  unsigned long long Cycles() const;
35 
36  private:
37  union Data {
38  unsigned long long a;
39  unsigned int b[2];
40  } start, end;
41 };
42 
43 inline void TimeStampCounter::Start()
44 {
45 #ifdef _MSC_VER
46  start.a = __rdtsc();
47 #else
48  asm volatile("rdtsc" : "=a"(start.b[0]), "=d"(start.b[1]) );
49 #endif
50 }
51 
52 inline void TimeStampCounter::Stop()
53 {
54 #ifdef _MSC_VER
55  end.a = __rdtsc();
56 #else
57  asm volatile("rdtsc" : "=a"(end.b[0]), "=d"(end.b[1]) );
58 #endif
59 }
60 
61 inline unsigned long long TimeStampCounter::Cycles() const
62 {
63  return end.a - start.a;
64 }
65 
66 #endif // TSC_H