src/os_timer.cpp
changeset 5835 e0ff603ae0b7
parent 5726 8f399788f6c9
child 6285 187e3ef04cc9
equal deleted inserted replaced
5834:7bf92d5a5a0f 5835:e0ff603ae0b7
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 
       
     5 #undef RDTSC_AVAILABLE
       
     6 
       
     7 /* rdtsc for MSC_VER, uses simple inline assembly, or _rdtsc
       
     8  * from external win64.asm because VS2005 does not support inline assembly */
       
     9 #if defined(_MSC_VER) && !defined(RDTSC_AVAILABLE)
       
    10 # if defined (_M_AMD64)
       
    11 extern uint64 _rdtsc(void);
       
    12 #	else
       
    13 uint64 _declspec(naked) _rdtsc(void)
       
    14 {
       
    15 	_asm {
       
    16 		rdtsc
       
    17 		ret
       
    18 	}
       
    19 }
       
    20 # endif
       
    21 # define RDTSC_AVAILABLE
       
    22 #endif
       
    23 
       
    24 /* rdtsc for OS/2. Hopefully this works, who knows */
       
    25 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
       
    26 unsigned __int64 _rdtsc( void);
       
    27 # pragma aux _rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
       
    28 # define RDTSC_AVAILABLE
       
    29 #endif
       
    30 
       
    31 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
       
    32 #if defined(__i386__) || defined(__x86_64__) && !defined(RDTSC_AVAILABLE)
       
    33 uint64 _rdtsc(void)
       
    34 {
       
    35 	uint32 high, low;
       
    36 	__asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
       
    37 	return ((uint64)high << 32) | low;
       
    38 }
       
    39 # define RDTSC_AVAILABLE
       
    40 #endif
       
    41 
       
    42 /* rdtsc for PPC which has this not */
       
    43 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
       
    44 uint64 _rdtsc(void)
       
    45 {
       
    46 	uint32 high = 0, high2 = 0, low;
       
    47 	/* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
       
    48 	 * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
       
    49 	 * very unlikely event that the lower part overflows to the upper part while we
       
    50 	 * read it; we double-check and reread the registers */
       
    51 	asm volatile (
       
    52 				  "mftbu %0\n"
       
    53 				  "mftb %1\n"
       
    54 				  "mftbu %2\n"
       
    55 				  "cmpw %3,%4\n"
       
    56 				  "bne- $-16\n"
       
    57 				  : "=r" (high), "=r" (low), "=r" (high2)
       
    58 				  : "0" (high), "2" (high2)
       
    59 				  );
       
    60 	return ((uint64)high << 32) | low;
       
    61 }
       
    62 # define RDTSC_AVAILABLE
       
    63 #endif
       
    64 
       
    65 /* In all other cases we have no support for rdtsc. No major issue,
       
    66  * you just won't be able to profile your code with TIC()/TOC() */
       
    67 #if !defined(RDTSC_AVAILABLE)
       
    68 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
       
    69 uint64 _rdtsc(void) {return 0;}
       
    70 #endif