# os_timer.cpp

Go to the documentation of this file.

    00001 /* $Id: os_timer.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013
    00014 #undef RDTSC_AVAILABLE
    00015
    00016 /* rdtsc for MSC_VER, uses simple inline assembly, or _rdtsc
    00017  * from external win64.asm because VS2005 does not support inline assembly */
    00018 #if defined(_MSC_VER) && !defined(RDTSC_AVAILABLE) && !defined(WINCE)
    00019 #include <intrin.h>
    00020 uint64 ottd_rdtsc()
    00021 {
    00022   return __rdtsc();
    00023 }
    00024 #define RDTSC_AVAILABLE
    00025 #endif
    00026
    00027 /* rdtsc for OS/2. Hopefully this works, who knows */
    00028 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
    00029 unsigned __int64 ottd_rdtsc();
    00030 # pragma aux ottd_rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
    00031 # define RDTSC_AVAILABLE
    00032 #endif
    00033
    00034 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
    00035 #if (defined(__i386__) || defined(__x86_64__)) && !defined(__DJGPP__) && !defined(RDTSC_AVAILABLE)
    00036 uint64 ottd_rdtsc()
    00037 {
    00038   uint32 high, low;
    00039   __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
    00040   return ((uint64)high << 32) | low;
    00041 }
    00042 # define RDTSC_AVAILABLE
    00043 #endif
    00044
    00045 /* rdtsc for PPC which has this not */
    00046 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
    00047 uint64 ottd_rdtsc()
    00048 {
    00049   uint32 high = 0, high2 = 0, low;
    00050   /* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
    00051    * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
    00052    * very unlikely event that the lower part overflows to the upper part while we
    00053    * read it; we double-check and reread the registers */
    00054   asm volatile (
    00055           "mftbu %0\n"
    00056           "mftb %1\n"
    00057           "mftbu %2\n"
    00058           "cmpw %3,%4\n"
    00059           "bne- $-16\n"
    00060           : "=r" (high), "=r" (low), "=r" (high2)
    00061           : "0" (high), "2" (high2)
    00062           );
    00063   return ((uint64)high << 32) | low;
    00064 }
    00065 # define RDTSC_AVAILABLE
    00066 #endif
    00067
    00068 /* In all other cases we have no support for rdtsc. No major issue,
    00069  * you just won't be able to profile your code with TIC()/TOC() */
    00070 #if !defined(RDTSC_AVAILABLE)
    00071 /* MSVC (in case of WinCE) can't handle #warning */
    00072 # if !defined(_MSC_VER)
    00073 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
    00074 # endif
    00075 uint64 ottd_rdtsc() {return 0;}
    00076 #endif