Compiler macro to detect BMI2 instruction set

牧云@^-^@ 提交于 2019-12-23 13:13:12

问题


I was searching on the web to find a proper solution, without much success. So I hope one of you know something about it: Is there any way to detect the "Intel Bit Manipulation Instruction Sets 2" (BMI2) compile time? I want to make some conditional thing based on the availability of it.


回答1:


With GCC you can check for the __BMI2__ macro. This macro will be defined if the target supports BMI2 (e.g. -mbmi2,-march=haswell). This is the macro that the instrinsic's headers (x86intrin.h, bmi2intrin.h) uses to check for BMI2 at compile time.

For runtime checks, __builtin_cpu_init() and __builtin_cpu_supports("bmi2") can be used in modern GCC (tested in GCC 5.1, 4.9 and lower doesn't have it).




回答2:


Run the CPUID intrinsic function with EAX=7, ECX=0, then check bit 3 of the returned EBX register (the BMI1 flag). EBX bit 8 is the BMI2 flag. Consult your compiler's documentation for how to call CPUID and get the data back from it.



来源:https://stackoverflow.com/questions/32214843/compiler-macro-to-detect-bmi2-instruction-set

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