From 457185e5f3fe1c7cc03f08857e37edf59b1604b1 Mon Sep 17 00:00:00 2001 From: Zhang Chi Date: Thu, 5 Oct 2023 15:37:25 +0200 Subject: [PATCH] [APP] add example vector code --- apps/vector_addition/main.c | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 apps/vector_addition/main.c diff --git a/apps/vector_addition/main.c b/apps/vector_addition/main.c new file mode 100644 index 000000000..aaa3c77f8 --- /dev/null +++ b/apps/vector_addition/main.c @@ -0,0 +1,46 @@ +#include +#include + +#ifndef SPIKE +#include "printf.h" +#else +#include "util.h" +#include +#endif + +uint64_t x[10] = {1,2,3,4,5,6,7,8,9,10}; +uint64_t y[10] = {10,9,8,7,6,5,4,3,2,1}; +uint64_t z[10]; + +int main() { + printf("Ariane says Hello!\n"); + + /* + * Example of Using Vector Instruction -- Vector Addition Z = Y + X + */ + + //step1 -- configure the vector length + uint64_t VL = 10; + asm volatile("vsetvli zero, %0, e64, m4, ta, ma" ::"r"(VL)); + + //step2 -- load x to vector register 0 (v0) + asm volatile("vle64.v v0, (%0);" ::"r"(x)); + + //step3 -- load y to vector register 4 (v4) + asm volatile("vle64.v v4, (%0);" ::"r"(y)); + + //step4 -- v0 + v4 -> v8 + asm volatile("vadd.vv v8, v4, v0;"); + + //step5 -- store v8 to z array + asm volatile("vse64.v v8, (%0);" ::"r"(z)); + + + // Check the results (CVA6) + for (int i = 0; i < 10; ++i) + { + printf("%d\n", z[i]); + } + + return 0; +}