将 128 位 xmm 寄存器的高位和低位 64 位相加

发布于 2024-08-14 22:22:33 字数 267 浏览 4 评论 0原文

我在 xmm0 中有两个压缩四字整数,我需要将它们加在一起并将结果存储在内存位置中。我可以保证每个整数的值都小于2^15。现在,我正在做以下事情:

int temp;
....   

   movdq2q mm0, xmm0
   psrldq xmm0, 8
   movdq2q mm1, xmm0
   paddq mm0,mm1
   movd temp, mm0

有更好的方法吗?

I have two packed quadword integers in xmm0 and I need to add them together and store the result in a memory location. I can guarantee that the value of the each integer is less than 2^15. Right now, I'm doing the following:

int temp;
....   

   movdq2q mm0, xmm0
   psrldq xmm0, 8
   movdq2q mm1, xmm0
   paddq mm0,mm1
   movd temp, mm0

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-08-21 22:22:33

首先,为什么使用四字来表示适合 16 位格式的值?抛开这一点,有几个解决方案:

pshufd xmm1, xmm0, EEh
paddq  xmm0, xmm1
movd   temp, xmm0

movdqa xmm1, xmm0
psrldq xmm1, 8
paddq  xmm0, xmm1
movd   temp, xmm0

movhlps xmm1, xmm0
paddq   xmm0, xmm1
movd    temp, xmm0

请注意,您实际上不需要使用 paddq,如果您愿意,您可以使用较窄的添加之一。

编辑对四个双四字求和——你所拥有的非常好。鉴于您知道其中的所有数据都适合每个插槽的低位双字,您可以尝试以下操作:

shufps  xmm0, xmm2, 88h
shufps  xmm4, xmm6, 88h
paddd   xmm0, xmm4
psrlq   xmm1, xmm0, 32
paddd   xmm0, xmm1
movhlps xmm1, xmm0
paddd   xmm0, xmm0
movd    temp, xmm0

这可能会或可能不会更快。

至于EMMS,这只是另一条指令。在任何涉及 MMX 寄存器的代码之后、任何使用 x87 浮点指令的代码之前,您都需要有 emms

First off, why are you using quadwords to represent values that would fit in a 16-bit format? Leaving that aside, a couple solutions:

pshufd xmm1, xmm0, EEh
paddq  xmm0, xmm1
movd   temp, xmm0

or

movdqa xmm1, xmm0
psrldq xmm1, 8
paddq  xmm0, xmm1
movd   temp, xmm0

or

movhlps xmm1, xmm0
paddq   xmm0, xmm1
movd    temp, xmm0

Note that you don't actually need to use paddq, you can get away with one of the narrower adds if you prefer.

edit summing four double quadwords -- what you have is pretty much fine. Given that you know that all the data in them fits into the low doubleword of each slot, you could try something like:

shufps  xmm0, xmm2, 88h
shufps  xmm4, xmm6, 88h
paddd   xmm0, xmm4
psrlq   xmm1, xmm0, 32
paddd   xmm0, xmm1
movhlps xmm1, xmm0
paddd   xmm0, xmm0
movd    temp, xmm0

which may or may not prove to be faster.

As for EMMS, it's just another instruction. After any code that touches the MMX registers, before any code that uses the x87 floating-point instructions you need to have emms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文