xref: /openbmc/qemu/tests/tcg/cris/libc/check_swap.c (revision 7c08eefc)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "sys.h"
5 #include "crisutils.h"
6 
7 #define N 8
8 #define W 4
9 #define B 2
10 #define R 1
11 
12 static always_inline int cris_swap(const int mode, int x)
13 {
14 	switch (mode)
15 	{
16 		case N: asm ("swapn\t%0\n" : "+r" (x) : "0" (x)); break;
17 		case W: asm ("swapw\t%0\n" : "+r" (x) : "0" (x)); break;
18 		case B: asm ("swapb\t%0\n" : "+r" (x) : "0" (x)); break;
19 		case R: asm ("swapr\t%0\n" : "+r" (x) : "0" (x)); break;
20 		case B|R: asm ("swapbr\t%0\n" : "+r" (x) : "0" (x)); break;
21 		case W|R: asm ("swapwr\t%0\n" : "+r" (x) : "0" (x)); break;
22 		case W|B: asm ("swapwb\t%0\n" : "+r" (x) : "0" (x)); break;
23 		case W|B|R: asm ("swapwbr\t%0\n" : "+r" (x) : "0" (x)); break;
24 		case N|R: asm ("swapnr\t%0\n" : "+r" (x) : "0" (x)); break;
25 		case N|B: asm ("swapnb\t%0\n" : "+r" (x) : "0" (x)); break;
26 		case N|B|R: asm ("swapnbr\t%0\n" : "+r" (x) : "0" (x)); break;
27 		case N|W: asm ("swapnw\t%0\n" : "+r" (x) : "0" (x)); break;
28 		default:
29 			err();
30 			break;
31 	}
32 	return x;
33 }
34 
35 /* Made this a macro to be able to pick up the location of the errors.  */
36 #define verify_swap(mode, val, expected, n, z)          \
37 do {                                                    \
38         int r;                                          \
39         cris_tst_cc_init();                             \
40 	r = cris_swap(mode, val);                       \
41         cris_tst_mov_cc(n, z);                          \
42 	if (r != expected)                              \
43 		err();                                  \
44 } while(0)
45 
46 void check_swap(void)
47 {
48 	/* Some of these numbers are borrowed from GDB's cris sim
49 	   testsuite.  */
50 	if (cris_swap(N, 0) != 0xffffffff)
51 		err();
52 	if (cris_swap(W, 0x12345678) != 0x56781234)
53 		err();
54 	if (cris_swap(B, 0x12345678) != 0x34127856)
55 		err();
56 
57 	verify_swap(R, 0x78134452, 0x1ec8224a, 0, 0);
58 	verify_swap(B, 0x78134452, 0x13785244, 0, 0);
59 	verify_swap(B|R, 0x78134452, 0xc81e4a22, 1, 0);
60 	verify_swap(W, 0x78134452, 0x44527813, 0, 0);
61 	verify_swap(W|R, 0x78134452, 0x224a1ec8, 0, 0);
62 	verify_swap(W|B|R, 0x78134452, 0x4a22c81e, 0, 0);
63 	verify_swap(N, 0x78134452, 0x87ecbbad, 1, 0);
64 	verify_swap(N|R, 0x78134452, 0xe137ddb5, 1, 0);
65 	verify_swap(N|B, 0x78134452, 0xec87adbb, 1, 0);
66 	verify_swap(N|B|R, 0x78134452, 0x37e1b5dd, 0, 0);
67 	verify_swap(N|W, 0x78134452, 0xbbad87ec, 1, 0);
68 	verify_swap(N|B|R, 0xffffffff, 0, 0, 1);
69 }
70 
71 int main(void)
72 {
73 	check_swap();
74 	pass();
75 	return 0;
76 }
77