gpt4 book ai didi

c - 如何并行比较两个以上的数字?

转载 作者:太空狗 更新时间:2023-10-29 17:23:50 25 4
gpt4 key购买 nike

是否可以使用 SSE4 在一条指令中比较多于一对数字?

Intel Reference以下是关于 PCMPGTQ 的内容

PCMPGTQ — Compare Packed Data for Greater Than

Performs an SIMD compare for the packed quadwords in the destination operand (first operand) and the source operand (second operand). If the data element in the first (destination) operand is greater than the corresponding element in the second (source) operand, the corresponding data element in the destination is set to all 1s; otherwise, it is set to 0s.

这并不是我真正想要的,因为我希望能够确定 vector 中哪些整数较大,哪些整数较小。

比如我需要比较

32 with 45
13 with 78
44 with 12
99 with 66

我计划将 [32, 13, 44, 99] 放在一个 vector 中,将 [45, 78, 12, 66] 放在另一个 vector 中并比较它们在一条指令中使用 SSE4,并将 [0, 0, 1, 1] 作为结果(0 - 小于,1 - 大于)

但这似乎不是 PCMPGTQ 所做的。关于如何在此级别使用并行性来加速比较,有什么建议吗?

最佳答案

我相信这实际上就是 PCMPGT 运算符家族所做的。后缀指定元素的大小 - B 表示 8 位元素,W 表示 16 位元素,D 表示 32 位元素, Q 用于 64 位元素。因此,如果您想一次比较 4 个 32 位数字,请使用带有 128 位 vector 参数的 PCMPGTD。有关这些操作码的伪代码描述,请参阅 this page

不过,他们不会只写10;他们将全 1 或全 0 写入每个元素,因此使用 PCMPGTB 比较 0x12345678876543210x8765432112345678 应该得到 0x0000FFFFFFFF0000.

This Intel white paper 给出了执行操作 a[i] = (a[i] > b[i]) ? a[i] : b[i](即 a[i] = max(a[i], b[i]))使用 vector 运算。

关于c - 如何并行比较两个以上的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com