Detecting Advanced Vector Extensions (AVX) support in Visual Studio

Every so often Intel or AMD come out with new instructions for their x86 and x64 instruction sets. Many of you may have heard of the Streaming SMID Extensions (SSE) instructions. The new Advanced Vector Extensions (AVX) instructions are similar to the older SSE instructions, but use new 32 byte (yes, byte) registers.

When building with a GCC like compiler, one usually isolates AVX code like so:

#ifdef __AVX__
// AVX intrinsics or inline assembly goes here
// Non AVX version with same functionality (or just error)

When you compile your code, you tell GCC the type of chip you want to target, or have it build with instructions suitable for the local machine. Inline assembly or intrinsics using AVX instructions will fail to compile if the build is not set up to allow them.

Visual Studio has a different expectation. Generally, one builds code that will run on all modern x86 or x64 chips and the program itself asks the CPU what instructions are supported. The program will branch at runtime depending on what CPU features are available. This post will give an overview of how to do this checking.


AVX instructions are only supported on (at time of writing) the latest and greatest OS, compiler and CPU versions. You will need the following:

  • Visual Studio 2010 SP1 or later
  • Windows 7 SP1 or later
  • A CPU that supports AVX instructions (check documentation).

The new AVX instructions use new YMM registers which the OS must save on a context switch. If you try to use them on a Windows version prior to Windows 7 SP1 they will be considered illegal instructions, even if the CPU has AVX support.

Performing the check

You query CPU capabilities on x86 and x64 chips using the CPUID instruction. Visual studio has an intrinsic called __cpuid that wraps this instruction. You can check if your chip supports AVX instructions using the __cpuid intrinsic, but that’s not enough. You also need to check that the OS is saving the new registers on context switch. This requires using the XGETBV instruction. A new intrinsic called _xgetbv is available for this in Visual Studio 2010 SP1.

Note that MSDN currently has no documentation of _xgetbv and other intrinsics added in Visual Studio 2010 SP1.

#include <stdio.h>
#include <intrin.h>

int main()
    bool avxSupported = false;

    // If Visual Studio 2010 SP1 or later
#if (_MSC_FULL_VER >= 160040219)
    // Checking for AVX requires 3 things:
    // 1) CPUID indicates that the OS uses XSAVE and XRSTORE
    //     instructions (allowing saving YMM registers on context
    //     switch)
    // 2) CPUID indicates support for AVX
    // 3) XGETBV indicates the AVX registers will be saved and
    //     restored on context switch
    // Note that XGETBV is only available on 686 or later CPUs, so
    // the instruction needs to be conditionally run.
    int cpuInfo[4];
    __cpuid(cpuInfo, 1);

    bool osUsesXSAVE_XRSTORE = cpuInfo[2] & (1 << 27) || false;
    bool cpuAVXSuport = cpuInfo[2] & (1 << 28) || false;

    if (osUsesXSAVE_XRSTORE && cpuAVXSuport)
        // Check if the OS will save the YMM registers
        unsigned long long xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
        avxSupported = (xcrFeatureMask & 0x6) || false;

    if (avxSupported)
        printf("AVX is supported\n");
        printf("AVX is NOT supported\n");

    return 0;


2 Responses to “Detecting Advanced Vector Extensions (AVX) support in Visual Studio”

  1. Andreas Stiller Says:

    Nice try, but wrong :
    avxSupported = (xcrFeatureMask & 0x6) || false;
    should be
    avxSupported = (xcrFeatureMask & 0x6) == 6;

  2. Alla Volin Says:

    What i don’t understood is if truth be told how you are now not really a lot more neatly-favored than you may be right now. You are very intelligent. You know thus considerably relating to this subject, made me individually consider it from so many varied angles. Its like women and men are not fascinated unless it is one thing to accomplish with Woman gaga! Your personal stuffs great. All the time maintain it up!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: