xref: /openbmc/qemu/target/sparc/vis_helper.c (revision a859602c746baf4892cc8ca1ce003e92411d1716)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * VIS op helpers
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
95650b549SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
23fcf5ef2aSThomas Huth 
24fcf5ef2aSThomas Huth /* This function uses non-native bit order */
25fcf5ef2aSThomas Huth #define GET_FIELD(X, FROM, TO)                                  \
26fcf5ef2aSThomas Huth     ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))
27fcf5ef2aSThomas Huth 
28fcf5ef2aSThomas Huth /* This function uses the order in the manuals, i.e. bit 0 is 2^0 */
29fcf5ef2aSThomas Huth #define GET_FIELD_SP(X, FROM, TO)               \
30fcf5ef2aSThomas Huth     GET_FIELD(X, 63 - (TO), 63 - (FROM))
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
33fcf5ef2aSThomas Huth {
34fcf5ef2aSThomas Huth     return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
35fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
36fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
37fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
38fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
39fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
40fcf5ef2aSThomas Huth         (((pixel_addr >> 55) & 1) << 4) |
41fcf5ef2aSThomas Huth         (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
42fcf5ef2aSThomas Huth         GET_FIELD_SP(pixel_addr, 11, 12);
43fcf5ef2aSThomas Huth }
44fcf5ef2aSThomas Huth 
45e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
46fcf5ef2aSThomas Huth #define VIS_B64(n) b[7 - (n)]
47fcf5ef2aSThomas Huth #define VIS_W64(n) w[3 - (n)]
48fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[3 - (n)]
49fcf5ef2aSThomas Huth #define VIS_L64(n) l[1 - (n)]
50fcf5ef2aSThomas Huth #define VIS_B32(n) b[3 - (n)]
51fcf5ef2aSThomas Huth #define VIS_W32(n) w[1 - (n)]
52fcf5ef2aSThomas Huth #else
53fcf5ef2aSThomas Huth #define VIS_B64(n) b[n]
54fcf5ef2aSThomas Huth #define VIS_W64(n) w[n]
55fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[n]
56fcf5ef2aSThomas Huth #define VIS_L64(n) l[n]
57fcf5ef2aSThomas Huth #define VIS_B32(n) b[n]
58fcf5ef2aSThomas Huth #define VIS_W32(n) w[n]
59fcf5ef2aSThomas Huth #endif
60fcf5ef2aSThomas Huth 
61fcf5ef2aSThomas Huth typedef union {
62fcf5ef2aSThomas Huth     uint8_t b[8];
63fcf5ef2aSThomas Huth     uint16_t w[4];
64fcf5ef2aSThomas Huth     int16_t sw[4];
65fcf5ef2aSThomas Huth     uint32_t l[2];
66fcf5ef2aSThomas Huth     uint64_t ll;
67fcf5ef2aSThomas Huth     float64 d;
68fcf5ef2aSThomas Huth } VIS64;
69fcf5ef2aSThomas Huth 
70fcf5ef2aSThomas Huth typedef union {
71fcf5ef2aSThomas Huth     uint8_t b[4];
72fcf5ef2aSThomas Huth     uint16_t w[2];
73fcf5ef2aSThomas Huth     uint32_t l;
74fcf5ef2aSThomas Huth     float32 f;
75fcf5ef2aSThomas Huth } VIS32;
76fcf5ef2aSThomas Huth 
77fcf5ef2aSThomas Huth uint64_t helper_fpmerge(uint64_t src1, uint64_t src2)
78fcf5ef2aSThomas Huth {
79fcf5ef2aSThomas Huth     VIS64 s, d;
80fcf5ef2aSThomas Huth 
81fcf5ef2aSThomas Huth     s.ll = src1;
82fcf5ef2aSThomas Huth     d.ll = src2;
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth     /* Reverse calculation order to handle overlap */
85fcf5ef2aSThomas Huth     d.VIS_B64(7) = s.VIS_B64(3);
86fcf5ef2aSThomas Huth     d.VIS_B64(6) = d.VIS_B64(3);
87fcf5ef2aSThomas Huth     d.VIS_B64(5) = s.VIS_B64(2);
88fcf5ef2aSThomas Huth     d.VIS_B64(4) = d.VIS_B64(2);
89fcf5ef2aSThomas Huth     d.VIS_B64(3) = s.VIS_B64(1);
90fcf5ef2aSThomas Huth     d.VIS_B64(2) = d.VIS_B64(1);
91fcf5ef2aSThomas Huth     d.VIS_B64(1) = s.VIS_B64(0);
92fcf5ef2aSThomas Huth     /* d.VIS_B64(0) = d.VIS_B64(0); */
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth     return d.ll;
95fcf5ef2aSThomas Huth }
96fcf5ef2aSThomas Huth 
979157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2)
98fcf5ef2aSThomas Huth {
999157dcccSRichard Henderson     VIS64 d;
1009157dcccSRichard Henderson     VIS32 s;
101fcf5ef2aSThomas Huth     uint32_t tmp;
102fcf5ef2aSThomas Huth 
1039157dcccSRichard Henderson     s.l = src1;
104fcf5ef2aSThomas Huth     d.ll = src2;
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth #define PMUL(r)                                                 \
1079157dcccSRichard Henderson     tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B32(r);       \
108fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                  \
109fcf5ef2aSThomas Huth         tmp += 0x100;                                           \
110fcf5ef2aSThomas Huth     }                                                           \
111fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
112fcf5ef2aSThomas Huth 
113fcf5ef2aSThomas Huth     PMUL(0);
114fcf5ef2aSThomas Huth     PMUL(1);
115fcf5ef2aSThomas Huth     PMUL(2);
116fcf5ef2aSThomas Huth     PMUL(3);
117fcf5ef2aSThomas Huth #undef PMUL
118fcf5ef2aSThomas Huth 
119fcf5ef2aSThomas Huth     return d.ll;
120fcf5ef2aSThomas Huth }
121fcf5ef2aSThomas Huth 
122*a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2)
123fcf5ef2aSThomas Huth {
124*a859602cSRichard Henderson     VIS32 s;
125*a859602cSRichard Henderson     VIS64 d;
126fcf5ef2aSThomas Huth     uint32_t tmp;
127fcf5ef2aSThomas Huth 
128*a859602cSRichard Henderson     s.l = src1;
129*a859602cSRichard Henderson     d.ll = 0;
130fcf5ef2aSThomas Huth 
131fcf5ef2aSThomas Huth #define PMUL(r)                                \
132*a859602cSRichard Henderson     do {                                       \
133*a859602cSRichard Henderson         tmp = src2 * (int32_t)s.VIS_B32(r);    \
134fcf5ef2aSThomas Huth         if ((tmp & 0xff) > 0x7f) {             \
135fcf5ef2aSThomas Huth             tmp += 0x100;                      \
136fcf5ef2aSThomas Huth         }                                      \
137*a859602cSRichard Henderson         d.VIS_W64(r) = tmp >> 8;               \
138*a859602cSRichard Henderson     } while (0)
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth     PMUL(0);
141fcf5ef2aSThomas Huth     PMUL(1);
142fcf5ef2aSThomas Huth     PMUL(2);
143fcf5ef2aSThomas Huth     PMUL(3);
144fcf5ef2aSThomas Huth #undef PMUL
145fcf5ef2aSThomas Huth 
146fcf5ef2aSThomas Huth     return d.ll;
147fcf5ef2aSThomas Huth }
148fcf5ef2aSThomas Huth 
149fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2)
150fcf5ef2aSThomas Huth {
151fcf5ef2aSThomas Huth     VIS64 s, d;
152fcf5ef2aSThomas Huth     uint32_t tmp;
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth     s.ll = src1;
155fcf5ef2aSThomas Huth     d.ll = src2;
156fcf5ef2aSThomas Huth 
157fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
158fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
159fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
160fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
161fcf5ef2aSThomas Huth     }                                                                   \
162fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
163fcf5ef2aSThomas Huth 
164fcf5ef2aSThomas Huth     PMUL(0);
165fcf5ef2aSThomas Huth     PMUL(1);
166fcf5ef2aSThomas Huth     PMUL(2);
167fcf5ef2aSThomas Huth     PMUL(3);
168fcf5ef2aSThomas Huth #undef PMUL
169fcf5ef2aSThomas Huth 
170fcf5ef2aSThomas Huth     return d.ll;
171fcf5ef2aSThomas Huth }
172fcf5ef2aSThomas Huth 
173fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2)
174fcf5ef2aSThomas Huth {
175fcf5ef2aSThomas Huth     VIS64 s, d;
176fcf5ef2aSThomas Huth     uint32_t tmp;
177fcf5ef2aSThomas Huth 
178fcf5ef2aSThomas Huth     s.ll = src1;
179fcf5ef2aSThomas Huth     d.ll = src2;
180fcf5ef2aSThomas Huth 
181fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
182fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
183fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
184fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
185fcf5ef2aSThomas Huth     }                                                                   \
186fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
187fcf5ef2aSThomas Huth 
188fcf5ef2aSThomas Huth     PMUL(0);
189fcf5ef2aSThomas Huth     PMUL(1);
190fcf5ef2aSThomas Huth     PMUL(2);
191fcf5ef2aSThomas Huth     PMUL(3);
192fcf5ef2aSThomas Huth #undef PMUL
193fcf5ef2aSThomas Huth 
194fcf5ef2aSThomas Huth     return d.ll;
195fcf5ef2aSThomas Huth }
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth uint64_t helper_fmuld8sux16(uint64_t src1, uint64_t src2)
198fcf5ef2aSThomas Huth {
199fcf5ef2aSThomas Huth     VIS64 s, d;
200fcf5ef2aSThomas Huth     uint32_t tmp;
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth     s.ll = src1;
203fcf5ef2aSThomas Huth     d.ll = src2;
204fcf5ef2aSThomas Huth 
205fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
206fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
207fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
208fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
209fcf5ef2aSThomas Huth     }                                                                   \
210fcf5ef2aSThomas Huth     d.VIS_L64(r) = tmp;
211fcf5ef2aSThomas Huth 
212fcf5ef2aSThomas Huth     /* Reverse calculation order to handle overlap */
213fcf5ef2aSThomas Huth     PMUL(1);
214fcf5ef2aSThomas Huth     PMUL(0);
215fcf5ef2aSThomas Huth #undef PMUL
216fcf5ef2aSThomas Huth 
217fcf5ef2aSThomas Huth     return d.ll;
218fcf5ef2aSThomas Huth }
219fcf5ef2aSThomas Huth 
220fcf5ef2aSThomas Huth uint64_t helper_fmuld8ulx16(uint64_t src1, uint64_t src2)
221fcf5ef2aSThomas Huth {
222fcf5ef2aSThomas Huth     VIS64 s, d;
223fcf5ef2aSThomas Huth     uint32_t tmp;
224fcf5ef2aSThomas Huth 
225fcf5ef2aSThomas Huth     s.ll = src1;
226fcf5ef2aSThomas Huth     d.ll = src2;
227fcf5ef2aSThomas Huth 
228fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
229fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
230fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
231fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
232fcf5ef2aSThomas Huth     }                                                                   \
233fcf5ef2aSThomas Huth     d.VIS_L64(r) = tmp;
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth     /* Reverse calculation order to handle overlap */
236fcf5ef2aSThomas Huth     PMUL(1);
237fcf5ef2aSThomas Huth     PMUL(0);
238fcf5ef2aSThomas Huth #undef PMUL
239fcf5ef2aSThomas Huth 
240fcf5ef2aSThomas Huth     return d.ll;
241fcf5ef2aSThomas Huth }
242fcf5ef2aSThomas Huth 
2437b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2)
244fcf5ef2aSThomas Huth {
245fcf5ef2aSThomas Huth     VIS32 s;
246fcf5ef2aSThomas Huth     VIS64 d;
247fcf5ef2aSThomas Huth 
2487b616f36SRichard Henderson     s.l = src2;
2497b616f36SRichard Henderson     d.ll = 0;
250fcf5ef2aSThomas Huth     d.VIS_W64(0) = s.VIS_B32(0) << 4;
251fcf5ef2aSThomas Huth     d.VIS_W64(1) = s.VIS_B32(1) << 4;
252fcf5ef2aSThomas Huth     d.VIS_W64(2) = s.VIS_B32(2) << 4;
253fcf5ef2aSThomas Huth     d.VIS_W64(3) = s.VIS_B32(3) << 4;
254fcf5ef2aSThomas Huth 
255fcf5ef2aSThomas Huth     return d.ll;
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth #define VIS_CMPHELPER(name, F)                                    \
259fcf5ef2aSThomas Huth     uint64_t name##16(uint64_t src1, uint64_t src2)               \
260fcf5ef2aSThomas Huth     {                                                             \
261fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
262fcf5ef2aSThomas Huth                                                                   \
263fcf5ef2aSThomas Huth         s.ll = src1;                                              \
264fcf5ef2aSThomas Huth         d.ll = src2;                                              \
265fcf5ef2aSThomas Huth                                                                   \
266fcf5ef2aSThomas Huth         d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0;     \
267fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0;    \
268fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0;    \
269fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0;    \
270fcf5ef2aSThomas Huth         d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0;           \
271fcf5ef2aSThomas Huth                                                                   \
272fcf5ef2aSThomas Huth         return d.ll;                                              \
273fcf5ef2aSThomas Huth     }                                                             \
274fcf5ef2aSThomas Huth                                                                   \
275fcf5ef2aSThomas Huth     uint64_t name##32(uint64_t src1, uint64_t src2)               \
276fcf5ef2aSThomas Huth     {                                                             \
277fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
278fcf5ef2aSThomas Huth                                                                   \
279fcf5ef2aSThomas Huth         s.ll = src1;                                              \
280fcf5ef2aSThomas Huth         d.ll = src2;                                              \
281fcf5ef2aSThomas Huth                                                                   \
282fcf5ef2aSThomas Huth         d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0;     \
283fcf5ef2aSThomas Huth         d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0;    \
284fcf5ef2aSThomas Huth         d.VIS_L64(1) = 0;                                         \
285fcf5ef2aSThomas Huth                                                                   \
286fcf5ef2aSThomas Huth         return d.ll;                                              \
287fcf5ef2aSThomas Huth     }
288fcf5ef2aSThomas Huth 
289fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b))
290fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b))
291fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b))
292fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b))
293fcf5ef2aSThomas Huth 
294fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
295fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
296fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE)
297fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE)
298fcf5ef2aSThomas Huth 
299fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2)
300fcf5ef2aSThomas Huth {
301fcf5ef2aSThomas Huth     int i;
302fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
303fcf5ef2aSThomas Huth         int s1, s2;
304fcf5ef2aSThomas Huth 
305fcf5ef2aSThomas Huth         s1 = (src1 >> (56 - (i * 8))) & 0xff;
306fcf5ef2aSThomas Huth         s2 = (src2 >> (56 - (i * 8))) & 0xff;
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth         /* Absolute value of difference. */
309fcf5ef2aSThomas Huth         s1 -= s2;
310fcf5ef2aSThomas Huth         if (s1 < 0) {
311fcf5ef2aSThomas Huth             s1 = -s1;
312fcf5ef2aSThomas Huth         }
313fcf5ef2aSThomas Huth 
314fcf5ef2aSThomas Huth         sum += s1;
315fcf5ef2aSThomas Huth     }
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth     return sum;
318fcf5ef2aSThomas Huth }
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2)
321fcf5ef2aSThomas Huth {
322fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0xf;
323fcf5ef2aSThomas Huth     uint32_t ret = 0;
324fcf5ef2aSThomas Huth     int byte;
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth     for (byte = 0; byte < 4; byte++) {
327fcf5ef2aSThomas Huth         uint32_t val;
328fcf5ef2aSThomas Huth         int16_t src = rs2 >> (byte * 16);
329fcf5ef2aSThomas Huth         int32_t scaled = src << scale;
330fcf5ef2aSThomas Huth         int32_t from_fixed = scaled >> 7;
331fcf5ef2aSThomas Huth 
332fcf5ef2aSThomas Huth         val = (from_fixed < 0 ?  0 :
333fcf5ef2aSThomas Huth                from_fixed > 255 ?  255 : from_fixed);
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth         ret |= val << (8 * byte);
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth     return ret;
339fcf5ef2aSThomas Huth }
340fcf5ef2aSThomas Huth 
341fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2)
342fcf5ef2aSThomas Huth {
343fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
344fcf5ef2aSThomas Huth     uint64_t ret = 0;
345fcf5ef2aSThomas Huth     int word;
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth     ret = (rs1 << 8) & ~(0x000000ff000000ffULL);
348fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
349fcf5ef2aSThomas Huth         uint64_t val;
350fcf5ef2aSThomas Huth         int32_t src = rs2 >> (word * 32);
351fcf5ef2aSThomas Huth         int64_t scaled = (int64_t)src << scale;
352fcf5ef2aSThomas Huth         int64_t from_fixed = scaled >> 23;
353fcf5ef2aSThomas Huth 
354fcf5ef2aSThomas Huth         val = (from_fixed < 0 ? 0 :
355fcf5ef2aSThomas Huth                (from_fixed > 255) ? 255 : from_fixed);
356fcf5ef2aSThomas Huth 
357fcf5ef2aSThomas Huth         ret |= val << (32 * word);
358fcf5ef2aSThomas Huth     }
359fcf5ef2aSThomas Huth 
360fcf5ef2aSThomas Huth     return ret;
361fcf5ef2aSThomas Huth }
362fcf5ef2aSThomas Huth 
363fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2)
364fcf5ef2aSThomas Huth {
365fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
366fcf5ef2aSThomas Huth     uint32_t ret = 0;
367fcf5ef2aSThomas Huth     int word;
368fcf5ef2aSThomas Huth 
369fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
370fcf5ef2aSThomas Huth         uint32_t val;
371fcf5ef2aSThomas Huth         int32_t src = rs2 >> (word * 32);
372fcf5ef2aSThomas Huth         int64_t scaled = (int64_t)src << scale;
373fcf5ef2aSThomas Huth         int64_t from_fixed = scaled >> 16;
374fcf5ef2aSThomas Huth 
375fcf5ef2aSThomas Huth         val = (from_fixed < -32768 ? -32768 :
376fcf5ef2aSThomas Huth                from_fixed > 32767 ?  32767 : from_fixed);
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth         ret |= (val & 0xffff) << (word * 16);
379fcf5ef2aSThomas Huth     }
380fcf5ef2aSThomas Huth 
381fcf5ef2aSThomas Huth     return ret;
382fcf5ef2aSThomas Huth }
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2)
385fcf5ef2aSThomas Huth {
386fcf5ef2aSThomas Huth     union {
387fcf5ef2aSThomas Huth         uint64_t ll[2];
388fcf5ef2aSThomas Huth         uint8_t b[16];
389fcf5ef2aSThomas Huth     } s;
390fcf5ef2aSThomas Huth     VIS64 r;
391fcf5ef2aSThomas Huth     uint32_t i, mask, host;
392fcf5ef2aSThomas Huth 
393fcf5ef2aSThomas Huth     /* Set up S such that we can index across all of the bytes.  */
394e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
395fcf5ef2aSThomas Huth     s.ll[0] = src1;
396fcf5ef2aSThomas Huth     s.ll[1] = src2;
397fcf5ef2aSThomas Huth     host = 0;
398fcf5ef2aSThomas Huth #else
399fcf5ef2aSThomas Huth     s.ll[1] = src1;
400fcf5ef2aSThomas Huth     s.ll[0] = src2;
401fcf5ef2aSThomas Huth     host = 15;
402fcf5ef2aSThomas Huth #endif
403fcf5ef2aSThomas Huth     mask = gsr >> 32;
404fcf5ef2aSThomas Huth 
405fcf5ef2aSThomas Huth     for (i = 0; i < 8; ++i) {
406fcf5ef2aSThomas Huth         unsigned e = (mask >> (28 - i*4)) & 0xf;
407fcf5ef2aSThomas Huth         r.VIS_B64(i) = s.b[e ^ host];
408fcf5ef2aSThomas Huth     }
409fcf5ef2aSThomas Huth 
410fcf5ef2aSThomas Huth     return r.ll;
411fcf5ef2aSThomas Huth }
412