# cpu.cpp

Go to the documentation of this file.

    00001 /* $Id: cpu.cpp 26225 2014-01-04 19:46:24Z 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 #include "core/bitmath_func.hpp"
    00014
    00015 #undef RDTSC_AVAILABLE
    00016
    00017 /* rdtsc for MSC_VER, uses simple inline assembly, or _rdtsc
    00018  * from external win64.asm because VS2005 does not support inline assembly */
    00019 #if defined(_MSC_VER) && !defined(RDTSC_AVAILABLE) && !defined(WINCE)
    00020 #include <intrin.h>
    00021 uint64 ottd_rdtsc()
    00022 {
    00023   return __rdtsc();
    00024 }
    00025 #define RDTSC_AVAILABLE
    00026 #endif
    00027
    00028 /* rdtsc for OS/2. Hopefully this works, who knows */
    00029 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
    00030 unsigned __int64 ottd_rdtsc();
    00031 # pragma aux ottd_rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
    00032 # define RDTSC_AVAILABLE
    00033 #endif
    00034
    00035 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
    00036 #if (defined(__i386__) || defined(__x86_64__)) && !defined(__DJGPP__) && !defined(RDTSC_AVAILABLE)
    00037 uint64 ottd_rdtsc()
    00038 {
    00039   uint32 high, low;
    00040   __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
    00041   return ((uint64)high << 32) | low;
    00042 }
    00043 # define RDTSC_AVAILABLE
    00044 #endif
    00045
    00046 /* rdtsc for PPC which has this not */
    00047 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
    00048 uint64 ottd_rdtsc()
    00049 {
    00050   uint32 high = 0, high2 = 0, low;
    00051   /* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
    00052    * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
    00053    * very unlikely event that the lower part overflows to the upper part while we
    00054    * read it; we double-check and reread the registers */
    00055   asm volatile (
    00056           "mftbu %0\n"
    00057           "mftb %1\n"
    00058           "mftbu %2\n"
    00059           "cmpw %3,%4\n"
    00060           "bne- $-16\n"
    00061           : "=r" (high), "=r" (low), "=r" (high2)
    00062           : "0" (high), "2" (high2)
    00063           );
    00064   return ((uint64)high << 32) | low;
    00065 }
    00066 # define RDTSC_AVAILABLE
    00067 #endif
    00068
    00069 /* In all other cases we have no support for rdtsc. No major issue,
    00070  * you just won't be able to profile your code with TIC()/TOC() */
    00071 #if !defined(RDTSC_AVAILABLE)
    00072 /* MSVC (in case of WinCE) can't handle #warning */
    00073 # if !defined(_MSC_VER)
    00074 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
    00075 # endif
    00076 uint64 ottd_rdtsc() {return 0;}
    00077 #endif
    00078
    00079
    00091 #if defined(_MSC_VER)
    00092 void ottd_cpuid(int info[4], int type)
    00093 {
    00094   __cpuid(info, type);
    00095 }
    00096 #elif defined(__x86_64__) || defined(__i386)
    00097 void ottd_cpuid(int info[4], int type)
    00098 {
    00099 #if defined(__i386) && defined(__PIC__)
    00100   /* The easy variant would be just cpuid, however... ebx is being used by the GOT (Global Offset Table)
    00101    * in case of PIC;
    00102    * clobbering ebx is no alternative: some compiler versions don't like this
    00103    * and will issue an error message like
    00104    *   "can't find a register in class 'BREG' while reloading 'asm'"
    00105    */
    00106   __asm__ __volatile__ (
    00107       "xchgl %%ebx, %1 \n\t"
    00108       "cpuid           \n\t"
    00109       "xchgl %%ebx, %1 \n\t"
    00110       : "=a" (info[0]), "=r" (info[1]), "=c" (info[2]), "=d" (info[3])
    00111       /* It is safe to write "=r" for (info[1]) as in case that PIC is enabled for i386,
    00112        * the compiler will not choose EBX as target register (but something else).
    00113        */
    00114       : "a" (type)
    00115   );
    00116 #else
    00117   __asm__ __volatile__ (
    00118       "cpuid           \n\t"
    00119       : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3])
    00120       : "a" (type)
    00121   );
    00122 #endif /* i386 PIC */
    00123 }
    00124 #else
    00125 void ottd_cpuid(int info[4], int type)
    00126 {
    00127   info[0] = info[1] = info[2] = info[3] = 0;
    00128 }
    00129 #endif
    00130
    00131 bool HasCPUIDFlag(uint type, uint index, uint bit)
    00132 {
    00133   int cpu_info[4] = {-1};
    00134   ottd_cpuid(cpu_info, 0);
    00135   uint max_info_type = cpu_info[0];
    00136   if (max_info_type < type) return false;
    00137
    00138   ottd_cpuid(cpu_info, type);
    00139   return HasBit(cpu_info[index], bit);
    00140 }