Search code examples
gccg++avxinstruction-setavx2

How to detect AVX2 support using gcc


I need to detect AVX2 support in my code take decisions accordingly.
I am aware of two methods - __builtin_cpu_supports("avx2") and #if defined(__AVX2__). Now the issue is one returns true and another false. The test code is as follows -

int main(){
    if(__builtin_cpu_supports("avx2")){
        std::cout<< "Builtin methods supports" << std::endl;
    }
    
    
#if defined(__AVX2__)
    std::cout<<"Builtin symbol present"<<std::endl;
#endif
return 0;
}

And the output is as follows -

Builtin methods supports

I am running a i7 - 9750h. And my processor does support AVX2 instruction set.

What is the correct way to identify. My GCC version in 9.3.1


Solution

  • I think you've got this wrong. You can use the macro to check, at compile-time, if you've configured gcc to compile with AVX2 support. The builtin can check, at run-time, if the CPU it's currently running on supports the AVX2 extensions.