Skip to content

Instantly share code, notes, and snippets.

@prabindh
Created June 3, 2020 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabindh/1437a04ec59a5f4bafced73350066ce2 to your computer and use it in GitHub Desktop.
Save prabindh/1437a04ec59a5f4bafced73350066ce2 to your computer and use it in GitHub Desktop.
cpuid for avx avx2
//
// Derived https://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
//
#include <iostream>
#include <string>
using namespace std;
#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]; }
};
int main_cpuid()
{
CPUID cpuIDAVX(1); // Get AVX
CPUID cpuIDAVX2(7); // Get AVX2
uint32_t avx = (cpuIDAVX.ECX() & 0x10000000) != 0;
uint32_t avx2 = (cpuIDAVX2.EBX() & 0x00000020) != 0;
cout << "AVX = " << avx << ", AVX2 = " << avx2 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment