tsc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* This file is part of the Vc library. {{{
  2. Copyright © 2009-2017 Matthias Kretz <kretz@kde.org>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the names of contributing organizations nor the
  11. names of its contributors may be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  17. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. }}}*/
  24. #ifndef VC_TSC_H_
  25. #define VC_TSC_H_
  26. #ifdef _MSC_VER
  27. #include <intrin.h>
  28. #pragma intrinsic(__rdtsc)
  29. #endif
  30. #include <cmath>
  31. #include <iomanip>
  32. #include <iostream>
  33. #include <sstream>
  34. class TimeStampCounter
  35. {
  36. public:
  37. void start();
  38. void stop();
  39. unsigned long long cycles() const;
  40. private:
  41. union Data {
  42. unsigned long long a;
  43. unsigned int b[2];
  44. } m_start, m_end;
  45. };
  46. inline void TimeStampCounter::start()
  47. {
  48. #if defined __MIC__
  49. asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc" : "=a"(m_start.b[0]), "=d"(m_start.b[1]) :: "ebx", "ecx" );
  50. #elif defined _MSC_VER
  51. unsigned int tmp;
  52. m_start.a = __rdtscp(&tmp);
  53. #elif defined __x86_64__ || defined __i386__
  54. asm volatile("rdtscp" : "=a"(m_start.b[0]), "=d"(m_start.b[1]) :: "ecx" );
  55. #else
  56. m_start = {};
  57. #endif
  58. }
  59. inline void TimeStampCounter::stop()
  60. {
  61. #if defined __MIC__
  62. asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc" : "=a"(m_end.b[0]), "=d"(m_end.b[1]) :: "ebx", "ecx" );
  63. #elif defined _MSC_VER
  64. unsigned int tmp;
  65. m_end.a = __rdtscp(&tmp);
  66. #elif defined __x86_64__ || defined __i386__
  67. asm volatile("rdtscp" : "=a"(m_end.b[0]), "=d"(m_end.b[1]) :: "ecx" );
  68. #else
  69. m_end = {};
  70. #endif
  71. }
  72. inline unsigned long long TimeStampCounter::cycles() const
  73. {
  74. return m_end.a - m_start.a;
  75. }
  76. inline std::ostream &operator<<(std::ostream &out, const TimeStampCounter &tsc)
  77. {
  78. std::ostringstream o;
  79. auto c = tsc.cycles();
  80. int blocks[10];
  81. int n = 0;
  82. for (int digits = std::ceil(std::log10(c)); digits > 0; digits -= 3) {
  83. blocks[n++] = c % 1000;
  84. c /= 1000;
  85. }
  86. if (n == 0) {
  87. return out;
  88. }
  89. o.fill('0');
  90. o << blocks[--n];
  91. while (n > 0) {
  92. o << '\'' << std::setw(3) << blocks[--n];
  93. }
  94. return out << o.str();
  95. }
  96. #endif // VC_TSC_H_
  97. // vim: foldmethod=marker