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.

1288 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 %+ %1m
  156. %else
  157. %define r%1m [rstk + stack_offset + %3]
  158. %define r%1mp dword r %+ %1m
  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], 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. %assign stack_size_padded (xmm_regs_used-6)*16+16-gprsize-(stack_offset&15)
  412. SUB rsp, stack_size_padded
  413. WIN64_PUSH_XMM
  414. %endif
  415. %endmacro
  416. %macro WIN64_RESTORE_XMM_INTERNAL 1
  417. %if xmm_regs_used > 6
  418. %assign %%i xmm_regs_used
  419. %rep (xmm_regs_used-6)
  420. %assign %%i %%i-1
  421. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+stack_size]
  422. %endrep
  423. %endif
  424. %if stack_size_padded > 0
  425. %if stack_size > 0 && (mmsize == 32 || HAVE_ALIGNED_STACK == 0)
  426. mov rsp, rstkm
  427. %else
  428. add %1, stack_size_padded
  429. %endif
  430. %endif
  431. %endmacro
  432. %macro WIN64_RESTORE_XMM 1
  433. WIN64_RESTORE_XMM_INTERNAL %1
  434. %assign stack_offset (stack_offset-stack_size_padded)
  435. %assign xmm_regs_used 0
  436. %endmacro
  437. %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32 || stack_size > 0
  438. %macro RET 0
  439. WIN64_RESTORE_XMM_INTERNAL rsp
  440. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  441. %if mmsize == 32
  442. vzeroupper
  443. %endif
  444. ret
  445. %endmacro
  446. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  447. DECLARE_REG 0, rdi
  448. DECLARE_REG 1, rsi
  449. DECLARE_REG 2, rdx
  450. DECLARE_REG 3, rcx
  451. DECLARE_REG 4, R8
  452. DECLARE_REG 5, R9
  453. DECLARE_REG 6, rax, 8
  454. DECLARE_REG 7, R10, 16
  455. DECLARE_REG 8, R11, 24
  456. DECLARE_REG 9, rbx, 32
  457. DECLARE_REG 10, rbp, 40
  458. DECLARE_REG 11, R12, 48
  459. DECLARE_REG 12, R13, 56
  460. DECLARE_REG 13, R14, 64
  461. DECLARE_REG 14, R15, 72
  462. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  463. %assign num_args %1
  464. %assign regs_used %2
  465. SETUP_STACK_POINTER %4
  466. ASSERT regs_used >= num_args
  467. ASSERT regs_used <= 15
  468. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  469. ALLOC_STACK %4
  470. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  471. DEFINE_ARGS_INTERNAL %0, %4, %5
  472. %endmacro
  473. %define has_epilogue regs_used > 9 || mmsize == 32 || stack_size > 0
  474. %macro RET 0
  475. %if stack_size_padded > 0
  476. %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
  477. mov rsp, rstkm
  478. %else
  479. add rsp, stack_size_padded
  480. %endif
  481. %endif
  482. POP_IF_USED 14, 13, 12, 11, 10, 9
  483. %if mmsize == 32
  484. vzeroupper
  485. %endif
  486. ret
  487. %endmacro
  488. %else ; X86_32 ;==============================================================
  489. DECLARE_REG 0, eax, 4
  490. DECLARE_REG 1, ecx, 8
  491. DECLARE_REG 2, edx, 12
  492. DECLARE_REG 3, ebx, 16
  493. DECLARE_REG 4, esi, 20
  494. DECLARE_REG 5, edi, 24
  495. DECLARE_REG 6, ebp, 28
  496. %define rsp esp
  497. %macro DECLARE_ARG 1-*
  498. %rep %0
  499. %define r%1m [rstk + stack_offset + 4*%1 + 4]
  500. %define r%1mp dword r%1m
  501. %rotate 1
  502. %endrep
  503. %endmacro
  504. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  505. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  506. %assign num_args %1
  507. %assign regs_used %2
  508. %if regs_used > 7
  509. %assign regs_used 7
  510. %endif
  511. SETUP_STACK_POINTER %4
  512. ASSERT regs_used <= 7
  513. ASSERT regs_used >= num_args
  514. PUSH_IF_USED 3, 4, 5, 6
  515. ALLOC_STACK %4
  516. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  517. DEFINE_ARGS_INTERNAL %0, %4, %5
  518. %endmacro
  519. %define has_epilogue regs_used > 3 || mmsize == 32 || stack_size > 0
  520. %macro RET 0
  521. %if stack_size_padded > 0
  522. %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
  523. mov rsp, rstkm
  524. %else
  525. add rsp, stack_size_padded
  526. %endif
  527. %endif
  528. POP_IF_USED 6, 5, 4, 3
  529. %if mmsize == 32
  530. vzeroupper
  531. %endif
  532. ret
  533. %endmacro
  534. %endif ;======================================================================
  535. %if WIN64 == 0
  536. %macro WIN64_SPILL_XMM 1
  537. %endmacro
  538. %macro WIN64_RESTORE_XMM 1
  539. %endmacro
  540. %macro WIN64_PUSH_XMM 0
  541. %endmacro
  542. %endif
  543. %macro REP_RET 0
  544. %if has_epilogue
  545. RET
  546. %else
  547. rep ret
  548. %endif
  549. %endmacro
  550. %macro TAIL_CALL 2 ; callee, is_nonadjacent
  551. %if has_epilogue
  552. call %1
  553. RET
  554. %elif %2
  555. jmp %1
  556. %endif
  557. %endmacro
  558. ;=============================================================================
  559. ; arch-independent part
  560. ;=============================================================================
  561. %assign function_align 16
  562. ; Begin a function.
  563. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  564. ; subsequent uses of the function name automatically refer to the mangled version.
  565. ; Appends cpuflags to the function name if cpuflags has been specified.
  566. %macro cglobal 1-2+ ; name, [PROLOGUE args]
  567. %if %0 == 1
  568. cglobal_internal %1 %+ SUFFIX
  569. %else
  570. cglobal_internal %1 %+ SUFFIX, %2
  571. %endif
  572. %endmacro
  573. %macro cglobal_internal 1-2+
  574. %ifndef cglobaled_%1
  575. %xdefine %1 mangle(program_name %+ _ %+ %1)
  576. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  577. CAT_XDEFINE cglobaled_, %1, 1
  578. %endif
  579. %xdefine current_function %1
  580. %ifidn __OUTPUT_FORMAT__,elf
  581. global %1:function hidden
  582. %else
  583. global %1
  584. %endif
  585. align function_align
  586. %1:
  587. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  588. %xdefine rstk rsp
  589. %assign stack_offset 0
  590. %assign stack_size 0
  591. %assign stack_size_padded 0
  592. %if %0 > 1
  593. PROLOGUE %2
  594. %endif
  595. %endmacro
  596. %macro cextern 1
  597. %xdefine %1 mangle(program_name %+ _ %+ %1)
  598. CAT_XDEFINE cglobaled_, %1, 1
  599. extern %1
  600. %endmacro
  601. ; like cextern, but without the prefix
  602. %macro cextern_naked 1
  603. %xdefine %1 mangle(%1)
  604. CAT_XDEFINE cglobaled_, %1, 1
  605. extern %1
  606. %endmacro
  607. %macro const 2+
  608. %xdefine %1 mangle(program_name %+ _ %+ %1)
  609. global %1
  610. %1: %2
  611. %endmacro
  612. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  613. ; executable by default.
  614. %ifidn __OUTPUT_FORMAT__,elf
  615. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  616. %endif
  617. ; cpuflags
  618. %assign cpuflags_mmx (1<<0)
  619. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  620. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  621. %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
  622. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  623. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  624. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  625. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  626. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  627. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  628. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  629. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  630. %assign cpuflags_xop (1<<12)| cpuflags_avx
  631. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  632. %assign cpuflags_avx2 (1<<14)| cpuflags_avx
  633. %assign cpuflags_fma3 (1<<15)| cpuflags_avx
  634. %assign cpuflags_cache32 (1<<16)
  635. %assign cpuflags_cache64 (1<<17)
  636. %assign cpuflags_slowctz (1<<18)
  637. %assign cpuflags_lzcnt (1<<19)
  638. %assign cpuflags_misalign (1<<20)
  639. %assign cpuflags_aligned (1<<21) ; not a cpu feature, but a function variant
  640. %assign cpuflags_atom (1<<22)
  641. %assign cpuflags_bmi1 (1<<23)
  642. %assign cpuflags_bmi2 (1<<24)|cpuflags_bmi1
  643. %assign cpuflags_tbm (1<<25)|cpuflags_bmi1
  644. %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
  645. %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
  646. ; Takes up to 2 cpuflags from the above list.
  647. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  648. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  649. %macro INIT_CPUFLAGS 0-2
  650. CPUNOP amdnop
  651. %if %0 >= 1
  652. %xdefine cpuname %1
  653. %assign cpuflags cpuflags_%1
  654. %if %0 >= 2
  655. %xdefine cpuname %1_%2
  656. %assign cpuflags cpuflags | cpuflags_%2
  657. %endif
  658. %xdefine SUFFIX _ %+ cpuname
  659. %if cpuflag(avx)
  660. %assign avx_enabled 1
  661. %endif
  662. %if mmsize == 16 && notcpuflag(sse2)
  663. %define mova movaps
  664. %define movu movups
  665. %define movnta movntps
  666. %endif
  667. %if cpuflag(aligned)
  668. %define movu mova
  669. %elifidn %1, sse3
  670. %define movu lddqu
  671. %endif
  672. %if notcpuflag(mmx2)
  673. CPUNOP basicnop
  674. %endif
  675. %else
  676. %xdefine SUFFIX
  677. %undef cpuname
  678. %undef cpuflags
  679. %endif
  680. %endmacro
  681. ; merge mmx and sse*
  682. %macro CAT_XDEFINE 3
  683. %xdefine %1%2 %3
  684. %endmacro
  685. %macro CAT_UNDEF 2
  686. %undef %1%2
  687. %endmacro
  688. %macro INIT_MMX 0-1+
  689. %assign avx_enabled 0
  690. %define RESET_MM_PERMUTATION INIT_MMX %1
  691. %define mmsize 8
  692. %define num_mmregs 8
  693. %define mova movq
  694. %define movu movq
  695. %define movh movd
  696. %define movnta movntq
  697. %assign %%i 0
  698. %rep 8
  699. CAT_XDEFINE m, %%i, mm %+ %%i
  700. CAT_XDEFINE nmm, %%i, %%i
  701. %assign %%i %%i+1
  702. %endrep
  703. %rep 8
  704. CAT_UNDEF m, %%i
  705. CAT_UNDEF nmm, %%i
  706. %assign %%i %%i+1
  707. %endrep
  708. INIT_CPUFLAGS %1
  709. %endmacro
  710. %macro INIT_XMM 0-1+
  711. %assign avx_enabled 0
  712. %define RESET_MM_PERMUTATION INIT_XMM %1
  713. %define mmsize 16
  714. %define num_mmregs 8
  715. %if ARCH_X86_64
  716. %define num_mmregs 16
  717. %endif
  718. %define mova movdqa
  719. %define movu movdqu
  720. %define movh movq
  721. %define movnta movntdq
  722. %assign %%i 0
  723. %rep num_mmregs
  724. CAT_XDEFINE m, %%i, xmm %+ %%i
  725. CAT_XDEFINE nxmm, %%i, %%i
  726. %assign %%i %%i+1
  727. %endrep
  728. INIT_CPUFLAGS %1
  729. %endmacro
  730. ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
  731. %macro INIT_AVX 0
  732. INIT_XMM
  733. %assign avx_enabled 1
  734. %define PALIGNR PALIGNR_SSSE3
  735. %define RESET_MM_PERMUTATION INIT_AVX
  736. %endmacro
  737. %macro INIT_YMM 0-1+
  738. %assign avx_enabled 1
  739. %define RESET_MM_PERMUTATION INIT_YMM %1
  740. %define mmsize 32
  741. %define num_mmregs 8
  742. %if ARCH_X86_64
  743. %define num_mmregs 16
  744. %endif
  745. %define mova vmovaps
  746. %define movu vmovups
  747. %undef movh
  748. %define movnta vmovntps
  749. %assign %%i 0
  750. %rep num_mmregs
  751. CAT_XDEFINE m, %%i, ymm %+ %%i
  752. CAT_XDEFINE nymm, %%i, %%i
  753. %assign %%i %%i+1
  754. %endrep
  755. INIT_CPUFLAGS %1
  756. %endmacro
  757. INIT_XMM
  758. ; I often want to use macros that permute their arguments. e.g. there's no
  759. ; efficient way to implement butterfly or transpose or dct without swapping some
  760. ; arguments.
  761. ;
  762. ; I would like to not have to manually keep track of the permutations:
  763. ; If I insert a permutation in the middle of a function, it should automatically
  764. ; change everything that follows. For more complex macros I may also have multiple
  765. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  766. ;
  767. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  768. ; permutes its arguments. It's equivalent to exchanging the contents of the
  769. ; registers, except that this way you exchange the register names instead, so it
  770. ; doesn't cost any cycles.
  771. %macro PERMUTE 2-* ; takes a list of pairs to swap
  772. %rep %0/2
  773. %xdefine tmp%2 m%2
  774. %xdefine ntmp%2 nm%2
  775. %rotate 2
  776. %endrep
  777. %rep %0/2
  778. %xdefine m%1 tmp%2
  779. %xdefine nm%1 ntmp%2
  780. %undef tmp%2
  781. %undef ntmp%2
  782. %rotate 2
  783. %endrep
  784. %endmacro
  785. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  786. %rep %0-1
  787. %ifdef m%1
  788. %xdefine tmp m%1
  789. %xdefine m%1 m%2
  790. %xdefine m%2 tmp
  791. CAT_XDEFINE n, m%1, %1
  792. CAT_XDEFINE n, m%2, %2
  793. %else
  794. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  795. ; Be careful using this mode in nested macros though, as in some cases there may be
  796. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  797. %xdefine %%n1 n %+ %1
  798. %xdefine %%n2 n %+ %2
  799. %xdefine tmp m %+ %%n1
  800. CAT_XDEFINE m, %%n1, m %+ %%n2
  801. CAT_XDEFINE m, %%n2, tmp
  802. CAT_XDEFINE n, m %+ %%n1, %%n1
  803. CAT_XDEFINE n, m %+ %%n2, %%n2
  804. %endif
  805. %undef tmp
  806. %rotate 1
  807. %endrep
  808. %endmacro
  809. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  810. ; calls to that function will automatically load the permutation, so values can
  811. ; be returned in mmregs.
  812. %macro SAVE_MM_PERMUTATION 0-1
  813. %if %0
  814. %xdefine %%f %1_m
  815. %else
  816. %xdefine %%f current_function %+ _m
  817. %endif
  818. %assign %%i 0
  819. %rep num_mmregs
  820. CAT_XDEFINE %%f, %%i, m %+ %%i
  821. %assign %%i %%i+1
  822. %endrep
  823. %endmacro
  824. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  825. %ifdef %1_m0
  826. %assign %%i 0
  827. %rep num_mmregs
  828. CAT_XDEFINE m, %%i, %1_m %+ %%i
  829. CAT_XDEFINE n, m %+ %%i, %%i
  830. %assign %%i %%i+1
  831. %endrep
  832. %endif
  833. %endmacro
  834. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  835. %macro call 1
  836. call_internal %1 %+ SUFFIX, %1
  837. %endmacro
  838. %macro call_internal 2
  839. %xdefine %%i %2
  840. %ifndef cglobaled_%2
  841. %ifdef cglobaled_%1
  842. %xdefine %%i %1
  843. %endif
  844. %endif
  845. call %%i
  846. LOAD_MM_PERMUTATION %%i
  847. %endmacro
  848. ; Substitutions that reduce instruction size but are functionally equivalent
  849. %macro add 2
  850. %ifnum %2
  851. %if %2==128
  852. sub %1, -128
  853. %else
  854. add %1, %2
  855. %endif
  856. %else
  857. add %1, %2
  858. %endif
  859. %endmacro
  860. %macro sub 2
  861. %ifnum %2
  862. %if %2==128
  863. add %1, -128
  864. %else
  865. sub %1, %2
  866. %endif
  867. %else
  868. sub %1, %2
  869. %endif
  870. %endmacro
  871. ;=============================================================================
  872. ; AVX abstraction layer
  873. ;=============================================================================
  874. %assign i 0
  875. %rep 16
  876. %if i < 8
  877. CAT_XDEFINE sizeofmm, i, 8
  878. %endif
  879. CAT_XDEFINE sizeofxmm, i, 16
  880. CAT_XDEFINE sizeofymm, i, 32
  881. %assign i i+1
  882. %endrep
  883. %undef i
  884. %macro CHECK_AVX_INSTR_EMU 3-*
  885. %xdefine %%opcode %1
  886. %xdefine %%dst %2
  887. %rep %0-2
  888. %ifidn %%dst, %3
  889. %error non-avx emulation of ``%%opcode'' is not supported
  890. %endif
  891. %rotate 1
  892. %endrep
  893. %endmacro
  894. ;%1 == instruction
  895. ;%2 == 1 if float, 0 if int
  896. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  897. ;%4 == number of operands given
  898. ;%5+: operands
  899. %macro RUN_AVX_INSTR 6-7+
  900. %ifid %6
  901. %define %%sizeofreg sizeof%6
  902. %elifid %5
  903. %define %%sizeofreg sizeof%5
  904. %else
  905. %define %%sizeofreg mmsize
  906. %endif
  907. %if %%sizeofreg==32
  908. %if %4>=3
  909. v%1 %5, %6, %7
  910. %else
  911. v%1 %5, %6
  912. %endif
  913. %else
  914. %if %%sizeofreg==8
  915. %define %%regmov movq
  916. %elif %2
  917. %define %%regmov movaps
  918. %else
  919. %define %%regmov movdqa
  920. %endif
  921. %if %4>=3+%3
  922. %ifnidn %5, %6
  923. %if avx_enabled && %%sizeofreg==16
  924. v%1 %5, %6, %7
  925. %else
  926. CHECK_AVX_INSTR_EMU {%1 %5, %6, %7}, %5, %7
  927. %%regmov %5, %6
  928. %1 %5, %7
  929. %endif
  930. %else
  931. %1 %5, %7
  932. %endif
  933. %elif %4>=3
  934. %1 %5, %6, %7
  935. %else
  936. %1 %5, %6
  937. %endif
  938. %endif
  939. %endmacro
  940. ; 3arg AVX ops with a memory arg can only have it in src2,
  941. ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
  942. ; So, if the op is symmetric and the wrong one is memory, swap them.
  943. %macro RUN_AVX_INSTR1 8
  944. %assign %%swap 0
  945. %if avx_enabled
  946. %ifnid %6
  947. %assign %%swap 1
  948. %endif
  949. %elifnidn %5, %6
  950. %ifnid %7
  951. %assign %%swap 1
  952. %endif
  953. %endif
  954. %if %%swap && %3 == 0 && %8 == 1
  955. RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
  956. %else
  957. RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
  958. %endif
  959. %endmacro
  960. ;%1 == instruction
  961. ;%2 == 1 if float, 0 if int
  962. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  963. ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
  964. %macro AVX_INSTR 4
  965. %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
  966. %ifidn %3, fnord
  967. RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
  968. %elifidn %4, fnord
  969. RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
  970. %elifidn %5, fnord
  971. RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
  972. %else
  973. RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
  974. %endif
  975. %endmacro
  976. %endmacro
  977. AVX_INSTR addpd, 1, 0, 1
  978. AVX_INSTR addps, 1, 0, 1
  979. AVX_INSTR addsd, 1, 0, 1
  980. AVX_INSTR addss, 1, 0, 1
  981. AVX_INSTR addsubpd, 1, 0, 0
  982. AVX_INSTR addsubps, 1, 0, 0
  983. AVX_INSTR andpd, 1, 0, 1
  984. AVX_INSTR andps, 1, 0, 1
  985. AVX_INSTR andnpd, 1, 0, 0
  986. AVX_INSTR andnps, 1, 0, 0
  987. AVX_INSTR blendpd, 1, 0, 0
  988. AVX_INSTR blendps, 1, 0, 0
  989. AVX_INSTR blendvpd, 1, 0, 0
  990. AVX_INSTR blendvps, 1, 0, 0
  991. AVX_INSTR cmppd, 1, 0, 0
  992. AVX_INSTR cmpps, 1, 0, 0
  993. AVX_INSTR cmpsd, 1, 0, 0
  994. AVX_INSTR cmpss, 1, 0, 0
  995. AVX_INSTR cvtdq2ps, 1, 0, 0
  996. AVX_INSTR cvtpd2dq, 1, 0, 0
  997. AVX_INSTR cvtps2dq, 1, 0, 0
  998. AVX_INSTR divpd, 1, 0, 0
  999. AVX_INSTR divps, 1, 0, 0
  1000. AVX_INSTR divsd, 1, 0, 0
  1001. AVX_INSTR divss, 1, 0, 0
  1002. AVX_INSTR dppd, 1, 1, 0
  1003. AVX_INSTR dpps, 1, 1, 0
  1004. AVX_INSTR haddpd, 1, 0, 0
  1005. AVX_INSTR haddps, 1, 0, 0
  1006. AVX_INSTR hsubpd, 1, 0, 0
  1007. AVX_INSTR hsubps, 1, 0, 0
  1008. AVX_INSTR maxpd, 1, 0, 1
  1009. AVX_INSTR maxps, 1, 0, 1
  1010. AVX_INSTR maxsd, 1, 0, 1
  1011. AVX_INSTR maxss, 1, 0, 1
  1012. AVX_INSTR minpd, 1, 0, 1
  1013. AVX_INSTR minps, 1, 0, 1
  1014. AVX_INSTR minsd, 1, 0, 1
  1015. AVX_INSTR minss, 1, 0, 1
  1016. AVX_INSTR movhlps, 1, 0, 0
  1017. AVX_INSTR movlhps, 1, 0, 0
  1018. AVX_INSTR movsd, 1, 0, 0
  1019. AVX_INSTR movss, 1, 0, 0
  1020. AVX_INSTR mpsadbw, 0, 1, 0
  1021. AVX_INSTR mulpd, 1, 0, 1
  1022. AVX_INSTR mulps, 1, 0, 1
  1023. AVX_INSTR mulsd, 1, 0, 1
  1024. AVX_INSTR mulss, 1, 0, 1
  1025. AVX_INSTR orpd, 1, 0, 1
  1026. AVX_INSTR orps, 1, 0, 1
  1027. AVX_INSTR pabsb, 0, 0, 0
  1028. AVX_INSTR pabsw, 0, 0, 0
  1029. AVX_INSTR pabsd, 0, 0, 0
  1030. AVX_INSTR packsswb, 0, 0, 0
  1031. AVX_INSTR packssdw, 0, 0, 0
  1032. AVX_INSTR packuswb, 0, 0, 0
  1033. AVX_INSTR packusdw, 0, 0, 0
  1034. AVX_INSTR paddb, 0, 0, 1
  1035. AVX_INSTR paddw, 0, 0, 1
  1036. AVX_INSTR paddd, 0, 0, 1
  1037. AVX_INSTR paddq, 0, 0, 1
  1038. AVX_INSTR paddsb, 0, 0, 1
  1039. AVX_INSTR paddsw, 0, 0, 1
  1040. AVX_INSTR paddusb, 0, 0, 1
  1041. AVX_INSTR paddusw, 0, 0, 1
  1042. AVX_INSTR palignr, 0, 1, 0
  1043. AVX_INSTR pand, 0, 0, 1
  1044. AVX_INSTR pandn, 0, 0, 0
  1045. AVX_INSTR pavgb, 0, 0, 1
  1046. AVX_INSTR pavgw, 0, 0, 1
  1047. AVX_INSTR pblendvb, 0, 0, 0
  1048. AVX_INSTR pblendw, 0, 1, 0
  1049. AVX_INSTR pcmpestri, 0, 0, 0
  1050. AVX_INSTR pcmpestrm, 0, 0, 0
  1051. AVX_INSTR pcmpistri, 0, 0, 0
  1052. AVX_INSTR pcmpistrm, 0, 0, 0
  1053. AVX_INSTR pcmpeqb, 0, 0, 1
  1054. AVX_INSTR pcmpeqw, 0, 0, 1
  1055. AVX_INSTR pcmpeqd, 0, 0, 1
  1056. AVX_INSTR pcmpeqq, 0, 0, 1
  1057. AVX_INSTR pcmpgtb, 0, 0, 0
  1058. AVX_INSTR pcmpgtw, 0, 0, 0
  1059. AVX_INSTR pcmpgtd, 0, 0, 0
  1060. AVX_INSTR pcmpgtq, 0, 0, 0
  1061. AVX_INSTR phaddw, 0, 0, 0
  1062. AVX_INSTR phaddd, 0, 0, 0
  1063. AVX_INSTR phaddsw, 0, 0, 0
  1064. AVX_INSTR phsubw, 0, 0, 0
  1065. AVX_INSTR phsubd, 0, 0, 0
  1066. AVX_INSTR phsubsw, 0, 0, 0
  1067. AVX_INSTR pmaddwd, 0, 0, 1
  1068. AVX_INSTR pmaddubsw, 0, 0, 0
  1069. AVX_INSTR pmaxsb, 0, 0, 1
  1070. AVX_INSTR pmaxsw, 0, 0, 1
  1071. AVX_INSTR pmaxsd, 0, 0, 1
  1072. AVX_INSTR pmaxub, 0, 0, 1
  1073. AVX_INSTR pmaxuw, 0, 0, 1
  1074. AVX_INSTR pmaxud, 0, 0, 1
  1075. AVX_INSTR pminsb, 0, 0, 1
  1076. AVX_INSTR pminsw, 0, 0, 1
  1077. AVX_INSTR pminsd, 0, 0, 1
  1078. AVX_INSTR pminub, 0, 0, 1
  1079. AVX_INSTR pminuw, 0, 0, 1
  1080. AVX_INSTR pminud, 0, 0, 1
  1081. AVX_INSTR pmovmskb, 0, 0, 0
  1082. AVX_INSTR pmulhuw, 0, 0, 1
  1083. AVX_INSTR pmulhrsw, 0, 0, 1
  1084. AVX_INSTR pmulhw, 0, 0, 1
  1085. AVX_INSTR pmullw, 0, 0, 1
  1086. AVX_INSTR pmulld, 0, 0, 1
  1087. AVX_INSTR pmuludq, 0, 0, 1
  1088. AVX_INSTR pmuldq, 0, 0, 1
  1089. AVX_INSTR por, 0, 0, 1
  1090. AVX_INSTR psadbw, 0, 0, 1
  1091. AVX_INSTR pshufb, 0, 0, 0
  1092. AVX_INSTR pshufd, 0, 1, 0
  1093. AVX_INSTR pshufhw, 0, 1, 0
  1094. AVX_INSTR pshuflw, 0, 1, 0
  1095. AVX_INSTR psignb, 0, 0, 0
  1096. AVX_INSTR psignw, 0, 0, 0
  1097. AVX_INSTR psignd, 0, 0, 0
  1098. AVX_INSTR psllw, 0, 0, 0
  1099. AVX_INSTR pslld, 0, 0, 0
  1100. AVX_INSTR psllq, 0, 0, 0
  1101. AVX_INSTR pslldq, 0, 0, 0
  1102. AVX_INSTR psraw, 0, 0, 0
  1103. AVX_INSTR psrad, 0, 0, 0
  1104. AVX_INSTR psrlw, 0, 0, 0
  1105. AVX_INSTR psrld, 0, 0, 0
  1106. AVX_INSTR psrlq, 0, 0, 0
  1107. AVX_INSTR psrldq, 0, 0, 0
  1108. AVX_INSTR psubb, 0, 0, 0
  1109. AVX_INSTR psubw, 0, 0, 0
  1110. AVX_INSTR psubd, 0, 0, 0
  1111. AVX_INSTR psubq, 0, 0, 0
  1112. AVX_INSTR psubsb, 0, 0, 0
  1113. AVX_INSTR psubsw, 0, 0, 0
  1114. AVX_INSTR psubusb, 0, 0, 0
  1115. AVX_INSTR psubusw, 0, 0, 0
  1116. AVX_INSTR ptest, 0, 0, 0
  1117. AVX_INSTR punpckhbw, 0, 0, 0
  1118. AVX_INSTR punpckhwd, 0, 0, 0
  1119. AVX_INSTR punpckhdq, 0, 0, 0
  1120. AVX_INSTR punpckhqdq, 0, 0, 0
  1121. AVX_INSTR punpcklbw, 0, 0, 0
  1122. AVX_INSTR punpcklwd, 0, 0, 0
  1123. AVX_INSTR punpckldq, 0, 0, 0
  1124. AVX_INSTR punpcklqdq, 0, 0, 0
  1125. AVX_INSTR pxor, 0, 0, 1
  1126. AVX_INSTR shufps, 1, 1, 0
  1127. AVX_INSTR subpd, 1, 0, 0
  1128. AVX_INSTR subps, 1, 0, 0
  1129. AVX_INSTR subsd, 1, 0, 0
  1130. AVX_INSTR subss, 1, 0, 0
  1131. AVX_INSTR unpckhpd, 1, 0, 0
  1132. AVX_INSTR unpckhps, 1, 0, 0
  1133. AVX_INSTR unpcklpd, 1, 0, 0
  1134. AVX_INSTR unpcklps, 1, 0, 0
  1135. AVX_INSTR xorpd, 1, 0, 1
  1136. AVX_INSTR xorps, 1, 0, 1
  1137. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  1138. AVX_INSTR pfadd, 1, 0, 1
  1139. AVX_INSTR pfsub, 1, 0, 0
  1140. AVX_INSTR pfmul, 1, 0, 1
  1141. ; base-4 constants for shuffles
  1142. %assign i 0
  1143. %rep 256
  1144. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  1145. %if j < 10
  1146. CAT_XDEFINE q000, j, i
  1147. %elif j < 100
  1148. CAT_XDEFINE q00, j, i
  1149. %elif j < 1000
  1150. CAT_XDEFINE q0, j, i
  1151. %else
  1152. CAT_XDEFINE q, j, i
  1153. %endif
  1154. %assign i i+1
  1155. %endrep
  1156. %undef i
  1157. %undef j
  1158. %macro FMA_INSTR 3
  1159. %macro %1 5-8 %1, %2, %3
  1160. %if cpuflag(xop) || cpuflag(fma4)
  1161. v%6 %1, %2, %3, %4
  1162. %else
  1163. %ifidn %1, %4
  1164. %7 %5, %2, %3
  1165. %8 %1, %4, %5
  1166. %else
  1167. %7 %1, %2, %3
  1168. %8 %1, %4
  1169. %endif
  1170. %endif
  1171. %endmacro
  1172. %endmacro
  1173. FMA_INSTR fmaddps, mulps, addps
  1174. FMA_INSTR pmacsdd, pmulld, paddd
  1175. FMA_INSTR pmacsww, pmullw, paddw
  1176. FMA_INSTR pmadcswd, pmaddwd, paddd
  1177. ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
  1178. ; This lets us use tzcnt without bumping the yasm version requirement yet.
  1179. %define tzcnt rep bsf