c++windowsgccmingwaes-ni

Within my c++ program is there a way to check if the CPU has AES-NI


I want to be able to check if a CPU has AES-NI available to it with C++ code on windows. (MinGW GCC)

I found a solution to this written in C# with visual studio.

Test for AES-NI instructions from C#

 private static bool IsAESNIPresent()
 {
    byte[] sn = new byte[16]; // !!! Here were 8 bytes

    if (!ExecuteCode(ref sn))
        return false;

    var ecx = BitConverter.ToUInt32(sn, 8);
    return (ecx & (1 << 25)) != 0;
 }

Is there an easy way to do the same with c++? (GCC)


Solution

  • There is some code in pycrypto that seems to be applicable. I took the essential parts of the code for testing:

    #include <cpuid.h>
    #include <stdint.h>
    #include <stdio.h>
    
    int main()
    {
        uint32_t eax, ebx, ecx, edx;
    
        eax = ebx = ecx = edx = 0;
        __get_cpuid(1, &eax, &ebx, &ecx, &edx);
        printf("%08x %08x %08x %08x\n", eax, ebx, ecx, edx);
        printf("Has AES-NI: %d\n", (ecx & bit_AES) > 0);
    
        return 0;
    }
    

    The results seem to correspond with the information provided by /proc/cpuinfo or information provided by intel web pages.

    For other capabilities, see clang documentation