~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/Common/Src/ArmCPUDetect.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2003 Dolphin Project.
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
12
// A copy of the GPL 2.0 should have been included with the program.
 
13
// If not, see http://www.gnu.org/licenses/
 
14
 
 
15
// Official SVN repository and contact information can be found at
 
16
// http://code.google.com/p/dolphin-emu/
 
17
 
 
18
#include "Common.h"
 
19
#include "CPUDetect.h"
 
20
#include "FileUtil.h"
 
21
 
 
22
// Only Linux platforms have /proc/cpuinfo
 
23
#if !defined(BLACKBERRY) && !defined(IOS) && !defined(__SYMBIAN32__)
 
24
const char procfile[] = "/proc/cpuinfo";
 
25
 
 
26
char *GetCPUString()
 
27
{
 
28
        const char marker[] = "Hardware\t: ";
 
29
        char *cpu_string = 0;
 
30
        // Count the number of processor lines in /proc/cpuinfo
 
31
        char buf[1024];
 
32
 
 
33
        File::IOFile file(procfile, "r");
 
34
        auto const fp = file.GetHandle();
 
35
        if (!fp)
 
36
                return 0;
 
37
        
 
38
        while (fgets(buf, sizeof(buf), fp))
 
39
        {
 
40
                if (strncmp(buf, marker, sizeof(marker) - 1))
 
41
                        continue;
 
42
                cpu_string = buf + sizeof(marker) - 1;
 
43
                cpu_string = strndup(cpu_string, strlen(cpu_string) - 1); // Strip the newline
 
44
                break;
 
45
        }
 
46
        
 
47
        return cpu_string;
 
48
}
 
49
 
 
50
unsigned char GetCPUImplementer()
 
51
{
 
52
        const char marker[] = "CPU implementer\t: ";
 
53
        char *implementer_string = 0;
 
54
        unsigned char implementer = 0;
 
55
        char buf[1024];
 
56
 
 
57
        File::IOFile file(procfile, "r");
 
58
        auto const fp = file.GetHandle();
 
59
        if (!fp)
 
60
                return 0;
 
61
 
 
62
        while (fgets(buf, sizeof(buf), fp))
 
63
        {
 
64
                if (strncmp(buf, marker, sizeof(marker) - 1))
 
65
                        continue;
 
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);
 
69
                break;
 
70
        }
 
71
 
 
72
        free(implementer_string);
 
73
 
 
74
        return implementer;
 
75
}
 
76
 
 
77
unsigned short GetCPUPart()
 
78
{
 
79
        const char marker[] = "CPU part\t: ";
 
80
        char *part_string = 0;
 
81
        unsigned short part = 0;
 
82
        char buf[1024];
 
83
 
 
84
        File::IOFile file(procfile, "r");
 
85
        auto const fp = file.GetHandle();
 
86
        if (!fp)
 
87
                return 0;
 
88
 
 
89
        while (fgets(buf, sizeof(buf), fp))
 
90
        {
 
91
                if (strncmp(buf, marker, sizeof(marker) - 1))
 
92
                        continue;
 
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);
 
96
                break;
 
97
        }
 
98
 
 
99
        free(part_string);
 
100
 
 
101
        return part;
 
102
}
 
103
 
 
104
bool CheckCPUFeature(const char *feature)
 
105
{
 
106
        const char marker[] = "Features\t: ";
 
107
        char buf[1024];
 
108
 
 
109
        File::IOFile file(procfile, "r");
 
110
        auto const fp = file.GetHandle();
 
111
        if (!fp)
 
112
                return 0;
 
113
        
 
114
        while (fgets(buf, sizeof(buf), fp))
 
115
        {
 
116
                if (strncmp(buf, marker, sizeof(marker) - 1))
 
117
                        continue;
 
118
                char *featurestring = buf + sizeof(marker) - 1;
 
119
                char *token = strtok(featurestring, " ");
 
120
                while (token != NULL)
 
121
                {
 
122
                        if (strstr(token, feature))
 
123
                                return true; 
 
124
                        token = strtok(NULL, " ");
 
125
                }
 
126
        }
 
127
        
 
128
        return false;
 
129
}
 
130
#endif
 
131
 
 
132
int GetCoreCount()
 
133
{
 
134
#ifdef __SYMBIAN32__
 
135
        return 1;
 
136
#elif defined(BLACKBERRY) || defined(IOS)
 
137
        return 2;
 
138
#else
 
139
        const char marker[] = "processor\t: ";
 
140
        int cores = 0;
 
141
        char buf[1024];
 
142
 
 
143
        File::IOFile file(procfile, "r");
 
144
        auto const fp = file.GetHandle();
 
145
        if (!fp)
 
146
                return 0;
 
147
        
 
148
        while (fgets(buf, sizeof(buf), fp))
 
149
        {
 
150
                if (strncmp(buf, marker, sizeof(marker) - 1))
 
151
                        continue;
 
152
                ++cores;
 
153
        }
 
154
        
 
155
        return cores;
 
156
#endif
 
157
}
 
158
 
 
159
CPUInfo cpu_info;
 
160
 
 
161
CPUInfo::CPUInfo() {
 
162
        Detect();
 
163
}
 
164
 
 
165
// Detects the various cpu features
 
166
void CPUInfo::Detect()
 
167
{
 
168
        // Set some defaults here
 
169
        // When ARMv8 cpus come out, these need to be updated.
 
170
        HTT = false;
 
171
        OS64bit = false;
 
172
        CPU64bit = false;
 
173
        Mode64bit = false;                               
 
174
        vendor = VENDOR_ARM;
 
175
        
 
176
        // Get the information about the CPU 
 
177
        num_cores = GetCoreCount();
 
178
#if defined(__SYMBIAN32__) || defined(BLACKBERRY) || defined(IOS)
 
179
        bool isVFP3 = false;
 
180
        bool isVFP4 = false;
 
181
#ifdef IOS
 
182
        isVFP3 = true;
 
183
    // Check for swift arch (VFP4`)
 
184
    #ifdef __ARM_ARCH_7S__
 
185
        isVFP4 = true;
 
186
    #endif // #ifdef __ARM_ARCH_7S__
 
187
#elif defined(BLACKBERRY)
 
188
        isVFP3 = true;
 
189
        const char cpuInfoPath[] = "/pps/services/hw_info/inventory";
 
190
        const char marker[] = "Processor_Name::";
 
191
        const char qcCPU[] = "MSM";
 
192
        char buf[1024];
 
193
        FILE* fp;
 
194
        if (fp = fopen(cpuInfoPath, "r"))
 
195
        {
 
196
                while (fgets(buf, sizeof(buf), fp))
 
197
                {
 
198
                        if (strncmp(buf, marker, sizeof(marker) - 1))
 
199
                                continue;
 
200
                        if (strncmp(buf + sizeof(marker) - 1, qcCPU, sizeof(qcCPU) - 1) == 0)
 
201
                                isVFP4 = true;
 
202
                        break;
 
203
                }
 
204
                fclose(fp);
 
205
        }
 
206
#endif
 
207
        // Hardcode this for now
 
208
        bSwp = true;
 
209
        bHalf = true;
 
210
        bThumb = false;
 
211
        bFastMult = true;
 
212
        bVFP = true;
 
213
        bEDSP = true;
 
214
        bThumbEE = isVFP3;
 
215
        bNEON = isVFP3;
 
216
        bVFPv3 = isVFP3;
 
217
        bTLS = true;
 
218
        bVFPv4 = isVFP4;
 
219
        bIDIVa = isVFP4;
 
220
        bIDIVt = isVFP4;
 
221
        bFP = false;
 
222
        bASIMD = false;
 
223
#else
 
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");
 
244
#endif
 
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__)
 
248
        bArmV7 = true;
 
249
#else
 
250
        bArmV7 = false;
 
251
#endif
 
252
}
 
253
 
 
254
// Turn the cpu info into a string we can show
 
255
std::string CPUInfo::Summarize()
 
256
{
 
257
        std::string sum;
 
258
#if defined(BLACKBERRY) || defined(IOS) || defined(__SYMBIAN32__)
 
259
        sum = StringFromFormat("%i cores", num_cores);
 
260
#else
 
261
        if (num_cores == 1)
 
262
                sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
 
263
        else
 
264
                sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
 
265
#endif
 
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";
 
279
 
 
280
        return sum;
 
281
}