我正在尝试在包含 __int128 的 C++ union 体上执行 128 位 cas - 我是否需要调整该 union 体以使用来自 https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html 的 __atomic_compare_exchange_n 内在函数?
最佳答案
这完全取决于您的目标架构。
如果您使用的是 x86,则 the documentation你链接的说 128 位类型没有原子比较和交换:
The ‘__atomic’ builtins can be used with any integral scalar or pointer type that is 1, 2, 4, or 8 bytes in length. 16-byte integral types are also allowed if ‘__int128’ (see __int128) is supported by the architecture.
关于 __int128 的链接文档,它说:
As an extension the integer scalar type __int128 is supported for targets which have an integer mode wide enough to hold 128 bits. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.
x86 没有足够宽的整数模式来容纳 128 位。即使在 64 位 x86 上,long long 类型也对应一个 64 位整数,宽度小于 128 位。因此,文档表明 x86 上的 128 位类型不支持内部函数。
也就是说,x86 架构确实有一个cmpxchg16b
指令,它允许在 16 字节类型上进行原子比较和交换。我不知道 GCC __atomic
内置函数是否支持发出这条指令。 (不幸的是,there are some x86 processors that do not support this instruction ;您需要确定您的目标处理器是否支持它,或者编写执行运行时检查的代码,如果当前系统不支持,则回退到替代实现. 早期的 64 位 AMD 处理器不支持 cmpxchg16b
, Intel Core/Core 2 处理器的某些步进也不支持。也是not supported by Intel's Many Integrated Core (MIC) architecture .)
无论如何,cmpxchg16b
确实实际上要求其目标操作数是 16 字节对齐的,因此任何导致它被发射的内在函数都会对其施加相同的要求用户。使用 __attribute__(( __aligned__(16)))
注释将实现此目标。
如果您使用的是 x86 以外的体系结构,并且它本身支持标量 128 位整数类型,那么您必须查阅其文档以查看其比较和交换指令是否需要对齐。它可能会,而且即使在没有必要的情况下对齐数据也几乎不会影响性能。
关于c++ - 128 位比较和交换本征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827685/