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)] 47*d6f898cfSRichard Henderson #define VIS_SB64(n) sb[7 - (n)] 48fcf5ef2aSThomas Huth #define VIS_W64(n) w[3 - (n)] 49fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[3 - (n)] 50fcf5ef2aSThomas Huth #define VIS_L64(n) l[1 - (n)] 51fcf5ef2aSThomas Huth #define VIS_B32(n) b[3 - (n)] 52fcf5ef2aSThomas Huth #define VIS_W32(n) w[1 - (n)] 53fcf5ef2aSThomas Huth #else 54fcf5ef2aSThomas Huth #define VIS_B64(n) b[n] 55*d6f898cfSRichard Henderson #define VIS_SB64(n) sb[n] 56fcf5ef2aSThomas Huth #define VIS_W64(n) w[n] 57fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[n] 58fcf5ef2aSThomas Huth #define VIS_L64(n) l[n] 59fcf5ef2aSThomas Huth #define VIS_B32(n) b[n] 60fcf5ef2aSThomas Huth #define VIS_W32(n) w[n] 61fcf5ef2aSThomas Huth #endif 62fcf5ef2aSThomas Huth 63fcf5ef2aSThomas Huth typedef union { 64fcf5ef2aSThomas Huth uint8_t b[8]; 65*d6f898cfSRichard Henderson int8_t sb[8]; 66fcf5ef2aSThomas Huth uint16_t w[4]; 67fcf5ef2aSThomas Huth int16_t sw[4]; 68fcf5ef2aSThomas Huth uint32_t l[2]; 69fcf5ef2aSThomas Huth uint64_t ll; 70fcf5ef2aSThomas Huth float64 d; 71fcf5ef2aSThomas Huth } VIS64; 72fcf5ef2aSThomas Huth 73fcf5ef2aSThomas Huth typedef union { 74fcf5ef2aSThomas Huth uint8_t b[4]; 75fcf5ef2aSThomas Huth uint16_t w[2]; 76fcf5ef2aSThomas Huth uint32_t l; 77fcf5ef2aSThomas Huth float32 f; 78fcf5ef2aSThomas Huth } VIS32; 79fcf5ef2aSThomas Huth 80d3ef26afSRichard Henderson uint64_t helper_fpmerge(uint32_t src1, uint32_t src2) 81fcf5ef2aSThomas Huth { 82d3ef26afSRichard Henderson VIS32 s1, s2; 83d3ef26afSRichard Henderson VIS64 d; 84fcf5ef2aSThomas Huth 85d3ef26afSRichard Henderson s1.l = src1; 86d3ef26afSRichard Henderson s2.l = src2; 87d3ef26afSRichard Henderson d.ll = 0; 88fcf5ef2aSThomas Huth 89d3ef26afSRichard Henderson d.VIS_B64(7) = s1.VIS_B32(3); 90d3ef26afSRichard Henderson d.VIS_B64(6) = s2.VIS_B32(3); 91d3ef26afSRichard Henderson d.VIS_B64(5) = s1.VIS_B32(2); 92d3ef26afSRichard Henderson d.VIS_B64(4) = s2.VIS_B32(2); 93d3ef26afSRichard Henderson d.VIS_B64(3) = s1.VIS_B32(1); 94d3ef26afSRichard Henderson d.VIS_B64(2) = s2.VIS_B32(1); 95d3ef26afSRichard Henderson d.VIS_B64(1) = s1.VIS_B32(0); 96d3ef26afSRichard Henderson d.VIS_B64(0) = s2.VIS_B32(0); 97fcf5ef2aSThomas Huth 98fcf5ef2aSThomas Huth return d.ll; 99fcf5ef2aSThomas Huth } 100fcf5ef2aSThomas Huth 101*d6f898cfSRichard Henderson static inline int do_ms16b(int x, int y) 102*d6f898cfSRichard Henderson { 103*d6f898cfSRichard Henderson return ((x * y) + 0x80) >> 8; 104*d6f898cfSRichard Henderson } 105*d6f898cfSRichard Henderson 1069157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2) 107fcf5ef2aSThomas Huth { 1089157dcccSRichard Henderson VIS64 d; 1099157dcccSRichard Henderson VIS32 s; 110fcf5ef2aSThomas Huth 1119157dcccSRichard Henderson s.l = src1; 112fcf5ef2aSThomas Huth d.ll = src2; 113fcf5ef2aSThomas Huth 114*d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), d.VIS_SW64(0)); 115*d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), d.VIS_SW64(1)); 116*d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), d.VIS_SW64(2)); 117*d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), d.VIS_SW64(3)); 118fcf5ef2aSThomas Huth 119fcf5ef2aSThomas Huth return d.ll; 120fcf5ef2aSThomas Huth } 121fcf5ef2aSThomas Huth 122a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2) 123fcf5ef2aSThomas Huth { 124a859602cSRichard Henderson VIS32 s; 125a859602cSRichard Henderson VIS64 d; 126fcf5ef2aSThomas Huth 127a859602cSRichard Henderson s.l = src1; 128a859602cSRichard Henderson d.ll = 0; 129fcf5ef2aSThomas Huth 130*d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), src2); 131*d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), src2); 132*d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), src2); 133*d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), src2); 134fcf5ef2aSThomas Huth 135fcf5ef2aSThomas Huth return d.ll; 136fcf5ef2aSThomas Huth } 137fcf5ef2aSThomas Huth 138fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2) 139fcf5ef2aSThomas Huth { 140fcf5ef2aSThomas Huth VIS64 s, d; 141fcf5ef2aSThomas Huth 142fcf5ef2aSThomas Huth s.ll = src1; 143fcf5ef2aSThomas Huth d.ll = src2; 144fcf5ef2aSThomas Huth 145*d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_SB64(1), d.VIS_SW64(0)); 146*d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_SB64(3), d.VIS_SW64(1)); 147*d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_SB64(5), d.VIS_SW64(2)); 148*d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_SB64(7), d.VIS_SW64(3)); 149fcf5ef2aSThomas Huth 150fcf5ef2aSThomas Huth return d.ll; 151fcf5ef2aSThomas Huth } 152fcf5ef2aSThomas Huth 153fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2) 154fcf5ef2aSThomas Huth { 155fcf5ef2aSThomas Huth VIS64 s, d; 156fcf5ef2aSThomas Huth 157fcf5ef2aSThomas Huth s.ll = src1; 158fcf5ef2aSThomas Huth d.ll = src2; 159fcf5ef2aSThomas Huth 160*d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B64(0), d.VIS_SW64(0)); 161*d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B64(2), d.VIS_SW64(1)); 162*d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B64(4), d.VIS_SW64(2)); 163*d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B64(6), d.VIS_SW64(3)); 164fcf5ef2aSThomas Huth 165fcf5ef2aSThomas Huth return d.ll; 166fcf5ef2aSThomas Huth } 167fcf5ef2aSThomas Huth 1687b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2) 169fcf5ef2aSThomas Huth { 170fcf5ef2aSThomas Huth VIS32 s; 171fcf5ef2aSThomas Huth VIS64 d; 172fcf5ef2aSThomas Huth 1737b616f36SRichard Henderson s.l = src2; 1747b616f36SRichard Henderson d.ll = 0; 175fcf5ef2aSThomas Huth d.VIS_W64(0) = s.VIS_B32(0) << 4; 176fcf5ef2aSThomas Huth d.VIS_W64(1) = s.VIS_B32(1) << 4; 177fcf5ef2aSThomas Huth d.VIS_W64(2) = s.VIS_B32(2) << 4; 178fcf5ef2aSThomas Huth d.VIS_W64(3) = s.VIS_B32(3) << 4; 179fcf5ef2aSThomas Huth 180fcf5ef2aSThomas Huth return d.ll; 181fcf5ef2aSThomas Huth } 182fcf5ef2aSThomas Huth 183fcf5ef2aSThomas Huth #define VIS_CMPHELPER(name, F) \ 184fcf5ef2aSThomas Huth uint64_t name##16(uint64_t src1, uint64_t src2) \ 185fcf5ef2aSThomas Huth { \ 186fcf5ef2aSThomas Huth VIS64 s, d; \ 187fcf5ef2aSThomas Huth \ 188fcf5ef2aSThomas Huth s.ll = src1; \ 189fcf5ef2aSThomas Huth d.ll = src2; \ 190fcf5ef2aSThomas Huth \ 191fcf5ef2aSThomas Huth d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0; \ 192fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0; \ 193fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0; \ 194fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0; \ 195fcf5ef2aSThomas Huth d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0; \ 196fcf5ef2aSThomas Huth \ 197fcf5ef2aSThomas Huth return d.ll; \ 198fcf5ef2aSThomas Huth } \ 199fcf5ef2aSThomas Huth \ 200fcf5ef2aSThomas Huth uint64_t name##32(uint64_t src1, uint64_t src2) \ 201fcf5ef2aSThomas Huth { \ 202fcf5ef2aSThomas Huth VIS64 s, d; \ 203fcf5ef2aSThomas Huth \ 204fcf5ef2aSThomas Huth s.ll = src1; \ 205fcf5ef2aSThomas Huth d.ll = src2; \ 206fcf5ef2aSThomas Huth \ 207fcf5ef2aSThomas Huth d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0; \ 208fcf5ef2aSThomas Huth d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0; \ 209fcf5ef2aSThomas Huth d.VIS_L64(1) = 0; \ 210fcf5ef2aSThomas Huth \ 211fcf5ef2aSThomas Huth return d.ll; \ 212fcf5ef2aSThomas Huth } 213fcf5ef2aSThomas Huth 214fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b)) 215fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b)) 216fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b)) 217fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b)) 218fcf5ef2aSThomas Huth 219fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT) 220fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ) 221fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE) 222fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE) 223fcf5ef2aSThomas Huth 224fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2) 225fcf5ef2aSThomas Huth { 226fcf5ef2aSThomas Huth int i; 227fcf5ef2aSThomas Huth for (i = 0; i < 8; i++) { 228fcf5ef2aSThomas Huth int s1, s2; 229fcf5ef2aSThomas Huth 230fcf5ef2aSThomas Huth s1 = (src1 >> (56 - (i * 8))) & 0xff; 231fcf5ef2aSThomas Huth s2 = (src2 >> (56 - (i * 8))) & 0xff; 232fcf5ef2aSThomas Huth 233fcf5ef2aSThomas Huth /* Absolute value of difference. */ 234fcf5ef2aSThomas Huth s1 -= s2; 235fcf5ef2aSThomas Huth if (s1 < 0) { 236fcf5ef2aSThomas Huth s1 = -s1; 237fcf5ef2aSThomas Huth } 238fcf5ef2aSThomas Huth 239fcf5ef2aSThomas Huth sum += s1; 240fcf5ef2aSThomas Huth } 241fcf5ef2aSThomas Huth 242fcf5ef2aSThomas Huth return sum; 243fcf5ef2aSThomas Huth } 244fcf5ef2aSThomas Huth 245fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2) 246fcf5ef2aSThomas Huth { 247fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0xf; 248fcf5ef2aSThomas Huth uint32_t ret = 0; 249fcf5ef2aSThomas Huth int byte; 250fcf5ef2aSThomas Huth 251fcf5ef2aSThomas Huth for (byte = 0; byte < 4; byte++) { 252fcf5ef2aSThomas Huth uint32_t val; 253fcf5ef2aSThomas Huth int16_t src = rs2 >> (byte * 16); 254fcf5ef2aSThomas Huth int32_t scaled = src << scale; 255fcf5ef2aSThomas Huth int32_t from_fixed = scaled >> 7; 256fcf5ef2aSThomas Huth 257fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 258fcf5ef2aSThomas Huth from_fixed > 255 ? 255 : from_fixed); 259fcf5ef2aSThomas Huth 260fcf5ef2aSThomas Huth ret |= val << (8 * byte); 261fcf5ef2aSThomas Huth } 262fcf5ef2aSThomas Huth 263fcf5ef2aSThomas Huth return ret; 264fcf5ef2aSThomas Huth } 265fcf5ef2aSThomas Huth 266fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2) 267fcf5ef2aSThomas Huth { 268fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 269fcf5ef2aSThomas Huth uint64_t ret = 0; 270fcf5ef2aSThomas Huth int word; 271fcf5ef2aSThomas Huth 272fcf5ef2aSThomas Huth ret = (rs1 << 8) & ~(0x000000ff000000ffULL); 273fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 274fcf5ef2aSThomas Huth uint64_t val; 275fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 276fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 277fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 23; 278fcf5ef2aSThomas Huth 279fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 280fcf5ef2aSThomas Huth (from_fixed > 255) ? 255 : from_fixed); 281fcf5ef2aSThomas Huth 282fcf5ef2aSThomas Huth ret |= val << (32 * word); 283fcf5ef2aSThomas Huth } 284fcf5ef2aSThomas Huth 285fcf5ef2aSThomas Huth return ret; 286fcf5ef2aSThomas Huth } 287fcf5ef2aSThomas Huth 288fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2) 289fcf5ef2aSThomas Huth { 290fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 291fcf5ef2aSThomas Huth uint32_t ret = 0; 292fcf5ef2aSThomas Huth int word; 293fcf5ef2aSThomas Huth 294fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 295fcf5ef2aSThomas Huth uint32_t val; 296fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 297fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 298fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 16; 299fcf5ef2aSThomas Huth 300fcf5ef2aSThomas Huth val = (from_fixed < -32768 ? -32768 : 301fcf5ef2aSThomas Huth from_fixed > 32767 ? 32767 : from_fixed); 302fcf5ef2aSThomas Huth 303fcf5ef2aSThomas Huth ret |= (val & 0xffff) << (word * 16); 304fcf5ef2aSThomas Huth } 305fcf5ef2aSThomas Huth 306fcf5ef2aSThomas Huth return ret; 307fcf5ef2aSThomas Huth } 308fcf5ef2aSThomas Huth 309fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2) 310fcf5ef2aSThomas Huth { 311fcf5ef2aSThomas Huth union { 312fcf5ef2aSThomas Huth uint64_t ll[2]; 313fcf5ef2aSThomas Huth uint8_t b[16]; 314fcf5ef2aSThomas Huth } s; 315fcf5ef2aSThomas Huth VIS64 r; 316fcf5ef2aSThomas Huth uint32_t i, mask, host; 317fcf5ef2aSThomas Huth 318fcf5ef2aSThomas Huth /* Set up S such that we can index across all of the bytes. */ 319e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 320fcf5ef2aSThomas Huth s.ll[0] = src1; 321fcf5ef2aSThomas Huth s.ll[1] = src2; 322fcf5ef2aSThomas Huth host = 0; 323fcf5ef2aSThomas Huth #else 324fcf5ef2aSThomas Huth s.ll[1] = src1; 325fcf5ef2aSThomas Huth s.ll[0] = src2; 326fcf5ef2aSThomas Huth host = 15; 327fcf5ef2aSThomas Huth #endif 328fcf5ef2aSThomas Huth mask = gsr >> 32; 329fcf5ef2aSThomas Huth 330fcf5ef2aSThomas Huth for (i = 0; i < 8; ++i) { 331fcf5ef2aSThomas Huth unsigned e = (mask >> (28 - i*4)) & 0xf; 332fcf5ef2aSThomas Huth r.VIS_B64(i) = s.b[e ^ host]; 333fcf5ef2aSThomas Huth } 334fcf5ef2aSThomas Huth 335fcf5ef2aSThomas Huth return r.ll; 336fcf5ef2aSThomas Huth } 337