I have two vectors of 4 integers each and I'd like to use a SIMD command to compare them (say generate a result vector where each entry is 0 or 1 according to the result of the comparison).
Then, I'd like to compare the result vector to a vector of 4 zeros and only if they're equal do something.
Do you know what SIMD commands I can use to do it?
Quickly check if two STL vectors contain same elements or not. Unlike normal C/C++ arrays, we don’t need to do element by element comparison to find if two given vectors contain same elements or not. In case of vectors, the operator “==” is overloaded to find the result quickly. Below is an example to demonstrate same.
While there's nothing particular wrong about doing two operations to achieve this, we can do better: a SIMD vector allows us to store multiple pieces of data in a single register by splitting our 64 bits of memory into smaller sections.
On the other hand, if similar means "in similar directions", compute the angle between the vectors: (u.v)/ ( |u| |v|). If similarity score means any sensible measure of the length of | x - y |, the vector norms relationship is provide the results. Definition.
Starting from Swift 5, SIMD Vector types that range from 2 to 64 lanes are available for use. This is how the previous character example can be written in Swift using SIMD2<Int32>: let character = SIMD2<Int32>( arrayLiteral: 2, 4) let knockback = SIMD2<Int32>( arrayLiteral: 2, 2) let result: SIMD2<Int32> = character &* knockback // 4,8
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:
vector1
, vector2
, result
all need to be 16 byte aligned{ -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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With