测试内置函数/内在函数

发布于 2024-10-05 12:51:04 字数 109 浏览 0 评论 0原文

我有一些使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2024-10-12 12:51:04

在最新版本的 clang 中,现在可以使用 __has_builtin() 宏检查内置内在函数是否存在,例如,

int popcount(int x)
{
#if __has_builtin(__builtin_popcount)
  return __builtin_popcount(x);
#else
  int count = 0;
  for (; x != 0; x &= x - 1)
    count++;
  return count;
#endif
}

希望 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.

int popcount(int x)
{
#if __has_builtin(__builtin_popcount)
  return __builtin_popcount(x);
#else
  int count = 0;
  for (; x != 0; x &= x - 1)
    count++;
  return count;
#endif
}

Let's hope GCC will also support __has_builtin() in the future.

彼岸花ソ最美的依靠 2024-10-12 12:51:04

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

烟花易冷人易散 2024-10-12 12:51:04

唯一应该开箱即用的事情是测试 gcc 版本,并希望这在所有体系结构上都能一致完成。

但这并不能保证,我最近遇到了类似的问题,不是内置函数,而是线程本地存储的 __thread 。这是在某些体系结构(linux)上实现的,但在其他体系结构(OS X,bsd?)上没有实现,并且无法通过宏找到它。

如果您有 gnu make,您可以执行类似的操作来检测 Makefile 中是否存在特定函数:

__THREAD := $(shell echo '__thread int i;' | ${CC} ${CFLAGS} -xc -c -o /dev/null - 2> /dev/null || echo "NO")
ifeq (${__THREAD},NO)
${warning thread local storage (TLS) with '__thread' is not supported, switching to pthread_getkey}
CFLAGS += -D__GNUC_NO_TLS__
endif

这可以避免使用更复杂的配置实用程序。

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:

__THREAD := $(shell echo '__thread int i;' | ${CC} ${CFLAGS} -xc -c -o /dev/null - 2> /dev/null || echo "NO")
ifeq (${__THREAD},NO)
${warning thread local storage (TLS) with '__thread' is not supported, switching to pthread_getkey}
CFLAGS += -D__GNUC_NO_TLS__
endif

This avoids to use more complex configuration utilities.

£烟消云散 2024-10-12 12:51:04

#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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文