C++ 中的 CPUID 实现

时间:2023-04-13
本文介绍了C++ 中的 CPUID 实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道这里是否有人有一些很好的 C++ CPUID 实现示例,可以从任何托管 .net 语言中引用.

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 获取有关运行我的软件的机器的信息(崩溃报告等...),并且我希望尽可能广泛地兼容所有内容.

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...

在人们开始告诉我使用 Google 之前:我在网上找到了一些示例,但通常它们并不意味着允许来自托管代码的交互,并且没有一个示例同时针对 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 信息实际上非常容易,这里有一个适用于 Windows、Linux 和 OSX 的 C++ 类:

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

为 Windows 添加了 #include ,每条评论.

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

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

上一篇:如何在 Intel 语法中使用 clang 生成汇编代码? 下一篇:C 中的函数 Prologue 和 Epilogue

相关文章