master
c 174 lines 5.08 KB
Raw
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 #include "hex_test.h"
36
37 #define insert(RES, X, WIDTH, OFFSET) \
38 asm("r7 = %1\n\t" \
39 "r7 = insert(r7, #" #WIDTH ", #" #OFFSET ")\n\t" \
40 "%0 = r7\n\t" \
41 : "=r"(RES) : "r"(X) : "r7")
42
43 static void test_insert(void)
44 {
45 uint32_t res;
46
47 insert(res, 0x12345678, 8, 1);
48 check32(res, 0x123456f0);
49 insert(res, 0x12345678, 0, 1);
50 check32(res, 0x12345678);
51 insert(res, 0x12345678, 20, 16);
52 check32(res, 0x56785678);
53 }
54
55 static inline uint32_t insert_rp(uint32_t x, uint32_t width, uint32_t offset)
56 {
57 uint64_t width_offset = (uint64_t)width << 32 | offset;
58 uint32_t res;
59 asm("r7 = %1\n\t"
60 "r7 = insert(r7, %2)\n\t"
61 "%0 = r7\n\t"
62 : "=r"(res) : "r"(x), "r"(width_offset) : "r7");
63 return res;
64
65 }
66
67 static void test_insert_rp(void)
68 {
69 check32(insert_rp(0x12345678, 8, 1), 0x123456f0);
70 check32(insert_rp(0x12345678, 63, 8), 0x34567878);
71 check32(insert_rp(0x12345678, 127, 8), 0x34567878);
72 check32(insert_rp(0x12345678, 8, 24), 0x78345678);
73 check32(insert_rp(0x12345678, 8, 63), 0x12345678);
74 check32(insert_rp(0x12345678, 8, 64), 0x00000000);
75 }
76
77 static inline uint32_t asr_r_svw_trun(uint64_t x, uint32_t y)
78 {
79 uint32_t res;
80 asm("r7 = %2\n\t"
81 "r7 = vasrw(%1, r7)\n\t"
82 "%0 = r7\n\t"
83 : "=r"(res) : "r"(x), "r"(y) : "r7");
84 return res;
85 }
86
87 static void test_asr_r_svw_trun(void)
88 {
89 check32(asr_r_svw_trun(0x1111111122222222ULL, 5),
90 0x88881111);
91 check32(asr_r_svw_trun(0x1111111122222222ULL, 63),
92 0x00000000);
93 check32(asr_r_svw_trun(0x1111111122222222ULL, 64),
94 0x00000000);
95 check32(asr_r_svw_trun(0x1111111122222222ULL, 127),
96 0x22224444);
97 check32(asr_r_svw_trun(0x1111111122222222ULL, 128),
98 0x11112222);
99 check32(asr_r_svw_trun(0xffffffff22222222ULL, 128),
100 0xffff2222);
101 }
102
103 static inline uint32_t swiz(uint32_t x)
104 {
105 uint32_t res;
106 asm("r7 = %1\n\t"
107 "r7 = swiz(r7)\n\t"
108 "%0 = r7\n\t"
109 : "=r"(res) : "r"(x) : "r7");
110 return res;
111 }
112
113 static void test_swiz(void)
114 {
115 check32(swiz(0x11223344), 0x44332211);
116 }
117
118 #define CMPY(NAME, ASM) \
119 static inline uint32_t NAME##_rd_eq_rs(uint32_t x, uint32_t y) \
120 { \
121 uint32_t res; \
122 asm("r7 = %1\n\t" \
123 ASM("r7", "%2") "\n\t" \
124 "%0 = r7\n\t" \
125 : "=r"(res) : "r"(x), "r"(y) : "r7"); \
126 return res; \
127 } \
128 static inline uint32_t NAME##_rd_eq_rt(uint32_t x, uint32_t y) \
129 { \
130 uint32_t res; \
131 asm("r7 = %2\n\t" \
132 ASM("%1", "r7") "\n\t" \
133 "%0 = r7\n\t" \
134 : "=r"(res) : "r"(x), "r"(y) : "r7"); \
135 return res; \
136 }
137
138 #define CMPY_RND_SAT(RS, RT) "r7 = cmpy(" RS "," RT "):rnd:sat"
139 #define CMPY_S1_RND_SAT(RS, RT) "r7 = cmpy(" RS "," RT "):<<1:rnd:sat"
140 #define CMPYC_RND_SAT(RS, RT) "r7 = cmpy(" RS "," RT "*):rnd:sat"
141 #define CMPYC_S1_RND_SAT(RS, RT) "r7 = cmpy(" RS "," RT "*):<<1:rnd:sat"
142
143 CMPY(cmpyrs_s0, CMPY_RND_SAT)
144 CMPY(cmpyrs_s1, CMPY_S1_RND_SAT)
145 CMPY(cmpyrsc_s0, CMPYC_RND_SAT)
146 CMPY(cmpyrsc_s1, CMPYC_S1_RND_SAT)
147
148 static void test_cmpy(void)
149 {
150 check32(cmpyrs_s0_rd_eq_rs(0x32195ce2, 0xef862430), 0x011b105b);
151 check32(cmpyrs_s0_rd_eq_rt(0x32195ce2, 0xef862430), 0x011b105b);
152 check32(cmpyrs_s1_rd_eq_rs(0x32195ce2, 0xef862430), 0x023520b5);
153 check32(cmpyrs_s1_rd_eq_rt(0x32195ce2, 0xef862430), 0x023520b5);
154 check32(cmpyrsc_s0_rd_eq_rs(0x32195ce2, 0xef862430), 0x0d0f09e8);
155 check32(cmpyrsc_s0_rd_eq_rt(0x32195ce2, 0xef862430), 0x0d0f09e8);
156 check32(cmpyrsc_s1_rd_eq_rs(0x32195ce2, 0xef862430), 0x1a1f13d0);
157 check32(cmpyrsc_s1_rd_eq_rt(0x32195ce2, 0xef862430), 0x1a1f13d0);
158
159 /* Both halves saturate */
160 check32(cmpyrs_s1_rd_eq_rs(0x80008000, 0x80008000), 0x7fff0000);
161 check32(cmpyrsc_s1_rd_eq_rs(0x7fff8001, 0x80017fff), 0x00008000);
162 }
163
164 int main()
165 {
166 test_insert();
167 test_insert_rp();
168 test_asr_r_svw_trun();
169 test_swiz();
170 test_cmpy();
171
172 puts(err ? "FAIL" : "PASS");
173 return err ? EXIT_FAILURE : EXIT_SUCCESS;
174 }