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.

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