-1

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Atharva Dubey
  • 832
  • 1
  • 8
  • 25

1 Answers1

2

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.

vec10
  • 88
  • 1
  • 4
  • Oh, that makes sense, Thanks a lot. – Atharva Dubey May 16 '21 at 12:44
  • 1
    @AtharvaDubey: "configured gcc..." means specifically `gcc -march=native`, `gcc -march=skylake`, or `-mavx2` (but don't *just* do the latter, it leaves out `-mfma`, `-mbmi2`, and especially leaves out tuning options to fix [Why doesn't gcc resolve \_mm256\_loadu\_pd as single vmovupd?](https://stackoverflow.com/q/52626726)) – Peter Cordes May 18 '21 at 16:08