You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
#include <arm_neon.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#define SIZE 1024
|
|
void vector_add(float* A, float* B, float* C, int size) {
|
|
for (int i = 0; i < size; ++i) {
|
|
C[i] = A[i] + B[i];
|
|
}
|
|
}
|
|
|
|
void vector_add_optimized(float* A, float* B, float* C, int size) {
|
|
int i;
|
|
for (i = 0; i < size; i += 4) {
|
|
|
|
float32x4_t a = vld1q_f32(A + i);
|
|
float32x4_t b = vld1q_f32(B + i);
|
|
float32x4_t c = vaddq_f32(a, b);
|
|
vst1q_f32(C + i, c);
|
|
}
|
|
|
|
for (; i < size; ++i) {
|
|
C[i] = A[i] + B[i];
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
|
|
float *A = (float*)malloc(SIZE * sizeof(float));
|
|
float *B = (float*)malloc(SIZE * sizeof(float));
|
|
float *C = (float*)malloc(SIZE * sizeof(float));
|
|
float *C_optimized = (float*)malloc(SIZE * sizeof(float));
|
|
|
|
|
|
for (int i = 0; i < SIZE; ++i) {
|
|
A[i] = rand() % 100;
|
|
B[i] = rand() % 100;
|
|
}
|
|
|
|
|
|
clock_t start = clock();
|
|
vector_add(A, B, C, SIZE);
|
|
clock_t end = clock();
|
|
double base_time = (double)(end - start) / CLOCKS_PER_SEC;
|
|
|
|
|
|
start = clock();
|
|
vector_add_optimized(A, B, C_optimized, SIZE);
|
|
end = clock();
|
|
double optimized_time = (double)(end - start) / CLOCKS_PER_SEC;
|
|
|
|
|
|
printf("base: %f s\n", base_time);
|
|
printf("NEON: %f s\n", optimized_time);
|
|
|
|
|
|
free(A);
|
|
free(B);
|
|
free(C);
|
|
free(C_optimized);
|
|
|
|
return 0;
|
|
}
|