hwtime.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. ** 2008 May 27
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. ******************************************************************************
  12. **
  13. ** This file contains inline asm code for retrieving "high-performance"
  14. ** counters for x86 class CPUs.
  15. */
  16. #ifndef _HWTIME_H_
  17. #define _HWTIME_H_
  18. /*
  19. ** The following routine only works on pentium-class (or newer) processors.
  20. ** It uses the RDTSC opcode to read the cycle count value out of the
  21. ** processor and returns that value. This can be used for high-res
  22. ** profiling.
  23. */
  24. #if (defined(__GNUC__) || defined(_MSC_VER)) && \
  25. (defined(i386) || defined(__i386__) || defined(_M_IX86))
  26. #if defined(__GNUC__)
  27. __inline__ sqlite_uint64 sqlite3Hwtime(void){
  28. unsigned int lo, hi;
  29. __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  30. return (sqlite_uint64)hi << 32 | lo;
  31. }
  32. #elif defined(_MSC_VER)
  33. __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
  34. __asm {
  35. rdtsc
  36. ret ; return value at EDX:EAX
  37. }
  38. }
  39. #endif
  40. #elif (defined(__GNUC__) && defined(__x86_64__))
  41. __inline__ sqlite_uint64 sqlite3Hwtime(void){
  42. unsigned long val;
  43. __asm__ __volatile__ ("rdtsc" : "=A" (val));
  44. return val;
  45. }
  46. #elif (defined(__GNUC__) && defined(__ppc__))
  47. __inline__ sqlite_uint64 sqlite3Hwtime(void){
  48. unsigned long long retval;
  49. unsigned long junk;
  50. __asm__ __volatile__ ("\n\
  51. 1: mftbu %1\n\
  52. mftb %L0\n\
  53. mftbu %0\n\
  54. cmpw %0,%1\n\
  55. bne 1b"
  56. : "=r" (retval), "=r" (junk));
  57. return retval;
  58. }
  59. #else
  60. #error Need implementation of sqlite3Hwtime() for your platform.
  61. /*
  62. ** To compile without implementing sqlite3Hwtime() for your platform,
  63. ** you can remove the above #error and use the following
  64. ** stub function. You will lose timing support for many
  65. ** of the debugging and testing utilities, but it should at
  66. ** least compile and run.
  67. */
  68. sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
  69. #endif
  70. #endif /* !defined(_HWTIME_H_) */