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)] 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 263*7b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2) 264fcf5ef2aSThomas Huth { 265fcf5ef2aSThomas Huth VIS32 s; 266fcf5ef2aSThomas Huth VIS64 d; 267fcf5ef2aSThomas Huth 268*7b616f36SRichard Henderson s.l = src2; 269*7b616f36SRichard Henderson d.ll = 0; 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_CMPHELPER(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(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0; \ 287fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0; \ 288fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0; \ 289fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0; \ 290fcf5ef2aSThomas Huth d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0; \ 291fcf5ef2aSThomas Huth \ 292fcf5ef2aSThomas Huth return d.ll; \ 293fcf5ef2aSThomas Huth } \ 294fcf5ef2aSThomas Huth \ 295fcf5ef2aSThomas Huth uint64_t name##32(uint64_t src1, uint64_t src2) \ 296fcf5ef2aSThomas Huth { \ 297fcf5ef2aSThomas Huth VIS64 s, d; \ 298fcf5ef2aSThomas Huth \ 299fcf5ef2aSThomas Huth s.ll = src1; \ 300fcf5ef2aSThomas Huth d.ll = src2; \ 301fcf5ef2aSThomas Huth \ 302fcf5ef2aSThomas Huth d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0; \ 303fcf5ef2aSThomas Huth d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0; \ 304fcf5ef2aSThomas Huth d.VIS_L64(1) = 0; \ 305fcf5ef2aSThomas Huth \ 306fcf5ef2aSThomas Huth return d.ll; \ 307fcf5ef2aSThomas Huth } 308fcf5ef2aSThomas Huth 309fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b)) 310fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b)) 311fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b)) 312fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b)) 313fcf5ef2aSThomas Huth 314fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT) 315fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ) 316fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE) 317fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE) 318fcf5ef2aSThomas Huth 319fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2) 320fcf5ef2aSThomas Huth { 321fcf5ef2aSThomas Huth int i; 322fcf5ef2aSThomas Huth for (i = 0; i < 8; i++) { 323fcf5ef2aSThomas Huth int s1, s2; 324fcf5ef2aSThomas Huth 325fcf5ef2aSThomas Huth s1 = (src1 >> (56 - (i * 8))) & 0xff; 326fcf5ef2aSThomas Huth s2 = (src2 >> (56 - (i * 8))) & 0xff; 327fcf5ef2aSThomas Huth 328fcf5ef2aSThomas Huth /* Absolute value of difference. */ 329fcf5ef2aSThomas Huth s1 -= s2; 330fcf5ef2aSThomas Huth if (s1 < 0) { 331fcf5ef2aSThomas Huth s1 = -s1; 332fcf5ef2aSThomas Huth } 333fcf5ef2aSThomas Huth 334fcf5ef2aSThomas Huth sum += s1; 335fcf5ef2aSThomas Huth } 336fcf5ef2aSThomas Huth 337fcf5ef2aSThomas Huth return sum; 338fcf5ef2aSThomas Huth } 339fcf5ef2aSThomas Huth 340fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2) 341fcf5ef2aSThomas Huth { 342fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0xf; 343fcf5ef2aSThomas Huth uint32_t ret = 0; 344fcf5ef2aSThomas Huth int byte; 345fcf5ef2aSThomas Huth 346fcf5ef2aSThomas Huth for (byte = 0; byte < 4; byte++) { 347fcf5ef2aSThomas Huth uint32_t val; 348fcf5ef2aSThomas Huth int16_t src = rs2 >> (byte * 16); 349fcf5ef2aSThomas Huth int32_t scaled = src << scale; 350fcf5ef2aSThomas Huth int32_t from_fixed = scaled >> 7; 351fcf5ef2aSThomas Huth 352fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 353fcf5ef2aSThomas Huth from_fixed > 255 ? 255 : from_fixed); 354fcf5ef2aSThomas Huth 355fcf5ef2aSThomas Huth ret |= val << (8 * byte); 356fcf5ef2aSThomas Huth } 357fcf5ef2aSThomas Huth 358fcf5ef2aSThomas Huth return ret; 359fcf5ef2aSThomas Huth } 360fcf5ef2aSThomas Huth 361fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2) 362fcf5ef2aSThomas Huth { 363fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 364fcf5ef2aSThomas Huth uint64_t ret = 0; 365fcf5ef2aSThomas Huth int word; 366fcf5ef2aSThomas Huth 367fcf5ef2aSThomas Huth ret = (rs1 << 8) & ~(0x000000ff000000ffULL); 368fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 369fcf5ef2aSThomas Huth uint64_t val; 370fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 371fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 372fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 23; 373fcf5ef2aSThomas Huth 374fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 375fcf5ef2aSThomas Huth (from_fixed > 255) ? 255 : from_fixed); 376fcf5ef2aSThomas Huth 377fcf5ef2aSThomas Huth ret |= val << (32 * word); 378fcf5ef2aSThomas Huth } 379fcf5ef2aSThomas Huth 380fcf5ef2aSThomas Huth return ret; 381fcf5ef2aSThomas Huth } 382fcf5ef2aSThomas Huth 383fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2) 384fcf5ef2aSThomas Huth { 385fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 386fcf5ef2aSThomas Huth uint32_t ret = 0; 387fcf5ef2aSThomas Huth int word; 388fcf5ef2aSThomas Huth 389fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 390fcf5ef2aSThomas Huth uint32_t val; 391fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 392fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 393fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 16; 394fcf5ef2aSThomas Huth 395fcf5ef2aSThomas Huth val = (from_fixed < -32768 ? -32768 : 396fcf5ef2aSThomas Huth from_fixed > 32767 ? 32767 : from_fixed); 397fcf5ef2aSThomas Huth 398fcf5ef2aSThomas Huth ret |= (val & 0xffff) << (word * 16); 399fcf5ef2aSThomas Huth } 400fcf5ef2aSThomas Huth 401fcf5ef2aSThomas Huth return ret; 402fcf5ef2aSThomas Huth } 403fcf5ef2aSThomas Huth 404fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2) 405fcf5ef2aSThomas Huth { 406fcf5ef2aSThomas Huth union { 407fcf5ef2aSThomas Huth uint64_t ll[2]; 408fcf5ef2aSThomas Huth uint8_t b[16]; 409fcf5ef2aSThomas Huth } s; 410fcf5ef2aSThomas Huth VIS64 r; 411fcf5ef2aSThomas Huth uint32_t i, mask, host; 412fcf5ef2aSThomas Huth 413fcf5ef2aSThomas Huth /* Set up S such that we can index across all of the bytes. */ 414e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 415fcf5ef2aSThomas Huth s.ll[0] = src1; 416fcf5ef2aSThomas Huth s.ll[1] = src2; 417fcf5ef2aSThomas Huth host = 0; 418fcf5ef2aSThomas Huth #else 419fcf5ef2aSThomas Huth s.ll[1] = src1; 420fcf5ef2aSThomas Huth s.ll[0] = src2; 421fcf5ef2aSThomas Huth host = 15; 422fcf5ef2aSThomas Huth #endif 423fcf5ef2aSThomas Huth mask = gsr >> 32; 424fcf5ef2aSThomas Huth 425fcf5ef2aSThomas Huth for (i = 0; i < 8; ++i) { 426fcf5ef2aSThomas Huth unsigned e = (mask >> (28 - i*4)) & 0xf; 427fcf5ef2aSThomas Huth r.VIS_B64(i) = s.b[e ^ host]; 428fcf5ef2aSThomas Huth } 429fcf5ef2aSThomas Huth 430fcf5ef2aSThomas Huth return r.ll; 431fcf5ef2aSThomas Huth } 432