使用SSE计算无符号整数之间的绝对差 [英] Compute the absolute difference between unsigned integers using SSE

查看:420
本文介绍了使用SSE计算无符号整数之间的绝对差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中有一个无分支的技术来计算两个无符号整数之间的绝对差值?例如给定变量a和b,当a = 3,b = 5或b = 3,a = 5时,我想使用值2。理想情况下,我也希望能够使用SSE寄存器将计算矢量化。

In C is there a branch-less technique to compute the absolute difference between two unsigned ints? For example given the variables a and b, I would like the value 2 for cases when a=3, b=5 or b=3, a=5. Ideally I would also like to be able to vectorize the computation using the SSE registers.

推荐答案

有几种方法可以做到,我只提一个:

There are several ways to do it, I'll just mention one:

SSE4


  • 使用 PMINUD PMAXUD 以分隔寄存器#1中较大的值和寄存器#2中较小的值。

  • 减去它们。

  • Use PMINUD and PMAXUD to separate the larger value in register #1, and the smaller value in register #2.
  • Subtract them.

MMX / SSE2

MMX/SSE2


  • 翻转这两个值的符号位,因为下一条指令只接受有符号整数比较。

  • PCMPGTD 。 c>(ba)(ab)计算结果 / code>

  • 使用 POR(PAND(mask,ab),PANDN(mask,ba))正确的绝对差值。

  • Flip the sign bit of the two values because the next instruction only accepts signed integer comparison.
  • PCMPGTD. Use this result as a mask.
  • Compute the results of both (a-b) and (b-a)
  • Use POR ( PAND ( mask, a-b ), PANDN ( mask, b-a ) ) to select the correct value for the absolute difference.

这篇关于使用SSE计算无符号整数之间的绝对差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆