Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / cpu.cpp
blobf1b498b6c1d66470e93a77d261b9ed8c6263bb25
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 /**
16 * Definitions for CPU detection:
18 * MSVC offers cpu information while gcc only implements in gcc 4.8
19 * __builtin_cpu_supports and friends
20 * http://msdn.microsoft.com/library/vstudio/hskdteyh%28v=vs.100%29.aspx
21 * http://gcc.gnu.org/onlinedocs/gcc/X86-Built-in-Functions.html
23 * Other platforms/architectures don't have CPUID, so zero the info and then
24 * most (if not all) of the features are set as if they do not exist.
26 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
27 void ottd_cpuid(int info[4], int type)
29 __cpuid(info, type);
31 #elif defined(__x86_64__) || defined(__i386)
32 void ottd_cpuid(int info[4], int type)
34 #if defined(__i386) && defined(__PIC__)
35 /* The easy variant would be just cpuid, however... ebx is being used by the GOT (Global Offset Table)
36 * in case of PIC;
37 * clobbering ebx is no alternative: some compiler versions don't like this
38 * and will issue an error message like
39 * "can't find a register in class 'BREG' while reloading 'asm'"
41 __asm__ __volatile__ (
42 "xchgl %%ebx, %1 \n\t"
43 "cpuid \n\t"
44 "xchgl %%ebx, %1 \n\t"
45 : "=a" (info[0]), "=r" (info[1]), "=c" (info[2]), "=d" (info[3])
46 /* It is safe to write "=r" for (info[1]) as in case that PIC is enabled for i386,
47 * the compiler will not choose EBX as target register (but something else).
49 : "a" (type)
51 #else
52 __asm__ __volatile__ (
53 "cpuid \n\t"
54 : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3])
55 : "a" (type)
57 #endif /* i386 PIC */
59 #elif defined(__e2k__) /* MCST Elbrus 2000*/
60 void ottd_cpuid(int info[4], int type)
62 info[0] = info[1] = info[2] = info[3] = 0;
63 if (type == 0) {
64 info[0] = 1;
65 } else if (type == 1) {
66 #if defined(__SSE4_1__)
67 info[2] |= (1<<19); /* HasCPUIDFlag(1, 2, 19) */
68 #endif
69 #if defined(__SSSE3__)
70 info[2] |= (1<<9); /* HasCPUIDFlag(1, 2, 9) */
71 #endif
72 #if defined(__SSE2__)
73 info[3] |= (1<<26); /* HasCPUIDFlag(1, 3, 26) */
74 #endif
77 #else
78 void ottd_cpuid(int info[4], int)
80 info[0] = info[1] = info[2] = info[3] = 0;
82 #endif
84 bool HasCPUIDFlag(uint type, uint index, uint bit)
86 int cpu_info[4] = {-1};
87 ottd_cpuid(cpu_info, 0);
88 uint max_info_type = cpu_info[0];
89 if (max_info_type < type) return false;
91 ottd_cpuid(cpu_info, type);
92 return HasBit(cpu_info[index], bit);