You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1109 lines
29KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm: x264asm abstraction layer
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2012 x264 project
  5. ;*
  6. ;* Authors: Loren Merritt <lorenm@u.washington.edu>
  7. ;* Anton Mitrofanov <BugMaster@narod.ru>
  8. ;* Jason Garrett-Glaser <darkshikari@gmail.com>
  9. ;* Henrik Gramner <hengar-6@student.ltu.se>
  10. ;*
  11. ;* Permission to use, copy, modify, and/or distribute this software for any
  12. ;* purpose with or without fee is hereby granted, provided that the above
  13. ;* copyright notice and this permission notice appear in all copies.
  14. ;*
  15. ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  16. ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  17. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  18. ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ;*****************************************************************************
  23. ; This is a header file for the x264ASM assembly language, which uses
  24. ; NASM/YASM syntax combined with a large number of macros to provide easy
  25. ; abstraction between different calling conventions (x86_32, win64, linux64).
  26. ; It also has various other useful features to simplify writing the kind of
  27. ; DSP functions that are most often used in x264.
  28. ; Unlike the rest of x264, this file is available under an ISC license, as it
  29. ; has significant usefulness outside of x264 and we want it to be available
  30. ; to the largest audience possible. Of course, if you modify it for your own
  31. ; purposes to add a new feature, we strongly encourage contributing a patch
  32. ; as this feature might be useful for others as well. Send patches or ideas
  33. ; to x264-devel@videolan.org .
  34. %define program_name ff
  35. %define UNIX64 0
  36. %define WIN64 0
  37. %if ARCH_X86_64
  38. %ifidn __OUTPUT_FORMAT__,win32
  39. %define WIN64 1
  40. %elifidn __OUTPUT_FORMAT__,win64
  41. %define WIN64 1
  42. %else
  43. %define UNIX64 1
  44. %endif
  45. %endif
  46. %ifdef PREFIX
  47. %define mangle(x) _ %+ x
  48. %else
  49. %define mangle(x) x
  50. %endif
  51. ; FIXME: All of the 64bit asm functions that take a stride as an argument
  52. ; via register, assume that the high dword of that register is filled with 0.
  53. ; This is true in practice (since we never do any 64bit arithmetic on strides,
  54. ; and x264's strides are all positive), but is not guaranteed by the ABI.
  55. ; Name of the .rodata section.
  56. ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
  57. ; so use a different read-only section.
  58. %macro SECTION_RODATA 0-1 16
  59. %ifidn __OUTPUT_FORMAT__,macho64
  60. SECTION .text align=%1
  61. %elifidn __OUTPUT_FORMAT__,macho
  62. SECTION .text align=%1
  63. fakegot:
  64. %elifidn __OUTPUT_FORMAT__,aout
  65. section .text
  66. %else
  67. SECTION .rodata align=%1
  68. %endif
  69. %endmacro
  70. ; aout does not support align=
  71. %macro SECTION_TEXT 0-1 16
  72. %ifidn __OUTPUT_FORMAT__,aout
  73. SECTION .text
  74. %else
  75. SECTION .text align=%1
  76. %endif
  77. %endmacro
  78. %if WIN64
  79. %define PIC
  80. %elif ARCH_X86_64 == 0
  81. ; x86_32 doesn't require PIC.
  82. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  83. ; the code contains a few textrels, so we'll skip that complexity.
  84. %undef PIC
  85. %endif
  86. %ifdef PIC
  87. default rel
  88. %endif
  89. ; Always use long nops (reduces 0x90 spam in disassembly on x86_32)
  90. CPU amdnop
  91. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  92. ; Currently this works only for leaf functions which load all their arguments
  93. ; into registers at the start, and make no other use of the stack. Luckily that
  94. ; covers most of x264's asm.
  95. ; PROLOGUE:
  96. ; %1 = number of arguments. loads them from stack if needed.
  97. ; %2 = number of registers used. pushes callee-saved regs if needed.
  98. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  99. ; %4 = list of names to define to registers
  100. ; PROLOGUE can also be invoked by adding the same options to cglobal
  101. ; e.g.
  102. ; cglobal foo, 2,3,0, dst, src, tmp
  103. ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
  104. ; TODO Some functions can use some args directly from the stack. If they're the
  105. ; last args then you can just not declare them, but if they're in the middle
  106. ; we need more flexible macro.
  107. ; RET:
  108. ; Pops anything that was pushed by PROLOGUE, and returns.
  109. ; REP_RET:
  110. ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
  111. ; which are slow when a normal ret follows a branch.
  112. ; registers:
  113. ; rN and rNq are the native-size register holding function argument N
  114. ; rNd, rNw, rNb are dword, word, and byte size
  115. ; rNm is the original location of arg N (a register or on the stack), dword
  116. ; rNmp is native size
  117. %macro DECLARE_REG 5-6
  118. %define r%1q %2
  119. %define r%1d %3
  120. %define r%1w %4
  121. %define r%1b %5
  122. %if %0 == 5
  123. %define r%1m %3
  124. %define r%1mp %2
  125. %elif ARCH_X86_64 ; memory
  126. %define r%1m [rsp + stack_offset + %6]
  127. %define r%1mp qword r %+ %1m
  128. %else
  129. %define r%1m [esp + stack_offset + %6]
  130. %define r%1mp dword r %+ %1m
  131. %endif
  132. %define r%1 %2
  133. %endmacro
  134. %macro DECLARE_REG_SIZE 2
  135. %define r%1q r%1
  136. %define e%1q r%1
  137. %define r%1d e%1
  138. %define e%1d e%1
  139. %define r%1w %1
  140. %define e%1w %1
  141. %define r%1b %2
  142. %define e%1b %2
  143. %if ARCH_X86_64 == 0
  144. %define r%1 e%1
  145. %endif
  146. %endmacro
  147. DECLARE_REG_SIZE ax, al
  148. DECLARE_REG_SIZE bx, bl
  149. DECLARE_REG_SIZE cx, cl
  150. DECLARE_REG_SIZE dx, dl
  151. DECLARE_REG_SIZE si, sil
  152. DECLARE_REG_SIZE di, dil
  153. DECLARE_REG_SIZE bp, bpl
  154. ; t# defines for when per-arch register allocation is more complex than just function arguments
  155. %macro DECLARE_REG_TMP 1-*
  156. %assign %%i 0
  157. %rep %0
  158. CAT_XDEFINE t, %%i, r%1
  159. %assign %%i %%i+1
  160. %rotate 1
  161. %endrep
  162. %endmacro
  163. %macro DECLARE_REG_TMP_SIZE 0-*
  164. %rep %0
  165. %define t%1q t%1 %+ q
  166. %define t%1d t%1 %+ d
  167. %define t%1w t%1 %+ w
  168. %define t%1b t%1 %+ b
  169. %rotate 1
  170. %endrep
  171. %endmacro
  172. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
  173. %if ARCH_X86_64
  174. %define gprsize 8
  175. %else
  176. %define gprsize 4
  177. %endif
  178. %macro PUSH 1
  179. push %1
  180. %assign stack_offset stack_offset+gprsize
  181. %endmacro
  182. %macro POP 1
  183. pop %1
  184. %assign stack_offset stack_offset-gprsize
  185. %endmacro
  186. %macro PUSH_IF_USED 1-*
  187. %rep %0
  188. %if %1 < regs_used
  189. PUSH r%1
  190. %endif
  191. %rotate 1
  192. %endrep
  193. %endmacro
  194. %macro POP_IF_USED 1-*
  195. %rep %0
  196. %if %1 < regs_used
  197. pop r%1
  198. %endif
  199. %rotate 1
  200. %endrep
  201. %endmacro
  202. %macro LOAD_IF_USED 1-*
  203. %rep %0
  204. %if %1 < num_args
  205. mov r%1, r %+ %1 %+ mp
  206. %endif
  207. %rotate 1
  208. %endrep
  209. %endmacro
  210. %macro SUB 2
  211. sub %1, %2
  212. %ifidn %1, rsp
  213. %assign stack_offset stack_offset+(%2)
  214. %endif
  215. %endmacro
  216. %macro ADD 2
  217. add %1, %2
  218. %ifidn %1, rsp
  219. %assign stack_offset stack_offset-(%2)
  220. %endif
  221. %endmacro
  222. %macro movifnidn 2
  223. %ifnidn %1, %2
  224. mov %1, %2
  225. %endif
  226. %endmacro
  227. %macro movsxdifnidn 2
  228. %ifnidn %1, %2
  229. movsxd %1, %2
  230. %endif
  231. %endmacro
  232. %macro ASSERT 1
  233. %if (%1) == 0
  234. %error assert failed
  235. %endif
  236. %endmacro
  237. %macro DEFINE_ARGS 0-*
  238. %ifdef n_arg_names
  239. %assign %%i 0
  240. %rep n_arg_names
  241. CAT_UNDEF arg_name %+ %%i, q
  242. CAT_UNDEF arg_name %+ %%i, d
  243. CAT_UNDEF arg_name %+ %%i, w
  244. CAT_UNDEF arg_name %+ %%i, b
  245. CAT_UNDEF arg_name %+ %%i, m
  246. CAT_UNDEF arg_name %+ %%i, mp
  247. CAT_UNDEF arg_name, %%i
  248. %assign %%i %%i+1
  249. %endrep
  250. %endif
  251. %xdefine %%stack_offset stack_offset
  252. %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
  253. %assign %%i 0
  254. %rep %0
  255. %xdefine %1q r %+ %%i %+ q
  256. %xdefine %1d r %+ %%i %+ d
  257. %xdefine %1w r %+ %%i %+ w
  258. %xdefine %1b r %+ %%i %+ b
  259. %xdefine %1m r %+ %%i %+ m
  260. %xdefine %1mp r %+ %%i %+ mp
  261. CAT_XDEFINE arg_name, %%i, %1
  262. %assign %%i %%i+1
  263. %rotate 1
  264. %endrep
  265. %xdefine stack_offset %%stack_offset
  266. %assign n_arg_names %0
  267. %endmacro
  268. %if WIN64 ; Windows x64 ;=================================================
  269. DECLARE_REG 0, rcx, ecx, cx, cl
  270. DECLARE_REG 1, rdx, edx, dx, dl
  271. DECLARE_REG 2, R8, R8D, R8W, R8B
  272. DECLARE_REG 3, R9, R9D, R9W, R9B
  273. DECLARE_REG 4, R10, R10D, R10W, R10B, 40
  274. DECLARE_REG 5, R11, R11D, R11W, R11B, 48
  275. DECLARE_REG 6, rax, eax, ax, al, 56
  276. DECLARE_REG 7, rdi, edi, di, dil, 64
  277. DECLARE_REG 8, rsi, esi, si, sil, 72
  278. DECLARE_REG 9, rbx, ebx, bx, bl, 80
  279. DECLARE_REG 10, rbp, ebp, bp, bpl, 88
  280. DECLARE_REG 11, R12, R12D, R12W, R12B, 96
  281. DECLARE_REG 12, R13, R13D, R13W, R13B, 104
  282. DECLARE_REG 13, R14, R14D, R14W, R14B, 112
  283. DECLARE_REG 14, R15, R15D, R15W, R15B, 120
  284. %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
  285. %assign num_args %1
  286. %assign regs_used %2
  287. ASSERT regs_used >= num_args
  288. ASSERT regs_used <= 15
  289. PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
  290. %if mmsize == 8
  291. %assign xmm_regs_used 0
  292. %else
  293. WIN64_SPILL_XMM %3
  294. %endif
  295. LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  296. DEFINE_ARGS %4
  297. %endmacro
  298. %macro WIN64_SPILL_XMM 1
  299. %assign xmm_regs_used %1
  300. ASSERT xmm_regs_used <= 16
  301. %if xmm_regs_used > 6
  302. SUB rsp, (xmm_regs_used-6)*16+16
  303. %assign %%i xmm_regs_used
  304. %rep (xmm_regs_used-6)
  305. %assign %%i %%i-1
  306. movdqa [rsp + (%%i-6)*16+(~stack_offset&8)], xmm %+ %%i
  307. %endrep
  308. %endif
  309. %endmacro
  310. %macro WIN64_RESTORE_XMM_INTERNAL 1
  311. %if xmm_regs_used > 6
  312. %assign %%i xmm_regs_used
  313. %rep (xmm_regs_used-6)
  314. %assign %%i %%i-1
  315. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+(~stack_offset&8)]
  316. %endrep
  317. add %1, (xmm_regs_used-6)*16+16
  318. %endif
  319. %endmacro
  320. %macro WIN64_RESTORE_XMM 1
  321. WIN64_RESTORE_XMM_INTERNAL %1
  322. %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
  323. %assign xmm_regs_used 0
  324. %endmacro
  325. %macro RET 0
  326. WIN64_RESTORE_XMM_INTERNAL rsp
  327. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  328. %if mmsize == 32
  329. vzeroupper
  330. %endif
  331. ret
  332. %endmacro
  333. %macro REP_RET 0
  334. %if regs_used > 7 || xmm_regs_used > 6 || mmsize == 32
  335. RET
  336. %else
  337. rep ret
  338. %endif
  339. %endmacro
  340. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  341. DECLARE_REG 0, rdi, edi, di, dil
  342. DECLARE_REG 1, rsi, esi, si, sil
  343. DECLARE_REG 2, rdx, edx, dx, dl
  344. DECLARE_REG 3, rcx, ecx, cx, cl
  345. DECLARE_REG 4, R8, R8D, R8W, R8B
  346. DECLARE_REG 5, R9, R9D, R9W, R9B
  347. DECLARE_REG 6, rax, eax, ax, al, 8
  348. DECLARE_REG 7, R10, R10D, R10W, R10B, 16
  349. DECLARE_REG 8, R11, R11D, R11W, R11B, 24
  350. DECLARE_REG 9, rbx, ebx, bx, bl, 32
  351. DECLARE_REG 10, rbp, ebp, bp, bpl, 40
  352. DECLARE_REG 11, R12, R12D, R12W, R12B, 48
  353. DECLARE_REG 12, R13, R13D, R13W, R13B, 56
  354. DECLARE_REG 13, R14, R14D, R14W, R14B, 64
  355. DECLARE_REG 14, R15, R15D, R15W, R15B, 72
  356. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  357. %assign num_args %1
  358. %assign regs_used %2
  359. ASSERT regs_used >= num_args
  360. ASSERT regs_used <= 15
  361. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  362. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  363. DEFINE_ARGS %4
  364. %endmacro
  365. %macro RET 0
  366. POP_IF_USED 14, 13, 12, 11, 10, 9
  367. %if mmsize == 32
  368. vzeroupper
  369. %endif
  370. ret
  371. %endmacro
  372. %macro REP_RET 0
  373. %if regs_used > 9 || mmsize == 32
  374. RET
  375. %else
  376. rep ret
  377. %endif
  378. %endmacro
  379. %else ; X86_32 ;==============================================================
  380. DECLARE_REG 0, eax, eax, ax, al, 4
  381. DECLARE_REG 1, ecx, ecx, cx, cl, 8
  382. DECLARE_REG 2, edx, edx, dx, dl, 12
  383. DECLARE_REG 3, ebx, ebx, bx, bl, 16
  384. DECLARE_REG 4, esi, esi, si, null, 20
  385. DECLARE_REG 5, edi, edi, di, null, 24
  386. DECLARE_REG 6, ebp, ebp, bp, null, 28
  387. %define rsp esp
  388. %macro DECLARE_ARG 1-*
  389. %rep %0
  390. %define r%1m [esp + stack_offset + 4*%1 + 4]
  391. %define r%1mp dword r%1m
  392. %rotate 1
  393. %endrep
  394. %endmacro
  395. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  396. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  397. %assign num_args %1
  398. %assign regs_used %2
  399. %if regs_used > 7
  400. %assign regs_used 7
  401. %endif
  402. ASSERT regs_used >= num_args
  403. PUSH_IF_USED 3, 4, 5, 6
  404. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  405. DEFINE_ARGS %4
  406. %endmacro
  407. %macro RET 0
  408. POP_IF_USED 6, 5, 4, 3
  409. %if mmsize == 32
  410. vzeroupper
  411. %endif
  412. ret
  413. %endmacro
  414. %macro REP_RET 0
  415. %if regs_used > 3 || mmsize == 32
  416. RET
  417. %else
  418. rep ret
  419. %endif
  420. %endmacro
  421. %endif ;======================================================================
  422. %if WIN64 == 0
  423. %macro WIN64_SPILL_XMM 1
  424. %endmacro
  425. %macro WIN64_RESTORE_XMM 1
  426. %endmacro
  427. %endif
  428. ;=============================================================================
  429. ; arch-independent part
  430. ;=============================================================================
  431. %assign function_align 16
  432. ; Begin a function.
  433. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  434. ; subsequent uses of the function name automatically refer to the mangled version.
  435. ; Appends cpuflags to the function name if cpuflags has been specified.
  436. %macro cglobal 1-2+ ; name, [PROLOGUE args]
  437. %if %0 == 1
  438. cglobal_internal %1 %+ SUFFIX
  439. %else
  440. cglobal_internal %1 %+ SUFFIX, %2
  441. %endif
  442. %endmacro
  443. %macro cglobal_internal 1-2+
  444. %ifndef cglobaled_%1
  445. %xdefine %1 mangle(program_name %+ _ %+ %1)
  446. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  447. CAT_XDEFINE cglobaled_, %1, 1
  448. %endif
  449. %xdefine current_function %1
  450. %ifidn __OUTPUT_FORMAT__,elf
  451. global %1:function hidden
  452. %else
  453. global %1
  454. %endif
  455. align function_align
  456. %1:
  457. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  458. %assign stack_offset 0
  459. %if %0 > 1
  460. PROLOGUE %2
  461. %endif
  462. %endmacro
  463. %macro cextern 1
  464. %xdefine %1 mangle(program_name %+ _ %+ %1)
  465. CAT_XDEFINE cglobaled_, %1, 1
  466. extern %1
  467. %endmacro
  468. ; like cextern, but without the prefix
  469. %macro cextern_naked 1
  470. %xdefine %1 mangle(%1)
  471. CAT_XDEFINE cglobaled_, %1, 1
  472. extern %1
  473. %endmacro
  474. %macro const 2+
  475. %xdefine %1 mangle(program_name %+ _ %+ %1)
  476. global %1
  477. %1: %2
  478. %endmacro
  479. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  480. ; executable by default.
  481. %ifidn __OUTPUT_FORMAT__,elf
  482. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  483. %endif
  484. ; cpuflags
  485. %assign cpuflags_mmx (1<<0)
  486. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  487. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  488. %assign cpuflags_3dnow2 (1<<3) | cpuflags_3dnow
  489. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  490. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  491. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  492. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  493. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  494. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  495. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  496. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  497. %assign cpuflags_xop (1<<12)| cpuflags_avx
  498. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  499. %assign cpuflags_cache32 (1<<16)
  500. %assign cpuflags_cache64 (1<<17)
  501. %assign cpuflags_slowctz (1<<18)
  502. %assign cpuflags_lzcnt (1<<19)
  503. %assign cpuflags_misalign (1<<20)
  504. %assign cpuflags_aligned (1<<21) ; not a cpu feature, but a function variant
  505. %assign cpuflags_atom (1<<22)
  506. %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
  507. %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
  508. ; Takes up to 2 cpuflags from the above list.
  509. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  510. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  511. %macro INIT_CPUFLAGS 0-2
  512. CPU amdnop
  513. %if %0 >= 1
  514. %xdefine cpuname %1
  515. %assign cpuflags cpuflags_%1
  516. %if %0 >= 2
  517. %xdefine cpuname %1_%2
  518. %assign cpuflags cpuflags | cpuflags_%2
  519. %endif
  520. %xdefine SUFFIX _ %+ cpuname
  521. %if cpuflag(avx)
  522. %assign avx_enabled 1
  523. %endif
  524. %if mmsize == 16 && notcpuflag(sse2)
  525. %define mova movaps
  526. %define movu movups
  527. %define movnta movntps
  528. %endif
  529. %if cpuflag(aligned)
  530. %define movu mova
  531. %elifidn %1, sse3
  532. %define movu lddqu
  533. %endif
  534. %if notcpuflag(mmx2)
  535. CPU basicnop
  536. %endif
  537. %else
  538. %xdefine SUFFIX
  539. %undef cpuname
  540. %undef cpuflags
  541. %endif
  542. %endmacro
  543. ; merge mmx and sse*
  544. %macro CAT_XDEFINE 3
  545. %xdefine %1%2 %3
  546. %endmacro
  547. %macro CAT_UNDEF 2
  548. %undef %1%2
  549. %endmacro
  550. %macro INIT_MMX 0-1+
  551. %assign avx_enabled 0
  552. %define RESET_MM_PERMUTATION INIT_MMX %1
  553. %define mmsize 8
  554. %define num_mmregs 8
  555. %define mova movq
  556. %define movu movq
  557. %define movh movd
  558. %define movnta movntq
  559. %assign %%i 0
  560. %rep 8
  561. CAT_XDEFINE m, %%i, mm %+ %%i
  562. CAT_XDEFINE nmm, %%i, %%i
  563. %assign %%i %%i+1
  564. %endrep
  565. %rep 8
  566. CAT_UNDEF m, %%i
  567. CAT_UNDEF nmm, %%i
  568. %assign %%i %%i+1
  569. %endrep
  570. INIT_CPUFLAGS %1
  571. %endmacro
  572. %macro INIT_XMM 0-1+
  573. %assign avx_enabled 0
  574. %define RESET_MM_PERMUTATION INIT_XMM %1
  575. %define mmsize 16
  576. %define num_mmregs 8
  577. %if ARCH_X86_64
  578. %define num_mmregs 16
  579. %endif
  580. %define mova movdqa
  581. %define movu movdqu
  582. %define movh movq
  583. %define movnta movntdq
  584. %assign %%i 0
  585. %rep num_mmregs
  586. CAT_XDEFINE m, %%i, xmm %+ %%i
  587. CAT_XDEFINE nxmm, %%i, %%i
  588. %assign %%i %%i+1
  589. %endrep
  590. INIT_CPUFLAGS %1
  591. %endmacro
  592. ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
  593. %macro INIT_AVX 0
  594. INIT_XMM
  595. %assign avx_enabled 1
  596. %define PALIGNR PALIGNR_SSSE3
  597. %define RESET_MM_PERMUTATION INIT_AVX
  598. %endmacro
  599. %macro INIT_YMM 0-1+
  600. %assign avx_enabled 1
  601. %define RESET_MM_PERMUTATION INIT_YMM %1
  602. %define mmsize 32
  603. %define num_mmregs 8
  604. %if ARCH_X86_64
  605. %define num_mmregs 16
  606. %endif
  607. %define mova vmovaps
  608. %define movu vmovups
  609. %undef movh
  610. %define movnta vmovntps
  611. %assign %%i 0
  612. %rep num_mmregs
  613. CAT_XDEFINE m, %%i, ymm %+ %%i
  614. CAT_XDEFINE nymm, %%i, %%i
  615. %assign %%i %%i+1
  616. %endrep
  617. INIT_CPUFLAGS %1
  618. %endmacro
  619. INIT_XMM
  620. ; I often want to use macros that permute their arguments. e.g. there's no
  621. ; efficient way to implement butterfly or transpose or dct without swapping some
  622. ; arguments.
  623. ;
  624. ; I would like to not have to manually keep track of the permutations:
  625. ; If I insert a permutation in the middle of a function, it should automatically
  626. ; change everything that follows. For more complex macros I may also have multiple
  627. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  628. ;
  629. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  630. ; permutes its arguments. It's equivalent to exchanging the contents of the
  631. ; registers, except that this way you exchange the register names instead, so it
  632. ; doesn't cost any cycles.
  633. %macro PERMUTE 2-* ; takes a list of pairs to swap
  634. %rep %0/2
  635. %xdefine tmp%2 m%2
  636. %xdefine ntmp%2 nm%2
  637. %rotate 2
  638. %endrep
  639. %rep %0/2
  640. %xdefine m%1 tmp%2
  641. %xdefine nm%1 ntmp%2
  642. %undef tmp%2
  643. %undef ntmp%2
  644. %rotate 2
  645. %endrep
  646. %endmacro
  647. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  648. %rep %0-1
  649. %ifdef m%1
  650. %xdefine tmp m%1
  651. %xdefine m%1 m%2
  652. %xdefine m%2 tmp
  653. CAT_XDEFINE n, m%1, %1
  654. CAT_XDEFINE n, m%2, %2
  655. %else
  656. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  657. ; Be careful using this mode in nested macros though, as in some cases there may be
  658. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  659. %xdefine %%n1 n %+ %1
  660. %xdefine %%n2 n %+ %2
  661. %xdefine tmp m %+ %%n1
  662. CAT_XDEFINE m, %%n1, m %+ %%n2
  663. CAT_XDEFINE m, %%n2, tmp
  664. CAT_XDEFINE n, m %+ %%n1, %%n1
  665. CAT_XDEFINE n, m %+ %%n2, %%n2
  666. %endif
  667. %undef tmp
  668. %rotate 1
  669. %endrep
  670. %endmacro
  671. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  672. ; calls to that function will automatically load the permutation, so values can
  673. ; be returned in mmregs.
  674. %macro SAVE_MM_PERMUTATION 0-1
  675. %if %0
  676. %xdefine %%f %1_m
  677. %else
  678. %xdefine %%f current_function %+ _m
  679. %endif
  680. %assign %%i 0
  681. %rep num_mmregs
  682. CAT_XDEFINE %%f, %%i, m %+ %%i
  683. %assign %%i %%i+1
  684. %endrep
  685. %endmacro
  686. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  687. %ifdef %1_m0
  688. %assign %%i 0
  689. %rep num_mmregs
  690. CAT_XDEFINE m, %%i, %1_m %+ %%i
  691. CAT_XDEFINE n, m %+ %%i, %%i
  692. %assign %%i %%i+1
  693. %endrep
  694. %endif
  695. %endmacro
  696. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  697. %macro call 1
  698. call_internal %1, %1 %+ SUFFIX
  699. %endmacro
  700. %macro call_internal 2
  701. %xdefine %%i %1
  702. %ifndef cglobaled_%1
  703. %ifdef cglobaled_%2
  704. %xdefine %%i %2
  705. %endif
  706. %endif
  707. call %%i
  708. LOAD_MM_PERMUTATION %%i
  709. %endmacro
  710. ; Substitutions that reduce instruction size but are functionally equivalent
  711. %macro add 2
  712. %ifnum %2
  713. %if %2==128
  714. sub %1, -128
  715. %else
  716. add %1, %2
  717. %endif
  718. %else
  719. add %1, %2
  720. %endif
  721. %endmacro
  722. %macro sub 2
  723. %ifnum %2
  724. %if %2==128
  725. add %1, -128
  726. %else
  727. sub %1, %2
  728. %endif
  729. %else
  730. sub %1, %2
  731. %endif
  732. %endmacro
  733. ;=============================================================================
  734. ; AVX abstraction layer
  735. ;=============================================================================
  736. %assign i 0
  737. %rep 16
  738. %if i < 8
  739. CAT_XDEFINE sizeofmm, i, 8
  740. %endif
  741. CAT_XDEFINE sizeofxmm, i, 16
  742. CAT_XDEFINE sizeofymm, i, 32
  743. %assign i i+1
  744. %endrep
  745. %undef i
  746. ;%1 == instruction
  747. ;%2 == 1 if float, 0 if int
  748. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  749. ;%4 == number of operands given
  750. ;%5+: operands
  751. %macro RUN_AVX_INSTR 6-7+
  752. %ifid %5
  753. %define %%size sizeof%5
  754. %else
  755. %define %%size mmsize
  756. %endif
  757. %if %%size==32
  758. %if %0 >= 7
  759. v%1 %5, %6, %7
  760. %else
  761. v%1 %5, %6
  762. %endif
  763. %else
  764. %if %%size==8
  765. %define %%regmov movq
  766. %elif %2
  767. %define %%regmov movaps
  768. %else
  769. %define %%regmov movdqa
  770. %endif
  771. %if %4>=3+%3
  772. %ifnidn %5, %6
  773. %if avx_enabled && sizeof%5==16
  774. v%1 %5, %6, %7
  775. %else
  776. %%regmov %5, %6
  777. %1 %5, %7
  778. %endif
  779. %else
  780. %1 %5, %7
  781. %endif
  782. %elif %3
  783. %1 %5, %6, %7
  784. %else
  785. %1 %5, %6
  786. %endif
  787. %endif
  788. %endmacro
  789. ; 3arg AVX ops with a memory arg can only have it in src2,
  790. ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
  791. ; So, if the op is symmetric and the wrong one is memory, swap them.
  792. %macro RUN_AVX_INSTR1 8
  793. %assign %%swap 0
  794. %if avx_enabled
  795. %ifnid %6
  796. %assign %%swap 1
  797. %endif
  798. %elifnidn %5, %6
  799. %ifnid %7
  800. %assign %%swap 1
  801. %endif
  802. %endif
  803. %if %%swap && %3 == 0 && %8 == 1
  804. RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
  805. %else
  806. RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
  807. %endif
  808. %endmacro
  809. ;%1 == instruction
  810. ;%2 == 1 if float, 0 if int
  811. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 3-operand (xmm, xmm, xmm)
  812. ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
  813. %macro AVX_INSTR 4
  814. %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
  815. %ifidn %3, fnord
  816. RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
  817. %elifidn %4, fnord
  818. RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
  819. %elifidn %5, fnord
  820. RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
  821. %else
  822. RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
  823. %endif
  824. %endmacro
  825. %endmacro
  826. AVX_INSTR addpd, 1, 0, 1
  827. AVX_INSTR addps, 1, 0, 1
  828. AVX_INSTR addsd, 1, 0, 1
  829. AVX_INSTR addss, 1, 0, 1
  830. AVX_INSTR addsubpd, 1, 0, 0
  831. AVX_INSTR addsubps, 1, 0, 0
  832. AVX_INSTR andpd, 1, 0, 1
  833. AVX_INSTR andps, 1, 0, 1
  834. AVX_INSTR andnpd, 1, 0, 0
  835. AVX_INSTR andnps, 1, 0, 0
  836. AVX_INSTR blendpd, 1, 0, 0
  837. AVX_INSTR blendps, 1, 0, 0
  838. AVX_INSTR blendvpd, 1, 0, 0
  839. AVX_INSTR blendvps, 1, 0, 0
  840. AVX_INSTR cmppd, 1, 0, 0
  841. AVX_INSTR cmpps, 1, 0, 0
  842. AVX_INSTR cmpsd, 1, 0, 0
  843. AVX_INSTR cmpss, 1, 0, 0
  844. AVX_INSTR cvtdq2ps, 1, 0, 0
  845. AVX_INSTR cvtps2dq, 1, 0, 0
  846. AVX_INSTR divpd, 1, 0, 0
  847. AVX_INSTR divps, 1, 0, 0
  848. AVX_INSTR divsd, 1, 0, 0
  849. AVX_INSTR divss, 1, 0, 0
  850. AVX_INSTR dppd, 1, 1, 0
  851. AVX_INSTR dpps, 1, 1, 0
  852. AVX_INSTR haddpd, 1, 0, 0
  853. AVX_INSTR haddps, 1, 0, 0
  854. AVX_INSTR hsubpd, 1, 0, 0
  855. AVX_INSTR hsubps, 1, 0, 0
  856. AVX_INSTR maxpd, 1, 0, 1
  857. AVX_INSTR maxps, 1, 0, 1
  858. AVX_INSTR maxsd, 1, 0, 1
  859. AVX_INSTR maxss, 1, 0, 1
  860. AVX_INSTR minpd, 1, 0, 1
  861. AVX_INSTR minps, 1, 0, 1
  862. AVX_INSTR minsd, 1, 0, 1
  863. AVX_INSTR minss, 1, 0, 1
  864. AVX_INSTR movhlps, 1, 0, 0
  865. AVX_INSTR movlhps, 1, 0, 0
  866. AVX_INSTR movsd, 1, 0, 0
  867. AVX_INSTR movss, 1, 0, 0
  868. AVX_INSTR mpsadbw, 0, 1, 0
  869. AVX_INSTR mulpd, 1, 0, 1
  870. AVX_INSTR mulps, 1, 0, 1
  871. AVX_INSTR mulsd, 1, 0, 1
  872. AVX_INSTR mulss, 1, 0, 1
  873. AVX_INSTR orpd, 1, 0, 1
  874. AVX_INSTR orps, 1, 0, 1
  875. AVX_INSTR packsswb, 0, 0, 0
  876. AVX_INSTR packssdw, 0, 0, 0
  877. AVX_INSTR packuswb, 0, 0, 0
  878. AVX_INSTR packusdw, 0, 0, 0
  879. AVX_INSTR paddb, 0, 0, 1
  880. AVX_INSTR paddw, 0, 0, 1
  881. AVX_INSTR paddd, 0, 0, 1
  882. AVX_INSTR paddq, 0, 0, 1
  883. AVX_INSTR paddsb, 0, 0, 1
  884. AVX_INSTR paddsw, 0, 0, 1
  885. AVX_INSTR paddusb, 0, 0, 1
  886. AVX_INSTR paddusw, 0, 0, 1
  887. AVX_INSTR palignr, 0, 1, 0
  888. AVX_INSTR pand, 0, 0, 1
  889. AVX_INSTR pandn, 0, 0, 0
  890. AVX_INSTR pavgb, 0, 0, 1
  891. AVX_INSTR pavgw, 0, 0, 1
  892. AVX_INSTR pblendvb, 0, 0, 0
  893. AVX_INSTR pblendw, 0, 1, 0
  894. AVX_INSTR pcmpestri, 0, 0, 0
  895. AVX_INSTR pcmpestrm, 0, 0, 0
  896. AVX_INSTR pcmpistri, 0, 0, 0
  897. AVX_INSTR pcmpistrm, 0, 0, 0
  898. AVX_INSTR pcmpeqb, 0, 0, 1
  899. AVX_INSTR pcmpeqw, 0, 0, 1
  900. AVX_INSTR pcmpeqd, 0, 0, 1
  901. AVX_INSTR pcmpeqq, 0, 0, 1
  902. AVX_INSTR pcmpgtb, 0, 0, 0
  903. AVX_INSTR pcmpgtw, 0, 0, 0
  904. AVX_INSTR pcmpgtd, 0, 0, 0
  905. AVX_INSTR pcmpgtq, 0, 0, 0
  906. AVX_INSTR phaddw, 0, 0, 0
  907. AVX_INSTR phaddd, 0, 0, 0
  908. AVX_INSTR phaddsw, 0, 0, 0
  909. AVX_INSTR phsubw, 0, 0, 0
  910. AVX_INSTR phsubd, 0, 0, 0
  911. AVX_INSTR phsubsw, 0, 0, 0
  912. AVX_INSTR pmaddwd, 0, 0, 1
  913. AVX_INSTR pmaddubsw, 0, 0, 0
  914. AVX_INSTR pmaxsb, 0, 0, 1
  915. AVX_INSTR pmaxsw, 0, 0, 1
  916. AVX_INSTR pmaxsd, 0, 0, 1
  917. AVX_INSTR pmaxub, 0, 0, 1
  918. AVX_INSTR pmaxuw, 0, 0, 1
  919. AVX_INSTR pmaxud, 0, 0, 1
  920. AVX_INSTR pminsb, 0, 0, 1
  921. AVX_INSTR pminsw, 0, 0, 1
  922. AVX_INSTR pminsd, 0, 0, 1
  923. AVX_INSTR pminub, 0, 0, 1
  924. AVX_INSTR pminuw, 0, 0, 1
  925. AVX_INSTR pminud, 0, 0, 1
  926. AVX_INSTR pmulhuw, 0, 0, 1
  927. AVX_INSTR pmulhrsw, 0, 0, 1
  928. AVX_INSTR pmulhw, 0, 0, 1
  929. AVX_INSTR pmullw, 0, 0, 1
  930. AVX_INSTR pmulld, 0, 0, 1
  931. AVX_INSTR pmuludq, 0, 0, 1
  932. AVX_INSTR pmuldq, 0, 0, 1
  933. AVX_INSTR por, 0, 0, 1
  934. AVX_INSTR psadbw, 0, 0, 1
  935. AVX_INSTR pshufb, 0, 0, 0
  936. AVX_INSTR psignb, 0, 0, 0
  937. AVX_INSTR psignw, 0, 0, 0
  938. AVX_INSTR psignd, 0, 0, 0
  939. AVX_INSTR psllw, 0, 0, 0
  940. AVX_INSTR pslld, 0, 0, 0
  941. AVX_INSTR psllq, 0, 0, 0
  942. AVX_INSTR pslldq, 0, 0, 0
  943. AVX_INSTR psraw, 0, 0, 0
  944. AVX_INSTR psrad, 0, 0, 0
  945. AVX_INSTR psrlw, 0, 0, 0
  946. AVX_INSTR psrld, 0, 0, 0
  947. AVX_INSTR psrlq, 0, 0, 0
  948. AVX_INSTR psrldq, 0, 0, 0
  949. AVX_INSTR psubb, 0, 0, 0
  950. AVX_INSTR psubw, 0, 0, 0
  951. AVX_INSTR psubd, 0, 0, 0
  952. AVX_INSTR psubq, 0, 0, 0
  953. AVX_INSTR psubsb, 0, 0, 0
  954. AVX_INSTR psubsw, 0, 0, 0
  955. AVX_INSTR psubusb, 0, 0, 0
  956. AVX_INSTR psubusw, 0, 0, 0
  957. AVX_INSTR punpckhbw, 0, 0, 0
  958. AVX_INSTR punpckhwd, 0, 0, 0
  959. AVX_INSTR punpckhdq, 0, 0, 0
  960. AVX_INSTR punpckhqdq, 0, 0, 0
  961. AVX_INSTR punpcklbw, 0, 0, 0
  962. AVX_INSTR punpcklwd, 0, 0, 0
  963. AVX_INSTR punpckldq, 0, 0, 0
  964. AVX_INSTR punpcklqdq, 0, 0, 0
  965. AVX_INSTR pxor, 0, 0, 1
  966. AVX_INSTR shufps, 1, 1, 0
  967. AVX_INSTR subpd, 1, 0, 0
  968. AVX_INSTR subps, 1, 0, 0
  969. AVX_INSTR subsd, 1, 0, 0
  970. AVX_INSTR subss, 1, 0, 0
  971. AVX_INSTR unpckhpd, 1, 0, 0
  972. AVX_INSTR unpckhps, 1, 0, 0
  973. AVX_INSTR unpcklpd, 1, 0, 0
  974. AVX_INSTR unpcklps, 1, 0, 0
  975. AVX_INSTR xorpd, 1, 0, 1
  976. AVX_INSTR xorps, 1, 0, 1
  977. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  978. AVX_INSTR pfadd, 1, 0, 1
  979. AVX_INSTR pfsub, 1, 0, 0
  980. AVX_INSTR pfmul, 1, 0, 1
  981. ; base-4 constants for shuffles
  982. %assign i 0
  983. %rep 256
  984. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  985. %if j < 10
  986. CAT_XDEFINE q000, j, i
  987. %elif j < 100
  988. CAT_XDEFINE q00, j, i
  989. %elif j < 1000
  990. CAT_XDEFINE q0, j, i
  991. %else
  992. CAT_XDEFINE q, j, i
  993. %endif
  994. %assign i i+1
  995. %endrep
  996. %undef i
  997. %undef j
  998. %macro FMA_INSTR 3
  999. %macro %1 4-7 %1, %2, %3
  1000. %if cpuflag(xop)
  1001. v%5 %1, %2, %3, %4
  1002. %else
  1003. %6 %1, %2, %3
  1004. %7 %1, %4
  1005. %endif
  1006. %endmacro
  1007. %endmacro
  1008. FMA_INSTR pmacsdd, pmulld, paddd
  1009. FMA_INSTR pmacsww, pmullw, paddw
  1010. FMA_INSTR pmadcswd, pmaddwd, paddd