Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if SSE is supported at runtime [duplicate]

Tags:

c++

c

avx

simd

sse

I would like to check if SSE4 or AVX is supported at runtime, so that my program may take advantage of processor specific instructions without creating a binary for each processor.

If I could determine it at runtime, I could use an interface and switch between different instruction sets.

like image 620
Thomas Avatar asked Mar 21 '15 00:03

Thomas


2 Answers

GCC has a way of doing this that starts by calling __builtin_cpu_init then calling __builtin_cpu_is and __builtin_cpu_supports to check features. https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/X86-Built-in-Functions.html

On x86, when using the C++ frontend, GCC supports "function multiversioning", which allows you to write multiple versions of the function, specify the target it should be used on, and let GCC take care of making sure it is called. https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Multiversioning.html

like image 187
Tyler Avatar answered Sep 28 '22 07:09

Tyler


On MSVC, extern int __isa_available has information about the CPU support on a MSVC build.

It is used by the vectorizer in MSVC 2013 to pick what assembly to run.

like image 32
Yakk - Adam Nevraumont Avatar answered Sep 28 '22 06:09

Yakk - Adam Nevraumont