OpenTTD Source 20260911-master-gee2b2ac12a
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "core/bitmath_func.hpp"
12#include "cpu.h"
13
14#include "safeguards.h"
15
17using CPUIDArray = std::array<int, 4>;
18
34static CPUIDArray CPUID([[maybe_unused]] int type)
35{
36 CPUIDArray info{};
37#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
38 __cpuid(info.data(), type);
39#elif defined(__i386) && defined(__PIC__)
40 /* The easy variant would be just cpuid, however... ebx is being used by the GOT (Global Offset Table)
41 * in case of PIC;
42 * clobbering ebx is no alternative: some compiler versions don't like this
43 * and will issue an error message like
44 * "can't find a register in class 'BREG' while reloading 'asm'"
45 */
46 __asm__ __volatile__ (
47 "xchgl %%ebx, %1 \n\t"
48 "cpuid \n\t"
49 "xchgl %%ebx, %1 \n\t"
50 : "=a" (info[0]), "=r" (info[1]), "=c" (info[2]), "=d" (info[3])
51 /* It is safe to write "=r" for (info[1]) as in case that PIC is enabled for i386,
52 * the compiler will not choose EBX as target register (but something else).
53 */
54 : "a" (type)
55 );
56#elif defined(__x86_64__) || defined(__i386)
57 __asm__ __volatile__ (
58 "cpuid \n\t"
59 : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3])
60 : "a" (type)
61 );
62#elif defined(__e2k__) /* MCST Elbrus 2000*/
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#endif
77 return info;
78}
79
80bool HasCPUIDFlag(uint type, uint index, uint bit)
81{
82 CPUIDArray cpu_info = CPUID(0);
83 uint max_info_type = cpu_info[0];
84 if (max_info_type < type) return false;
85
86 cpu_info = CPUID(type);
87 return HasBit(cpu_info[index], bit);
88}
Functions related to bit mathematics.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
std::array< int, 4 > CPUIDArray
Container for CPUID information.
Definition cpu.cpp:17
bool HasCPUIDFlag(uint type, uint index, uint bit)
Check whether the current CPU has the given flag.
Definition cpu.cpp:80
static CPUIDArray CPUID(int type)
Get the CPUID information from the CPU.
Definition cpu.cpp:34
Functions related to CPU specific instructions.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.