mmxloop.h 11 KB

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