shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 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 | ** |
shane | b08c1d0 | 2008-06-12 02:24:38 +0000 | [diff] [blame] | 16 | ** $Id: hwtime.h,v 1.2 2008/06/12 02:24:39 shane Exp $ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 17 | */ |
| 18 | #ifndef _HWTIME_H_ |
| 19 | #define _HWTIME_H_ |
| 20 | |
| 21 | /* |
| 22 | ** The following routine only works on pentium-class (or newer) processors. |
| 23 | ** It uses the RDTSC opcode to read the cycle count value out of the |
| 24 | ** processor and returns that value. This can be used for high-res |
| 25 | ** profiling. |
| 26 | */ |
| 27 | #if (defined(__GNUC__) || defined(_MSC_VER)) && \ |
| 28 | (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
| 29 | |
| 30 | #if defined(__GNUC__) |
| 31 | |
| 32 | __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 33 | unsigned int lo, hi; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 34 | __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 35 | return (sqlite_uint64)hi << 32 | lo; |
| 36 | } |
| 37 | |
| 38 | #elif defined(_MSC_VER) |
| 39 | |
| 40 | __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ |
| 41 | __asm { |
| 42 | rdtsc |
| 43 | ret ; return value at EDX:EAX |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #endif |
| 48 | |
shane | b08c1d0 | 2008-06-12 02:24:38 +0000 | [diff] [blame] | 49 | #elif (defined(__GNUC__) && defined(__x86_64__)) |
| 50 | |
| 51 | __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 52 | unsigned long val; |
| 53 | __asm__ __volatile__ ("rdtsc" : "=A" (val)); |
| 54 | return val; |
| 55 | } |
| 56 | |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 57 | #else |
| 58 | |
| 59 | #error Need implementation of sqlite3Hwtime() for your platform. |
| 60 | |
| 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 | |
| 70 | #endif |
| 71 | |
| 72 | #endif /* !defined(_HWTIME_H_) */ |