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.

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