Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / cpu.cpp
blobb93f0fa5ed0e367db0ba27d89f296d765a144c39
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file cpu.cpp OS/CPU/compiler dependent CPU specific calls. */
10 #include "stdafx.h"
11 #include "core/bitmath_func.hpp"
13 #include "safeguards.h"
15 #undef RDTSC_AVAILABLE
17 /* rdtsc for MSC_VER, uses simple inline assembly, or _rdtsc
18 * from external win64.asm because VS2005 does not support inline assembly */
19 #if defined(_MSC_VER) && !defined(RDTSC_AVAILABLE)
20 #include <intrin.h>
21 #include <windows.h>
22 uint64 ottd_rdtsc()
24 #if defined(_M_ARM)
25 return __rdpmccntr64();
26 #elif defined(_M_ARM64)
27 return _ReadStatusReg(ARM64_PMCCNTR_EL0);
28 #else
29 return __rdtsc();
30 #endif
32 #define RDTSC_AVAILABLE
33 #endif
35 /* rdtsc for OS/2. Hopefully this works, who knows */
36 #if defined (__WATCOMC__) && !defined(RDTSC_AVAILABLE)
37 unsigned __int64 ottd_rdtsc();
38 # pragma aux ottd_rdtsc = 0x0F 0x31 value [edx eax] parm nomemory modify exact [edx eax] nomemory;
39 # define RDTSC_AVAILABLE
40 #endif
42 /* rdtsc for all other *nix-en (hopefully). Use GCC syntax */
43 #if (defined(__i386__) || defined(__x86_64__)) && !defined(RDTSC_AVAILABLE)
44 uint64 ottd_rdtsc()
46 uint32 high, low;
47 __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
48 return ((uint64)high << 32) | low;
50 # define RDTSC_AVAILABLE
51 #endif
53 /* rdtsc for PPC which has this not */
54 #if (defined(__POWERPC__) || defined(__powerpc__)) && !defined(RDTSC_AVAILABLE)
55 uint64 ottd_rdtsc()
57 uint32 high = 0, high2 = 0, low;
58 /* PPC does not have rdtsc, so we cheat by reading the two 32-bit time-counters
59 * it has, 'Move From Time Base (Upper)'. Since these are two reads, in the
60 * very unlikely event that the lower part overflows to the upper part while we
61 * read it; we double-check and reread the registers */
62 asm volatile (
63 "mftbu %0\n"
64 "mftb %1\n"
65 "mftbu %2\n"
66 "cmpw %3,%4\n"
67 "bne- $-16\n"
68 : "=r" (high), "=r" (low), "=r" (high2)
69 : "0" (high), "2" (high2)
71 return ((uint64)high << 32) | low;
73 # define RDTSC_AVAILABLE
74 #endif
76 #if defined(__EMSCRIPTEN__) && !defined(RDTSC_AVAILABLE)
77 /* On emscripten doing TIC/TOC would be ill-advised */
78 uint64 ottd_rdtsc() {return 0;}
79 # define RDTSC_AVAILABLE
80 #endif
82 /* In all other cases we have no support for rdtsc. No major issue,
83 * you just won't be able to profile your code with TIC()/TOC() */
84 #if !defined(RDTSC_AVAILABLE)
85 #warning "(non-fatal) No support for rdtsc(), you won't be able to profile with TIC/TOC"
86 uint64 ottd_rdtsc() {return 0;}
87 #endif
90 /**
91 * Definitions for CPU detection:
93 * MSVC offers cpu information while gcc only implements in gcc 4.8
94 * __builtin_cpu_supports and friends
95 * http://msdn.microsoft.com/library/vstudio/hskdteyh%28v=vs.100%29.aspx
96 * http://gcc.gnu.org/onlinedocs/gcc/X86-Built-in-Functions.html
98 * Other platforms/architectures don't have CPUID, so zero the info and then
99 * most (if not all) of the features are set as if they do not exist.
101 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
102 void ottd_cpuid(int info[4], int type)
104 __cpuid(info, type);
106 #elif defined(__x86_64__) || defined(__i386)
107 void ottd_cpuid(int info[4], int type)
109 #if defined(__i386) && defined(__PIC__)
110 /* The easy variant would be just cpuid, however... ebx is being used by the GOT (Global Offset Table)
111 * in case of PIC;
112 * clobbering ebx is no alternative: some compiler versions don't like this
113 * and will issue an error message like
114 * "can't find a register in class 'BREG' while reloading 'asm'"
116 __asm__ __volatile__ (
117 "xchgl %%ebx, %1 \n\t"
118 "cpuid \n\t"
119 "xchgl %%ebx, %1 \n\t"
120 : "=a" (info[0]), "=r" (info[1]), "=c" (info[2]), "=d" (info[3])
121 /* It is safe to write "=r" for (info[1]) as in case that PIC is enabled for i386,
122 * the compiler will not choose EBX as target register (but something else).
124 : "a" (type)
126 #else
127 __asm__ __volatile__ (
128 "cpuid \n\t"
129 : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3])
130 : "a" (type)
132 #endif /* i386 PIC */
134 #else
135 void ottd_cpuid(int info[4], int type)
137 info[0] = info[1] = info[2] = info[3] = 0;
139 #endif
141 bool HasCPUIDFlag(uint type, uint index, uint bit)
143 int cpu_info[4] = {-1};
144 ottd_cpuid(cpu_info, 0);
145 uint max_info_type = cpu_info[0];
146 if (max_info_type < type) return false;
148 ottd_cpuid(cpu_info, type);
149 return HasBit(cpu_info[index], bit);