rdtsc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2008 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Read TimeStamp Counter (RDTSC)
  33. * http://www-unix.mcs.anl.gov/~kazutomo/rdtsc.html
  34. * I'm not really sure what the license is, but I'll assume Kazutomo Yoshii is
  35. * cool with me using it since he published it on his website.
  36. * Should also check out: http://www.fftw.org/cycle.h
  37. */
  38. #ifndef __RDTSC_H__
  39. #define __RDTSC_H__
  40. u_int64_t rdtsc_calibrate(u_int32_t mhz);
  41. #if defined(__i386__)
  42. #define HAVE_RDTSC 1
  43. static inline u_int64_t
  44. rdtsc(void)
  45. {
  46. u_int64_t x;
  47. __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  48. return x;
  49. }
  50. #elif defined(__x86_64__)
  51. #define HAVE_RDTSC 1
  52. static inline u_int64_t
  53. rdtsc(void)
  54. {
  55. unsigned hi, lo;
  56. __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  57. return ( (u_int64_t)lo)|( ((u_int64_t)hi)<<32 );
  58. }
  59. #elif defined(__powerpc__)
  60. #define HAVE_RDTSC 1
  61. static inline u_int64_t
  62. rdtsc(void)
  63. {
  64. u_int64_t result=0;
  65. u_int32_t upper, lower,tmp;
  66. __asm__ volatile(
  67. "0: \n"
  68. "\tmftbu %0 \n"
  69. "\tmftb %1 \n"
  70. "\tmftbu %2 \n"
  71. "\tcmpw %2,%0 \n"
  72. "\tbne 0b \n"
  73. : "=r"(upper),"=r"(lower),"=r"(tmp)
  74. );
  75. result = upper;
  76. result = result<<32;
  77. result = result|lower;
  78. return(result);
  79. }
  80. #else
  81. /* do not HAVE_RDTSC for your platform */
  82. #endif
  83. /* only define rdtsc_sleep() if we have rdtsc() */
  84. #ifdef HAVE_RDTSC
  85. /*
  86. * sleeps for sleep time, using the rdtsc counter for accuracy
  87. * you need to call rdtsc_calibrate() BEFORE this or you'll sleep for
  88. * an additional .1 sec the very first time you call it.
  89. */
  90. static inline void
  91. rdtsc_sleep(const struct timespec sleep)
  92. {
  93. u_int64_t sleep_until;
  94. u_int64_t now = 0;
  95. static u_int64_t clicks_per_usec = 0;
  96. sleep_until = rdtsc();
  97. clicks_per_usec = clicks_per_usec > 0 ? clicks_per_usec : rdtsc_calibrate(0);
  98. sleep_until += clicks_per_usec * TIMESPEC_TO_MICROSEC(&sleep);
  99. while (now < sleep_until)
  100. now = rdtsc();
  101. }
  102. #endif
  103. #endif /* __RDTSC_H__ */