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.

1290 lines
34KB

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