Version: SMASH-3.1
tsc.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2009-2014 Matthias Kretz <kretz@kde.org>
4  * Copyright (c) 2014-2015,2017-2018,2020-2021,2023
5  * SMASH Team
6  *
7  * GNU General Public License (GPLv3 or later)
8  *
9  */
10 
11 #ifndef SRC_INCLUDE_SMASH_TSC_H_
12 #define SRC_INCLUDE_SMASH_TSC_H_
13 
14 #ifndef USE_NANOBENCHMARKING_CODE
15 #error "Nanobenchmark code tried to be used without enabling it via CMake."
16 #else
17 
18 #include <cstdint>
19 #include <iosfwd>
20 
21 #ifdef _MSC_VER
22 #include <intrin.h>
23 #pragma intrinsic(__rdtsc)
24 #endif
25 
26 namespace smash {
27 
34 class TimeStampCounter {
35  public:
37  void start();
38 
40  void stop();
41 
43  uint64_t cycles() const;
44 
45  private:
47  union Data {
49  uint64_t a;
51  unsigned int b[2];
52  };
54  Data m_start;
56  Data m_end;
57 };
58 
59 inline void TimeStampCounter::start() {
60 #ifdef VC_IMPL_MIC
61  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
62  : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ebx", "ecx");
63 #elif defined _MSC_VER
64  unsigned int tmp;
65  m_start.a = __rdtscp(&tmp);
66 #else
67  asm volatile("rdtscp" : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ecx");
68 #endif
69 }
70 
71 inline void TimeStampCounter::stop() {
72 #ifdef VC_IMPL_MIC
73  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
74  : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ebx", "ecx");
75 #elif defined _MSC_VER
76  unsigned int tmp;
77  m_end.a = __rdtscp(&tmp);
78 #else
79  asm volatile("rdtscp" : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ecx");
80 #endif
81 }
82 
83 inline uint64_t TimeStampCounter::cycles() const { return m_end.a - m_start.a; }
84 
85 std::ostream &operator<<(std::ostream &out, const TimeStampCounter &tsc);
86 
87 } // namespace smash
88 
89 #endif // USE_NANOBENCHMARKING_CODE
90 #endif // SRC_INCLUDE_SMASH_TSC_H_
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:547
Definition: action.h:24