OpenTTD Source 20260218-master-g2123fca5ea
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
13#include "safeguards.h"
14
16using CPUIDArray = std::array<int, 4>;
17
33static CPUIDArray CPUID([[maybe_unused]] int type)
34{
35 CPUIDArray info{};
36#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
37 __cpuid(info.data(), type);
38#elif defined(__i386) && defined(__PIC__)
39 /* The easy variant would be just cpuid, however... ebx is being used by the GOT (Global Offset Table)
40 * in case of PIC;
41 * clobbering ebx is no alternative: some compiler versions don't like this
42 * and will issue an error message like
43 * "can't find a register in class 'BREG' while reloading 'asm'"
44 */
45 __asm__ __volatile__ (
46 "xchgl %%ebx, %1 \n\t"
47 "cpuid \n\t"
48 "xchgl %%ebx, %1 \n\t"
49 : "=a" (info[0]), "=r" (info[1]), "=c" (info[2]), "=d" (info[3])
50 /* It is safe to write "=r" for (info[1]) as in case that PIC is enabled for i386,
51 * the compiler will not choose EBX as target register (but something else).
52 */
53 : "a" (type)
54 );
55#elif defined(__x86_64__) || defined(__i386)
56 __asm__ __volatile__ (
57 "cpuid \n\t"
58 : "=a" (info[0]), "=b" (info[1]), "=c" (info[2]), "=d" (info[3])
59 : "a" (type)
60 );
61#elif defined(__e2k__) /* MCST Elbrus 2000*/
62 if (type == 0) {
63 info[0] = 1;
64 } else if (type == 1) {
65# if defined(__SSE4_1__)
66 info[2] |= (1<<19); /* HasCPUIDFlag(1, 2, 19) */
67# endif
68# if defined(__SSSE3__)
69 info[2] |= (1<<9); /* HasCPUIDFlag(1, 2, 9) */
70# endif
71# if defined(__SSE2__)
72 info[3] |= (1<<26); /* HasCPUIDFlag(1, 3, 26) */
73# endif
74 }
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:16
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:33
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.