|
|
|
@ -0,0 +1,58 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
|
|
#include <arm_neon.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define SIZE 1024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void vector_add_optimized(float* A, float* B, float* C, int size) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < size - 3; i += 4) {
|
|
|
|
|
|
|
|
|
|
float32x4_t a_vec = vld1q_f32(&A[i]);
|
|
|
|
|
float32x4_t b_vec = vld1q_f32(&B[i]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float32x4_t result_vec = vaddq_f32(a_vec, b_vec);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vst1q_f32(&C[i], result_vec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; i < size; i++) {
|
|
|
|
|
C[i] = A[i] + B[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
|
|
float A[SIZE];
|
|
|
|
|
float B[SIZE];
|
|
|
|
|
float C[SIZE];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
|
|
A[i] = (float)(rand() % 100);
|
|
|
|
|
B[i] = (float)(rand() % 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clock_t start_time, end_time;
|
|
|
|
|
|
|
|
|
|
start_time = clock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector_add_optimized(A, B, C, SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_time = clock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
|
|
|
|
|
printf("NEON优化向量加法的运行时间:%f 秒\n", elapsed_time);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|