jfdctflt-sse.asm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. ;
  2. ; Floating-point FDCT (SSE)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. ; Copyright (C) 2016, 2024, 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) or Yasm.
  12. ;
  13. ; This file contains a floating-point implementation of the forward DCT
  14. ; (Discrete Cosine Transform). The following code is based directly on
  15. ; the IJG's original jfdctflt.c; see the jfdctflt.c for more details.
  16. %include "jsimdext.inc"
  17. %include "jdct.inc"
  18. ; --------------------------------------------------------------------------
  19. %macro unpcklps2 2 ; %1=(0 1 2 3) / %2=(4 5 6 7) => %1=(0 1 4 5)
  20. shufps %1, %2, 0x44
  21. %endmacro
  22. %macro unpckhps2 2 ; %1=(0 1 2 3) / %2=(4 5 6 7) => %1=(2 3 6 7)
  23. shufps %1, %2, 0xEE
  24. %endmacro
  25. ; --------------------------------------------------------------------------
  26. SECTION SEG_CONST
  27. ALIGNZ 32
  28. GLOBAL_DATA(jconst_fdct_float_sse)
  29. EXTN(jconst_fdct_float_sse):
  30. PD_0_382 times 4 dd 0.382683432365089771728460
  31. PD_0_707 times 4 dd 0.707106781186547524400844
  32. PD_0_541 times 4 dd 0.541196100146196984399723
  33. PD_1_306 times 4 dd 1.306562964876376527856643
  34. ALIGNZ 32
  35. ; --------------------------------------------------------------------------
  36. SECTION SEG_TEXT
  37. BITS 32
  38. ;
  39. ; Perform the forward DCT on one block of samples.
  40. ;
  41. ; GLOBAL(void)
  42. ; jsimd_fdct_float_sse(FAST_FLOAT *data)
  43. ;
  44. %define data(b) (b) + 8 ; FAST_FLOAT *data
  45. %define original_ebp ebp + 0
  46. %define wk(i) ebp - (WK_NUM - (i)) * SIZEOF_XMMWORD
  47. ; xmmword wk[WK_NUM]
  48. %define WK_NUM 2
  49. align 32
  50. GLOBAL_FUNCTION(jsimd_fdct_float_sse)
  51. EXTN(jsimd_fdct_float_sse):
  52. push ebp
  53. mov eax, esp ; eax = original ebp
  54. sub esp, byte 4
  55. and esp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
  56. mov [esp], eax
  57. mov ebp, esp ; ebp = aligned ebp
  58. lea esp, [wk(0)]
  59. PUSHPIC ebx
  60. ; push ecx ; need not be preserved
  61. ; push edx ; need not be preserved
  62. ; push esi ; unused
  63. ; push edi ; unused
  64. GET_GOT ebx ; get GOT address
  65. ; ---- Pass 1: process rows.
  66. mov edx, POINTER [data(eax)] ; (FAST_FLOAT *)
  67. mov ecx, DCTSIZE/4
  68. ALIGNX 16, 7
  69. .rowloop:
  70. movaps xmm0, XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)]
  71. movaps xmm1, XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)]
  72. movaps xmm2, XMMWORD [XMMBLOCK(2,1,edx,SIZEOF_FAST_FLOAT)]
  73. movaps xmm3, XMMWORD [XMMBLOCK(3,1,edx,SIZEOF_FAST_FLOAT)]
  74. ; xmm0=(20 21 22 23), xmm2=(24 25 26 27)
  75. ; xmm1=(30 31 32 33), xmm3=(34 35 36 37)
  76. movaps xmm4, xmm0 ; transpose coefficients(phase 1)
  77. unpcklps xmm0, xmm1 ; xmm0=(20 30 21 31)
  78. unpckhps xmm4, xmm1 ; xmm4=(22 32 23 33)
  79. movaps xmm5, xmm2 ; transpose coefficients(phase 1)
  80. unpcklps xmm2, xmm3 ; xmm2=(24 34 25 35)
  81. unpckhps xmm5, xmm3 ; xmm5=(26 36 27 37)
  82. movaps xmm6, XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)]
  83. movaps xmm7, XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)]
  84. movaps xmm1, XMMWORD [XMMBLOCK(0,1,edx,SIZEOF_FAST_FLOAT)]
  85. movaps xmm3, XMMWORD [XMMBLOCK(1,1,edx,SIZEOF_FAST_FLOAT)]
  86. ; xmm6=(00 01 02 03), xmm1=(04 05 06 07)
  87. ; xmm7=(10 11 12 13), xmm3=(14 15 16 17)
  88. movaps XMMWORD [wk(0)], xmm4 ; wk(0)=(22 32 23 33)
  89. movaps XMMWORD [wk(1)], xmm2 ; wk(1)=(24 34 25 35)
  90. movaps xmm4, xmm6 ; transpose coefficients(phase 1)
  91. unpcklps xmm6, xmm7 ; xmm6=(00 10 01 11)
  92. unpckhps xmm4, xmm7 ; xmm4=(02 12 03 13)
  93. movaps xmm2, xmm1 ; transpose coefficients(phase 1)
  94. unpcklps xmm1, xmm3 ; xmm1=(04 14 05 15)
  95. unpckhps xmm2, xmm3 ; xmm2=(06 16 07 17)
  96. movaps xmm7, xmm6 ; transpose coefficients(phase 2)
  97. unpcklps2 xmm6, xmm0 ; xmm6=(00 10 20 30)=data0
  98. unpckhps2 xmm7, xmm0 ; xmm7=(01 11 21 31)=data1
  99. movaps xmm3, xmm2 ; transpose coefficients(phase 2)
  100. unpcklps2 xmm2, xmm5 ; xmm2=(06 16 26 36)=data6
  101. unpckhps2 xmm3, xmm5 ; xmm3=(07 17 27 37)=data7
  102. movaps xmm0, xmm7
  103. movaps xmm5, xmm6
  104. subps xmm7, xmm2 ; xmm7=data1-data6=tmp6
  105. subps xmm6, xmm3 ; xmm6=data0-data7=tmp7
  106. addps xmm0, xmm2 ; xmm0=data1+data6=tmp1
  107. addps xmm5, xmm3 ; xmm5=data0+data7=tmp0
  108. movaps xmm2, XMMWORD [wk(0)] ; xmm2=(22 32 23 33)
  109. movaps xmm3, XMMWORD [wk(1)] ; xmm3=(24 34 25 35)
  110. movaps XMMWORD [wk(0)], xmm7 ; wk(0)=tmp6
  111. movaps XMMWORD [wk(1)], xmm6 ; wk(1)=tmp7
  112. movaps xmm7, xmm4 ; transpose coefficients(phase 2)
  113. unpcklps2 xmm4, xmm2 ; xmm4=(02 12 22 32)=data2
  114. unpckhps2 xmm7, xmm2 ; xmm7=(03 13 23 33)=data3
  115. movaps xmm6, xmm1 ; transpose coefficients(phase 2)
  116. unpcklps2 xmm1, xmm3 ; xmm1=(04 14 24 34)=data4
  117. unpckhps2 xmm6, xmm3 ; xmm6=(05 15 25 35)=data5
  118. movaps xmm2, xmm7
  119. movaps xmm3, xmm4
  120. addps xmm7, xmm1 ; xmm7=data3+data4=tmp3
  121. addps xmm4, xmm6 ; xmm4=data2+data5=tmp2
  122. subps xmm2, xmm1 ; xmm2=data3-data4=tmp4
  123. subps xmm3, xmm6 ; xmm3=data2-data5=tmp5
  124. ; -- Even part
  125. movaps xmm1, xmm5
  126. movaps xmm6, xmm0
  127. subps xmm5, xmm7 ; xmm5=tmp13
  128. subps xmm0, xmm4 ; xmm0=tmp12
  129. addps xmm1, xmm7 ; xmm1=tmp10
  130. addps xmm6, xmm4 ; xmm6=tmp11
  131. addps xmm0, xmm5
  132. mulps xmm0, [GOTOFF(ebx,PD_0_707)] ; xmm0=z1
  133. movaps xmm7, xmm1
  134. movaps xmm4, xmm5
  135. subps xmm1, xmm6 ; xmm1=data4
  136. subps xmm5, xmm0 ; xmm5=data6
  137. addps xmm7, xmm6 ; xmm7=data0
  138. addps xmm4, xmm0 ; xmm4=data2
  139. movaps XMMWORD [XMMBLOCK(0,1,edx,SIZEOF_FAST_FLOAT)], xmm1
  140. movaps XMMWORD [XMMBLOCK(2,1,edx,SIZEOF_FAST_FLOAT)], xmm5
  141. movaps XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)], xmm7
  142. movaps XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)], xmm4
  143. ; -- Odd part
  144. movaps xmm6, XMMWORD [wk(0)] ; xmm6=tmp6
  145. movaps xmm0, XMMWORD [wk(1)] ; xmm0=tmp7
  146. addps xmm2, xmm3 ; xmm2=tmp10
  147. addps xmm3, xmm6 ; xmm3=tmp11
  148. addps xmm6, xmm0 ; xmm6=tmp12, xmm0=tmp7
  149. mulps xmm3, [GOTOFF(ebx,PD_0_707)] ; xmm3=z3
  150. movaps xmm1, xmm2 ; xmm1=tmp10
  151. subps xmm2, xmm6
  152. mulps xmm2, [GOTOFF(ebx,PD_0_382)] ; xmm2=z5
  153. mulps xmm1, [GOTOFF(ebx,PD_0_541)] ; xmm1=MULTIPLY(tmp10,FIX_0_541196)
  154. mulps xmm6, [GOTOFF(ebx,PD_1_306)] ; xmm6=MULTIPLY(tmp12,FIX_1_306562)
  155. addps xmm1, xmm2 ; xmm1=z2
  156. addps xmm6, xmm2 ; xmm6=z4
  157. movaps xmm5, xmm0
  158. subps xmm0, xmm3 ; xmm0=z13
  159. addps xmm5, xmm3 ; xmm5=z11
  160. movaps xmm7, xmm0
  161. movaps xmm4, xmm5
  162. subps xmm0, xmm1 ; xmm0=data3
  163. subps xmm5, xmm6 ; xmm5=data7
  164. addps xmm7, xmm1 ; xmm7=data5
  165. addps xmm4, xmm6 ; xmm4=data1
  166. movaps XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)], xmm0
  167. movaps XMMWORD [XMMBLOCK(3,1,edx,SIZEOF_FAST_FLOAT)], xmm5
  168. movaps XMMWORD [XMMBLOCK(1,1,edx,SIZEOF_FAST_FLOAT)], xmm7
  169. movaps XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)], xmm4
  170. add edx, 4*DCTSIZE*SIZEOF_FAST_FLOAT
  171. dec ecx
  172. jnz near .rowloop
  173. ; ---- Pass 2: process columns.
  174. mov edx, POINTER [data(eax)] ; (FAST_FLOAT *)
  175. mov ecx, DCTSIZE/4
  176. ALIGNX 16, 7
  177. .columnloop:
  178. movaps xmm0, XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)]
  179. movaps xmm1, XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)]
  180. movaps xmm2, XMMWORD [XMMBLOCK(6,0,edx,SIZEOF_FAST_FLOAT)]
  181. movaps xmm3, XMMWORD [XMMBLOCK(7,0,edx,SIZEOF_FAST_FLOAT)]
  182. ; xmm0=(02 12 22 32), xmm2=(42 52 62 72)
  183. ; xmm1=(03 13 23 33), xmm3=(43 53 63 73)
  184. movaps xmm4, xmm0 ; transpose coefficients(phase 1)
  185. unpcklps xmm0, xmm1 ; xmm0=(02 03 12 13)
  186. unpckhps xmm4, xmm1 ; xmm4=(22 23 32 33)
  187. movaps xmm5, xmm2 ; transpose coefficients(phase 1)
  188. unpcklps xmm2, xmm3 ; xmm2=(42 43 52 53)
  189. unpckhps xmm5, xmm3 ; xmm5=(62 63 72 73)
  190. movaps xmm6, XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)]
  191. movaps xmm7, XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)]
  192. movaps xmm1, XMMWORD [XMMBLOCK(4,0,edx,SIZEOF_FAST_FLOAT)]
  193. movaps xmm3, XMMWORD [XMMBLOCK(5,0,edx,SIZEOF_FAST_FLOAT)]
  194. ; xmm6=(00 10 20 30), xmm1=(40 50 60 70)
  195. ; xmm7=(01 11 21 31), xmm3=(41 51 61 71)
  196. movaps XMMWORD [wk(0)], xmm4 ; wk(0)=(22 23 32 33)
  197. movaps XMMWORD [wk(1)], xmm2 ; wk(1)=(42 43 52 53)
  198. movaps xmm4, xmm6 ; transpose coefficients(phase 1)
  199. unpcklps xmm6, xmm7 ; xmm6=(00 01 10 11)
  200. unpckhps xmm4, xmm7 ; xmm4=(20 21 30 31)
  201. movaps xmm2, xmm1 ; transpose coefficients(phase 1)
  202. unpcklps xmm1, xmm3 ; xmm1=(40 41 50 51)
  203. unpckhps xmm2, xmm3 ; xmm2=(60 61 70 71)
  204. movaps xmm7, xmm6 ; transpose coefficients(phase 2)
  205. unpcklps2 xmm6, xmm0 ; xmm6=(00 01 02 03)=data0
  206. unpckhps2 xmm7, xmm0 ; xmm7=(10 11 12 13)=data1
  207. movaps xmm3, xmm2 ; transpose coefficients(phase 2)
  208. unpcklps2 xmm2, xmm5 ; xmm2=(60 61 62 63)=data6
  209. unpckhps2 xmm3, xmm5 ; xmm3=(70 71 72 73)=data7
  210. movaps xmm0, xmm7
  211. movaps xmm5, xmm6
  212. subps xmm7, xmm2 ; xmm7=data1-data6=tmp6
  213. subps xmm6, xmm3 ; xmm6=data0-data7=tmp7
  214. addps xmm0, xmm2 ; xmm0=data1+data6=tmp1
  215. addps xmm5, xmm3 ; xmm5=data0+data7=tmp0
  216. movaps xmm2, XMMWORD [wk(0)] ; xmm2=(22 23 32 33)
  217. movaps xmm3, XMMWORD [wk(1)] ; xmm3=(42 43 52 53)
  218. movaps XMMWORD [wk(0)], xmm7 ; wk(0)=tmp6
  219. movaps XMMWORD [wk(1)], xmm6 ; wk(1)=tmp7
  220. movaps xmm7, xmm4 ; transpose coefficients(phase 2)
  221. unpcklps2 xmm4, xmm2 ; xmm4=(20 21 22 23)=data2
  222. unpckhps2 xmm7, xmm2 ; xmm7=(30 31 32 33)=data3
  223. movaps xmm6, xmm1 ; transpose coefficients(phase 2)
  224. unpcklps2 xmm1, xmm3 ; xmm1=(40 41 42 43)=data4
  225. unpckhps2 xmm6, xmm3 ; xmm6=(50 51 52 53)=data5
  226. movaps xmm2, xmm7
  227. movaps xmm3, xmm4
  228. addps xmm7, xmm1 ; xmm7=data3+data4=tmp3
  229. addps xmm4, xmm6 ; xmm4=data2+data5=tmp2
  230. subps xmm2, xmm1 ; xmm2=data3-data4=tmp4
  231. subps xmm3, xmm6 ; xmm3=data2-data5=tmp5
  232. ; -- Even part
  233. movaps xmm1, xmm5
  234. movaps xmm6, xmm0
  235. subps xmm5, xmm7 ; xmm5=tmp13
  236. subps xmm0, xmm4 ; xmm0=tmp12
  237. addps xmm1, xmm7 ; xmm1=tmp10
  238. addps xmm6, xmm4 ; xmm6=tmp11
  239. addps xmm0, xmm5
  240. mulps xmm0, [GOTOFF(ebx,PD_0_707)] ; xmm0=z1
  241. movaps xmm7, xmm1
  242. movaps xmm4, xmm5
  243. subps xmm1, xmm6 ; xmm1=data4
  244. subps xmm5, xmm0 ; xmm5=data6
  245. addps xmm7, xmm6 ; xmm7=data0
  246. addps xmm4, xmm0 ; xmm4=data2
  247. movaps XMMWORD [XMMBLOCK(4,0,edx,SIZEOF_FAST_FLOAT)], xmm1
  248. movaps XMMWORD [XMMBLOCK(6,0,edx,SIZEOF_FAST_FLOAT)], xmm5
  249. movaps XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)], xmm7
  250. movaps XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)], xmm4
  251. ; -- Odd part
  252. movaps xmm6, XMMWORD [wk(0)] ; xmm6=tmp6
  253. movaps xmm0, XMMWORD [wk(1)] ; xmm0=tmp7
  254. addps xmm2, xmm3 ; xmm2=tmp10
  255. addps xmm3, xmm6 ; xmm3=tmp11
  256. addps xmm6, xmm0 ; xmm6=tmp12, xmm0=tmp7
  257. mulps xmm3, [GOTOFF(ebx,PD_0_707)] ; xmm3=z3
  258. movaps xmm1, xmm2 ; xmm1=tmp10
  259. subps xmm2, xmm6
  260. mulps xmm2, [GOTOFF(ebx,PD_0_382)] ; xmm2=z5
  261. mulps xmm1, [GOTOFF(ebx,PD_0_541)] ; xmm1=MULTIPLY(tmp10,FIX_0_541196)
  262. mulps xmm6, [GOTOFF(ebx,PD_1_306)] ; xmm6=MULTIPLY(tmp12,FIX_1_306562)
  263. addps xmm1, xmm2 ; xmm1=z2
  264. addps xmm6, xmm2 ; xmm6=z4
  265. movaps xmm5, xmm0
  266. subps xmm0, xmm3 ; xmm0=z13
  267. addps xmm5, xmm3 ; xmm5=z11
  268. movaps xmm7, xmm0
  269. movaps xmm4, xmm5
  270. subps xmm0, xmm1 ; xmm0=data3
  271. subps xmm5, xmm6 ; xmm5=data7
  272. addps xmm7, xmm1 ; xmm7=data5
  273. addps xmm4, xmm6 ; xmm4=data1
  274. movaps XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)], xmm0
  275. movaps XMMWORD [XMMBLOCK(7,0,edx,SIZEOF_FAST_FLOAT)], xmm5
  276. movaps XMMWORD [XMMBLOCK(5,0,edx,SIZEOF_FAST_FLOAT)], xmm7
  277. movaps XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)], xmm4
  278. add edx, byte 4*SIZEOF_FAST_FLOAT
  279. dec ecx
  280. jnz near .columnloop
  281. ; pop edi ; unused
  282. ; pop esi ; unused
  283. ; pop edx ; need not be preserved
  284. ; pop ecx ; need not be preserved
  285. POPPIC ebx
  286. mov esp, ebp ; esp <- aligned ebp
  287. pop esp ; esp <- original ebp
  288. pop ebp
  289. ret
  290. ; For some reason, the OS X linker does not honor the request to align the
  291. ; segment unless we do this.
  292. align 32