c++ - 如何仅使用 SSE2 对 double/int 进行 floor/int?

标签 c++ simd truncate intrinsics sse2

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 in dst.

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/

相关文章:

rust - 默认发布版本是否始终使用 SSSE3 指令?

c++ - 销毁具有指针值的 map 的正确方法

c++ - 在 C++ 中将 uint8_t 数组转换为字符串

c - 仅保留 16 位字中的 10 个有用位

javascript - 如何使用 jQuery chop 文本但保留 HTML 格式?

string - 如何获取要在 Tkx 标签中使用的字符串的 "width"

ios - 如何检查 UILabel 是否被截断?

C++ 无效的指针值

c++ - 当调用刚体树中的函数(如 doKinematics、CreateKinematicCache)时,如何修复 cpp 中的 "undefined reference to"错误

arrays - 向量化访问非连续内存位置的循环