OpenTTD Source  20241108-master-g80f628063a
cpu.cpp
Go to the documentation of this file.
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  */
7 
10 #include "stdafx.h"
11 #include "core/bitmath_func.hpp"
12 
13 #include "safeguards.h"
14 
26 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
27 void ottd_cpuid(int info[4], int type)
28 {
29  __cpuid(info, type);
30 }
31 #elif defined(__x86_64__) || defined(__i386)
32 void ottd_cpuid(int info[4], int type)
33 {
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'"
40  */
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).
48  */
49  : "a" (type)
50  );
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)
56  );
57 #endif /* i386 PIC */
58 }
59 #elif defined(__e2k__) /* MCST Elbrus 2000*/
60 void ottd_cpuid(int info[4], int type)
61 {
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
75  }
76 }
77 #else
78 void ottd_cpuid(int info[4], int)
79 {
80  info[0] = info[1] = info[2] = info[3] = 0;
81 }
82 #endif
83 
84 bool HasCPUIDFlag(uint type, uint index, uint bit)
85 {
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;
90 
91  ottd_cpuid(cpu_info, type);
92  return HasBit(cpu_info[index], bit);
93 }
Functions related to bit mathematics.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
bool HasCPUIDFlag(uint type, uint index, uint bit)
Check whether the current CPU has the given flag.
Definition: cpu.cpp:84
void ottd_cpuid(int info[4], int)
Definitions for CPU detection:
Definition: cpu.cpp:78
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.