1
// Copyright (C) 2003 Dolphin Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
19
#include "CPUDetect.h"
22
// Only Linux platforms have /proc/cpuinfo
23
#if !defined(BLACKBERRY) && !defined(IOS) && !defined(__SYMBIAN32__)
24
const char procfile[] = "/proc/cpuinfo";
28
const char marker[] = "Hardware\t: ";
30
// Count the number of processor lines in /proc/cpuinfo
33
File::IOFile file(procfile, "r");
34
auto const fp = file.GetHandle();
38
while (fgets(buf, sizeof(buf), fp))
40
if (strncmp(buf, marker, sizeof(marker) - 1))
42
cpu_string = buf + sizeof(marker) - 1;
43
cpu_string = strndup(cpu_string, strlen(cpu_string) - 1); // Strip the newline
50
unsigned char GetCPUImplementer()
52
const char marker[] = "CPU implementer\t: ";
53
char *implementer_string = 0;
54
unsigned char implementer = 0;
57
File::IOFile file(procfile, "r");
58
auto const fp = file.GetHandle();
62
while (fgets(buf, sizeof(buf), fp))
64
if (strncmp(buf, marker, sizeof(marker) - 1))
66
implementer_string = buf + sizeof(marker) - 1;
67
implementer_string = strndup(implementer_string, strlen(implementer_string) - 1); // Strip the newline
68
sscanf(implementer_string, "0x%02hhx", &implementer);
72
free(implementer_string);
77
unsigned short GetCPUPart()
79
const char marker[] = "CPU part\t: ";
80
char *part_string = 0;
81
unsigned short part = 0;
84
File::IOFile file(procfile, "r");
85
auto const fp = file.GetHandle();
89
while (fgets(buf, sizeof(buf), fp))
91
if (strncmp(buf, marker, sizeof(marker) - 1))
93
part_string = buf + sizeof(marker) - 1;
94
part_string = strndup(part_string, strlen(part_string) - 1); // Strip the newline
95
sscanf(part_string, "0x%03hx", &part);
104
bool CheckCPUFeature(const char *feature)
106
const char marker[] = "Features\t: ";
109
File::IOFile file(procfile, "r");
110
auto const fp = file.GetHandle();
114
while (fgets(buf, sizeof(buf), fp))
116
if (strncmp(buf, marker, sizeof(marker) - 1))
118
char *featurestring = buf + sizeof(marker) - 1;
119
char *token = strtok(featurestring, " ");
120
while (token != NULL)
122
if (strstr(token, feature))
124
token = strtok(NULL, " ");
136
#elif defined(BLACKBERRY) || defined(IOS)
139
const char marker[] = "processor\t: ";
143
File::IOFile file(procfile, "r");
144
auto const fp = file.GetHandle();
148
while (fgets(buf, sizeof(buf), fp))
150
if (strncmp(buf, marker, sizeof(marker) - 1))
165
// Detects the various cpu features
166
void CPUInfo::Detect()
168
// Set some defaults here
169
// When ARMv8 cpus come out, these need to be updated.
176
// Get the information about the CPU
177
num_cores = GetCoreCount();
178
#if defined(__SYMBIAN32__) || defined(BLACKBERRY) || defined(IOS)
183
// Check for swift arch (VFP4`)
184
#ifdef __ARM_ARCH_7S__
186
#endif // #ifdef __ARM_ARCH_7S__
187
#elif defined(BLACKBERRY)
189
const char cpuInfoPath[] = "/pps/services/hw_info/inventory";
190
const char marker[] = "Processor_Name::";
191
const char qcCPU[] = "MSM";
194
if (fp = fopen(cpuInfoPath, "r"))
196
while (fgets(buf, sizeof(buf), fp))
198
if (strncmp(buf, marker, sizeof(marker) - 1))
200
if (strncmp(buf + sizeof(marker) - 1, qcCPU, sizeof(qcCPU) - 1) == 0)
207
// Hardcode this for now
224
strncpy(cpu_string, GetCPUString(), sizeof(cpu_string));
225
bSwp = CheckCPUFeature("swp");
226
bHalf = CheckCPUFeature("half");
227
bThumb = CheckCPUFeature("thumb");
228
bFastMult = CheckCPUFeature("fastmult");
229
bVFP = CheckCPUFeature("vfp");
230
bEDSP = CheckCPUFeature("edsp");
231
bThumbEE = CheckCPUFeature("thumbee");
232
bNEON = CheckCPUFeature("neon");
233
bVFPv3 = CheckCPUFeature("vfpv3");
234
bTLS = CheckCPUFeature("tls");
235
bVFPv4 = CheckCPUFeature("vfpv4");
236
bIDIVa = CheckCPUFeature("idiva");
237
bIDIVt = CheckCPUFeature("idivt");
238
// Qualcomm Krait supports IDIVA but it doesn't report it. Check for krait.
239
if (GetCPUImplementer() == 0x51 && GetCPUPart() == 0x6F) // Krait(300) is 0x6F, Scorpion is 0x4D
240
bIDIVa = bIDIVt = true;
241
// These two require ARMv8 or higher
242
bFP = CheckCPUFeature("fp");
243
bASIMD = CheckCPUFeature("asimd");
245
// On android, we build a separate library for ARMv7 so this is fine.
246
// TODO: Check for ARMv7 on other platforms.
247
#if defined(__ARM_ARCH_7A__)
254
// Turn the cpu info into a string we can show
255
std::string CPUInfo::Summarize()
258
#if defined(BLACKBERRY) || defined(IOS) || defined(__SYMBIAN32__)
259
sum = StringFromFormat("%i cores", num_cores);
262
sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
264
sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
266
if (bSwp) sum += ", SWP";
267
if (bHalf) sum += ", Half";
268
if (bThumb) sum += ", Thumb";
269
if (bFastMult) sum += ", FastMult";
270
if (bVFP) sum += ", VFP";
271
if (bEDSP) sum += ", EDSP";
272
if (bThumbEE) sum += ", ThumbEE";
273
if (bNEON) sum += ", NEON";
274
if (bVFPv3) sum += ", VFPv3";
275
if (bTLS) sum += ", TLS";
276
if (bVFPv4) sum += ", VFPv4";
277
if (bIDIVa) sum += ", IDIVa";
278
if (bIDIVt) sum += ", IDIVt";