jfdctflt-sse.asm 14 KB

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