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" 23*029b0283SRichard Henderson #include "crypto/clmul.h" 24fcf5ef2aSThomas Huth 25fa9079a8SRichard Henderson target_ulong helper_array8(target_ulong rs1, target_ulong rs2) 26fcf5ef2aSThomas Huth { 27fa9079a8SRichard Henderson /* 28fa9079a8SRichard Henderson * From Oracle SPARC Architecture 2015: 29fa9079a8SRichard Henderson * Architecturally, an illegal R[rs2] value (>5) causes the array 30fa9079a8SRichard Henderson * instructions to produce undefined results. For historic reference, 31fa9079a8SRichard Henderson * past implementations of these instructions have ignored R[rs2]{63:3} 32fa9079a8SRichard Henderson * and have treated R[rs2] values of 6 and 7 as if they were 5. 33fa9079a8SRichard Henderson */ 34fa9079a8SRichard Henderson target_ulong n = MIN(rs2 & 7, 5); 35fa9079a8SRichard Henderson 36fa9079a8SRichard Henderson target_ulong x_int = (rs1 >> 11) & 0x7ff; 37fa9079a8SRichard Henderson target_ulong y_int = (rs1 >> 33) & 0x7ff; 38fa9079a8SRichard Henderson target_ulong z_int = rs1 >> 55; 39fa9079a8SRichard Henderson 40fa9079a8SRichard Henderson target_ulong lower_x = x_int & 3; 41fa9079a8SRichard Henderson target_ulong lower_y = y_int & 3; 42fa9079a8SRichard Henderson target_ulong lower_z = z_int & 1; 43fa9079a8SRichard Henderson 44fa9079a8SRichard Henderson target_ulong middle_x = (x_int >> 2) & 15; 45fa9079a8SRichard Henderson target_ulong middle_y = (y_int >> 2) & 15; 46fa9079a8SRichard Henderson target_ulong middle_z = (z_int >> 1) & 15; 47fa9079a8SRichard Henderson 48fa9079a8SRichard Henderson target_ulong upper_x = (x_int >> 6) & ((1 << n) - 1); 49fa9079a8SRichard Henderson target_ulong upper_y = (y_int >> 6) & ((1 << n) - 1); 50fa9079a8SRichard Henderson target_ulong upper_z = z_int >> 5; 51fa9079a8SRichard Henderson 52fa9079a8SRichard Henderson return (upper_z << (17 + 2 * n)) 53fa9079a8SRichard Henderson | (upper_y << (17 + n)) 54fa9079a8SRichard Henderson | (upper_x << 17) 55fa9079a8SRichard Henderson | (middle_z << 13) 56fa9079a8SRichard Henderson | (middle_y << 9) 57fa9079a8SRichard Henderson | (middle_x << 5) 58fa9079a8SRichard Henderson | (lower_z << 4) 59fa9079a8SRichard Henderson | (lower_y << 2) 60fa9079a8SRichard Henderson | lower_x; 61fcf5ef2aSThomas Huth } 62fcf5ef2aSThomas Huth 63e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 64fcf5ef2aSThomas Huth #define VIS_B64(n) b[7 - (n)] 65d6f898cfSRichard Henderson #define VIS_SB64(n) sb[7 - (n)] 66fcf5ef2aSThomas Huth #define VIS_W64(n) w[3 - (n)] 67fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[3 - (n)] 68fcf5ef2aSThomas Huth #define VIS_L64(n) l[1 - (n)] 69fcf5ef2aSThomas Huth #define VIS_B32(n) b[3 - (n)] 70fcf5ef2aSThomas Huth #define VIS_W32(n) w[1 - (n)] 71fcf5ef2aSThomas Huth #else 72fcf5ef2aSThomas Huth #define VIS_B64(n) b[n] 73d6f898cfSRichard Henderson #define VIS_SB64(n) sb[n] 74fcf5ef2aSThomas Huth #define VIS_W64(n) w[n] 75fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[n] 76fcf5ef2aSThomas Huth #define VIS_L64(n) l[n] 77fcf5ef2aSThomas Huth #define VIS_B32(n) b[n] 78fcf5ef2aSThomas Huth #define VIS_W32(n) w[n] 79fcf5ef2aSThomas Huth #endif 80fcf5ef2aSThomas Huth 81fcf5ef2aSThomas Huth typedef union { 82fcf5ef2aSThomas Huth uint8_t b[8]; 83d6f898cfSRichard Henderson int8_t sb[8]; 84fcf5ef2aSThomas Huth uint16_t w[4]; 85fcf5ef2aSThomas Huth int16_t sw[4]; 86fcf5ef2aSThomas Huth uint32_t l[2]; 87fcf5ef2aSThomas Huth uint64_t ll; 88fcf5ef2aSThomas Huth float64 d; 89fcf5ef2aSThomas Huth } VIS64; 90fcf5ef2aSThomas Huth 91fcf5ef2aSThomas Huth typedef union { 92fcf5ef2aSThomas Huth uint8_t b[4]; 93fcf5ef2aSThomas Huth uint16_t w[2]; 94fcf5ef2aSThomas Huth uint32_t l; 95fcf5ef2aSThomas Huth float32 f; 96fcf5ef2aSThomas Huth } VIS32; 97fcf5ef2aSThomas Huth 98d3ef26afSRichard Henderson uint64_t helper_fpmerge(uint32_t src1, uint32_t src2) 99fcf5ef2aSThomas Huth { 100d3ef26afSRichard Henderson VIS32 s1, s2; 101d3ef26afSRichard Henderson VIS64 d; 102fcf5ef2aSThomas Huth 103d3ef26afSRichard Henderson s1.l = src1; 104d3ef26afSRichard Henderson s2.l = src2; 105d3ef26afSRichard Henderson d.ll = 0; 106fcf5ef2aSThomas Huth 107d3ef26afSRichard Henderson d.VIS_B64(7) = s1.VIS_B32(3); 108d3ef26afSRichard Henderson d.VIS_B64(6) = s2.VIS_B32(3); 109d3ef26afSRichard Henderson d.VIS_B64(5) = s1.VIS_B32(2); 110d3ef26afSRichard Henderson d.VIS_B64(4) = s2.VIS_B32(2); 111d3ef26afSRichard Henderson d.VIS_B64(3) = s1.VIS_B32(1); 112d3ef26afSRichard Henderson d.VIS_B64(2) = s2.VIS_B32(1); 113d3ef26afSRichard Henderson d.VIS_B64(1) = s1.VIS_B32(0); 114d3ef26afSRichard Henderson d.VIS_B64(0) = s2.VIS_B32(0); 115fcf5ef2aSThomas Huth 116fcf5ef2aSThomas Huth return d.ll; 117fcf5ef2aSThomas Huth } 118fcf5ef2aSThomas Huth 119d6f898cfSRichard Henderson static inline int do_ms16b(int x, int y) 120d6f898cfSRichard Henderson { 121d6f898cfSRichard Henderson return ((x * y) + 0x80) >> 8; 122d6f898cfSRichard Henderson } 123d6f898cfSRichard Henderson 1249157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2) 125fcf5ef2aSThomas Huth { 1269157dcccSRichard Henderson VIS64 d; 1279157dcccSRichard Henderson VIS32 s; 128fcf5ef2aSThomas Huth 1299157dcccSRichard Henderson s.l = src1; 130fcf5ef2aSThomas Huth d.ll = src2; 131fcf5ef2aSThomas Huth 132d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), d.VIS_SW64(0)); 133d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), d.VIS_SW64(1)); 134d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), d.VIS_SW64(2)); 135d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), d.VIS_SW64(3)); 136fcf5ef2aSThomas Huth 137fcf5ef2aSThomas Huth return d.ll; 138fcf5ef2aSThomas Huth } 139fcf5ef2aSThomas Huth 140a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2) 141fcf5ef2aSThomas Huth { 142a859602cSRichard Henderson VIS32 s; 143a859602cSRichard Henderson VIS64 d; 144fcf5ef2aSThomas Huth 145a859602cSRichard Henderson s.l = src1; 146a859602cSRichard Henderson d.ll = 0; 147fcf5ef2aSThomas Huth 148d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), src2); 149d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), src2); 150d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), src2); 151d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), src2); 152fcf5ef2aSThomas Huth 153fcf5ef2aSThomas Huth return d.ll; 154fcf5ef2aSThomas Huth } 155fcf5ef2aSThomas Huth 156fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2) 157fcf5ef2aSThomas Huth { 158fcf5ef2aSThomas Huth VIS64 s, d; 159fcf5ef2aSThomas Huth 160fcf5ef2aSThomas Huth s.ll = src1; 161fcf5ef2aSThomas Huth d.ll = src2; 162fcf5ef2aSThomas Huth 163d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_SB64(1), d.VIS_SW64(0)); 164d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_SB64(3), d.VIS_SW64(1)); 165d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_SB64(5), d.VIS_SW64(2)); 166d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_SB64(7), d.VIS_SW64(3)); 167fcf5ef2aSThomas Huth 168fcf5ef2aSThomas Huth return d.ll; 169fcf5ef2aSThomas Huth } 170fcf5ef2aSThomas Huth 171fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2) 172fcf5ef2aSThomas Huth { 173fcf5ef2aSThomas Huth VIS64 s, d; 174fcf5ef2aSThomas Huth 175fcf5ef2aSThomas Huth s.ll = src1; 176fcf5ef2aSThomas Huth d.ll = src2; 177fcf5ef2aSThomas Huth 178b5c96047SRichard Henderson d.VIS_W64(0) = (s.VIS_B64(0) * d.VIS_SW64(0) + 0x8000) >> 16; 179b5c96047SRichard Henderson d.VIS_W64(1) = (s.VIS_B64(2) * d.VIS_SW64(1) + 0x8000) >> 16; 180b5c96047SRichard Henderson d.VIS_W64(2) = (s.VIS_B64(4) * d.VIS_SW64(2) + 0x8000) >> 16; 181b5c96047SRichard Henderson d.VIS_W64(3) = (s.VIS_B64(6) * d.VIS_SW64(3) + 0x8000) >> 16; 182fcf5ef2aSThomas Huth 183fcf5ef2aSThomas Huth return d.ll; 184fcf5ef2aSThomas Huth } 185fcf5ef2aSThomas Huth 1867b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2) 187fcf5ef2aSThomas Huth { 188fcf5ef2aSThomas Huth VIS32 s; 189fcf5ef2aSThomas Huth VIS64 d; 190fcf5ef2aSThomas Huth 1917b616f36SRichard Henderson s.l = src2; 1927b616f36SRichard Henderson d.ll = 0; 193fcf5ef2aSThomas Huth d.VIS_W64(0) = s.VIS_B32(0) << 4; 194fcf5ef2aSThomas Huth d.VIS_W64(1) = s.VIS_B32(1) << 4; 195fcf5ef2aSThomas Huth d.VIS_W64(2) = s.VIS_B32(2) << 4; 196fcf5ef2aSThomas Huth d.VIS_W64(3) = s.VIS_B32(3) << 4; 197fcf5ef2aSThomas Huth 198fcf5ef2aSThomas Huth return d.ll; 199fcf5ef2aSThomas Huth } 200fcf5ef2aSThomas Huth 201fcf5ef2aSThomas Huth #define VIS_CMPHELPER(name, F) \ 202fcf5ef2aSThomas Huth uint64_t name##16(uint64_t src1, uint64_t src2) \ 203fcf5ef2aSThomas Huth { \ 204fcf5ef2aSThomas Huth VIS64 s, d; \ 205fcf5ef2aSThomas Huth \ 206fcf5ef2aSThomas Huth s.ll = src1; \ 207fcf5ef2aSThomas Huth d.ll = src2; \ 208fcf5ef2aSThomas Huth \ 209fcf5ef2aSThomas Huth d.VIS_W64(0) = F(s.VIS_W64(0), d.VIS_W64(0)) ? 1 : 0; \ 210fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(1), d.VIS_W64(1)) ? 2 : 0; \ 211fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(2), d.VIS_W64(2)) ? 4 : 0; \ 212fcf5ef2aSThomas Huth d.VIS_W64(0) |= F(s.VIS_W64(3), d.VIS_W64(3)) ? 8 : 0; \ 213fcf5ef2aSThomas Huth d.VIS_W64(1) = d.VIS_W64(2) = d.VIS_W64(3) = 0; \ 214fcf5ef2aSThomas Huth \ 215fcf5ef2aSThomas Huth return d.ll; \ 216fcf5ef2aSThomas Huth } \ 217fcf5ef2aSThomas Huth \ 218fcf5ef2aSThomas Huth uint64_t name##32(uint64_t src1, uint64_t src2) \ 219fcf5ef2aSThomas Huth { \ 220fcf5ef2aSThomas Huth VIS64 s, d; \ 221fcf5ef2aSThomas Huth \ 222fcf5ef2aSThomas Huth s.ll = src1; \ 223fcf5ef2aSThomas Huth d.ll = src2; \ 224fcf5ef2aSThomas Huth \ 225fcf5ef2aSThomas Huth d.VIS_L64(0) = F(s.VIS_L64(0), d.VIS_L64(0)) ? 1 : 0; \ 226fcf5ef2aSThomas Huth d.VIS_L64(0) |= F(s.VIS_L64(1), d.VIS_L64(1)) ? 2 : 0; \ 227fcf5ef2aSThomas Huth d.VIS_L64(1) = 0; \ 228fcf5ef2aSThomas Huth \ 229fcf5ef2aSThomas Huth return d.ll; \ 230fcf5ef2aSThomas Huth } 231fcf5ef2aSThomas Huth 232fcf5ef2aSThomas Huth #define FCMPGT(a, b) ((a) > (b)) 233fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b)) 234fcf5ef2aSThomas Huth #define FCMPLE(a, b) ((a) <= (b)) 235fcf5ef2aSThomas Huth #define FCMPNE(a, b) ((a) != (b)) 236fcf5ef2aSThomas Huth 237fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpgt, FCMPGT) 238fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpeq, FCMPEQ) 239fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmple, FCMPLE) 240fcf5ef2aSThomas Huth VIS_CMPHELPER(helper_fcmpne, FCMPNE) 241fcf5ef2aSThomas Huth 242669e0774SRichard Henderson uint64_t helper_fcmpeq8(uint64_t src1, uint64_t src2) 243669e0774SRichard Henderson { 244669e0774SRichard Henderson uint64_t a = src1 ^ src2; 245669e0774SRichard Henderson uint64_t m = 0x7f7f7f7f7f7f7f7fULL; 246669e0774SRichard Henderson uint64_t c = ~(((a & m) + m) | a | m); 247669e0774SRichard Henderson 248669e0774SRichard Henderson /* a.......b.......c.......d.......e.......f.......g.......h....... */ 249669e0774SRichard Henderson c |= c << 7; 250669e0774SRichard Henderson /* ab......bc......cd......de......ef......fg......gh......h....... */ 251669e0774SRichard Henderson c |= c << 14; 252669e0774SRichard Henderson /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */ 253669e0774SRichard Henderson c |= c << 28; 254669e0774SRichard Henderson /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */ 255669e0774SRichard Henderson return c >> 56; 256669e0774SRichard Henderson } 257669e0774SRichard Henderson 258669e0774SRichard Henderson uint64_t helper_fcmpne8(uint64_t src1, uint64_t src2) 259669e0774SRichard Henderson { 260669e0774SRichard Henderson return helper_fcmpeq8(src1, src2) ^ 0xff; 261669e0774SRichard Henderson } 262669e0774SRichard Henderson 263669e0774SRichard Henderson uint64_t helper_fcmpule8(uint64_t src1, uint64_t src2) 264669e0774SRichard Henderson { 265669e0774SRichard Henderson VIS64 s1, s2; 266669e0774SRichard Henderson uint64_t r = 0; 267669e0774SRichard Henderson 268669e0774SRichard Henderson s1.ll = src1; 269669e0774SRichard Henderson s2.ll = src2; 270669e0774SRichard Henderson 271669e0774SRichard Henderson for (int i = 0; i < 8; ++i) { 272669e0774SRichard Henderson r |= (s1.VIS_B64(i) <= s2.VIS_B64(i)) << i; 273669e0774SRichard Henderson } 274669e0774SRichard Henderson return r; 275669e0774SRichard Henderson } 276669e0774SRichard Henderson 277669e0774SRichard Henderson uint64_t helper_fcmpugt8(uint64_t src1, uint64_t src2) 278669e0774SRichard Henderson { 279669e0774SRichard Henderson return helper_fcmpule8(src1, src2) ^ 0xff; 280669e0774SRichard Henderson } 281669e0774SRichard Henderson 282fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2) 283fcf5ef2aSThomas Huth { 284fcf5ef2aSThomas Huth int i; 285fcf5ef2aSThomas Huth for (i = 0; i < 8; i++) { 286fcf5ef2aSThomas Huth int s1, s2; 287fcf5ef2aSThomas Huth 288fcf5ef2aSThomas Huth s1 = (src1 >> (56 - (i * 8))) & 0xff; 289fcf5ef2aSThomas Huth s2 = (src2 >> (56 - (i * 8))) & 0xff; 290fcf5ef2aSThomas Huth 291fcf5ef2aSThomas Huth /* Absolute value of difference. */ 292fcf5ef2aSThomas Huth s1 -= s2; 293fcf5ef2aSThomas Huth if (s1 < 0) { 294fcf5ef2aSThomas Huth s1 = -s1; 295fcf5ef2aSThomas Huth } 296fcf5ef2aSThomas Huth 297fcf5ef2aSThomas Huth sum += s1; 298fcf5ef2aSThomas Huth } 299fcf5ef2aSThomas Huth 300fcf5ef2aSThomas Huth return sum; 301fcf5ef2aSThomas Huth } 302fcf5ef2aSThomas Huth 303fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2) 304fcf5ef2aSThomas Huth { 305fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0xf; 306fcf5ef2aSThomas Huth uint32_t ret = 0; 307fcf5ef2aSThomas Huth int byte; 308fcf5ef2aSThomas Huth 309fcf5ef2aSThomas Huth for (byte = 0; byte < 4; byte++) { 310fcf5ef2aSThomas Huth uint32_t val; 311fcf5ef2aSThomas Huth int16_t src = rs2 >> (byte * 16); 312fcf5ef2aSThomas Huth int32_t scaled = src << scale; 313fcf5ef2aSThomas Huth int32_t from_fixed = scaled >> 7; 314fcf5ef2aSThomas Huth 315fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 316fcf5ef2aSThomas Huth from_fixed > 255 ? 255 : from_fixed); 317fcf5ef2aSThomas Huth 318fcf5ef2aSThomas Huth ret |= val << (8 * byte); 319fcf5ef2aSThomas Huth } 320fcf5ef2aSThomas Huth 321fcf5ef2aSThomas Huth return ret; 322fcf5ef2aSThomas Huth } 323fcf5ef2aSThomas Huth 324fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2) 325fcf5ef2aSThomas Huth { 326fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 327fcf5ef2aSThomas Huth uint64_t ret = 0; 328fcf5ef2aSThomas Huth int word; 329fcf5ef2aSThomas Huth 330fcf5ef2aSThomas Huth ret = (rs1 << 8) & ~(0x000000ff000000ffULL); 331fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 332fcf5ef2aSThomas Huth uint64_t val; 333fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 334fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 335fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 23; 336fcf5ef2aSThomas Huth 337fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 338fcf5ef2aSThomas Huth (from_fixed > 255) ? 255 : from_fixed); 339fcf5ef2aSThomas Huth 340fcf5ef2aSThomas Huth ret |= val << (32 * word); 341fcf5ef2aSThomas Huth } 342fcf5ef2aSThomas Huth 343fcf5ef2aSThomas Huth return ret; 344fcf5ef2aSThomas Huth } 345fcf5ef2aSThomas Huth 346fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2) 347fcf5ef2aSThomas Huth { 348fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 349fcf5ef2aSThomas Huth uint32_t ret = 0; 350fcf5ef2aSThomas Huth int word; 351fcf5ef2aSThomas Huth 352fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 353fcf5ef2aSThomas Huth uint32_t val; 354fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 355fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 356fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 16; 357fcf5ef2aSThomas Huth 358fcf5ef2aSThomas Huth val = (from_fixed < -32768 ? -32768 : 359fcf5ef2aSThomas Huth from_fixed > 32767 ? 32767 : from_fixed); 360fcf5ef2aSThomas Huth 361fcf5ef2aSThomas Huth ret |= (val & 0xffff) << (word * 16); 362fcf5ef2aSThomas Huth } 363fcf5ef2aSThomas Huth 364fcf5ef2aSThomas Huth return ret; 365fcf5ef2aSThomas Huth } 366fcf5ef2aSThomas Huth 367fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2) 368fcf5ef2aSThomas Huth { 369fcf5ef2aSThomas Huth union { 370fcf5ef2aSThomas Huth uint64_t ll[2]; 371fcf5ef2aSThomas Huth uint8_t b[16]; 372fcf5ef2aSThomas Huth } s; 373fcf5ef2aSThomas Huth VIS64 r; 374fcf5ef2aSThomas Huth uint32_t i, mask, host; 375fcf5ef2aSThomas Huth 376fcf5ef2aSThomas Huth /* Set up S such that we can index across all of the bytes. */ 377e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 378fcf5ef2aSThomas Huth s.ll[0] = src1; 379fcf5ef2aSThomas Huth s.ll[1] = src2; 380fcf5ef2aSThomas Huth host = 0; 381fcf5ef2aSThomas Huth #else 382fcf5ef2aSThomas Huth s.ll[1] = src1; 383fcf5ef2aSThomas Huth s.ll[0] = src2; 384fcf5ef2aSThomas Huth host = 15; 385fcf5ef2aSThomas Huth #endif 386fcf5ef2aSThomas Huth mask = gsr >> 32; 387fcf5ef2aSThomas Huth 388fcf5ef2aSThomas Huth for (i = 0; i < 8; ++i) { 389fcf5ef2aSThomas Huth unsigned e = (mask >> (28 - i*4)) & 0xf; 390fcf5ef2aSThomas Huth r.VIS_B64(i) = s.b[e ^ host]; 391fcf5ef2aSThomas Huth } 392fcf5ef2aSThomas Huth 393fcf5ef2aSThomas Huth return r.ll; 394fcf5ef2aSThomas Huth } 395c973b4e8SRichard Henderson 396c973b4e8SRichard Henderson uint64_t helper_cmask8(uint64_t gsr, uint64_t src) 397c973b4e8SRichard Henderson { 398c973b4e8SRichard Henderson uint32_t mask = 0; 399c973b4e8SRichard Henderson 400c973b4e8SRichard Henderson mask |= (src & 0x01 ? 0x00000007 : 0x0000000f); 401c973b4e8SRichard Henderson mask |= (src & 0x02 ? 0x00000060 : 0x000000e0); 402c973b4e8SRichard Henderson mask |= (src & 0x04 ? 0x00000500 : 0x00000d00); 403c973b4e8SRichard Henderson mask |= (src & 0x08 ? 0x00004000 : 0x0000c000); 404c973b4e8SRichard Henderson mask |= (src & 0x10 ? 0x00030000 : 0x000b0000); 405c973b4e8SRichard Henderson mask |= (src & 0x20 ? 0x00200000 : 0x00a00000); 406c973b4e8SRichard Henderson mask |= (src & 0x40 ? 0x01000000 : 0x09000000); 407c973b4e8SRichard Henderson mask |= (src & 0x80 ? 0x00000000 : 0x80000000); 408c973b4e8SRichard Henderson 409c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 410c973b4e8SRichard Henderson } 411c973b4e8SRichard Henderson 412c973b4e8SRichard Henderson uint64_t helper_cmask16(uint64_t gsr, uint64_t src) 413c973b4e8SRichard Henderson { 414c973b4e8SRichard Henderson uint32_t mask = 0; 415c973b4e8SRichard Henderson 416c973b4e8SRichard Henderson mask |= (src & 0x1 ? 0x00000067 : 0x000000ef); 417c973b4e8SRichard Henderson mask |= (src & 0x2 ? 0x00004500 : 0x0000cd00); 418c973b4e8SRichard Henderson mask |= (src & 0x4 ? 0x00230000 : 0x00ab0000); 419c973b4e8SRichard Henderson mask |= (src & 0x8 ? 0x01000000 : 0x89000000); 420c973b4e8SRichard Henderson 421c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 422c973b4e8SRichard Henderson } 423c973b4e8SRichard Henderson 424c973b4e8SRichard Henderson uint64_t helper_cmask32(uint64_t gsr, uint64_t src) 425c973b4e8SRichard Henderson { 426c973b4e8SRichard Henderson uint32_t mask = 0; 427c973b4e8SRichard Henderson 428c973b4e8SRichard Henderson mask |= (src & 0x1 ? 0x00004567 : 0x0000cdef); 429c973b4e8SRichard Henderson mask |= (src & 0x2 ? 0x01230000 : 0x89ab0000); 430c973b4e8SRichard Henderson 431c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 432c973b4e8SRichard Henderson } 4337837185eSRichard Henderson 4347837185eSRichard Henderson static inline uint16_t do_fchksm16(uint16_t src1, uint16_t src2) 4357837185eSRichard Henderson { 4367837185eSRichard Henderson uint16_t a = src1 + src2; 4377837185eSRichard Henderson uint16_t c = a < src1; 4387837185eSRichard Henderson return a + c; 4397837185eSRichard Henderson } 4407837185eSRichard Henderson 4417837185eSRichard Henderson uint64_t helper_fchksm16(uint64_t src1, uint64_t src2) 4427837185eSRichard Henderson { 4437837185eSRichard Henderson VIS64 r, s1, s2; 4447837185eSRichard Henderson 4457837185eSRichard Henderson s1.ll = src1; 4467837185eSRichard Henderson s2.ll = src2; 4477837185eSRichard Henderson r.ll = 0; 4487837185eSRichard Henderson 4497837185eSRichard Henderson r.VIS_W64(0) = do_fchksm16(s1.VIS_W64(0), s2.VIS_W64(0)); 4507837185eSRichard Henderson r.VIS_W64(1) = do_fchksm16(s1.VIS_W64(1), s2.VIS_W64(1)); 4517837185eSRichard Henderson r.VIS_W64(2) = do_fchksm16(s1.VIS_W64(2), s2.VIS_W64(2)); 4527837185eSRichard Henderson r.VIS_W64(3) = do_fchksm16(s1.VIS_W64(3), s2.VIS_W64(3)); 4537837185eSRichard Henderson 4547837185eSRichard Henderson return r.ll; 4557837185eSRichard Henderson } 456d6ff1ccbSRichard Henderson 457d6ff1ccbSRichard Henderson static inline int16_t do_fmean16(int16_t src1, int16_t src2) 458d6ff1ccbSRichard Henderson { 459d6ff1ccbSRichard Henderson return (src1 + src2 + 1) / 2; 460d6ff1ccbSRichard Henderson } 461d6ff1ccbSRichard Henderson 462d6ff1ccbSRichard Henderson uint64_t helper_fmean16(uint64_t src1, uint64_t src2) 463d6ff1ccbSRichard Henderson { 464d6ff1ccbSRichard Henderson VIS64 r, s1, s2; 465d6ff1ccbSRichard Henderson 466d6ff1ccbSRichard Henderson s1.ll = src1; 467d6ff1ccbSRichard Henderson s2.ll = src2; 468d6ff1ccbSRichard Henderson r.ll = 0; 469d6ff1ccbSRichard Henderson 470d6ff1ccbSRichard Henderson r.VIS_SW64(0) = do_fmean16(s1.VIS_SW64(0), s2.VIS_SW64(0)); 471d6ff1ccbSRichard Henderson r.VIS_SW64(1) = do_fmean16(s1.VIS_SW64(1), s2.VIS_SW64(1)); 472d6ff1ccbSRichard Henderson r.VIS_SW64(2) = do_fmean16(s1.VIS_SW64(2), s2.VIS_SW64(2)); 473d6ff1ccbSRichard Henderson r.VIS_SW64(3) = do_fmean16(s1.VIS_SW64(3), s2.VIS_SW64(3)); 474d6ff1ccbSRichard Henderson 475d6ff1ccbSRichard Henderson return r.ll; 476d6ff1ccbSRichard Henderson } 477fbc5c8d4SRichard Henderson 478fbc5c8d4SRichard Henderson uint64_t helper_fslas16(uint64_t src1, uint64_t src2) 479fbc5c8d4SRichard Henderson { 480fbc5c8d4SRichard Henderson VIS64 r, s1, s2; 481fbc5c8d4SRichard Henderson 482fbc5c8d4SRichard Henderson s1.ll = src1; 483fbc5c8d4SRichard Henderson s2.ll = src2; 484fbc5c8d4SRichard Henderson r.ll = 0; 485fbc5c8d4SRichard Henderson 486fbc5c8d4SRichard Henderson for (int i = 0; i < 4; ++i) { 487fbc5c8d4SRichard Henderson int t = s1.VIS_SW64(i) << (s2.VIS_W64(i) % 16); 488fbc5c8d4SRichard Henderson t = MIN(t, INT16_MAX); 489fbc5c8d4SRichard Henderson t = MAX(t, INT16_MIN); 490fbc5c8d4SRichard Henderson r.VIS_SW64(i) = t; 491fbc5c8d4SRichard Henderson } 492fbc5c8d4SRichard Henderson 493fbc5c8d4SRichard Henderson return r.ll; 494fbc5c8d4SRichard Henderson } 495fbc5c8d4SRichard Henderson 496fbc5c8d4SRichard Henderson uint64_t helper_fslas32(uint64_t src1, uint64_t src2) 497fbc5c8d4SRichard Henderson { 498fbc5c8d4SRichard Henderson VIS64 r, s1, s2; 499fbc5c8d4SRichard Henderson 500fbc5c8d4SRichard Henderson s1.ll = src1; 501fbc5c8d4SRichard Henderson s2.ll = src2; 502fbc5c8d4SRichard Henderson r.ll = 0; 503fbc5c8d4SRichard Henderson 504fbc5c8d4SRichard Henderson for (int i = 0; i < 2; ++i) { 505fbc5c8d4SRichard Henderson int64_t t = (int64_t)(int32_t)s1.VIS_L64(i) << (s2.VIS_L64(i) % 32); 506fbc5c8d4SRichard Henderson t = MIN(t, INT32_MAX); 507fbc5c8d4SRichard Henderson t = MAX(t, INT32_MIN); 508fbc5c8d4SRichard Henderson r.VIS_L64(i) = t; 509fbc5c8d4SRichard Henderson } 510fbc5c8d4SRichard Henderson 511fbc5c8d4SRichard Henderson return r.ll; 512fbc5c8d4SRichard Henderson } 513*029b0283SRichard Henderson 514*029b0283SRichard Henderson uint64_t helper_xmulx(uint64_t src1, uint64_t src2) 515*029b0283SRichard Henderson { 516*029b0283SRichard Henderson return int128_getlo(clmul_64(src1, src2)); 517*029b0283SRichard Henderson } 518*029b0283SRichard Henderson 519*029b0283SRichard Henderson uint64_t helper_xmulxhi(uint64_t src1, uint64_t src2) 520*029b0283SRichard Henderson { 521*029b0283SRichard Henderson return int128_gethi(clmul_64(src1, src2)); 522*029b0283SRichard Henderson } 523