Standard way to call `ffsl` in C++?

爷,独闯天下 提交于 2019-12-11 03:34:14

问题


The ffsl function is part of glibc. In GCC it is also available via __builtin_ffsl. It returns the index of the least significant bit in a long.

Is there a way to access this functionality in standards-conforming C++ code? I would like to get at these versions (if available) because they are written in assembly for high performance. (Most architectures provide an instruction for this function or similar.)


回答1:


There is no standard function to achieve this and each compiler typically provides it's own intrinsic to compute ffs (see e.g. Wikipedia for relatively complete list). So your best take would be to do a wrapper

#ifdef __GNUC__
#  define ffs(x) __builtin_ffs(x)
#elif __INTEL_COMPILER
#  define ffs(x) _bit_scan_forward(x)
...

Also note that __builtin_ffs and ffs(3) may generate non-equivalent code (I'd use compiler intrinsic as these tend to be better optimized by compiler, on the other hand GCC intrinsics are not always well optimized).



来源:https://stackoverflow.com/questions/41369765/standard-way-to-call-ffsl-in-c

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