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/>.
8 /** @file cpu.cpp OS/CPU/compiler dependent CPU specific calls. */
11 #include "core/bitmath_func.hpp"
13 #include "safeguards.h"
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
)
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)
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"
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).
52 __asm__
__volatile__ (
54 : "=a" (info
[0]), "=b" (info
[1]), "=c" (info
[2]), "=d" (info
[3])
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;
65 } else if (type
== 1) {
66 #if defined(__SSE4_1__)
67 info
[2] |= (1<<19); /* HasCPUIDFlag(1, 2, 19) */
69 #if defined(__SSSE3__)
70 info
[2] |= (1<<9); /* HasCPUIDFlag(1, 2, 9) */
73 info
[3] |= (1<<26); /* HasCPUIDFlag(1, 3, 26) */
78 void ottd_cpuid(int info
[4], int)
80 info
[0] = info
[1] = info
[2] = info
[3] = 0;
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
);