1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4/PNI support 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2005 Fabrice Bellard 5fcf5ef2aSThomas Huth * Copyright (c) 2008 Intel Corporation <andrew.zaborowski@intel.com> 6fcf5ef2aSThomas Huth * 7fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or 8fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public 9fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either 10d9ff33adSChetan Pant * version 2.1 of the License, or (at your option) any later version. 11fcf5ef2aSThomas Huth * 12fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful, 13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15fcf5ef2aSThomas Huth * Lesser General Public License for more details. 16fcf5ef2aSThomas Huth * 17fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public 18fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>. 19fcf5ef2aSThomas Huth */ 20fcf5ef2aSThomas Huth 21fcf5ef2aSThomas Huth #include "crypto/aes.h" 22fcf5ef2aSThomas Huth 23fcf5ef2aSThomas Huth #if SHIFT == 0 24fcf5ef2aSThomas Huth #define Reg MMXReg 25d22697ddSPaolo Bonzini #define SIZE 8 26fcf5ef2aSThomas Huth #define XMM_ONLY(...) 27fcf5ef2aSThomas Huth #define B(n) MMX_B(n) 28fcf5ef2aSThomas Huth #define W(n) MMX_W(n) 29fcf5ef2aSThomas Huth #define L(n) MMX_L(n) 30fcf5ef2aSThomas Huth #define Q(n) MMX_Q(n) 31fcf5ef2aSThomas Huth #define SUFFIX _mmx 32fcf5ef2aSThomas Huth #else 33fcf5ef2aSThomas Huth #define Reg ZMMReg 34d22697ddSPaolo Bonzini #define SIZE 16 35fcf5ef2aSThomas Huth #define XMM_ONLY(...) __VA_ARGS__ 36fcf5ef2aSThomas Huth #define B(n) ZMM_B(n) 37fcf5ef2aSThomas Huth #define W(n) ZMM_W(n) 38fcf5ef2aSThomas Huth #define L(n) ZMM_L(n) 39fcf5ef2aSThomas Huth #define Q(n) ZMM_Q(n) 40fcf5ef2aSThomas Huth #define SUFFIX _xmm 41fcf5ef2aSThomas Huth #endif 42fcf5ef2aSThomas Huth 4318592d2eSPaul Brook #define LANE_WIDTH (SHIFT ? 16 : 8) 44d45b0de6SPaul Brook #define PACK_WIDTH (LANE_WIDTH / 2) 4518592d2eSPaul Brook 46d22697ddSPaolo Bonzini /* 47d22697ddSPaolo Bonzini * Copy the relevant parts of a Reg value around. In the case where 48d22697ddSPaolo Bonzini * sizeof(Reg) > SIZE, these helpers operate only on the lower bytes of 49d22697ddSPaolo Bonzini * a 64 byte ZMMReg, so we must copy only those and keep the top bytes 50d22697ddSPaolo Bonzini * untouched in the guest-visible destination destination register. 51d22697ddSPaolo Bonzini * Note that the "lower bytes" are placed last in memory on big-endian 52d22697ddSPaolo Bonzini * hosts, which store the vector backwards in memory. In that case the 53d22697ddSPaolo Bonzini * copy *starts* at B(SIZE - 1) and ends at B(0), the opposite of 54d22697ddSPaolo Bonzini * the little-endian case. 55d22697ddSPaolo Bonzini */ 56d22697ddSPaolo Bonzini #if HOST_BIG_ENDIAN 57d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&((d).B(SIZE - 1)), &(r).B(SIZE - 1), SIZE) 58d22697ddSPaolo Bonzini #else 59d22697ddSPaolo Bonzini #define MOVE(d, r) memcpy(&(d).B(0), &(r).B(0), SIZE) 60d22697ddSPaolo Bonzini #endif 61d22697ddSPaolo Bonzini 6218592d2eSPaul Brook #if SHIFT == 0 6318592d2eSPaul Brook #define FPSRL(x, c) ((x) >> shift) 6418592d2eSPaul Brook #define FPSRAW(x, c) ((int16_t)(x) >> shift) 6518592d2eSPaul Brook #define FPSRAL(x, c) ((int32_t)(x) >> shift) 6618592d2eSPaul Brook #define FPSLL(x, c) ((x) << shift) 67fcf5ef2aSThomas Huth #endif 6818592d2eSPaul Brook 6918592d2eSPaul Brook void glue(helper_psrlw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 7018592d2eSPaul Brook { 7118592d2eSPaul Brook Reg *s = d; 7218592d2eSPaul Brook int shift; 7318592d2eSPaul Brook if (c->Q(0) > 15) { 7418592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 7518592d2eSPaul Brook d->Q(i) = 0; 7618592d2eSPaul Brook } 77fcf5ef2aSThomas Huth } else { 7818592d2eSPaul Brook shift = c->B(0); 7918592d2eSPaul Brook for (int i = 0; i < 4 << SHIFT; i++) { 8018592d2eSPaul Brook d->W(i) = FPSRL(s->W(i), shift); 8118592d2eSPaul Brook } 82fcf5ef2aSThomas Huth } 83fcf5ef2aSThomas Huth } 84fcf5ef2aSThomas Huth 8518592d2eSPaul Brook void glue(helper_psllw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 86fcf5ef2aSThomas Huth { 8718592d2eSPaul Brook Reg *s = d; 88fcf5ef2aSThomas Huth int shift; 8918592d2eSPaul Brook if (c->Q(0) > 15) { 9018592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 9118592d2eSPaul Brook d->Q(i) = 0; 9218592d2eSPaul Brook } 9318592d2eSPaul Brook } else { 9418592d2eSPaul Brook shift = c->B(0); 9518592d2eSPaul Brook for (int i = 0; i < 4 << SHIFT; i++) { 9618592d2eSPaul Brook d->W(i) = FPSLL(s->W(i), shift); 9718592d2eSPaul Brook } 9818592d2eSPaul Brook } 9918592d2eSPaul Brook } 100fcf5ef2aSThomas Huth 10118592d2eSPaul Brook void glue(helper_psraw, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 10218592d2eSPaul Brook { 10318592d2eSPaul Brook Reg *s = d; 10418592d2eSPaul Brook int shift; 10518592d2eSPaul Brook if (c->Q(0) > 15) { 106fcf5ef2aSThomas Huth shift = 15; 107fcf5ef2aSThomas Huth } else { 10818592d2eSPaul Brook shift = c->B(0); 109fcf5ef2aSThomas Huth } 11018592d2eSPaul Brook for (int i = 0; i < 4 << SHIFT; i++) { 11118592d2eSPaul Brook d->W(i) = FPSRAW(s->W(i), shift); 11218592d2eSPaul Brook } 113fcf5ef2aSThomas Huth } 114fcf5ef2aSThomas Huth 11518592d2eSPaul Brook void glue(helper_psrld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 116fcf5ef2aSThomas Huth { 11718592d2eSPaul Brook Reg *s = d; 118fcf5ef2aSThomas Huth int shift; 11918592d2eSPaul Brook if (c->Q(0) > 31) { 12018592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 12118592d2eSPaul Brook d->Q(i) = 0; 12218592d2eSPaul Brook } 123fcf5ef2aSThomas Huth } else { 12418592d2eSPaul Brook shift = c->B(0); 12518592d2eSPaul Brook for (int i = 0; i < 2 << SHIFT; i++) { 12618592d2eSPaul Brook d->L(i) = FPSRL(s->L(i), shift); 12718592d2eSPaul Brook } 128fcf5ef2aSThomas Huth } 129fcf5ef2aSThomas Huth } 130fcf5ef2aSThomas Huth 13118592d2eSPaul Brook void glue(helper_pslld, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 132fcf5ef2aSThomas Huth { 13318592d2eSPaul Brook Reg *s = d; 134fcf5ef2aSThomas Huth int shift; 13518592d2eSPaul Brook if (c->Q(0) > 31) { 13618592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 13718592d2eSPaul Brook d->Q(i) = 0; 13818592d2eSPaul Brook } 139fcf5ef2aSThomas Huth } else { 14018592d2eSPaul Brook shift = c->B(0); 14118592d2eSPaul Brook for (int i = 0; i < 2 << SHIFT; i++) { 14218592d2eSPaul Brook d->L(i) = FPSLL(s->L(i), shift); 14318592d2eSPaul Brook } 144fcf5ef2aSThomas Huth } 145fcf5ef2aSThomas Huth } 146fcf5ef2aSThomas Huth 14718592d2eSPaul Brook void glue(helper_psrad, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 148fcf5ef2aSThomas Huth { 14918592d2eSPaul Brook Reg *s = d; 150fcf5ef2aSThomas Huth int shift; 15118592d2eSPaul Brook if (c->Q(0) > 31) { 152fcf5ef2aSThomas Huth shift = 31; 153fcf5ef2aSThomas Huth } else { 15418592d2eSPaul Brook shift = c->B(0); 155fcf5ef2aSThomas Huth } 15618592d2eSPaul Brook for (int i = 0; i < 2 << SHIFT; i++) { 15718592d2eSPaul Brook d->L(i) = FPSRAL(s->L(i), shift); 15818592d2eSPaul Brook } 159fcf5ef2aSThomas Huth } 160fcf5ef2aSThomas Huth 16118592d2eSPaul Brook void glue(helper_psrlq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 162fcf5ef2aSThomas Huth { 16318592d2eSPaul Brook Reg *s = d; 164fcf5ef2aSThomas Huth int shift; 16518592d2eSPaul Brook if (c->Q(0) > 63) { 16618592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 16718592d2eSPaul Brook d->Q(i) = 0; 16818592d2eSPaul Brook } 169fcf5ef2aSThomas Huth } else { 17018592d2eSPaul Brook shift = c->B(0); 17118592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 17218592d2eSPaul Brook d->Q(i) = FPSRL(s->Q(i), shift); 17318592d2eSPaul Brook } 174fcf5ef2aSThomas Huth } 175fcf5ef2aSThomas Huth } 176fcf5ef2aSThomas Huth 17718592d2eSPaul Brook void glue(helper_psllq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 178fcf5ef2aSThomas Huth { 17918592d2eSPaul Brook Reg *s = d; 180fcf5ef2aSThomas Huth int shift; 18118592d2eSPaul Brook if (c->Q(0) > 63) { 18218592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 18318592d2eSPaul Brook d->Q(i) = 0; 18418592d2eSPaul Brook } 185fcf5ef2aSThomas Huth } else { 18618592d2eSPaul Brook shift = c->B(0); 18718592d2eSPaul Brook for (int i = 0; i < 1 << SHIFT; i++) { 18818592d2eSPaul Brook d->Q(i) = FPSLL(s->Q(i), shift); 18918592d2eSPaul Brook } 190fcf5ef2aSThomas Huth } 191fcf5ef2aSThomas Huth } 192fcf5ef2aSThomas Huth 19318592d2eSPaul Brook #if SHIFT >= 1 19418592d2eSPaul Brook void glue(helper_psrldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 195fcf5ef2aSThomas Huth { 19618592d2eSPaul Brook Reg *s = d; 19718592d2eSPaul Brook int shift, i, j; 198fcf5ef2aSThomas Huth 19918592d2eSPaul Brook shift = c->L(0); 200fcf5ef2aSThomas Huth if (shift > 16) { 201fcf5ef2aSThomas Huth shift = 16; 202fcf5ef2aSThomas Huth } 20318592d2eSPaul Brook for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) { 204fcf5ef2aSThomas Huth for (i = 0; i < 16 - shift; i++) { 20518592d2eSPaul Brook d->B(j + i) = s->B(j + i + shift); 206fcf5ef2aSThomas Huth } 207fcf5ef2aSThomas Huth for (i = 16 - shift; i < 16; i++) { 20818592d2eSPaul Brook d->B(j + i) = 0; 20918592d2eSPaul Brook } 210fcf5ef2aSThomas Huth } 211fcf5ef2aSThomas Huth } 212fcf5ef2aSThomas Huth 21318592d2eSPaul Brook void glue(helper_pslldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *c) 214fcf5ef2aSThomas Huth { 21518592d2eSPaul Brook Reg *s = d; 21618592d2eSPaul Brook int shift, i, j; 217fcf5ef2aSThomas Huth 21818592d2eSPaul Brook shift = c->L(0); 219fcf5ef2aSThomas Huth if (shift > 16) { 220fcf5ef2aSThomas Huth shift = 16; 221fcf5ef2aSThomas Huth } 22218592d2eSPaul Brook for (j = 0; j < 8 << SHIFT; j += LANE_WIDTH) { 223fcf5ef2aSThomas Huth for (i = 15; i >= shift; i--) { 22418592d2eSPaul Brook d->B(j + i) = s->B(j + i - shift); 225fcf5ef2aSThomas Huth } 226fcf5ef2aSThomas Huth for (i = 0; i < shift; i++) { 22718592d2eSPaul Brook d->B(j + i) = 0; 22818592d2eSPaul Brook } 229fcf5ef2aSThomas Huth } 230fcf5ef2aSThomas Huth } 231fcf5ef2aSThomas Huth #endif 232fcf5ef2aSThomas Huth 233ee04a3c8SPaul Brook #define SSE_HELPER_1(name, elem, num, F) \ 234fcf5ef2aSThomas Huth void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 235fcf5ef2aSThomas Huth { \ 236ee04a3c8SPaul Brook int n = num; \ 237ee04a3c8SPaul Brook for (int i = 0; i < n; i++) { \ 238ee04a3c8SPaul Brook d->elem(i) = F(s->elem(i)); \ 239ee04a3c8SPaul Brook } \ 240fcf5ef2aSThomas Huth } 241fcf5ef2aSThomas Huth 242ee04a3c8SPaul Brook #define SSE_HELPER_2(name, elem, num, F) \ 243ee04a3c8SPaul Brook void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 244ee04a3c8SPaul Brook { \ 245ee04a3c8SPaul Brook Reg *v = d; \ 246ee04a3c8SPaul Brook int n = num; \ 247ee04a3c8SPaul Brook for (int i = 0; i < n; i++) { \ 248ee04a3c8SPaul Brook d->elem(i) = F(v->elem(i), s->elem(i)); \ 249ee04a3c8SPaul Brook } \ 250ee04a3c8SPaul Brook } 251ee04a3c8SPaul Brook 252ee04a3c8SPaul Brook #define SSE_HELPER_B(name, F) \ 253ee04a3c8SPaul Brook SSE_HELPER_2(name, B, 8 << SHIFT, F) 254ee04a3c8SPaul Brook 255fcf5ef2aSThomas Huth #define SSE_HELPER_W(name, F) \ 256ee04a3c8SPaul Brook SSE_HELPER_2(name, W, 4 << SHIFT, F) 257fcf5ef2aSThomas Huth 258fcf5ef2aSThomas Huth #define SSE_HELPER_L(name, F) \ 259ee04a3c8SPaul Brook SSE_HELPER_2(name, L, 2 << SHIFT, F) 260fcf5ef2aSThomas Huth 261fcf5ef2aSThomas Huth #define SSE_HELPER_Q(name, F) \ 262ee04a3c8SPaul Brook SSE_HELPER_2(name, Q, 1 << SHIFT, F) 263fcf5ef2aSThomas Huth 264fcf5ef2aSThomas Huth #if SHIFT == 0 265fcf5ef2aSThomas Huth static inline int satub(int x) 266fcf5ef2aSThomas Huth { 267fcf5ef2aSThomas Huth if (x < 0) { 268fcf5ef2aSThomas Huth return 0; 269fcf5ef2aSThomas Huth } else if (x > 255) { 270fcf5ef2aSThomas Huth return 255; 271fcf5ef2aSThomas Huth } else { 272fcf5ef2aSThomas Huth return x; 273fcf5ef2aSThomas Huth } 274fcf5ef2aSThomas Huth } 275fcf5ef2aSThomas Huth 276fcf5ef2aSThomas Huth static inline int satuw(int x) 277fcf5ef2aSThomas Huth { 278fcf5ef2aSThomas Huth if (x < 0) { 279fcf5ef2aSThomas Huth return 0; 280fcf5ef2aSThomas Huth } else if (x > 65535) { 281fcf5ef2aSThomas Huth return 65535; 282fcf5ef2aSThomas Huth } else { 283fcf5ef2aSThomas Huth return x; 284fcf5ef2aSThomas Huth } 285fcf5ef2aSThomas Huth } 286fcf5ef2aSThomas Huth 287fcf5ef2aSThomas Huth static inline int satsb(int x) 288fcf5ef2aSThomas Huth { 289fcf5ef2aSThomas Huth if (x < -128) { 290fcf5ef2aSThomas Huth return -128; 291fcf5ef2aSThomas Huth } else if (x > 127) { 292fcf5ef2aSThomas Huth return 127; 293fcf5ef2aSThomas Huth } else { 294fcf5ef2aSThomas Huth return x; 295fcf5ef2aSThomas Huth } 296fcf5ef2aSThomas Huth } 297fcf5ef2aSThomas Huth 298fcf5ef2aSThomas Huth static inline int satsw(int x) 299fcf5ef2aSThomas Huth { 300fcf5ef2aSThomas Huth if (x < -32768) { 301fcf5ef2aSThomas Huth return -32768; 302fcf5ef2aSThomas Huth } else if (x > 32767) { 303fcf5ef2aSThomas Huth return 32767; 304fcf5ef2aSThomas Huth } else { 305fcf5ef2aSThomas Huth return x; 306fcf5ef2aSThomas Huth } 307fcf5ef2aSThomas Huth } 308fcf5ef2aSThomas Huth 309fcf5ef2aSThomas Huth #define FADD(a, b) ((a) + (b)) 310fcf5ef2aSThomas Huth #define FADDUB(a, b) satub((a) + (b)) 311fcf5ef2aSThomas Huth #define FADDUW(a, b) satuw((a) + (b)) 312fcf5ef2aSThomas Huth #define FADDSB(a, b) satsb((int8_t)(a) + (int8_t)(b)) 313fcf5ef2aSThomas Huth #define FADDSW(a, b) satsw((int16_t)(a) + (int16_t)(b)) 314fcf5ef2aSThomas Huth 315fcf5ef2aSThomas Huth #define FSUB(a, b) ((a) - (b)) 316fcf5ef2aSThomas Huth #define FSUBUB(a, b) satub((a) - (b)) 317fcf5ef2aSThomas Huth #define FSUBUW(a, b) satuw((a) - (b)) 318fcf5ef2aSThomas Huth #define FSUBSB(a, b) satsb((int8_t)(a) - (int8_t)(b)) 319fcf5ef2aSThomas Huth #define FSUBSW(a, b) satsw((int16_t)(a) - (int16_t)(b)) 320fcf5ef2aSThomas Huth #define FMINUB(a, b) ((a) < (b)) ? (a) : (b) 321fcf5ef2aSThomas Huth #define FMINSW(a, b) ((int16_t)(a) < (int16_t)(b)) ? (a) : (b) 322fcf5ef2aSThomas Huth #define FMAXUB(a, b) ((a) > (b)) ? (a) : (b) 323fcf5ef2aSThomas Huth #define FMAXSW(a, b) ((int16_t)(a) > (int16_t)(b)) ? (a) : (b) 324fcf5ef2aSThomas Huth 325fcf5ef2aSThomas Huth #define FAND(a, b) ((a) & (b)) 326fcf5ef2aSThomas Huth #define FANDN(a, b) ((~(a)) & (b)) 327fcf5ef2aSThomas Huth #define FOR(a, b) ((a) | (b)) 328fcf5ef2aSThomas Huth #define FXOR(a, b) ((a) ^ (b)) 329fcf5ef2aSThomas Huth 330fcf5ef2aSThomas Huth #define FCMPGTB(a, b) ((int8_t)(a) > (int8_t)(b) ? -1 : 0) 331fcf5ef2aSThomas Huth #define FCMPGTW(a, b) ((int16_t)(a) > (int16_t)(b) ? -1 : 0) 332fcf5ef2aSThomas Huth #define FCMPGTL(a, b) ((int32_t)(a) > (int32_t)(b) ? -1 : 0) 333fcf5ef2aSThomas Huth #define FCMPEQ(a, b) ((a) == (b) ? -1 : 0) 334fcf5ef2aSThomas Huth 335fcf5ef2aSThomas Huth #define FMULLW(a, b) ((a) * (b)) 336fcf5ef2aSThomas Huth #define FMULHRW(a, b) (((int16_t)(a) * (int16_t)(b) + 0x8000) >> 16) 337fcf5ef2aSThomas Huth #define FMULHUW(a, b) ((a) * (b) >> 16) 338fcf5ef2aSThomas Huth #define FMULHW(a, b) ((int16_t)(a) * (int16_t)(b) >> 16) 339fcf5ef2aSThomas Huth 340fcf5ef2aSThomas Huth #define FAVG(a, b) (((a) + (b) + 1) >> 1) 341fcf5ef2aSThomas Huth #endif 342fcf5ef2aSThomas Huth 343fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddb, FADD) 344fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddw, FADD) 345fcf5ef2aSThomas Huth SSE_HELPER_L(helper_paddl, FADD) 346fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_paddq, FADD) 347fcf5ef2aSThomas Huth 348fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubb, FSUB) 349fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubw, FSUB) 350fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psubl, FSUB) 351fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_psubq, FSUB) 352fcf5ef2aSThomas Huth 353fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddusb, FADDUB) 354fcf5ef2aSThomas Huth SSE_HELPER_B(helper_paddsb, FADDSB) 355fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubusb, FSUBUB) 356fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psubsb, FSUBSB) 357fcf5ef2aSThomas Huth 358fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddusw, FADDUW) 359fcf5ef2aSThomas Huth SSE_HELPER_W(helper_paddsw, FADDSW) 360fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubusw, FSUBUW) 361fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psubsw, FSUBSW) 362fcf5ef2aSThomas Huth 363fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminub, FMINUB) 364fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxub, FMAXUB) 365fcf5ef2aSThomas Huth 366fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminsw, FMINSW) 367fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxsw, FMAXSW) 368fcf5ef2aSThomas Huth 369fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pand, FAND) 370fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pandn, FANDN) 371fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_por, FOR) 372fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pxor, FXOR) 373fcf5ef2aSThomas Huth 374fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpgtb, FCMPGTB) 375fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpgtw, FCMPGTW) 376fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpgtl, FCMPGTL) 377fcf5ef2aSThomas Huth 378fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pcmpeqb, FCMPEQ) 379fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pcmpeqw, FCMPEQ) 380fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pcmpeql, FCMPEQ) 381fcf5ef2aSThomas Huth 382fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmullw, FMULLW) 383fcf5ef2aSThomas Huth #if SHIFT == 0 384fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrw, FMULHRW) 385fcf5ef2aSThomas Huth #endif 386fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhuw, FMULHUW) 387fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhw, FMULHW) 388fcf5ef2aSThomas Huth 389fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pavgb, FAVG) 390fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pavgw, FAVG) 391fcf5ef2aSThomas Huth 392fcf5ef2aSThomas Huth void glue(helper_pmuludq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 393fcf5ef2aSThomas Huth { 394e894bae8SPaul Brook Reg *v = d; 395e894bae8SPaul Brook int i; 396e894bae8SPaul Brook 397e894bae8SPaul Brook for (i = 0; i < (1 << SHIFT); i++) { 398e894bae8SPaul Brook d->Q(i) = (uint64_t)s->L(i * 2) * (uint64_t)v->L(i * 2); 399e894bae8SPaul Brook } 400fcf5ef2aSThomas Huth } 401fcf5ef2aSThomas Huth 402fcf5ef2aSThomas Huth void glue(helper_pmaddwd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 403fcf5ef2aSThomas Huth { 404e894bae8SPaul Brook Reg *v = d; 405fcf5ef2aSThomas Huth int i; 406fcf5ef2aSThomas Huth 407fcf5ef2aSThomas Huth for (i = 0; i < (2 << SHIFT); i++) { 408e894bae8SPaul Brook d->L(i) = (int16_t)s->W(2 * i) * (int16_t)v->W(2 * i) + 409e894bae8SPaul Brook (int16_t)s->W(2 * i + 1) * (int16_t)v->W(2 * i + 1); 410fcf5ef2aSThomas Huth } 411fcf5ef2aSThomas Huth } 412fcf5ef2aSThomas Huth 413fcf5ef2aSThomas Huth #if SHIFT == 0 414fcf5ef2aSThomas Huth static inline int abs1(int a) 415fcf5ef2aSThomas Huth { 416fcf5ef2aSThomas Huth if (a < 0) { 417fcf5ef2aSThomas Huth return -a; 418fcf5ef2aSThomas Huth } else { 419fcf5ef2aSThomas Huth return a; 420fcf5ef2aSThomas Huth } 421fcf5ef2aSThomas Huth } 422fcf5ef2aSThomas Huth #endif 423e894bae8SPaul Brook 424fcf5ef2aSThomas Huth void glue(helper_psadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 425fcf5ef2aSThomas Huth { 426e894bae8SPaul Brook Reg *v = d; 427e894bae8SPaul Brook int i; 428fcf5ef2aSThomas Huth 429e894bae8SPaul Brook for (i = 0; i < (1 << SHIFT); i++) { 430e894bae8SPaul Brook unsigned int val = 0; 431e894bae8SPaul Brook val += abs1(v->B(8 * i + 0) - s->B(8 * i + 0)); 432e894bae8SPaul Brook val += abs1(v->B(8 * i + 1) - s->B(8 * i + 1)); 433e894bae8SPaul Brook val += abs1(v->B(8 * i + 2) - s->B(8 * i + 2)); 434e894bae8SPaul Brook val += abs1(v->B(8 * i + 3) - s->B(8 * i + 3)); 435e894bae8SPaul Brook val += abs1(v->B(8 * i + 4) - s->B(8 * i + 4)); 436e894bae8SPaul Brook val += abs1(v->B(8 * i + 5) - s->B(8 * i + 5)); 437e894bae8SPaul Brook val += abs1(v->B(8 * i + 6) - s->B(8 * i + 6)); 438e894bae8SPaul Brook val += abs1(v->B(8 * i + 7) - s->B(8 * i + 7)); 439e894bae8SPaul Brook d->Q(i) = val; 440e894bae8SPaul Brook } 441fcf5ef2aSThomas Huth } 442fcf5ef2aSThomas Huth 443fcf5ef2aSThomas Huth void glue(helper_maskmov, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 444fcf5ef2aSThomas Huth target_ulong a0) 445fcf5ef2aSThomas Huth { 446fcf5ef2aSThomas Huth int i; 447fcf5ef2aSThomas Huth 448fcf5ef2aSThomas Huth for (i = 0; i < (8 << SHIFT); i++) { 449fcf5ef2aSThomas Huth if (s->B(i) & 0x80) { 450fcf5ef2aSThomas Huth cpu_stb_data_ra(env, a0 + i, d->B(i), GETPC()); 451fcf5ef2aSThomas Huth } 452fcf5ef2aSThomas Huth } 453fcf5ef2aSThomas Huth } 454fcf5ef2aSThomas Huth 455fcf5ef2aSThomas Huth void glue(helper_movl_mm_T0, SUFFIX)(Reg *d, uint32_t val) 456fcf5ef2aSThomas Huth { 457e894bae8SPaul Brook int i; 458e894bae8SPaul Brook 459fcf5ef2aSThomas Huth d->L(0) = val; 460fcf5ef2aSThomas Huth d->L(1) = 0; 461e894bae8SPaul Brook for (i = 1; i < (1 << SHIFT); i++) { 462e894bae8SPaul Brook d->Q(i) = 0; 463e894bae8SPaul Brook } 464fcf5ef2aSThomas Huth } 465fcf5ef2aSThomas Huth 466fcf5ef2aSThomas Huth #ifdef TARGET_X86_64 467fcf5ef2aSThomas Huth void glue(helper_movq_mm_T0, SUFFIX)(Reg *d, uint64_t val) 468fcf5ef2aSThomas Huth { 469e894bae8SPaul Brook int i; 470e894bae8SPaul Brook 471fcf5ef2aSThomas Huth d->Q(0) = val; 472e894bae8SPaul Brook for (i = 1; i < (1 << SHIFT); i++) { 473e894bae8SPaul Brook d->Q(i) = 0; 474e894bae8SPaul Brook } 475fcf5ef2aSThomas Huth } 476fcf5ef2aSThomas Huth #endif 477fcf5ef2aSThomas Huth 478d45b0de6SPaul Brook #define SHUFFLE4(F, a, b, offset) do { \ 479d45b0de6SPaul Brook r0 = a->F((order & 3) + offset); \ 480d45b0de6SPaul Brook r1 = a->F(((order >> 2) & 3) + offset); \ 481d45b0de6SPaul Brook r2 = b->F(((order >> 4) & 3) + offset); \ 482d45b0de6SPaul Brook r3 = b->F(((order >> 6) & 3) + offset); \ 483d45b0de6SPaul Brook d->F(offset) = r0; \ 484d45b0de6SPaul Brook d->F(offset + 1) = r1; \ 485d45b0de6SPaul Brook d->F(offset + 2) = r2; \ 486d45b0de6SPaul Brook d->F(offset + 3) = r3; \ 487d45b0de6SPaul Brook } while (0) 488d45b0de6SPaul Brook 489fcf5ef2aSThomas Huth #if SHIFT == 0 490fcf5ef2aSThomas Huth void glue(helper_pshufw, SUFFIX)(Reg *d, Reg *s, int order) 491fcf5ef2aSThomas Huth { 492d45b0de6SPaul Brook uint16_t r0, r1, r2, r3; 493fcf5ef2aSThomas Huth 494d45b0de6SPaul Brook SHUFFLE4(W, s, s, 0); 495fcf5ef2aSThomas Huth } 496fcf5ef2aSThomas Huth #else 497ce4fa29fSPaolo Bonzini void glue(helper_shufps, SUFFIX)(Reg *d, Reg *s, int order) 498fcf5ef2aSThomas Huth { 499d45b0de6SPaul Brook Reg *v = d; 500d45b0de6SPaul Brook uint32_t r0, r1, r2, r3; 501d45b0de6SPaul Brook int i; 502fcf5ef2aSThomas Huth 503d45b0de6SPaul Brook for (i = 0; i < 2 << SHIFT; i += 4) { 504d45b0de6SPaul Brook SHUFFLE4(L, v, s, i); 505d45b0de6SPaul Brook } 506fcf5ef2aSThomas Huth } 507fcf5ef2aSThomas Huth 508ce4fa29fSPaolo Bonzini void glue(helper_shufpd, SUFFIX)(Reg *d, Reg *s, int order) 509fcf5ef2aSThomas Huth { 510d45b0de6SPaul Brook Reg *v = d; 511d45b0de6SPaul Brook uint64_t r0, r1; 512d45b0de6SPaul Brook int i; 513fcf5ef2aSThomas Huth 514d45b0de6SPaul Brook for (i = 0; i < 1 << SHIFT; i += 2) { 515d45b0de6SPaul Brook r0 = v->Q(((order & 1) & 1) + i); 516d45b0de6SPaul Brook r1 = s->Q(((order >> 1) & 1) + i); 517d45b0de6SPaul Brook d->Q(i) = r0; 518d45b0de6SPaul Brook d->Q(i + 1) = r1; 519d45b0de6SPaul Brook order >>= 2; 520d45b0de6SPaul Brook } 521fcf5ef2aSThomas Huth } 522fcf5ef2aSThomas Huth 523fcf5ef2aSThomas Huth void glue(helper_pshufd, SUFFIX)(Reg *d, Reg *s, int order) 524fcf5ef2aSThomas Huth { 525d45b0de6SPaul Brook uint32_t r0, r1, r2, r3; 526d45b0de6SPaul Brook int i; 527fcf5ef2aSThomas Huth 528d45b0de6SPaul Brook for (i = 0; i < 2 << SHIFT; i += 4) { 529d45b0de6SPaul Brook SHUFFLE4(L, s, s, i); 530d45b0de6SPaul Brook } 531fcf5ef2aSThomas Huth } 532fcf5ef2aSThomas Huth 533fcf5ef2aSThomas Huth void glue(helper_pshuflw, SUFFIX)(Reg *d, Reg *s, int order) 534fcf5ef2aSThomas Huth { 535d45b0de6SPaul Brook uint16_t r0, r1, r2, r3; 536d45b0de6SPaul Brook int i, j; 537fcf5ef2aSThomas Huth 538d45b0de6SPaul Brook for (i = 0, j = 1; j < 1 << SHIFT; i += 8, j += 2) { 539d45b0de6SPaul Brook SHUFFLE4(W, s, s, i); 540d45b0de6SPaul Brook d->Q(j) = s->Q(j); 541d45b0de6SPaul Brook } 542fcf5ef2aSThomas Huth } 543fcf5ef2aSThomas Huth 544fcf5ef2aSThomas Huth void glue(helper_pshufhw, SUFFIX)(Reg *d, Reg *s, int order) 545fcf5ef2aSThomas Huth { 546d45b0de6SPaul Brook uint16_t r0, r1, r2, r3; 547d45b0de6SPaul Brook int i, j; 548fcf5ef2aSThomas Huth 549d45b0de6SPaul Brook for (i = 4, j = 0; j < 1 << SHIFT; i += 8, j += 2) { 550d45b0de6SPaul Brook d->Q(j) = s->Q(j); 551d45b0de6SPaul Brook SHUFFLE4(W, s, s, i); 552d45b0de6SPaul Brook } 553fcf5ef2aSThomas Huth } 554fcf5ef2aSThomas Huth #endif 555fcf5ef2aSThomas Huth 556*3403cafeSPaul Brook #if SHIFT >= 1 557fcf5ef2aSThomas Huth /* FPU ops */ 558fcf5ef2aSThomas Huth /* XXX: not accurate */ 559fcf5ef2aSThomas Huth 560*3403cafeSPaul Brook #define SSE_HELPER_P(name, F) \ 561*3403cafeSPaul Brook void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, \ 562*3403cafeSPaul Brook Reg *d, Reg *s) \ 563fcf5ef2aSThomas Huth { \ 564*3403cafeSPaul Brook Reg *v = d; \ 565*3403cafeSPaul Brook int i; \ 566*3403cafeSPaul Brook for (i = 0; i < 2 << SHIFT; i++) { \ 567*3403cafeSPaul Brook d->ZMM_S(i) = F(32, v->ZMM_S(i), s->ZMM_S(i)); \ 568fcf5ef2aSThomas Huth } \ 569*3403cafeSPaul Brook } \ 570*3403cafeSPaul Brook \ 571*3403cafeSPaul Brook void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, \ 572*3403cafeSPaul Brook Reg *d, Reg *s) \ 573*3403cafeSPaul Brook { \ 574*3403cafeSPaul Brook Reg *v = d; \ 575*3403cafeSPaul Brook int i; \ 576*3403cafeSPaul Brook for (i = 0; i < 1 << SHIFT; i++) { \ 577*3403cafeSPaul Brook d->ZMM_D(i) = F(64, v->ZMM_D(i), s->ZMM_D(i)); \ 578*3403cafeSPaul Brook } \ 579*3403cafeSPaul Brook } 580*3403cafeSPaul Brook 581*3403cafeSPaul Brook #if SHIFT == 1 582*3403cafeSPaul Brook 583*3403cafeSPaul Brook #define SSE_HELPER_S(name, F) \ 584*3403cafeSPaul Brook SSE_HELPER_P(name, F) \ 585fcf5ef2aSThomas Huth \ 586fcf5ef2aSThomas Huth void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s)\ 587fcf5ef2aSThomas Huth { \ 588*3403cafeSPaul Brook Reg *v = d; \ 589*3403cafeSPaul Brook d->ZMM_S(0) = F(32, v->ZMM_S(0), s->ZMM_S(0)); \ 590fcf5ef2aSThomas Huth } \ 591fcf5ef2aSThomas Huth \ 592fcf5ef2aSThomas Huth void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s)\ 593fcf5ef2aSThomas Huth { \ 594*3403cafeSPaul Brook Reg *v = d; \ 595*3403cafeSPaul Brook d->ZMM_D(0) = F(64, v->ZMM_D(0), s->ZMM_D(0)); \ 596fcf5ef2aSThomas Huth } 597fcf5ef2aSThomas Huth 598*3403cafeSPaul Brook #else 599*3403cafeSPaul Brook 600*3403cafeSPaul Brook #define SSE_HELPER_S(name, F) SSE_HELPER_P(name, F) 601*3403cafeSPaul Brook 602*3403cafeSPaul Brook #endif 603*3403cafeSPaul Brook 604fcf5ef2aSThomas Huth #define FPU_ADD(size, a, b) float ## size ## _add(a, b, &env->sse_status) 605fcf5ef2aSThomas Huth #define FPU_SUB(size, a, b) float ## size ## _sub(a, b, &env->sse_status) 606fcf5ef2aSThomas Huth #define FPU_MUL(size, a, b) float ## size ## _mul(a, b, &env->sse_status) 607fcf5ef2aSThomas Huth #define FPU_DIV(size, a, b) float ## size ## _div(a, b, &env->sse_status) 608fcf5ef2aSThomas Huth 609fcf5ef2aSThomas Huth /* Note that the choice of comparison op here is important to get the 610fcf5ef2aSThomas Huth * special cases right: for min and max Intel specifies that (-0,0), 611fcf5ef2aSThomas Huth * (NaN, anything) and (anything, NaN) return the second argument. 612fcf5ef2aSThomas Huth */ 613fcf5ef2aSThomas Huth #define FPU_MIN(size, a, b) \ 614fcf5ef2aSThomas Huth (float ## size ## _lt(a, b, &env->sse_status) ? (a) : (b)) 615fcf5ef2aSThomas Huth #define FPU_MAX(size, a, b) \ 616fcf5ef2aSThomas Huth (float ## size ## _lt(b, a, &env->sse_status) ? (a) : (b)) 617fcf5ef2aSThomas Huth 618fcf5ef2aSThomas Huth SSE_HELPER_S(add, FPU_ADD) 619fcf5ef2aSThomas Huth SSE_HELPER_S(sub, FPU_SUB) 620fcf5ef2aSThomas Huth SSE_HELPER_S(mul, FPU_MUL) 621fcf5ef2aSThomas Huth SSE_HELPER_S(div, FPU_DIV) 622fcf5ef2aSThomas Huth SSE_HELPER_S(min, FPU_MIN) 623fcf5ef2aSThomas Huth SSE_HELPER_S(max, FPU_MAX) 624fcf5ef2aSThomas Huth 625*3403cafeSPaul Brook void glue(helper_sqrtps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 626*3403cafeSPaul Brook { 627*3403cafeSPaul Brook int i; 628*3403cafeSPaul Brook for (i = 0; i < 2 << SHIFT; i++) { 629*3403cafeSPaul Brook d->ZMM_S(i) = float32_sqrt(s->ZMM_S(i), &env->sse_status); 630*3403cafeSPaul Brook } 631*3403cafeSPaul Brook } 632*3403cafeSPaul Brook 633*3403cafeSPaul Brook void glue(helper_sqrtpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 634*3403cafeSPaul Brook { 635*3403cafeSPaul Brook int i; 636*3403cafeSPaul Brook for (i = 0; i < 1 << SHIFT; i++) { 637*3403cafeSPaul Brook d->ZMM_D(i) = float64_sqrt(s->ZMM_D(i), &env->sse_status); 638*3403cafeSPaul Brook } 639*3403cafeSPaul Brook } 640*3403cafeSPaul Brook 641*3403cafeSPaul Brook #if SHIFT == 1 642*3403cafeSPaul Brook void helper_sqrtss(CPUX86State *env, Reg *d, Reg *s) 643*3403cafeSPaul Brook { 644*3403cafeSPaul Brook d->ZMM_S(0) = float32_sqrt(s->ZMM_S(0), &env->sse_status); 645*3403cafeSPaul Brook } 646*3403cafeSPaul Brook 647*3403cafeSPaul Brook void helper_sqrtsd(CPUX86State *env, Reg *d, Reg *s) 648*3403cafeSPaul Brook { 649*3403cafeSPaul Brook d->ZMM_D(0) = float64_sqrt(s->ZMM_D(0), &env->sse_status); 650*3403cafeSPaul Brook } 651*3403cafeSPaul Brook #endif 652fcf5ef2aSThomas Huth 653fcf5ef2aSThomas Huth /* float to float conversions */ 654ce4fa29fSPaolo Bonzini void glue(helper_cvtps2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 655fcf5ef2aSThomas Huth { 656fcf5ef2aSThomas Huth float32 s0, s1; 657fcf5ef2aSThomas Huth 658fcf5ef2aSThomas Huth s0 = s->ZMM_S(0); 659fcf5ef2aSThomas Huth s1 = s->ZMM_S(1); 660fcf5ef2aSThomas Huth d->ZMM_D(0) = float32_to_float64(s0, &env->sse_status); 661fcf5ef2aSThomas Huth d->ZMM_D(1) = float32_to_float64(s1, &env->sse_status); 662fcf5ef2aSThomas Huth } 663fcf5ef2aSThomas Huth 664ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 665fcf5ef2aSThomas Huth { 666fcf5ef2aSThomas Huth d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status); 667fcf5ef2aSThomas Huth d->ZMM_S(1) = float64_to_float32(s->ZMM_D(1), &env->sse_status); 668fcf5ef2aSThomas Huth d->Q(1) = 0; 669fcf5ef2aSThomas Huth } 670fcf5ef2aSThomas Huth 671fcf5ef2aSThomas Huth void helper_cvtss2sd(CPUX86State *env, Reg *d, Reg *s) 672fcf5ef2aSThomas Huth { 673fcf5ef2aSThomas Huth d->ZMM_D(0) = float32_to_float64(s->ZMM_S(0), &env->sse_status); 674fcf5ef2aSThomas Huth } 675fcf5ef2aSThomas Huth 676fcf5ef2aSThomas Huth void helper_cvtsd2ss(CPUX86State *env, Reg *d, Reg *s) 677fcf5ef2aSThomas Huth { 678fcf5ef2aSThomas Huth d->ZMM_S(0) = float64_to_float32(s->ZMM_D(0), &env->sse_status); 679fcf5ef2aSThomas Huth } 680fcf5ef2aSThomas Huth 681fcf5ef2aSThomas Huth /* integer to float */ 682ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 683fcf5ef2aSThomas Huth { 684fcf5ef2aSThomas Huth d->ZMM_S(0) = int32_to_float32(s->ZMM_L(0), &env->sse_status); 685fcf5ef2aSThomas Huth d->ZMM_S(1) = int32_to_float32(s->ZMM_L(1), &env->sse_status); 686fcf5ef2aSThomas Huth d->ZMM_S(2) = int32_to_float32(s->ZMM_L(2), &env->sse_status); 687fcf5ef2aSThomas Huth d->ZMM_S(3) = int32_to_float32(s->ZMM_L(3), &env->sse_status); 688fcf5ef2aSThomas Huth } 689fcf5ef2aSThomas Huth 690ce4fa29fSPaolo Bonzini void glue(helper_cvtdq2pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 691fcf5ef2aSThomas Huth { 692fcf5ef2aSThomas Huth int32_t l0, l1; 693fcf5ef2aSThomas Huth 694fcf5ef2aSThomas Huth l0 = (int32_t)s->ZMM_L(0); 695fcf5ef2aSThomas Huth l1 = (int32_t)s->ZMM_L(1); 696fcf5ef2aSThomas Huth d->ZMM_D(0) = int32_to_float64(l0, &env->sse_status); 697fcf5ef2aSThomas Huth d->ZMM_D(1) = int32_to_float64(l1, &env->sse_status); 698fcf5ef2aSThomas Huth } 699fcf5ef2aSThomas Huth 700fcf5ef2aSThomas Huth void helper_cvtpi2ps(CPUX86State *env, ZMMReg *d, MMXReg *s) 701fcf5ef2aSThomas Huth { 702fcf5ef2aSThomas Huth d->ZMM_S(0) = int32_to_float32(s->MMX_L(0), &env->sse_status); 703fcf5ef2aSThomas Huth d->ZMM_S(1) = int32_to_float32(s->MMX_L(1), &env->sse_status); 704fcf5ef2aSThomas Huth } 705fcf5ef2aSThomas Huth 706fcf5ef2aSThomas Huth void helper_cvtpi2pd(CPUX86State *env, ZMMReg *d, MMXReg *s) 707fcf5ef2aSThomas Huth { 708fcf5ef2aSThomas Huth d->ZMM_D(0) = int32_to_float64(s->MMX_L(0), &env->sse_status); 709fcf5ef2aSThomas Huth d->ZMM_D(1) = int32_to_float64(s->MMX_L(1), &env->sse_status); 710fcf5ef2aSThomas Huth } 711fcf5ef2aSThomas Huth 712fcf5ef2aSThomas Huth void helper_cvtsi2ss(CPUX86State *env, ZMMReg *d, uint32_t val) 713fcf5ef2aSThomas Huth { 714fcf5ef2aSThomas Huth d->ZMM_S(0) = int32_to_float32(val, &env->sse_status); 715fcf5ef2aSThomas Huth } 716fcf5ef2aSThomas Huth 717fcf5ef2aSThomas Huth void helper_cvtsi2sd(CPUX86State *env, ZMMReg *d, uint32_t val) 718fcf5ef2aSThomas Huth { 719fcf5ef2aSThomas Huth d->ZMM_D(0) = int32_to_float64(val, &env->sse_status); 720fcf5ef2aSThomas Huth } 721fcf5ef2aSThomas Huth 722fcf5ef2aSThomas Huth #ifdef TARGET_X86_64 723fcf5ef2aSThomas Huth void helper_cvtsq2ss(CPUX86State *env, ZMMReg *d, uint64_t val) 724fcf5ef2aSThomas Huth { 725fcf5ef2aSThomas Huth d->ZMM_S(0) = int64_to_float32(val, &env->sse_status); 726fcf5ef2aSThomas Huth } 727fcf5ef2aSThomas Huth 728fcf5ef2aSThomas Huth void helper_cvtsq2sd(CPUX86State *env, ZMMReg *d, uint64_t val) 729fcf5ef2aSThomas Huth { 730fcf5ef2aSThomas Huth d->ZMM_D(0) = int64_to_float64(val, &env->sse_status); 731fcf5ef2aSThomas Huth } 732fcf5ef2aSThomas Huth #endif 733fcf5ef2aSThomas Huth 734fcf5ef2aSThomas Huth /* float to integer */ 7351e8a98b5SPeter Maydell 7361e8a98b5SPeter Maydell /* 7371e8a98b5SPeter Maydell * x86 mandates that we return the indefinite integer value for the result 7381e8a98b5SPeter Maydell * of any float-to-integer conversion that raises the 'invalid' exception. 7391e8a98b5SPeter Maydell * Wrap the softfloat functions to get this behaviour. 7401e8a98b5SPeter Maydell */ 7411e8a98b5SPeter Maydell #define WRAP_FLOATCONV(RETTYPE, FN, FLOATTYPE, INDEFVALUE) \ 7421e8a98b5SPeter Maydell static inline RETTYPE x86_##FN(FLOATTYPE a, float_status *s) \ 7431e8a98b5SPeter Maydell { \ 7441e8a98b5SPeter Maydell int oldflags, newflags; \ 7451e8a98b5SPeter Maydell RETTYPE r; \ 7461e8a98b5SPeter Maydell \ 7471e8a98b5SPeter Maydell oldflags = get_float_exception_flags(s); \ 7481e8a98b5SPeter Maydell set_float_exception_flags(0, s); \ 7491e8a98b5SPeter Maydell r = FN(a, s); \ 7501e8a98b5SPeter Maydell newflags = get_float_exception_flags(s); \ 7511e8a98b5SPeter Maydell if (newflags & float_flag_invalid) { \ 7521e8a98b5SPeter Maydell r = INDEFVALUE; \ 7531e8a98b5SPeter Maydell } \ 7541e8a98b5SPeter Maydell set_float_exception_flags(newflags | oldflags, s); \ 7551e8a98b5SPeter Maydell return r; \ 7561e8a98b5SPeter Maydell } 7571e8a98b5SPeter Maydell 7581e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32, float32, INT32_MIN) 7591e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float32_to_int32_round_to_zero, float32, INT32_MIN) 7601e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32, float64, INT32_MIN) 7611e8a98b5SPeter Maydell WRAP_FLOATCONV(int32_t, float64_to_int32_round_to_zero, float64, INT32_MIN) 7621e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64, float32, INT64_MIN) 7631e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float32_to_int64_round_to_zero, float32, INT64_MIN) 7641e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64, float64, INT64_MIN) 7651e8a98b5SPeter Maydell WRAP_FLOATCONV(int64_t, float64_to_int64_round_to_zero, float64, INT64_MIN) 7661e8a98b5SPeter Maydell 767ce4fa29fSPaolo Bonzini void glue(helper_cvtps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 768fcf5ef2aSThomas Huth { 7691e8a98b5SPeter Maydell d->ZMM_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status); 7701e8a98b5SPeter Maydell d->ZMM_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status); 7711e8a98b5SPeter Maydell d->ZMM_L(2) = x86_float32_to_int32(s->ZMM_S(2), &env->sse_status); 7721e8a98b5SPeter Maydell d->ZMM_L(3) = x86_float32_to_int32(s->ZMM_S(3), &env->sse_status); 773fcf5ef2aSThomas Huth } 774fcf5ef2aSThomas Huth 775ce4fa29fSPaolo Bonzini void glue(helper_cvtpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 776fcf5ef2aSThomas Huth { 7771e8a98b5SPeter Maydell d->ZMM_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status); 7781e8a98b5SPeter Maydell d->ZMM_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status); 779fcf5ef2aSThomas Huth d->ZMM_Q(1) = 0; 780fcf5ef2aSThomas Huth } 781fcf5ef2aSThomas Huth 782fcf5ef2aSThomas Huth void helper_cvtps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s) 783fcf5ef2aSThomas Huth { 7841e8a98b5SPeter Maydell d->MMX_L(0) = x86_float32_to_int32(s->ZMM_S(0), &env->sse_status); 7851e8a98b5SPeter Maydell d->MMX_L(1) = x86_float32_to_int32(s->ZMM_S(1), &env->sse_status); 786fcf5ef2aSThomas Huth } 787fcf5ef2aSThomas Huth 788fcf5ef2aSThomas Huth void helper_cvtpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s) 789fcf5ef2aSThomas Huth { 7901e8a98b5SPeter Maydell d->MMX_L(0) = x86_float64_to_int32(s->ZMM_D(0), &env->sse_status); 7911e8a98b5SPeter Maydell d->MMX_L(1) = x86_float64_to_int32(s->ZMM_D(1), &env->sse_status); 792fcf5ef2aSThomas Huth } 793fcf5ef2aSThomas Huth 794fcf5ef2aSThomas Huth int32_t helper_cvtss2si(CPUX86State *env, ZMMReg *s) 795fcf5ef2aSThomas Huth { 7961e8a98b5SPeter Maydell return x86_float32_to_int32(s->ZMM_S(0), &env->sse_status); 797fcf5ef2aSThomas Huth } 798fcf5ef2aSThomas Huth 799fcf5ef2aSThomas Huth int32_t helper_cvtsd2si(CPUX86State *env, ZMMReg *s) 800fcf5ef2aSThomas Huth { 8011e8a98b5SPeter Maydell return x86_float64_to_int32(s->ZMM_D(0), &env->sse_status); 802fcf5ef2aSThomas Huth } 803fcf5ef2aSThomas Huth 804fcf5ef2aSThomas Huth #ifdef TARGET_X86_64 805fcf5ef2aSThomas Huth int64_t helper_cvtss2sq(CPUX86State *env, ZMMReg *s) 806fcf5ef2aSThomas Huth { 8071e8a98b5SPeter Maydell return x86_float32_to_int64(s->ZMM_S(0), &env->sse_status); 808fcf5ef2aSThomas Huth } 809fcf5ef2aSThomas Huth 810fcf5ef2aSThomas Huth int64_t helper_cvtsd2sq(CPUX86State *env, ZMMReg *s) 811fcf5ef2aSThomas Huth { 8121e8a98b5SPeter Maydell return x86_float64_to_int64(s->ZMM_D(0), &env->sse_status); 813fcf5ef2aSThomas Huth } 814fcf5ef2aSThomas Huth #endif 815fcf5ef2aSThomas Huth 816fcf5ef2aSThomas Huth /* float to integer truncated */ 817ce4fa29fSPaolo Bonzini void glue(helper_cvttps2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 818fcf5ef2aSThomas Huth { 8191e8a98b5SPeter Maydell d->ZMM_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status); 8201e8a98b5SPeter Maydell d->ZMM_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status); 8211e8a98b5SPeter Maydell d->ZMM_L(2) = x86_float32_to_int32_round_to_zero(s->ZMM_S(2), &env->sse_status); 8221e8a98b5SPeter Maydell d->ZMM_L(3) = x86_float32_to_int32_round_to_zero(s->ZMM_S(3), &env->sse_status); 823fcf5ef2aSThomas Huth } 824fcf5ef2aSThomas Huth 825ce4fa29fSPaolo Bonzini void glue(helper_cvttpd2dq, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 826fcf5ef2aSThomas Huth { 8271e8a98b5SPeter Maydell d->ZMM_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status); 8281e8a98b5SPeter Maydell d->ZMM_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status); 829fcf5ef2aSThomas Huth d->ZMM_Q(1) = 0; 830fcf5ef2aSThomas Huth } 831fcf5ef2aSThomas Huth 832fcf5ef2aSThomas Huth void helper_cvttps2pi(CPUX86State *env, MMXReg *d, ZMMReg *s) 833fcf5ef2aSThomas Huth { 8341e8a98b5SPeter Maydell d->MMX_L(0) = x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status); 8351e8a98b5SPeter Maydell d->MMX_L(1) = x86_float32_to_int32_round_to_zero(s->ZMM_S(1), &env->sse_status); 836fcf5ef2aSThomas Huth } 837fcf5ef2aSThomas Huth 838fcf5ef2aSThomas Huth void helper_cvttpd2pi(CPUX86State *env, MMXReg *d, ZMMReg *s) 839fcf5ef2aSThomas Huth { 8401e8a98b5SPeter Maydell d->MMX_L(0) = x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status); 8411e8a98b5SPeter Maydell d->MMX_L(1) = x86_float64_to_int32_round_to_zero(s->ZMM_D(1), &env->sse_status); 842fcf5ef2aSThomas Huth } 843fcf5ef2aSThomas Huth 844fcf5ef2aSThomas Huth int32_t helper_cvttss2si(CPUX86State *env, ZMMReg *s) 845fcf5ef2aSThomas Huth { 8461e8a98b5SPeter Maydell return x86_float32_to_int32_round_to_zero(s->ZMM_S(0), &env->sse_status); 847fcf5ef2aSThomas Huth } 848fcf5ef2aSThomas Huth 849fcf5ef2aSThomas Huth int32_t helper_cvttsd2si(CPUX86State *env, ZMMReg *s) 850fcf5ef2aSThomas Huth { 8511e8a98b5SPeter Maydell return x86_float64_to_int32_round_to_zero(s->ZMM_D(0), &env->sse_status); 852fcf5ef2aSThomas Huth } 853fcf5ef2aSThomas Huth 854fcf5ef2aSThomas Huth #ifdef TARGET_X86_64 855fcf5ef2aSThomas Huth int64_t helper_cvttss2sq(CPUX86State *env, ZMMReg *s) 856fcf5ef2aSThomas Huth { 8571e8a98b5SPeter Maydell return x86_float32_to_int64_round_to_zero(s->ZMM_S(0), &env->sse_status); 858fcf5ef2aSThomas Huth } 859fcf5ef2aSThomas Huth 860fcf5ef2aSThomas Huth int64_t helper_cvttsd2sq(CPUX86State *env, ZMMReg *s) 861fcf5ef2aSThomas Huth { 8621e8a98b5SPeter Maydell return x86_float64_to_int64_round_to_zero(s->ZMM_D(0), &env->sse_status); 863fcf5ef2aSThomas Huth } 864fcf5ef2aSThomas Huth #endif 865fcf5ef2aSThomas Huth 866ce4fa29fSPaolo Bonzini void glue(helper_rsqrtps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 867fcf5ef2aSThomas Huth { 868418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 869*3403cafeSPaul Brook int i; 870*3403cafeSPaul Brook for (i = 0; i < 2 << SHIFT; i++) { 871*3403cafeSPaul Brook d->ZMM_S(i) = float32_div(float32_one, 872*3403cafeSPaul Brook float32_sqrt(s->ZMM_S(i), &env->sse_status), 873fcf5ef2aSThomas Huth &env->sse_status); 874*3403cafeSPaul Brook } 875418b0f93SJoseph Myers set_float_exception_flags(old_flags, &env->sse_status); 876fcf5ef2aSThomas Huth } 877fcf5ef2aSThomas Huth 878fcf5ef2aSThomas Huth void helper_rsqrtss(CPUX86State *env, ZMMReg *d, ZMMReg *s) 879fcf5ef2aSThomas Huth { 880418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 881fcf5ef2aSThomas Huth d->ZMM_S(0) = float32_div(float32_one, 882fcf5ef2aSThomas Huth float32_sqrt(s->ZMM_S(0), &env->sse_status), 883fcf5ef2aSThomas Huth &env->sse_status); 884418b0f93SJoseph Myers set_float_exception_flags(old_flags, &env->sse_status); 885fcf5ef2aSThomas Huth } 886fcf5ef2aSThomas Huth 887ce4fa29fSPaolo Bonzini void glue(helper_rcpps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 888fcf5ef2aSThomas Huth { 889418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 890*3403cafeSPaul Brook int i; 891*3403cafeSPaul Brook for (i = 0; i < 2 << SHIFT; i++) { 892*3403cafeSPaul Brook d->ZMM_S(i) = float32_div(float32_one, s->ZMM_S(i), &env->sse_status); 893*3403cafeSPaul Brook } 894418b0f93SJoseph Myers set_float_exception_flags(old_flags, &env->sse_status); 895fcf5ef2aSThomas Huth } 896fcf5ef2aSThomas Huth 897fcf5ef2aSThomas Huth void helper_rcpss(CPUX86State *env, ZMMReg *d, ZMMReg *s) 898fcf5ef2aSThomas Huth { 899418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 900fcf5ef2aSThomas Huth d->ZMM_S(0) = float32_div(float32_one, s->ZMM_S(0), &env->sse_status); 901418b0f93SJoseph Myers set_float_exception_flags(old_flags, &env->sse_status); 902fcf5ef2aSThomas Huth } 903fcf5ef2aSThomas Huth 904fcf5ef2aSThomas Huth static inline uint64_t helper_extrq(uint64_t src, int shift, int len) 905fcf5ef2aSThomas Huth { 906fcf5ef2aSThomas Huth uint64_t mask; 907fcf5ef2aSThomas Huth 908fcf5ef2aSThomas Huth if (len == 0) { 909fcf5ef2aSThomas Huth mask = ~0LL; 910fcf5ef2aSThomas Huth } else { 911fcf5ef2aSThomas Huth mask = (1ULL << len) - 1; 912fcf5ef2aSThomas Huth } 913fcf5ef2aSThomas Huth return (src >> shift) & mask; 914fcf5ef2aSThomas Huth } 915fcf5ef2aSThomas Huth 916fcf5ef2aSThomas Huth void helper_extrq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s) 917fcf5ef2aSThomas Huth { 918fcf5ef2aSThomas Huth d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), s->ZMM_B(1), s->ZMM_B(0)); 919fcf5ef2aSThomas Huth } 920fcf5ef2aSThomas Huth 921fcf5ef2aSThomas Huth void helper_extrq_i(CPUX86State *env, ZMMReg *d, int index, int length) 922fcf5ef2aSThomas Huth { 923fcf5ef2aSThomas Huth d->ZMM_Q(0) = helper_extrq(d->ZMM_Q(0), index, length); 924fcf5ef2aSThomas Huth } 925fcf5ef2aSThomas Huth 926fcf5ef2aSThomas Huth static inline uint64_t helper_insertq(uint64_t src, int shift, int len) 927fcf5ef2aSThomas Huth { 928fcf5ef2aSThomas Huth uint64_t mask; 929fcf5ef2aSThomas Huth 930fcf5ef2aSThomas Huth if (len == 0) { 931fcf5ef2aSThomas Huth mask = ~0ULL; 932fcf5ef2aSThomas Huth } else { 933fcf5ef2aSThomas Huth mask = (1ULL << len) - 1; 934fcf5ef2aSThomas Huth } 935fcf5ef2aSThomas Huth return (src & ~(mask << shift)) | ((src & mask) << shift); 936fcf5ef2aSThomas Huth } 937fcf5ef2aSThomas Huth 938fcf5ef2aSThomas Huth void helper_insertq_r(CPUX86State *env, ZMMReg *d, ZMMReg *s) 939fcf5ef2aSThomas Huth { 940fcf5ef2aSThomas Huth d->ZMM_Q(0) = helper_insertq(s->ZMM_Q(0), s->ZMM_B(9), s->ZMM_B(8)); 941fcf5ef2aSThomas Huth } 942fcf5ef2aSThomas Huth 943fcf5ef2aSThomas Huth void helper_insertq_i(CPUX86State *env, ZMMReg *d, int index, int length) 944fcf5ef2aSThomas Huth { 945fcf5ef2aSThomas Huth d->ZMM_Q(0) = helper_insertq(d->ZMM_Q(0), index, length); 946fcf5ef2aSThomas Huth } 947fcf5ef2aSThomas Huth 948ce4fa29fSPaolo Bonzini void glue(helper_haddps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 949fcf5ef2aSThomas Huth { 950fcf5ef2aSThomas Huth ZMMReg r; 951fcf5ef2aSThomas Huth 952fcf5ef2aSThomas Huth r.ZMM_S(0) = float32_add(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status); 953fcf5ef2aSThomas Huth r.ZMM_S(1) = float32_add(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status); 954fcf5ef2aSThomas Huth r.ZMM_S(2) = float32_add(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status); 955fcf5ef2aSThomas Huth r.ZMM_S(3) = float32_add(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status); 956d22697ddSPaolo Bonzini MOVE(*d, r); 957fcf5ef2aSThomas Huth } 958fcf5ef2aSThomas Huth 959ce4fa29fSPaolo Bonzini void glue(helper_haddpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 960fcf5ef2aSThomas Huth { 961fcf5ef2aSThomas Huth ZMMReg r; 962fcf5ef2aSThomas Huth 963fcf5ef2aSThomas Huth r.ZMM_D(0) = float64_add(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status); 964fcf5ef2aSThomas Huth r.ZMM_D(1) = float64_add(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status); 965d22697ddSPaolo Bonzini MOVE(*d, r); 966fcf5ef2aSThomas Huth } 967fcf5ef2aSThomas Huth 968ce4fa29fSPaolo Bonzini void glue(helper_hsubps, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 969fcf5ef2aSThomas Huth { 970fcf5ef2aSThomas Huth ZMMReg r; 971fcf5ef2aSThomas Huth 972fcf5ef2aSThomas Huth r.ZMM_S(0) = float32_sub(d->ZMM_S(0), d->ZMM_S(1), &env->sse_status); 973fcf5ef2aSThomas Huth r.ZMM_S(1) = float32_sub(d->ZMM_S(2), d->ZMM_S(3), &env->sse_status); 974fcf5ef2aSThomas Huth r.ZMM_S(2) = float32_sub(s->ZMM_S(0), s->ZMM_S(1), &env->sse_status); 975fcf5ef2aSThomas Huth r.ZMM_S(3) = float32_sub(s->ZMM_S(2), s->ZMM_S(3), &env->sse_status); 976d22697ddSPaolo Bonzini MOVE(*d, r); 977fcf5ef2aSThomas Huth } 978fcf5ef2aSThomas Huth 979ce4fa29fSPaolo Bonzini void glue(helper_hsubpd, SUFFIX)(CPUX86State *env, ZMMReg *d, ZMMReg *s) 980fcf5ef2aSThomas Huth { 981fcf5ef2aSThomas Huth ZMMReg r; 982fcf5ef2aSThomas Huth 983fcf5ef2aSThomas Huth r.ZMM_D(0) = float64_sub(d->ZMM_D(0), d->ZMM_D(1), &env->sse_status); 984fcf5ef2aSThomas Huth r.ZMM_D(1) = float64_sub(s->ZMM_D(0), s->ZMM_D(1), &env->sse_status); 985d22697ddSPaolo Bonzini MOVE(*d, r); 986fcf5ef2aSThomas Huth } 987fcf5ef2aSThomas Huth 988*3403cafeSPaul Brook void glue(helper_addsubps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 989fcf5ef2aSThomas Huth { 990*3403cafeSPaul Brook Reg *v = d; 991*3403cafeSPaul Brook int i; 992*3403cafeSPaul Brook for (i = 0; i < 2 << SHIFT; i += 2) { 993*3403cafeSPaul Brook d->ZMM_S(i) = float32_sub(v->ZMM_S(i), s->ZMM_S(i), &env->sse_status); 994*3403cafeSPaul Brook d->ZMM_S(i+1) = float32_add(v->ZMM_S(i+1), s->ZMM_S(i+1), &env->sse_status); 995*3403cafeSPaul Brook } 996fcf5ef2aSThomas Huth } 997fcf5ef2aSThomas Huth 998*3403cafeSPaul Brook void glue(helper_addsubpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 999fcf5ef2aSThomas Huth { 1000*3403cafeSPaul Brook Reg *v = d; 1001*3403cafeSPaul Brook int i; 1002*3403cafeSPaul Brook for (i = 0; i < 1 << SHIFT; i += 2) { 1003*3403cafeSPaul Brook d->ZMM_D(i) = float64_sub(v->ZMM_D(i), s->ZMM_D(i), &env->sse_status); 1004*3403cafeSPaul Brook d->ZMM_D(i+1) = float64_add(v->ZMM_D(i+1), s->ZMM_D(i+1), &env->sse_status); 1005*3403cafeSPaul Brook } 1006fcf5ef2aSThomas Huth } 1007fcf5ef2aSThomas Huth 1008fcf5ef2aSThomas Huth /* XXX: unordered */ 1009fcf5ef2aSThomas Huth #define SSE_HELPER_CMP(name, F) \ 1010ce4fa29fSPaolo Bonzini void glue(helper_ ## name ## ps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\ 1011fcf5ef2aSThomas Huth { \ 1012fcf5ef2aSThomas Huth d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \ 1013fcf5ef2aSThomas Huth d->ZMM_L(1) = F(32, d->ZMM_S(1), s->ZMM_S(1)); \ 1014fcf5ef2aSThomas Huth d->ZMM_L(2) = F(32, d->ZMM_S(2), s->ZMM_S(2)); \ 1015fcf5ef2aSThomas Huth d->ZMM_L(3) = F(32, d->ZMM_S(3), s->ZMM_S(3)); \ 1016fcf5ef2aSThomas Huth } \ 1017fcf5ef2aSThomas Huth \ 1018fcf5ef2aSThomas Huth void helper_ ## name ## ss(CPUX86State *env, Reg *d, Reg *s) \ 1019fcf5ef2aSThomas Huth { \ 1020fcf5ef2aSThomas Huth d->ZMM_L(0) = F(32, d->ZMM_S(0), s->ZMM_S(0)); \ 1021fcf5ef2aSThomas Huth } \ 1022fcf5ef2aSThomas Huth \ 1023ce4fa29fSPaolo Bonzini void glue(helper_ ## name ## pd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s)\ 1024fcf5ef2aSThomas Huth { \ 1025fcf5ef2aSThomas Huth d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \ 1026fcf5ef2aSThomas Huth d->ZMM_Q(1) = F(64, d->ZMM_D(1), s->ZMM_D(1)); \ 1027fcf5ef2aSThomas Huth } \ 1028fcf5ef2aSThomas Huth \ 1029fcf5ef2aSThomas Huth void helper_ ## name ## sd(CPUX86State *env, Reg *d, Reg *s) \ 1030fcf5ef2aSThomas Huth { \ 1031fcf5ef2aSThomas Huth d->ZMM_Q(0) = F(64, d->ZMM_D(0), s->ZMM_D(0)); \ 1032fcf5ef2aSThomas Huth } 1033fcf5ef2aSThomas Huth 1034fcf5ef2aSThomas Huth #define FPU_CMPEQ(size, a, b) \ 1035fcf5ef2aSThomas Huth (float ## size ## _eq_quiet(a, b, &env->sse_status) ? -1 : 0) 1036fcf5ef2aSThomas Huth #define FPU_CMPLT(size, a, b) \ 1037fcf5ef2aSThomas Huth (float ## size ## _lt(a, b, &env->sse_status) ? -1 : 0) 1038fcf5ef2aSThomas Huth #define FPU_CMPLE(size, a, b) \ 1039fcf5ef2aSThomas Huth (float ## size ## _le(a, b, &env->sse_status) ? -1 : 0) 1040fcf5ef2aSThomas Huth #define FPU_CMPUNORD(size, a, b) \ 1041fcf5ef2aSThomas Huth (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? -1 : 0) 1042fcf5ef2aSThomas Huth #define FPU_CMPNEQ(size, a, b) \ 1043fcf5ef2aSThomas Huth (float ## size ## _eq_quiet(a, b, &env->sse_status) ? 0 : -1) 1044fcf5ef2aSThomas Huth #define FPU_CMPNLT(size, a, b) \ 1045fcf5ef2aSThomas Huth (float ## size ## _lt(a, b, &env->sse_status) ? 0 : -1) 1046fcf5ef2aSThomas Huth #define FPU_CMPNLE(size, a, b) \ 1047fcf5ef2aSThomas Huth (float ## size ## _le(a, b, &env->sse_status) ? 0 : -1) 1048fcf5ef2aSThomas Huth #define FPU_CMPORD(size, a, b) \ 1049fcf5ef2aSThomas Huth (float ## size ## _unordered_quiet(a, b, &env->sse_status) ? 0 : -1) 1050fcf5ef2aSThomas Huth 1051fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpeq, FPU_CMPEQ) 1052fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmplt, FPU_CMPLT) 1053fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmple, FPU_CMPLE) 1054fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpunord, FPU_CMPUNORD) 1055fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpneq, FPU_CMPNEQ) 1056fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnlt, FPU_CMPNLT) 1057fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpnle, FPU_CMPNLE) 1058fcf5ef2aSThomas Huth SSE_HELPER_CMP(cmpord, FPU_CMPORD) 1059fcf5ef2aSThomas Huth 1060fcf5ef2aSThomas Huth static const int comis_eflags[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C}; 1061fcf5ef2aSThomas Huth 1062fcf5ef2aSThomas Huth void helper_ucomiss(CPUX86State *env, Reg *d, Reg *s) 1063fcf5ef2aSThomas Huth { 106471bfd65cSRichard Henderson FloatRelation ret; 1065fcf5ef2aSThomas Huth float32 s0, s1; 1066fcf5ef2aSThomas Huth 1067fcf5ef2aSThomas Huth s0 = d->ZMM_S(0); 1068fcf5ef2aSThomas Huth s1 = s->ZMM_S(0); 1069fcf5ef2aSThomas Huth ret = float32_compare_quiet(s0, s1, &env->sse_status); 1070fcf5ef2aSThomas Huth CC_SRC = comis_eflags[ret + 1]; 1071fcf5ef2aSThomas Huth } 1072fcf5ef2aSThomas Huth 1073fcf5ef2aSThomas Huth void helper_comiss(CPUX86State *env, Reg *d, Reg *s) 1074fcf5ef2aSThomas Huth { 107571bfd65cSRichard Henderson FloatRelation ret; 1076fcf5ef2aSThomas Huth float32 s0, s1; 1077fcf5ef2aSThomas Huth 1078fcf5ef2aSThomas Huth s0 = d->ZMM_S(0); 1079fcf5ef2aSThomas Huth s1 = s->ZMM_S(0); 1080fcf5ef2aSThomas Huth ret = float32_compare(s0, s1, &env->sse_status); 1081fcf5ef2aSThomas Huth CC_SRC = comis_eflags[ret + 1]; 1082fcf5ef2aSThomas Huth } 1083fcf5ef2aSThomas Huth 1084fcf5ef2aSThomas Huth void helper_ucomisd(CPUX86State *env, Reg *d, Reg *s) 1085fcf5ef2aSThomas Huth { 108671bfd65cSRichard Henderson FloatRelation ret; 1087fcf5ef2aSThomas Huth float64 d0, d1; 1088fcf5ef2aSThomas Huth 1089fcf5ef2aSThomas Huth d0 = d->ZMM_D(0); 1090fcf5ef2aSThomas Huth d1 = s->ZMM_D(0); 1091fcf5ef2aSThomas Huth ret = float64_compare_quiet(d0, d1, &env->sse_status); 1092fcf5ef2aSThomas Huth CC_SRC = comis_eflags[ret + 1]; 1093fcf5ef2aSThomas Huth } 1094fcf5ef2aSThomas Huth 1095fcf5ef2aSThomas Huth void helper_comisd(CPUX86State *env, Reg *d, Reg *s) 1096fcf5ef2aSThomas Huth { 109771bfd65cSRichard Henderson FloatRelation ret; 1098fcf5ef2aSThomas Huth float64 d0, d1; 1099fcf5ef2aSThomas Huth 1100fcf5ef2aSThomas Huth d0 = d->ZMM_D(0); 1101fcf5ef2aSThomas Huth d1 = s->ZMM_D(0); 1102fcf5ef2aSThomas Huth ret = float64_compare(d0, d1, &env->sse_status); 1103fcf5ef2aSThomas Huth CC_SRC = comis_eflags[ret + 1]; 1104fcf5ef2aSThomas Huth } 1105fcf5ef2aSThomas Huth 1106ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskps, SUFFIX)(CPUX86State *env, Reg *s) 1107fcf5ef2aSThomas Huth { 1108fcf5ef2aSThomas Huth int b0, b1, b2, b3; 1109fcf5ef2aSThomas Huth 1110fcf5ef2aSThomas Huth b0 = s->ZMM_L(0) >> 31; 1111fcf5ef2aSThomas Huth b1 = s->ZMM_L(1) >> 31; 1112fcf5ef2aSThomas Huth b2 = s->ZMM_L(2) >> 31; 1113fcf5ef2aSThomas Huth b3 = s->ZMM_L(3) >> 31; 1114fcf5ef2aSThomas Huth return b0 | (b1 << 1) | (b2 << 2) | (b3 << 3); 1115fcf5ef2aSThomas Huth } 1116fcf5ef2aSThomas Huth 1117ce4fa29fSPaolo Bonzini uint32_t glue(helper_movmskpd, SUFFIX)(CPUX86State *env, Reg *s) 1118fcf5ef2aSThomas Huth { 1119fcf5ef2aSThomas Huth int b0, b1; 1120fcf5ef2aSThomas Huth 1121fcf5ef2aSThomas Huth b0 = s->ZMM_L(1) >> 31; 1122fcf5ef2aSThomas Huth b1 = s->ZMM_L(3) >> 31; 1123fcf5ef2aSThomas Huth return b0 | (b1 << 1); 1124fcf5ef2aSThomas Huth } 1125fcf5ef2aSThomas Huth 1126fcf5ef2aSThomas Huth #endif 1127fcf5ef2aSThomas Huth 1128fcf5ef2aSThomas Huth uint32_t glue(helper_pmovmskb, SUFFIX)(CPUX86State *env, Reg *s) 1129fcf5ef2aSThomas Huth { 1130fcf5ef2aSThomas Huth uint32_t val; 1131e894bae8SPaul Brook int i; 1132fcf5ef2aSThomas Huth 1133fcf5ef2aSThomas Huth val = 0; 1134e894bae8SPaul Brook for (i = 0; i < (1 << SHIFT); i++) { 1135e894bae8SPaul Brook uint8_t byte = 0; 1136e894bae8SPaul Brook byte |= (s->B(8 * i + 0) >> 7); 1137e894bae8SPaul Brook byte |= (s->B(8 * i + 1) >> 6) & 0x02; 1138e894bae8SPaul Brook byte |= (s->B(8 * i + 2) >> 5) & 0x04; 1139e894bae8SPaul Brook byte |= (s->B(8 * i + 3) >> 4) & 0x08; 1140e894bae8SPaul Brook byte |= (s->B(8 * i + 4) >> 3) & 0x10; 1141e894bae8SPaul Brook byte |= (s->B(8 * i + 5) >> 2) & 0x20; 1142e894bae8SPaul Brook byte |= (s->B(8 * i + 6) >> 1) & 0x40; 1143e894bae8SPaul Brook byte |= (s->B(8 * i + 7)) & 0x80; 1144e894bae8SPaul Brook val |= byte << (8 * i); 1145e894bae8SPaul Brook } 1146fcf5ef2aSThomas Huth return val; 1147fcf5ef2aSThomas Huth } 1148fcf5ef2aSThomas Huth 1149d45b0de6SPaul Brook #define PACK_HELPER_B(name, F) \ 1150d45b0de6SPaul Brook void glue(helper_pack ## name, SUFFIX)(CPUX86State *env, \ 1151d45b0de6SPaul Brook Reg *d, Reg *s) \ 1152d45b0de6SPaul Brook { \ 1153d45b0de6SPaul Brook Reg *v = d; \ 1154d45b0de6SPaul Brook uint8_t r[PACK_WIDTH * 2]; \ 1155d45b0de6SPaul Brook int j, k; \ 1156d45b0de6SPaul Brook for (j = 0; j < 4 << SHIFT; j += PACK_WIDTH) { \ 1157d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH; k++) { \ 1158d45b0de6SPaul Brook r[k] = F((int16_t)v->W(j + k)); \ 1159d45b0de6SPaul Brook } \ 1160d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH; k++) { \ 1161d45b0de6SPaul Brook r[PACK_WIDTH + k] = F((int16_t)s->W(j + k)); \ 1162d45b0de6SPaul Brook } \ 1163d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH * 2; k++) { \ 1164d45b0de6SPaul Brook d->B(2 * j + k) = r[k]; \ 1165d45b0de6SPaul Brook } \ 1166d45b0de6SPaul Brook } \ 1167fcf5ef2aSThomas Huth } 1168fcf5ef2aSThomas Huth 1169d45b0de6SPaul Brook PACK_HELPER_B(sswb, satsb) 1170d45b0de6SPaul Brook PACK_HELPER_B(uswb, satub) 1171fcf5ef2aSThomas Huth 1172fcf5ef2aSThomas Huth void glue(helper_packssdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1173fcf5ef2aSThomas Huth { 1174d45b0de6SPaul Brook Reg *v = d; 1175d45b0de6SPaul Brook uint16_t r[PACK_WIDTH]; 1176d45b0de6SPaul Brook int j, k; 1177fcf5ef2aSThomas Huth 1178d45b0de6SPaul Brook for (j = 0; j < 2 << SHIFT; j += PACK_WIDTH / 2) { 1179d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH / 2; k++) { 1180d45b0de6SPaul Brook r[k] = satsw(v->L(j + k)); 1181d45b0de6SPaul Brook } 1182d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH / 2; k++) { 1183d45b0de6SPaul Brook r[PACK_WIDTH / 2 + k] = satsw(s->L(j + k)); 1184d45b0de6SPaul Brook } 1185d45b0de6SPaul Brook for (k = 0; k < PACK_WIDTH; k++) { 1186d45b0de6SPaul Brook d->W(2 * j + k) = r[k]; 1187d45b0de6SPaul Brook } 1188d45b0de6SPaul Brook } 1189fcf5ef2aSThomas Huth } 1190fcf5ef2aSThomas Huth 1191fcf5ef2aSThomas Huth #define UNPCK_OP(base_name, base) \ 1192fcf5ef2aSThomas Huth \ 1193fcf5ef2aSThomas Huth void glue(helper_punpck ## base_name ## bw, SUFFIX)(CPUX86State *env,\ 1194fcf5ef2aSThomas Huth Reg *d, Reg *s) \ 1195fcf5ef2aSThomas Huth { \ 1196d45b0de6SPaul Brook Reg *v = d; \ 1197d45b0de6SPaul Brook uint8_t r[PACK_WIDTH * 2]; \ 1198d45b0de6SPaul Brook int j, i; \ 1199fcf5ef2aSThomas Huth \ 1200d45b0de6SPaul Brook for (j = 0; j < 8 << SHIFT; ) { \ 1201d45b0de6SPaul Brook int k = j + base * PACK_WIDTH; \ 1202d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH; i++) { \ 1203d45b0de6SPaul Brook r[2 * i] = v->B(k + i); \ 1204d45b0de6SPaul Brook r[2 * i + 1] = s->B(k + i); \ 1205d45b0de6SPaul Brook } \ 1206d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH * 2; i++, j++) { \ 1207d45b0de6SPaul Brook d->B(j) = r[i]; \ 1208d45b0de6SPaul Brook } \ 1209d45b0de6SPaul Brook } \ 1210fcf5ef2aSThomas Huth } \ 1211fcf5ef2aSThomas Huth \ 1212fcf5ef2aSThomas Huth void glue(helper_punpck ## base_name ## wd, SUFFIX)(CPUX86State *env,\ 1213fcf5ef2aSThomas Huth Reg *d, Reg *s) \ 1214fcf5ef2aSThomas Huth { \ 1215d45b0de6SPaul Brook Reg *v = d; \ 1216d45b0de6SPaul Brook uint16_t r[PACK_WIDTH]; \ 1217d45b0de6SPaul Brook int j, i; \ 1218fcf5ef2aSThomas Huth \ 1219d45b0de6SPaul Brook for (j = 0; j < 4 << SHIFT; ) { \ 1220d45b0de6SPaul Brook int k = j + base * PACK_WIDTH / 2; \ 1221d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH / 2; i++) { \ 1222d45b0de6SPaul Brook r[2 * i] = v->W(k + i); \ 1223d45b0de6SPaul Brook r[2 * i + 1] = s->W(k + i); \ 1224d45b0de6SPaul Brook } \ 1225d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH; i++, j++) { \ 1226d45b0de6SPaul Brook d->W(j) = r[i]; \ 1227d45b0de6SPaul Brook } \ 1228d45b0de6SPaul Brook } \ 1229fcf5ef2aSThomas Huth } \ 1230fcf5ef2aSThomas Huth \ 1231fcf5ef2aSThomas Huth void glue(helper_punpck ## base_name ## dq, SUFFIX)(CPUX86State *env,\ 1232fcf5ef2aSThomas Huth Reg *d, Reg *s) \ 1233fcf5ef2aSThomas Huth { \ 1234d45b0de6SPaul Brook Reg *v = d; \ 1235d45b0de6SPaul Brook uint32_t r[PACK_WIDTH / 2]; \ 1236d45b0de6SPaul Brook int j, i; \ 1237fcf5ef2aSThomas Huth \ 1238d45b0de6SPaul Brook for (j = 0; j < 2 << SHIFT; ) { \ 1239d45b0de6SPaul Brook int k = j + base * PACK_WIDTH / 4; \ 1240d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH / 4; i++) { \ 1241d45b0de6SPaul Brook r[2 * i] = v->L(k + i); \ 1242d45b0de6SPaul Brook r[2 * i + 1] = s->L(k + i); \ 1243d45b0de6SPaul Brook } \ 1244d45b0de6SPaul Brook for (i = 0; i < PACK_WIDTH / 2; i++, j++) { \ 1245d45b0de6SPaul Brook d->L(j) = r[i]; \ 1246d45b0de6SPaul Brook } \ 1247d45b0de6SPaul Brook } \ 1248fcf5ef2aSThomas Huth } \ 1249fcf5ef2aSThomas Huth \ 1250fcf5ef2aSThomas Huth XMM_ONLY( \ 1251d45b0de6SPaul Brook void glue(helper_punpck ## base_name ## qdq, SUFFIX)( \ 1252d45b0de6SPaul Brook CPUX86State *env, Reg *d, Reg *s) \ 1253fcf5ef2aSThomas Huth { \ 1254d45b0de6SPaul Brook Reg *v = d; \ 1255d45b0de6SPaul Brook uint64_t r[2]; \ 1256d45b0de6SPaul Brook int i; \ 1257fcf5ef2aSThomas Huth \ 1258d45b0de6SPaul Brook for (i = 0; i < 1 << SHIFT; i += 2) { \ 1259d45b0de6SPaul Brook r[0] = v->Q(base + i); \ 1260d45b0de6SPaul Brook r[1] = s->Q(base + i); \ 1261d45b0de6SPaul Brook d->Q(i) = r[0]; \ 1262d45b0de6SPaul Brook d->Q(i + 1) = r[1]; \ 1263d45b0de6SPaul Brook } \ 1264fcf5ef2aSThomas Huth } \ 1265fcf5ef2aSThomas Huth ) 1266fcf5ef2aSThomas Huth 1267fcf5ef2aSThomas Huth UNPCK_OP(l, 0) 1268fcf5ef2aSThomas Huth UNPCK_OP(h, 1) 1269fcf5ef2aSThomas Huth 1270d45b0de6SPaul Brook #undef PACK_WIDTH 1271d45b0de6SPaul Brook #undef PACK_HELPER_B 1272d45b0de6SPaul Brook #undef UNPCK_OP 1273d45b0de6SPaul Brook 1274d45b0de6SPaul Brook 1275fcf5ef2aSThomas Huth /* 3DNow! float ops */ 1276fcf5ef2aSThomas Huth #if SHIFT == 0 1277fcf5ef2aSThomas Huth void helper_pi2fd(CPUX86State *env, MMXReg *d, MMXReg *s) 1278fcf5ef2aSThomas Huth { 1279fcf5ef2aSThomas Huth d->MMX_S(0) = int32_to_float32(s->MMX_L(0), &env->mmx_status); 1280fcf5ef2aSThomas Huth d->MMX_S(1) = int32_to_float32(s->MMX_L(1), &env->mmx_status); 1281fcf5ef2aSThomas Huth } 1282fcf5ef2aSThomas Huth 1283fcf5ef2aSThomas Huth void helper_pi2fw(CPUX86State *env, MMXReg *d, MMXReg *s) 1284fcf5ef2aSThomas Huth { 1285fcf5ef2aSThomas Huth d->MMX_S(0) = int32_to_float32((int16_t)s->MMX_W(0), &env->mmx_status); 1286fcf5ef2aSThomas Huth d->MMX_S(1) = int32_to_float32((int16_t)s->MMX_W(2), &env->mmx_status); 1287fcf5ef2aSThomas Huth } 1288fcf5ef2aSThomas Huth 1289fcf5ef2aSThomas Huth void helper_pf2id(CPUX86State *env, MMXReg *d, MMXReg *s) 1290fcf5ef2aSThomas Huth { 1291fcf5ef2aSThomas Huth d->MMX_L(0) = float32_to_int32_round_to_zero(s->MMX_S(0), &env->mmx_status); 1292fcf5ef2aSThomas Huth d->MMX_L(1) = float32_to_int32_round_to_zero(s->MMX_S(1), &env->mmx_status); 1293fcf5ef2aSThomas Huth } 1294fcf5ef2aSThomas Huth 1295fcf5ef2aSThomas Huth void helper_pf2iw(CPUX86State *env, MMXReg *d, MMXReg *s) 1296fcf5ef2aSThomas Huth { 1297fcf5ef2aSThomas Huth d->MMX_L(0) = satsw(float32_to_int32_round_to_zero(s->MMX_S(0), 1298fcf5ef2aSThomas Huth &env->mmx_status)); 1299fcf5ef2aSThomas Huth d->MMX_L(1) = satsw(float32_to_int32_round_to_zero(s->MMX_S(1), 1300fcf5ef2aSThomas Huth &env->mmx_status)); 1301fcf5ef2aSThomas Huth } 1302fcf5ef2aSThomas Huth 1303fcf5ef2aSThomas Huth void helper_pfacc(CPUX86State *env, MMXReg *d, MMXReg *s) 1304fcf5ef2aSThomas Huth { 130525bdec79SPaolo Bonzini float32 r; 1306fcf5ef2aSThomas Huth 130725bdec79SPaolo Bonzini r = float32_add(d->MMX_S(0), d->MMX_S(1), &env->mmx_status); 130825bdec79SPaolo Bonzini d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status); 130925bdec79SPaolo Bonzini d->MMX_S(0) = r; 1310fcf5ef2aSThomas Huth } 1311fcf5ef2aSThomas Huth 1312fcf5ef2aSThomas Huth void helper_pfadd(CPUX86State *env, MMXReg *d, MMXReg *s) 1313fcf5ef2aSThomas Huth { 1314fcf5ef2aSThomas Huth d->MMX_S(0) = float32_add(d->MMX_S(0), s->MMX_S(0), &env->mmx_status); 1315fcf5ef2aSThomas Huth d->MMX_S(1) = float32_add(d->MMX_S(1), s->MMX_S(1), &env->mmx_status); 1316fcf5ef2aSThomas Huth } 1317fcf5ef2aSThomas Huth 1318fcf5ef2aSThomas Huth void helper_pfcmpeq(CPUX86State *env, MMXReg *d, MMXReg *s) 1319fcf5ef2aSThomas Huth { 1320fcf5ef2aSThomas Huth d->MMX_L(0) = float32_eq_quiet(d->MMX_S(0), s->MMX_S(0), 1321fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1322fcf5ef2aSThomas Huth d->MMX_L(1) = float32_eq_quiet(d->MMX_S(1), s->MMX_S(1), 1323fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1324fcf5ef2aSThomas Huth } 1325fcf5ef2aSThomas Huth 1326fcf5ef2aSThomas Huth void helper_pfcmpge(CPUX86State *env, MMXReg *d, MMXReg *s) 1327fcf5ef2aSThomas Huth { 1328fcf5ef2aSThomas Huth d->MMX_L(0) = float32_le(s->MMX_S(0), d->MMX_S(0), 1329fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1330fcf5ef2aSThomas Huth d->MMX_L(1) = float32_le(s->MMX_S(1), d->MMX_S(1), 1331fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1332fcf5ef2aSThomas Huth } 1333fcf5ef2aSThomas Huth 1334fcf5ef2aSThomas Huth void helper_pfcmpgt(CPUX86State *env, MMXReg *d, MMXReg *s) 1335fcf5ef2aSThomas Huth { 1336fcf5ef2aSThomas Huth d->MMX_L(0) = float32_lt(s->MMX_S(0), d->MMX_S(0), 1337fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1338fcf5ef2aSThomas Huth d->MMX_L(1) = float32_lt(s->MMX_S(1), d->MMX_S(1), 1339fcf5ef2aSThomas Huth &env->mmx_status) ? -1 : 0; 1340fcf5ef2aSThomas Huth } 1341fcf5ef2aSThomas Huth 1342fcf5ef2aSThomas Huth void helper_pfmax(CPUX86State *env, MMXReg *d, MMXReg *s) 1343fcf5ef2aSThomas Huth { 1344fcf5ef2aSThomas Huth if (float32_lt(d->MMX_S(0), s->MMX_S(0), &env->mmx_status)) { 1345fcf5ef2aSThomas Huth d->MMX_S(0) = s->MMX_S(0); 1346fcf5ef2aSThomas Huth } 1347fcf5ef2aSThomas Huth if (float32_lt(d->MMX_S(1), s->MMX_S(1), &env->mmx_status)) { 1348fcf5ef2aSThomas Huth d->MMX_S(1) = s->MMX_S(1); 1349fcf5ef2aSThomas Huth } 1350fcf5ef2aSThomas Huth } 1351fcf5ef2aSThomas Huth 1352fcf5ef2aSThomas Huth void helper_pfmin(CPUX86State *env, MMXReg *d, MMXReg *s) 1353fcf5ef2aSThomas Huth { 1354fcf5ef2aSThomas Huth if (float32_lt(s->MMX_S(0), d->MMX_S(0), &env->mmx_status)) { 1355fcf5ef2aSThomas Huth d->MMX_S(0) = s->MMX_S(0); 1356fcf5ef2aSThomas Huth } 1357fcf5ef2aSThomas Huth if (float32_lt(s->MMX_S(1), d->MMX_S(1), &env->mmx_status)) { 1358fcf5ef2aSThomas Huth d->MMX_S(1) = s->MMX_S(1); 1359fcf5ef2aSThomas Huth } 1360fcf5ef2aSThomas Huth } 1361fcf5ef2aSThomas Huth 1362fcf5ef2aSThomas Huth void helper_pfmul(CPUX86State *env, MMXReg *d, MMXReg *s) 1363fcf5ef2aSThomas Huth { 1364fcf5ef2aSThomas Huth d->MMX_S(0) = float32_mul(d->MMX_S(0), s->MMX_S(0), &env->mmx_status); 1365fcf5ef2aSThomas Huth d->MMX_S(1) = float32_mul(d->MMX_S(1), s->MMX_S(1), &env->mmx_status); 1366fcf5ef2aSThomas Huth } 1367fcf5ef2aSThomas Huth 1368fcf5ef2aSThomas Huth void helper_pfnacc(CPUX86State *env, MMXReg *d, MMXReg *s) 1369fcf5ef2aSThomas Huth { 137025bdec79SPaolo Bonzini float32 r; 1371fcf5ef2aSThomas Huth 137225bdec79SPaolo Bonzini r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status); 137325bdec79SPaolo Bonzini d->MMX_S(1) = float32_sub(s->MMX_S(0), s->MMX_S(1), &env->mmx_status); 137425bdec79SPaolo Bonzini d->MMX_S(0) = r; 1375fcf5ef2aSThomas Huth } 1376fcf5ef2aSThomas Huth 1377fcf5ef2aSThomas Huth void helper_pfpnacc(CPUX86State *env, MMXReg *d, MMXReg *s) 1378fcf5ef2aSThomas Huth { 137925bdec79SPaolo Bonzini float32 r; 1380fcf5ef2aSThomas Huth 138125bdec79SPaolo Bonzini r = float32_sub(d->MMX_S(0), d->MMX_S(1), &env->mmx_status); 138225bdec79SPaolo Bonzini d->MMX_S(1) = float32_add(s->MMX_S(0), s->MMX_S(1), &env->mmx_status); 138325bdec79SPaolo Bonzini d->MMX_S(0) = r; 1384fcf5ef2aSThomas Huth } 1385fcf5ef2aSThomas Huth 1386fcf5ef2aSThomas Huth void helper_pfrcp(CPUX86State *env, MMXReg *d, MMXReg *s) 1387fcf5ef2aSThomas Huth { 1388fcf5ef2aSThomas Huth d->MMX_S(0) = float32_div(float32_one, s->MMX_S(0), &env->mmx_status); 1389fcf5ef2aSThomas Huth d->MMX_S(1) = d->MMX_S(0); 1390fcf5ef2aSThomas Huth } 1391fcf5ef2aSThomas Huth 1392fcf5ef2aSThomas Huth void helper_pfrsqrt(CPUX86State *env, MMXReg *d, MMXReg *s) 1393fcf5ef2aSThomas Huth { 1394fcf5ef2aSThomas Huth d->MMX_L(1) = s->MMX_L(0) & 0x7fffffff; 1395fcf5ef2aSThomas Huth d->MMX_S(1) = float32_div(float32_one, 1396fcf5ef2aSThomas Huth float32_sqrt(d->MMX_S(1), &env->mmx_status), 1397fcf5ef2aSThomas Huth &env->mmx_status); 1398fcf5ef2aSThomas Huth d->MMX_L(1) |= s->MMX_L(0) & 0x80000000; 1399fcf5ef2aSThomas Huth d->MMX_L(0) = d->MMX_L(1); 1400fcf5ef2aSThomas Huth } 1401fcf5ef2aSThomas Huth 1402fcf5ef2aSThomas Huth void helper_pfsub(CPUX86State *env, MMXReg *d, MMXReg *s) 1403fcf5ef2aSThomas Huth { 1404fcf5ef2aSThomas Huth d->MMX_S(0) = float32_sub(d->MMX_S(0), s->MMX_S(0), &env->mmx_status); 1405fcf5ef2aSThomas Huth d->MMX_S(1) = float32_sub(d->MMX_S(1), s->MMX_S(1), &env->mmx_status); 1406fcf5ef2aSThomas Huth } 1407fcf5ef2aSThomas Huth 1408fcf5ef2aSThomas Huth void helper_pfsubr(CPUX86State *env, MMXReg *d, MMXReg *s) 1409fcf5ef2aSThomas Huth { 1410fcf5ef2aSThomas Huth d->MMX_S(0) = float32_sub(s->MMX_S(0), d->MMX_S(0), &env->mmx_status); 1411fcf5ef2aSThomas Huth d->MMX_S(1) = float32_sub(s->MMX_S(1), d->MMX_S(1), &env->mmx_status); 1412fcf5ef2aSThomas Huth } 1413fcf5ef2aSThomas Huth 1414fcf5ef2aSThomas Huth void helper_pswapd(CPUX86State *env, MMXReg *d, MMXReg *s) 1415fcf5ef2aSThomas Huth { 141625bdec79SPaolo Bonzini uint32_t r; 1417fcf5ef2aSThomas Huth 141825bdec79SPaolo Bonzini r = s->MMX_L(0); 141925bdec79SPaolo Bonzini d->MMX_L(0) = s->MMX_L(1); 142025bdec79SPaolo Bonzini d->MMX_L(1) = r; 1421fcf5ef2aSThomas Huth } 1422fcf5ef2aSThomas Huth #endif 1423fcf5ef2aSThomas Huth 1424fcf5ef2aSThomas Huth /* SSSE3 op helpers */ 1425fcf5ef2aSThomas Huth void glue(helper_pshufb, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1426fcf5ef2aSThomas Huth { 1427d45b0de6SPaul Brook Reg *v = d; 1428fcf5ef2aSThomas Huth int i; 1429d45b0de6SPaul Brook #if SHIFT == 0 1430d45b0de6SPaul Brook uint8_t r[8]; 1431fcf5ef2aSThomas Huth 1432d45b0de6SPaul Brook for (i = 0; i < 8; i++) { 1433d45b0de6SPaul Brook r[i] = (s->B(i) & 0x80) ? 0 : (v->B(s->B(i) & 7)); 1434fcf5ef2aSThomas Huth } 1435d45b0de6SPaul Brook for (i = 0; i < 8; i++) { 1436d45b0de6SPaul Brook d->B(i) = r[i]; 1437fcf5ef2aSThomas Huth } 1438d45b0de6SPaul Brook #else 1439d45b0de6SPaul Brook uint8_t r[8 << SHIFT]; 1440fcf5ef2aSThomas Huth 1441d45b0de6SPaul Brook for (i = 0; i < 8 << SHIFT; i++) { 1442d45b0de6SPaul Brook int j = i & ~0xf; 1443d45b0de6SPaul Brook r[i] = (s->B(i) & 0x80) ? 0 : v->B(j | (s->B(i) & 0xf)); 1444fcf5ef2aSThomas Huth } 1445d45b0de6SPaul Brook for (i = 0; i < 8 << SHIFT; i++) { 1446d45b0de6SPaul Brook d->B(i) = r[i]; 1447fcf5ef2aSThomas Huth } 1448fcf5ef2aSThomas Huth #endif 1449fcf5ef2aSThomas Huth } 1450fcf5ef2aSThomas Huth 1451d45b0de6SPaul Brook #define SSE_HELPER_HW(name, F) \ 1452d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 1453d45b0de6SPaul Brook { \ 1454d45b0de6SPaul Brook Reg *v = d; \ 1455d45b0de6SPaul Brook uint16_t r[4 << SHIFT]; \ 1456d45b0de6SPaul Brook int i, j, k; \ 1457d45b0de6SPaul Brook for (k = 0; k < 4 << SHIFT; k += LANE_WIDTH / 2) { \ 1458d45b0de6SPaul Brook for (i = j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \ 1459d45b0de6SPaul Brook r[i + k] = F(v->W(j + k), v->W(j + k + 1)); \ 1460d45b0de6SPaul Brook } \ 1461d45b0de6SPaul Brook for (j = 0; j < LANE_WIDTH / 2; i++, j += 2) { \ 1462d45b0de6SPaul Brook r[i + k] = F(s->W(j + k), s->W(j + k + 1)); \ 1463d45b0de6SPaul Brook } \ 1464d45b0de6SPaul Brook } \ 1465d45b0de6SPaul Brook for (i = 0; i < 4 << SHIFT; i++) { \ 1466d45b0de6SPaul Brook d->W(i) = r[i]; \ 1467d45b0de6SPaul Brook } \ 1468fcf5ef2aSThomas Huth } 1469fcf5ef2aSThomas Huth 1470d45b0de6SPaul Brook #define SSE_HELPER_HL(name, F) \ 1471d45b0de6SPaul Brook void glue(helper_ ## name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 1472d45b0de6SPaul Brook { \ 1473d45b0de6SPaul Brook Reg *v = d; \ 1474d45b0de6SPaul Brook uint32_t r[2 << SHIFT]; \ 1475d45b0de6SPaul Brook int i, j, k; \ 1476d45b0de6SPaul Brook for (k = 0; k < 2 << SHIFT; k += LANE_WIDTH / 4) { \ 1477d45b0de6SPaul Brook for (i = j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \ 1478d45b0de6SPaul Brook r[i + k] = F(v->L(j + k), v->L(j + k + 1)); \ 1479d45b0de6SPaul Brook } \ 1480d45b0de6SPaul Brook for (j = 0; j < LANE_WIDTH / 4; i++, j += 2) { \ 1481d45b0de6SPaul Brook r[i + k] = F(s->L(j + k), s->L(j + k + 1)); \ 1482d45b0de6SPaul Brook } \ 1483d45b0de6SPaul Brook } \ 1484d45b0de6SPaul Brook for (i = 0; i < 2 << SHIFT; i++) { \ 1485d45b0de6SPaul Brook d->L(i) = r[i]; \ 1486d45b0de6SPaul Brook } \ 1487fcf5ef2aSThomas Huth } 1488fcf5ef2aSThomas Huth 1489d45b0de6SPaul Brook SSE_HELPER_HW(phaddw, FADD) 1490d45b0de6SPaul Brook SSE_HELPER_HW(phsubw, FSUB) 1491d45b0de6SPaul Brook SSE_HELPER_HW(phaddsw, FADDSW) 1492d45b0de6SPaul Brook SSE_HELPER_HW(phsubsw, FSUBSW) 1493d45b0de6SPaul Brook SSE_HELPER_HL(phaddd, FADD) 1494d45b0de6SPaul Brook SSE_HELPER_HL(phsubd, FSUB) 149575046ad7SPaolo Bonzini 1496d45b0de6SPaul Brook #undef SSE_HELPER_HW 1497d45b0de6SPaul Brook #undef SSE_HELPER_HL 1498d45b0de6SPaul Brook 1499d45b0de6SPaul Brook void glue(helper_pmaddubsw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1500d45b0de6SPaul Brook { 1501d45b0de6SPaul Brook Reg *v = d; 1502d45b0de6SPaul Brook int i; 1503d45b0de6SPaul Brook for (i = 0; i < 4 << SHIFT; i++) { 1504d45b0de6SPaul Brook d->W(i) = satsw((int8_t)s->B(i * 2) * (uint8_t)v->B(i * 2) + 1505d45b0de6SPaul Brook (int8_t)s->B(i * 2 + 1) * (uint8_t)v->B(i * 2 + 1)); 1506d45b0de6SPaul Brook } 1507fcf5ef2aSThomas Huth } 1508fcf5ef2aSThomas Huth 1509ee04a3c8SPaul Brook #define FABSB(x) (x > INT8_MAX ? -(int8_t)x : x) 1510ee04a3c8SPaul Brook #define FABSW(x) (x > INT16_MAX ? -(int16_t)x : x) 1511ee04a3c8SPaul Brook #define FABSL(x) (x > INT32_MAX ? -(int32_t)x : x) 1512ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsb, B, 8 << SHIFT, FABSB) 1513ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsw, W, 4 << SHIFT, FABSW) 1514ee04a3c8SPaul Brook SSE_HELPER_1(helper_pabsd, L, 2 << SHIFT, FABSL) 1515fcf5ef2aSThomas Huth 1516fcf5ef2aSThomas Huth #define FMULHRSW(d, s) (((int16_t) d * (int16_t)s + 0x4000) >> 15) 1517fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmulhrsw, FMULHRSW) 1518fcf5ef2aSThomas Huth 1519fcf5ef2aSThomas Huth #define FSIGNB(d, s) (s <= INT8_MAX ? s ? d : 0 : -(int8_t)d) 1520fcf5ef2aSThomas Huth #define FSIGNW(d, s) (s <= INT16_MAX ? s ? d : 0 : -(int16_t)d) 1521fcf5ef2aSThomas Huth #define FSIGNL(d, s) (s <= INT32_MAX ? s ? d : 0 : -(int32_t)d) 1522fcf5ef2aSThomas Huth SSE_HELPER_B(helper_psignb, FSIGNB) 1523fcf5ef2aSThomas Huth SSE_HELPER_W(helper_psignw, FSIGNW) 1524fcf5ef2aSThomas Huth SSE_HELPER_L(helper_psignd, FSIGNL) 1525fcf5ef2aSThomas Huth 1526fcf5ef2aSThomas Huth void glue(helper_palignr, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1527fcf5ef2aSThomas Huth int32_t shift) 1528fcf5ef2aSThomas Huth { 1529d45b0de6SPaul Brook Reg *v = d; 1530d45b0de6SPaul Brook int i; 1531fcf5ef2aSThomas Huth 1532fcf5ef2aSThomas Huth /* XXX could be checked during translation */ 1533d45b0de6SPaul Brook if (shift >= (SHIFT ? 32 : 16)) { 1534d45b0de6SPaul Brook for (i = 0; i < (1 << SHIFT); i++) { 1535d45b0de6SPaul Brook d->Q(i) = 0; 1536d45b0de6SPaul Brook } 1537fcf5ef2aSThomas Huth } else { 1538fcf5ef2aSThomas Huth shift <<= 3; 1539fcf5ef2aSThomas Huth #define SHR(v, i) (i < 64 && i > -64 ? i > 0 ? v >> (i) : (v << -(i)) : 0) 1540fcf5ef2aSThomas Huth #if SHIFT == 0 1541d45b0de6SPaul Brook d->Q(0) = SHR(s->Q(0), shift - 0) | 1542d45b0de6SPaul Brook SHR(v->Q(0), shift - 64); 1543fcf5ef2aSThomas Huth #else 1544d45b0de6SPaul Brook for (i = 0; i < (1 << SHIFT); i += 2) { 1545d45b0de6SPaul Brook uint64_t r0, r1; 1546d45b0de6SPaul Brook 1547d45b0de6SPaul Brook r0 = SHR(s->Q(i), shift - 0) | 1548d45b0de6SPaul Brook SHR(s->Q(i + 1), shift - 64) | 1549d45b0de6SPaul Brook SHR(v->Q(i), shift - 128) | 1550d45b0de6SPaul Brook SHR(v->Q(i + 1), shift - 192); 1551d45b0de6SPaul Brook r1 = SHR(s->Q(i), shift + 64) | 1552d45b0de6SPaul Brook SHR(s->Q(i + 1), shift - 0) | 1553d45b0de6SPaul Brook SHR(v->Q(i), shift - 64) | 1554d45b0de6SPaul Brook SHR(v->Q(i + 1), shift - 128); 1555d45b0de6SPaul Brook d->Q(i) = r0; 1556d45b0de6SPaul Brook d->Q(i + 1) = r1; 1557d45b0de6SPaul Brook } 1558fcf5ef2aSThomas Huth #endif 1559fcf5ef2aSThomas Huth #undef SHR 1560fcf5ef2aSThomas Huth } 1561fcf5ef2aSThomas Huth } 1562fcf5ef2aSThomas Huth 1563fcf5ef2aSThomas Huth #define XMM0 (env->xmm_regs[0]) 1564fcf5ef2aSThomas Huth 1565fcf5ef2aSThomas Huth #if SHIFT == 1 1566fcf5ef2aSThomas Huth #define SSE_HELPER_V(name, elem, num, F) \ 1567fcf5ef2aSThomas Huth void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 1568fcf5ef2aSThomas Huth { \ 1569fcf5ef2aSThomas Huth d->elem(0) = F(d->elem(0), s->elem(0), XMM0.elem(0)); \ 1570fcf5ef2aSThomas Huth d->elem(1) = F(d->elem(1), s->elem(1), XMM0.elem(1)); \ 1571fcf5ef2aSThomas Huth if (num > 2) { \ 1572fcf5ef2aSThomas Huth d->elem(2) = F(d->elem(2), s->elem(2), XMM0.elem(2)); \ 1573fcf5ef2aSThomas Huth d->elem(3) = F(d->elem(3), s->elem(3), XMM0.elem(3)); \ 1574fcf5ef2aSThomas Huth if (num > 4) { \ 1575fcf5ef2aSThomas Huth d->elem(4) = F(d->elem(4), s->elem(4), XMM0.elem(4)); \ 1576fcf5ef2aSThomas Huth d->elem(5) = F(d->elem(5), s->elem(5), XMM0.elem(5)); \ 1577fcf5ef2aSThomas Huth d->elem(6) = F(d->elem(6), s->elem(6), XMM0.elem(6)); \ 1578fcf5ef2aSThomas Huth d->elem(7) = F(d->elem(7), s->elem(7), XMM0.elem(7)); \ 1579fcf5ef2aSThomas Huth if (num > 8) { \ 1580fcf5ef2aSThomas Huth d->elem(8) = F(d->elem(8), s->elem(8), XMM0.elem(8)); \ 1581fcf5ef2aSThomas Huth d->elem(9) = F(d->elem(9), s->elem(9), XMM0.elem(9)); \ 1582fcf5ef2aSThomas Huth d->elem(10) = F(d->elem(10), s->elem(10), XMM0.elem(10)); \ 1583fcf5ef2aSThomas Huth d->elem(11) = F(d->elem(11), s->elem(11), XMM0.elem(11)); \ 1584fcf5ef2aSThomas Huth d->elem(12) = F(d->elem(12), s->elem(12), XMM0.elem(12)); \ 1585fcf5ef2aSThomas Huth d->elem(13) = F(d->elem(13), s->elem(13), XMM0.elem(13)); \ 1586fcf5ef2aSThomas Huth d->elem(14) = F(d->elem(14), s->elem(14), XMM0.elem(14)); \ 1587fcf5ef2aSThomas Huth d->elem(15) = F(d->elem(15), s->elem(15), XMM0.elem(15)); \ 1588fcf5ef2aSThomas Huth } \ 1589fcf5ef2aSThomas Huth } \ 1590fcf5ef2aSThomas Huth } \ 1591fcf5ef2aSThomas Huth } 1592fcf5ef2aSThomas Huth 1593fcf5ef2aSThomas Huth #define SSE_HELPER_I(name, elem, num, F) \ 1594fcf5ef2aSThomas Huth void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t imm) \ 1595fcf5ef2aSThomas Huth { \ 1596fcf5ef2aSThomas Huth d->elem(0) = F(d->elem(0), s->elem(0), ((imm >> 0) & 1)); \ 1597fcf5ef2aSThomas Huth d->elem(1) = F(d->elem(1), s->elem(1), ((imm >> 1) & 1)); \ 1598fcf5ef2aSThomas Huth if (num > 2) { \ 1599fcf5ef2aSThomas Huth d->elem(2) = F(d->elem(2), s->elem(2), ((imm >> 2) & 1)); \ 1600fcf5ef2aSThomas Huth d->elem(3) = F(d->elem(3), s->elem(3), ((imm >> 3) & 1)); \ 1601fcf5ef2aSThomas Huth if (num > 4) { \ 1602fcf5ef2aSThomas Huth d->elem(4) = F(d->elem(4), s->elem(4), ((imm >> 4) & 1)); \ 1603fcf5ef2aSThomas Huth d->elem(5) = F(d->elem(5), s->elem(5), ((imm >> 5) & 1)); \ 1604fcf5ef2aSThomas Huth d->elem(6) = F(d->elem(6), s->elem(6), ((imm >> 6) & 1)); \ 1605fcf5ef2aSThomas Huth d->elem(7) = F(d->elem(7), s->elem(7), ((imm >> 7) & 1)); \ 1606fcf5ef2aSThomas Huth if (num > 8) { \ 1607fcf5ef2aSThomas Huth d->elem(8) = F(d->elem(8), s->elem(8), ((imm >> 8) & 1)); \ 1608fcf5ef2aSThomas Huth d->elem(9) = F(d->elem(9), s->elem(9), ((imm >> 9) & 1)); \ 1609fcf5ef2aSThomas Huth d->elem(10) = F(d->elem(10), s->elem(10), \ 1610fcf5ef2aSThomas Huth ((imm >> 10) & 1)); \ 1611fcf5ef2aSThomas Huth d->elem(11) = F(d->elem(11), s->elem(11), \ 1612fcf5ef2aSThomas Huth ((imm >> 11) & 1)); \ 1613fcf5ef2aSThomas Huth d->elem(12) = F(d->elem(12), s->elem(12), \ 1614fcf5ef2aSThomas Huth ((imm >> 12) & 1)); \ 1615fcf5ef2aSThomas Huth d->elem(13) = F(d->elem(13), s->elem(13), \ 1616fcf5ef2aSThomas Huth ((imm >> 13) & 1)); \ 1617fcf5ef2aSThomas Huth d->elem(14) = F(d->elem(14), s->elem(14), \ 1618fcf5ef2aSThomas Huth ((imm >> 14) & 1)); \ 1619fcf5ef2aSThomas Huth d->elem(15) = F(d->elem(15), s->elem(15), \ 1620fcf5ef2aSThomas Huth ((imm >> 15) & 1)); \ 1621fcf5ef2aSThomas Huth } \ 1622fcf5ef2aSThomas Huth } \ 1623fcf5ef2aSThomas Huth } \ 1624fcf5ef2aSThomas Huth } 1625fcf5ef2aSThomas Huth 1626fcf5ef2aSThomas Huth /* SSE4.1 op helpers */ 1627fcf5ef2aSThomas Huth #define FBLENDVB(d, s, m) ((m & 0x80) ? s : d) 1628fcf5ef2aSThomas Huth #define FBLENDVPS(d, s, m) ((m & 0x80000000) ? s : d) 1629fcf5ef2aSThomas Huth #define FBLENDVPD(d, s, m) ((m & 0x8000000000000000LL) ? s : d) 1630fcf5ef2aSThomas Huth SSE_HELPER_V(helper_pblendvb, B, 16, FBLENDVB) 1631fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvps, L, 4, FBLENDVPS) 1632fcf5ef2aSThomas Huth SSE_HELPER_V(helper_blendvpd, Q, 2, FBLENDVPD) 1633fcf5ef2aSThomas Huth 1634fcf5ef2aSThomas Huth void glue(helper_ptest, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1635fcf5ef2aSThomas Huth { 1636e894bae8SPaul Brook uint64_t zf = 0, cf = 0; 1637e894bae8SPaul Brook int i; 1638fcf5ef2aSThomas Huth 1639e894bae8SPaul Brook for (i = 0; i < 1 << SHIFT; i++) { 1640e894bae8SPaul Brook zf |= (s->Q(i) & d->Q(i)); 1641e894bae8SPaul Brook cf |= (s->Q(i) & ~d->Q(i)); 1642e894bae8SPaul Brook } 1643fcf5ef2aSThomas Huth CC_SRC = (zf ? 0 : CC_Z) | (cf ? 0 : CC_C); 1644fcf5ef2aSThomas Huth } 1645fcf5ef2aSThomas Huth 1646fcf5ef2aSThomas Huth #define SSE_HELPER_F(name, elem, num, F) \ 1647fcf5ef2aSThomas Huth void glue(name, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) \ 1648fcf5ef2aSThomas Huth { \ 1649e894bae8SPaul Brook int n = num; \ 1650e894bae8SPaul Brook for (int i = n; --i >= 0; ) { \ 1651e894bae8SPaul Brook d->elem(i) = F(i); \ 1652fcf5ef2aSThomas Huth } \ 1653fcf5ef2aSThomas Huth } 1654fcf5ef2aSThomas Huth 1655e894bae8SPaul Brook #if SHIFT > 0 1656e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbw, W, 4 << SHIFT, (int8_t) s->B) 1657e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbd, L, 2 << SHIFT, (int8_t) s->B) 1658e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxbq, Q, 1 << SHIFT, (int8_t) s->B) 1659e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwd, L, 2 << SHIFT, (int16_t) s->W) 1660e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxwq, Q, 1 << SHIFT, (int16_t) s->W) 1661e894bae8SPaul Brook SSE_HELPER_F(helper_pmovsxdq, Q, 1 << SHIFT, (int32_t) s->L) 1662e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbw, W, 4 << SHIFT, s->B) 1663e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbd, L, 2 << SHIFT, s->B) 1664e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxbq, Q, 1 << SHIFT, s->B) 1665e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwd, L, 2 << SHIFT, s->W) 1666e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxwq, Q, 1 << SHIFT, s->W) 1667e894bae8SPaul Brook SSE_HELPER_F(helper_pmovzxdq, Q, 1 << SHIFT, s->L) 1668e894bae8SPaul Brook #endif 1669fcf5ef2aSThomas Huth 1670fcf5ef2aSThomas Huth void glue(helper_pmuldq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1671fcf5ef2aSThomas Huth { 1672e894bae8SPaul Brook Reg *v = d; 1673e894bae8SPaul Brook int i; 1674e894bae8SPaul Brook 1675e894bae8SPaul Brook for (i = 0; i < 1 << SHIFT; i++) { 1676e894bae8SPaul Brook d->Q(i) = (int64_t)(int32_t) v->L(2 * i) * (int32_t) s->L(2 * i); 1677e894bae8SPaul Brook } 1678fcf5ef2aSThomas Huth } 1679fcf5ef2aSThomas Huth 1680fcf5ef2aSThomas Huth #define FCMPEQQ(d, s) (d == s ? -1 : 0) 1681fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpeqq, FCMPEQQ) 1682fcf5ef2aSThomas Huth 1683fcf5ef2aSThomas Huth void glue(helper_packusdw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1684fcf5ef2aSThomas Huth { 1685d45b0de6SPaul Brook Reg *v = d; 1686d45b0de6SPaul Brook uint16_t r[8]; 1687d45b0de6SPaul Brook int i, j, k; 168880e19606SJoseph Myers 1689d45b0de6SPaul Brook for (i = 0, j = 0; i <= 2 << SHIFT; i += 8, j += 4) { 1690d45b0de6SPaul Brook r[0] = satuw(v->L(j)); 1691d45b0de6SPaul Brook r[1] = satuw(v->L(j + 1)); 1692d45b0de6SPaul Brook r[2] = satuw(v->L(j + 2)); 1693d45b0de6SPaul Brook r[3] = satuw(v->L(j + 3)); 1694d45b0de6SPaul Brook r[4] = satuw(s->L(j)); 1695d45b0de6SPaul Brook r[5] = satuw(s->L(j + 1)); 1696d45b0de6SPaul Brook r[6] = satuw(s->L(j + 2)); 1697d45b0de6SPaul Brook r[7] = satuw(s->L(j + 3)); 1698d45b0de6SPaul Brook for (k = 0; k < 8; k++) { 1699d45b0de6SPaul Brook d->W(i + k) = r[k]; 1700d45b0de6SPaul Brook } 1701d45b0de6SPaul Brook } 1702fcf5ef2aSThomas Huth } 1703fcf5ef2aSThomas Huth 1704fcf5ef2aSThomas Huth #define FMINSB(d, s) MIN((int8_t)d, (int8_t)s) 1705fcf5ef2aSThomas Huth #define FMINSD(d, s) MIN((int32_t)d, (int32_t)s) 1706fcf5ef2aSThomas Huth #define FMAXSB(d, s) MAX((int8_t)d, (int8_t)s) 1707fcf5ef2aSThomas Huth #define FMAXSD(d, s) MAX((int32_t)d, (int32_t)s) 1708fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pminsb, FMINSB) 1709fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminsd, FMINSD) 1710fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pminuw, MIN) 1711fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pminud, MIN) 1712fcf5ef2aSThomas Huth SSE_HELPER_B(helper_pmaxsb, FMAXSB) 1713fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxsd, FMAXSD) 1714fcf5ef2aSThomas Huth SSE_HELPER_W(helper_pmaxuw, MAX) 1715fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmaxud, MAX) 1716fcf5ef2aSThomas Huth 1717fcf5ef2aSThomas Huth #define FMULLD(d, s) ((int32_t)d * (int32_t)s) 1718fcf5ef2aSThomas Huth SSE_HELPER_L(helper_pmulld, FMULLD) 1719fcf5ef2aSThomas Huth 1720fcf5ef2aSThomas Huth void glue(helper_phminposuw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 1721fcf5ef2aSThomas Huth { 1722fcf5ef2aSThomas Huth int idx = 0; 1723fcf5ef2aSThomas Huth 1724fcf5ef2aSThomas Huth if (s->W(1) < s->W(idx)) { 1725fcf5ef2aSThomas Huth idx = 1; 1726fcf5ef2aSThomas Huth } 1727fcf5ef2aSThomas Huth if (s->W(2) < s->W(idx)) { 1728fcf5ef2aSThomas Huth idx = 2; 1729fcf5ef2aSThomas Huth } 1730fcf5ef2aSThomas Huth if (s->W(3) < s->W(idx)) { 1731fcf5ef2aSThomas Huth idx = 3; 1732fcf5ef2aSThomas Huth } 1733fcf5ef2aSThomas Huth if (s->W(4) < s->W(idx)) { 1734fcf5ef2aSThomas Huth idx = 4; 1735fcf5ef2aSThomas Huth } 1736fcf5ef2aSThomas Huth if (s->W(5) < s->W(idx)) { 1737fcf5ef2aSThomas Huth idx = 5; 1738fcf5ef2aSThomas Huth } 1739fcf5ef2aSThomas Huth if (s->W(6) < s->W(idx)) { 1740fcf5ef2aSThomas Huth idx = 6; 1741fcf5ef2aSThomas Huth } 1742fcf5ef2aSThomas Huth if (s->W(7) < s->W(idx)) { 1743fcf5ef2aSThomas Huth idx = 7; 1744fcf5ef2aSThomas Huth } 1745fcf5ef2aSThomas Huth 1746fcf5ef2aSThomas Huth d->W(0) = s->W(idx); 1747aa406feaSJoseph Myers d->W(1) = idx; 1748aa406feaSJoseph Myers d->L(1) = 0; 1749aa406feaSJoseph Myers d->Q(1) = 0; 1750fcf5ef2aSThomas Huth } 1751fcf5ef2aSThomas Huth 1752fcf5ef2aSThomas Huth void glue(helper_roundps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1753fcf5ef2aSThomas Huth uint32_t mode) 1754fcf5ef2aSThomas Huth { 1755418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 1756fcf5ef2aSThomas Huth signed char prev_rounding_mode; 1757fcf5ef2aSThomas Huth 1758fcf5ef2aSThomas Huth prev_rounding_mode = env->sse_status.float_rounding_mode; 1759fcf5ef2aSThomas Huth if (!(mode & (1 << 2))) { 1760fcf5ef2aSThomas Huth switch (mode & 3) { 1761fcf5ef2aSThomas Huth case 0: 1762fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_nearest_even, &env->sse_status); 1763fcf5ef2aSThomas Huth break; 1764fcf5ef2aSThomas Huth case 1: 1765fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_down, &env->sse_status); 1766fcf5ef2aSThomas Huth break; 1767fcf5ef2aSThomas Huth case 2: 1768fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_up, &env->sse_status); 1769fcf5ef2aSThomas Huth break; 1770fcf5ef2aSThomas Huth case 3: 1771fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_to_zero, &env->sse_status); 1772fcf5ef2aSThomas Huth break; 1773fcf5ef2aSThomas Huth } 1774fcf5ef2aSThomas Huth } 1775fcf5ef2aSThomas Huth 1776fcf5ef2aSThomas Huth d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status); 1777fcf5ef2aSThomas Huth d->ZMM_S(1) = float32_round_to_int(s->ZMM_S(1), &env->sse_status); 1778fcf5ef2aSThomas Huth d->ZMM_S(2) = float32_round_to_int(s->ZMM_S(2), &env->sse_status); 1779fcf5ef2aSThomas Huth d->ZMM_S(3) = float32_round_to_int(s->ZMM_S(3), &env->sse_status); 1780fcf5ef2aSThomas Huth 1781418b0f93SJoseph Myers if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) { 1782fcf5ef2aSThomas Huth set_float_exception_flags(get_float_exception_flags(&env->sse_status) & 1783fcf5ef2aSThomas Huth ~float_flag_inexact, 1784fcf5ef2aSThomas Huth &env->sse_status); 1785fcf5ef2aSThomas Huth } 1786fcf5ef2aSThomas Huth env->sse_status.float_rounding_mode = prev_rounding_mode; 1787fcf5ef2aSThomas Huth } 1788fcf5ef2aSThomas Huth 1789fcf5ef2aSThomas Huth void glue(helper_roundpd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1790fcf5ef2aSThomas Huth uint32_t mode) 1791fcf5ef2aSThomas Huth { 1792418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 1793fcf5ef2aSThomas Huth signed char prev_rounding_mode; 1794fcf5ef2aSThomas Huth 1795fcf5ef2aSThomas Huth prev_rounding_mode = env->sse_status.float_rounding_mode; 1796fcf5ef2aSThomas Huth if (!(mode & (1 << 2))) { 1797fcf5ef2aSThomas Huth switch (mode & 3) { 1798fcf5ef2aSThomas Huth case 0: 1799fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_nearest_even, &env->sse_status); 1800fcf5ef2aSThomas Huth break; 1801fcf5ef2aSThomas Huth case 1: 1802fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_down, &env->sse_status); 1803fcf5ef2aSThomas Huth break; 1804fcf5ef2aSThomas Huth case 2: 1805fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_up, &env->sse_status); 1806fcf5ef2aSThomas Huth break; 1807fcf5ef2aSThomas Huth case 3: 1808fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_to_zero, &env->sse_status); 1809fcf5ef2aSThomas Huth break; 1810fcf5ef2aSThomas Huth } 1811fcf5ef2aSThomas Huth } 1812fcf5ef2aSThomas Huth 1813fcf5ef2aSThomas Huth d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status); 1814fcf5ef2aSThomas Huth d->ZMM_D(1) = float64_round_to_int(s->ZMM_D(1), &env->sse_status); 1815fcf5ef2aSThomas Huth 1816418b0f93SJoseph Myers if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) { 1817fcf5ef2aSThomas Huth set_float_exception_flags(get_float_exception_flags(&env->sse_status) & 1818fcf5ef2aSThomas Huth ~float_flag_inexact, 1819fcf5ef2aSThomas Huth &env->sse_status); 1820fcf5ef2aSThomas Huth } 1821fcf5ef2aSThomas Huth env->sse_status.float_rounding_mode = prev_rounding_mode; 1822fcf5ef2aSThomas Huth } 1823fcf5ef2aSThomas Huth 1824fcf5ef2aSThomas Huth void glue(helper_roundss, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1825fcf5ef2aSThomas Huth uint32_t mode) 1826fcf5ef2aSThomas Huth { 1827418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 1828fcf5ef2aSThomas Huth signed char prev_rounding_mode; 1829fcf5ef2aSThomas Huth 1830fcf5ef2aSThomas Huth prev_rounding_mode = env->sse_status.float_rounding_mode; 1831fcf5ef2aSThomas Huth if (!(mode & (1 << 2))) { 1832fcf5ef2aSThomas Huth switch (mode & 3) { 1833fcf5ef2aSThomas Huth case 0: 1834fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_nearest_even, &env->sse_status); 1835fcf5ef2aSThomas Huth break; 1836fcf5ef2aSThomas Huth case 1: 1837fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_down, &env->sse_status); 1838fcf5ef2aSThomas Huth break; 1839fcf5ef2aSThomas Huth case 2: 1840fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_up, &env->sse_status); 1841fcf5ef2aSThomas Huth break; 1842fcf5ef2aSThomas Huth case 3: 1843fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_to_zero, &env->sse_status); 1844fcf5ef2aSThomas Huth break; 1845fcf5ef2aSThomas Huth } 1846fcf5ef2aSThomas Huth } 1847fcf5ef2aSThomas Huth 1848fcf5ef2aSThomas Huth d->ZMM_S(0) = float32_round_to_int(s->ZMM_S(0), &env->sse_status); 1849fcf5ef2aSThomas Huth 1850418b0f93SJoseph Myers if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) { 1851fcf5ef2aSThomas Huth set_float_exception_flags(get_float_exception_flags(&env->sse_status) & 1852fcf5ef2aSThomas Huth ~float_flag_inexact, 1853fcf5ef2aSThomas Huth &env->sse_status); 1854fcf5ef2aSThomas Huth } 1855fcf5ef2aSThomas Huth env->sse_status.float_rounding_mode = prev_rounding_mode; 1856fcf5ef2aSThomas Huth } 1857fcf5ef2aSThomas Huth 1858fcf5ef2aSThomas Huth void glue(helper_roundsd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1859fcf5ef2aSThomas Huth uint32_t mode) 1860fcf5ef2aSThomas Huth { 1861418b0f93SJoseph Myers uint8_t old_flags = get_float_exception_flags(&env->sse_status); 1862fcf5ef2aSThomas Huth signed char prev_rounding_mode; 1863fcf5ef2aSThomas Huth 1864fcf5ef2aSThomas Huth prev_rounding_mode = env->sse_status.float_rounding_mode; 1865fcf5ef2aSThomas Huth if (!(mode & (1 << 2))) { 1866fcf5ef2aSThomas Huth switch (mode & 3) { 1867fcf5ef2aSThomas Huth case 0: 1868fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_nearest_even, &env->sse_status); 1869fcf5ef2aSThomas Huth break; 1870fcf5ef2aSThomas Huth case 1: 1871fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_down, &env->sse_status); 1872fcf5ef2aSThomas Huth break; 1873fcf5ef2aSThomas Huth case 2: 1874fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_up, &env->sse_status); 1875fcf5ef2aSThomas Huth break; 1876fcf5ef2aSThomas Huth case 3: 1877fcf5ef2aSThomas Huth set_float_rounding_mode(float_round_to_zero, &env->sse_status); 1878fcf5ef2aSThomas Huth break; 1879fcf5ef2aSThomas Huth } 1880fcf5ef2aSThomas Huth } 1881fcf5ef2aSThomas Huth 1882fcf5ef2aSThomas Huth d->ZMM_D(0) = float64_round_to_int(s->ZMM_D(0), &env->sse_status); 1883fcf5ef2aSThomas Huth 1884418b0f93SJoseph Myers if (mode & (1 << 3) && !(old_flags & float_flag_inexact)) { 1885fcf5ef2aSThomas Huth set_float_exception_flags(get_float_exception_flags(&env->sse_status) & 1886fcf5ef2aSThomas Huth ~float_flag_inexact, 1887fcf5ef2aSThomas Huth &env->sse_status); 1888fcf5ef2aSThomas Huth } 1889fcf5ef2aSThomas Huth env->sse_status.float_rounding_mode = prev_rounding_mode; 1890fcf5ef2aSThomas Huth } 1891fcf5ef2aSThomas Huth 1892fcf5ef2aSThomas Huth #define FBLENDP(d, s, m) (m ? s : d) 1893fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendps, L, 4, FBLENDP) 1894fcf5ef2aSThomas Huth SSE_HELPER_I(helper_blendpd, Q, 2, FBLENDP) 1895fcf5ef2aSThomas Huth SSE_HELPER_I(helper_pblendw, W, 8, FBLENDP) 1896fcf5ef2aSThomas Huth 1897fcf5ef2aSThomas Huth void glue(helper_dpps, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask) 1898fcf5ef2aSThomas Huth { 1899bf30ad8cSPaolo Bonzini float32 prod1, prod2, temp2, temp3, temp4; 1900fcf5ef2aSThomas Huth 1901bf30ad8cSPaolo Bonzini /* 1902bf30ad8cSPaolo Bonzini * We must evaluate (A+B)+(C+D), not ((A+B)+C)+D 1903bf30ad8cSPaolo Bonzini * to correctly round the intermediate results 1904bf30ad8cSPaolo Bonzini */ 1905fcf5ef2aSThomas Huth if (mask & (1 << 4)) { 1906bf30ad8cSPaolo Bonzini prod1 = float32_mul(d->ZMM_S(0), s->ZMM_S(0), &env->sse_status); 1907bf30ad8cSPaolo Bonzini } else { 1908bf30ad8cSPaolo Bonzini prod1 = float32_zero; 1909fcf5ef2aSThomas Huth } 1910fcf5ef2aSThomas Huth if (mask & (1 << 5)) { 1911bf30ad8cSPaolo Bonzini prod2 = float32_mul(d->ZMM_S(1), s->ZMM_S(1), &env->sse_status); 1912bf30ad8cSPaolo Bonzini } else { 1913bf30ad8cSPaolo Bonzini prod2 = float32_zero; 1914fcf5ef2aSThomas Huth } 1915bf30ad8cSPaolo Bonzini temp2 = float32_add(prod1, prod2, &env->sse_status); 1916fcf5ef2aSThomas Huth if (mask & (1 << 6)) { 1917bf30ad8cSPaolo Bonzini prod1 = float32_mul(d->ZMM_S(2), s->ZMM_S(2), &env->sse_status); 1918bf30ad8cSPaolo Bonzini } else { 1919bf30ad8cSPaolo Bonzini prod1 = float32_zero; 1920fcf5ef2aSThomas Huth } 1921fcf5ef2aSThomas Huth if (mask & (1 << 7)) { 1922bf30ad8cSPaolo Bonzini prod2 = float32_mul(d->ZMM_S(3), s->ZMM_S(3), &env->sse_status); 1923bf30ad8cSPaolo Bonzini } else { 1924bf30ad8cSPaolo Bonzini prod2 = float32_zero; 1925fcf5ef2aSThomas Huth } 1926bf30ad8cSPaolo Bonzini temp3 = float32_add(prod1, prod2, &env->sse_status); 1927bf30ad8cSPaolo Bonzini temp4 = float32_add(temp2, temp3, &env->sse_status); 1928bf30ad8cSPaolo Bonzini 1929bf30ad8cSPaolo Bonzini d->ZMM_S(0) = (mask & (1 << 0)) ? temp4 : float32_zero; 1930bf30ad8cSPaolo Bonzini d->ZMM_S(1) = (mask & (1 << 1)) ? temp4 : float32_zero; 1931bf30ad8cSPaolo Bonzini d->ZMM_S(2) = (mask & (1 << 2)) ? temp4 : float32_zero; 1932bf30ad8cSPaolo Bonzini d->ZMM_S(3) = (mask & (1 << 3)) ? temp4 : float32_zero; 1933fcf5ef2aSThomas Huth } 1934fcf5ef2aSThomas Huth 1935fcf5ef2aSThomas Huth void glue(helper_dppd, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, uint32_t mask) 1936fcf5ef2aSThomas Huth { 1937bf30ad8cSPaolo Bonzini float64 prod1, prod2, temp2; 1938fcf5ef2aSThomas Huth 1939fcf5ef2aSThomas Huth if (mask & (1 << 4)) { 1940bf30ad8cSPaolo Bonzini prod1 = float64_mul(d->ZMM_D(0), s->ZMM_D(0), &env->sse_status); 1941bf30ad8cSPaolo Bonzini } else { 1942bf30ad8cSPaolo Bonzini prod1 = float64_zero; 1943fcf5ef2aSThomas Huth } 1944fcf5ef2aSThomas Huth if (mask & (1 << 5)) { 1945bf30ad8cSPaolo Bonzini prod2 = float64_mul(d->ZMM_D(1), s->ZMM_D(1), &env->sse_status); 1946bf30ad8cSPaolo Bonzini } else { 1947bf30ad8cSPaolo Bonzini prod2 = float64_zero; 1948fcf5ef2aSThomas Huth } 1949bf30ad8cSPaolo Bonzini temp2 = float64_add(prod1, prod2, &env->sse_status); 1950bf30ad8cSPaolo Bonzini d->ZMM_D(0) = (mask & (1 << 0)) ? temp2 : float64_zero; 1951bf30ad8cSPaolo Bonzini d->ZMM_D(1) = (mask & (1 << 1)) ? temp2 : float64_zero; 1952fcf5ef2aSThomas Huth } 1953fcf5ef2aSThomas Huth 1954fcf5ef2aSThomas Huth void glue(helper_mpsadbw, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 1955fcf5ef2aSThomas Huth uint32_t offset) 1956fcf5ef2aSThomas Huth { 1957d45b0de6SPaul Brook Reg *v = d; 1958d45b0de6SPaul Brook int i, j; 1959d45b0de6SPaul Brook uint16_t r[8]; 1960fcf5ef2aSThomas Huth 1961d45b0de6SPaul Brook for (j = 0; j < 4 << SHIFT; ) { 1962d45b0de6SPaul Brook int s0 = (j * 2) + ((offset & 3) << 2); 1963d45b0de6SPaul Brook int d0 = (j * 2) + ((offset & 4) << 0); 1964d45b0de6SPaul Brook for (i = 0; i < LANE_WIDTH / 2; i++, d0++) { 1965d45b0de6SPaul Brook r[i] = 0; 1966d45b0de6SPaul Brook r[i] += abs1(v->B(d0 + 0) - s->B(s0 + 0)); 1967d45b0de6SPaul Brook r[i] += abs1(v->B(d0 + 1) - s->B(s0 + 1)); 1968d45b0de6SPaul Brook r[i] += abs1(v->B(d0 + 2) - s->B(s0 + 2)); 1969d45b0de6SPaul Brook r[i] += abs1(v->B(d0 + 3) - s->B(s0 + 3)); 1970fcf5ef2aSThomas Huth } 1971d45b0de6SPaul Brook for (i = 0; i < LANE_WIDTH / 2; i++, j++) { 1972d45b0de6SPaul Brook d->W(j) = r[i]; 1973d45b0de6SPaul Brook } 1974d45b0de6SPaul Brook offset >>= 3; 1975d45b0de6SPaul Brook } 1976fcf5ef2aSThomas Huth } 1977fcf5ef2aSThomas Huth 1978fcf5ef2aSThomas Huth /* SSE4.2 op helpers */ 1979fcf5ef2aSThomas Huth #define FCMPGTQ(d, s) ((int64_t)d > (int64_t)s ? -1 : 0) 1980fcf5ef2aSThomas Huth SSE_HELPER_Q(helper_pcmpgtq, FCMPGTQ) 1981fcf5ef2aSThomas Huth 1982fcf5ef2aSThomas Huth static inline int pcmp_elen(CPUX86State *env, int reg, uint32_t ctrl) 1983fcf5ef2aSThomas Huth { 1984d1da229fSPaul Brook target_long val, limit; 1985fcf5ef2aSThomas Huth 1986fcf5ef2aSThomas Huth /* Presence of REX.W is indicated by a bit higher than 7 set */ 1987fcf5ef2aSThomas Huth if (ctrl >> 8) { 1988d1da229fSPaul Brook val = (target_long)env->regs[reg]; 1989fcf5ef2aSThomas Huth } else { 1990d1da229fSPaul Brook val = (int32_t)env->regs[reg]; 1991fcf5ef2aSThomas Huth } 1992fcf5ef2aSThomas Huth if (ctrl & 1) { 1993d1da229fSPaul Brook limit = 8; 1994fcf5ef2aSThomas Huth } else { 1995d1da229fSPaul Brook limit = 16; 1996fcf5ef2aSThomas Huth } 1997d1da229fSPaul Brook if ((val > limit) || (val < -limit)) { 1998d1da229fSPaul Brook return limit; 1999fcf5ef2aSThomas Huth } 2000d1da229fSPaul Brook return abs1(val); 2001fcf5ef2aSThomas Huth } 2002fcf5ef2aSThomas Huth 2003fcf5ef2aSThomas Huth static inline int pcmp_ilen(Reg *r, uint8_t ctrl) 2004fcf5ef2aSThomas Huth { 2005fcf5ef2aSThomas Huth int val = 0; 2006fcf5ef2aSThomas Huth 2007fcf5ef2aSThomas Huth if (ctrl & 1) { 2008fcf5ef2aSThomas Huth while (val < 8 && r->W(val)) { 2009fcf5ef2aSThomas Huth val++; 2010fcf5ef2aSThomas Huth } 2011fcf5ef2aSThomas Huth } else { 2012fcf5ef2aSThomas Huth while (val < 16 && r->B(val)) { 2013fcf5ef2aSThomas Huth val++; 2014fcf5ef2aSThomas Huth } 2015fcf5ef2aSThomas Huth } 2016fcf5ef2aSThomas Huth 2017fcf5ef2aSThomas Huth return val; 2018fcf5ef2aSThomas Huth } 2019fcf5ef2aSThomas Huth 2020fcf5ef2aSThomas Huth static inline int pcmp_val(Reg *r, uint8_t ctrl, int i) 2021fcf5ef2aSThomas Huth { 2022fcf5ef2aSThomas Huth switch ((ctrl >> 0) & 3) { 2023fcf5ef2aSThomas Huth case 0: 2024fcf5ef2aSThomas Huth return r->B(i); 2025fcf5ef2aSThomas Huth case 1: 2026fcf5ef2aSThomas Huth return r->W(i); 2027fcf5ef2aSThomas Huth case 2: 2028fcf5ef2aSThomas Huth return (int8_t)r->B(i); 2029fcf5ef2aSThomas Huth case 3: 2030fcf5ef2aSThomas Huth default: 2031fcf5ef2aSThomas Huth return (int16_t)r->W(i); 2032fcf5ef2aSThomas Huth } 2033fcf5ef2aSThomas Huth } 2034fcf5ef2aSThomas Huth 2035fcf5ef2aSThomas Huth static inline unsigned pcmpxstrx(CPUX86State *env, Reg *d, Reg *s, 2036fcf5ef2aSThomas Huth int8_t ctrl, int valids, int validd) 2037fcf5ef2aSThomas Huth { 2038fcf5ef2aSThomas Huth unsigned int res = 0; 2039fcf5ef2aSThomas Huth int v; 2040fcf5ef2aSThomas Huth int j, i; 2041fcf5ef2aSThomas Huth int upper = (ctrl & 1) ? 7 : 15; 2042fcf5ef2aSThomas Huth 2043fcf5ef2aSThomas Huth valids--; 2044fcf5ef2aSThomas Huth validd--; 2045fcf5ef2aSThomas Huth 2046fcf5ef2aSThomas Huth CC_SRC = (valids < upper ? CC_Z : 0) | (validd < upper ? CC_S : 0); 2047fcf5ef2aSThomas Huth 2048fcf5ef2aSThomas Huth switch ((ctrl >> 2) & 3) { 2049fcf5ef2aSThomas Huth case 0: 2050fcf5ef2aSThomas Huth for (j = valids; j >= 0; j--) { 2051fcf5ef2aSThomas Huth res <<= 1; 2052fcf5ef2aSThomas Huth v = pcmp_val(s, ctrl, j); 2053fcf5ef2aSThomas Huth for (i = validd; i >= 0; i--) { 2054fcf5ef2aSThomas Huth res |= (v == pcmp_val(d, ctrl, i)); 2055fcf5ef2aSThomas Huth } 2056fcf5ef2aSThomas Huth } 2057fcf5ef2aSThomas Huth break; 2058fcf5ef2aSThomas Huth case 1: 2059fcf5ef2aSThomas Huth for (j = valids; j >= 0; j--) { 2060fcf5ef2aSThomas Huth res <<= 1; 2061fcf5ef2aSThomas Huth v = pcmp_val(s, ctrl, j); 2062fcf5ef2aSThomas Huth for (i = ((validd - 1) | 1); i >= 0; i -= 2) { 2063fcf5ef2aSThomas Huth res |= (pcmp_val(d, ctrl, i - 0) >= v && 2064fcf5ef2aSThomas Huth pcmp_val(d, ctrl, i - 1) <= v); 2065fcf5ef2aSThomas Huth } 2066fcf5ef2aSThomas Huth } 2067fcf5ef2aSThomas Huth break; 2068fcf5ef2aSThomas Huth case 2: 2069fcf5ef2aSThomas Huth res = (1 << (upper - MAX(valids, validd))) - 1; 2070fcf5ef2aSThomas Huth res <<= MAX(valids, validd) - MIN(valids, validd); 2071fcf5ef2aSThomas Huth for (i = MIN(valids, validd); i >= 0; i--) { 2072fcf5ef2aSThomas Huth res <<= 1; 2073fcf5ef2aSThomas Huth v = pcmp_val(s, ctrl, i); 2074fcf5ef2aSThomas Huth res |= (v == pcmp_val(d, ctrl, i)); 2075fcf5ef2aSThomas Huth } 2076fcf5ef2aSThomas Huth break; 2077fcf5ef2aSThomas Huth case 3: 2078ae35eea7SJoseph Myers if (validd == -1) { 2079ae35eea7SJoseph Myers res = (2 << upper) - 1; 2080ae35eea7SJoseph Myers break; 2081ae35eea7SJoseph Myers } 2082bc921b27SJoseph Myers for (j = valids == upper ? valids : valids - validd; j >= 0; j--) { 2083fcf5ef2aSThomas Huth res <<= 1; 2084fcf5ef2aSThomas Huth v = 1; 2085bc921b27SJoseph Myers for (i = MIN(valids - j, validd); i >= 0; i--) { 2086fcf5ef2aSThomas Huth v &= (pcmp_val(s, ctrl, i + j) == pcmp_val(d, ctrl, i)); 2087fcf5ef2aSThomas Huth } 2088fcf5ef2aSThomas Huth res |= v; 2089fcf5ef2aSThomas Huth } 2090fcf5ef2aSThomas Huth break; 2091fcf5ef2aSThomas Huth } 2092fcf5ef2aSThomas Huth 2093fcf5ef2aSThomas Huth switch ((ctrl >> 4) & 3) { 2094fcf5ef2aSThomas Huth case 1: 2095fcf5ef2aSThomas Huth res ^= (2 << upper) - 1; 2096fcf5ef2aSThomas Huth break; 2097fcf5ef2aSThomas Huth case 3: 2098fcf5ef2aSThomas Huth res ^= (1 << (valids + 1)) - 1; 2099fcf5ef2aSThomas Huth break; 2100fcf5ef2aSThomas Huth } 2101fcf5ef2aSThomas Huth 2102fcf5ef2aSThomas Huth if (res) { 2103fcf5ef2aSThomas Huth CC_SRC |= CC_C; 2104fcf5ef2aSThomas Huth } 2105fcf5ef2aSThomas Huth if (res & 1) { 2106fcf5ef2aSThomas Huth CC_SRC |= CC_O; 2107fcf5ef2aSThomas Huth } 2108fcf5ef2aSThomas Huth 2109fcf5ef2aSThomas Huth return res; 2110fcf5ef2aSThomas Huth } 2111fcf5ef2aSThomas Huth 2112fcf5ef2aSThomas Huth void glue(helper_pcmpestri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2113fcf5ef2aSThomas Huth uint32_t ctrl) 2114fcf5ef2aSThomas Huth { 2115fcf5ef2aSThomas Huth unsigned int res = pcmpxstrx(env, d, s, ctrl, 2116fcf5ef2aSThomas Huth pcmp_elen(env, R_EDX, ctrl), 2117fcf5ef2aSThomas Huth pcmp_elen(env, R_EAX, ctrl)); 2118fcf5ef2aSThomas Huth 2119fcf5ef2aSThomas Huth if (res) { 2120fcf5ef2aSThomas Huth env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res); 2121fcf5ef2aSThomas Huth } else { 2122fcf5ef2aSThomas Huth env->regs[R_ECX] = 16 >> (ctrl & (1 << 0)); 2123fcf5ef2aSThomas Huth } 2124fcf5ef2aSThomas Huth } 2125fcf5ef2aSThomas Huth 2126fcf5ef2aSThomas Huth void glue(helper_pcmpestrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2127fcf5ef2aSThomas Huth uint32_t ctrl) 2128fcf5ef2aSThomas Huth { 2129fcf5ef2aSThomas Huth int i; 2130fcf5ef2aSThomas Huth unsigned int res = pcmpxstrx(env, d, s, ctrl, 2131fcf5ef2aSThomas Huth pcmp_elen(env, R_EDX, ctrl), 2132fcf5ef2aSThomas Huth pcmp_elen(env, R_EAX, ctrl)); 2133fcf5ef2aSThomas Huth 2134fcf5ef2aSThomas Huth if ((ctrl >> 6) & 1) { 2135fcf5ef2aSThomas Huth if (ctrl & 1) { 2136fcf5ef2aSThomas Huth for (i = 0; i < 8; i++, res >>= 1) { 2137fcf5ef2aSThomas Huth env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0; 2138fcf5ef2aSThomas Huth } 2139fcf5ef2aSThomas Huth } else { 2140fcf5ef2aSThomas Huth for (i = 0; i < 16; i++, res >>= 1) { 2141fcf5ef2aSThomas Huth env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0; 2142fcf5ef2aSThomas Huth } 2143fcf5ef2aSThomas Huth } 2144fcf5ef2aSThomas Huth } else { 2145fcf5ef2aSThomas Huth env->xmm_regs[0].Q(1) = 0; 2146fcf5ef2aSThomas Huth env->xmm_regs[0].Q(0) = res; 2147fcf5ef2aSThomas Huth } 2148fcf5ef2aSThomas Huth } 2149fcf5ef2aSThomas Huth 2150fcf5ef2aSThomas Huth void glue(helper_pcmpistri, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2151fcf5ef2aSThomas Huth uint32_t ctrl) 2152fcf5ef2aSThomas Huth { 2153fcf5ef2aSThomas Huth unsigned int res = pcmpxstrx(env, d, s, ctrl, 2154fcf5ef2aSThomas Huth pcmp_ilen(s, ctrl), 2155fcf5ef2aSThomas Huth pcmp_ilen(d, ctrl)); 2156fcf5ef2aSThomas Huth 2157fcf5ef2aSThomas Huth if (res) { 2158fcf5ef2aSThomas Huth env->regs[R_ECX] = (ctrl & (1 << 6)) ? 31 - clz32(res) : ctz32(res); 2159fcf5ef2aSThomas Huth } else { 2160fcf5ef2aSThomas Huth env->regs[R_ECX] = 16 >> (ctrl & (1 << 0)); 2161fcf5ef2aSThomas Huth } 2162fcf5ef2aSThomas Huth } 2163fcf5ef2aSThomas Huth 2164fcf5ef2aSThomas Huth void glue(helper_pcmpistrm, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2165fcf5ef2aSThomas Huth uint32_t ctrl) 2166fcf5ef2aSThomas Huth { 2167fcf5ef2aSThomas Huth int i; 2168fcf5ef2aSThomas Huth unsigned int res = pcmpxstrx(env, d, s, ctrl, 2169fcf5ef2aSThomas Huth pcmp_ilen(s, ctrl), 2170fcf5ef2aSThomas Huth pcmp_ilen(d, ctrl)); 2171fcf5ef2aSThomas Huth 2172fcf5ef2aSThomas Huth if ((ctrl >> 6) & 1) { 2173fcf5ef2aSThomas Huth if (ctrl & 1) { 2174fcf5ef2aSThomas Huth for (i = 0; i < 8; i++, res >>= 1) { 2175fcf5ef2aSThomas Huth env->xmm_regs[0].W(i) = (res & 1) ? ~0 : 0; 2176fcf5ef2aSThomas Huth } 2177fcf5ef2aSThomas Huth } else { 2178fcf5ef2aSThomas Huth for (i = 0; i < 16; i++, res >>= 1) { 2179fcf5ef2aSThomas Huth env->xmm_regs[0].B(i) = (res & 1) ? ~0 : 0; 2180fcf5ef2aSThomas Huth } 2181fcf5ef2aSThomas Huth } 2182fcf5ef2aSThomas Huth } else { 2183fcf5ef2aSThomas Huth env->xmm_regs[0].Q(1) = 0; 2184fcf5ef2aSThomas Huth env->xmm_regs[0].Q(0) = res; 2185fcf5ef2aSThomas Huth } 2186fcf5ef2aSThomas Huth } 2187fcf5ef2aSThomas Huth 2188fcf5ef2aSThomas Huth #define CRCPOLY 0x1edc6f41 2189fcf5ef2aSThomas Huth #define CRCPOLY_BITREV 0x82f63b78 2190fcf5ef2aSThomas Huth target_ulong helper_crc32(uint32_t crc1, target_ulong msg, uint32_t len) 2191fcf5ef2aSThomas Huth { 2192fcf5ef2aSThomas Huth target_ulong crc = (msg & ((target_ulong) -1 >> 2193fcf5ef2aSThomas Huth (TARGET_LONG_BITS - len))) ^ crc1; 2194fcf5ef2aSThomas Huth 2195fcf5ef2aSThomas Huth while (len--) { 2196fcf5ef2aSThomas Huth crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_BITREV : 0); 2197fcf5ef2aSThomas Huth } 2198fcf5ef2aSThomas Huth 2199fcf5ef2aSThomas Huth return crc; 2200fcf5ef2aSThomas Huth } 2201fcf5ef2aSThomas Huth 2202fcf5ef2aSThomas Huth void glue(helper_pclmulqdq, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2203fcf5ef2aSThomas Huth uint32_t ctrl) 2204fcf5ef2aSThomas Huth { 2205fcf5ef2aSThomas Huth uint64_t ah, al, b, resh, resl; 2206fcf5ef2aSThomas Huth 2207fcf5ef2aSThomas Huth ah = 0; 2208fcf5ef2aSThomas Huth al = d->Q((ctrl & 1) != 0); 2209fcf5ef2aSThomas Huth b = s->Q((ctrl & 16) != 0); 2210fcf5ef2aSThomas Huth resh = resl = 0; 2211fcf5ef2aSThomas Huth 2212fcf5ef2aSThomas Huth while (b) { 2213fcf5ef2aSThomas Huth if (b & 1) { 2214fcf5ef2aSThomas Huth resl ^= al; 2215fcf5ef2aSThomas Huth resh ^= ah; 2216fcf5ef2aSThomas Huth } 2217fcf5ef2aSThomas Huth ah = (ah << 1) | (al >> 63); 2218fcf5ef2aSThomas Huth al <<= 1; 2219fcf5ef2aSThomas Huth b >>= 1; 2220fcf5ef2aSThomas Huth } 2221fcf5ef2aSThomas Huth 2222fcf5ef2aSThomas Huth d->Q(0) = resl; 2223fcf5ef2aSThomas Huth d->Q(1) = resh; 2224fcf5ef2aSThomas Huth } 2225fcf5ef2aSThomas Huth 2226fcf5ef2aSThomas Huth void glue(helper_aesdec, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 2227fcf5ef2aSThomas Huth { 2228fcf5ef2aSThomas Huth int i; 2229fcf5ef2aSThomas Huth Reg st = *d; 2230fcf5ef2aSThomas Huth Reg rk = *s; 2231fcf5ef2aSThomas Huth 2232fcf5ef2aSThomas Huth for (i = 0 ; i < 4 ; i++) { 2233fcf5ef2aSThomas Huth d->L(i) = rk.L(i) ^ bswap32(AES_Td0[st.B(AES_ishifts[4*i+0])] ^ 2234fcf5ef2aSThomas Huth AES_Td1[st.B(AES_ishifts[4*i+1])] ^ 2235fcf5ef2aSThomas Huth AES_Td2[st.B(AES_ishifts[4*i+2])] ^ 2236fcf5ef2aSThomas Huth AES_Td3[st.B(AES_ishifts[4*i+3])]); 2237fcf5ef2aSThomas Huth } 2238fcf5ef2aSThomas Huth } 2239fcf5ef2aSThomas Huth 2240fcf5ef2aSThomas Huth void glue(helper_aesdeclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 2241fcf5ef2aSThomas Huth { 2242fcf5ef2aSThomas Huth int i; 2243fcf5ef2aSThomas Huth Reg st = *d; 2244fcf5ef2aSThomas Huth Reg rk = *s; 2245fcf5ef2aSThomas Huth 2246fcf5ef2aSThomas Huth for (i = 0; i < 16; i++) { 2247fcf5ef2aSThomas Huth d->B(i) = rk.B(i) ^ (AES_isbox[st.B(AES_ishifts[i])]); 2248fcf5ef2aSThomas Huth } 2249fcf5ef2aSThomas Huth } 2250fcf5ef2aSThomas Huth 2251fcf5ef2aSThomas Huth void glue(helper_aesenc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 2252fcf5ef2aSThomas Huth { 2253fcf5ef2aSThomas Huth int i; 2254fcf5ef2aSThomas Huth Reg st = *d; 2255fcf5ef2aSThomas Huth Reg rk = *s; 2256fcf5ef2aSThomas Huth 2257fcf5ef2aSThomas Huth for (i = 0 ; i < 4 ; i++) { 2258fcf5ef2aSThomas Huth d->L(i) = rk.L(i) ^ bswap32(AES_Te0[st.B(AES_shifts[4*i+0])] ^ 2259fcf5ef2aSThomas Huth AES_Te1[st.B(AES_shifts[4*i+1])] ^ 2260fcf5ef2aSThomas Huth AES_Te2[st.B(AES_shifts[4*i+2])] ^ 2261fcf5ef2aSThomas Huth AES_Te3[st.B(AES_shifts[4*i+3])]); 2262fcf5ef2aSThomas Huth } 2263fcf5ef2aSThomas Huth } 2264fcf5ef2aSThomas Huth 2265fcf5ef2aSThomas Huth void glue(helper_aesenclast, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 2266fcf5ef2aSThomas Huth { 2267fcf5ef2aSThomas Huth int i; 2268fcf5ef2aSThomas Huth Reg st = *d; 2269fcf5ef2aSThomas Huth Reg rk = *s; 2270fcf5ef2aSThomas Huth 2271fcf5ef2aSThomas Huth for (i = 0; i < 16; i++) { 2272fcf5ef2aSThomas Huth d->B(i) = rk.B(i) ^ (AES_sbox[st.B(AES_shifts[i])]); 2273fcf5ef2aSThomas Huth } 2274fcf5ef2aSThomas Huth 2275fcf5ef2aSThomas Huth } 2276fcf5ef2aSThomas Huth 2277fcf5ef2aSThomas Huth void glue(helper_aesimc, SUFFIX)(CPUX86State *env, Reg *d, Reg *s) 2278fcf5ef2aSThomas Huth { 2279fcf5ef2aSThomas Huth int i; 2280fcf5ef2aSThomas Huth Reg tmp = *s; 2281fcf5ef2aSThomas Huth 2282fcf5ef2aSThomas Huth for (i = 0 ; i < 4 ; i++) { 2283fcf5ef2aSThomas Huth d->L(i) = bswap32(AES_imc[tmp.B(4*i+0)][0] ^ 2284fcf5ef2aSThomas Huth AES_imc[tmp.B(4*i+1)][1] ^ 2285fcf5ef2aSThomas Huth AES_imc[tmp.B(4*i+2)][2] ^ 2286fcf5ef2aSThomas Huth AES_imc[tmp.B(4*i+3)][3]); 2287fcf5ef2aSThomas Huth } 2288fcf5ef2aSThomas Huth } 2289fcf5ef2aSThomas Huth 2290fcf5ef2aSThomas Huth void glue(helper_aeskeygenassist, SUFFIX)(CPUX86State *env, Reg *d, Reg *s, 2291fcf5ef2aSThomas Huth uint32_t ctrl) 2292fcf5ef2aSThomas Huth { 2293fcf5ef2aSThomas Huth int i; 2294fcf5ef2aSThomas Huth Reg tmp = *s; 2295fcf5ef2aSThomas Huth 2296fcf5ef2aSThomas Huth for (i = 0 ; i < 4 ; i++) { 2297fcf5ef2aSThomas Huth d->B(i) = AES_sbox[tmp.B(i + 4)]; 2298fcf5ef2aSThomas Huth d->B(i + 8) = AES_sbox[tmp.B(i + 12)]; 2299fcf5ef2aSThomas Huth } 2300fcf5ef2aSThomas Huth d->L(1) = (d->L(0) << 24 | d->L(0) >> 8) ^ ctrl; 2301fcf5ef2aSThomas Huth d->L(3) = (d->L(2) << 24 | d->L(2) >> 8) ^ ctrl; 2302fcf5ef2aSThomas Huth } 2303fcf5ef2aSThomas Huth #endif 2304fcf5ef2aSThomas Huth 2305*3403cafeSPaul Brook #undef SSE_HELPER_S 2306*3403cafeSPaul Brook 2307fcf5ef2aSThomas Huth #undef SHIFT 2308fcf5ef2aSThomas Huth #undef XMM_ONLY 2309fcf5ef2aSThomas Huth #undef Reg 2310fcf5ef2aSThomas Huth #undef B 2311fcf5ef2aSThomas Huth #undef W 2312fcf5ef2aSThomas Huth #undef L 2313fcf5ef2aSThomas Huth #undef Q 2314fcf5ef2aSThomas Huth #undef SUFFIX 2315d22697ddSPaolo Bonzini #undef SIZE 2316