在为Windows编写新代码时,我偶然发现_cpuinfo()了Windows API.因为我主要处理Linux环境(GCC),所以我希望能够访问CPUInfo.
我尝试过以下方法:
#include <iostream>
int main()
{
int a, b;
for (a = 0; a < 5; a++)
{
__asm ( "mov %1, %%eax; " // a into eax
"cpuid;"
"mov %%eax, %0;" // eax into b
:"=r"(b) // output
:"r"(a) // input
:"%eax","%ebx","%ecx","%edx" // clobbered register
);
std::cout << "The code " << a << " gives " << b << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这使用组件,但我不想重新发明轮子.有没有其他方法可以在没有汇编的情况下实现CPUInfo?
编译器错误:
lewis@lewis-desktop:~/Desktop/prog$ g++ -Wall CPUInfo.cpp
CPUInfo.cpp: In function ‘int main()’:
CPUInfo.cpp:10:22: error: expected ‘)’ before ‘;’ token
CPUInfo.cpp:10:23: error: expected primary-expression before ‘)’ token
CPUInfo.cpp:10:23: error: expected ‘;’ before ‘)’ token
CPUInfo.cpp:8:8: warning: unused variable ‘b’ [-Wunused-variable]
CPUInfo.cpp:12:8: error: expected ‘}’ at end of input
Run Code Online (Sandbox Code Playgroud)
Dav*_*nan 35
由于您正在使用GCC进行编译,因此您可以包含cpuid.h声明这些函数的内容:
/* Return highest supported input value for cpuid instruction. ext can
be either 0x0 or 0x8000000 to return highest supported value for
basic or extended cpuid information. Function returns 0 if cpuid
is not supported or whatever cpuid returns in eax register. If sig
pointer is non-null, then first four bytes of the signature
(as found in ebx register) are returned in location pointed by sig. */
unsigned int __get_cpuid_max (unsigned int __ext, unsigned int *__sig)
/* Return cpuid data for requested cpuid level, as found in returned
eax, ebx, ecx and edx registers. The function checks if cpuid is
supported and returns 1 for valid cpuid information or 0 for
unsupported cpuid level. All pointers are required to be non-null. */
int __get_cpuid (unsigned int __level,
unsigned int *__eax, unsigned int *__ebx,
unsigned int *__ecx, unsigned int *__edx)
Run Code Online (Sandbox Code Playgroud)
您不需要也不应该重新实现此功能.
for (a =0; a < 5; ++a;)
Run Code Online (Sandbox Code Playgroud)
那里应该只有两个分号.你有三个.
这是基本的C/C++语法; CPUID是一个红色的鲱鱼.
| 归档时间: |
|
| 查看次数: |
14397 次 |
| 最近记录: |