在float
中,floor()
似乎比int()
更容易,例如:
float z = floor(LOG2EF * x + 0.5f);
const int32_t n = int32_t(z);
成为:
__m128 z = _mm_add_ps(_mm_mul_ps(log2ef, x), half);
__m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(z));
z = _mm_sub_ps(t, _mm_and_ps(_mm_cmplt_ps(z, t), one));
__m128i n = _mm_cvtps_epi32(z);
但是如何使用 only SSE2 在 double
中实现这一点?
这是我要转换的双重版本:
double z = floor(LOG2E * x + 0.5);
const int32_t n = int32_t(z);
最佳答案
只需使用您的单精度 (... ps...
) 内在的:
__m128i n = _mm_cvtpd_epi32(z);
根据 Intel Intrinsics Guide,该内在函数确实可用于 SSE2:https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=4966,1917&techs=SSE2
__m128i _mm_cvtpd_epi32 (__m128d a)
Convert packed double-precision (64-bit) floating-point elements in
a
to packed 32-bit integers, and store the results indst
.FOR j := 0 to 1 i := 32*j k := 64*j dst[i+31:i] := Convert_FP64_To_Int32(a[k+63:k]) ENDFOR
关于c++ - 如何仅使用 SSE2 对 double/int 进行 floor/int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406161/