fft_mmx.asm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. ;******************************************************************************
  2. ;* FFT transform with SSE/3DNow optimizations
  3. ;* Copyright (c) 2008 Loren Merritt
  4. ;*
  5. ;* This file is part of FFmpeg.
  6. ;*
  7. ;* FFmpeg is free software; you can redistribute it and/or
  8. ;* modify it under the terms of the GNU Lesser General Public
  9. ;* License as published by the Free Software Foundation; either
  10. ;* version 2.1 of the License, or (at your option) any later version.
  11. ;*
  12. ;* FFmpeg is distributed in the hope that it will be useful,
  13. ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;* Lesser General Public License for more details.
  16. ;*
  17. ;* You should have received a copy of the GNU Lesser General Public
  18. ;* License along with FFmpeg; if not, write to the Free Software
  19. ;* 51, Inc., Foundation Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. ;******************************************************************************
  21. ; These functions are not individually interchangeable with the C versions.
  22. ; While C takes arrays of FFTComplex, SSE/3DNow leave intermediate results
  23. ; in blocks as conventient to the vector size.
  24. ; i.e. {4x real, 4x imaginary, 4x real, ...} (or 2x respectively)
  25. %include "x86inc.asm"
  26. SECTION_RODATA
  27. %define M_SQRT1_2 0.70710678118654752440
  28. ps_root2: times 4 dd M_SQRT1_2
  29. ps_root2mppm: dd -M_SQRT1_2, M_SQRT1_2, M_SQRT1_2, -M_SQRT1_2
  30. ps_m1p1: dd 1<<31, 0
  31. %assign i 16
  32. %rep 13
  33. cextern ff_cos_ %+ i
  34. %assign i i<<1
  35. %endrep
  36. %ifdef ARCH_X86_64
  37. %define pointer dq
  38. %else
  39. %define pointer dd
  40. %endif
  41. %macro IF0 1+
  42. %endmacro
  43. %macro IF1 1+
  44. %1
  45. %endmacro
  46. section .text align=16
  47. %macro T2_3DN 4 ; z0, z1, mem0, mem1
  48. mova %1, %3
  49. mova %2, %1
  50. pfadd %1, %4
  51. pfsub %2, %4
  52. %endmacro
  53. %macro T4_3DN 6 ; z0, z1, z2, z3, tmp0, tmp1
  54. mova %5, %3
  55. pfsub %3, %4
  56. pfadd %5, %4 ; {t6,t5}
  57. pxor %3, [ps_m1p1 GLOBAL] ; {t8,t7}
  58. mova %6, %1
  59. pswapd %3, %3
  60. pfadd %1, %5 ; {r0,i0}
  61. pfsub %6, %5 ; {r2,i2}
  62. mova %4, %2
  63. pfadd %2, %3 ; {r1,i1}
  64. pfsub %4, %3 ; {r3,i3}
  65. SWAP %3, %6
  66. %endmacro
  67. ; in: %1={r0,i0,r1,i1} %2={r2,i2,r3,i3}
  68. ; out: %1={r0,r1,r2,r3} %2={i0,i1,i2,i3}
  69. %macro T4_SSE 3
  70. mova %3, %1
  71. shufps %1, %2, 0x64 ; {r0,i0,r3,i2}
  72. shufps %3, %2, 0xce ; {r1,i1,r2,i3}
  73. mova %2, %1
  74. addps %1, %3 ; {t1,t2,t6,t5}
  75. subps %2, %3 ; {t3,t4,t8,t7}
  76. mova %3, %1
  77. shufps %1, %2, 0x44 ; {t1,t2,t3,t4}
  78. shufps %3, %2, 0xbe ; {t6,t5,t7,t8}
  79. mova %2, %1
  80. addps %1, %3 ; {r0,i0,r1,i1}
  81. subps %2, %3 ; {r2,i2,r3,i3}
  82. mova %3, %1
  83. shufps %1, %2, 0x88 ; {r0,r1,r2,r3}
  84. shufps %3, %2, 0xdd ; {i0,i1,i2,i3}
  85. SWAP %2, %3
  86. %endmacro
  87. %macro T8_SSE 6 ; r0,i0,r1,i1,t0,t1
  88. mova %5, %3
  89. shufps %3, %4, 0x44 ; {r4,i4,r6,i6}
  90. shufps %5, %4, 0xee ; {r5,i5,r7,i7}
  91. mova %6, %3
  92. subps %3, %5 ; {r5,i5,r7,i7}
  93. addps %6, %5 ; {t1,t2,t3,t4}
  94. mova %5, %3
  95. shufps %5, %5, 0xb1 ; {i5,r5,i7,r7}
  96. mulps %3, [ps_root2mppm GLOBAL] ; {-r5,i5,r7,-i7}
  97. mulps %5, [ps_root2 GLOBAL]
  98. addps %3, %5 ; {t8,t7,ta,t9}
  99. mova %5, %6
  100. shufps %6, %3, 0x36 ; {t3,t2,t9,t8}
  101. shufps %5, %3, 0x9c ; {t1,t4,t7,ta}
  102. mova %3, %6
  103. addps %6, %5 ; {t1,t2,t9,ta}
  104. subps %3, %5 ; {t6,t5,tc,tb}
  105. mova %5, %6
  106. shufps %6, %3, 0xd8 ; {t1,t9,t5,tb}
  107. shufps %5, %3, 0x8d ; {t2,ta,t6,tc}
  108. mova %3, %1
  109. mova %4, %2
  110. addps %1, %6 ; {r0,r1,r2,r3}
  111. addps %2, %5 ; {i0,i1,i2,i3}
  112. subps %3, %6 ; {r4,r5,r6,r7}
  113. subps %4, %5 ; {i4,i5,i6,i7}
  114. %endmacro
  115. ; scheduled for cpu-bound sizes
  116. %macro PASS_SMALL 3 ; (to load m4-m7), wre, wim
  117. IF%1 mova m4, Z(4)
  118. IF%1 mova m5, Z(5)
  119. mova m0, %2 ; wre
  120. mova m2, m4
  121. mova m1, %3 ; wim
  122. mova m3, m5
  123. mulps m2, m0 ; r2*wre
  124. IF%1 mova m6, Z(6)
  125. mulps m3, m1 ; i2*wim
  126. IF%1 mova m7, Z(7)
  127. mulps m4, m1 ; r2*wim
  128. mulps m5, m0 ; i2*wre
  129. addps m2, m3 ; r2*wre + i2*wim
  130. mova m3, m1
  131. mulps m1, m6 ; r3*wim
  132. subps m5, m4 ; i2*wre - r2*wim
  133. mova m4, m0
  134. mulps m3, m7 ; i3*wim
  135. mulps m4, m6 ; r3*wre
  136. mulps m0, m7 ; i3*wre
  137. subps m4, m3 ; r3*wre - i3*wim
  138. mova m3, Z(0)
  139. addps m0, m1 ; i3*wre + r3*wim
  140. mova m1, m4
  141. addps m4, m2 ; t5
  142. subps m1, m2 ; t3
  143. subps m3, m4 ; r2
  144. addps m4, Z(0) ; r0
  145. mova m6, Z(2)
  146. mova Z(4), m3
  147. mova Z(0), m4
  148. mova m3, m5
  149. subps m5, m0 ; t4
  150. mova m4, m6
  151. subps m6, m5 ; r3
  152. addps m5, m4 ; r1
  153. mova Z(6), m6
  154. mova Z(2), m5
  155. mova m2, Z(3)
  156. addps m3, m0 ; t6
  157. subps m2, m1 ; i3
  158. mova m7, Z(1)
  159. addps m1, Z(3) ; i1
  160. mova Z(7), m2
  161. mova Z(3), m1
  162. mova m4, m7
  163. subps m7, m3 ; i2
  164. addps m3, m4 ; i0
  165. mova Z(5), m7
  166. mova Z(1), m3
  167. %endmacro
  168. ; scheduled to avoid store->load aliasing
  169. %macro PASS_BIG 1 ; (!interleave)
  170. mova m4, Z(4) ; r2
  171. mova m5, Z(5) ; i2
  172. mova m2, m4
  173. mova m0, [wq] ; wre
  174. mova m3, m5
  175. mova m1, [wq+o1q] ; wim
  176. mulps m2, m0 ; r2*wre
  177. mova m6, Z(6) ; r3
  178. mulps m3, m1 ; i2*wim
  179. mova m7, Z(7) ; i3
  180. mulps m4, m1 ; r2*wim
  181. mulps m5, m0 ; i2*wre
  182. addps m2, m3 ; r2*wre + i2*wim
  183. mova m3, m1
  184. mulps m1, m6 ; r3*wim
  185. subps m5, m4 ; i2*wre - r2*wim
  186. mova m4, m0
  187. mulps m3, m7 ; i3*wim
  188. mulps m4, m6 ; r3*wre
  189. mulps m0, m7 ; i3*wre
  190. subps m4, m3 ; r3*wre - i3*wim
  191. mova m3, Z(0)
  192. addps m0, m1 ; i3*wre + r3*wim
  193. mova m1, m4
  194. addps m4, m2 ; t5
  195. subps m1, m2 ; t3
  196. subps m3, m4 ; r2
  197. addps m4, Z(0) ; r0
  198. mova m6, Z(2)
  199. mova Z(4), m3
  200. mova Z(0), m4
  201. mova m3, m5
  202. subps m5, m0 ; t4
  203. mova m4, m6
  204. subps m6, m5 ; r3
  205. addps m5, m4 ; r1
  206. IF%1 mova Z(6), m6
  207. IF%1 mova Z(2), m5
  208. mova m2, Z(3)
  209. addps m3, m0 ; t6
  210. subps m2, m1 ; i3
  211. mova m7, Z(1)
  212. addps m1, Z(3) ; i1
  213. IF%1 mova Z(7), m2
  214. IF%1 mova Z(3), m1
  215. mova m4, m7
  216. subps m7, m3 ; i2
  217. addps m3, m4 ; i0
  218. IF%1 mova Z(5), m7
  219. IF%1 mova Z(1), m3
  220. %if %1==0
  221. mova m4, m5 ; r1
  222. mova m0, m6 ; r3
  223. unpcklps m5, m1
  224. unpckhps m4, m1
  225. unpcklps m6, m2
  226. unpckhps m0, m2
  227. mova m1, Z(0)
  228. mova m2, Z(4)
  229. mova Z(2), m5
  230. mova Z(3), m4
  231. mova Z(6), m6
  232. mova Z(7), m0
  233. mova m5, m1 ; r0
  234. mova m4, m2 ; r2
  235. unpcklps m1, m3
  236. unpckhps m5, m3
  237. unpcklps m2, m7
  238. unpckhps m4, m7
  239. mova Z(0), m1
  240. mova Z(1), m5
  241. mova Z(4), m2
  242. mova Z(5), m4
  243. %endif
  244. %endmacro
  245. %macro PUNPCK 3
  246. mova %3, %1
  247. punpckldq %1, %2
  248. punpckhdq %3, %2
  249. %endmacro
  250. INIT_XMM
  251. %define Z(x) [r0+mmsize*x]
  252. align 16
  253. fft4_sse:
  254. mova m0, Z(0)
  255. mova m1, Z(1)
  256. T4_SSE m0, m1, m2
  257. mova Z(0), m0
  258. mova Z(1), m1
  259. ret
  260. align 16
  261. fft8_sse:
  262. mova m0, Z(0)
  263. mova m1, Z(1)
  264. T4_SSE m0, m1, m2
  265. mova m2, Z(2)
  266. mova m3, Z(3)
  267. T8_SSE m0, m1, m2, m3, m4, m5
  268. mova Z(0), m0
  269. mova Z(1), m1
  270. mova Z(2), m2
  271. mova Z(3), m3
  272. ret
  273. align 16
  274. fft16_sse:
  275. mova m0, Z(0)
  276. mova m1, Z(1)
  277. T4_SSE m0, m1, m2
  278. mova m2, Z(2)
  279. mova m3, Z(3)
  280. T8_SSE m0, m1, m2, m3, m4, m5
  281. mova m4, Z(4)
  282. mova m5, Z(5)
  283. mova Z(0), m0
  284. mova Z(1), m1
  285. mova Z(2), m2
  286. mova Z(3), m3
  287. T4_SSE m4, m5, m6
  288. mova m6, Z(6)
  289. mova m7, Z(7)
  290. T4_SSE m6, m7, m0
  291. PASS_SMALL 0, [ff_cos_16 GLOBAL], [ff_cos_16+16 GLOBAL]
  292. ret
  293. INIT_MMX
  294. %macro FFT48_3DN 1
  295. align 16
  296. fft4%1:
  297. T2_3DN m0, m1, Z(0), Z(1)
  298. mova m2, Z(2)
  299. mova m3, Z(3)
  300. T4_3DN m0, m1, m2, m3, m4, m5
  301. PUNPCK m0, m1, m4
  302. PUNPCK m2, m3, m5
  303. mova Z(0), m0
  304. mova Z(1), m4
  305. mova Z(2), m2
  306. mova Z(3), m5
  307. ret
  308. align 16
  309. fft8%1:
  310. T2_3DN m0, m1, Z(0), Z(1)
  311. mova m2, Z(2)
  312. mova m3, Z(3)
  313. T4_3DN m0, m1, m2, m3, m4, m5
  314. mova Z(0), m0
  315. mova Z(2), m2
  316. T2_3DN m4, m5, Z(4), Z(5)
  317. T2_3DN m6, m7, Z(6), Z(7)
  318. pswapd m0, m5
  319. pswapd m2, m7
  320. pxor m0, [ps_m1p1 GLOBAL]
  321. pxor m2, [ps_m1p1 GLOBAL]
  322. pfsub m5, m0
  323. pfadd m7, m2
  324. pfmul m5, [ps_root2 GLOBAL]
  325. pfmul m7, [ps_root2 GLOBAL]
  326. T4_3DN m1, m3, m5, m7, m0, m2
  327. mova Z(5), m5
  328. mova Z(7), m7
  329. mova m0, Z(0)
  330. mova m2, Z(2)
  331. T4_3DN m0, m2, m4, m6, m5, m7
  332. PUNPCK m0, m1, m5
  333. PUNPCK m2, m3, m7
  334. mova Z(0), m0
  335. mova Z(1), m5
  336. mova Z(2), m2
  337. mova Z(3), m7
  338. PUNPCK m4, Z(5), m5
  339. PUNPCK m6, Z(7), m7
  340. mova Z(4), m4
  341. mova Z(5), m5
  342. mova Z(6), m6
  343. mova Z(7), m7
  344. ret
  345. %endmacro
  346. FFT48_3DN _3dn2
  347. %macro pswapd 2
  348. %ifidn %1, %2
  349. movd [r0+12], %1
  350. punpckhdq %1, [r0+8]
  351. %else
  352. movq %1, %2
  353. psrlq %1, 32
  354. punpckldq %1, %2
  355. %endif
  356. %endmacro
  357. FFT48_3DN _3dn
  358. %define Z(x) [zq + o1q*(x&6)*((x/6)^1) + o3q*(x/6) + mmsize*(x&1)]
  359. %macro DECL_PASS 2+ ; name, payload
  360. align 16
  361. %1:
  362. DEFINE_ARGS z, w, n, o1, o3
  363. lea o3q, [nq*3]
  364. lea o1q, [nq*8]
  365. shl o3q, 4
  366. .loop:
  367. %2
  368. add zq, mmsize*2
  369. add wq, mmsize
  370. sub nd, mmsize/8
  371. jg .loop
  372. rep ret
  373. %endmacro
  374. INIT_XMM
  375. DECL_PASS pass_sse, PASS_BIG 1
  376. DECL_PASS pass_interleave_sse, PASS_BIG 0
  377. INIT_MMX
  378. %define mulps pfmul
  379. %define addps pfadd
  380. %define subps pfsub
  381. %define unpcklps punpckldq
  382. %define unpckhps punpckhdq
  383. DECL_PASS pass_3dn, PASS_SMALL 1, [wq], [wq+o1q]
  384. DECL_PASS pass_interleave_3dn, PASS_BIG 0
  385. %define pass_3dn2 pass_3dn
  386. %define pass_interleave_3dn2 pass_interleave_3dn
  387. %macro DECL_FFT 2-3 ; nbits, cpu, suffix
  388. %xdefine list_of_fft fft4%2, fft8%2
  389. %if %1==5
  390. %xdefine list_of_fft list_of_fft, fft16%2
  391. %endif
  392. %assign n 1<<%1
  393. %rep 17-%1
  394. %assign n2 n/2
  395. %assign n4 n/4
  396. %xdefine list_of_fft list_of_fft, fft %+ n %+ %3%2
  397. align 16
  398. fft %+ n %+ %3%2:
  399. call fft %+ n2 %+ %2
  400. add r0, n*4 - (n&(-2<<%1))
  401. call fft %+ n4 %+ %2
  402. add r0, n*2 - (n2&(-2<<%1))
  403. call fft %+ n4 %+ %2
  404. sub r0, n*6 + (n2&(-2<<%1))
  405. lea r1, [ff_cos_ %+ n GLOBAL]
  406. mov r2d, n4/2
  407. jmp pass%3%2
  408. %assign n n*2
  409. %endrep
  410. %undef n
  411. %ifidn __OUTPUT_FORMAT__,macho64
  412. section .rodata
  413. %endif
  414. align 8
  415. dispatch_tab%3%2: pointer list_of_fft
  416. section .text
  417. ; On x86_32, this function does the register saving and restoring for all of fft.
  418. ; The others pass args in registers and don't spill anything.
  419. cglobal fft_dispatch%3%2, 2,5,0, z, nbits
  420. lea r2, [dispatch_tab%3%2 GLOBAL]
  421. mov r2, [r2 + (nbitsq-2)*gprsize]
  422. call r2
  423. RET
  424. %endmacro ; DECL_FFT
  425. DECL_FFT 5, _sse
  426. DECL_FFT 5, _sse, _interleave
  427. DECL_FFT 4, _3dn
  428. DECL_FFT 4, _3dn, _interleave
  429. DECL_FFT 4, _3dn2
  430. DECL_FFT 4, _3dn2, _interleave