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.

49 lines
1008 B

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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 C_vec = vaddq_f32(A_vec, B_vec);
vst1q_f32(C + i, C_vec);
}
for (; i < size; i++) {
C[i] = A[i] + B[i];
}
}
int main() {
float A[SIZE], B[SIZE], C[SIZE];
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
A[i] = rand() % 100;
B[i] = rand() % 100;
}
clock_t start, end;
start = clock();
vector_add_optimized(A, B, C, SIZE);
end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("NEON优化向量加法的运行时间为: %lf 秒\n", time_spent);
return 0;
}