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" 23029b0283SRichard 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)] 69*b3c934ddSRichard Henderson #define VIS_SL64(n) sl[1 - (n)] 70fcf5ef2aSThomas Huth #define VIS_B32(n) b[3 - (n)] 71fcf5ef2aSThomas Huth #define VIS_W32(n) w[1 - (n)] 72fcf5ef2aSThomas Huth #else 73fcf5ef2aSThomas Huth #define VIS_B64(n) b[n] 74d6f898cfSRichard Henderson #define VIS_SB64(n) sb[n] 75fcf5ef2aSThomas Huth #define VIS_W64(n) w[n] 76fcf5ef2aSThomas Huth #define VIS_SW64(n) sw[n] 77fcf5ef2aSThomas Huth #define VIS_L64(n) l[n] 78*b3c934ddSRichard Henderson #define VIS_SL64(n) sl[n] 79fcf5ef2aSThomas Huth #define VIS_B32(n) b[n] 80fcf5ef2aSThomas Huth #define VIS_W32(n) w[n] 81fcf5ef2aSThomas Huth #endif 82fcf5ef2aSThomas Huth 83fcf5ef2aSThomas Huth typedef union { 84fcf5ef2aSThomas Huth uint8_t b[8]; 85d6f898cfSRichard Henderson int8_t sb[8]; 86fcf5ef2aSThomas Huth uint16_t w[4]; 87fcf5ef2aSThomas Huth int16_t sw[4]; 88fcf5ef2aSThomas Huth uint32_t l[2]; 89*b3c934ddSRichard Henderson int32_t sl[2]; 90fcf5ef2aSThomas Huth uint64_t ll; 91fcf5ef2aSThomas Huth float64 d; 92fcf5ef2aSThomas Huth } VIS64; 93fcf5ef2aSThomas Huth 94fcf5ef2aSThomas Huth typedef union { 95fcf5ef2aSThomas Huth uint8_t b[4]; 96fcf5ef2aSThomas Huth uint16_t w[2]; 97fcf5ef2aSThomas Huth uint32_t l; 98fcf5ef2aSThomas Huth float32 f; 99fcf5ef2aSThomas Huth } VIS32; 100fcf5ef2aSThomas Huth 101d3ef26afSRichard Henderson uint64_t helper_fpmerge(uint32_t src1, uint32_t src2) 102fcf5ef2aSThomas Huth { 103d3ef26afSRichard Henderson VIS32 s1, s2; 104d3ef26afSRichard Henderson VIS64 d; 105fcf5ef2aSThomas Huth 106d3ef26afSRichard Henderson s1.l = src1; 107d3ef26afSRichard Henderson s2.l = src2; 108d3ef26afSRichard Henderson d.ll = 0; 109fcf5ef2aSThomas Huth 110d3ef26afSRichard Henderson d.VIS_B64(7) = s1.VIS_B32(3); 111d3ef26afSRichard Henderson d.VIS_B64(6) = s2.VIS_B32(3); 112d3ef26afSRichard Henderson d.VIS_B64(5) = s1.VIS_B32(2); 113d3ef26afSRichard Henderson d.VIS_B64(4) = s2.VIS_B32(2); 114d3ef26afSRichard Henderson d.VIS_B64(3) = s1.VIS_B32(1); 115d3ef26afSRichard Henderson d.VIS_B64(2) = s2.VIS_B32(1); 116d3ef26afSRichard Henderson d.VIS_B64(1) = s1.VIS_B32(0); 117d3ef26afSRichard Henderson d.VIS_B64(0) = s2.VIS_B32(0); 118fcf5ef2aSThomas Huth 119fcf5ef2aSThomas Huth return d.ll; 120fcf5ef2aSThomas Huth } 121fcf5ef2aSThomas Huth 122d6f898cfSRichard Henderson static inline int do_ms16b(int x, int y) 123d6f898cfSRichard Henderson { 124d6f898cfSRichard Henderson return ((x * y) + 0x80) >> 8; 125d6f898cfSRichard Henderson } 126d6f898cfSRichard Henderson 1279157dcccSRichard Henderson uint64_t helper_fmul8x16(uint32_t src1, uint64_t src2) 128fcf5ef2aSThomas Huth { 1299157dcccSRichard Henderson VIS64 d; 1309157dcccSRichard Henderson VIS32 s; 131fcf5ef2aSThomas Huth 1329157dcccSRichard Henderson s.l = src1; 133fcf5ef2aSThomas Huth d.ll = src2; 134fcf5ef2aSThomas Huth 135d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), d.VIS_SW64(0)); 136d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), d.VIS_SW64(1)); 137d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), d.VIS_SW64(2)); 138d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), d.VIS_SW64(3)); 139fcf5ef2aSThomas Huth 140fcf5ef2aSThomas Huth return d.ll; 141fcf5ef2aSThomas Huth } 142fcf5ef2aSThomas Huth 143a859602cSRichard Henderson uint64_t helper_fmul8x16a(uint32_t src1, int32_t src2) 144fcf5ef2aSThomas Huth { 145a859602cSRichard Henderson VIS32 s; 146a859602cSRichard Henderson VIS64 d; 147fcf5ef2aSThomas Huth 148a859602cSRichard Henderson s.l = src1; 149a859602cSRichard Henderson d.ll = 0; 150fcf5ef2aSThomas Huth 151d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_B32(0), src2); 152d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_B32(1), src2); 153d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_B32(2), src2); 154d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_B32(3), src2); 155fcf5ef2aSThomas Huth 156fcf5ef2aSThomas Huth return d.ll; 157fcf5ef2aSThomas Huth } 158fcf5ef2aSThomas Huth 159fcf5ef2aSThomas Huth uint64_t helper_fmul8sux16(uint64_t src1, uint64_t src2) 160fcf5ef2aSThomas Huth { 161fcf5ef2aSThomas Huth VIS64 s, d; 162fcf5ef2aSThomas Huth 163fcf5ef2aSThomas Huth s.ll = src1; 164fcf5ef2aSThomas Huth d.ll = src2; 165fcf5ef2aSThomas Huth 166d6f898cfSRichard Henderson d.VIS_W64(0) = do_ms16b(s.VIS_SB64(1), d.VIS_SW64(0)); 167d6f898cfSRichard Henderson d.VIS_W64(1) = do_ms16b(s.VIS_SB64(3), d.VIS_SW64(1)); 168d6f898cfSRichard Henderson d.VIS_W64(2) = do_ms16b(s.VIS_SB64(5), d.VIS_SW64(2)); 169d6f898cfSRichard Henderson d.VIS_W64(3) = do_ms16b(s.VIS_SB64(7), d.VIS_SW64(3)); 170fcf5ef2aSThomas Huth 171fcf5ef2aSThomas Huth return d.ll; 172fcf5ef2aSThomas Huth } 173fcf5ef2aSThomas Huth 174fcf5ef2aSThomas Huth uint64_t helper_fmul8ulx16(uint64_t src1, uint64_t src2) 175fcf5ef2aSThomas Huth { 176fcf5ef2aSThomas Huth VIS64 s, d; 177fcf5ef2aSThomas Huth 178fcf5ef2aSThomas Huth s.ll = src1; 179fcf5ef2aSThomas Huth d.ll = src2; 180fcf5ef2aSThomas Huth 181b5c96047SRichard Henderson d.VIS_W64(0) = (s.VIS_B64(0) * d.VIS_SW64(0) + 0x8000) >> 16; 182b5c96047SRichard Henderson d.VIS_W64(1) = (s.VIS_B64(2) * d.VIS_SW64(1) + 0x8000) >> 16; 183b5c96047SRichard Henderson d.VIS_W64(2) = (s.VIS_B64(4) * d.VIS_SW64(2) + 0x8000) >> 16; 184b5c96047SRichard Henderson d.VIS_W64(3) = (s.VIS_B64(6) * d.VIS_SW64(3) + 0x8000) >> 16; 185fcf5ef2aSThomas Huth 186fcf5ef2aSThomas Huth return d.ll; 187fcf5ef2aSThomas Huth } 188fcf5ef2aSThomas Huth 1897b616f36SRichard Henderson uint64_t helper_fexpand(uint32_t src2) 190fcf5ef2aSThomas Huth { 191fcf5ef2aSThomas Huth VIS32 s; 192fcf5ef2aSThomas Huth VIS64 d; 193fcf5ef2aSThomas Huth 1947b616f36SRichard Henderson s.l = src2; 1957b616f36SRichard Henderson d.ll = 0; 196fcf5ef2aSThomas Huth d.VIS_W64(0) = s.VIS_B32(0) << 4; 197fcf5ef2aSThomas Huth d.VIS_W64(1) = s.VIS_B32(1) << 4; 198fcf5ef2aSThomas Huth d.VIS_W64(2) = s.VIS_B32(2) << 4; 199fcf5ef2aSThomas Huth d.VIS_W64(3) = s.VIS_B32(3) << 4; 200fcf5ef2aSThomas Huth 201fcf5ef2aSThomas Huth return d.ll; 202fcf5ef2aSThomas Huth } 203fcf5ef2aSThomas Huth 204669e0774SRichard Henderson uint64_t helper_fcmpeq8(uint64_t src1, uint64_t src2) 205669e0774SRichard Henderson { 206669e0774SRichard Henderson uint64_t a = src1 ^ src2; 207669e0774SRichard Henderson uint64_t m = 0x7f7f7f7f7f7f7f7fULL; 208669e0774SRichard Henderson uint64_t c = ~(((a & m) + m) | a | m); 209669e0774SRichard Henderson 210669e0774SRichard Henderson /* a.......b.......c.......d.......e.......f.......g.......h....... */ 211669e0774SRichard Henderson c |= c << 7; 212669e0774SRichard Henderson /* ab......bc......cd......de......ef......fg......gh......h....... */ 213669e0774SRichard Henderson c |= c << 14; 214669e0774SRichard Henderson /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */ 215669e0774SRichard Henderson c |= c << 28; 216669e0774SRichard Henderson /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */ 217669e0774SRichard Henderson return c >> 56; 218669e0774SRichard Henderson } 219669e0774SRichard Henderson 220669e0774SRichard Henderson uint64_t helper_fcmpne8(uint64_t src1, uint64_t src2) 221669e0774SRichard Henderson { 222669e0774SRichard Henderson return helper_fcmpeq8(src1, src2) ^ 0xff; 223669e0774SRichard Henderson } 224669e0774SRichard Henderson 225*b3c934ddSRichard Henderson uint64_t helper_fcmple8(uint64_t src1, uint64_t src2) 226*b3c934ddSRichard Henderson { 227*b3c934ddSRichard Henderson VIS64 s1, s2; 228*b3c934ddSRichard Henderson uint64_t r = 0; 229*b3c934ddSRichard Henderson 230*b3c934ddSRichard Henderson s1.ll = src1; 231*b3c934ddSRichard Henderson s2.ll = src2; 232*b3c934ddSRichard Henderson 233*b3c934ddSRichard Henderson for (int i = 0; i < 8; ++i) { 234*b3c934ddSRichard Henderson r |= (s1.VIS_SB64(i) <= s2.VIS_SB64(i)) << i; 235*b3c934ddSRichard Henderson } 236*b3c934ddSRichard Henderson return r; 237*b3c934ddSRichard Henderson } 238*b3c934ddSRichard Henderson 239*b3c934ddSRichard Henderson uint64_t helper_fcmpgt8(uint64_t src1, uint64_t src2) 240*b3c934ddSRichard Henderson { 241*b3c934ddSRichard Henderson return helper_fcmple8(src1, src2) ^ 0xff; 242*b3c934ddSRichard Henderson } 243*b3c934ddSRichard Henderson 244669e0774SRichard Henderson uint64_t helper_fcmpule8(uint64_t src1, uint64_t src2) 245669e0774SRichard Henderson { 246669e0774SRichard Henderson VIS64 s1, s2; 247669e0774SRichard Henderson uint64_t r = 0; 248669e0774SRichard Henderson 249669e0774SRichard Henderson s1.ll = src1; 250669e0774SRichard Henderson s2.ll = src2; 251669e0774SRichard Henderson 252669e0774SRichard Henderson for (int i = 0; i < 8; ++i) { 253669e0774SRichard Henderson r |= (s1.VIS_B64(i) <= s2.VIS_B64(i)) << i; 254669e0774SRichard Henderson } 255669e0774SRichard Henderson return r; 256669e0774SRichard Henderson } 257669e0774SRichard Henderson 258669e0774SRichard Henderson uint64_t helper_fcmpugt8(uint64_t src1, uint64_t src2) 259669e0774SRichard Henderson { 260669e0774SRichard Henderson return helper_fcmpule8(src1, src2) ^ 0xff; 261669e0774SRichard Henderson } 262669e0774SRichard Henderson 263*b3c934ddSRichard Henderson uint64_t helper_fcmpeq16(uint64_t src1, uint64_t src2) 264*b3c934ddSRichard Henderson { 265*b3c934ddSRichard Henderson uint64_t a = src1 ^ src2; 266*b3c934ddSRichard Henderson uint64_t m = 0x7fff7fff7fff7fffULL; 267*b3c934ddSRichard Henderson uint64_t c = ~(((a & m) + m) | a | m); 268*b3c934ddSRichard Henderson 269*b3c934ddSRichard Henderson /* a...............b...............c...............d............... */ 270*b3c934ddSRichard Henderson c |= c << 15; 271*b3c934ddSRichard Henderson /* ab..............bc..............cd..............d............... */ 272*b3c934ddSRichard Henderson c |= c << 30; 273*b3c934ddSRichard Henderson /* abcd............bcd.............cd..............d............... */ 274*b3c934ddSRichard Henderson return c >> 60; 275*b3c934ddSRichard Henderson } 276*b3c934ddSRichard Henderson 277*b3c934ddSRichard Henderson uint64_t helper_fcmpne16(uint64_t src1, uint64_t src2) 278*b3c934ddSRichard Henderson { 279*b3c934ddSRichard Henderson return helper_fcmpeq16(src1, src2) ^ 0xf; 280*b3c934ddSRichard Henderson } 281*b3c934ddSRichard Henderson 282*b3c934ddSRichard Henderson uint64_t helper_fcmple16(uint64_t src1, uint64_t src2) 283*b3c934ddSRichard Henderson { 284*b3c934ddSRichard Henderson VIS64 s1, s2; 285*b3c934ddSRichard Henderson uint64_t r = 0; 286*b3c934ddSRichard Henderson 287*b3c934ddSRichard Henderson s1.ll = src1; 288*b3c934ddSRichard Henderson s2.ll = src2; 289*b3c934ddSRichard Henderson 290*b3c934ddSRichard Henderson for (int i = 0; i < 4; ++i) { 291*b3c934ddSRichard Henderson r |= (s1.VIS_SW64(i) <= s2.VIS_SW64(i)) << i; 292*b3c934ddSRichard Henderson } 293*b3c934ddSRichard Henderson return r; 294*b3c934ddSRichard Henderson } 295*b3c934ddSRichard Henderson 296*b3c934ddSRichard Henderson uint64_t helper_fcmpgt16(uint64_t src1, uint64_t src2) 297*b3c934ddSRichard Henderson { 298*b3c934ddSRichard Henderson return helper_fcmple16(src1, src2) ^ 0xf; 299*b3c934ddSRichard Henderson } 300*b3c934ddSRichard Henderson 301*b3c934ddSRichard Henderson uint64_t helper_fcmpule16(uint64_t src1, uint64_t src2) 302*b3c934ddSRichard Henderson { 303*b3c934ddSRichard Henderson VIS64 s1, s2; 304*b3c934ddSRichard Henderson uint64_t r = 0; 305*b3c934ddSRichard Henderson 306*b3c934ddSRichard Henderson s1.ll = src1; 307*b3c934ddSRichard Henderson s2.ll = src2; 308*b3c934ddSRichard Henderson 309*b3c934ddSRichard Henderson for (int i = 0; i < 4; ++i) { 310*b3c934ddSRichard Henderson r |= (s1.VIS_W64(i) <= s2.VIS_W64(i)) << i; 311*b3c934ddSRichard Henderson } 312*b3c934ddSRichard Henderson return r; 313*b3c934ddSRichard Henderson } 314*b3c934ddSRichard Henderson 315*b3c934ddSRichard Henderson uint64_t helper_fcmpugt16(uint64_t src1, uint64_t src2) 316*b3c934ddSRichard Henderson { 317*b3c934ddSRichard Henderson return helper_fcmpule16(src1, src2) ^ 0xf; 318*b3c934ddSRichard Henderson } 319*b3c934ddSRichard Henderson 320*b3c934ddSRichard Henderson uint64_t helper_fcmpeq32(uint64_t src1, uint64_t src2) 321*b3c934ddSRichard Henderson { 322*b3c934ddSRichard Henderson uint64_t a = src1 ^ src2; 323*b3c934ddSRichard Henderson return ((uint32_t)a == 0) | (a >> 32 ? 0 : 2); 324*b3c934ddSRichard Henderson } 325*b3c934ddSRichard Henderson 326*b3c934ddSRichard Henderson uint64_t helper_fcmpne32(uint64_t src1, uint64_t src2) 327*b3c934ddSRichard Henderson { 328*b3c934ddSRichard Henderson uint64_t a = src1 ^ src2; 329*b3c934ddSRichard Henderson return ((uint32_t)a != 0) | (a >> 32 ? 2 : 0); 330*b3c934ddSRichard Henderson } 331*b3c934ddSRichard Henderson 332*b3c934ddSRichard Henderson uint64_t helper_fcmple32(uint64_t src1, uint64_t src2) 333*b3c934ddSRichard Henderson { 334*b3c934ddSRichard Henderson VIS64 s1, s2; 335*b3c934ddSRichard Henderson uint64_t r = 0; 336*b3c934ddSRichard Henderson 337*b3c934ddSRichard Henderson s1.ll = src1; 338*b3c934ddSRichard Henderson s2.ll = src2; 339*b3c934ddSRichard Henderson 340*b3c934ddSRichard Henderson for (int i = 0; i < 2; ++i) { 341*b3c934ddSRichard Henderson r |= (s1.VIS_SL64(i) <= s2.VIS_SL64(i)) << i; 342*b3c934ddSRichard Henderson } 343*b3c934ddSRichard Henderson return r; 344*b3c934ddSRichard Henderson } 345*b3c934ddSRichard Henderson 346*b3c934ddSRichard Henderson uint64_t helper_fcmpgt32(uint64_t src1, uint64_t src2) 347*b3c934ddSRichard Henderson { 348*b3c934ddSRichard Henderson return helper_fcmple32(src1, src2) ^ 3; 349*b3c934ddSRichard Henderson } 350*b3c934ddSRichard Henderson 351*b3c934ddSRichard Henderson uint64_t helper_fcmpule32(uint64_t src1, uint64_t src2) 352*b3c934ddSRichard Henderson { 353*b3c934ddSRichard Henderson VIS64 s1, s2; 354*b3c934ddSRichard Henderson uint64_t r = 0; 355*b3c934ddSRichard Henderson 356*b3c934ddSRichard Henderson s1.ll = src1; 357*b3c934ddSRichard Henderson s2.ll = src2; 358*b3c934ddSRichard Henderson 359*b3c934ddSRichard Henderson for (int i = 0; i < 2; ++i) { 360*b3c934ddSRichard Henderson r |= (s1.VIS_L64(i) <= s2.VIS_L64(i)) << i; 361*b3c934ddSRichard Henderson } 362*b3c934ddSRichard Henderson return r; 363*b3c934ddSRichard Henderson } 364*b3c934ddSRichard Henderson 365*b3c934ddSRichard Henderson uint64_t helper_fcmpugt32(uint64_t src1, uint64_t src2) 366*b3c934ddSRichard Henderson { 367*b3c934ddSRichard Henderson return helper_fcmpule32(src1, src2) ^ 3; 368*b3c934ddSRichard Henderson } 369*b3c934ddSRichard Henderson 370fcf5ef2aSThomas Huth uint64_t helper_pdist(uint64_t sum, uint64_t src1, uint64_t src2) 371fcf5ef2aSThomas Huth { 372fcf5ef2aSThomas Huth int i; 373fcf5ef2aSThomas Huth for (i = 0; i < 8; i++) { 374fcf5ef2aSThomas Huth int s1, s2; 375fcf5ef2aSThomas Huth 376fcf5ef2aSThomas Huth s1 = (src1 >> (56 - (i * 8))) & 0xff; 377fcf5ef2aSThomas Huth s2 = (src2 >> (56 - (i * 8))) & 0xff; 378fcf5ef2aSThomas Huth 379fcf5ef2aSThomas Huth /* Absolute value of difference. */ 380fcf5ef2aSThomas Huth s1 -= s2; 381fcf5ef2aSThomas Huth if (s1 < 0) { 382fcf5ef2aSThomas Huth s1 = -s1; 383fcf5ef2aSThomas Huth } 384fcf5ef2aSThomas Huth 385fcf5ef2aSThomas Huth sum += s1; 386fcf5ef2aSThomas Huth } 387fcf5ef2aSThomas Huth 388fcf5ef2aSThomas Huth return sum; 389fcf5ef2aSThomas Huth } 390fcf5ef2aSThomas Huth 391fcf5ef2aSThomas Huth uint32_t helper_fpack16(uint64_t gsr, uint64_t rs2) 392fcf5ef2aSThomas Huth { 393fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0xf; 394fcf5ef2aSThomas Huth uint32_t ret = 0; 395fcf5ef2aSThomas Huth int byte; 396fcf5ef2aSThomas Huth 397fcf5ef2aSThomas Huth for (byte = 0; byte < 4; byte++) { 398fcf5ef2aSThomas Huth uint32_t val; 399fcf5ef2aSThomas Huth int16_t src = rs2 >> (byte * 16); 400fcf5ef2aSThomas Huth int32_t scaled = src << scale; 401fcf5ef2aSThomas Huth int32_t from_fixed = scaled >> 7; 402fcf5ef2aSThomas Huth 403fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 404fcf5ef2aSThomas Huth from_fixed > 255 ? 255 : from_fixed); 405fcf5ef2aSThomas Huth 406fcf5ef2aSThomas Huth ret |= val << (8 * byte); 407fcf5ef2aSThomas Huth } 408fcf5ef2aSThomas Huth 409fcf5ef2aSThomas Huth return ret; 410fcf5ef2aSThomas Huth } 411fcf5ef2aSThomas Huth 412fcf5ef2aSThomas Huth uint64_t helper_fpack32(uint64_t gsr, uint64_t rs1, uint64_t rs2) 413fcf5ef2aSThomas Huth { 414fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 415fcf5ef2aSThomas Huth uint64_t ret = 0; 416fcf5ef2aSThomas Huth int word; 417fcf5ef2aSThomas Huth 418fcf5ef2aSThomas Huth ret = (rs1 << 8) & ~(0x000000ff000000ffULL); 419fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 420fcf5ef2aSThomas Huth uint64_t val; 421fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 422fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 423fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 23; 424fcf5ef2aSThomas Huth 425fcf5ef2aSThomas Huth val = (from_fixed < 0 ? 0 : 426fcf5ef2aSThomas Huth (from_fixed > 255) ? 255 : from_fixed); 427fcf5ef2aSThomas Huth 428fcf5ef2aSThomas Huth ret |= val << (32 * word); 429fcf5ef2aSThomas Huth } 430fcf5ef2aSThomas Huth 431fcf5ef2aSThomas Huth return ret; 432fcf5ef2aSThomas Huth } 433fcf5ef2aSThomas Huth 434fcf5ef2aSThomas Huth uint32_t helper_fpackfix(uint64_t gsr, uint64_t rs2) 435fcf5ef2aSThomas Huth { 436fcf5ef2aSThomas Huth int scale = (gsr >> 3) & 0x1f; 437fcf5ef2aSThomas Huth uint32_t ret = 0; 438fcf5ef2aSThomas Huth int word; 439fcf5ef2aSThomas Huth 440fcf5ef2aSThomas Huth for (word = 0; word < 2; word++) { 441fcf5ef2aSThomas Huth uint32_t val; 442fcf5ef2aSThomas Huth int32_t src = rs2 >> (word * 32); 443fcf5ef2aSThomas Huth int64_t scaled = (int64_t)src << scale; 444fcf5ef2aSThomas Huth int64_t from_fixed = scaled >> 16; 445fcf5ef2aSThomas Huth 446fcf5ef2aSThomas Huth val = (from_fixed < -32768 ? -32768 : 447fcf5ef2aSThomas Huth from_fixed > 32767 ? 32767 : from_fixed); 448fcf5ef2aSThomas Huth 449fcf5ef2aSThomas Huth ret |= (val & 0xffff) << (word * 16); 450fcf5ef2aSThomas Huth } 451fcf5ef2aSThomas Huth 452fcf5ef2aSThomas Huth return ret; 453fcf5ef2aSThomas Huth } 454fcf5ef2aSThomas Huth 455fcf5ef2aSThomas Huth uint64_t helper_bshuffle(uint64_t gsr, uint64_t src1, uint64_t src2) 456fcf5ef2aSThomas Huth { 457fcf5ef2aSThomas Huth union { 458fcf5ef2aSThomas Huth uint64_t ll[2]; 459fcf5ef2aSThomas Huth uint8_t b[16]; 460fcf5ef2aSThomas Huth } s; 461fcf5ef2aSThomas Huth VIS64 r; 462fcf5ef2aSThomas Huth uint32_t i, mask, host; 463fcf5ef2aSThomas Huth 464fcf5ef2aSThomas Huth /* Set up S such that we can index across all of the bytes. */ 465e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN 466fcf5ef2aSThomas Huth s.ll[0] = src1; 467fcf5ef2aSThomas Huth s.ll[1] = src2; 468fcf5ef2aSThomas Huth host = 0; 469fcf5ef2aSThomas Huth #else 470fcf5ef2aSThomas Huth s.ll[1] = src1; 471fcf5ef2aSThomas Huth s.ll[0] = src2; 472fcf5ef2aSThomas Huth host = 15; 473fcf5ef2aSThomas Huth #endif 474fcf5ef2aSThomas Huth mask = gsr >> 32; 475fcf5ef2aSThomas Huth 476fcf5ef2aSThomas Huth for (i = 0; i < 8; ++i) { 477fcf5ef2aSThomas Huth unsigned e = (mask >> (28 - i*4)) & 0xf; 478fcf5ef2aSThomas Huth r.VIS_B64(i) = s.b[e ^ host]; 479fcf5ef2aSThomas Huth } 480fcf5ef2aSThomas Huth 481fcf5ef2aSThomas Huth return r.ll; 482fcf5ef2aSThomas Huth } 483c973b4e8SRichard Henderson 484c973b4e8SRichard Henderson uint64_t helper_cmask8(uint64_t gsr, uint64_t src) 485c973b4e8SRichard Henderson { 486c973b4e8SRichard Henderson uint32_t mask = 0; 487c973b4e8SRichard Henderson 488c973b4e8SRichard Henderson mask |= (src & 0x01 ? 0x00000007 : 0x0000000f); 489c973b4e8SRichard Henderson mask |= (src & 0x02 ? 0x00000060 : 0x000000e0); 490c973b4e8SRichard Henderson mask |= (src & 0x04 ? 0x00000500 : 0x00000d00); 491c973b4e8SRichard Henderson mask |= (src & 0x08 ? 0x00004000 : 0x0000c000); 492c973b4e8SRichard Henderson mask |= (src & 0x10 ? 0x00030000 : 0x000b0000); 493c973b4e8SRichard Henderson mask |= (src & 0x20 ? 0x00200000 : 0x00a00000); 494c973b4e8SRichard Henderson mask |= (src & 0x40 ? 0x01000000 : 0x09000000); 495c973b4e8SRichard Henderson mask |= (src & 0x80 ? 0x00000000 : 0x80000000); 496c973b4e8SRichard Henderson 497c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 498c973b4e8SRichard Henderson } 499c973b4e8SRichard Henderson 500c973b4e8SRichard Henderson uint64_t helper_cmask16(uint64_t gsr, uint64_t src) 501c973b4e8SRichard Henderson { 502c973b4e8SRichard Henderson uint32_t mask = 0; 503c973b4e8SRichard Henderson 504c973b4e8SRichard Henderson mask |= (src & 0x1 ? 0x00000067 : 0x000000ef); 505c973b4e8SRichard Henderson mask |= (src & 0x2 ? 0x00004500 : 0x0000cd00); 506c973b4e8SRichard Henderson mask |= (src & 0x4 ? 0x00230000 : 0x00ab0000); 507c973b4e8SRichard Henderson mask |= (src & 0x8 ? 0x01000000 : 0x89000000); 508c973b4e8SRichard Henderson 509c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 510c973b4e8SRichard Henderson } 511c973b4e8SRichard Henderson 512c973b4e8SRichard Henderson uint64_t helper_cmask32(uint64_t gsr, uint64_t src) 513c973b4e8SRichard Henderson { 514c973b4e8SRichard Henderson uint32_t mask = 0; 515c973b4e8SRichard Henderson 516c973b4e8SRichard Henderson mask |= (src & 0x1 ? 0x00004567 : 0x0000cdef); 517c973b4e8SRichard Henderson mask |= (src & 0x2 ? 0x01230000 : 0x89ab0000); 518c973b4e8SRichard Henderson 519c973b4e8SRichard Henderson return deposit64(gsr, 32, 32, mask); 520c973b4e8SRichard Henderson } 5217837185eSRichard Henderson 5227837185eSRichard Henderson static inline uint16_t do_fchksm16(uint16_t src1, uint16_t src2) 5237837185eSRichard Henderson { 5247837185eSRichard Henderson uint16_t a = src1 + src2; 5257837185eSRichard Henderson uint16_t c = a < src1; 5267837185eSRichard Henderson return a + c; 5277837185eSRichard Henderson } 5287837185eSRichard Henderson 5297837185eSRichard Henderson uint64_t helper_fchksm16(uint64_t src1, uint64_t src2) 5307837185eSRichard Henderson { 5317837185eSRichard Henderson VIS64 r, s1, s2; 5327837185eSRichard Henderson 5337837185eSRichard Henderson s1.ll = src1; 5347837185eSRichard Henderson s2.ll = src2; 5357837185eSRichard Henderson r.ll = 0; 5367837185eSRichard Henderson 5377837185eSRichard Henderson r.VIS_W64(0) = do_fchksm16(s1.VIS_W64(0), s2.VIS_W64(0)); 5387837185eSRichard Henderson r.VIS_W64(1) = do_fchksm16(s1.VIS_W64(1), s2.VIS_W64(1)); 5397837185eSRichard Henderson r.VIS_W64(2) = do_fchksm16(s1.VIS_W64(2), s2.VIS_W64(2)); 5407837185eSRichard Henderson r.VIS_W64(3) = do_fchksm16(s1.VIS_W64(3), s2.VIS_W64(3)); 5417837185eSRichard Henderson 5427837185eSRichard Henderson return r.ll; 5437837185eSRichard Henderson } 544d6ff1ccbSRichard Henderson 545d6ff1ccbSRichard Henderson static inline int16_t do_fmean16(int16_t src1, int16_t src2) 546d6ff1ccbSRichard Henderson { 547d6ff1ccbSRichard Henderson return (src1 + src2 + 1) / 2; 548d6ff1ccbSRichard Henderson } 549d6ff1ccbSRichard Henderson 550d6ff1ccbSRichard Henderson uint64_t helper_fmean16(uint64_t src1, uint64_t src2) 551d6ff1ccbSRichard Henderson { 552d6ff1ccbSRichard Henderson VIS64 r, s1, s2; 553d6ff1ccbSRichard Henderson 554d6ff1ccbSRichard Henderson s1.ll = src1; 555d6ff1ccbSRichard Henderson s2.ll = src2; 556d6ff1ccbSRichard Henderson r.ll = 0; 557d6ff1ccbSRichard Henderson 558d6ff1ccbSRichard Henderson r.VIS_SW64(0) = do_fmean16(s1.VIS_SW64(0), s2.VIS_SW64(0)); 559d6ff1ccbSRichard Henderson r.VIS_SW64(1) = do_fmean16(s1.VIS_SW64(1), s2.VIS_SW64(1)); 560d6ff1ccbSRichard Henderson r.VIS_SW64(2) = do_fmean16(s1.VIS_SW64(2), s2.VIS_SW64(2)); 561d6ff1ccbSRichard Henderson r.VIS_SW64(3) = do_fmean16(s1.VIS_SW64(3), s2.VIS_SW64(3)); 562d6ff1ccbSRichard Henderson 563d6ff1ccbSRichard Henderson return r.ll; 564d6ff1ccbSRichard Henderson } 565fbc5c8d4SRichard Henderson 566fbc5c8d4SRichard Henderson uint64_t helper_fslas16(uint64_t src1, uint64_t src2) 567fbc5c8d4SRichard Henderson { 568fbc5c8d4SRichard Henderson VIS64 r, s1, s2; 569fbc5c8d4SRichard Henderson 570fbc5c8d4SRichard Henderson s1.ll = src1; 571fbc5c8d4SRichard Henderson s2.ll = src2; 572fbc5c8d4SRichard Henderson r.ll = 0; 573fbc5c8d4SRichard Henderson 574fbc5c8d4SRichard Henderson for (int i = 0; i < 4; ++i) { 575fbc5c8d4SRichard Henderson int t = s1.VIS_SW64(i) << (s2.VIS_W64(i) % 16); 576fbc5c8d4SRichard Henderson t = MIN(t, INT16_MAX); 577fbc5c8d4SRichard Henderson t = MAX(t, INT16_MIN); 578fbc5c8d4SRichard Henderson r.VIS_SW64(i) = t; 579fbc5c8d4SRichard Henderson } 580fbc5c8d4SRichard Henderson 581fbc5c8d4SRichard Henderson return r.ll; 582fbc5c8d4SRichard Henderson } 583fbc5c8d4SRichard Henderson 584fbc5c8d4SRichard Henderson uint64_t helper_fslas32(uint64_t src1, uint64_t src2) 585fbc5c8d4SRichard Henderson { 586fbc5c8d4SRichard Henderson VIS64 r, s1, s2; 587fbc5c8d4SRichard Henderson 588fbc5c8d4SRichard Henderson s1.ll = src1; 589fbc5c8d4SRichard Henderson s2.ll = src2; 590fbc5c8d4SRichard Henderson r.ll = 0; 591fbc5c8d4SRichard Henderson 592fbc5c8d4SRichard Henderson for (int i = 0; i < 2; ++i) { 593fbc5c8d4SRichard Henderson int64_t t = (int64_t)(int32_t)s1.VIS_L64(i) << (s2.VIS_L64(i) % 32); 594fbc5c8d4SRichard Henderson t = MIN(t, INT32_MAX); 595fbc5c8d4SRichard Henderson t = MAX(t, INT32_MIN); 596fbc5c8d4SRichard Henderson r.VIS_L64(i) = t; 597fbc5c8d4SRichard Henderson } 598fbc5c8d4SRichard Henderson 599fbc5c8d4SRichard Henderson return r.ll; 600fbc5c8d4SRichard Henderson } 601029b0283SRichard Henderson 602029b0283SRichard Henderson uint64_t helper_xmulx(uint64_t src1, uint64_t src2) 603029b0283SRichard Henderson { 604029b0283SRichard Henderson return int128_getlo(clmul_64(src1, src2)); 605029b0283SRichard Henderson } 606029b0283SRichard Henderson 607029b0283SRichard Henderson uint64_t helper_xmulxhi(uint64_t src1, uint64_t src2) 608029b0283SRichard Henderson { 609029b0283SRichard Henderson return int128_gethi(clmul_64(src1, src2)); 610029b0283SRichard Henderson } 611