Is the RVV used correctly?

Is RVV used correctly in this way? If so, are there any suggestions for code implementation?

testcase:

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<riscv_vector.h>

int main(){

int input1[5] = {0,1,2,4,6};

int input2[5] = {0,1,2,4,6};

vint32m1_t *a1 = (vint32m1_t *)input1;

vint32m1_t *a2 = (vint32m1_t *)input2;

vint32m1_t bb;

int vl = vsetvl_e32m1(10000);

bb = vadd_vv_i32m1( *a1, *a2, vl);

for (int i=0;i<5;i++)

printf("%d\n",bb[i]);

return 1;

}

error:

clang --target=riscv64-unknown-linux-gnu -march=rv64gcv0p10 -menable-experimental-extensions --sysroot=xxx --gcc-toolchain=xxx test.c

Error:

test.c:14:20: error: subscripted value is not an array, pointer, or vector

printf("%d\n",bb[i]);

We don’t support the subscript operator directly on RVV vectors because we don’t know how many elements can fit in a register. The *a1 and *a2 will try to read an entire register worth of data, but there is no guarantee that 5 ints will fit in a register. It might only fit 4. You’ll need an outer loop to handle that case. A more correct version looks something like this

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<riscv_vector.h>

int main(){
int input1[5] = {0,1,2,4,6};
int input2[5] = {0,1,2,4,6};
int *input1ptr = input1;
int *input2ptr = input2;

int elts = 5;
do {
// Ask hardware how many elements we can read at once.
int vl = vsetvl_e32m1(5);
vint32m1_t a1 = vle32_v_i32m1(input1ptr, vl);
vint32m1_t a2 = vle32_v_i32m1(input2ptr, vl);
vint32m1_t bb = vadd_vv_i32m1(a1, a2, vl);

for (int i=0;i<vl;i++) {
// Shift element to lsbs.
vint32m1_t shift = vslidedown_vx_i32m1(vundefined_i32m1(), bb, i, vl);
// Extract element 0 and print.
printf("%d\n",vmv_x_s_i32m1_i32(shift));
}

// Decrement elts by number read;
elts -= vl;
// Increment by pointers by the number read.
input1ptr += vl;
input2ptr += vl;
} while (elts != 0);

return 1;

}