简体   繁体   English

AVX2 64位无符号整数比较

[英]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 (因此cp0xFFF...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 ): 但是我的输出如下(按cpab顺序):

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.

相关问题 在许多 64 位位掩码上分别计算每个位位置,使用 AVX 而不是 AVX2 - Count each bit-position separately over many 64-bit bitmasks, with AVX but not AVX2 AVX2整数比较,较小的等于 - AVX2 integer comparison for smaller equal 如何在 C 中生成随机的 64 位无符号整数 - How to generate random 64-bit unsigned integer in C 为稀疏的64位无符号整数创建最小完美哈希 - Create Minimum perfect hash for sparse 64-bit unsigned integer 优化从AVX2寄存器中提取64位值 - Optimize extraction of 64 bit value from AVX2 register 减去两个无符号的32位整数并分配给64位有符号整数 - subtracting two unsigned 32-bit integers and assign to 64-bit signed integer 如何将2 ^ 63添加到带符号的64位整数并将其转换为无符号的64位整数,而不使用中间的128位整数 - How to add 2^63 to a signed 64-bit integer and cast it to a unsigned 64-bit integer without using 128-bit integer in the middle 将X字节写入文件描述符的正确方法,其中X是无符号的64位整数 - Correct way to write X bytes to a file descriptor where X is a unsigned 64-bit integer C群集无符号64位整数的计数,最大值为15 - C Population Count of unsigned 64-bit integer with a maximum value of 15 AVX将64位整数转换为64位浮点数 - AVX convert 64 bit integer to 64 bit float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM