aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/misc/mptCPU.cpp
blob: 56b7bd63f2437526122aac903869a940c593f0f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * mptCPU.cpp
 * ----------
 * Purpose: CPU feature detection.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "mptCPU.h"

#include "../common/mptStringBuffer.h"

#if defined(MPT_ENABLE_ARCH_INTRINSICS)
#if MPT_COMPILER_MSVC && (defined(MPT_ENABLE_ARCH_X86) || defined(MPT_ENABLE_ARCH_AMD64))
#include <intrin.h>
#endif // MPT_COMPILER_MSVC && (MPT_ENABLE_ARCH_X86 || MPT_ENABLE_ARCH_AMD64)
#endif // MPT_ENABLE_ARCH_INTRINSICS


OPENMPT_NAMESPACE_BEGIN


namespace CPU
{


#if defined(MPT_ENABLE_ARCH_INTRINSICS)


uint32 EnabledFeatures = 0;


#if MPT_COMPILER_MSVC && (defined(MPT_ENABLE_ARCH_X86) || defined(MPT_ENABLE_ARCH_AMD64))


typedef char cpuid_result_string[12];


struct cpuid_result {
	uint32 a;
	uint32 b;
	uint32 c;
	uint32 d;
	std::string as_string() const
	{
		cpuid_result_string result;
		result[0+0] = (b >> 0) & 0xff;
		result[0+1] = (b >> 8) & 0xff;
		result[0+2] = (b >>16) & 0xff;
		result[0+3] = (b >>24) & 0xff;
		result[4+0] = (d >> 0) & 0xff;
		result[4+1] = (d >> 8) & 0xff;
		result[4+2] = (d >>16) & 0xff;
		result[4+3] = (d >>24) & 0xff;
		result[8+0] = (c >> 0) & 0xff;
		result[8+1] = (c >> 8) & 0xff;
		result[8+2] = (c >>16) & 0xff;
		result[8+3] = (c >>24) & 0xff;
		return std::string(result, result + 12);
	}
	std::string as_string4() const
	{
		std::string result;
		result.push_back(static_cast<uint8>((a >>  0) & 0xff));
		result.push_back(static_cast<uint8>((a >>  8) & 0xff));
		result.push_back(static_cast<uint8>((a >> 16) & 0xff));
		result.push_back(static_cast<uint8>((a >> 24) & 0xff));
		result.push_back(static_cast<uint8>((b >>  0) & 0xff));
		result.push_back(static_cast<uint8>((b >>  8) & 0xff));
		result.push_back(static_cast<uint8>((b >> 16) & 0xff));
		result.push_back(static_cast<uint8>((b >> 24) & 0xff));
		result.push_back(static_cast<uint8>((c >>  0) & 0xff));
		result.push_back(static_cast<uint8>((c >>  8) & 0xff));
		result.push_back(static_cast<uint8>((c >> 16) & 0xff));
		result.push_back(static_cast<uint8>((c >> 24) & 0xff));
		result.push_back(static_cast<uint8>((d >>  0) & 0xff));
		result.push_back(static_cast<uint8>((d >>  8) & 0xff));
		result.push_back(static_cast<uint8>((d >> 16) & 0xff));
		result.push_back(static_cast<uint8>((d >> 24) & 0xff));
		return result;
	}
};


static cpuid_result cpuid(uint32 function)
{
	cpuid_result result;
	int CPUInfo[4];
	__cpuid(CPUInfo, function);
	result.a = CPUInfo[0];
	result.b = CPUInfo[1];
	result.c = CPUInfo[2];
	result.d = CPUInfo[3];
	return result;
}


static cpuid_result cpuidex(uint32 function_a, uint32 function_c)
{
	cpuid_result result;
	int CPUInfo[4];
	__cpuidex(CPUInfo, function_a, function_c);
	result.a = CPUInfo[0];
	result.b = CPUInfo[1];
	result.c = CPUInfo[2];
	result.d = CPUInfo[3];
	return result;
}


Info::Info()
{

	cpuid_result VendorString = cpuid(0x00000000u);
	mpt::String::WriteAutoBuf(VendorID) = VendorString.as_string();
	if(VendorString.a >= 0x00000001u)
	{
		cpuid_result StandardFeatureFlags = cpuid(0x00000001u);
		CPUID = StandardFeatureFlags.a;
		uint32 BaseStepping = (StandardFeatureFlags.a >>  0) & 0x0f;
		uint32 BaseModel    = (StandardFeatureFlags.a >>  4) & 0x0f;
		uint32 BaseFamily   = (StandardFeatureFlags.a >>  8) & 0x0f;
		uint32 ExtModel     = (StandardFeatureFlags.a >> 16) & 0x0f;
		uint32 ExtFamily    = (StandardFeatureFlags.a >> 20) & 0xff;
		if(BaseFamily == 0xf)
		{
			Family = static_cast<uint16>(ExtFamily + BaseFamily);
		} else
		{
			Family = static_cast<uint16>(BaseFamily);
		}
		if((BaseFamily == 0x6) || (BaseFamily == 0xf))
		{
			Model = static_cast<uint8>((ExtModel << 4) | (BaseModel << 0));
		} else
		{
			Model = static_cast<uint8>(BaseModel);
		}
		Stepping = static_cast<uint8>(BaseStepping);
		if(StandardFeatureFlags.d & (1<<23)) AvailableFeatures |= feature::mmx;
		if(StandardFeatureFlags.d & (1<<25)) AvailableFeatures |= feature::sse;
		if(StandardFeatureFlags.d & (1<<26)) AvailableFeatures |= feature::sse2;
		if(StandardFeatureFlags.c & (1<< 0)) AvailableFeatures |= feature::sse3;
		if(StandardFeatureFlags.c & (1<< 9)) AvailableFeatures |= feature::ssse3;
		if(StandardFeatureFlags.c & (1<<19)) AvailableFeatures |= feature::sse4_1;
		if(StandardFeatureFlags.c & (1<<20)) AvailableFeatures |= feature::sse4_2;
		if(StandardFeatureFlags.c & (1<<28)) AvailableFeatures |= feature::avx;
	}
	if(VendorString.a >= 0x00000007u)
	{
		cpuid_result ExtendedFeatures = cpuidex(0x00000007u, 0x00000000u);
		if(ExtendedFeatures.b & (1<< 5)) AvailableFeatures |= feature::avx2;
	}

	cpuid_result ExtendedVendorString = cpuid(0x80000000u);
	if(ExtendedVendorString.a >= 0x80000001u)
	{
		cpuid_result ExtendedFeatureFlags = cpuid(0x80000001u);
		if(ExtendedFeatureFlags.d & (1<<29)) AvailableFeatures |= feature::lm;
	}
	if(ExtendedVendorString.a >= 0x80000004u)
	{
		mpt::String::WriteAutoBuf(BrandID) = cpuid(0x80000002u).as_string4() + cpuid(0x80000003u).as_string4() + cpuid(0x80000004u).as_string4();
	}

}


#elif MPT_COMPILER_MSVC && (defined(MPT_ENABLE_ARCH_X86) || defined(MPT_ENABLE_ARCH_AMD64))


Info::Info()
{

	if(IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE) != 0)    AvailableFeatures |= feature::mmx;
	if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE) != 0)   AvailableFeatures |= feature::sse;
	if(IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0) AvailableFeatures |= feature::sse2;
	if(IsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE) != 0)   AvailableFeatures |= feature::sse3;

}


#else // !(MPT_COMPILER_MSVC && (MPT_ENABLE_ARCH_X86 || MPT_ENABLE_ARCH_AMD64))


Info::Info()
{
	return;
}


#endif // MPT_COMPILER_MSVC && (MPT_ENABLE_ARCH_X86 || MPT_ENABLE_ARCH_AMD64)


const Info & Info::Get()
{
	static Info info;
	return info;
}


struct InfoInitializer
{
	InfoInitializer()
	{
		Info::Get();
	}
};


static InfoInitializer g_InfoInitializer;


void EnableAvailableFeatures()
{
	EnabledFeatures = Info::Get().AvailableFeatures;
}


#endif // MPT_ENABLE_ARCH_INTRINSICS


uint32 GetMinimumFeatures()
{
	uint32 flags = 0;
	#ifdef MPT_ENABLE_ARCH_INTRINSICS
		#if MPT_COMPILER_MSVC
			#if defined(_M_X64)
				flags |= feature::lm | feature::sse | feature::sse2;
			#elif defined(_M_IX86)
				#if defined(_M_IX86_FP)
					#if (_M_IX86_FP >= 2)
						flags |= feature::sse | feature::sse2;
					#elif (_M_IX86_FP == 1)
						flags |= feature::sse;
					#endif
				#endif
			#endif
			#if defined(__AVX__)
				flags |= feature::avx;
			#endif
			#if defined(__AVX2__)
				flags |= feature::avx2;
			#endif
		#endif	
	#endif // MPT_ENABLE_ARCH_INTRINSICS
	return flags;
}



} // namespace CPU


OPENMPT_NAMESPACE_END