如何否定(更改符号)__m128 类型变量中的浮点元素? [英] How to negate (change sign) of the floating point elements in a __m128 type variable?

查看:30
本文介绍了如何否定(更改符号)__m128 类型变量中的浮点元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何单个指令或函数可以反转 __m128 中每个浮点数的符号?即 a = r0:r1:r2:r3 ===>a = -r0:-r1:-r2:-r3?

Is there any single instruction or function that can invert the sign of every float inside a __m128? i.e. a = r0:r1:r2:r3 ===> a = -r0:-r1:-r2:-r3?

我知道这可以通过 _mm_sub_ps(_mm_set1_ps(0.0),a) 来完成,但是因为 _mm_set1_ps(0.0) 是一个多指令功能?

I know this can be done by _mm_sub_ps(_mm_set1_ps(0.0),a), but isn't it potentially slow since _mm_set1_ps(0.0) is a multi-instruction function?

推荐答案

在实践中,您的编译器应该很好地生成 0.0.0 的常量向量.它可能只会使用 _mm_xor_ps,并且如果您的代码处于循环中,它无论如何都应该将常量生成提升到循环之外.所以,最重要的是,使用您最初的想法:

In practice your compiler should do a good job of generating the constant vector for 0.0. It will probably just use _mm_xor_ps, and if your code is in a loop it should hoist the constant generation out of the loop anyway. So, bottom line, use your original idea of:

v = _mm_sub_ps(_mm_set1_ps(0.0), v);

或另一个常见的技巧,即:

or another common trick, which is:

v = _mm_xor_ps(v, _mm_set1_ps(-0.0));

它只是翻转符号位而不是做减法(不如第一种方法安全,因为它对 NaN 没有做正确的事情,但在某些情况下可能更有效).

which just flips the sign bits instead of doing a subtraction (not quite as safe as the first method, since it doesn't do the right thing with NaNs, but may be more efficient in some cases).

这篇关于如何否定(更改符号)__m128 类型变量中的浮点元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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