# os_timer.cpp

Go to the documentation of this file.

    00001 /* $Id: os_timer.cpp 10778 2007-08-03 23:26:12Z truelight $ */
    00002
    00005 #include "stdafx.h"
    00006
    00007 #undef RDTSC_AVAILABLE
    00008
    00009 /* rdtsc for MSC_VER, uses simple inline assembly, or _rdtsc
    00010  * from external win64.asm because VS2005 does not support inline assembly */
    00011 #if defined(_MSC_VER) && !defined(RDTSC_AVAILABLE) && !defined(WINCE)
    00012 # if _MSC_VER >= 1400
    00013 #include <intrin.h>
    00014 uint64 _rdtsc()
    00015 {
    00016   return __rdtsc();
    00017 }
    00018 # else
    00019 uint64 _declspec(naked) _rdtsc()
    00020 {
    00021   _asm {
    00022     rdtsc
    00023     ret
    00024   }
    00025 }
    00026 # endif
    00027 # define RDTSC_AVAILABLE
    00028 #endif
    00029
    00030 /* rdtsc for OS/2. Hopefully this works, who knows */
    00031 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
    00032 unsigned __int64 _rdtsc();
    00033 # pragma aux _rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
    00034 # define RDTSC_AVAILABLE
    00035 #endif
    00036
    00037 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
    00038 #if defined(__i386__) || defined(__x86_64__) && !defined(RDTSC_AVAILABLE)
    00039 uint64 _rdtsc()
    00040 {
    00041   uint32 high, low;
    00042   __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
    00043   return ((uint64)high << 32) | low;
    00044 }
    00045 # define RDTSC_AVAILABLE
    00046 #endif
    00047
    00048 /* rdtsc for PPC which has this not */
    00049 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
    00050 uint64 _rdtsc()
    00051 {
    00052   uint32 high = 0, high2 = 0, low;
    00053   /* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
    00054    * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
    00055    * very unlikely event that the lower part overflows to the upper part while we
    00056    * read it; we double-check and reread the registers */
    00057   asm volatile (
    00058           "mftbu %0\n"
    00059           "mftb %1\n"
    00060           "mftbu %2\n"
    00061           "cmpw %3,%4\n"
    00062           "bne- $-16\n"
    00063           : "=r" (high), "=r" (low), "=r" (high2)
    00064           : "0" (high), "2" (high2)
    00065           );
    00066   return ((uint64)high << 32) | low;
    00067 }
    00068 # define RDTSC_AVAILABLE
    00069 #endif
    00070
    00071 /* In all other cases we have no support for rdtsc. No major issue,
    00072  * you just won't be able to profile your code with TIC()/TOC() */
    00073 #if !defined(RDTSC_AVAILABLE)
    00074 /* MSVC (in case of WinCE) can't handle #warning */
    00075 # if !defined(_MSC_VER)
    00076 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
    00077 # endif
    00078 uint64 _rdtsc() {return 0;}
    00079 #endif