xref: /openbmc/qemu/target/sparc/vis_helper.c (revision 9157dccc7e71f7c94581c38f38acbef9a21bbe9a)
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 
97*9157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2)
98fcf5ef2aSThomas Huth {
99*9157dcccSRichard Henderson     VIS64 d;
100*9157dcccSRichard Henderson     VIS32 s;
101fcf5ef2aSThomas Huth     uint32_t tmp;
102fcf5ef2aSThomas Huth 
103*9157dcccSRichard Henderson     s.l = src1;
104fcf5ef2aSThomas Huth     d.ll = src2;
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth #define PMUL(r)                                                 \
107*9157dcccSRichard 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 
122fcf5ef2aSThomas Huth uint64_t helper_fmul8x16al(uint64_t src1, uint64_t src2)
123fcf5ef2aSThomas Huth {
124fcf5ef2aSThomas Huth     VIS64 s, d;
125fcf5ef2aSThomas Huth     uint32_t tmp;
126fcf5ef2aSThomas Huth 
127fcf5ef2aSThomas Huth     s.ll = src1;
128fcf5ef2aSThomas Huth     d.ll = src2;
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth #define PMUL(r)                                                 \
131fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(1) * (int32_t)s.VIS_B64(r);       \
132fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                  \
133fcf5ef2aSThomas Huth         tmp += 0x100;                                           \
134fcf5ef2aSThomas Huth     }                                                           \
135fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth     PMUL(0);
138fcf5ef2aSThomas Huth     PMUL(1);
139fcf5ef2aSThomas Huth     PMUL(2);
140fcf5ef2aSThomas Huth     PMUL(3);
141fcf5ef2aSThomas Huth #undef PMUL
142fcf5ef2aSThomas Huth 
143fcf5ef2aSThomas Huth     return d.ll;
144fcf5ef2aSThomas Huth }
145fcf5ef2aSThomas Huth 
146fcf5ef2aSThomas Huth uint64_t helper_fmul8x16au(uint64_t src1, uint64_t src2)
147fcf5ef2aSThomas Huth {
148fcf5ef2aSThomas Huth     VIS64 s, d;
149fcf5ef2aSThomas Huth     uint32_t tmp;
150fcf5ef2aSThomas Huth 
151fcf5ef2aSThomas Huth     s.ll = src1;
152fcf5ef2aSThomas Huth     d.ll = src2;
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth #define PMUL(r)                                                 \
155fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(0) * (int32_t)s.VIS_B64(r);       \
156fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                  \
157fcf5ef2aSThomas Huth         tmp += 0x100;                                           \
158fcf5ef2aSThomas Huth     }                                                           \
159fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
160fcf5ef2aSThomas Huth 
161fcf5ef2aSThomas Huth     PMUL(0);
162fcf5ef2aSThomas Huth     PMUL(1);
163fcf5ef2aSThomas Huth     PMUL(2);
164fcf5ef2aSThomas Huth     PMUL(3);
165fcf5ef2aSThomas Huth #undef PMUL
166fcf5ef2aSThomas Huth 
167fcf5ef2aSThomas Huth     return d.ll;
168fcf5ef2aSThomas Huth }
169fcf5ef2aSThomas Huth 
170fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2)
171fcf5ef2aSThomas Huth {
172fcf5ef2aSThomas Huth     VIS64 s, d;
173fcf5ef2aSThomas Huth     uint32_t tmp;
174fcf5ef2aSThomas Huth 
175fcf5ef2aSThomas Huth     s.ll = src1;
176fcf5ef2aSThomas Huth     d.ll = src2;
177fcf5ef2aSThomas Huth 
178fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
179fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
180fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
181fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
182fcf5ef2aSThomas Huth     }                                                                   \
183fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
184fcf5ef2aSThomas Huth 
185fcf5ef2aSThomas Huth     PMUL(0);
186fcf5ef2aSThomas Huth     PMUL(1);
187fcf5ef2aSThomas Huth     PMUL(2);
188fcf5ef2aSThomas Huth     PMUL(3);
189fcf5ef2aSThomas Huth #undef PMUL
190fcf5ef2aSThomas Huth 
191fcf5ef2aSThomas Huth     return d.ll;
192fcf5ef2aSThomas Huth }
193fcf5ef2aSThomas Huth 
194fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2)
195fcf5ef2aSThomas Huth {
196fcf5ef2aSThomas Huth     VIS64 s, d;
197fcf5ef2aSThomas Huth     uint32_t tmp;
198fcf5ef2aSThomas Huth 
199fcf5ef2aSThomas Huth     s.ll = src1;
200fcf5ef2aSThomas Huth     d.ll = src2;
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
203fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
204fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
205fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
206fcf5ef2aSThomas Huth     }                                                                   \
207fcf5ef2aSThomas Huth     d.VIS_W64(r) = tmp >> 8;
208fcf5ef2aSThomas Huth 
209fcf5ef2aSThomas Huth     PMUL(0);
210fcf5ef2aSThomas Huth     PMUL(1);
211fcf5ef2aSThomas Huth     PMUL(2);
212fcf5ef2aSThomas Huth     PMUL(3);
213fcf5ef2aSThomas Huth #undef PMUL
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth     return d.ll;
216fcf5ef2aSThomas Huth }
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth uint64_t helper_fmuld8sux16(uint64_t src1, uint64_t src2)
219fcf5ef2aSThomas Huth {
220fcf5ef2aSThomas Huth     VIS64 s, d;
221fcf5ef2aSThomas Huth     uint32_t tmp;
222fcf5ef2aSThomas Huth 
223fcf5ef2aSThomas Huth     s.ll = src1;
224fcf5ef2aSThomas Huth     d.ll = src2;
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
227fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
228fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
229fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
230fcf5ef2aSThomas Huth     }                                                                   \
231fcf5ef2aSThomas Huth     d.VIS_L64(r) = tmp;
232fcf5ef2aSThomas Huth 
233fcf5ef2aSThomas Huth     /* Reverse calculation order to handle overlap */
234fcf5ef2aSThomas Huth     PMUL(1);
235fcf5ef2aSThomas Huth     PMUL(0);
236fcf5ef2aSThomas Huth #undef PMUL
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth     return d.ll;
239fcf5ef2aSThomas Huth }
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth uint64_t helper_fmuld8ulx16(uint64_t src1, uint64_t src2)
242fcf5ef2aSThomas Huth {
243fcf5ef2aSThomas Huth     VIS64 s, d;
244fcf5ef2aSThomas Huth     uint32_t tmp;
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth     s.ll = src1;
247fcf5ef2aSThomas Huth     d.ll = src2;
248fcf5ef2aSThomas Huth 
249fcf5ef2aSThomas Huth #define PMUL(r)                                                         \
250fcf5ef2aSThomas Huth     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
251fcf5ef2aSThomas Huth     if ((tmp & 0xff) > 0x7f) {                                          \
252fcf5ef2aSThomas Huth         tmp += 0x100;                                                   \
253fcf5ef2aSThomas Huth     }                                                                   \
254fcf5ef2aSThomas Huth     d.VIS_L64(r) = tmp;
255fcf5ef2aSThomas Huth 
256fcf5ef2aSThomas Huth     /* Reverse calculation order to handle overlap */
257fcf5ef2aSThomas Huth     PMUL(1);
258fcf5ef2aSThomas Huth     PMUL(0);
259fcf5ef2aSThomas Huth #undef PMUL
260fcf5ef2aSThomas Huth 
261fcf5ef2aSThomas Huth     return d.ll;
262fcf5ef2aSThomas Huth }
263fcf5ef2aSThomas Huth 
2647b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2)
265fcf5ef2aSThomas Huth {
266fcf5ef2aSThomas Huth     VIS32 s;
267fcf5ef2aSThomas Huth     VIS64 d;
268fcf5ef2aSThomas Huth 
2697b616f36SRichard Henderson     s.l = src2;
2707b616f36SRichard Henderson     d.ll = 0;
271fcf5ef2aSThomas Huth     d.VIS_W64(0) = s.VIS_B32(0) << 4;
272fcf5ef2aSThomas Huth     d.VIS_W64(1) = s.VIS_B32(1) << 4;
273fcf5ef2aSThomas Huth     d.VIS_W64(2) = s.VIS_B32(2) << 4;
274fcf5ef2aSThomas Huth     d.VIS_W64(3) = s.VIS_B32(3) << 4;
275fcf5ef2aSThomas Huth 
276fcf5ef2aSThomas Huth     return d.ll;
277fcf5ef2aSThomas Huth }
278fcf5ef2aSThomas Huth 
279fcf5ef2aSThomas Huth #define VIS_CMPHELPER(name, F)                                    \
280fcf5ef2aSThomas Huth     uint64_t name##16(uint64_t src1, uint64_t src2)               \
281fcf5ef2aSThomas Huth     {                                                             \
282fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
283fcf5ef2aSThomas Huth                                                                   \
284fcf5ef2aSThomas Huth         s.ll = src1;                                              \
285fcf5ef2aSThomas Huth         d.ll = src2;                                              \
286fcf5ef2aSThomas Huth                                                                   \
287fcf5ef2aSThomas Huth         d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0;     \
288fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0;    \
289fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0;    \
290fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0;    \
291fcf5ef2aSThomas Huth         d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0;           \
292fcf5ef2aSThomas Huth                                                                   \
293fcf5ef2aSThomas Huth         return d.ll;                                              \
294fcf5ef2aSThomas Huth     }                                                             \
295fcf5ef2aSThomas Huth                                                                   \
296fcf5ef2aSThomas Huth     uint64_t name##32(uint64_t src1, uint64_t src2)               \
297fcf5ef2aSThomas Huth     {                                                             \
298fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
299fcf5ef2aSThomas Huth                                                                   \
300fcf5ef2aSThomas Huth         s.ll = src1;                                              \
301fcf5ef2aSThomas Huth         d.ll = src2;                                              \
302fcf5ef2aSThomas Huth                                                                   \
303fcf5ef2aSThomas Huth         d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0;     \
304fcf5ef2aSThomas Huth         d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0;    \
305fcf5ef2aSThomas Huth         d.VIS_L64(1) = 0;                                         \
306fcf5ef2aSThomas Huth                                                                   \
307fcf5ef2aSThomas Huth         return d.ll;                                              \
308fcf5ef2aSThomas Huth     }
309fcf5ef2aSThomas Huth 
310fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b))
311fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b))
312fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b))
313fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b))
314fcf5ef2aSThomas Huth 
315fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
316fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
317fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE)
318fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE)
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2)
321fcf5ef2aSThomas Huth {
322fcf5ef2aSThomas Huth     int i;
323fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
324fcf5ef2aSThomas Huth         int s1, s2;
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth         s1 = (src1 >> (56 - (i * 8))) & 0xff;
327fcf5ef2aSThomas Huth         s2 = (src2 >> (56 - (i * 8))) & 0xff;
328fcf5ef2aSThomas Huth 
329fcf5ef2aSThomas Huth         /* Absolute value of difference. */
330fcf5ef2aSThomas Huth         s1 -= s2;
331fcf5ef2aSThomas Huth         if (s1 < 0) {
332fcf5ef2aSThomas Huth             s1 = -s1;
333fcf5ef2aSThomas Huth         }
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth         sum += s1;
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth     return sum;
339fcf5ef2aSThomas Huth }
340fcf5ef2aSThomas Huth 
341fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2)
342fcf5ef2aSThomas Huth {
343fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0xf;
344fcf5ef2aSThomas Huth     uint32_t ret = 0;
345fcf5ef2aSThomas Huth     int byte;
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth     for (byte = 0; byte < 4; byte++) {
348fcf5ef2aSThomas Huth         uint32_t val;
349fcf5ef2aSThomas Huth         int16_t src = rs2 >> (byte * 16);
350fcf5ef2aSThomas Huth         int32_t scaled = src << scale;
351fcf5ef2aSThomas Huth         int32_t from_fixed = scaled >> 7;
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth         val = (from_fixed < 0 ?  0 :
354fcf5ef2aSThomas Huth                from_fixed > 255 ?  255 : from_fixed);
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth         ret |= val << (8 * byte);
357fcf5ef2aSThomas Huth     }
358fcf5ef2aSThomas Huth 
359fcf5ef2aSThomas Huth     return ret;
360fcf5ef2aSThomas Huth }
361fcf5ef2aSThomas Huth 
362fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2)
363fcf5ef2aSThomas Huth {
364fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
365fcf5ef2aSThomas Huth     uint64_t ret = 0;
366fcf5ef2aSThomas Huth     int word;
367fcf5ef2aSThomas Huth 
368fcf5ef2aSThomas Huth     ret = (rs1 << 8) & ~(0x000000ff000000ffULL);
369fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
370fcf5ef2aSThomas Huth         uint64_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 >> 23;
374fcf5ef2aSThomas Huth 
375fcf5ef2aSThomas Huth         val = (from_fixed < 0 ? 0 :
376fcf5ef2aSThomas Huth                (from_fixed > 255) ? 255 : from_fixed);
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth         ret |= val << (32 * word);
379fcf5ef2aSThomas Huth     }
380fcf5ef2aSThomas Huth 
381fcf5ef2aSThomas Huth     return ret;
382fcf5ef2aSThomas Huth }
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2)
385fcf5ef2aSThomas Huth {
386fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
387fcf5ef2aSThomas Huth     uint32_t ret = 0;
388fcf5ef2aSThomas Huth     int word;
389fcf5ef2aSThomas Huth 
390fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
391fcf5ef2aSThomas Huth         uint32_t val;
392fcf5ef2aSThomas Huth         int32_t src = rs2 >> (word * 32);
393fcf5ef2aSThomas Huth         int64_t scaled = (int64_t)src << scale;
394fcf5ef2aSThomas Huth         int64_t from_fixed = scaled >> 16;
395fcf5ef2aSThomas Huth 
396fcf5ef2aSThomas Huth         val = (from_fixed < -32768 ? -32768 :
397fcf5ef2aSThomas Huth                from_fixed > 32767 ?  32767 : from_fixed);
398fcf5ef2aSThomas Huth 
399fcf5ef2aSThomas Huth         ret |= (val & 0xffff) << (word * 16);
400fcf5ef2aSThomas Huth     }
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth     return ret;
403fcf5ef2aSThomas Huth }
404fcf5ef2aSThomas Huth 
405fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2)
406fcf5ef2aSThomas Huth {
407fcf5ef2aSThomas Huth     union {
408fcf5ef2aSThomas Huth         uint64_t ll[2];
409fcf5ef2aSThomas Huth         uint8_t b[16];
410fcf5ef2aSThomas Huth     } s;
411fcf5ef2aSThomas Huth     VIS64 r;
412fcf5ef2aSThomas Huth     uint32_t i, mask, host;
413fcf5ef2aSThomas Huth 
414fcf5ef2aSThomas Huth     /* Set up S such that we can index across all of the bytes.  */
415e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
416fcf5ef2aSThomas Huth     s.ll[0] = src1;
417fcf5ef2aSThomas Huth     s.ll[1] = src2;
418fcf5ef2aSThomas Huth     host = 0;
419fcf5ef2aSThomas Huth #else
420fcf5ef2aSThomas Huth     s.ll[1] = src1;
421fcf5ef2aSThomas Huth     s.ll[0] = src2;
422fcf5ef2aSThomas Huth     host = 15;
423fcf5ef2aSThomas Huth #endif
424fcf5ef2aSThomas Huth     mask = gsr >> 32;
425fcf5ef2aSThomas Huth 
426fcf5ef2aSThomas Huth     for (i = 0; i < 8; ++i) {
427fcf5ef2aSThomas Huth         unsigned e = (mask >> (28 - i*4)) & 0xf;
428fcf5ef2aSThomas Huth         r.VIS_B64(i) = s.b[e ^ host];
429fcf5ef2aSThomas Huth     }
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth     return r.ll;
432fcf5ef2aSThomas Huth }
433