# os_timer.cpp

Go to the documentation of this file.

    00001 /* $Id: os_timer.cpp 15296 2009-01-30 17:54:48Z smatz $ */
    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 #include <intrin.h>
    00013 uint64 ottd_rdtsc()
    00014 {
    00015   return __rdtsc();
    00016 }
    00017 #define RDTSC_AVAILABLE
    00018 #endif
    00019
    00020 /* rdtsc for OS/2. Hopefully this works, who knows */
    00021 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
    00022 unsigned __int64 ottd_rdtsc();
    00023 # pragma aux ottd_rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
    00024 # define RDTSC_AVAILABLE
    00025 #endif
    00026
    00027 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
    00028 #if (defined(__i386__) || defined(__x86_64__)) && !defined(__DJGPP__) && !defined(RDTSC_AVAILABLE)
    00029 uint64 ottd_rdtsc()
    00030 {
    00031   uint32 high, low;
    00032   __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
    00033   return ((uint64)high << 32) | low;
    00034 }
    00035 # define RDTSC_AVAILABLE
    00036 #endif
    00037
    00038 /* rdtsc for PPC which has this not */
    00039 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
    00040 uint64 ottd_rdtsc()
    00041 {
    00042   uint32 high = 0, high2 = 0, low;
    00043   /* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
    00044    * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
    00045    * very unlikely event that the lower part overflows to the upper part while we
    00046    * read it; we double-check and reread the registers */
    00047   asm volatile (
    00048           "mftbu %0\n"
    00049           "mftb %1\n"
    00050           "mftbu %2\n"
    00051           "cmpw %3,%4\n"
    00052           "bne- $-16\n"
    00053           : "=r" (high), "=r" (low), "=r" (high2)
    00054           : "0" (high), "2" (high2)
    00055           );
    00056   return ((uint64)high << 32) | low;
    00057 }
    00058 # define RDTSC_AVAILABLE
    00059 #endif
    00060
    00061 /* In all other cases we have no support for rdtsc. No major issue,
    00062  * you just won't be able to profile your code with TIC()/TOC() */
    00063 #if !defined(RDTSC_AVAILABLE)
    00064 /* MSVC (in case of WinCE) can't handle #warning */
    00065 # if !defined(_MSC_VER)
    00066 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
    00067 # endif
    00068 uint64 ottd_rdtsc() {return 0;}
    00069 #endif