-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimd_example.c
30 lines (22 loc) · 952 Bytes
/
simd_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <emmintrin.h>
int main()
{
int16_t input[16] __attribute__ ((aligned (16))) = {0, 0, 1, -1, 2, -2, 3, -3}; // ...
int16_t ref[8] __attribute__ ((aligned (16))) = {-1,-1,-1,-1,-1,-1,-1,-1};
int16_t output[8] __attribute__ ((aligned (16)));
// Load the first 128 bits (8 16-bit elements) of the input array into an XMM register
__m128i reg_input = _mm_load_si128((__m128i*)input);
// Load the 8 reference elements for comparison
__m128i reg_ref = _mm_load_si128((__m128i*)ref);
// Compare 8 input elements to reference elements (input[i] > ref[i])
__m128i reg_output = _mm_cmpgt_epi16(reg_input, reg_ref);
// Store the output register to memory
_mm_store_si128((__m128i*)output, reg_output);
for (int i = 0; i < 8; ++i)
printf("%d ", input[i]);
printf("\n");
for (int i = 0; i < 8; ++i)
printf("%d ", output[i]);
return 0;
}