xref: /linux/tools/testing/selftests/riscv/vector/v_initval_nolibc.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "../../kselftest.h"
4 #define MAX_VSIZE	(8192 * 32)
5 
6 void dump(char *ptr, int size)
7 {
8 	int i = 0;
9 
10 	for (i = 0; i < size; i++) {
11 		if (i != 0) {
12 			if (i % 16 == 0)
13 				printf("\n");
14 			else if (i % 8 == 0)
15 				printf("  ");
16 		}
17 		printf("%02x ", ptr[i]);
18 	}
19 	printf("\n");
20 }
21 
22 int main(void)
23 {
24 	int i;
25 	unsigned long vl;
26 	char *datap, *tmp;
27 
28 	datap = malloc(MAX_VSIZE);
29 	if (!datap) {
30 		ksft_test_result_fail("fail to allocate memory for size = %d\n", MAX_VSIZE);
31 		exit(-1);
32 	}
33 
34 	tmp = datap;
35 	asm volatile (
36 		".option push\n\t"
37 		".option arch, +v\n\t"
38 		"vsetvli	%0, x0, e8, m8, ta, ma\n\t"
39 		"vse8.v		v0, (%2)\n\t"
40 		"add		%1, %2, %0\n\t"
41 		"vse8.v		v8, (%1)\n\t"
42 		"add		%1, %1, %0\n\t"
43 		"vse8.v		v16, (%1)\n\t"
44 		"add		%1, %1, %0\n\t"
45 		"vse8.v		v24, (%1)\n\t"
46 		".option pop\n\t"
47 		: "=&r" (vl), "=r" (tmp) : "r" (datap) : "memory");
48 
49 	ksft_print_msg("vl = %lu\n", vl);
50 
51 	if (datap[0] != 0x00 && datap[0] != 0xff) {
52 		ksft_test_result_fail("v-regesters are not properly initialized\n");
53 		dump(datap, vl * 4);
54 		exit(-1);
55 	}
56 
57 	for (i = 1; i < vl * 4; i++) {
58 		if (datap[i] != datap[0]) {
59 			ksft_test_result_fail("detect stale values on v-regesters\n");
60 			dump(datap, vl * 4);
61 			exit(-2);
62 		}
63 	}
64 
65 	free(datap);
66 	ksft_exit_pass();
67 	return 0;
68 }
69