Version: SMASH-2.2
tsc.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009-2014 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 SRC_INCLUDE_SMASH_TSC_H_
22 #define SRC_INCLUDE_SMASH_TSC_H_
23 
24 #ifndef USE_NANOBENCHMARKING_CODE
25 #error "Nanobenchmark code tried to be used without enabling it via CMake."
26 #else
27 
28 #include <cstdint>
29 #include <iosfwd>
30 
31 #ifdef _MSC_VER
32 #include <intrin.h>
33 #pragma intrinsic(__rdtsc)
34 #endif
35 
36 namespace smash {
37 
44 class TimeStampCounter {
45  public:
47  void start();
48 
50  void stop();
51 
53  uint64_t cycles() const;
54 
55  private:
57  union Data {
59  uint64_t a;
61  unsigned int b[2];
62  };
64  Data m_start;
66  Data m_end;
67 };
68 
69 inline void TimeStampCounter::start() {
70 #ifdef VC_IMPL_MIC
71  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
72  : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ebx", "ecx");
73 #elif defined _MSC_VER
74  unsigned int tmp;
75  m_start.a = __rdtscp(&tmp);
76 #else
77  asm volatile("rdtscp" : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ecx");
78 #endif
79 }
80 
81 inline void TimeStampCounter::stop() {
82 #ifdef VC_IMPL_MIC
83  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
84  : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ebx", "ecx");
85 #elif defined _MSC_VER
86  unsigned int tmp;
87  m_end.a = __rdtscp(&tmp);
88 #else
89  asm volatile("rdtscp" : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ecx");
90 #endif
91 }
92 
93 inline uint64_t TimeStampCounter::cycles() const { return m_end.a - m_start.a; }
94 
95 std::ostream &operator<<(std::ostream &out, const TimeStampCounter &tsc);
96 
97 } // namespace smash
98 
99 #endif // USE_NANOBENCHMARKING_CODE
100 #endif // SRC_INCLUDE_SMASH_TSC_H_
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:532
Definition: action.h:24