xref: /qemu/tests/tcg/hexagon/brev.c (revision 0d57cd61)
1af7f1821STaylor Simpson /*
20d57cd61STaylor Simpson  *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3af7f1821STaylor Simpson  *
4af7f1821STaylor Simpson  *  This program is free software; you can redistribute it and/or modify
5af7f1821STaylor Simpson  *  it under the terms of the GNU General Public License as published by
6af7f1821STaylor Simpson  *  the Free Software Foundation; either version 2 of the License, or
7af7f1821STaylor Simpson  *  (at your option) any later version.
8af7f1821STaylor Simpson  *
9af7f1821STaylor Simpson  *  This program is distributed in the hope that it will be useful,
10af7f1821STaylor Simpson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11af7f1821STaylor Simpson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12af7f1821STaylor Simpson  *  GNU General Public License for more details.
13af7f1821STaylor Simpson  *
14af7f1821STaylor Simpson  *  You should have received a copy of the GNU General Public License
15af7f1821STaylor Simpson  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16af7f1821STaylor Simpson  */
17af7f1821STaylor Simpson 
18af7f1821STaylor Simpson #include <stdio.h>
19af7f1821STaylor Simpson #include <string.h>
200d57cd61STaylor Simpson #include <stdint.h>
21af7f1821STaylor Simpson 
22af7f1821STaylor Simpson int err;
23af7f1821STaylor Simpson 
240d57cd61STaylor Simpson #include "hex_test.h"
250d57cd61STaylor Simpson 
26af7f1821STaylor Simpson #define NBITS          8
27af7f1821STaylor Simpson #define SIZE           (1 << NBITS)
28af7f1821STaylor Simpson 
290d57cd61STaylor Simpson int64_t       dbuf[SIZE] __attribute__((aligned(1 << 16))) = {0};
300d57cd61STaylor Simpson int32_t       wbuf[SIZE] __attribute__((aligned(1 << 16))) = {0};
310d57cd61STaylor Simpson int16_t       hbuf[SIZE] __attribute__((aligned(1 << 16))) = {0};
320d57cd61STaylor Simpson uint8_t       bbuf[SIZE] __attribute__((aligned(1 << 16))) = {0};
33af7f1821STaylor Simpson 
34af7f1821STaylor Simpson /*
35af7f1821STaylor Simpson  * We use the C preporcessor to deal with the combinations of types
36af7f1821STaylor Simpson  */
37af7f1821STaylor Simpson 
38af7f1821STaylor Simpson #define BREV_LOAD(SZ, RES, ADDR, INC) \
39af7f1821STaylor Simpson     __asm__( \
40af7f1821STaylor Simpson         "m0 = %2\n\t" \
41af7f1821STaylor Simpson         "%0 = mem" #SZ "(%1++m0:brev)\n\t" \
42af7f1821STaylor Simpson         : "=r"(RES), "+r"(ADDR) \
43af7f1821STaylor Simpson         : "r"(INC) \
44af7f1821STaylor Simpson         : "m0")
45af7f1821STaylor Simpson 
46af7f1821STaylor Simpson #define BREV_LOAD_b(RES, ADDR, INC) \
47af7f1821STaylor Simpson     BREV_LOAD(b, RES, ADDR, INC)
48af7f1821STaylor Simpson #define BREV_LOAD_ub(RES, ADDR, INC) \
49af7f1821STaylor Simpson     BREV_LOAD(ub, RES, ADDR, INC)
50af7f1821STaylor Simpson #define BREV_LOAD_h(RES, ADDR, INC) \
51af7f1821STaylor Simpson     BREV_LOAD(h, RES, ADDR, INC)
52af7f1821STaylor Simpson #define BREV_LOAD_uh(RES, ADDR, INC) \
53af7f1821STaylor Simpson     BREV_LOAD(uh, RES, ADDR, INC)
54af7f1821STaylor Simpson #define BREV_LOAD_w(RES, ADDR, INC) \
55af7f1821STaylor Simpson     BREV_LOAD(w, RES, ADDR, INC)
56af7f1821STaylor Simpson #define BREV_LOAD_d(RES, ADDR, INC) \
57af7f1821STaylor Simpson     BREV_LOAD(d, RES, ADDR, INC)
58af7f1821STaylor Simpson 
59af7f1821STaylor Simpson #define BREV_STORE(SZ, PART, ADDR, VAL, INC) \
60af7f1821STaylor Simpson     __asm__( \
61af7f1821STaylor Simpson         "m0 = %2\n\t" \
62af7f1821STaylor Simpson         "mem" #SZ "(%0++m0:brev) = %1" PART "\n\t" \
63af7f1821STaylor Simpson         : "+r"(ADDR) \
64af7f1821STaylor Simpson         : "r"(VAL), "r"(INC) \
65af7f1821STaylor Simpson         : "m0", "memory")
66af7f1821STaylor Simpson 
67af7f1821STaylor Simpson #define BREV_STORE_b(ADDR, VAL, INC) \
68af7f1821STaylor Simpson     BREV_STORE(b, "", ADDR, VAL, INC)
69af7f1821STaylor Simpson #define BREV_STORE_h(ADDR, VAL, INC) \
70af7f1821STaylor Simpson     BREV_STORE(h, "", ADDR, VAL, INC)
71af7f1821STaylor Simpson #define BREV_STORE_f(ADDR, VAL, INC) \
72af7f1821STaylor Simpson     BREV_STORE(h, ".H", ADDR, VAL, INC)
73af7f1821STaylor Simpson #define BREV_STORE_w(ADDR, VAL, INC) \
74af7f1821STaylor Simpson     BREV_STORE(w, "", ADDR, VAL, INC)
75af7f1821STaylor Simpson #define BREV_STORE_d(ADDR, VAL, INC) \
76af7f1821STaylor Simpson     BREV_STORE(d, "", ADDR, VAL, INC)
77af7f1821STaylor Simpson 
78af7f1821STaylor Simpson #define BREV_STORE_NEW(SZ, ADDR, VAL, INC) \
79af7f1821STaylor Simpson     __asm__( \
80af7f1821STaylor Simpson         "m0 = %2\n\t" \
81af7f1821STaylor Simpson         "{\n\t" \
82af7f1821STaylor Simpson         "    r5 = %1\n\t" \
83af7f1821STaylor Simpson         "    mem" #SZ "(%0++m0:brev) = r5.new\n\t" \
84af7f1821STaylor Simpson         "}\n\t" \
85af7f1821STaylor Simpson         : "+r"(ADDR) \
86af7f1821STaylor Simpson         : "r"(VAL), "r"(INC) \
87af7f1821STaylor Simpson         : "r5", "m0", "memory")
88af7f1821STaylor Simpson 
89af7f1821STaylor Simpson #define BREV_STORE_bnew(ADDR, VAL, INC) \
90af7f1821STaylor Simpson     BREV_STORE_NEW(b, ADDR, VAL, INC)
91af7f1821STaylor Simpson #define BREV_STORE_hnew(ADDR, VAL, INC) \
92af7f1821STaylor Simpson     BREV_STORE_NEW(h, ADDR, VAL, INC)
93af7f1821STaylor Simpson #define BREV_STORE_wnew(ADDR, VAL, INC) \
94af7f1821STaylor Simpson     BREV_STORE_NEW(w, ADDR, VAL, INC)
95af7f1821STaylor Simpson 
bitreverse(uint32_t x)960d57cd61STaylor Simpson uint32_t bitreverse(uint32_t x)
97af7f1821STaylor Simpson {
980d57cd61STaylor Simpson     uint32_t result = 0;
990d57cd61STaylor Simpson     for (int i = 0; i < NBITS; i++) {
100af7f1821STaylor Simpson         result <<= 1;
101af7f1821STaylor Simpson         result |= x & 1;
102af7f1821STaylor Simpson         x >>= 1;
103af7f1821STaylor Simpson     }
104af7f1821STaylor Simpson     return result;
105af7f1821STaylor Simpson }
106af7f1821STaylor Simpson 
sext8(int32_t x)1070d57cd61STaylor Simpson int32_t sext8(int32_t x)
108af7f1821STaylor Simpson {
109af7f1821STaylor Simpson     return (x << 24) >> 24;
110af7f1821STaylor Simpson }
111af7f1821STaylor Simpson 
112af7f1821STaylor Simpson #define TEST_BREV_LOAD(SZ, TYPE, BUF, SHIFT, EXP) \
113af7f1821STaylor Simpson     do { \
114af7f1821STaylor Simpson         p = BUF; \
1150d57cd61STaylor Simpson         for (int i = 0; i < SIZE; i++) { \
116af7f1821STaylor Simpson             TYPE result; \
117af7f1821STaylor Simpson             BREV_LOAD_##SZ(result, p, 1 << (SHIFT - NBITS)); \
1180d57cd61STaylor Simpson             check32(result, EXP); \
119af7f1821STaylor Simpson         } \
120af7f1821STaylor Simpson     } while (0)
121af7f1821STaylor Simpson 
122af7f1821STaylor Simpson #define TEST_BREV_STORE(SZ, TYPE, BUF, VAL, SHIFT) \
123af7f1821STaylor Simpson     do { \
124af7f1821STaylor Simpson         p = BUF; \
125af7f1821STaylor Simpson         memset(BUF, 0xff, sizeof(BUF)); \
1260d57cd61STaylor Simpson         for (int i = 0; i < SIZE; i++) { \
127af7f1821STaylor Simpson             BREV_STORE_##SZ(p, (TYPE)(VAL), 1 << (SHIFT - NBITS)); \
128af7f1821STaylor Simpson         } \
1290d57cd61STaylor Simpson         for (int i = 0; i < SIZE; i++) { \
1300d57cd61STaylor Simpson             check32(BUF[i], bitreverse(i)); \
131af7f1821STaylor Simpson         } \
132af7f1821STaylor Simpson     } while (0)
133af7f1821STaylor Simpson 
134af7f1821STaylor Simpson #define TEST_BREV_STORE_NEW(SZ, BUF, SHIFT) \
135af7f1821STaylor Simpson     do { \
136af7f1821STaylor Simpson         p = BUF; \
137af7f1821STaylor Simpson         memset(BUF, 0xff, sizeof(BUF)); \
1380d57cd61STaylor Simpson         for (int i = 0; i < SIZE; i++) { \
139af7f1821STaylor Simpson             BREV_STORE_##SZ(p, i, 1 << (SHIFT - NBITS)); \
140af7f1821STaylor Simpson         } \
1410d57cd61STaylor Simpson         for (int i = 0; i < SIZE; i++) { \
1420d57cd61STaylor Simpson             check32(BUF[i], bitreverse(i)); \
143af7f1821STaylor Simpson         } \
144af7f1821STaylor Simpson     } while (0)
145af7f1821STaylor Simpson 
146af7f1821STaylor Simpson /*
147af7f1821STaylor Simpson  * We'll set high_half[i] = i << 16 for use in the .H form of store
148af7f1821STaylor Simpson  * which stores from the high half of the word.
149af7f1821STaylor Simpson  */
150af7f1821STaylor Simpson int high_half[SIZE];
151af7f1821STaylor Simpson 
main()152af7f1821STaylor Simpson int main()
153af7f1821STaylor Simpson {
154af7f1821STaylor Simpson     void *p;
155af7f1821STaylor Simpson 
1560d57cd61STaylor Simpson     for (int i = 0; i < SIZE; i++) {
157af7f1821STaylor Simpson         bbuf[i] = bitreverse(i);
158af7f1821STaylor Simpson         hbuf[i] = bitreverse(i);
159af7f1821STaylor Simpson         wbuf[i] = bitreverse(i);
160af7f1821STaylor Simpson         dbuf[i] = bitreverse(i);
161af7f1821STaylor Simpson         high_half[i] = i << 16;
162af7f1821STaylor Simpson     }
163af7f1821STaylor Simpson 
1640d57cd61STaylor Simpson     TEST_BREV_LOAD(b,  int32_t,   bbuf, 16, sext8(i));
1650d57cd61STaylor Simpson     TEST_BREV_LOAD(ub, int32_t,   bbuf, 16, i);
1660d57cd61STaylor Simpson     TEST_BREV_LOAD(h,  int32_t,   hbuf, 15, i);
1670d57cd61STaylor Simpson     TEST_BREV_LOAD(uh, int32_t,   hbuf, 15, i);
1680d57cd61STaylor Simpson     TEST_BREV_LOAD(w,  int32_t,   wbuf, 14, i);
1690d57cd61STaylor Simpson     TEST_BREV_LOAD(d,  int64_t,   dbuf, 13, i);
170af7f1821STaylor Simpson 
1710d57cd61STaylor Simpson     TEST_BREV_STORE(b, int32_t,   bbuf, i,            16);
1720d57cd61STaylor Simpson     TEST_BREV_STORE(h, int32_t,   hbuf, i,            15);
1730d57cd61STaylor Simpson     TEST_BREV_STORE(f, int32_t,   hbuf, high_half[i], 15);
1740d57cd61STaylor Simpson     TEST_BREV_STORE(w, int32_t,   wbuf, i,            14);
1750d57cd61STaylor Simpson     TEST_BREV_STORE(d, int64_t,   dbuf, i,            13);
176af7f1821STaylor Simpson 
177af7f1821STaylor Simpson     TEST_BREV_STORE_NEW(bnew, bbuf, 16);
178af7f1821STaylor Simpson     TEST_BREV_STORE_NEW(hnew, hbuf, 15);
179af7f1821STaylor Simpson     TEST_BREV_STORE_NEW(wnew, wbuf, 14);
180af7f1821STaylor Simpson 
181af7f1821STaylor Simpson     puts(err ? "FAIL" : "PASS");
182af7f1821STaylor Simpson     return err ? 1 : 0;
183af7f1821STaylor Simpson }
184