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.

1096 lines
28KB

  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. ret
  329. %endmacro
  330. %macro REP_RET 0
  331. %if regs_used > 7 || xmm_regs_used > 6
  332. RET
  333. %else
  334. rep ret
  335. %endif
  336. %endmacro
  337. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  338. DECLARE_REG 0, rdi, edi, di, dil
  339. DECLARE_REG 1, rsi, esi, si, sil
  340. DECLARE_REG 2, rdx, edx, dx, dl
  341. DECLARE_REG 3, rcx, ecx, cx, cl
  342. DECLARE_REG 4, R8, R8D, R8W, R8B
  343. DECLARE_REG 5, R9, R9D, R9W, R9B
  344. DECLARE_REG 6, rax, eax, ax, al, 8
  345. DECLARE_REG 7, R10, R10D, R10W, R10B, 16
  346. DECLARE_REG 8, R11, R11D, R11W, R11B, 24
  347. DECLARE_REG 9, rbx, ebx, bx, bl, 32
  348. DECLARE_REG 10, rbp, ebp, bp, bpl, 40
  349. DECLARE_REG 11, R12, R12D, R12W, R12B, 48
  350. DECLARE_REG 12, R13, R13D, R13W, R13B, 56
  351. DECLARE_REG 13, R14, R14D, R14W, R14B, 64
  352. DECLARE_REG 14, R15, R15D, R15W, R15B, 72
  353. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  354. %assign num_args %1
  355. %assign regs_used %2
  356. ASSERT regs_used >= num_args
  357. ASSERT regs_used <= 15
  358. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  359. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  360. DEFINE_ARGS %4
  361. %endmacro
  362. %macro RET 0
  363. POP_IF_USED 14, 13, 12, 11, 10, 9
  364. ret
  365. %endmacro
  366. %macro REP_RET 0
  367. %if regs_used > 9
  368. RET
  369. %else
  370. rep ret
  371. %endif
  372. %endmacro
  373. %else ; X86_32 ;==============================================================
  374. DECLARE_REG 0, eax, eax, ax, al, 4
  375. DECLARE_REG 1, ecx, ecx, cx, cl, 8
  376. DECLARE_REG 2, edx, edx, dx, dl, 12
  377. DECLARE_REG 3, ebx, ebx, bx, bl, 16
  378. DECLARE_REG 4, esi, esi, si, null, 20
  379. DECLARE_REG 5, edi, edi, di, null, 24
  380. DECLARE_REG 6, ebp, ebp, bp, null, 28
  381. %define rsp esp
  382. %macro DECLARE_ARG 1-*
  383. %rep %0
  384. %define r%1m [esp + stack_offset + 4*%1 + 4]
  385. %define r%1mp dword r%1m
  386. %rotate 1
  387. %endrep
  388. %endmacro
  389. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  390. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  391. %assign num_args %1
  392. %assign regs_used %2
  393. %if regs_used > 7
  394. %assign regs_used 7
  395. %endif
  396. ASSERT regs_used >= num_args
  397. PUSH_IF_USED 3, 4, 5, 6
  398. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  399. DEFINE_ARGS %4
  400. %endmacro
  401. %macro RET 0
  402. POP_IF_USED 6, 5, 4, 3
  403. ret
  404. %endmacro
  405. %macro REP_RET 0
  406. %if regs_used > 3
  407. RET
  408. %else
  409. rep ret
  410. %endif
  411. %endmacro
  412. %endif ;======================================================================
  413. %if WIN64 == 0
  414. %macro WIN64_SPILL_XMM 1
  415. %endmacro
  416. %macro WIN64_RESTORE_XMM 1
  417. %endmacro
  418. %endif
  419. ;=============================================================================
  420. ; arch-independent part
  421. ;=============================================================================
  422. %assign function_align 16
  423. ; Begin a function.
  424. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  425. ; subsequent uses of the function name automatically refer to the mangled version.
  426. ; Appends cpuflags to the function name if cpuflags has been specified.
  427. %macro cglobal 1-2+ ; name, [PROLOGUE args]
  428. %if %0 == 1
  429. cglobal_internal %1 %+ SUFFIX
  430. %else
  431. cglobal_internal %1 %+ SUFFIX, %2
  432. %endif
  433. %endmacro
  434. %macro cglobal_internal 1-2+
  435. %ifndef cglobaled_%1
  436. %xdefine %1 mangle(program_name %+ _ %+ %1)
  437. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  438. CAT_XDEFINE cglobaled_, %1, 1
  439. %endif
  440. %xdefine current_function %1
  441. %ifidn __OUTPUT_FORMAT__,elf
  442. global %1:function hidden
  443. %else
  444. global %1
  445. %endif
  446. align function_align
  447. %1:
  448. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  449. %assign stack_offset 0
  450. %if %0 > 1
  451. PROLOGUE %2
  452. %endif
  453. %endmacro
  454. %macro cextern 1
  455. %xdefine %1 mangle(program_name %+ _ %+ %1)
  456. CAT_XDEFINE cglobaled_, %1, 1
  457. extern %1
  458. %endmacro
  459. ; like cextern, but without the prefix
  460. %macro cextern_naked 1
  461. %xdefine %1 mangle(%1)
  462. CAT_XDEFINE cglobaled_, %1, 1
  463. extern %1
  464. %endmacro
  465. %macro const 2+
  466. %xdefine %1 mangle(program_name %+ _ %+ %1)
  467. global %1
  468. %1: %2
  469. %endmacro
  470. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  471. ; executable by default.
  472. %ifidn __OUTPUT_FORMAT__,elf
  473. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  474. %endif
  475. ; cpuflags
  476. %assign cpuflags_mmx (1<<0)
  477. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  478. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  479. %assign cpuflags_3dnow2 (1<<3) | cpuflags_3dnow
  480. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  481. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  482. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  483. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  484. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  485. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  486. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  487. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  488. %assign cpuflags_xop (1<<12)| cpuflags_avx
  489. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  490. %assign cpuflags_cache32 (1<<16)
  491. %assign cpuflags_cache64 (1<<17)
  492. %assign cpuflags_slowctz (1<<18)
  493. %assign cpuflags_lzcnt (1<<19)
  494. %assign cpuflags_misalign (1<<20)
  495. %assign cpuflags_aligned (1<<21) ; not a cpu feature, but a function variant
  496. %assign cpuflags_atom (1<<22)
  497. %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
  498. %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
  499. ; Takes up to 2 cpuflags from the above list.
  500. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  501. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  502. %macro INIT_CPUFLAGS 0-2
  503. %if %0 >= 1
  504. %xdefine cpuname %1
  505. %assign cpuflags cpuflags_%1
  506. %if %0 >= 2
  507. %xdefine cpuname %1_%2
  508. %assign cpuflags cpuflags | cpuflags_%2
  509. %endif
  510. %xdefine SUFFIX _ %+ cpuname
  511. %if cpuflag(avx)
  512. %assign avx_enabled 1
  513. %endif
  514. %if mmsize == 16 && notcpuflag(sse2)
  515. %define mova movaps
  516. %define movu movups
  517. %define movnta movntps
  518. %endif
  519. %if cpuflag(aligned)
  520. %define movu mova
  521. %elifidn %1, sse3
  522. %define movu lddqu
  523. %endif
  524. %else
  525. %xdefine SUFFIX
  526. %undef cpuname
  527. %undef cpuflags
  528. %endif
  529. %endmacro
  530. ; merge mmx and sse*
  531. %macro CAT_XDEFINE 3
  532. %xdefine %1%2 %3
  533. %endmacro
  534. %macro CAT_UNDEF 2
  535. %undef %1%2
  536. %endmacro
  537. %macro INIT_MMX 0-1+
  538. %assign avx_enabled 0
  539. %define RESET_MM_PERMUTATION INIT_MMX %1
  540. %define mmsize 8
  541. %define num_mmregs 8
  542. %define mova movq
  543. %define movu movq
  544. %define movh movd
  545. %define movnta movntq
  546. %assign %%i 0
  547. %rep 8
  548. CAT_XDEFINE m, %%i, mm %+ %%i
  549. CAT_XDEFINE nmm, %%i, %%i
  550. %assign %%i %%i+1
  551. %endrep
  552. %rep 8
  553. CAT_UNDEF m, %%i
  554. CAT_UNDEF nmm, %%i
  555. %assign %%i %%i+1
  556. %endrep
  557. INIT_CPUFLAGS %1
  558. %endmacro
  559. %macro INIT_XMM 0-1+
  560. %assign avx_enabled 0
  561. %define RESET_MM_PERMUTATION INIT_XMM %1
  562. %define mmsize 16
  563. %define num_mmregs 8
  564. %if ARCH_X86_64
  565. %define num_mmregs 16
  566. %endif
  567. %define mova movdqa
  568. %define movu movdqu
  569. %define movh movq
  570. %define movnta movntdq
  571. %assign %%i 0
  572. %rep num_mmregs
  573. CAT_XDEFINE m, %%i, xmm %+ %%i
  574. CAT_XDEFINE nxmm, %%i, %%i
  575. %assign %%i %%i+1
  576. %endrep
  577. INIT_CPUFLAGS %1
  578. %endmacro
  579. ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
  580. %macro INIT_AVX 0
  581. INIT_XMM
  582. %assign avx_enabled 1
  583. %define PALIGNR PALIGNR_SSSE3
  584. %define RESET_MM_PERMUTATION INIT_AVX
  585. %endmacro
  586. %macro INIT_YMM 0-1+
  587. %assign avx_enabled 1
  588. %define RESET_MM_PERMUTATION INIT_YMM %1
  589. %define mmsize 32
  590. %define num_mmregs 8
  591. %if ARCH_X86_64
  592. %define num_mmregs 16
  593. %endif
  594. %define mova vmovaps
  595. %define movu vmovups
  596. %undef movh
  597. %define movnta vmovntps
  598. %assign %%i 0
  599. %rep num_mmregs
  600. CAT_XDEFINE m, %%i, ymm %+ %%i
  601. CAT_XDEFINE nymm, %%i, %%i
  602. %assign %%i %%i+1
  603. %endrep
  604. INIT_CPUFLAGS %1
  605. %endmacro
  606. INIT_XMM
  607. ; I often want to use macros that permute their arguments. e.g. there's no
  608. ; efficient way to implement butterfly or transpose or dct without swapping some
  609. ; arguments.
  610. ;
  611. ; I would like to not have to manually keep track of the permutations:
  612. ; If I insert a permutation in the middle of a function, it should automatically
  613. ; change everything that follows. For more complex macros I may also have multiple
  614. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  615. ;
  616. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  617. ; permutes its arguments. It's equivalent to exchanging the contents of the
  618. ; registers, except that this way you exchange the register names instead, so it
  619. ; doesn't cost any cycles.
  620. %macro PERMUTE 2-* ; takes a list of pairs to swap
  621. %rep %0/2
  622. %xdefine tmp%2 m%2
  623. %xdefine ntmp%2 nm%2
  624. %rotate 2
  625. %endrep
  626. %rep %0/2
  627. %xdefine m%1 tmp%2
  628. %xdefine nm%1 ntmp%2
  629. %undef tmp%2
  630. %undef ntmp%2
  631. %rotate 2
  632. %endrep
  633. %endmacro
  634. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  635. %rep %0-1
  636. %ifdef m%1
  637. %xdefine tmp m%1
  638. %xdefine m%1 m%2
  639. %xdefine m%2 tmp
  640. CAT_XDEFINE n, m%1, %1
  641. CAT_XDEFINE n, m%2, %2
  642. %else
  643. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  644. ; Be careful using this mode in nested macros though, as in some cases there may be
  645. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  646. %xdefine %%n1 n %+ %1
  647. %xdefine %%n2 n %+ %2
  648. %xdefine tmp m %+ %%n1
  649. CAT_XDEFINE m, %%n1, m %+ %%n2
  650. CAT_XDEFINE m, %%n2, tmp
  651. CAT_XDEFINE n, m %+ %%n1, %%n1
  652. CAT_XDEFINE n, m %+ %%n2, %%n2
  653. %endif
  654. %undef tmp
  655. %rotate 1
  656. %endrep
  657. %endmacro
  658. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  659. ; calls to that function will automatically load the permutation, so values can
  660. ; be returned in mmregs.
  661. %macro SAVE_MM_PERMUTATION 0-1
  662. %if %0
  663. %xdefine %%f %1_m
  664. %else
  665. %xdefine %%f current_function %+ _m
  666. %endif
  667. %assign %%i 0
  668. %rep num_mmregs
  669. CAT_XDEFINE %%f, %%i, m %+ %%i
  670. %assign %%i %%i+1
  671. %endrep
  672. %endmacro
  673. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  674. %ifdef %1_m0
  675. %assign %%i 0
  676. %rep num_mmregs
  677. CAT_XDEFINE m, %%i, %1_m %+ %%i
  678. CAT_XDEFINE n, m %+ %%i, %%i
  679. %assign %%i %%i+1
  680. %endrep
  681. %endif
  682. %endmacro
  683. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  684. %macro call 1
  685. call_internal %1, %1 %+ SUFFIX
  686. %endmacro
  687. %macro call_internal 2
  688. %xdefine %%i %1
  689. %ifndef cglobaled_%1
  690. %ifdef cglobaled_%2
  691. %xdefine %%i %2
  692. %endif
  693. %endif
  694. call %%i
  695. LOAD_MM_PERMUTATION %%i
  696. %endmacro
  697. ; Substitutions that reduce instruction size but are functionally equivalent
  698. %macro add 2
  699. %ifnum %2
  700. %if %2==128
  701. sub %1, -128
  702. %else
  703. add %1, %2
  704. %endif
  705. %else
  706. add %1, %2
  707. %endif
  708. %endmacro
  709. %macro sub 2
  710. %ifnum %2
  711. %if %2==128
  712. add %1, -128
  713. %else
  714. sub %1, %2
  715. %endif
  716. %else
  717. sub %1, %2
  718. %endif
  719. %endmacro
  720. ;=============================================================================
  721. ; AVX abstraction layer
  722. ;=============================================================================
  723. %assign i 0
  724. %rep 16
  725. %if i < 8
  726. CAT_XDEFINE sizeofmm, i, 8
  727. %endif
  728. CAT_XDEFINE sizeofxmm, i, 16
  729. CAT_XDEFINE sizeofymm, i, 32
  730. %assign i i+1
  731. %endrep
  732. %undef i
  733. ;%1 == instruction
  734. ;%2 == 1 if float, 0 if int
  735. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  736. ;%4 == number of operands given
  737. ;%5+: operands
  738. %macro RUN_AVX_INSTR 6-7+
  739. %ifid %5
  740. %define %%size sizeof%5
  741. %else
  742. %define %%size mmsize
  743. %endif
  744. %if %%size==32
  745. %if %0 >= 7
  746. v%1 %5, %6, %7
  747. %else
  748. v%1 %5, %6
  749. %endif
  750. %else
  751. %if %%size==8
  752. %define %%regmov movq
  753. %elif %2
  754. %define %%regmov movaps
  755. %else
  756. %define %%regmov movdqa
  757. %endif
  758. %if %4>=3+%3
  759. %ifnidn %5, %6
  760. %if avx_enabled && sizeof%5==16
  761. v%1 %5, %6, %7
  762. %else
  763. %%regmov %5, %6
  764. %1 %5, %7
  765. %endif
  766. %else
  767. %1 %5, %7
  768. %endif
  769. %elif %3
  770. %1 %5, %6, %7
  771. %else
  772. %1 %5, %6
  773. %endif
  774. %endif
  775. %endmacro
  776. ; 3arg AVX ops with a memory arg can only have it in src2,
  777. ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
  778. ; So, if the op is symmetric and the wrong one is memory, swap them.
  779. %macro RUN_AVX_INSTR1 8
  780. %assign %%swap 0
  781. %if avx_enabled
  782. %ifnid %6
  783. %assign %%swap 1
  784. %endif
  785. %elifnidn %5, %6
  786. %ifnid %7
  787. %assign %%swap 1
  788. %endif
  789. %endif
  790. %if %%swap && %3 == 0 && %8 == 1
  791. RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
  792. %else
  793. RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
  794. %endif
  795. %endmacro
  796. ;%1 == instruction
  797. ;%2 == 1 if float, 0 if int
  798. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 3-operand (xmm, xmm, xmm)
  799. ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
  800. %macro AVX_INSTR 4
  801. %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
  802. %ifidn %3, fnord
  803. RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
  804. %elifidn %4, fnord
  805. RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
  806. %elifidn %5, fnord
  807. RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
  808. %else
  809. RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
  810. %endif
  811. %endmacro
  812. %endmacro
  813. AVX_INSTR addpd, 1, 0, 1
  814. AVX_INSTR addps, 1, 0, 1
  815. AVX_INSTR addsd, 1, 0, 1
  816. AVX_INSTR addss, 1, 0, 1
  817. AVX_INSTR addsubpd, 1, 0, 0
  818. AVX_INSTR addsubps, 1, 0, 0
  819. AVX_INSTR andpd, 1, 0, 1
  820. AVX_INSTR andps, 1, 0, 1
  821. AVX_INSTR andnpd, 1, 0, 0
  822. AVX_INSTR andnps, 1, 0, 0
  823. AVX_INSTR blendpd, 1, 0, 0
  824. AVX_INSTR blendps, 1, 0, 0
  825. AVX_INSTR blendvpd, 1, 0, 0
  826. AVX_INSTR blendvps, 1, 0, 0
  827. AVX_INSTR cmppd, 1, 0, 0
  828. AVX_INSTR cmpps, 1, 0, 0
  829. AVX_INSTR cmpsd, 1, 0, 0
  830. AVX_INSTR cmpss, 1, 0, 0
  831. AVX_INSTR cvtdq2ps, 1, 0, 0
  832. AVX_INSTR cvtps2dq, 1, 0, 0
  833. AVX_INSTR divpd, 1, 0, 0
  834. AVX_INSTR divps, 1, 0, 0
  835. AVX_INSTR divsd, 1, 0, 0
  836. AVX_INSTR divss, 1, 0, 0
  837. AVX_INSTR dppd, 1, 1, 0
  838. AVX_INSTR dpps, 1, 1, 0
  839. AVX_INSTR haddpd, 1, 0, 0
  840. AVX_INSTR haddps, 1, 0, 0
  841. AVX_INSTR hsubpd, 1, 0, 0
  842. AVX_INSTR hsubps, 1, 0, 0
  843. AVX_INSTR maxpd, 1, 0, 1
  844. AVX_INSTR maxps, 1, 0, 1
  845. AVX_INSTR maxsd, 1, 0, 1
  846. AVX_INSTR maxss, 1, 0, 1
  847. AVX_INSTR minpd, 1, 0, 1
  848. AVX_INSTR minps, 1, 0, 1
  849. AVX_INSTR minsd, 1, 0, 1
  850. AVX_INSTR minss, 1, 0, 1
  851. AVX_INSTR movhlps, 1, 0, 0
  852. AVX_INSTR movlhps, 1, 0, 0
  853. AVX_INSTR movsd, 1, 0, 0
  854. AVX_INSTR movss, 1, 0, 0
  855. AVX_INSTR mpsadbw, 0, 1, 0
  856. AVX_INSTR mulpd, 1, 0, 1
  857. AVX_INSTR mulps, 1, 0, 1
  858. AVX_INSTR mulsd, 1, 0, 1
  859. AVX_INSTR mulss, 1, 0, 1
  860. AVX_INSTR orpd, 1, 0, 1
  861. AVX_INSTR orps, 1, 0, 1
  862. AVX_INSTR packsswb, 0, 0, 0
  863. AVX_INSTR packssdw, 0, 0, 0
  864. AVX_INSTR packuswb, 0, 0, 0
  865. AVX_INSTR packusdw, 0, 0, 0
  866. AVX_INSTR paddb, 0, 0, 1
  867. AVX_INSTR paddw, 0, 0, 1
  868. AVX_INSTR paddd, 0, 0, 1
  869. AVX_INSTR paddq, 0, 0, 1
  870. AVX_INSTR paddsb, 0, 0, 1
  871. AVX_INSTR paddsw, 0, 0, 1
  872. AVX_INSTR paddusb, 0, 0, 1
  873. AVX_INSTR paddusw, 0, 0, 1
  874. AVX_INSTR palignr, 0, 1, 0
  875. AVX_INSTR pand, 0, 0, 1
  876. AVX_INSTR pandn, 0, 0, 0
  877. AVX_INSTR pavgb, 0, 0, 1
  878. AVX_INSTR pavgw, 0, 0, 1
  879. AVX_INSTR pblendvb, 0, 0, 0
  880. AVX_INSTR pblendw, 0, 1, 0
  881. AVX_INSTR pcmpestri, 0, 0, 0
  882. AVX_INSTR pcmpestrm, 0, 0, 0
  883. AVX_INSTR pcmpistri, 0, 0, 0
  884. AVX_INSTR pcmpistrm, 0, 0, 0
  885. AVX_INSTR pcmpeqb, 0, 0, 1
  886. AVX_INSTR pcmpeqw, 0, 0, 1
  887. AVX_INSTR pcmpeqd, 0, 0, 1
  888. AVX_INSTR pcmpeqq, 0, 0, 1
  889. AVX_INSTR pcmpgtb, 0, 0, 0
  890. AVX_INSTR pcmpgtw, 0, 0, 0
  891. AVX_INSTR pcmpgtd, 0, 0, 0
  892. AVX_INSTR pcmpgtq, 0, 0, 0
  893. AVX_INSTR phaddw, 0, 0, 0
  894. AVX_INSTR phaddd, 0, 0, 0
  895. AVX_INSTR phaddsw, 0, 0, 0
  896. AVX_INSTR phsubw, 0, 0, 0
  897. AVX_INSTR phsubd, 0, 0, 0
  898. AVX_INSTR phsubsw, 0, 0, 0
  899. AVX_INSTR pmaddwd, 0, 0, 1
  900. AVX_INSTR pmaddubsw, 0, 0, 0
  901. AVX_INSTR pmaxsb, 0, 0, 1
  902. AVX_INSTR pmaxsw, 0, 0, 1
  903. AVX_INSTR pmaxsd, 0, 0, 1
  904. AVX_INSTR pmaxub, 0, 0, 1
  905. AVX_INSTR pmaxuw, 0, 0, 1
  906. AVX_INSTR pmaxud, 0, 0, 1
  907. AVX_INSTR pminsb, 0, 0, 1
  908. AVX_INSTR pminsw, 0, 0, 1
  909. AVX_INSTR pminsd, 0, 0, 1
  910. AVX_INSTR pminub, 0, 0, 1
  911. AVX_INSTR pminuw, 0, 0, 1
  912. AVX_INSTR pminud, 0, 0, 1
  913. AVX_INSTR pmulhuw, 0, 0, 1
  914. AVX_INSTR pmulhrsw, 0, 0, 1
  915. AVX_INSTR pmulhw, 0, 0, 1
  916. AVX_INSTR pmullw, 0, 0, 1
  917. AVX_INSTR pmulld, 0, 0, 1
  918. AVX_INSTR pmuludq, 0, 0, 1
  919. AVX_INSTR pmuldq, 0, 0, 1
  920. AVX_INSTR por, 0, 0, 1
  921. AVX_INSTR psadbw, 0, 0, 1
  922. AVX_INSTR pshufb, 0, 0, 0
  923. AVX_INSTR psignb, 0, 0, 0
  924. AVX_INSTR psignw, 0, 0, 0
  925. AVX_INSTR psignd, 0, 0, 0
  926. AVX_INSTR psllw, 0, 0, 0
  927. AVX_INSTR pslld, 0, 0, 0
  928. AVX_INSTR psllq, 0, 0, 0
  929. AVX_INSTR pslldq, 0, 0, 0
  930. AVX_INSTR psraw, 0, 0, 0
  931. AVX_INSTR psrad, 0, 0, 0
  932. AVX_INSTR psrlw, 0, 0, 0
  933. AVX_INSTR psrld, 0, 0, 0
  934. AVX_INSTR psrlq, 0, 0, 0
  935. AVX_INSTR psrldq, 0, 0, 0
  936. AVX_INSTR psubb, 0, 0, 0
  937. AVX_INSTR psubw, 0, 0, 0
  938. AVX_INSTR psubd, 0, 0, 0
  939. AVX_INSTR psubq, 0, 0, 0
  940. AVX_INSTR psubsb, 0, 0, 0
  941. AVX_INSTR psubsw, 0, 0, 0
  942. AVX_INSTR psubusb, 0, 0, 0
  943. AVX_INSTR psubusw, 0, 0, 0
  944. AVX_INSTR punpckhbw, 0, 0, 0
  945. AVX_INSTR punpckhwd, 0, 0, 0
  946. AVX_INSTR punpckhdq, 0, 0, 0
  947. AVX_INSTR punpckhqdq, 0, 0, 0
  948. AVX_INSTR punpcklbw, 0, 0, 0
  949. AVX_INSTR punpcklwd, 0, 0, 0
  950. AVX_INSTR punpckldq, 0, 0, 0
  951. AVX_INSTR punpcklqdq, 0, 0, 0
  952. AVX_INSTR pxor, 0, 0, 1
  953. AVX_INSTR shufps, 1, 1, 0
  954. AVX_INSTR subpd, 1, 0, 0
  955. AVX_INSTR subps, 1, 0, 0
  956. AVX_INSTR subsd, 1, 0, 0
  957. AVX_INSTR subss, 1, 0, 0
  958. AVX_INSTR unpckhpd, 1, 0, 0
  959. AVX_INSTR unpckhps, 1, 0, 0
  960. AVX_INSTR unpcklpd, 1, 0, 0
  961. AVX_INSTR unpcklps, 1, 0, 0
  962. AVX_INSTR xorpd, 1, 0, 1
  963. AVX_INSTR xorps, 1, 0, 1
  964. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  965. AVX_INSTR pfadd, 1, 0, 1
  966. AVX_INSTR pfsub, 1, 0, 0
  967. AVX_INSTR pfmul, 1, 0, 1
  968. ; base-4 constants for shuffles
  969. %assign i 0
  970. %rep 256
  971. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  972. %if j < 10
  973. CAT_XDEFINE q000, j, i
  974. %elif j < 100
  975. CAT_XDEFINE q00, j, i
  976. %elif j < 1000
  977. CAT_XDEFINE q0, j, i
  978. %else
  979. CAT_XDEFINE q, j, i
  980. %endif
  981. %assign i i+1
  982. %endrep
  983. %undef i
  984. %undef j
  985. %macro FMA_INSTR 3
  986. %macro %1 4-7 %1, %2, %3
  987. %if cpuflag(xop)
  988. v%5 %1, %2, %3, %4
  989. %else
  990. %6 %1, %2, %3
  991. %7 %1, %4
  992. %endif
  993. %endmacro
  994. %endmacro
  995. FMA_INSTR pmacsdd, pmulld, paddd
  996. FMA_INSTR pmacsww, pmullw, paddw
  997. FMA_INSTR pmadcswd, pmaddwd, paddd