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.

1171 lines
30KB

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