xref: /openbmc/qemu/target/sparc/vis_helper.c (revision 669e077437d5782682e2ac4f1082fcbf102b5680)
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 
24fa9079a8SRichard Henderson target_ulong helper_array8(target_ulong rs1, target_ulong rs2)
25fcf5ef2aSThomas Huth {
26fa9079a8SRichard Henderson     /*
27fa9079a8SRichard Henderson      * From Oracle SPARC Architecture 2015:
28fa9079a8SRichard Henderson      * Architecturally, an illegal R[rs2] value (>5) causes the array
29fa9079a8SRichard Henderson      * instructions to produce undefined results. For historic reference,
30fa9079a8SRichard Henderson      * past implementations of these instructions have ignored R[rs2]{63:3}
31fa9079a8SRichard Henderson      * and have treated R[rs2] values of 6 and 7 as if they were 5.
32fa9079a8SRichard Henderson      */
33fa9079a8SRichard Henderson     target_ulong n = MIN(rs2 & 7, 5);
34fa9079a8SRichard Henderson 
35fa9079a8SRichard Henderson     target_ulong x_int = (rs1 >> 11) & 0x7ff;
36fa9079a8SRichard Henderson     target_ulong y_int = (rs1 >> 33) & 0x7ff;
37fa9079a8SRichard Henderson     target_ulong z_int = rs1 >> 55;
38fa9079a8SRichard Henderson 
39fa9079a8SRichard Henderson     target_ulong lower_x = x_int & 3;
40fa9079a8SRichard Henderson     target_ulong lower_y = y_int & 3;
41fa9079a8SRichard Henderson     target_ulong lower_z = z_int & 1;
42fa9079a8SRichard Henderson 
43fa9079a8SRichard Henderson     target_ulong middle_x = (x_int >> 2) & 15;
44fa9079a8SRichard Henderson     target_ulong middle_y = (y_int >> 2) & 15;
45fa9079a8SRichard Henderson     target_ulong middle_z = (z_int >> 1) & 15;
46fa9079a8SRichard Henderson 
47fa9079a8SRichard Henderson     target_ulong upper_x = (x_int >> 6) & ((1 << n) - 1);
48fa9079a8SRichard Henderson     target_ulong upper_y = (y_int >> 6) & ((1 << n) - 1);
49fa9079a8SRichard Henderson     target_ulong upper_z = z_int >> 5;
50fa9079a8SRichard Henderson 
51fa9079a8SRichard Henderson     return (upper_z << (17 + 2 * n))
52fa9079a8SRichard Henderson          | (upper_y << (17 + n))
53fa9079a8SRichard Henderson          | (upper_x << 17)
54fa9079a8SRichard Henderson          | (middle_z << 13)
55fa9079a8SRichard Henderson          | (middle_y << 9)
56fa9079a8SRichard Henderson          | (middle_x << 5)
57fa9079a8SRichard Henderson          | (lower_z << 4)
58fa9079a8SRichard Henderson          | (lower_y << 2)
59fa9079a8SRichard Henderson          | lower_x;
60fcf5ef2aSThomas Huth }
61fcf5ef2aSThomas Huth 
62e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
63fcf5ef2aSThomas Huth #define VIS_B64(n) b[7 - (n)]
64d6f898cfSRichard Henderson #define VIS_SB64(n) sb[7 - (n)]
65fcf5ef2aSThomas Huth #define VIS_W64(n) w[3 - (n)]
66fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[3 - (n)]
67fcf5ef2aSThomas Huth #define VIS_L64(n) l[1 - (n)]
68fcf5ef2aSThomas Huth #define VIS_B32(n) b[3 - (n)]
69fcf5ef2aSThomas Huth #define VIS_W32(n) w[1 - (n)]
70fcf5ef2aSThomas Huth #else
71fcf5ef2aSThomas Huth #define VIS_B64(n) b[n]
72d6f898cfSRichard Henderson #define VIS_SB64(n) sb[n]
73fcf5ef2aSThomas Huth #define VIS_W64(n) w[n]
74fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[n]
75fcf5ef2aSThomas Huth #define VIS_L64(n) l[n]
76fcf5ef2aSThomas Huth #define VIS_B32(n) b[n]
77fcf5ef2aSThomas Huth #define VIS_W32(n) w[n]
78fcf5ef2aSThomas Huth #endif
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth typedef union {
81fcf5ef2aSThomas Huth     uint8_t b[8];
82d6f898cfSRichard Henderson     int8_t sb[8];
83fcf5ef2aSThomas Huth     uint16_t w[4];
84fcf5ef2aSThomas Huth     int16_t sw[4];
85fcf5ef2aSThomas Huth     uint32_t l[2];
86fcf5ef2aSThomas Huth     uint64_t ll;
87fcf5ef2aSThomas Huth     float64 d;
88fcf5ef2aSThomas Huth } VIS64;
89fcf5ef2aSThomas Huth 
90fcf5ef2aSThomas Huth typedef union {
91fcf5ef2aSThomas Huth     uint8_t b[4];
92fcf5ef2aSThomas Huth     uint16_t w[2];
93fcf5ef2aSThomas Huth     uint32_t l;
94fcf5ef2aSThomas Huth     float32 f;
95fcf5ef2aSThomas Huth } VIS32;
96fcf5ef2aSThomas Huth 
97d3ef26afSRichard Henderson uint64_t helper_fpmerge(uint32_t src1, uint32_t src2)
98fcf5ef2aSThomas Huth {
99d3ef26afSRichard Henderson     VIS32 s1, s2;
100d3ef26afSRichard Henderson     VIS64 d;
101fcf5ef2aSThomas Huth 
102d3ef26afSRichard Henderson     s1.l = src1;
103d3ef26afSRichard Henderson     s2.l = src2;
104d3ef26afSRichard Henderson     d.ll = 0;
105fcf5ef2aSThomas Huth 
106d3ef26afSRichard Henderson     d.VIS_B64(7) = s1.VIS_B32(3);
107d3ef26afSRichard Henderson     d.VIS_B64(6) = s2.VIS_B32(3);
108d3ef26afSRichard Henderson     d.VIS_B64(5) = s1.VIS_B32(2);
109d3ef26afSRichard Henderson     d.VIS_B64(4) = s2.VIS_B32(2);
110d3ef26afSRichard Henderson     d.VIS_B64(3) = s1.VIS_B32(1);
111d3ef26afSRichard Henderson     d.VIS_B64(2) = s2.VIS_B32(1);
112d3ef26afSRichard Henderson     d.VIS_B64(1) = s1.VIS_B32(0);
113d3ef26afSRichard Henderson     d.VIS_B64(0) = s2.VIS_B32(0);
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     return d.ll;
116fcf5ef2aSThomas Huth }
117fcf5ef2aSThomas Huth 
118d6f898cfSRichard Henderson static inline int do_ms16b(int x, int y)
119d6f898cfSRichard Henderson {
120d6f898cfSRichard Henderson     return ((x * y) + 0x80) >> 8;
121d6f898cfSRichard Henderson }
122d6f898cfSRichard Henderson 
1239157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2)
124fcf5ef2aSThomas Huth {
1259157dcccSRichard Henderson     VIS64 d;
1269157dcccSRichard Henderson     VIS32 s;
127fcf5ef2aSThomas Huth 
1289157dcccSRichard Henderson     s.l = src1;
129fcf5ef2aSThomas Huth     d.ll = src2;
130fcf5ef2aSThomas Huth 
131d6f898cfSRichard Henderson     d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), d.VIS_SW64(0));
132d6f898cfSRichard Henderson     d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), d.VIS_SW64(1));
133d6f898cfSRichard Henderson     d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), d.VIS_SW64(2));
134d6f898cfSRichard Henderson     d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), d.VIS_SW64(3));
135fcf5ef2aSThomas Huth 
136fcf5ef2aSThomas Huth     return d.ll;
137fcf5ef2aSThomas Huth }
138fcf5ef2aSThomas Huth 
139a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2)
140fcf5ef2aSThomas Huth {
141a859602cSRichard Henderson     VIS32 s;
142a859602cSRichard Henderson     VIS64 d;
143fcf5ef2aSThomas Huth 
144a859602cSRichard Henderson     s.l = src1;
145a859602cSRichard Henderson     d.ll = 0;
146fcf5ef2aSThomas Huth 
147d6f898cfSRichard Henderson     d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), src2);
148d6f898cfSRichard Henderson     d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), src2);
149d6f898cfSRichard Henderson     d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), src2);
150d6f898cfSRichard Henderson     d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), src2);
151fcf5ef2aSThomas Huth 
152fcf5ef2aSThomas Huth     return d.ll;
153fcf5ef2aSThomas Huth }
154fcf5ef2aSThomas Huth 
155fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2)
156fcf5ef2aSThomas Huth {
157fcf5ef2aSThomas Huth     VIS64 s, d;
158fcf5ef2aSThomas Huth 
159fcf5ef2aSThomas Huth     s.ll = src1;
160fcf5ef2aSThomas Huth     d.ll = src2;
161fcf5ef2aSThomas Huth 
162d6f898cfSRichard Henderson     d.VIS_W64(0) = do_ms16b(s.VIS_SB64(1), d.VIS_SW64(0));
163d6f898cfSRichard Henderson     d.VIS_W64(1) = do_ms16b(s.VIS_SB64(3), d.VIS_SW64(1));
164d6f898cfSRichard Henderson     d.VIS_W64(2) = do_ms16b(s.VIS_SB64(5), d.VIS_SW64(2));
165d6f898cfSRichard Henderson     d.VIS_W64(3) = do_ms16b(s.VIS_SB64(7), d.VIS_SW64(3));
166fcf5ef2aSThomas Huth 
167fcf5ef2aSThomas Huth     return d.ll;
168fcf5ef2aSThomas Huth }
169fcf5ef2aSThomas Huth 
170fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2)
171fcf5ef2aSThomas Huth {
172fcf5ef2aSThomas Huth     VIS64 s, d;
173fcf5ef2aSThomas Huth 
174fcf5ef2aSThomas Huth     s.ll = src1;
175fcf5ef2aSThomas Huth     d.ll = src2;
176fcf5ef2aSThomas Huth 
177b5c96047SRichard Henderson     d.VIS_W64(0) = (s.VIS_B64(0) * d.VIS_SW64(0) + 0x8000) >> 16;
178b5c96047SRichard Henderson     d.VIS_W64(1) = (s.VIS_B64(2) * d.VIS_SW64(1) + 0x8000) >> 16;
179b5c96047SRichard Henderson     d.VIS_W64(2) = (s.VIS_B64(4) * d.VIS_SW64(2) + 0x8000) >> 16;
180b5c96047SRichard Henderson     d.VIS_W64(3) = (s.VIS_B64(6) * d.VIS_SW64(3) + 0x8000) >> 16;
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth     return d.ll;
183fcf5ef2aSThomas Huth }
184fcf5ef2aSThomas Huth 
1857b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2)
186fcf5ef2aSThomas Huth {
187fcf5ef2aSThomas Huth     VIS32 s;
188fcf5ef2aSThomas Huth     VIS64 d;
189fcf5ef2aSThomas Huth 
1907b616f36SRichard Henderson     s.l = src2;
1917b616f36SRichard Henderson     d.ll = 0;
192fcf5ef2aSThomas Huth     d.VIS_W64(0) = s.VIS_B32(0) << 4;
193fcf5ef2aSThomas Huth     d.VIS_W64(1) = s.VIS_B32(1) << 4;
194fcf5ef2aSThomas Huth     d.VIS_W64(2) = s.VIS_B32(2) << 4;
195fcf5ef2aSThomas Huth     d.VIS_W64(3) = s.VIS_B32(3) << 4;
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth     return d.ll;
198fcf5ef2aSThomas Huth }
199fcf5ef2aSThomas Huth 
200fcf5ef2aSThomas Huth #define VIS_CMPHELPER(name, F)                                    \
201fcf5ef2aSThomas Huth     uint64_t name##16(uint64_t src1, uint64_t src2)               \
202fcf5ef2aSThomas Huth     {                                                             \
203fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
204fcf5ef2aSThomas Huth                                                                   \
205fcf5ef2aSThomas Huth         s.ll = src1;                                              \
206fcf5ef2aSThomas Huth         d.ll = src2;                                              \
207fcf5ef2aSThomas Huth                                                                   \
208fcf5ef2aSThomas Huth         d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0;     \
209fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0;    \
210fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0;    \
211fcf5ef2aSThomas Huth         d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0;    \
212fcf5ef2aSThomas Huth         d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0;           \
213fcf5ef2aSThomas Huth                                                                   \
214fcf5ef2aSThomas Huth         return d.ll;                                              \
215fcf5ef2aSThomas Huth     }                                                             \
216fcf5ef2aSThomas Huth                                                                   \
217fcf5ef2aSThomas Huth     uint64_t name##32(uint64_t src1, uint64_t src2)               \
218fcf5ef2aSThomas Huth     {                                                             \
219fcf5ef2aSThomas Huth         VIS64 s, d;                                               \
220fcf5ef2aSThomas Huth                                                                   \
221fcf5ef2aSThomas Huth         s.ll = src1;                                              \
222fcf5ef2aSThomas Huth         d.ll = src2;                                              \
223fcf5ef2aSThomas Huth                                                                   \
224fcf5ef2aSThomas Huth         d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0;     \
225fcf5ef2aSThomas Huth         d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0;    \
226fcf5ef2aSThomas Huth         d.VIS_L64(1) = 0;                                         \
227fcf5ef2aSThomas Huth                                                                   \
228fcf5ef2aSThomas Huth         return d.ll;                                              \
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b))
232fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b))
233fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b))
234fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b))
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
237fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
238fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE)
239fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE)
240fcf5ef2aSThomas Huth 
241*669e0774SRichard Henderson uint64_t helper_fcmpeq8(uint64_t src1, uint64_t src2)
242*669e0774SRichard Henderson {
243*669e0774SRichard Henderson     uint64_t a = src1 ^ src2;
244*669e0774SRichard Henderson     uint64_t m = 0x7f7f7f7f7f7f7f7fULL;
245*669e0774SRichard Henderson     uint64_t c = ~(((a & m) + m) | a | m);
246*669e0774SRichard Henderson 
247*669e0774SRichard Henderson     /* a.......b.......c.......d.......e.......f.......g.......h....... */
248*669e0774SRichard Henderson     c |= c << 7;
249*669e0774SRichard Henderson     /* ab......bc......cd......de......ef......fg......gh......h....... */
250*669e0774SRichard Henderson     c |= c << 14;
251*669e0774SRichard Henderson     /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */
252*669e0774SRichard Henderson     c |= c << 28;
253*669e0774SRichard Henderson     /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */
254*669e0774SRichard Henderson     return c >> 56;
255*669e0774SRichard Henderson }
256*669e0774SRichard Henderson 
257*669e0774SRichard Henderson uint64_t helper_fcmpne8(uint64_t src1, uint64_t src2)
258*669e0774SRichard Henderson {
259*669e0774SRichard Henderson     return helper_fcmpeq8(src1, src2) ^ 0xff;
260*669e0774SRichard Henderson }
261*669e0774SRichard Henderson 
262*669e0774SRichard Henderson uint64_t helper_fcmpule8(uint64_t src1, uint64_t src2)
263*669e0774SRichard Henderson {
264*669e0774SRichard Henderson     VIS64 s1, s2;
265*669e0774SRichard Henderson     uint64_t r = 0;
266*669e0774SRichard Henderson 
267*669e0774SRichard Henderson     s1.ll = src1;
268*669e0774SRichard Henderson     s2.ll = src2;
269*669e0774SRichard Henderson 
270*669e0774SRichard Henderson     for (int i = 0; i < 8; ++i) {
271*669e0774SRichard Henderson         r |= (s1.VIS_B64(i) <= s2.VIS_B64(i)) << i;
272*669e0774SRichard Henderson     }
273*669e0774SRichard Henderson     return r;
274*669e0774SRichard Henderson }
275*669e0774SRichard Henderson 
276*669e0774SRichard Henderson uint64_t helper_fcmpugt8(uint64_t src1, uint64_t src2)
277*669e0774SRichard Henderson {
278*669e0774SRichard Henderson     return helper_fcmpule8(src1, src2) ^ 0xff;
279*669e0774SRichard Henderson }
280*669e0774SRichard Henderson 
281fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2)
282fcf5ef2aSThomas Huth {
283fcf5ef2aSThomas Huth     int i;
284fcf5ef2aSThomas Huth     for (i = 0; i < 8; i++) {
285fcf5ef2aSThomas Huth         int s1, s2;
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth         s1 = (src1 >> (56 - (i * 8))) & 0xff;
288fcf5ef2aSThomas Huth         s2 = (src2 >> (56 - (i * 8))) & 0xff;
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth         /* Absolute value of difference. */
291fcf5ef2aSThomas Huth         s1 -= s2;
292fcf5ef2aSThomas Huth         if (s1 < 0) {
293fcf5ef2aSThomas Huth             s1 = -s1;
294fcf5ef2aSThomas Huth         }
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth         sum += s1;
297fcf5ef2aSThomas Huth     }
298fcf5ef2aSThomas Huth 
299fcf5ef2aSThomas Huth     return sum;
300fcf5ef2aSThomas Huth }
301fcf5ef2aSThomas Huth 
302fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2)
303fcf5ef2aSThomas Huth {
304fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0xf;
305fcf5ef2aSThomas Huth     uint32_t ret = 0;
306fcf5ef2aSThomas Huth     int byte;
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth     for (byte = 0; byte < 4; byte++) {
309fcf5ef2aSThomas Huth         uint32_t val;
310fcf5ef2aSThomas Huth         int16_t src = rs2 >> (byte * 16);
311fcf5ef2aSThomas Huth         int32_t scaled = src << scale;
312fcf5ef2aSThomas Huth         int32_t from_fixed = scaled >> 7;
313fcf5ef2aSThomas Huth 
314fcf5ef2aSThomas Huth         val = (from_fixed < 0 ?  0 :
315fcf5ef2aSThomas Huth                from_fixed > 255 ?  255 : from_fixed);
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth         ret |= val << (8 * byte);
318fcf5ef2aSThomas Huth     }
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth     return ret;
321fcf5ef2aSThomas Huth }
322fcf5ef2aSThomas Huth 
323fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2)
324fcf5ef2aSThomas Huth {
325fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
326fcf5ef2aSThomas Huth     uint64_t ret = 0;
327fcf5ef2aSThomas Huth     int word;
328fcf5ef2aSThomas Huth 
329fcf5ef2aSThomas Huth     ret = (rs1 << 8) & ~(0x000000ff000000ffULL);
330fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
331fcf5ef2aSThomas Huth         uint64_t val;
332fcf5ef2aSThomas Huth         int32_t src = rs2 >> (word * 32);
333fcf5ef2aSThomas Huth         int64_t scaled = (int64_t)src << scale;
334fcf5ef2aSThomas Huth         int64_t from_fixed = scaled >> 23;
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth         val = (from_fixed < 0 ? 0 :
337fcf5ef2aSThomas Huth                (from_fixed > 255) ? 255 : from_fixed);
338fcf5ef2aSThomas Huth 
339fcf5ef2aSThomas Huth         ret |= val << (32 * word);
340fcf5ef2aSThomas Huth     }
341fcf5ef2aSThomas Huth 
342fcf5ef2aSThomas Huth     return ret;
343fcf5ef2aSThomas Huth }
344fcf5ef2aSThomas Huth 
345fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2)
346fcf5ef2aSThomas Huth {
347fcf5ef2aSThomas Huth     int scale = (gsr >> 3) & 0x1f;
348fcf5ef2aSThomas Huth     uint32_t ret = 0;
349fcf5ef2aSThomas Huth     int word;
350fcf5ef2aSThomas Huth 
351fcf5ef2aSThomas Huth     for (word = 0; word < 2; word++) {
352fcf5ef2aSThomas Huth         uint32_t val;
353fcf5ef2aSThomas Huth         int32_t src = rs2 >> (word * 32);
354fcf5ef2aSThomas Huth         int64_t scaled = (int64_t)src << scale;
355fcf5ef2aSThomas Huth         int64_t from_fixed = scaled >> 16;
356fcf5ef2aSThomas Huth 
357fcf5ef2aSThomas Huth         val = (from_fixed < -32768 ? -32768 :
358fcf5ef2aSThomas Huth                from_fixed > 32767 ?  32767 : from_fixed);
359fcf5ef2aSThomas Huth 
360fcf5ef2aSThomas Huth         ret |= (val & 0xffff) << (word * 16);
361fcf5ef2aSThomas Huth     }
362fcf5ef2aSThomas Huth 
363fcf5ef2aSThomas Huth     return ret;
364fcf5ef2aSThomas Huth }
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2)
367fcf5ef2aSThomas Huth {
368fcf5ef2aSThomas Huth     union {
369fcf5ef2aSThomas Huth         uint64_t ll[2];
370fcf5ef2aSThomas Huth         uint8_t b[16];
371fcf5ef2aSThomas Huth     } s;
372fcf5ef2aSThomas Huth     VIS64 r;
373fcf5ef2aSThomas Huth     uint32_t i, mask, host;
374fcf5ef2aSThomas Huth 
375fcf5ef2aSThomas Huth     /* Set up S such that we can index across all of the bytes.  */
376e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
377fcf5ef2aSThomas Huth     s.ll[0] = src1;
378fcf5ef2aSThomas Huth     s.ll[1] = src2;
379fcf5ef2aSThomas Huth     host = 0;
380fcf5ef2aSThomas Huth #else
381fcf5ef2aSThomas Huth     s.ll[1] = src1;
382fcf5ef2aSThomas Huth     s.ll[0] = src2;
383fcf5ef2aSThomas Huth     host = 15;
384fcf5ef2aSThomas Huth #endif
385fcf5ef2aSThomas Huth     mask = gsr >> 32;
386fcf5ef2aSThomas Huth 
387fcf5ef2aSThomas Huth     for (i = 0; i < 8; ++i) {
388fcf5ef2aSThomas Huth         unsigned e = (mask >> (28 - i*4)) & 0xf;
389fcf5ef2aSThomas Huth         r.VIS_B64(i) = s.b[e ^ host];
390fcf5ef2aSThomas Huth     }
391fcf5ef2aSThomas Huth 
392fcf5ef2aSThomas Huth     return r.ll;
393fcf5ef2aSThomas Huth }
394c973b4e8SRichard Henderson 
395c973b4e8SRichard Henderson uint64_t helper_cmask8(uint64_t gsr, uint64_t src)
396c973b4e8SRichard Henderson {
397c973b4e8SRichard Henderson     uint32_t mask = 0;
398c973b4e8SRichard Henderson 
399c973b4e8SRichard Henderson     mask |= (src & 0x01 ? 0x00000007 : 0x0000000f);
400c973b4e8SRichard Henderson     mask |= (src & 0x02 ? 0x00000060 : 0x000000e0);
401c973b4e8SRichard Henderson     mask |= (src & 0x04 ? 0x00000500 : 0x00000d00);
402c973b4e8SRichard Henderson     mask |= (src & 0x08 ? 0x00004000 : 0x0000c000);
403c973b4e8SRichard Henderson     mask |= (src & 0x10 ? 0x00030000 : 0x000b0000);
404c973b4e8SRichard Henderson     mask |= (src & 0x20 ? 0x00200000 : 0x00a00000);
405c973b4e8SRichard Henderson     mask |= (src & 0x40 ? 0x01000000 : 0x09000000);
406c973b4e8SRichard Henderson     mask |= (src & 0x80 ? 0x00000000 : 0x80000000);
407c973b4e8SRichard Henderson 
408c973b4e8SRichard Henderson     return deposit64(gsr, 32, 32, mask);
409c973b4e8SRichard Henderson }
410c973b4e8SRichard Henderson 
411c973b4e8SRichard Henderson uint64_t helper_cmask16(uint64_t gsr, uint64_t src)
412c973b4e8SRichard Henderson {
413c973b4e8SRichard Henderson     uint32_t mask = 0;
414c973b4e8SRichard Henderson 
415c973b4e8SRichard Henderson     mask |= (src & 0x1 ? 0x00000067 : 0x000000ef);
416c973b4e8SRichard Henderson     mask |= (src & 0x2 ? 0x00004500 : 0x0000cd00);
417c973b4e8SRichard Henderson     mask |= (src & 0x4 ? 0x00230000 : 0x00ab0000);
418c973b4e8SRichard Henderson     mask |= (src & 0x8 ? 0x01000000 : 0x89000000);
419c973b4e8SRichard Henderson 
420c973b4e8SRichard Henderson     return deposit64(gsr, 32, 32, mask);
421c973b4e8SRichard Henderson }
422c973b4e8SRichard Henderson 
423c973b4e8SRichard Henderson uint64_t helper_cmask32(uint64_t gsr, uint64_t src)
424c973b4e8SRichard Henderson {
425c973b4e8SRichard Henderson     uint32_t mask = 0;
426c973b4e8SRichard Henderson 
427c973b4e8SRichard Henderson     mask |= (src & 0x1 ? 0x00004567 : 0x0000cdef);
428c973b4e8SRichard Henderson     mask |= (src & 0x2 ? 0x01230000 : 0x89ab0000);
429c973b4e8SRichard Henderson 
430c973b4e8SRichard Henderson     return deposit64(gsr, 32, 32, mask);
431c973b4e8SRichard Henderson }
4327837185eSRichard Henderson 
4337837185eSRichard Henderson static inline uint16_t do_fchksm16(uint16_t src1, uint16_t src2)
4347837185eSRichard Henderson {
4357837185eSRichard Henderson     uint16_t a = src1 + src2;
4367837185eSRichard Henderson     uint16_t c = a < src1;
4377837185eSRichard Henderson     return a + c;
4387837185eSRichard Henderson }
4397837185eSRichard Henderson 
4407837185eSRichard Henderson uint64_t helper_fchksm16(uint64_t src1, uint64_t src2)
4417837185eSRichard Henderson {
4427837185eSRichard Henderson     VIS64 r, s1, s2;
4437837185eSRichard Henderson 
4447837185eSRichard Henderson     s1.ll = src1;
4457837185eSRichard Henderson     s2.ll = src2;
4467837185eSRichard Henderson     r.ll = 0;
4477837185eSRichard Henderson 
4487837185eSRichard Henderson     r.VIS_W64(0) = do_fchksm16(s1.VIS_W64(0), s2.VIS_W64(0));
4497837185eSRichard Henderson     r.VIS_W64(1) = do_fchksm16(s1.VIS_W64(1), s2.VIS_W64(1));
4507837185eSRichard Henderson     r.VIS_W64(2) = do_fchksm16(s1.VIS_W64(2), s2.VIS_W64(2));
4517837185eSRichard Henderson     r.VIS_W64(3) = do_fchksm16(s1.VIS_W64(3), s2.VIS_W64(3));
4527837185eSRichard Henderson 
4537837185eSRichard Henderson     return r.ll;
4547837185eSRichard Henderson }
455d6ff1ccbSRichard Henderson 
456d6ff1ccbSRichard Henderson static inline int16_t do_fmean16(int16_t src1, int16_t src2)
457d6ff1ccbSRichard Henderson {
458d6ff1ccbSRichard Henderson     return (src1 + src2 + 1) / 2;
459d6ff1ccbSRichard Henderson }
460d6ff1ccbSRichard Henderson 
461d6ff1ccbSRichard Henderson uint64_t helper_fmean16(uint64_t src1, uint64_t src2)
462d6ff1ccbSRichard Henderson {
463d6ff1ccbSRichard Henderson     VIS64 r, s1, s2;
464d6ff1ccbSRichard Henderson 
465d6ff1ccbSRichard Henderson     s1.ll = src1;
466d6ff1ccbSRichard Henderson     s2.ll = src2;
467d6ff1ccbSRichard Henderson     r.ll = 0;
468d6ff1ccbSRichard Henderson 
469d6ff1ccbSRichard Henderson     r.VIS_SW64(0) = do_fmean16(s1.VIS_SW64(0), s2.VIS_SW64(0));
470d6ff1ccbSRichard Henderson     r.VIS_SW64(1) = do_fmean16(s1.VIS_SW64(1), s2.VIS_SW64(1));
471d6ff1ccbSRichard Henderson     r.VIS_SW64(2) = do_fmean16(s1.VIS_SW64(2), s2.VIS_SW64(2));
472d6ff1ccbSRichard Henderson     r.VIS_SW64(3) = do_fmean16(s1.VIS_SW64(3), s2.VIS_SW64(3));
473d6ff1ccbSRichard Henderson 
474d6ff1ccbSRichard Henderson     return r.ll;
475d6ff1ccbSRichard Henderson }
476