Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20161017' into...
[qemu/ar7.git] / target-sparc / vis_helper.c
blob8a9b763d0b5b0639b38c9d24e799c99b42db1903
1 /*
2 * VIS op helpers
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
24 /* This function uses non-native bit order */
25 #define GET_FIELD(X, FROM, TO) \
26 ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))
28 /* This function uses the order in the manuals, i.e. bit 0 is 2^0 */
29 #define GET_FIELD_SP(X, FROM, TO) \
30 GET_FIELD(X, 63 - (TO), 63 - (FROM))
32 target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
34 return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
35 (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
36 (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
37 (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
38 (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
39 (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
40 (((pixel_addr >> 55) & 1) << 4) |
41 (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
42 GET_FIELD_SP(pixel_addr, 11, 12);
45 #ifdef HOST_WORDS_BIGENDIAN
46 #define VIS_B64(n) b[7 - (n)]
47 #define VIS_W64(n) w[3 - (n)]
48 #define VIS_SW64(n) sw[3 - (n)]
49 #define VIS_L64(n) l[1 - (n)]
50 #define VIS_B32(n) b[3 - (n)]
51 #define VIS_W32(n) w[1 - (n)]
52 #else
53 #define VIS_B64(n) b[n]
54 #define VIS_W64(n) w[n]
55 #define VIS_SW64(n) sw[n]
56 #define VIS_L64(n) l[n]
57 #define VIS_B32(n) b[n]
58 #define VIS_W32(n) w[n]
59 #endif
61 typedef union {
62 uint8_t b[8];
63 uint16_t w[4];
64 int16_t sw[4];
65 uint32_t l[2];
66 uint64_t ll;
67 float64 d;
68 } VIS64;
70 typedef union {
71 uint8_t b[4];
72 uint16_t w[2];
73 uint32_t l;
74 float32 f;
75 } VIS32;
77 uint64_t helper_fpmerge(uint64_t src1, uint64_t src2)
79 VIS64 s, d;
81 s.ll = src1;
82 d.ll = src2;
84 /* Reverse calculation order to handle overlap */
85 d.VIS_B64(7) = s.VIS_B64(3);
86 d.VIS_B64(6) = d.VIS_B64(3);
87 d.VIS_B64(5) = s.VIS_B64(2);
88 d.VIS_B64(4) = d.VIS_B64(2);
89 d.VIS_B64(3) = s.VIS_B64(1);
90 d.VIS_B64(2) = d.VIS_B64(1);
91 d.VIS_B64(1) = s.VIS_B64(0);
92 /* d.VIS_B64(0) = d.VIS_B64(0); */
94 return d.ll;
97 uint64_t helper_fmul8x16(uint64_t src1, uint64_t src2)
99 VIS64 s, d;
100 uint32_t tmp;
102 s.ll = src1;
103 d.ll = src2;
105 #define PMUL(r) \
106 tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B64(r); \
107 if ((tmp & 0xff) > 0x7f) { \
108 tmp += 0x100; \
110 d.VIS_W64(r) = tmp >> 8;
112 PMUL(0);
113 PMUL(1);
114 PMUL(2);
115 PMUL(3);
116 #undef PMUL
118 return d.ll;
121 uint64_t helper_fmul8x16al(uint64_t src1, uint64_t src2)
123 VIS64 s, d;
124 uint32_t tmp;
126 s.ll = src1;
127 d.ll = src2;
129 #define PMUL(r) \
130 tmp = (int32_t)d.VIS_SW64(1) * (int32_t)s.VIS_B64(r); \
131 if ((tmp & 0xff) > 0x7f) { \
132 tmp += 0x100; \
134 d.VIS_W64(r) = tmp >> 8;
136 PMUL(0);
137 PMUL(1);
138 PMUL(2);
139 PMUL(3);
140 #undef PMUL
142 return d.ll;
145 uint64_t helper_fmul8x16au(uint64_t src1, uint64_t src2)
147 VIS64 s, d;
148 uint32_t tmp;
150 s.ll = src1;
151 d.ll = src2;
153 #define PMUL(r) \
154 tmp = (int32_t)d.VIS_SW64(0) * (int32_t)s.VIS_B64(r); \
155 if ((tmp & 0xff) > 0x7f) { \
156 tmp += 0x100; \
158 d.VIS_W64(r) = tmp >> 8;
160 PMUL(0);
161 PMUL(1);
162 PMUL(2);
163 PMUL(3);
164 #undef PMUL
166 return d.ll;
169 uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2)
171 VIS64 s, d;
172 uint32_t tmp;
174 s.ll = src1;
175 d.ll = src2;
177 #define PMUL(r) \
178 tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8); \
179 if ((tmp & 0xff) > 0x7f) { \
180 tmp += 0x100; \
182 d.VIS_W64(r) = tmp >> 8;
184 PMUL(0);
185 PMUL(1);
186 PMUL(2);
187 PMUL(3);
188 #undef PMUL
190 return d.ll;
193 uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2)
195 VIS64 s, d;
196 uint32_t tmp;
198 s.ll = src1;
199 d.ll = src2;
201 #define PMUL(r) \
202 tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2)); \
203 if ((tmp & 0xff) > 0x7f) { \
204 tmp += 0x100; \
206 d.VIS_W64(r) = tmp >> 8;
208 PMUL(0);
209 PMUL(1);
210 PMUL(2);
211 PMUL(3);
212 #undef PMUL
214 return d.ll;
217 uint64_t helper_fmuld8sux16(uint64_t src1, uint64_t src2)
219 VIS64 s, d;
220 uint32_t tmp;
222 s.ll = src1;
223 d.ll = src2;
225 #define PMUL(r) \
226 tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8); \
227 if ((tmp & 0xff) > 0x7f) { \
228 tmp += 0x100; \
230 d.VIS_L64(r) = tmp;
232 /* Reverse calculation order to handle overlap */
233 PMUL(1);
234 PMUL(0);
235 #undef PMUL
237 return d.ll;
240 uint64_t helper_fmuld8ulx16(uint64_t src1, uint64_t src2)
242 VIS64 s, d;
243 uint32_t tmp;
245 s.ll = src1;
246 d.ll = src2;
248 #define PMUL(r) \
249 tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2)); \
250 if ((tmp & 0xff) > 0x7f) { \
251 tmp += 0x100; \
253 d.VIS_L64(r) = tmp;
255 /* Reverse calculation order to handle overlap */
256 PMUL(1);
257 PMUL(0);
258 #undef PMUL
260 return d.ll;
263 uint64_t helper_fexpand(uint64_t src1, uint64_t src2)
265 VIS32 s;
266 VIS64 d;
268 s.l = (uint32_t)src1;
269 d.ll = src2;
270 d.VIS_W64(0) = s.VIS_B32(0) << 4;
271 d.VIS_W64(1) = s.VIS_B32(1) << 4;
272 d.VIS_W64(2) = s.VIS_B32(2) << 4;
273 d.VIS_W64(3) = s.VIS_B32(3) << 4;
275 return d.ll;
278 #define VIS_HELPER(name, F) \
279 uint64_t name##16(uint64_t src1, uint64_t src2) \
281 VIS64 s, d; \
283 s.ll = src1; \
284 d.ll = src2; \
286 d.VIS_W64(0) = F(d.VIS_W64(0), s.VIS_W64(0)); \
287 d.VIS_W64(1) = F(d.VIS_W64(1), s.VIS_W64(1)); \
288 d.VIS_W64(2) = F(d.VIS_W64(2), s.VIS_W64(2)); \
289 d.VIS_W64(3) = F(d.VIS_W64(3), s.VIS_W64(3)); \
291 return d.ll; \
294 uint32_t name##16s(uint32_t src1, uint32_t src2) \
296 VIS32 s, d; \
298 s.l = src1; \
299 d.l = src2; \
301 d.VIS_W32(0) = F(d.VIS_W32(0), s.VIS_W32(0)); \
302 d.VIS_W32(1) = F(d.VIS_W32(1), s.VIS_W32(1)); \
304 return d.l; \
307 uint64_t name##32(uint64_t src1, uint64_t src2) \
309 VIS64 s, d; \
311 s.ll = src1; \
312 d.ll = src2; \
314 d.VIS_L64(0) = F(d.VIS_L64(0), s.VIS_L64(0)); \
315 d.VIS_L64(1) = F(d.VIS_L64(1), s.VIS_L64(1)); \
317 return d.ll; \
320 uint32_t name##32s(uint32_t src1, uint32_t src2) \
322 VIS32 s, d; \
324 s.l = src1; \
325 d.l = src2; \
327 d.l = F(d.l, s.l); \
329 return d.l; \
332 #define FADD(a, b) ((a) + (b))
333 #define FSUB(a, b) ((a) - (b))
334 VIS_HELPER(helper_fpadd, FADD)
335 VIS_HELPER(helper_fpsub, FSUB)
337 #define VIS_CMPHELPER(name, F) \
338 uint64_t name##16(uint64_t src1, uint64_t src2) \
340 VIS64 s, d; \
342 s.ll = src1; \
343 d.ll = src2; \
345 d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0; \
346 d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0; \
347 d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0; \
348 d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0; \
349 d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0; \
351 return d.ll; \
354 uint64_t name##32(uint64_t src1, uint64_t src2) \
356 VIS64 s, d; \
358 s.ll = src1; \
359 d.ll = src2; \
361 d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0; \
362 d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0; \
363 d.VIS_L64(1) = 0; \
365 return d.ll; \
368 #define FCMPGT(a, b) ((a) > (b))
369 #define FCMPEQ(a, b) ((a) == (b))
370 #define FCMPLE(a, b) ((a) <= (b))
371 #define FCMPNE(a, b) ((a) != (b))
373 VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
374 VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
375 VIS_CMPHELPER(helper_fcmple, FCMPLE)
376 VIS_CMPHELPER(helper_fcmpne, FCMPNE)
378 uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2)
380 int i;
381 for (i = 0; i < 8; i++) {
382 int s1, s2;
384 s1 = (src1 >> (56 - (i * 8))) & 0xff;
385 s2 = (src2 >> (56 - (i * 8))) & 0xff;
387 /* Absolute value of difference. */
388 s1 -= s2;
389 if (s1 < 0) {
390 s1 = -s1;
393 sum += s1;
396 return sum;
399 uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2)
401 int scale = (gsr >> 3) & 0xf;
402 uint32_t ret = 0;
403 int byte;
405 for (byte = 0; byte < 4; byte++) {
406 uint32_t val;
407 int16_t src = rs2 >> (byte * 16);
408 int32_t scaled = src << scale;
409 int32_t from_fixed = scaled >> 7;
411 val = (from_fixed < 0 ? 0 :
412 from_fixed > 255 ? 255 : from_fixed);
414 ret |= val << (8 * byte);
417 return ret;
420 uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2)
422 int scale = (gsr >> 3) & 0x1f;
423 uint64_t ret = 0;
424 int word;
426 ret = (rs1 << 8) & ~(0x000000ff000000ffULL);
427 for (word = 0; word < 2; word++) {
428 uint64_t val;
429 int32_t src = rs2 >> (word * 32);
430 int64_t scaled = (int64_t)src << scale;
431 int64_t from_fixed = scaled >> 23;
433 val = (from_fixed < 0 ? 0 :
434 (from_fixed > 255) ? 255 : from_fixed);
436 ret |= val << (32 * word);
439 return ret;
442 uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2)
444 int scale = (gsr >> 3) & 0x1f;
445 uint32_t ret = 0;
446 int word;
448 for (word = 0; word < 2; word++) {
449 uint32_t val;
450 int32_t src = rs2 >> (word * 32);
451 int64_t scaled = (int64_t)src << scale;
452 int64_t from_fixed = scaled >> 16;
454 val = (from_fixed < -32768 ? -32768 :
455 from_fixed > 32767 ? 32767 : from_fixed);
457 ret |= (val & 0xffff) << (word * 16);
460 return ret;
463 uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2)
465 union {
466 uint64_t ll[2];
467 uint8_t b[16];
468 } s;
469 VIS64 r;
470 uint32_t i, mask, host;
472 /* Set up S such that we can index across all of the bytes. */
473 #ifdef HOST_WORDS_BIGENDIAN
474 s.ll[0] = src1;
475 s.ll[1] = src2;
476 host = 0;
477 #else
478 s.ll[1] = src1;
479 s.ll[0] = src2;
480 host = 15;
481 #endif
482 mask = gsr >> 32;
484 for (i = 0; i < 8; ++i) {
485 unsigned e = (mask >> (28 - i*4)) & 0xf;
486 r.VIS_B64(i) = s.b[e ^ host];
489 return r.ll;