michael@0: #if !defined(_x86_mmxloop_H) michael@0: # define _x86_mmxloop_H (1) michael@0: # include michael@0: # include "x86int.h" michael@0: michael@0: #if defined(OC_X86_ASM) michael@0: michael@0: /*On entry, mm0={a0,...,a7}, mm1={b0,...,b7}, mm2={c0,...,c7}, mm3={d0,...d7}. michael@0: On exit, mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)} and michael@0: mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}; mm0 and mm3 are clobbered.*/ michael@0: #define OC_LOOP_FILTER8_MMX \ michael@0: "#OC_LOOP_FILTER8_MMX\n\t" \ michael@0: /*mm7=0*/ \ michael@0: "pxor %%mm7,%%mm7\n\t" \ michael@0: /*mm6:mm0={a0,...,a7}*/ \ michael@0: "movq %%mm0,%%mm6\n\t" \ michael@0: "punpcklbw %%mm7,%%mm0\n\t" \ michael@0: "punpckhbw %%mm7,%%mm6\n\t" \ michael@0: /*mm3:mm5={d0,...,d7}*/ \ michael@0: "movq %%mm3,%%mm5\n\t" \ michael@0: "punpcklbw %%mm7,%%mm3\n\t" \ michael@0: "punpckhbw %%mm7,%%mm5\n\t" \ michael@0: /*mm6:mm0={a0-d0,...,a7-d7}*/ \ michael@0: "psubw %%mm3,%%mm0\n\t" \ michael@0: "psubw %%mm5,%%mm6\n\t" \ michael@0: /*mm3:mm1={b0,...,b7}*/ \ michael@0: "movq %%mm1,%%mm3\n\t" \ michael@0: "punpcklbw %%mm7,%%mm1\n\t" \ michael@0: "movq %%mm2,%%mm4\n\t" \ michael@0: "punpckhbw %%mm7,%%mm3\n\t" \ michael@0: /*mm5:mm4={c0,...,c7}*/ \ michael@0: "movq %%mm2,%%mm5\n\t" \ michael@0: "punpcklbw %%mm7,%%mm4\n\t" \ michael@0: "punpckhbw %%mm7,%%mm5\n\t" \ michael@0: /*mm7={3}x4 \ michael@0: mm5:mm4={c0-b0,...,c7-b7}*/ \ michael@0: "pcmpeqw %%mm7,%%mm7\n\t" \ michael@0: "psubw %%mm1,%%mm4\n\t" \ michael@0: "psrlw $14,%%mm7\n\t" \ michael@0: "psubw %%mm3,%%mm5\n\t" \ michael@0: /*Scale by 3.*/ \ michael@0: "pmullw %%mm7,%%mm4\n\t" \ michael@0: "pmullw %%mm7,%%mm5\n\t" \ michael@0: /*mm7={4}x4 \ michael@0: mm5:mm4=f={a0-d0+3*(c0-b0),...,a7-d7+3*(c7-b7)}*/ \ michael@0: "psrlw $1,%%mm7\n\t" \ michael@0: "paddw %%mm0,%%mm4\n\t" \ michael@0: "psllw $2,%%mm7\n\t" \ michael@0: "movq (%[ll]),%%mm0\n\t" \ michael@0: "paddw %%mm6,%%mm5\n\t" \ michael@0: /*R_i has the range [-127,128], so we compute -R_i instead. \ michael@0: mm4=-R_i=-(f+4>>3)=0xFF^(f-4>>3)*/ \ michael@0: "psubw %%mm7,%%mm4\n\t" \ michael@0: "psubw %%mm7,%%mm5\n\t" \ michael@0: "psraw $3,%%mm4\n\t" \ michael@0: "psraw $3,%%mm5\n\t" \ michael@0: "pcmpeqb %%mm7,%%mm7\n\t" \ michael@0: "packsswb %%mm5,%%mm4\n\t" \ michael@0: "pxor %%mm6,%%mm6\n\t" \ michael@0: "pxor %%mm7,%%mm4\n\t" \ michael@0: "packuswb %%mm3,%%mm1\n\t" \ michael@0: /*Now compute lflim of -mm4 cf. Section 7.10 of the sepc.*/ \ michael@0: /*There's no unsigned byte+signed byte with unsigned saturation op code, so \ michael@0: we have to split things by sign (the other option is to work in 16 bits, \ michael@0: but working in 8 bits gives much better parallelism). \ michael@0: We compute abs(R_i), but save a mask of which terms were negative in mm6. \ michael@0: Then we compute mm4=abs(lflim(R_i,L))=min(abs(R_i),max(2*L-abs(R_i),0)). \ michael@0: Finally, we split mm4 into positive and negative pieces using the mask in \ michael@0: mm6, and add and subtract them as appropriate.*/ \ michael@0: /*mm4=abs(-R_i)*/ \ michael@0: /*mm7=255-2*L*/ \ michael@0: "pcmpgtb %%mm4,%%mm6\n\t" \ michael@0: "psubb %%mm0,%%mm7\n\t" \ michael@0: "pxor %%mm6,%%mm4\n\t" \ michael@0: "psubb %%mm0,%%mm7\n\t" \ michael@0: "psubb %%mm6,%%mm4\n\t" \ michael@0: /*mm7=255-max(2*L-abs(R_i),0)*/ \ michael@0: "paddusb %%mm4,%%mm7\n\t" \ michael@0: /*mm4=min(abs(R_i),max(2*L-abs(R_i),0))*/ \ michael@0: "paddusb %%mm7,%%mm4\n\t" \ michael@0: "psubusb %%mm7,%%mm4\n\t" \ michael@0: /*Now split mm4 by the original sign of -R_i.*/ \ michael@0: "movq %%mm4,%%mm5\n\t" \ michael@0: "pand %%mm6,%%mm4\n\t" \ michael@0: "pandn %%mm5,%%mm6\n\t" \ michael@0: /*mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)}*/ \ michael@0: /*mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}*/ \ michael@0: "paddusb %%mm4,%%mm1\n\t" \ michael@0: "psubusb %%mm4,%%mm2\n\t" \ michael@0: "psubusb %%mm6,%%mm1\n\t" \ michael@0: "paddusb %%mm6,%%mm2\n\t" \ michael@0: michael@0: /*On entry, mm0={a0,...,a7}, mm1={b0,...,b7}, mm2={c0,...,c7}, mm3={d0,...d7}. michael@0: On exit, mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)} and michael@0: mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}. michael@0: All other MMX registers are clobbered.*/ michael@0: #define OC_LOOP_FILTER8_MMXEXT \ michael@0: "#OC_LOOP_FILTER8_MMXEXT\n\t" \ michael@0: /*R_i=(a_i-3*b_i+3*c_i-d_i+4>>3) has the range [-127,128], so we compute \ michael@0: -R_i=(-a_i+3*b_i-3*c_i+d_i+3>>3) instead.*/ \ michael@0: /*This first part is based on the transformation \ michael@0: f = -(3*(c-b)+a-d+4>>3) \ michael@0: = -(3*(c+255-b)+(a+255-d)+4-1020>>3) \ michael@0: = -(3*(c+~b)+(a+~d)-1016>>3) \ michael@0: = 127-(3*(c+~b)+(a+~d)>>3) \ michael@0: = 128+~(3*(c+~b)+(a+~d)>>3) (mod 256). \ michael@0: Although pavgb(a,b) = (a+b+1>>1) (biased up), we rely heavily on the \ michael@0: fact that ~pavgb(~a,~b) = (a+b>>1) (biased down). \ michael@0: Using this, the last expression above can be computed in 8 bits of working \ michael@0: precision via: \ michael@0: u = ~pavgb(~b,c); \ michael@0: v = pavgb(b,~c); \ michael@0: This mask is 0 or 0xFF, and controls whether t is biased up or down: \ michael@0: m = u-v; \ michael@0: t = m^pavgb(m^~a,m^d); \ michael@0: f = 128+pavgb(pavgb(t,u),v); \ michael@0: This required some careful analysis to ensure that carries are propagated \ michael@0: correctly in all cases, but has been checked exhaustively.*/ \ michael@0: /*input (a, b, c, d, ., ., ., .)*/ \ michael@0: /*ff=0xFF; \ michael@0: u=b; \ michael@0: v=c; \ michael@0: ll=255-2*L;*/ \ michael@0: "pcmpeqb %%mm7,%%mm7\n\t" \ michael@0: "movq %%mm1,%%mm4\n\t" \ michael@0: "movq %%mm2,%%mm5\n\t" \ michael@0: "movq (%[ll]),%%mm6\n\t" \ michael@0: /*allocated u, v, ll, ff: (a, b, c, d, u, v, ll, ff)*/ \ michael@0: /*u^=ff; \ michael@0: v^=ff;*/ \ michael@0: "pxor %%mm7,%%mm4\n\t" \ michael@0: "pxor %%mm7,%%mm5\n\t" \ michael@0: /*allocated ll: (a, b, c, d, u, v, ll, ff)*/ \ michael@0: /*u=pavgb(u,c); \ michael@0: v=pavgb(v,b);*/ \ michael@0: "pavgb %%mm2,%%mm4\n\t" \ michael@0: "pavgb %%mm1,%%mm5\n\t" \ michael@0: /*u^=ff; \ michael@0: a^=ff;*/ \ michael@0: "pxor %%mm7,%%mm4\n\t" \ michael@0: "pxor %%mm7,%%mm0\n\t" \ michael@0: /*m=u-v;*/ \ michael@0: "psubb %%mm5,%%mm4\n\t" \ michael@0: /*freed u, allocated m: (a, b, c, d, m, v, ll, ff)*/ \ michael@0: /*a^=m; \ michael@0: d^=m;*/ \ michael@0: "pxor %%mm4,%%mm0\n\t" \ michael@0: "pxor %%mm4,%%mm3\n\t" \ michael@0: /*t=pavgb(a,d);*/ \ michael@0: "pavgb %%mm3,%%mm0\n\t" \ michael@0: "psllw $7,%%mm7\n\t" \ michael@0: /*freed a, d, ff, allocated t, of: (t, b, c, ., m, v, ll, of)*/ \ michael@0: /*t^=m; \ michael@0: u=m+v;*/ \ michael@0: "pxor %%mm4,%%mm0\n\t" \ michael@0: "paddb %%mm5,%%mm4\n\t" \ michael@0: /*freed t, m, allocated f, u: (f, b, c, ., u, v, ll, of)*/ \ michael@0: /*f=pavgb(f,u); \ michael@0: of=128;*/ \ michael@0: "pavgb %%mm4,%%mm0\n\t" \ michael@0: "packsswb %%mm7,%%mm7\n\t" \ michael@0: /*freed u, ff, allocated ll: (f, b, c, ., ll, v, ll, of)*/ \ michael@0: /*f=pavgb(f,v);*/ \ michael@0: "pavgb %%mm5,%%mm0\n\t" \ michael@0: "movq %%mm7,%%mm3\n\t" \ michael@0: "movq %%mm6,%%mm4\n\t" \ michael@0: /*freed v, allocated of: (f, b, c, of, ll, ., ll, of)*/ \ michael@0: /*Now compute lflim of R_i=-(128+mm0) cf. Section 7.10 of the sepc.*/ \ michael@0: /*There's no unsigned byte+signed byte with unsigned saturation op code, so \ michael@0: we have to split things by sign (the other option is to work in 16 bits, \ michael@0: but staying in 8 bits gives much better parallelism).*/ \ michael@0: /*Instead of adding the offset of 128 in mm3, we use it to split mm0. \ michael@0: This is the same number of instructions as computing a mask and splitting \ michael@0: after the lflim computation, but has shorter dependency chains.*/ \ michael@0: /*mm0=R_i<0?-R_i:0 (denoted abs(R_i<0))\ michael@0: mm3=R_i>0?R_i:0* (denoted abs(R_i>0))*/ \ michael@0: "psubusb %%mm0,%%mm3\n\t" \ michael@0: "psubusb %%mm7,%%mm0\n\t" \ michael@0: /*mm6=255-max(2*L-abs(R_i<0),0) \ michael@0: mm4=255-max(2*L-abs(R_i>0),0)*/ \ michael@0: "paddusb %%mm3,%%mm4\n\t" \ michael@0: "paddusb %%mm0,%%mm6\n\t" \ michael@0: /*mm0=min(abs(R_i<0),max(2*L-abs(R_i<0),0)) \ michael@0: mm3=min(abs(R_i>0),max(2*L-abs(R_i>0),0))*/ \ michael@0: "paddusb %%mm4,%%mm3\n\t" \ michael@0: "paddusb %%mm6,%%mm0\n\t" \ michael@0: "psubusb %%mm4,%%mm3\n\t" \ michael@0: "psubusb %%mm6,%%mm0\n\t" \ michael@0: /*mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)}*/ \ michael@0: /*mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}*/ \ michael@0: "paddusb %%mm3,%%mm1\n\t" \ michael@0: "psubusb %%mm3,%%mm2\n\t" \ michael@0: "psubusb %%mm0,%%mm1\n\t" \ michael@0: "paddusb %%mm0,%%mm2\n\t" \ michael@0: michael@0: #define OC_LOOP_FILTER_V(_filter,_pix,_ystride,_ll) \ michael@0: do{ \ michael@0: ptrdiff_t ystride3__; \ michael@0: __asm__ __volatile__( \ michael@0: /*mm0={a0,...,a7}*/ \ michael@0: "movq (%[pix]),%%mm0\n\t" \ michael@0: /*ystride3=_ystride*3*/ \ michael@0: "lea (%[ystride],%[ystride],2),%[ystride3]\n\t" \ michael@0: /*mm3={d0,...,d7}*/ \ michael@0: "movq (%[pix],%[ystride3]),%%mm3\n\t" \ michael@0: /*mm1={b0,...,b7}*/ \ michael@0: "movq (%[pix],%[ystride]),%%mm1\n\t" \ michael@0: /*mm2={c0,...,c7}*/ \ michael@0: "movq (%[pix],%[ystride],2),%%mm2\n\t" \ michael@0: _filter \ michael@0: /*Write it back out.*/ \ michael@0: "movq %%mm1,(%[pix],%[ystride])\n\t" \ michael@0: "movq %%mm2,(%[pix],%[ystride],2)\n\t" \ michael@0: :[ystride3]"=&r"(ystride3__) \ michael@0: :[pix]"r"(_pix-_ystride*2),[ystride]"r"((ptrdiff_t)(_ystride)), \ michael@0: [ll]"r"(_ll) \ michael@0: :"memory" \ michael@0: ); \ michael@0: } \ michael@0: while(0) michael@0: michael@0: #define OC_LOOP_FILTER_H(_filter,_pix,_ystride,_ll) \ michael@0: do{ \ michael@0: unsigned char *pix__; \ michael@0: ptrdiff_t ystride3__; \ michael@0: ptrdiff_t d__; \ michael@0: pix__=(_pix)-2; \ michael@0: __asm__ __volatile__( \ michael@0: /*x x x x d0 c0 b0 a0*/ \ michael@0: "movd (%[pix]),%%mm0\n\t" \ michael@0: /*x x x x d1 c1 b1 a1*/ \ michael@0: "movd (%[pix],%[ystride]),%%mm1\n\t" \ michael@0: /*ystride3=_ystride*3*/ \ michael@0: "lea (%[ystride],%[ystride],2),%[ystride3]\n\t" \ michael@0: /*x x x x d2 c2 b2 a2*/ \ michael@0: "movd (%[pix],%[ystride],2),%%mm2\n\t" \ michael@0: /*x x x x d3 c3 b3 a3*/ \ michael@0: "lea (%[pix],%[ystride],4),%[d]\n\t" \ michael@0: "movd (%[pix],%[ystride3]),%%mm3\n\t" \ michael@0: /*x x x x d4 c4 b4 a4*/ \ michael@0: "movd (%[d]),%%mm4\n\t" \ michael@0: /*x x x x d5 c5 b5 a5*/ \ michael@0: "movd (%[d],%[ystride]),%%mm5\n\t" \ michael@0: /*x x x x d6 c6 b6 a6*/ \ michael@0: "movd (%[d],%[ystride],2),%%mm6\n\t" \ michael@0: /*x x x x d7 c7 b7 a7*/ \ michael@0: "movd (%[d],%[ystride3]),%%mm7\n\t" \ michael@0: /*mm0=d1 d0 c1 c0 b1 b0 a1 a0*/ \ michael@0: "punpcklbw %%mm1,%%mm0\n\t" \ michael@0: /*mm2=d3 d2 c3 c2 b3 b2 a3 a2*/ \ michael@0: "punpcklbw %%mm3,%%mm2\n\t" \ michael@0: /*mm3=d1 d0 c1 c0 b1 b0 a1 a0*/ \ michael@0: "movq %%mm0,%%mm3\n\t" \ michael@0: /*mm0=b3 b2 b1 b0 a3 a2 a1 a0*/ \ michael@0: "punpcklwd %%mm2,%%mm0\n\t" \ michael@0: /*mm3=d3 d2 d1 d0 c3 c2 c1 c0*/ \ michael@0: "punpckhwd %%mm2,%%mm3\n\t" \ michael@0: /*mm1=b3 b2 b1 b0 a3 a2 a1 a0*/ \ michael@0: "movq %%mm0,%%mm1\n\t" \ michael@0: /*mm4=d5 d4 c5 c4 b5 b4 a5 a4*/ \ michael@0: "punpcklbw %%mm5,%%mm4\n\t" \ michael@0: /*mm6=d7 d6 c7 c6 b7 b6 a7 a6*/ \ michael@0: "punpcklbw %%mm7,%%mm6\n\t" \ michael@0: /*mm5=d5 d4 c5 c4 b5 b4 a5 a4*/ \ michael@0: "movq %%mm4,%%mm5\n\t" \ michael@0: /*mm4=b7 b6 b5 b4 a7 a6 a5 a4*/ \ michael@0: "punpcklwd %%mm6,%%mm4\n\t" \ michael@0: /*mm5=d7 d6 d5 d4 c7 c6 c5 c4*/ \ michael@0: "punpckhwd %%mm6,%%mm5\n\t" \ michael@0: /*mm2=d3 d2 d1 d0 c3 c2 c1 c0*/ \ michael@0: "movq %%mm3,%%mm2\n\t" \ michael@0: /*mm0=a7 a6 a5 a4 a3 a2 a1 a0*/ \ michael@0: "punpckldq %%mm4,%%mm0\n\t" \ michael@0: /*mm1=b7 b6 b5 b4 b3 b2 b1 b0*/ \ michael@0: "punpckhdq %%mm4,%%mm1\n\t" \ michael@0: /*mm2=c7 c6 c5 c4 c3 c2 c1 c0*/ \ michael@0: "punpckldq %%mm5,%%mm2\n\t" \ michael@0: /*mm3=d7 d6 d5 d4 d3 d2 d1 d0*/ \ michael@0: "punpckhdq %%mm5,%%mm3\n\t" \ michael@0: _filter \ michael@0: /*mm2={b0+R_0'',...,b7+R_7''}*/ \ michael@0: "movq %%mm1,%%mm0\n\t" \ michael@0: /*mm1={b0+R_0'',c0-R_0'',...,b3+R_3'',c3-R_3''}*/ \ michael@0: "punpcklbw %%mm2,%%mm1\n\t" \ michael@0: /*mm2={b4+R_4'',c4-R_4'',...,b7+R_7'',c7-R_7''}*/ \ michael@0: "punpckhbw %%mm2,%%mm0\n\t" \ michael@0: /*[d]=c1 b1 c0 b0*/ \ michael@0: "movd %%mm1,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix])\n\t" \ michael@0: "psrlq $32,%%mm1\n\t" \ michael@0: "shr $16,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride])\n\t" \ michael@0: /*[d]=c3 b3 c2 b2*/ \ michael@0: "movd %%mm1,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride],2)\n\t" \ michael@0: "shr $16,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride3])\n\t" \ michael@0: "lea (%[pix],%[ystride],4),%[pix]\n\t" \ michael@0: /*[d]=c5 b5 c4 b4*/ \ michael@0: "movd %%mm0,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix])\n\t" \ michael@0: "psrlq $32,%%mm0\n\t" \ michael@0: "shr $16,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride])\n\t" \ michael@0: /*[d]=c7 b7 c6 b6*/ \ michael@0: "movd %%mm0,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride],2)\n\t" \ michael@0: "shr $16,%[d]\n\t" \ michael@0: "movw %w[d],1(%[pix],%[ystride3])\n\t" \ michael@0: :[pix]"+r"(pix__),[ystride3]"=&r"(ystride3__),[d]"=&r"(d__) \ michael@0: :[ystride]"r"((ptrdiff_t)(_ystride)),[ll]"r"(_ll) \ michael@0: :"memory" \ michael@0: ); \ michael@0: } \ michael@0: while(0) michael@0: michael@0: # endif michael@0: #endif