From ce916d2fe7c5b6a473cac985c60444d145cca338 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:06 +0800 Subject: [PATCH] ADD file via upload --- Yang/2.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Yang/2.c diff --git a/Yang/2.c b/Yang/2.c new file mode 100644 index 0000000..900a1d6 --- /dev/null +++ b/Yang/2.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +void vectorAddNEON(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i <= size - 4; i += 4) { + float32x4_t vecA = vld1q_f32(&A[i]); + float32x4_t vecB = vld1q_f32(&B[i]); + + float32x4_t vecC = vaddq_f32(vecA, vecB); + vst1q_f32(&C[i], vecC); + } + 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)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + srand((unsigned)time(NULL)); + for (int i = 0; i < SIZE; ++i) { + A[i] = (float)(rand() % 100); + B[i] = (float)(rand() % 100); + } + + clock_t start_time = clock(); + vectorAdd(A, B, C, SIZE); + clock_t end_time = clock(); + double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("tradition: %f", time1); + + start_time = clock(); + vectorAddNEON(A, B, C, SIZE); + end_time = clock(); + time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("NEON %f",time1); + + free(A); + free(B); + free(C); +}