jfdctfst-mmx.asm 15 KB

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