x86 CPU Dispatching for SSE/AVX in C++

青春壹個敷衍的年華 提交于 2019-12-07 17:29:29

问题


I have an algorithm which benefits from hand optimisation with SSE(2) intrinsics. Moreover, the algorithm will also be able to benefit from the 256-bit AVX registers in the future.

My question is what is the best way to

  • Register the availability variants of my class at compile time; so if my classes are, say: Foo, FooSSE2 and FooAVX I require a means of determining at runtime what classes are compiled in.
  • Determine the capabilities of the current CPU. At the lowest level this will result in a cpuid call.
  • Decide at runtime what to use based on what is compiled and what is supported.

While I can hack most of the above it seems to be a common enough problem that some best practices must have emerged. Ideally I am trying to avoid the #ifdef mess

#ifdef COMPILE_SSE2
    if (sse2_supported)
        // Use the SSE2 class
#endif

回答1:


Just create a "factory" class or function to create appropriate instances of your class and hide all the logic in the file that implements the factory.

Have some class or file local boolean values like "isSSE2Supported" or "isAVXSupported". On startup, call some function to initialize these values. Your factory logic can then check against the values to determine which class to use.

Since SSE2 is always available on x64 chips, you don't really want to avoid all of the ifdefs. You can avoid compiling in some of the classes for x64 builds.



来源:https://stackoverflow.com/questions/4788592/x86-cpu-dispatching-for-sse-avx-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!