#include <cstdio>
#include <cstring>
#if defined(__GNUC__)
#if defined(__clang__)
#define COMPILER_FMT "Clang: %s"
#else
#define COMPILER_FMT "GNUC: %s"
#endif
#define COMPILER_VERSION __VERSION__
#elif defined(_MSC_VER)
#define COMPILER_FMT "MSVC: %d"
#define COMPILER_VERSION _MSC_FULL_VER
#else
#error Please add your compiler here.
#endif
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx)
: "memory");
}
static inline void __cpuid(unsigned *cpuinfo, int op) {
cpuinfo[0] = op;
cpuinfo[2] = 0;
native_cpuid(&cpuinfo[0], &cpuinfo[1], &cpuinfo[2], &cpuinfo[3]);
}
int main() {
printf("Built with " COMPILER_FMT "\n", COMPILER_VERSION);
printf("C++ version: %ld\n", __cplusplus);
unsigned CPUInfo[4] = {0};
unsigned nExIds, i = 0;
char CPUBrandString[0x40];
// Get the information associated with each extended ID.
__cpuid(CPUInfo, 0x80000000);
nExIds = CPUInfo[0];
for (i=0x80000000; i<=nExIds; ++i)
{
__cpuid(CPUInfo, i);
// Interpret CPU brand string
if (i == 0x80000002)
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000003)
memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000004)
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
}
//string includes manufacturer, model and clockspeed
printf("CPU: %s\n", CPUBrandString);
printf("Pointer width (bits): %d\n", 8 * sizeof(char *));
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 9.26 us | 12 KB | Accepted | Score: 100 | 显示更多 |