-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathvector-add.cpp
More file actions
36 lines (31 loc) · 955 Bytes
/
Copy pathvector-add.cpp
File metadata and controls
36 lines (31 loc) · 955 Bytes
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
31
32
33
34
35
36
#include <simdpp/simd.h>
#include <iostream>
// Initializes vector to store values
void init_vector(float* a, float* b, size_t size) {
for (int i=0; i<size; i++) {
a[i] = i;
b[i] = size - i - 1;
}
}
// Test result of SIMD operations on vector
void test_result(float* result, size_t size) {
for (int i=0; i<size; i++) {
assert(result[i] == size - 1);
}
}
using namespace simdpp;
int main() {
const size_t SIZE = 1024;
float vec_a[SIZE];
float vec_b[SIZE];
float result[SIZE];
init_vector(vec_a, vec_b, SIZE);
for (int i=0; i<SIZE; i+=4) {
float32<4> xmmA = load(vec_a + i); //loads 4 floats into xmmA
float32<4> xmmB = load(vec_b + i); //loads 4 floats into xmmB
float32<4> xmmC = add(xmmA, xmmB); //Vector add of xmmA and xmmB
store(result + i, xmmC); //Store result into the vector
}
test_result(result, SIZE);
return 0;
}