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.

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