CPUID实现在C ++中 [英] CPUID implementations in C++

查看:209
本文介绍了CPUID实现在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果有人在这里有一个C ++可以从任何托管.NET语言中引用CPUID执行一些很好的例子。

I would like to know if somebody around here has some good examples of a C++ CPUID implementation that can be referenced from any of the managed .net languages.

此外,应该不是这种情况下,我应该知道的X86和X64之间的某些差异的实施?

Also, should this not be the case, should I be aware of certain implementation differences between X86 and X64?

我想用CPUID来获得我的软件是在(crashreporting等)运行的计算机上的信息,我想保留一切尽可能广泛地兼容。

I would like to use CPUID to get info on the machine my software is running on (crashreporting etc...) and I want to keep everything as widely compatible as possible.

首要原因我想问的是因为我是一个总的小白,当谈到写的什么,可能将全部机器指令,虽然我有基本知识有关CPU寄存器等等...

Primary reason I ask is because I am a total noob when it comes to writing what will probably be all machine instructions though I have basic knowledge about CPU registers and so on...

在人们开始告诉我要谷歌:我发现了一些在线的例子,但通常他们并不意味着允许从托管code互动和没有一个示例是同时针对X86和X64。大多数示例似乎是X86特有的。

Before people start telling me to Google: I found some examples online, but usually they were not meant to allow interaction from managed code and none of the examples were aimed at both X86 and X64. Most examples appeared to be X86 specific.

干杯,

克里斯

推荐答案

访问原始CPUID信息实际上是很容易的,这里是一个C ++类,在Windows,Linux和OSX作品:

Accessing raw CPUID information is actually very easy, here is a C++ class for that which works in Windows, Linux and OSX:

#ifndef CPUID_H
#define CPUID_H

#ifdef _WIN32
#include <limits.h>
#include <intrin.h>
typedef unsigned __int32  uint32_t;

#else
#include <stdint.h>
#endif

class CPUID {
  uint32_t regs[4];

public:
  explicit CPUID(unsigned i) {
#ifdef _WIN32
    __cpuid((int *)regs, (int)i);

#else
    asm volatile
      ("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
       : "a" (i), "c" (0));
    // ECX is set to zero for CPUID function 4
#endif
  }

  const uint32_t &EAX() const {return regs[0];}
  const uint32_t &EBX() const {return regs[1];}
  const uint32_t &ECX() const {return regs[2];}
  const uint32_t &EDX() const {return regs[3];}
};

#endif // CPUID_H

要使用它只是实例化的类的实例,加载CPUID指令你有兴趣,并检查寄存器。例如:

To use it just instantiate an instance of the class, load the CPUID instruction you are interested in and examine the registers. For example:

#include "CPUID.h"

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
  CPUID cpuID(0); // Get CPU vendor

  string vendor;
  vendor += string((const char *)&cpuID.EBX(), 4);
  vendor += string((const char *)&cpuID.EDX(), 4);
  vendor += string((const char *)&cpuID.ECX(), 4);

  cout << "CPU vendor = " << vendor << endl;

  return 0;
}

本维基百科页面告诉你如何使用CPUID: http://en.wikipedia.org/wiki/CPUID

This Wikipedia page tells you how to use CPUID: http://en.wikipedia.org/wiki/CPUID

编辑:新增的#include&LT; intrin.h&GT; 适用于Windows,每个评论

Added #include <intrin.h> for Windows, per comments.

这篇关于CPUID实现在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆