jfdctfst-mmx.asm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. ;
  2. ; jfdctfst.asm - fast integer FDCT (MMX)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. ;
  6. ; Based on the x86 SIMD extension for IJG JPEG library
  7. ; Copyright (C) 1999-2006, MIYASAKA Masaru.
  8. ; For conditions of distribution and use, see copyright notice in jsimdext.inc
  9. ;
  10. ; This file should be assembled with NASM (Netwide Assembler),
  11. ; can *not* be assembled with Microsoft's MASM or any compatible
  12. ; assembler (including Borland's Turbo Assembler).
  13. ; NASM is available from http://nasm.sourceforge.net/ or
  14. ; http://sourceforge.net/project/showfiles.php?group_id=6208
  15. ;
  16. ; This file contains a fast, not so accurate integer implementation of
  17. ; the forward DCT (Discrete Cosine Transform). The following code is
  18. ; based directly on the IJG's original jfdctfst.c; see the jfdctfst.c
  19. ; for more details.
  20. ;
  21. ; [TAB8]
  22. %include "jsimdext.inc"
  23. %include "jdct.inc"
  24. ; --------------------------------------------------------------------------
  25. %define CONST_BITS 8 ; 14 is also OK.
  26. %if CONST_BITS == 8
  27. F_0_382 equ 98 ; FIX(0.382683433)
  28. F_0_541 equ 139 ; FIX(0.541196100)
  29. F_0_707 equ 181 ; FIX(0.707106781)
  30. F_1_306 equ 334 ; FIX(1.306562965)
  31. %else
  32. ; NASM cannot do compile-time arithmetic on floating-point constants.
  33. %define DESCALE(x,n) (((x)+(1<<((n)-1)))>>(n))
  34. F_0_382 equ DESCALE( 410903207,30-CONST_BITS) ; FIX(0.382683433)
  35. F_0_541 equ DESCALE( 581104887,30-CONST_BITS) ; FIX(0.541196100)
  36. F_0_707 equ DESCALE( 759250124,30-CONST_BITS) ; FIX(0.707106781)
  37. F_1_306 equ DESCALE(1402911301,30-CONST_BITS) ; FIX(1.306562965)
  38. %endif
  39. ; --------------------------------------------------------------------------
  40. SECTION SEG_CONST
  41. ; PRE_MULTIPLY_SCALE_BITS <= 2 (to avoid overflow)
  42. ; CONST_BITS + CONST_SHIFT + PRE_MULTIPLY_SCALE_BITS == 16 (for pmulhw)
  43. %define PRE_MULTIPLY_SCALE_BITS 2
  44. %define CONST_SHIFT (16 - PRE_MULTIPLY_SCALE_BITS - CONST_BITS)
  45. alignz 16
  46. global EXTN(jconst_fdct_ifast_mmx)
  47. EXTN(jconst_fdct_ifast_mmx):
  48. PW_F0707 times 4 dw F_0_707 << CONST_SHIFT
  49. PW_F0382 times 4 dw F_0_382 << CONST_SHIFT
  50. PW_F0541 times 4 dw F_0_541 << CONST_SHIFT
  51. PW_F1306 times 4 dw F_1_306 << CONST_SHIFT
  52. alignz 16
  53. ; --------------------------------------------------------------------------
  54. SECTION SEG_TEXT
  55. BITS 32
  56. ;
  57. ; Perform the forward DCT on one block of samples.
  58. ;
  59. ; GLOBAL(void)
  60. ; jsimd_fdct_ifast_mmx (DCTELEM *data)
  61. ;
  62. %define data(b) (b)+8 ; DCTELEM *data
  63. %define original_ebp ebp+0
  64. %define wk(i) ebp-(WK_NUM-(i))*SIZEOF_MMWORD ; mmword wk[WK_NUM]
  65. %define WK_NUM 2
  66. align 16
  67. global EXTN(jsimd_fdct_ifast_mmx)
  68. EXTN(jsimd_fdct_ifast_mmx):
  69. push ebp
  70. mov eax,esp ; eax = original ebp
  71. sub esp, byte 4
  72. and esp, byte (-SIZEOF_MMWORD) ; align to 64 bits
  73. mov [esp],eax
  74. mov ebp,esp ; ebp = aligned ebp
  75. lea esp, [wk(0)]
  76. pushpic ebx
  77. ; push ecx ; need not be preserved
  78. ; push edx ; need not be preserved
  79. ; push esi ; unused
  80. ; push edi ; unused
  81. get_GOT ebx ; get GOT address
  82. ; ---- Pass 1: process rows.
  83. mov edx, POINTER [data(eax)] ; (DCTELEM *)
  84. mov ecx, DCTSIZE/4
  85. alignx 16,7
  86. .rowloop:
  87. movq mm0, MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)]
  88. movq mm1, MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)]
  89. movq mm2, MMWORD [MMBLOCK(2,1,edx,SIZEOF_DCTELEM)]
  90. movq mm3, MMWORD [MMBLOCK(3,1,edx,SIZEOF_DCTELEM)]
  91. ; mm0=(20 21 22 23), mm2=(24 25 26 27)
  92. ; mm1=(30 31 32 33), mm3=(34 35 36 37)
  93. movq mm4,mm0 ; transpose coefficients(phase 1)
  94. punpcklwd mm0,mm1 ; mm0=(20 30 21 31)
  95. punpckhwd mm4,mm1 ; mm4=(22 32 23 33)
  96. movq mm5,mm2 ; transpose coefficients(phase 1)
  97. punpcklwd mm2,mm3 ; mm2=(24 34 25 35)
  98. punpckhwd mm5,mm3 ; mm5=(26 36 27 37)
  99. movq mm6, MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)]
  100. movq mm7, MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)]
  101. movq mm1, MMWORD [MMBLOCK(0,1,edx,SIZEOF_DCTELEM)]
  102. movq mm3, MMWORD [MMBLOCK(1,1,edx,SIZEOF_DCTELEM)]
  103. ; mm6=(00 01 02 03), mm1=(04 05 06 07)
  104. ; mm7=(10 11 12 13), mm3=(14 15 16 17)
  105. movq MMWORD [wk(0)], mm4 ; wk(0)=(22 32 23 33)
  106. movq MMWORD [wk(1)], mm2 ; wk(1)=(24 34 25 35)
  107. movq mm4,mm6 ; transpose coefficients(phase 1)
  108. punpcklwd mm6,mm7 ; mm6=(00 10 01 11)
  109. punpckhwd mm4,mm7 ; mm4=(02 12 03 13)
  110. movq mm2,mm1 ; transpose coefficients(phase 1)
  111. punpcklwd mm1,mm3 ; mm1=(04 14 05 15)
  112. punpckhwd mm2,mm3 ; mm2=(06 16 07 17)
  113. movq mm7,mm6 ; transpose coefficients(phase 2)
  114. punpckldq mm6,mm0 ; mm6=(00 10 20 30)=data0
  115. punpckhdq mm7,mm0 ; mm7=(01 11 21 31)=data1
  116. movq mm3,mm2 ; transpose coefficients(phase 2)
  117. punpckldq mm2,mm5 ; mm2=(06 16 26 36)=data6
  118. punpckhdq mm3,mm5 ; mm3=(07 17 27 37)=data7
  119. movq mm0,mm7
  120. movq mm5,mm6
  121. psubw mm7,mm2 ; mm7=data1-data6=tmp6
  122. psubw mm6,mm3 ; mm6=data0-data7=tmp7
  123. paddw mm0,mm2 ; mm0=data1+data6=tmp1
  124. paddw mm5,mm3 ; mm5=data0+data7=tmp0
  125. movq mm2, MMWORD [wk(0)] ; mm2=(22 32 23 33)
  126. movq mm3, MMWORD [wk(1)] ; mm3=(24 34 25 35)
  127. movq MMWORD [wk(0)], mm7 ; wk(0)=tmp6
  128. movq MMWORD [wk(1)], mm6 ; wk(1)=tmp7
  129. movq mm7,mm4 ; transpose coefficients(phase 2)
  130. punpckldq mm4,mm2 ; mm4=(02 12 22 32)=data2
  131. punpckhdq mm7,mm2 ; mm7=(03 13 23 33)=data3
  132. movq mm6,mm1 ; transpose coefficients(phase 2)
  133. punpckldq mm1,mm3 ; mm1=(04 14 24 34)=data4
  134. punpckhdq mm6,mm3 ; mm6=(05 15 25 35)=data5
  135. movq mm2,mm7
  136. movq mm3,mm4
  137. paddw mm7,mm1 ; mm7=data3+data4=tmp3
  138. paddw mm4,mm6 ; mm4=data2+data5=tmp2
  139. psubw mm2,mm1 ; mm2=data3-data4=tmp4
  140. psubw mm3,mm6 ; mm3=data2-data5=tmp5
  141. ; -- Even part
  142. movq mm1,mm5
  143. movq mm6,mm0
  144. psubw mm5,mm7 ; mm5=tmp13
  145. psubw mm0,mm4 ; mm0=tmp12
  146. paddw mm1,mm7 ; mm1=tmp10
  147. paddw mm6,mm4 ; mm6=tmp11
  148. paddw mm0,mm5
  149. psllw mm0,PRE_MULTIPLY_SCALE_BITS
  150. pmulhw mm0,[GOTOFF(ebx,PW_F0707)] ; mm0=z1
  151. movq mm7,mm1
  152. movq mm4,mm5
  153. psubw mm1,mm6 ; mm1=data4
  154. psubw mm5,mm0 ; mm5=data6
  155. paddw mm7,mm6 ; mm7=data0
  156. paddw mm4,mm0 ; mm4=data2
  157. movq MMWORD [MMBLOCK(0,1,edx,SIZEOF_DCTELEM)], mm1
  158. movq MMWORD [MMBLOCK(2,1,edx,SIZEOF_DCTELEM)], mm5
  159. movq MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)], mm7
  160. movq MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)], mm4
  161. ; -- Odd part
  162. movq mm6, MMWORD [wk(0)] ; mm6=tmp6
  163. movq mm0, MMWORD [wk(1)] ; mm0=tmp7
  164. paddw mm2,mm3 ; mm2=tmp10
  165. paddw mm3,mm6 ; mm3=tmp11
  166. paddw mm6,mm0 ; mm6=tmp12, mm0=tmp7
  167. psllw mm2,PRE_MULTIPLY_SCALE_BITS
  168. psllw mm6,PRE_MULTIPLY_SCALE_BITS
  169. psllw mm3,PRE_MULTIPLY_SCALE_BITS
  170. pmulhw mm3,[GOTOFF(ebx,PW_F0707)] ; mm3=z3
  171. movq mm1,mm2 ; mm1=tmp10
  172. psubw mm2,mm6
  173. pmulhw mm2,[GOTOFF(ebx,PW_F0382)] ; mm2=z5
  174. pmulhw mm1,[GOTOFF(ebx,PW_F0541)] ; mm1=MULTIPLY(tmp10,FIX_0_54119610)
  175. pmulhw mm6,[GOTOFF(ebx,PW_F1306)] ; mm6=MULTIPLY(tmp12,FIX_1_30656296)
  176. paddw mm1,mm2 ; mm1=z2
  177. paddw mm6,mm2 ; mm6=z4
  178. movq mm5,mm0
  179. psubw mm0,mm3 ; mm0=z13
  180. paddw mm5,mm3 ; mm5=z11
  181. movq mm7,mm0
  182. movq mm4,mm5
  183. psubw mm0,mm1 ; mm0=data3
  184. psubw mm5,mm6 ; mm5=data7
  185. paddw mm7,mm1 ; mm7=data5
  186. paddw mm4,mm6 ; mm4=data1
  187. movq MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)], mm0
  188. movq MMWORD [MMBLOCK(3,1,edx,SIZEOF_DCTELEM)], mm5
  189. movq MMWORD [MMBLOCK(1,1,edx,SIZEOF_DCTELEM)], mm7
  190. movq MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)], mm4
  191. add edx, byte 4*DCTSIZE*SIZEOF_DCTELEM
  192. dec ecx
  193. jnz near .rowloop
  194. ; ---- Pass 2: process columns.
  195. mov edx, POINTER [data(eax)] ; (DCTELEM *)
  196. mov ecx, DCTSIZE/4
  197. alignx 16,7
  198. .columnloop:
  199. movq mm0, MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)]
  200. movq mm1, MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)]
  201. movq mm2, MMWORD [MMBLOCK(6,0,edx,SIZEOF_DCTELEM)]
  202. movq mm3, MMWORD [MMBLOCK(7,0,edx,SIZEOF_DCTELEM)]
  203. ; mm0=(02 12 22 32), mm2=(42 52 62 72)
  204. ; mm1=(03 13 23 33), mm3=(43 53 63 73)
  205. movq mm4,mm0 ; transpose coefficients(phase 1)
  206. punpcklwd mm0,mm1 ; mm0=(02 03 12 13)
  207. punpckhwd mm4,mm1 ; mm4=(22 23 32 33)
  208. movq mm5,mm2 ; transpose coefficients(phase 1)
  209. punpcklwd mm2,mm3 ; mm2=(42 43 52 53)
  210. punpckhwd mm5,mm3 ; mm5=(62 63 72 73)
  211. movq mm6, MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)]
  212. movq mm7, MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)]
  213. movq mm1, MMWORD [MMBLOCK(4,0,edx,SIZEOF_DCTELEM)]
  214. movq mm3, MMWORD [MMBLOCK(5,0,edx,SIZEOF_DCTELEM)]
  215. ; mm6=(00 10 20 30), mm1=(40 50 60 70)
  216. ; mm7=(01 11 21 31), mm3=(41 51 61 71)
  217. movq MMWORD [wk(0)], mm4 ; wk(0)=(22 23 32 33)
  218. movq MMWORD [wk(1)], mm2 ; wk(1)=(42 43 52 53)
  219. movq mm4,mm6 ; transpose coefficients(phase 1)
  220. punpcklwd mm6,mm7 ; mm6=(00 01 10 11)
  221. punpckhwd mm4,mm7 ; mm4=(20 21 30 31)
  222. movq mm2,mm1 ; transpose coefficients(phase 1)
  223. punpcklwd mm1,mm3 ; mm1=(40 41 50 51)
  224. punpckhwd mm2,mm3 ; mm2=(60 61 70 71)
  225. movq mm7,mm6 ; transpose coefficients(phase 2)
  226. punpckldq mm6,mm0 ; mm6=(00 01 02 03)=data0
  227. punpckhdq mm7,mm0 ; mm7=(10 11 12 13)=data1
  228. movq mm3,mm2 ; transpose coefficients(phase 2)
  229. punpckldq mm2,mm5 ; mm2=(60 61 62 63)=data6
  230. punpckhdq mm3,mm5 ; mm3=(70 71 72 73)=data7
  231. movq mm0,mm7
  232. movq mm5,mm6
  233. psubw mm7,mm2 ; mm7=data1-data6=tmp6
  234. psubw mm6,mm3 ; mm6=data0-data7=tmp7
  235. paddw mm0,mm2 ; mm0=data1+data6=tmp1
  236. paddw mm5,mm3 ; mm5=data0+data7=tmp0
  237. movq mm2, MMWORD [wk(0)] ; mm2=(22 23 32 33)
  238. movq mm3, MMWORD [wk(1)] ; mm3=(42 43 52 53)
  239. movq MMWORD [wk(0)], mm7 ; wk(0)=tmp6
  240. movq MMWORD [wk(1)], mm6 ; wk(1)=tmp7
  241. movq mm7,mm4 ; transpose coefficients(phase 2)
  242. punpckldq mm4,mm2 ; mm4=(20 21 22 23)=data2
  243. punpckhdq mm7,mm2 ; mm7=(30 31 32 33)=data3
  244. movq mm6,mm1 ; transpose coefficients(phase 2)
  245. punpckldq mm1,mm3 ; mm1=(40 41 42 43)=data4
  246. punpckhdq mm6,mm3 ; mm6=(50 51 52 53)=data5
  247. movq mm2,mm7
  248. movq mm3,mm4
  249. paddw mm7,mm1 ; mm7=data3+data4=tmp3
  250. paddw mm4,mm6 ; mm4=data2+data5=tmp2
  251. psubw mm2,mm1 ; mm2=data3-data4=tmp4
  252. psubw mm3,mm6 ; mm3=data2-data5=tmp5
  253. ; -- Even part
  254. movq mm1,mm5
  255. movq mm6,mm0
  256. psubw mm5,mm7 ; mm5=tmp13
  257. psubw mm0,mm4 ; mm0=tmp12
  258. paddw mm1,mm7 ; mm1=tmp10
  259. paddw mm6,mm4 ; mm6=tmp11
  260. paddw mm0,mm5
  261. psllw mm0,PRE_MULTIPLY_SCALE_BITS
  262. pmulhw mm0,[GOTOFF(ebx,PW_F0707)] ; mm0=z1
  263. movq mm7,mm1
  264. movq mm4,mm5
  265. psubw mm1,mm6 ; mm1=data4
  266. psubw mm5,mm0 ; mm5=data6
  267. paddw mm7,mm6 ; mm7=data0
  268. paddw mm4,mm0 ; mm4=data2
  269. movq MMWORD [MMBLOCK(4,0,edx,SIZEOF_DCTELEM)], mm1
  270. movq MMWORD [MMBLOCK(6,0,edx,SIZEOF_DCTELEM)], mm5
  271. movq MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)], mm7
  272. movq MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)], mm4
  273. ; -- Odd part
  274. movq mm6, MMWORD [wk(0)] ; mm6=tmp6
  275. movq mm0, MMWORD [wk(1)] ; mm0=tmp7
  276. paddw mm2,mm3 ; mm2=tmp10
  277. paddw mm3,mm6 ; mm3=tmp11
  278. paddw mm6,mm0 ; mm6=tmp12, mm0=tmp7
  279. psllw mm2,PRE_MULTIPLY_SCALE_BITS
  280. psllw mm6,PRE_MULTIPLY_SCALE_BITS
  281. psllw mm3,PRE_MULTIPLY_SCALE_BITS
  282. pmulhw mm3,[GOTOFF(ebx,PW_F0707)] ; mm3=z3
  283. movq mm1,mm2 ; mm1=tmp10
  284. psubw mm2,mm6
  285. pmulhw mm2,[GOTOFF(ebx,PW_F0382)] ; mm2=z5
  286. pmulhw mm1,[GOTOFF(ebx,PW_F0541)] ; mm1=MULTIPLY(tmp10,FIX_0_54119610)
  287. pmulhw mm6,[GOTOFF(ebx,PW_F1306)] ; mm6=MULTIPLY(tmp12,FIX_1_30656296)
  288. paddw mm1,mm2 ; mm1=z2
  289. paddw mm6,mm2 ; mm6=z4
  290. movq mm5,mm0
  291. psubw mm0,mm3 ; mm0=z13
  292. paddw mm5,mm3 ; mm5=z11
  293. movq mm7,mm0
  294. movq mm4,mm5
  295. psubw mm0,mm1 ; mm0=data3
  296. psubw mm5,mm6 ; mm5=data7
  297. paddw mm7,mm1 ; mm7=data5
  298. paddw mm4,mm6 ; mm4=data1
  299. movq MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)], mm0
  300. movq MMWORD [MMBLOCK(7,0,edx,SIZEOF_DCTELEM)], mm5
  301. movq MMWORD [MMBLOCK(5,0,edx,SIZEOF_DCTELEM)], mm7
  302. movq MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)], mm4
  303. add edx, byte 4*SIZEOF_DCTELEM
  304. dec ecx
  305. jnz near .columnloop
  306. emms ; empty MMX state
  307. ; pop edi ; unused
  308. ; pop esi ; unused
  309. ; pop edx ; need not be preserved
  310. ; pop ecx ; need not be preserved
  311. poppic ebx
  312. mov esp,ebp ; esp <- aligned ebp
  313. pop esp ; esp <- original ebp
  314. pop ebp
  315. ret
  316. ; For some reason, the OS X linker does not honor the request to align the
  317. ; segment unless we do this.
  318. align 16