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