#if !defined(_x86_mmxloop_H)1# define _x86_mmxloop_H (1)2# include <stddef.h>3# include "x86int.h"45#if defined(OC_X86_ASM)67/*On entry, mm0={a0,...,a7}, mm1={b0,...,b7}, mm2={c0,...,c7}, mm3={d0,...d7}.8On exit, mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)} and9mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}; mm0 and mm3 are clobbered.*/10#define OC_LOOP_FILTER8_MMX \11"#OC_LOOP_FILTER8_MMX\n\t" \12/*mm7=0*/ \13"pxor %%mm7,%%mm7\n\t" \14/*mm6:mm0={a0,...,a7}*/ \15"movq %%mm0,%%mm6\n\t" \16"punpcklbw %%mm7,%%mm0\n\t" \17"punpckhbw %%mm7,%%mm6\n\t" \18/*mm3:mm5={d0,...,d7}*/ \19"movq %%mm3,%%mm5\n\t" \20"punpcklbw %%mm7,%%mm3\n\t" \21"punpckhbw %%mm7,%%mm5\n\t" \22/*mm6:mm0={a0-d0,...,a7-d7}*/ \23"psubw %%mm3,%%mm0\n\t" \24"psubw %%mm5,%%mm6\n\t" \25/*mm3:mm1={b0,...,b7}*/ \26"movq %%mm1,%%mm3\n\t" \27"punpcklbw %%mm7,%%mm1\n\t" \28"movq %%mm2,%%mm4\n\t" \29"punpckhbw %%mm7,%%mm3\n\t" \30/*mm5:mm4={c0,...,c7}*/ \31"movq %%mm2,%%mm5\n\t" \32"punpcklbw %%mm7,%%mm4\n\t" \33"punpckhbw %%mm7,%%mm5\n\t" \34/*mm7={3}x4 \35mm5:mm4={c0-b0,...,c7-b7}*/ \36"pcmpeqw %%mm7,%%mm7\n\t" \37"psubw %%mm1,%%mm4\n\t" \38"psrlw $14,%%mm7\n\t" \39"psubw %%mm3,%%mm5\n\t" \40/*Scale by 3.*/ \41"pmullw %%mm7,%%mm4\n\t" \42"pmullw %%mm7,%%mm5\n\t" \43/*mm7={4}x4 \44mm5:mm4=f={a0-d0+3*(c0-b0),...,a7-d7+3*(c7-b7)}*/ \45"psrlw $1,%%mm7\n\t" \46"paddw %%mm0,%%mm4\n\t" \47"psllw $2,%%mm7\n\t" \48"movq (%[ll]),%%mm0\n\t" \49"paddw %%mm6,%%mm5\n\t" \50/*R_i has the range [-127,128], so we compute -R_i instead. \51mm4=-R_i=-(f+4>>3)=0xFF^(f-4>>3)*/ \52"psubw %%mm7,%%mm4\n\t" \53"psubw %%mm7,%%mm5\n\t" \54"psraw $3,%%mm4\n\t" \55"psraw $3,%%mm5\n\t" \56"pcmpeqb %%mm7,%%mm7\n\t" \57"packsswb %%mm5,%%mm4\n\t" \58"pxor %%mm6,%%mm6\n\t" \59"pxor %%mm7,%%mm4\n\t" \60"packuswb %%mm3,%%mm1\n\t" \61/*Now compute lflim of -mm4 cf. Section 7.10 of the sepc.*/ \62/*There's no unsigned byte+signed byte with unsigned saturation op code, so \63we have to split things by sign (the other option is to work in 16 bits, \64but working in 8 bits gives much better parallelism). \65We compute abs(R_i), but save a mask of which terms were negative in mm6. \66Then we compute mm4=abs(lflim(R_i,L))=min(abs(R_i),max(2*L-abs(R_i),0)). \67Finally, we split mm4 into positive and negative pieces using the mask in \68mm6, and add and subtract them as appropriate.*/ \69/*mm4=abs(-R_i)*/ \70/*mm7=255-2*L*/ \71"pcmpgtb %%mm4,%%mm6\n\t" \72"psubb %%mm0,%%mm7\n\t" \73"pxor %%mm6,%%mm4\n\t" \74"psubb %%mm0,%%mm7\n\t" \75"psubb %%mm6,%%mm4\n\t" \76/*mm7=255-max(2*L-abs(R_i),0)*/ \77"paddusb %%mm4,%%mm7\n\t" \78/*mm4=min(abs(R_i),max(2*L-abs(R_i),0))*/ \79"paddusb %%mm7,%%mm4\n\t" \80"psubusb %%mm7,%%mm4\n\t" \81/*Now split mm4 by the original sign of -R_i.*/ \82"movq %%mm4,%%mm5\n\t" \83"pand %%mm6,%%mm4\n\t" \84"pandn %%mm5,%%mm6\n\t" \85/*mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)}*/ \86/*mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}*/ \87"paddusb %%mm4,%%mm1\n\t" \88"psubusb %%mm4,%%mm2\n\t" \89"psubusb %%mm6,%%mm1\n\t" \90"paddusb %%mm6,%%mm2\n\t" \9192/*On entry, mm0={a0,...,a7}, mm1={b0,...,b7}, mm2={c0,...,c7}, mm3={d0,...d7}.93On exit, mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)} and94mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}.95All other MMX registers are clobbered.*/96#define OC_LOOP_FILTER8_MMXEXT \97"#OC_LOOP_FILTER8_MMXEXT\n\t" \98/*R_i=(a_i-3*b_i+3*c_i-d_i+4>>3) has the range [-127,128], so we compute \99-R_i=(-a_i+3*b_i-3*c_i+d_i+3>>3) instead.*/ \100/*This first part is based on the transformation \101f = -(3*(c-b)+a-d+4>>3) \102= -(3*(c+255-b)+(a+255-d)+4-1020>>3) \103= -(3*(c+~b)+(a+~d)-1016>>3) \104= 127-(3*(c+~b)+(a+~d)>>3) \105= 128+~(3*(c+~b)+(a+~d)>>3) (mod 256). \106Although pavgb(a,b) = (a+b+1>>1) (biased up), we rely heavily on the \107fact that ~pavgb(~a,~b) = (a+b>>1) (biased down). \108Using this, the last expression above can be computed in 8 bits of working \109precision via: \110u = ~pavgb(~b,c); \111v = pavgb(b,~c); \112This mask is 0 or 0xFF, and controls whether t is biased up or down: \113m = u-v; \114t = m^pavgb(m^~a,m^d); \115f = 128+pavgb(pavgb(t,u),v); \116This required some careful analysis to ensure that carries are propagated \117correctly in all cases, but has been checked exhaustively.*/ \118/*input (a, b, c, d, ., ., ., .)*/ \119/*ff=0xFF; \120u=b; \121v=c; \122ll=255-2*L;*/ \123"pcmpeqb %%mm7,%%mm7\n\t" \124"movq %%mm1,%%mm4\n\t" \125"movq %%mm2,%%mm5\n\t" \126"movq (%[ll]),%%mm6\n\t" \127/*allocated u, v, ll, ff: (a, b, c, d, u, v, ll, ff)*/ \128/*u^=ff; \129v^=ff;*/ \130"pxor %%mm7,%%mm4\n\t" \131"pxor %%mm7,%%mm5\n\t" \132/*allocated ll: (a, b, c, d, u, v, ll, ff)*/ \133/*u=pavgb(u,c); \134v=pavgb(v,b);*/ \135"pavgb %%mm2,%%mm4\n\t" \136"pavgb %%mm1,%%mm5\n\t" \137/*u^=ff; \138a^=ff;*/ \139"pxor %%mm7,%%mm4\n\t" \140"pxor %%mm7,%%mm0\n\t" \141/*m=u-v;*/ \142"psubb %%mm5,%%mm4\n\t" \143/*freed u, allocated m: (a, b, c, d, m, v, ll, ff)*/ \144/*a^=m; \145d^=m;*/ \146"pxor %%mm4,%%mm0\n\t" \147"pxor %%mm4,%%mm3\n\t" \148/*t=pavgb(a,d);*/ \149"pavgb %%mm3,%%mm0\n\t" \150"psllw $7,%%mm7\n\t" \151/*freed a, d, ff, allocated t, of: (t, b, c, ., m, v, ll, of)*/ \152/*t^=m; \153u=m+v;*/ \154"pxor %%mm4,%%mm0\n\t" \155"paddb %%mm5,%%mm4\n\t" \156/*freed t, m, allocated f, u: (f, b, c, ., u, v, ll, of)*/ \157/*f=pavgb(f,u); \158of=128;*/ \159"pavgb %%mm4,%%mm0\n\t" \160"packsswb %%mm7,%%mm7\n\t" \161/*freed u, ff, allocated ll: (f, b, c, ., ll, v, ll, of)*/ \162/*f=pavgb(f,v);*/ \163"pavgb %%mm5,%%mm0\n\t" \164"movq %%mm7,%%mm3\n\t" \165"movq %%mm6,%%mm4\n\t" \166/*freed v, allocated of: (f, b, c, of, ll, ., ll, of)*/ \167/*Now compute lflim of R_i=-(128+mm0) cf. Section 7.10 of the sepc.*/ \168/*There's no unsigned byte+signed byte with unsigned saturation op code, so \169we have to split things by sign (the other option is to work in 16 bits, \170but staying in 8 bits gives much better parallelism).*/ \171/*Instead of adding the offset of 128 in mm3, we use it to split mm0. \172This is the same number of instructions as computing a mask and splitting \173after the lflim computation, but has shorter dependency chains.*/ \174/*mm0=R_i<0?-R_i:0 (denoted abs(R_i<0))\175mm3=R_i>0?R_i:0* (denoted abs(R_i>0))*/ \176"psubusb %%mm0,%%mm3\n\t" \177"psubusb %%mm7,%%mm0\n\t" \178/*mm6=255-max(2*L-abs(R_i<0),0) \179mm4=255-max(2*L-abs(R_i>0),0)*/ \180"paddusb %%mm3,%%mm4\n\t" \181"paddusb %%mm0,%%mm6\n\t" \182/*mm0=min(abs(R_i<0),max(2*L-abs(R_i<0),0)) \183mm3=min(abs(R_i>0),max(2*L-abs(R_i>0),0))*/ \184"paddusb %%mm4,%%mm3\n\t" \185"paddusb %%mm6,%%mm0\n\t" \186"psubusb %%mm4,%%mm3\n\t" \187"psubusb %%mm6,%%mm0\n\t" \188/*mm1={b0+lflim(R_0,L),...,b7+lflim(R_7,L)}*/ \189/*mm2={c0-lflim(R_0,L),...,c7-lflim(R_7,L)}*/ \190"paddusb %%mm3,%%mm1\n\t" \191"psubusb %%mm3,%%mm2\n\t" \192"psubusb %%mm0,%%mm1\n\t" \193"paddusb %%mm0,%%mm2\n\t" \194195#define OC_LOOP_FILTER_V(_filter,_pix,_ystride,_ll) \196do{ \197ptrdiff_t ystride3__; \198__asm__ __volatile__( \199/*mm0={a0,...,a7}*/ \200"movq (%[pix]),%%mm0\n\t" \201/*ystride3=_ystride*3*/ \202"lea (%[ystride],%[ystride],2),%[ystride3]\n\t" \203/*mm3={d0,...,d7}*/ \204"movq (%[pix],%[ystride3]),%%mm3\n\t" \205/*mm1={b0,...,b7}*/ \206"movq (%[pix],%[ystride]),%%mm1\n\t" \207/*mm2={c0,...,c7}*/ \208"movq (%[pix],%[ystride],2),%%mm2\n\t" \209_filter \210/*Write it back out.*/ \211"movq %%mm1,(%[pix],%[ystride])\n\t" \212"movq %%mm2,(%[pix],%[ystride],2)\n\t" \213:[ystride3]"=&r"(ystride3__) \214:[pix]"r"(_pix-_ystride*2),[ystride]"r"((ptrdiff_t)(_ystride)), \215[ll]"r"(_ll) \216:"memory" \217); \218} \219while(0)220221#define OC_LOOP_FILTER_H(_filter,_pix,_ystride,_ll) \222do{ \223unsigned char *pix__; \224ptrdiff_t ystride3__; \225ptrdiff_t d__; \226pix__=(_pix)-2; \227__asm__ __volatile__( \228/*x x x x d0 c0 b0 a0*/ \229"movd (%[pix]),%%mm0\n\t" \230/*x x x x d1 c1 b1 a1*/ \231"movd (%[pix],%[ystride]),%%mm1\n\t" \232/*ystride3=_ystride*3*/ \233"lea (%[ystride],%[ystride],2),%[ystride3]\n\t" \234/*x x x x d2 c2 b2 a2*/ \235"movd (%[pix],%[ystride],2),%%mm2\n\t" \236/*x x x x d3 c3 b3 a3*/ \237"lea (%[pix],%[ystride],4),%[d]\n\t" \238"movd (%[pix],%[ystride3]),%%mm3\n\t" \239/*x x x x d4 c4 b4 a4*/ \240"movd (%[d]),%%mm4\n\t" \241/*x x x x d5 c5 b5 a5*/ \242"movd (%[d],%[ystride]),%%mm5\n\t" \243/*x x x x d6 c6 b6 a6*/ \244"movd (%[d],%[ystride],2),%%mm6\n\t" \245/*x x x x d7 c7 b7 a7*/ \246"movd (%[d],%[ystride3]),%%mm7\n\t" \247/*mm0=d1 d0 c1 c0 b1 b0 a1 a0*/ \248"punpcklbw %%mm1,%%mm0\n\t" \249/*mm2=d3 d2 c3 c2 b3 b2 a3 a2*/ \250"punpcklbw %%mm3,%%mm2\n\t" \251/*mm3=d1 d0 c1 c0 b1 b0 a1 a0*/ \252"movq %%mm0,%%mm3\n\t" \253/*mm0=b3 b2 b1 b0 a3 a2 a1 a0*/ \254"punpcklwd %%mm2,%%mm0\n\t" \255/*mm3=d3 d2 d1 d0 c3 c2 c1 c0*/ \256"punpckhwd %%mm2,%%mm3\n\t" \257/*mm1=b3 b2 b1 b0 a3 a2 a1 a0*/ \258"movq %%mm0,%%mm1\n\t" \259/*mm4=d5 d4 c5 c4 b5 b4 a5 a4*/ \260"punpcklbw %%mm5,%%mm4\n\t" \261/*mm6=d7 d6 c7 c6 b7 b6 a7 a6*/ \262"punpcklbw %%mm7,%%mm6\n\t" \263/*mm5=d5 d4 c5 c4 b5 b4 a5 a4*/ \264"movq %%mm4,%%mm5\n\t" \265/*mm4=b7 b6 b5 b4 a7 a6 a5 a4*/ \266"punpcklwd %%mm6,%%mm4\n\t" \267/*mm5=d7 d6 d5 d4 c7 c6 c5 c4*/ \268"punpckhwd %%mm6,%%mm5\n\t" \269/*mm2=d3 d2 d1 d0 c3 c2 c1 c0*/ \270"movq %%mm3,%%mm2\n\t" \271/*mm0=a7 a6 a5 a4 a3 a2 a1 a0*/ \272"punpckldq %%mm4,%%mm0\n\t" \273/*mm1=b7 b6 b5 b4 b3 b2 b1 b0*/ \274"punpckhdq %%mm4,%%mm1\n\t" \275/*mm2=c7 c6 c5 c4 c3 c2 c1 c0*/ \276"punpckldq %%mm5,%%mm2\n\t" \277/*mm3=d7 d6 d5 d4 d3 d2 d1 d0*/ \278"punpckhdq %%mm5,%%mm3\n\t" \279_filter \280/*mm2={b0+R_0'',...,b7+R_7''}*/ \281"movq %%mm1,%%mm0\n\t" \282/*mm1={b0+R_0'',c0-R_0'',...,b3+R_3'',c3-R_3''}*/ \283"punpcklbw %%mm2,%%mm1\n\t" \284/*mm2={b4+R_4'',c4-R_4'',...,b7+R_7'',c7-R_7''}*/ \285"punpckhbw %%mm2,%%mm0\n\t" \286/*[d]=c1 b1 c0 b0*/ \287"movd %%mm1,%[d]\n\t" \288"movw %w[d],1(%[pix])\n\t" \289"psrlq $32,%%mm1\n\t" \290"shr $16,%[d]\n\t" \291"movw %w[d],1(%[pix],%[ystride])\n\t" \292/*[d]=c3 b3 c2 b2*/ \293"movd %%mm1,%[d]\n\t" \294"movw %w[d],1(%[pix],%[ystride],2)\n\t" \295"shr $16,%[d]\n\t" \296"movw %w[d],1(%[pix],%[ystride3])\n\t" \297"lea (%[pix],%[ystride],4),%[pix]\n\t" \298/*[d]=c5 b5 c4 b4*/ \299"movd %%mm0,%[d]\n\t" \300"movw %w[d],1(%[pix])\n\t" \301"psrlq $32,%%mm0\n\t" \302"shr $16,%[d]\n\t" \303"movw %w[d],1(%[pix],%[ystride])\n\t" \304/*[d]=c7 b7 c6 b6*/ \305"movd %%mm0,%[d]\n\t" \306"movw %w[d],1(%[pix],%[ystride],2)\n\t" \307"shr $16,%[d]\n\t" \308"movw %w[d],1(%[pix],%[ystride3])\n\t" \309:[pix]"+r"(pix__),[ystride3]"=&r"(ystride3__),[d]"=&r"(d__) \310:[ystride]"r"((ptrdiff_t)(_ystride)),[ll]"r"(_ll) \311:"memory" \312); \313} \314while(0)315316# endif317#endif318319320