测试内置函数/内在函数
我有一些使用 gcc 内在函数的代码。我想包含代码,以防内在缺失。我该怎么做?
#ifdef __builtin_ctzll
不起作用。
I have some code that uses gcc intrinsics. I would like to include code in case the intrinsic is missing. How can I do this?
#ifdef __builtin_ctzll
does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在最新版本的 clang 中,现在可以使用 __has_builtin() 宏检查内置内在函数是否存在,例如,
希望 GCC 将来也支持 __has_builtin() 。
With recent versions of clang it is now possible to check if builtin intrinsics exist using the
__has_builtin()
macro e.g.Let's hope GCC will also support
__has_builtin()
in the future.GCC 现在(过去几年)支持
__has_builtin
:它于 2020 年 5 月 7 日在 GCC 10.1 中引入。在此处查看该版本的新功能。另外,感谢 @Charles 评论,请在此处找到错误报告
GCC now (for the last few years) supports
__has_builtin
: It was introduced in GCC 10.1, May 7 2020. See the new features for that version here.Also, thanks to @Charles comment, find the bug report here
唯一应该开箱即用的事情是测试 gcc 版本,并希望这在所有体系结构上都能一致完成。
但这并不能保证,我最近遇到了类似的问题,不是内置函数,而是线程本地存储的 __thread 。这是在某些体系结构(linux)上实现的,但在其他体系结构(OS X,bsd?)上没有实现,并且无法通过宏找到它。
如果您有 gnu make,您可以执行类似的操作来检测 Makefile 中是否存在特定函数:
这可以避免使用更复杂的配置实用程序。
The only thing that should work out of the box is to test the gcc version and hoping that this is consistently done on all architectures.
This is not guaranteed, though, I recently had a similar problem not with builtin functions but with
__thread
for thread local storage. This is implemented on some architectures (linux) but not not on others (OS X, bsd?) and there was no way to find this out with a macro.If you have gnu make you can do something similar to detect existence of a particular function in your Makefile:
This avoids to use more complex configuration utilities.
#ifdef
指令检查__builtin_ctzll
是否定义为宏名称,它不会帮助您确定__builtin_ctzll
是否为__builtin_ctzll代码> 函数存在。
我对 gcc 内置函数不够熟悉,无法为您提供更多帮助:内在函数怎么会丢失?
The
#ifdef
directive checks whether__builtin_ctzll
is defined as a macro name, it won't help you determine if a__builtin_ctzll
function exists.I'm not familiar enough with gcc builtins to help you more than this : how could the intrinsic be missing ?