[英]AVX2 64-bit unsigned integer comparison
I'm trying to compare two __m256i
(4 packed 64-bit integers). 我正在尝试比较两个__m256i
(4个打包的64位整数)。 To do so, I use the _mm256_cmpgt_epi64 function. 为此,我使用_mm256_cmpgt_epi64函数。
The function works as expected except for a few comparisons, as if the function did not consider the largest bit of the 64-bit integer. 除了进行一些比较外,该函数按预期工作,好像该函数未考虑64位整数的最大位。 As shown below in the second and third comparisons. 如下第二和第三比较所示。
Here a MCVE, where I expect each 64-bit integer from a
to be larger than its peer from b
(thus cp
should be 0xFFF...FFF
): 这里一个MCVE,其中I期望从每个64位整数a
为比从它的对等较大的b
(因此cp
应0xFFF...FFF
):
#include <immintrin.h>
#include <x86intrin.h>
#include <stdio.h>
#include <inttypes.h>
// gcc mcve.c -mavx2 -madx && ./a.out
int print_avx2_hex256(__m256i ymm)
{
const size_t n = sizeof(__m256i) / sizeof(u_int64_t);
u_int64_t buffer[n];
_mm256_storeu_si256((__m256i*)buffer, ymm);
for (int i=0; i<n; ++i)
printf("%016"PRIx64" ", buffer[i]);
printf("\n");
return 0;
}
int compare(__m256i a, __m256i b)
{
__m256i cp = _mm256_cmpgt_epi64(a,b);
print_avx2_hex256(cp); // Comparison
print_avx2_hex256(a);
print_avx2_hex256(b);
return 0;
}
int main()
{
u_int64_t _a[4] = {0xf, 0xf000000000000000, 0xd00000000000000d, 0x0f00000000000000};
u_int64_t _b[4] = {0x2, 0x2000000000000000, 0x2000000000000002, 0x0200000000000000};
__m256i a = _mm256_setr_epi64x(_a[0], _a[1], _a[2], _a[3]);
__m256i b = _mm256_setr_epi64x(_b[0], _b[1], _b[2], _b[3]);
compare(a,b);
return 0;
}
However my output is as follow (in order cp
, a
, b
): 但是我的输出如下(按cp
, a
, b
顺序):
ffffffffffffffff 0000000000000000 0000000000000000 ffffffffffffffff
000000000000000f f000000000000000 d00000000000000d 0f00000000000000
0000000000000002 2000000000000000 2000000000000002 0200000000000000
I'm not familiar with the Intel Intrinsics functions, so if someone can tell me what I'm doing wrong, it would be appreciated :) 我不熟悉Intel Intrinsics函数,因此如果有人可以告诉我我做错了什么,将不胜感激:)
Your issue is that _mm256_cmpgt_epi64
compares signed integers, so if you set the most significant bit on one of the i64s, it is considered negative. 您的问题是_mm256_cmpgt_epi64
比较带符号的整数,因此,如果您在i64之一上设置了最高有效位,则它被认为是负数。 For instance, 0xf000000000000000
is negative, 0x2000000000000000
is not, and _mm256_cmpgt_epi64
(rightly) tells you that the latter is greater. 例如, 0xf000000000000000
为负, 0x2000000000000000
不是,并且_mm256_cmpgt_epi64
(正确)告诉您后者更大。
It doesn't look like there's a strictly equivalent function that compares unsigned ints, but you can use _mm256_cmpgt_epu64_mask
, which returns an __mmask8
bit-field. 看起来没有一个严格的等效函数比较无符号的int,但是您可以使用_mm256_cmpgt_epu64_mask
,该__mmask8
返回__mmask8
位字段。
EDIT: forgot to mention that _mm256_cmpgt_epu64_mask
requires AVX512, which may not be available to you. 编辑:忘记提及_mm256_cmpgt_epu64_mask
需要AVX512,它可能对您不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.