xref: /sqlite-3.40.0/src/hwtime.h (revision 5cd89cfe)
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 ** $Id: hwtime.h,v 1.3 2008/08/01 14:33:15 shane Exp $
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;
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 
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 
57 #elif (defined(__GNUC__) && defined(__ppc__))
58 
59   __inline__ sqlite_uint64 sqlite3Hwtime(void){
60       unsigned long long retval;
61       unsigned long junk;
62       __asm__ __volatile__ ("\n\
63           1:      mftbu   %1\n\
64                   mftb    %L0\n\
65                   mftbu   %0\n\
66                   cmpw    %0,%1\n\
67                   bne     1b"
68                   : "=r" (retval), "=r" (junk));
69       return retval;
70   }
71 
72 #else
73 
74   #error Need implementation of sqlite3Hwtime() for your platform.
75 
76   /*
77   ** To compile without implementing sqlite3Hwtime() for your platform,
78   ** you can remove the above #error and use the following
79   ** stub function.  You will lose timing support for many
80   ** of the debugging and testing utilities, but it should at
81   ** least compile and run.
82   */
83   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
84 
85 #endif
86 
87 #endif /* !defined(_HWTIME_H_) */
88