在C ++中调用`ffsl`的标准方法? [英] Standard way to call `ffsl` in C++?

查看:230
本文介绍了在C ++中调用`ffsl`的标准方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ffsl 函数是glibc的一部分。在GCC中,也可以通过 __ builtin_ffsl

有没有办法在符合标准的C ++代码中访问此功能?我希望获得这些版本(如果有),因为它们是为提高性能而使用汇编语言编写的。 (大多数体系结构都提供了有关此功能或类似功能的指令。)

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.)

推荐答案

没有标准功能可以实现此功能,每个编译器通常提供计算ffs本身的内在函数(有关相对完整的列表,请参见例如 Wikipedia )。因此,最好的方法是做一个包装器

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)
...

还请注意, __ builtin_ffs ffs(3)可能会生成非等效代码(我会使用编译器内部函数,因为它们往往会被编译器更好地优化,但另一方面,GCC内部函数并不总是得到很好的优化。)

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).

这篇关于在C ++中调用`ffsl`的标准方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆