How to compare two vectors using SIMD and get a single boolean result?

£可爱£侵袭症+ 提交于 2019-12-03 00:52:51

To compare two SIMD vectors:

#include <stdint.h>
#include <xmmintrin.h>

int32_t __attribute__ ((aligned(16))) vector1[4] = { 1, 2, 3, 4 };
int32_t __attribute__ ((aligned(16))) vector2[4] = { 1, 2, 2, 2 };
int32_t __attribute__ ((aligned(16))) result[4];

__m128i v1 = _mm_load_si128((__m128i *)vector1);
__m128i v2 = _mm_load_si128((__m128i *)vector2);
__m128i vcmp = _mm_cmpeq_epi32(v1, v2);
_mm_store_si128((__m128i *)result, vcmp);

Notes:

  • data is assumed to be 32 bit integers
  • vector1, vector2, result all need to be 16 byte aligned
  • result will be -1 for equal, 0 for not equal ({ -1, -1, 0, 0 } for above code example)

UPDATE

If you just want a single Boolean result for the case where all 4 elements match then you can do it like this:

#include <stdint.h>
#include <xmmintrin.h>

int32_t __attribute__ ((aligned(16))) vector1[4] = { 1, 2, 3, 4 };
int32_t __attribute__ ((aligned(16))) vector2[4] = { 1, 2, 2, 2 };

__m128i v1 = _mm_load_si128((__m128i *)vector1);
__m128i v2 = _mm_load_si128((__m128i *)vector2);
__m128i vcmp = _mm_cmpeq_epi32(v1, v2);
uint16_t mask = _mm_movemask_epi8(vcmp);
int result = (mask == 0xffff);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!