xref: /qemu/tests/tcg/hexagon/read_write_overlap.c (revision ab930e80)
1 /*
2  *  Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Test instructions where the semantics write to the destination
20  * before all the operand reads have been completed.
21  *
22  * These instructions are problematic when we short-circuit the
23  * register writes because the destination and source operands could
24  * be the same TCGv.
25  *
26  * We test by forcing the read and write to be register r7.
27  */
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 
33 int err;
34 
35 static void __check(const char *filename, int line, int x, int expect)
36 {
37     if (x != expect) {
38         printf("ERROR %s:%d - 0x%08x != 0x%08x\n",
39                filename, line, x, expect);
40         err++;
41     }
42 }
43 
44 #define check(x, expect) __check(__FILE__, __LINE__, (x), (expect))
45 
46 #define insert(RES, X, WIDTH, OFFSET) \
47     asm("r7 = %1\n\t" \
48         "r7 = insert(r7, #" #WIDTH ", #" #OFFSET ")\n\t" \
49         "%0 = r7\n\t" \
50         : "=r"(RES) : "r"(X) : "r7")
51 
52 static void test_insert(void)
53 {
54     uint32_t res;
55 
56     insert(res, 0x12345678, 8, 1);
57     check(res, 0x123456f0);
58     insert(res, 0x12345678, 0, 1);
59     check(res, 0x12345678);
60     insert(res, 0x12345678, 20, 16);
61     check(res, 0x56785678);
62 }
63 
64 static inline uint32_t insert_rp(uint32_t x, uint32_t width, uint32_t offset)
65 {
66     uint64_t width_offset = (uint64_t)width << 32 | offset;
67     uint32_t res;
68     asm("r7 = %1\n\t"
69         "r7 = insert(r7, %2)\n\t"
70         "%0 = r7\n\t"
71         : "=r"(res) : "r"(x), "r"(width_offset) : "r7");
72     return res;
73 
74 }
75 
76 static void test_insert_rp(void)
77 {
78     check(insert_rp(0x12345678,   8,  1), 0x123456f0);
79     check(insert_rp(0x12345678,  63,  8), 0x34567878);
80     check(insert_rp(0x12345678, 127,  8), 0x34567878);
81     check(insert_rp(0x12345678,   8, 24), 0x78345678);
82     check(insert_rp(0x12345678,   8, 63), 0x12345678);
83     check(insert_rp(0x12345678,   8, 64), 0x00000000);
84 }
85 
86 static inline uint32_t asr_r_svw_trun(uint64_t x, uint32_t y)
87 {
88     uint32_t res;
89     asm("r7 = %2\n\t"
90         "r7 = vasrw(%1, r7)\n\t"
91         "%0 = r7\n\t"
92         : "=r"(res) : "r"(x), "r"(y) : "r7");
93     return res;
94 }
95 
96 static void test_asr_r_svw_trun(void)
97 {
98     check(asr_r_svw_trun(0x1111111122222222ULL, 5),
99           0x88881111);
100     check(asr_r_svw_trun(0x1111111122222222ULL, 63),
101           0x00000000);
102     check(asr_r_svw_trun(0x1111111122222222ULL, 64),
103           0x00000000);
104     check(asr_r_svw_trun(0x1111111122222222ULL, 127),
105           0x22224444);
106     check(asr_r_svw_trun(0x1111111122222222ULL, 128),
107           0x11112222);
108     check(asr_r_svw_trun(0xffffffff22222222ULL, 128),
109           0xffff2222);
110 }
111 
112 static inline uint32_t swiz(uint32_t x)
113 {
114     uint32_t res;
115     asm("r7 = %1\n\t"
116         "r7 = swiz(r7)\n\t"
117         "%0 = r7\n\t"
118         : "=r"(res) : "r"(x) : "r7");
119     return res;
120 }
121 
122 static void test_swiz(void)
123 {
124     check(swiz(0x11223344), 0x44332211);
125 }
126 
127 int main()
128 {
129     test_insert();
130     test_insert_rp();
131     test_asr_r_svw_trun();
132     test_swiz();
133 
134     puts(err ? "FAIL" : "PASS");
135     return err ? EXIT_FAILURE : EXIT_SUCCESS;
136 }
137