image



// Analysis_CPU.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <bitset>
#include <array>
#include <string>
#include <intrin.h>


/*
MSDN:
        https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?redirectedfrom=MSDN&view=msvc-160#Example
*/
using namespace std;

int main()
{
        int nIds_;
        arraycpui;
        std::vector> data_;
        data_.empty();
        std::string vendor_;

        bool isIntel_;
        bool isAMD_;

        // Calling __cpuid with 0x0 as the function_id argument
        // gets the number of the highest valid function ID.
        __cpuid(cpui.data(), 0);
        nIds_ = cpui[0];

        for (int i = 0; i <= nIds_; ++i)
        {
                __cpuidex(cpui.data(), i, 0);
                data_.push_back(cpui);
        }

        // Capture vendor string
        char vendor[0x20];
        int  nExIds;
        memset(vendor, 0, sizeof(vendor));
        *reinterpret_cast(vendor) = data_[0][1];
        *reinterpret_cast(vendor + 4) = data_[0][3];
        *reinterpret_cast(vendor + 8) = data_[0][2];
        vendor_ = vendor;
        if (vendor_ == "GenuineIntel")
        {
                isIntel_ = true;
        }
        else
        if (vendor_ == "AuthenticAMD") 
        {
                isAMD_ = true;
        }

        // Calling __cpuid with 0x80000000 as the function_id argument
        // gets the number of the highest valid extended ID.
        __cpuid(cpui.data(), 0x80000000);
        nExIds = cpui[0];
        char processor[0x40];
        std::vector> procData;
        procData.empty();
        memset(processor, 0, sizeof(processor));

        for (int i = 0x80000000; i <= nExIds; ++i)
        {
                __cpuidex(cpui.data(), i, 0);
                procData.push_back(cpui);
        }
        // Interpret CPU brand string if reported
        if (nExIds >= 0x80000004)
        {
                memcpy(processor, procData[2].data(), sizeof(cpui));
                memcpy(processor + 16, procData[3].data(), sizeof(cpui));
                memcpy(processor + 32, procData[4].data(), sizeof(cpui));
        }
        printf("Vendor: %s\n", vendor);
        printf("Processor: %s\n", processor);
}



}

Eric 發表在 痞客邦 留言(0) 人氣()