VS2019版本x86。
template <int i> float get() const {
int f = _mm_extract_ps(fmm, i);
return (float const&)f;
}
当使用
return (float&)f;
编译器使用时extractps m32, ...
movss xmm0, m32
。正确的结果
当使用
return (float const&)f;
编译器使用时extractps eax, ...
movd xmm0, eax
。错误的结果
T&和T const&首先是T,然后是const的主要思想。 const只是程序员的某种协议(protocol)。您知道您可以解决它。但是汇编代码中没有任何const,只能输入float IS。而且我认为对于float&和float const&,它都必须是汇编中的float表示形式(cpu寄存器)。我们可以使用中间int reg32,但最终解释必须是float。
此时,它看起来像回归,因为它以前工作得很好。而且在这种情况下使用float&绝对是奇怪的,因为我们不应该涉及float const&安全性,但是float&的temp var确实值得怀疑。
微软回答:
Hi Truthfinder, thanks for the self-contained repro. As it happens, this behavior is actually correct. As my colleague @Xiang Fan [MSFT] described in an internal email:
The conversions performed by [a c-style cast] tries the following sequence: (4.1) — a const_cast (7.6.1.11), (4.2) — a static_cast (7.6.1.9), (4.3) — a static_cast followed by a const_cast, (4.4) — a reinterpret_cast (7.6.1.10), or (4.5) — a reinterpret_cast followed by a const_cast,
If a conversion can be interpreted in more than one of the ways listed above, the interpretation that appears first in the list is used.
So in your case, (const float &) is converted to static_cast, which has the effect "the initializer expression is implicitly converted to a prvalue of type “cv1 T1”. The temporary materialization conversion is applied and the reference is bound to the result."
But in the other case, (float &) is converted to reinterpret_cast because static_cast isn’t valid, which is the same as reinterpret_cast(&operand).
The actual "bug" you're observing is that one cast does: "transform the float-typed value "1.0" into the equivalent int-typed value "1"", while the other cast says "find the bit representation of 1.0 as a float, and then interpret those bits as an int".
For this reason we recommend against c-style casts.
Thanks!
MS论坛链接:https://developercommunity.visualstudio.com/content/problem/411552/extract-ps-intrinsics-bug.html
有任何想法吗?
P.S.我真正想要的是:
float val = _mm_extract_ps(xmm, 3);
在手动汇编中,我可以编写:
extractps val, xmm0, 3
其中val是float 32内存变量。只有一个!操作说明。我想在编译器生成的汇编代码中看到相同的结果。请勿乱序播放或进行其他任何过多的指示。最糟糕的可接受情况是:extractps reg32, xmm0, 3; mov val, reg32
。关于T&和T const&的观点:
对于这两种情况,变量的类型必须相同。但是现在
float&
将m32解释为float32,而float const&
将m32解释为int32。int main() {
int z = 1;
float x = (float&)z;
float y = (float const&)z;
printf("%f %f %i", x, y, x==y);
return 0;
}
Out: 0.000000 1.000000 0
真的可以吗?
最好的祝福,
真理发现者
最佳答案
关于C++强制转换语义存在一个有趣的问题(Microsoft已经为您简短地回答了这个问题),但是它与您对_mm_extract_ps
的误用混合在一起,导致首先需要进行类型双关。 (并且仅显示等效的asm,省略了int-> float转换。)如果其他人想在另一个答案中扩展标准ese的话,那就太好了。
TL:DR:改用它:0或1 shufps。没有提取物,没有类型修剪。
template <int i> float get(__m128 input) {
__m128 tmp = input;
if (i) // constexpr i means this branch is compile-time-only
tmp = _mm_shuffle_ps(tmp,tmp,i); // shuffle it to the bottom.
return _mm_cvtss_f32(tmp);
}
如果您确实有一个内存目标用例,则应该在asm中查找需要
float*
输出arg的函数,而不是需要xmm0
中的结果的函数。 (是的,这是extractps
指令的用例,但可以说不是_mm_extract_ps
内在函数。gcc和clang在优化extractps
时使用*out = get<2>(in)
,尽管MSVC忽略了它,但仍使用shufps + movss。)您显示的两个asm块都只是将xmm0的低32位复制到某个地方,而没有转换为int。您忽略了重要的区别,只展示了无用的部分,它以2种不同的方式(注册或存储)无用地从xmm0中复制了
float
位模式,然后又将其复制回来。 movd
是未经修改的位的纯拷贝,就像movss负载一样。在迫使编译器完全使用
extractps
之后,使用的是编译器的选择。通过寄存器来回执行的延迟比存储/重新加载要低,但是要处理更多的ALU。尝试对punt键入
(float const&)
确实包括从FP到整数的转换,您没有显示。好像我们需要更多的理由来避免指针/引用强制转换进行类型绑定(bind)一样,这的确意味着不同:(float const&)f将整数位模式(来自_mm_extract_ps
)用作int
并将其转换为float
。 ,我将您的代码on the Godbolt compiler explorer放入,以查看您遗漏的内容。
float get1_with_extractps_const(__m128 fmm) {
int f = _mm_extract_ps(fmm, 1);
return (float const&)f;
}
;; from MSVC -O2 -Gv (vectorcall passes __m128 in xmm0)
float get1_with_extractps_const(__m128) PROC ; get1_with_extractps_const, COMDAT
extractps eax, xmm0, 1 ; copy the bit-pattern to eax
movd xmm0, eax ; these 2 insns are an alternative to pxor xmm0,xmm0 + cvtsi2ss xmm0,eax to avoid false deps and zero the upper elements
cvtdq2ps xmm0, xmm0 ; packed conversion is 1 uop
ret 0
GCC通过以下方式进行编译:
get1_with_extractps_const(float __vector(4)): # gcc8.2 -O3 -msse4
extractps eax, xmm0, 1
pxor xmm0, xmm0 ; cvtsi2ss has an output dependency so gcc always does this
cvtsi2ss xmm0, eax ; MSVC's way is probably better for float.
ret
显然,MSVC确实为类型绑定(bind)定义了指针/引用转换的行为。普通ISO C++不会(严格别名UB),其他编译器也不会。使用
memcpy
键入pun或进行 union (C++中的GNU C和MSVC支持扩展)。当然,在这种情况下,将要 vector 的 vector 元素类型化为整数然后返回是可怕的。gcc仅针对
(float &)f
发出有关严格混叠违规的警告。 并且GCC/clang与MSVC一致,仅此版本是类型双关语,而不是通过隐式转换实现float
。 C++很奇怪!float get1_with_extractps_nonconst(__m128 fmm) {
int f = _mm_extract_ps(fmm, 1);
return (float &)f;
}
<source>: In function 'float get_with_extractps_nonconst(__m128)':
<source>:21:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return (float &)f;
^
gcc完全优化了
extractps
。# gcc8.2 -O3 -msse4
get1_with_extractps_nonconst(float __vector(4)):
shufps xmm0, xmm0, 85 ; 0x55 = broadcast element 1 to all elements
ret
Clang使用SSE3
movshdup
将元素1复制到0。(并将元素3复制到2)。但是MSVC没有,这是永远不要使用它的另一个原因:
float get1_with_extractps_nonconst(__m128) PROC
extractps DWORD PTR f$[rsp], xmm0, 1 ; store
movss xmm0, DWORD PTR f$[rsp] ; reload
ret 0
不要为此使用
_mm_extract_ps
您的两个版本都很糟糕,因为这不是的
_mm_extract_ps
或extractps
。 Intel SSE: Why does `_mm_extract_ps` return `int` instead of `float`?寄存器中的
float
与 vector 的低位元素相同。高元素不需要清零。如果这样做的话,您可能想使用insertps
,它可以根据立即数来执行xmm,xmm和零元素。使用
_mm_shuffle_ps
将所需的元素移到寄存器的低位,然后它是标量浮点数。 (并且您可以使用_mm_cvtss_f32
告诉C++编译器)。这应该只编译为shufps xmm0,xmm0,2
,而不能编译为extractps
或任何mov
。template <int i> float get() const {
__m128 tmp = fmm;
if (i) // i=0 means the element is already in place
tmp = _mm_shuffle_ps(tmp,tmp,i); // else shuffle it to the bottom.
return _mm_cvtss_f32(tmp);
}
(我跳过了使用
_MM_SHUFFLE(0,0,0,i)
,因为它等于i
。)如果您的
fmm
是在内存中,而不是在寄存器中,那么希望编译器可以优化shuffle,而不仅仅是movss xmm0, [mem]
。 MSVC 19.14确实做到了这一点,至少对于堆栈情况下的function-arg而言。我没有测试其他编译器,但是clang应该可以设法优化_mm_shuffle_ps
;它非常擅长于通过混洗。测试用例证明可以有效地进行编译
例如一个带有您的函数的非类成员版本的测试用例,以及一个将其内联为特定
i
的调用方:#include <immintrin.h>
template <int i> float get(__m128 input) {
__m128 tmp = input;
if (i) // i=0 means the element is already in place
tmp = _mm_shuffle_ps(tmp,tmp,i); // else shuffle it to the bottom.
return _mm_cvtss_f32(tmp);
}
// MSVC -Gv (vectorcall) passes arg in xmm0
// With plain dumb x64 fastcall, arg is on the stack, and it *does* just MOVSS load without shuffling
float get2(__m128 in) {
return get<2>(in);
}
From the Godbolt compiler explorer,来自MSVC,clang和gcc的asm输出:
;; MSVC -O2 -Gv
float get<2>(__m128) PROC ; get<2>, COMDAT
shufps xmm0, xmm0, 2
ret 0
float get<2>(__m128) ENDP ; get<2>
;; MSVC -O2 (without Gv, so the vector comes from memory)
input$ = 8
float get<2>(__m128) PROC ; get<2>, COMDAT
movss xmm0, DWORD PTR [rcx+8]
ret 0
float get<2>(__m128) ENDP ; get<2>
# gcc8.2 -O3 for x86-64 System V (arg in xmm0)
get2(float __vector(4)):
shufps xmm0, xmm0, 2 # with -msse4, we get unpckhps
ret
# clang7.0 -O3 for x86-64 System V (arg in xmm0)
get2(float __vector(4)):
unpckhpd xmm0, xmm0 # xmm0 = xmm0[1,1]
ret
clang的shuffle优化器简化为
unpckhpd
,在某些旧的CPU上速度更快。不幸的是,它没有注意到它可以使用movhlps xmm0,xmm0
,它也很快并且短了1个字节。
关于c++ - 使用(float&)int可以进行类型修剪,(float const&)int可以像(float)int那样转换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54454585/