xref: /qemu/target/sparc/vis_helper.c (revision d3ef26afde77fbdedd5b30282134ff99d0fe5cc5)
11bccec25SBlue Swirl /*
21bccec25SBlue Swirl  * VIS op helpers
31bccec25SBlue Swirl  *
41bccec25SBlue Swirl  *  Copyright (c) 2003-2005 Fabrice Bellard
51bccec25SBlue Swirl  *
61bccec25SBlue Swirl  * This library is free software; you can redistribute it and/or
71bccec25SBlue Swirl  * modify it under the terms of the GNU Lesser General Public
81bccec25SBlue Swirl  * License as published by the Free Software Foundation; either
95650b549SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
101bccec25SBlue Swirl  *
111bccec25SBlue Swirl  * This library is distributed in the hope that it will be useful,
121bccec25SBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
131bccec25SBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
141bccec25SBlue Swirl  * Lesser General Public License for more details.
151bccec25SBlue Swirl  *
161bccec25SBlue Swirl  * You should have received a copy of the GNU Lesser General Public
171bccec25SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
181bccec25SBlue Swirl  */
191bccec25SBlue Swirl 
20db5ebe5fSPeter Maydell #include "qemu/osdep.h"
211bccec25SBlue Swirl #include "cpu.h"
222ef6175aSRichard Henderson #include "exec/helper-proto.h"
231bccec25SBlue Swirl 
241bccec25SBlue Swirl /* This function uses non-native bit order */
251bccec25SBlue Swirl #define GET_FIELD(X, FROM, TO)                                  \
261bccec25SBlue Swirl     ((X) >> (63 - (TO)) & ((1ULL << ((TO) - (FROM) + 1)) - 1))
271bccec25SBlue Swirl 
281bccec25SBlue Swirl /* This function uses the order in the manuals, i.e. bit 0 is 2^0 */
291bccec25SBlue Swirl #define GET_FIELD_SP(X, FROM, TO)               \
301bccec25SBlue Swirl     GET_FIELD(X, 63 - (TO), 63 - (FROM))
311bccec25SBlue Swirl 
32f027c3b1SRichard Henderson target_ulong helper_array8(target_ulong pixel_addr, target_ulong cubesize)
331bccec25SBlue Swirl {
341bccec25SBlue Swirl     return (GET_FIELD_SP(pixel_addr, 60, 63) << (17 + 2 * cubesize)) |
351bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 39, 39 + cubesize - 1) << (17 + cubesize)) |
361bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 17 + cubesize - 1, 17) << 17) |
371bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 56, 59) << 13) |
381bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 35, 38) << 9) |
391bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 13, 16) << 5) |
401bccec25SBlue Swirl         (((pixel_addr >> 55) & 1) << 4) |
411bccec25SBlue Swirl         (GET_FIELD_SP(pixel_addr, 33, 34) << 2) |
421bccec25SBlue Swirl         GET_FIELD_SP(pixel_addr, 11, 12);
431bccec25SBlue Swirl }
441bccec25SBlue Swirl 
45e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
461bccec25SBlue Swirl #define VIS_B64(n) b[7 - (n)]
471bccec25SBlue Swirl #define VIS_W64(n) w[3 - (n)]
481bccec25SBlue Swirl #define VIS_SW64(n) sw[3 - (n)]
491bccec25SBlue Swirl #define VIS_L64(n) l[1 - (n)]
501bccec25SBlue Swirl #define VIS_B32(n) b[3 - (n)]
511bccec25SBlue Swirl #define VIS_W32(n) w[1 - (n)]
521bccec25SBlue Swirl #else
531bccec25SBlue Swirl #define VIS_B64(n) b[n]
541bccec25SBlue Swirl #define VIS_W64(n) w[n]
551bccec25SBlue Swirl #define VIS_SW64(n) sw[n]
561bccec25SBlue Swirl #define VIS_L64(n) l[n]
571bccec25SBlue Swirl #define VIS_B32(n) b[n]
581bccec25SBlue Swirl #define VIS_W32(n) w[n]
591bccec25SBlue Swirl #endif
601bccec25SBlue Swirl 
611bccec25SBlue Swirl typedef union {
621bccec25SBlue Swirl     uint8_t b[8];
631bccec25SBlue Swirl     uint16_t w[4];
641bccec25SBlue Swirl     int16_t sw[4];
651bccec25SBlue Swirl     uint32_t l[2];
661bccec25SBlue Swirl     uint64_t ll;
671bccec25SBlue Swirl     float64 d;
681bccec25SBlue Swirl } VIS64;
691bccec25SBlue Swirl 
701bccec25SBlue Swirl typedef union {
711bccec25SBlue Swirl     uint8_t b[4];
721bccec25SBlue Swirl     uint16_t w[2];
731bccec25SBlue Swirl     uint32_t l;
741bccec25SBlue Swirl     float32 f;
751bccec25SBlue Swirl } VIS32;
761bccec25SBlue Swirl 
77*d3ef26afSRichard Henderson uint64_t helper_fpmerge(uint32_t src1, uint32_t src2)
781bccec25SBlue Swirl {
79*d3ef26afSRichard Henderson     VIS32 s1, s2;
80*d3ef26afSRichard Henderson     VIS64 d;
811bccec25SBlue Swirl 
82*d3ef26afSRichard Henderson     s1.l = src1;
83*d3ef26afSRichard Henderson     s2.l = src2;
84*d3ef26afSRichard Henderson     d.ll = 0;
851bccec25SBlue Swirl 
86*d3ef26afSRichard Henderson     d.VIS_B64(7) = s1.VIS_B32(3);
87*d3ef26afSRichard Henderson     d.VIS_B64(6) = s2.VIS_B32(3);
88*d3ef26afSRichard Henderson     d.VIS_B64(5) = s1.VIS_B32(2);
89*d3ef26afSRichard Henderson     d.VIS_B64(4) = s2.VIS_B32(2);
90*d3ef26afSRichard Henderson     d.VIS_B64(3) = s1.VIS_B32(1);
91*d3ef26afSRichard Henderson     d.VIS_B64(2) = s2.VIS_B32(1);
92*d3ef26afSRichard Henderson     d.VIS_B64(1) = s1.VIS_B32(0);
93*d3ef26afSRichard Henderson     d.VIS_B64(0) = s2.VIS_B32(0);
941bccec25SBlue Swirl 
9503fb8cfcSRichard Henderson     return d.ll;
961bccec25SBlue Swirl }
971bccec25SBlue Swirl 
989157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2)
991bccec25SBlue Swirl {
1009157dcccSRichard Henderson     VIS64 d;
1019157dcccSRichard Henderson     VIS32 s;
1021bccec25SBlue Swirl     uint32_t tmp;
1031bccec25SBlue Swirl 
1049157dcccSRichard Henderson     s.l = src1;
10503fb8cfcSRichard Henderson     d.ll = src2;
1061bccec25SBlue Swirl 
1071bccec25SBlue Swirl #define PMUL(r)                                                 \
1089157dcccSRichard Henderson     tmp = (int32_t)d.VIS_SW64(r) * (int32_t)s.VIS_B32(r);       \
1091bccec25SBlue Swirl     if ((tmp & 0xff) > 0x7f) {                                  \
1101bccec25SBlue Swirl         tmp += 0x100;                                           \
1111bccec25SBlue Swirl     }                                                           \
1121bccec25SBlue Swirl     d.VIS_W64(r) = tmp >> 8;
1131bccec25SBlue Swirl 
1141bccec25SBlue Swirl     PMUL(0);
1151bccec25SBlue Swirl     PMUL(1);
1161bccec25SBlue Swirl     PMUL(2);
1171bccec25SBlue Swirl     PMUL(3);
1181bccec25SBlue Swirl #undef PMUL
1191bccec25SBlue Swirl 
12003fb8cfcSRichard Henderson     return d.ll;
1211bccec25SBlue Swirl }
1221bccec25SBlue Swirl 
123a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2)
1241bccec25SBlue Swirl {
125a859602cSRichard Henderson     VIS32 s;
126a859602cSRichard Henderson     VIS64 d;
1271bccec25SBlue Swirl     uint32_t tmp;
1281bccec25SBlue Swirl 
129a859602cSRichard Henderson     s.l = src1;
130a859602cSRichard Henderson     d.ll = 0;
1311bccec25SBlue Swirl 
1321bccec25SBlue Swirl #define PMUL(r)                                \
133a859602cSRichard Henderson     do {                                       \
134a859602cSRichard Henderson         tmp = src2 * (int32_t)s.VIS_B32(r);    \
1351bccec25SBlue Swirl         if ((tmp & 0xff) > 0x7f) {             \
1361bccec25SBlue Swirl             tmp += 0x100;                      \
1371bccec25SBlue Swirl         }                                      \
138a859602cSRichard Henderson         d.VIS_W64(r) = tmp >> 8;               \
139a859602cSRichard Henderson     } while (0)
1401bccec25SBlue Swirl 
1411bccec25SBlue Swirl     PMUL(0);
1421bccec25SBlue Swirl     PMUL(1);
1431bccec25SBlue Swirl     PMUL(2);
1441bccec25SBlue Swirl     PMUL(3);
1451bccec25SBlue Swirl #undef PMUL
1461bccec25SBlue Swirl 
14703fb8cfcSRichard Henderson     return d.ll;
1481bccec25SBlue Swirl }
1491bccec25SBlue Swirl 
150f027c3b1SRichard Henderson uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2)
1511bccec25SBlue Swirl {
1521bccec25SBlue Swirl     VIS64 s, d;
1531bccec25SBlue Swirl     uint32_t tmp;
1541bccec25SBlue Swirl 
15503fb8cfcSRichard Henderson     s.ll = src1;
15603fb8cfcSRichard Henderson     d.ll = src2;
1571bccec25SBlue Swirl 
1581bccec25SBlue Swirl #define PMUL(r)                                                         \
1591bccec25SBlue Swirl     tmp = (int32_t)d.VIS_SW64(r) * ((int32_t)s.VIS_SW64(r) >> 8);       \
1601bccec25SBlue Swirl     if ((tmp & 0xff) > 0x7f) {                                          \
1611bccec25SBlue Swirl         tmp += 0x100;                                                   \
1621bccec25SBlue Swirl     }                                                                   \
1631bccec25SBlue Swirl     d.VIS_W64(r) = tmp >> 8;
1641bccec25SBlue Swirl 
1651bccec25SBlue Swirl     PMUL(0);
1661bccec25SBlue Swirl     PMUL(1);
1671bccec25SBlue Swirl     PMUL(2);
1681bccec25SBlue Swirl     PMUL(3);
1691bccec25SBlue Swirl #undef PMUL
1701bccec25SBlue Swirl 
17103fb8cfcSRichard Henderson     return d.ll;
1721bccec25SBlue Swirl }
1731bccec25SBlue Swirl 
174f027c3b1SRichard Henderson uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2)
1751bccec25SBlue Swirl {
1761bccec25SBlue Swirl     VIS64 s, d;
1771bccec25SBlue Swirl     uint32_t tmp;
1781bccec25SBlue Swirl 
17903fb8cfcSRichard Henderson     s.ll = src1;
18003fb8cfcSRichard Henderson     d.ll = src2;
1811bccec25SBlue Swirl 
1821bccec25SBlue Swirl #define PMUL(r)                                                         \
1831bccec25SBlue Swirl     tmp = (int32_t)d.VIS_SW64(r) * ((uint32_t)s.VIS_B64(r * 2));        \
1841bccec25SBlue Swirl     if ((tmp & 0xff) > 0x7f) {                                          \
1851bccec25SBlue Swirl         tmp += 0x100;                                                   \
1861bccec25SBlue Swirl     }                                                                   \
1871bccec25SBlue Swirl     d.VIS_W64(r) = tmp >> 8;
1881bccec25SBlue Swirl 
1891bccec25SBlue Swirl     PMUL(0);
1901bccec25SBlue Swirl     PMUL(1);
1911bccec25SBlue Swirl     PMUL(2);
1921bccec25SBlue Swirl     PMUL(3);
1931bccec25SBlue Swirl #undef PMUL
1941bccec25SBlue Swirl 
19503fb8cfcSRichard Henderson     return d.ll;
1961bccec25SBlue Swirl }
1971bccec25SBlue Swirl 
1987b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2)
1991bccec25SBlue Swirl {
2001bccec25SBlue Swirl     VIS32 s;
2011bccec25SBlue Swirl     VIS64 d;
2021bccec25SBlue Swirl 
2037b616f36SRichard Henderson     s.l = src2;
2047b616f36SRichard Henderson     d.ll = 0;
2051bccec25SBlue Swirl     d.VIS_W64(0) = s.VIS_B32(0) << 4;
2061bccec25SBlue Swirl     d.VIS_W64(1) = s.VIS_B32(1) << 4;
2071bccec25SBlue Swirl     d.VIS_W64(2) = s.VIS_B32(2) << 4;
2081bccec25SBlue Swirl     d.VIS_W64(3) = s.VIS_B32(3) << 4;
2091bccec25SBlue Swirl 
21003fb8cfcSRichard Henderson     return d.ll;
2111bccec25SBlue Swirl }
2121bccec25SBlue Swirl 
2131bccec25SBlue Swirl #define VIS_CMPHELPER(name, F)                                    \
214f027c3b1SRichard Henderson     uint64_t name##16(uint64_t src1, uint64_t src2)               \
2151bccec25SBlue Swirl     {                                                             \
2161bccec25SBlue Swirl         VIS64 s, d;                                               \
2171bccec25SBlue Swirl                                                                   \
21803fb8cfcSRichard Henderson         s.ll = src1;                                              \
21903fb8cfcSRichard Henderson         d.ll = src2;                                              \
2201bccec25SBlue Swirl                                                                   \
2211bccec25SBlue Swirl         d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0;     \
2221bccec25SBlue Swirl         d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0;    \
2231bccec25SBlue Swirl         d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0;    \
2241bccec25SBlue Swirl         d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0;    \
2251bccec25SBlue Swirl         d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0;           \
2261bccec25SBlue Swirl                                                                   \
2271bccec25SBlue Swirl         return d.ll;                                              \
2281bccec25SBlue Swirl     }                                                             \
2291bccec25SBlue Swirl                                                                   \
230f027c3b1SRichard Henderson     uint64_t name##32(uint64_t src1, uint64_t src2)               \
2311bccec25SBlue Swirl     {                                                             \
2321bccec25SBlue Swirl         VIS64 s, d;                                               \
2331bccec25SBlue Swirl                                                                   \
23403fb8cfcSRichard Henderson         s.ll = src1;                                              \
23503fb8cfcSRichard Henderson         d.ll = src2;                                              \
2361bccec25SBlue Swirl                                                                   \
2371bccec25SBlue Swirl         d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0;     \
2381bccec25SBlue Swirl         d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0;    \
2391bccec25SBlue Swirl         d.VIS_L64(1) = 0;                                         \
2401bccec25SBlue Swirl                                                                   \
2411bccec25SBlue Swirl         return d.ll;                                              \
2421bccec25SBlue Swirl     }
2431bccec25SBlue Swirl 
2441bccec25SBlue Swirl #define FCMPGT(a, b) ((a) > (b))
2451bccec25SBlue Swirl #define FCMPEQ(a, b) ((a) == (b))
2461bccec25SBlue Swirl #define FCMPLE(a, b) ((a) <= (b))
2471bccec25SBlue Swirl #define FCMPNE(a, b) ((a) != (b))
2481bccec25SBlue Swirl 
2491bccec25SBlue Swirl VIS_CMPHELPER(helper_fcmpgt, FCMPGT)
2501bccec25SBlue Swirl VIS_CMPHELPER(helper_fcmpeq, FCMPEQ)
2511bccec25SBlue Swirl VIS_CMPHELPER(helper_fcmple, FCMPLE)
2521bccec25SBlue Swirl VIS_CMPHELPER(helper_fcmpne, FCMPNE)
253f888300bSRichard Henderson 
254f888300bSRichard Henderson uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2)
255f888300bSRichard Henderson {
256f888300bSRichard Henderson     int i;
257f888300bSRichard Henderson     for (i = 0; i < 8; i++) {
258f888300bSRichard Henderson         int s1, s2;
259f888300bSRichard Henderson 
260f888300bSRichard Henderson         s1 = (src1 >> (56 - (i * 8))) & 0xff;
261f888300bSRichard Henderson         s2 = (src2 >> (56 - (i * 8))) & 0xff;
262f888300bSRichard Henderson 
263f888300bSRichard Henderson         /* Absolute value of difference. */
264f888300bSRichard Henderson         s1 -= s2;
265f888300bSRichard Henderson         if (s1 < 0) {
266f888300bSRichard Henderson             s1 = -s1;
267f888300bSRichard Henderson         }
268f888300bSRichard Henderson 
269f888300bSRichard Henderson         sum += s1;
270f888300bSRichard Henderson     }
271f888300bSRichard Henderson 
272f888300bSRichard Henderson     return sum;
273f888300bSRichard Henderson }
2742dedf314SRichard Henderson 
2752dedf314SRichard Henderson uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2)
2762dedf314SRichard Henderson {
2772dedf314SRichard Henderson     int scale = (gsr >> 3) & 0xf;
2782dedf314SRichard Henderson     uint32_t ret = 0;
2792dedf314SRichard Henderson     int byte;
2802dedf314SRichard Henderson 
2812dedf314SRichard Henderson     for (byte = 0; byte < 4; byte++) {
2822dedf314SRichard Henderson         uint32_t val;
2832dedf314SRichard Henderson         int16_t src = rs2 >> (byte * 16);
2842dedf314SRichard Henderson         int32_t scaled = src << scale;
2852dedf314SRichard Henderson         int32_t from_fixed = scaled >> 7;
2862dedf314SRichard Henderson 
2872dedf314SRichard Henderson         val = (from_fixed < 0 ?  0 :
2882dedf314SRichard Henderson                from_fixed > 255 ?  255 : from_fixed);
2892dedf314SRichard Henderson 
2902dedf314SRichard Henderson         ret |= val << (8 * byte);
2912dedf314SRichard Henderson     }
2922dedf314SRichard Henderson 
2932dedf314SRichard Henderson     return ret;
2942dedf314SRichard Henderson }
2952dedf314SRichard Henderson 
2962dedf314SRichard Henderson uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2)
2972dedf314SRichard Henderson {
2982dedf314SRichard Henderson     int scale = (gsr >> 3) & 0x1f;
2992dedf314SRichard Henderson     uint64_t ret = 0;
3002dedf314SRichard Henderson     int word;
3012dedf314SRichard Henderson 
3022dedf314SRichard Henderson     ret = (rs1 << 8) & ~(0x000000ff000000ffULL);
3032dedf314SRichard Henderson     for (word = 0; word < 2; word++) {
3042dedf314SRichard Henderson         uint64_t val;
3052dedf314SRichard Henderson         int32_t src = rs2 >> (word * 32);
3062dedf314SRichard Henderson         int64_t scaled = (int64_t)src << scale;
3072dedf314SRichard Henderson         int64_t from_fixed = scaled >> 23;
3082dedf314SRichard Henderson 
3092dedf314SRichard Henderson         val = (from_fixed < 0 ? 0 :
3102dedf314SRichard Henderson                (from_fixed > 255) ? 255 : from_fixed);
3112dedf314SRichard Henderson 
3122dedf314SRichard Henderson         ret |= val << (32 * word);
3132dedf314SRichard Henderson     }
3142dedf314SRichard Henderson 
3152dedf314SRichard Henderson     return ret;
3162dedf314SRichard Henderson }
3172dedf314SRichard Henderson 
3182dedf314SRichard Henderson uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2)
3192dedf314SRichard Henderson {
3202dedf314SRichard Henderson     int scale = (gsr >> 3) & 0x1f;
3212dedf314SRichard Henderson     uint32_t ret = 0;
3222dedf314SRichard Henderson     int word;
3232dedf314SRichard Henderson 
3242dedf314SRichard Henderson     for (word = 0; word < 2; word++) {
3252dedf314SRichard Henderson         uint32_t val;
3262dedf314SRichard Henderson         int32_t src = rs2 >> (word * 32);
32712a3567cSPaolo Bonzini         int64_t scaled = (int64_t)src << scale;
3282dedf314SRichard Henderson         int64_t from_fixed = scaled >> 16;
3292dedf314SRichard Henderson 
3302dedf314SRichard Henderson         val = (from_fixed < -32768 ? -32768 :
3312dedf314SRichard Henderson                from_fixed > 32767 ?  32767 : from_fixed);
3322dedf314SRichard Henderson 
3332dedf314SRichard Henderson         ret |= (val & 0xffff) << (word * 16);
3342dedf314SRichard Henderson     }
3352dedf314SRichard Henderson 
3362dedf314SRichard Henderson     return ret;
3372dedf314SRichard Henderson }
338793a137aSRichard Henderson 
339520c0d8dSAndreas Färber uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2)
340793a137aSRichard Henderson {
341793a137aSRichard Henderson     union {
342793a137aSRichard Henderson         uint64_t ll[2];
343793a137aSRichard Henderson         uint8_t b[16];
344793a137aSRichard Henderson     } s;
345793a137aSRichard Henderson     VIS64 r;
346793a137aSRichard Henderson     uint32_t i, mask, host;
347793a137aSRichard Henderson 
348793a137aSRichard Henderson     /* Set up S such that we can index across all of the bytes.  */
349e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
350793a137aSRichard Henderson     s.ll[0] = src1;
351793a137aSRichard Henderson     s.ll[1] = src2;
352793a137aSRichard Henderson     host = 0;
353793a137aSRichard Henderson #else
354793a137aSRichard Henderson     s.ll[1] = src1;
355793a137aSRichard Henderson     s.ll[0] = src2;
356793a137aSRichard Henderson     host = 15;
357793a137aSRichard Henderson #endif
358793a137aSRichard Henderson     mask = gsr >> 32;
359793a137aSRichard Henderson 
360793a137aSRichard Henderson     for (i = 0; i < 8; ++i) {
361793a137aSRichard Henderson         unsigned e = (mask >> (28 - i*4)) & 0xf;
362793a137aSRichard Henderson         r.VIS_B64(i) = s.b[e ^ host];
363793a137aSRichard Henderson     }
364793a137aSRichard Henderson 
365793a137aSRichard Henderson     return r.ll;
366793a137aSRichard Henderson }
367