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 int main()
119 {
120     test_insert();
121     test_insert_rp();
122     test_asr_r_svw_trun();
123     test_swiz();
124 
125     puts(err ? "FAIL" : "PASS");
126     return err ? EXIT_FAILURE : EXIT_SUCCESS;
127 }
128