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.

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