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.

1716 lines
49KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm: x264asm abstraction layer
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2018 x264 project
  5. ;*
  6. ;* Authors: Loren Merritt <lorenm@u.washington.edu>
  7. ;* Henrik Gramner <henrik@gramner.com>
  8. ;* Anton Mitrofanov <BugMaster@narod.ru>
  9. ;* Fiona Glaser <fiona@x264.com>
  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. %if HAVE_ALIGNED_STACK
  41. %define STACK_ALIGNMENT 16
  42. %endif
  43. %ifndef STACK_ALIGNMENT
  44. %if ARCH_X86_64
  45. %define STACK_ALIGNMENT 16
  46. %else
  47. %define STACK_ALIGNMENT 4
  48. %endif
  49. %endif
  50. %define WIN64 0
  51. %define UNIX64 0
  52. %if ARCH_X86_64
  53. %ifidn __OUTPUT_FORMAT__,win32
  54. %define WIN64 1
  55. %elifidn __OUTPUT_FORMAT__,win64
  56. %define WIN64 1
  57. %elifidn __OUTPUT_FORMAT__,x64
  58. %define WIN64 1
  59. %else
  60. %define UNIX64 1
  61. %endif
  62. %endif
  63. %define FORMAT_ELF 0
  64. %ifidn __OUTPUT_FORMAT__,elf
  65. %define FORMAT_ELF 1
  66. %elifidn __OUTPUT_FORMAT__,elf32
  67. %define FORMAT_ELF 1
  68. %elifidn __OUTPUT_FORMAT__,elf64
  69. %define FORMAT_ELF 1
  70. %endif
  71. %ifdef PREFIX
  72. %define mangle(x) _ %+ x
  73. %else
  74. %define mangle(x) x
  75. %endif
  76. ; aout does not support align=
  77. ; NOTE: This section is out of sync with x264, in order to
  78. ; keep supporting OS/2.
  79. %macro SECTION_RODATA 0-1 16
  80. %ifidn __OUTPUT_FORMAT__,aout
  81. SECTION .text
  82. %elifidn __OUTPUT_FORMAT__,coff
  83. SECTION .text
  84. %elifidn __OUTPUT_FORMAT__,win32
  85. SECTION .rdata align=%1
  86. %elif WIN64
  87. SECTION .rdata align=%1
  88. %else
  89. SECTION .rodata align=%1
  90. %endif
  91. %endmacro
  92. %if WIN64
  93. %define PIC
  94. %elif ARCH_X86_64 == 0
  95. ; x86_32 doesn't require PIC.
  96. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  97. ; the code contains a few textrels, so we'll skip that complexity.
  98. %undef PIC
  99. %endif
  100. %ifdef PIC
  101. default rel
  102. %endif
  103. %macro CPUNOP 1
  104. %if HAVE_CPUNOP
  105. CPU %1
  106. %endif
  107. %endmacro
  108. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  109. ; Currently this works only for leaf functions which load all their arguments
  110. ; into registers at the start, and make no other use of the stack. Luckily that
  111. ; covers most of x264's asm.
  112. ; PROLOGUE:
  113. ; %1 = number of arguments. loads them from stack if needed.
  114. ; %2 = number of registers used. pushes callee-saved regs if needed.
  115. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  116. ; %4 = (optional) stack size to be allocated. The stack will be aligned before
  117. ; allocating the specified stack size. If the required stack alignment is
  118. ; larger than the known stack alignment the stack will be manually aligned
  119. ; and an extra register will be allocated to hold the original stack
  120. ; pointer (to not invalidate r0m etc.). To prevent the use of an extra
  121. ; register as stack pointer, request a negative stack size.
  122. ; %4+/%5+ = list of names to define to registers
  123. ; PROLOGUE can also be invoked by adding the same options to cglobal
  124. ; e.g.
  125. ; cglobal foo, 2,3,7,0x40, dst, src, tmp
  126. ; declares a function (foo) that automatically loads two arguments (dst and
  127. ; src) into registers, uses one additional register (tmp) plus 7 vector
  128. ; registers (m0-m6) and allocates 0x40 bytes of stack space.
  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. ; Use this instead of RET if it's a branch target.
  136. ; registers:
  137. ; rN and rNq are the native-size register holding function argument N
  138. ; rNd, rNw, rNb are dword, word, and byte size
  139. ; rNh is the high 8 bits of the word size
  140. ; rNm is the original location of arg N (a register or on the stack), dword
  141. ; rNmp is native size
  142. %macro DECLARE_REG 2-3
  143. %define r%1q %2
  144. %define r%1d %2d
  145. %define r%1w %2w
  146. %define r%1b %2b
  147. %define r%1h %2h
  148. %define %2q %2
  149. %if %0 == 2
  150. %define r%1m %2d
  151. %define r%1mp %2
  152. %elif ARCH_X86_64 ; memory
  153. %define r%1m [rstk + stack_offset + %3]
  154. %define r%1mp qword r %+ %1 %+ m
  155. %else
  156. %define r%1m [rstk + stack_offset + %3]
  157. %define r%1mp dword r %+ %1 %+ m
  158. %endif
  159. %define r%1 %2
  160. %endmacro
  161. %macro DECLARE_REG_SIZE 3
  162. %define r%1q r%1
  163. %define e%1q r%1
  164. %define r%1d e%1
  165. %define e%1d e%1
  166. %define r%1w %1
  167. %define e%1w %1
  168. %define r%1h %3
  169. %define e%1h %3
  170. %define r%1b %2
  171. %define e%1b %2
  172. %if ARCH_X86_64 == 0
  173. %define r%1 e%1
  174. %endif
  175. %endmacro
  176. DECLARE_REG_SIZE ax, al, ah
  177. DECLARE_REG_SIZE bx, bl, bh
  178. DECLARE_REG_SIZE cx, cl, ch
  179. DECLARE_REG_SIZE dx, dl, dh
  180. DECLARE_REG_SIZE si, sil, null
  181. DECLARE_REG_SIZE di, dil, null
  182. DECLARE_REG_SIZE bp, bpl, null
  183. ; t# defines for when per-arch register allocation is more complex than just function arguments
  184. %macro DECLARE_REG_TMP 1-*
  185. %assign %%i 0
  186. %rep %0
  187. CAT_XDEFINE t, %%i, r%1
  188. %assign %%i %%i+1
  189. %rotate 1
  190. %endrep
  191. %endmacro
  192. %macro DECLARE_REG_TMP_SIZE 0-*
  193. %rep %0
  194. %define t%1q t%1 %+ q
  195. %define t%1d t%1 %+ d
  196. %define t%1w t%1 %+ w
  197. %define t%1h t%1 %+ h
  198. %define t%1b t%1 %+ b
  199. %rotate 1
  200. %endrep
  201. %endmacro
  202. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
  203. %if ARCH_X86_64
  204. %define gprsize 8
  205. %else
  206. %define gprsize 4
  207. %endif
  208. %macro PUSH 1
  209. push %1
  210. %ifidn rstk, rsp
  211. %assign stack_offset stack_offset+gprsize
  212. %endif
  213. %endmacro
  214. %macro POP 1
  215. pop %1
  216. %ifidn rstk, rsp
  217. %assign stack_offset stack_offset-gprsize
  218. %endif
  219. %endmacro
  220. %macro PUSH_IF_USED 1-*
  221. %rep %0
  222. %if %1 < regs_used
  223. PUSH r%1
  224. %endif
  225. %rotate 1
  226. %endrep
  227. %endmacro
  228. %macro POP_IF_USED 1-*
  229. %rep %0
  230. %if %1 < regs_used
  231. pop r%1
  232. %endif
  233. %rotate 1
  234. %endrep
  235. %endmacro
  236. %macro LOAD_IF_USED 1-*
  237. %rep %0
  238. %if %1 < num_args
  239. mov r%1, r %+ %1 %+ mp
  240. %endif
  241. %rotate 1
  242. %endrep
  243. %endmacro
  244. %macro SUB 2
  245. sub %1, %2
  246. %ifidn %1, rstk
  247. %assign stack_offset stack_offset+(%2)
  248. %endif
  249. %endmacro
  250. %macro ADD 2
  251. add %1, %2
  252. %ifidn %1, rstk
  253. %assign stack_offset stack_offset-(%2)
  254. %endif
  255. %endmacro
  256. %macro movifnidn 2
  257. %ifnidn %1, %2
  258. mov %1, %2
  259. %endif
  260. %endmacro
  261. %macro movsxdifnidn 2
  262. %ifnidn %1, %2
  263. movsxd %1, %2
  264. %endif
  265. %endmacro
  266. %macro ASSERT 1
  267. %if (%1) == 0
  268. %error assertion ``%1'' failed
  269. %endif
  270. %endmacro
  271. %macro DEFINE_ARGS 0-*
  272. %ifdef n_arg_names
  273. %assign %%i 0
  274. %rep n_arg_names
  275. CAT_UNDEF arg_name %+ %%i, q
  276. CAT_UNDEF arg_name %+ %%i, d
  277. CAT_UNDEF arg_name %+ %%i, w
  278. CAT_UNDEF arg_name %+ %%i, h
  279. CAT_UNDEF arg_name %+ %%i, b
  280. CAT_UNDEF arg_name %+ %%i, m
  281. CAT_UNDEF arg_name %+ %%i, mp
  282. CAT_UNDEF arg_name, %%i
  283. %assign %%i %%i+1
  284. %endrep
  285. %endif
  286. %xdefine %%stack_offset stack_offset
  287. %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
  288. %assign %%i 0
  289. %rep %0
  290. %xdefine %1q r %+ %%i %+ q
  291. %xdefine %1d r %+ %%i %+ d
  292. %xdefine %1w r %+ %%i %+ w
  293. %xdefine %1h r %+ %%i %+ h
  294. %xdefine %1b r %+ %%i %+ b
  295. %xdefine %1m r %+ %%i %+ m
  296. %xdefine %1mp r %+ %%i %+ mp
  297. CAT_XDEFINE arg_name, %%i, %1
  298. %assign %%i %%i+1
  299. %rotate 1
  300. %endrep
  301. %xdefine stack_offset %%stack_offset
  302. %assign n_arg_names %0
  303. %endmacro
  304. %define required_stack_alignment ((mmsize + 15) & ~15)
  305. %define vzeroupper_required (mmsize > 16 && (ARCH_X86_64 == 0 || xmm_regs_used > 16 || notcpuflag(avx512)))
  306. %define high_mm_regs (16*cpuflag(avx512))
  307. %macro ALLOC_STACK 1-2 0 ; stack_size, n_xmm_regs (for win64 only)
  308. %ifnum %1
  309. %if %1 != 0
  310. %assign %%pad 0
  311. %assign stack_size %1
  312. %if stack_size < 0
  313. %assign stack_size -stack_size
  314. %endif
  315. %if WIN64
  316. %assign %%pad %%pad + 32 ; shadow space
  317. %if mmsize != 8
  318. %assign xmm_regs_used %2
  319. %if xmm_regs_used > 8
  320. %assign %%pad %%pad + (xmm_regs_used-8)*16 ; callee-saved xmm registers
  321. %endif
  322. %endif
  323. %endif
  324. %if required_stack_alignment <= STACK_ALIGNMENT
  325. ; maintain the current stack alignment
  326. %assign stack_size_padded stack_size + %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
  327. SUB rsp, stack_size_padded
  328. %else
  329. %assign %%reg_num (regs_used - 1)
  330. %xdefine rstk r %+ %%reg_num
  331. ; align stack, and save original stack location directly above
  332. ; it, i.e. in [rsp+stack_size_padded], so we can restore the
  333. ; stack in a single instruction (i.e. mov rsp, rstk or mov
  334. ; rsp, [rsp+stack_size_padded])
  335. %if %1 < 0 ; need to store rsp on stack
  336. %xdefine rstkm [rsp + stack_size + %%pad]
  337. %assign %%pad %%pad + gprsize
  338. %else ; can keep rsp in rstk during whole function
  339. %xdefine rstkm rstk
  340. %endif
  341. %assign stack_size_padded stack_size + ((%%pad + required_stack_alignment-1) & ~(required_stack_alignment-1))
  342. mov rstk, rsp
  343. and rsp, ~(required_stack_alignment-1)
  344. sub rsp, stack_size_padded
  345. movifnidn rstkm, rstk
  346. %endif
  347. WIN64_PUSH_XMM
  348. %endif
  349. %endif
  350. %endmacro
  351. %macro SETUP_STACK_POINTER 1
  352. %ifnum %1
  353. %if %1 != 0 && required_stack_alignment > STACK_ALIGNMENT
  354. %if %1 > 0
  355. ; Reserve an additional register for storing the original stack pointer, but avoid using
  356. ; eax/rax for this purpose since it can potentially get overwritten as a return value.
  357. %assign regs_used (regs_used + 1)
  358. %if ARCH_X86_64 && regs_used == 7
  359. %assign regs_used 8
  360. %elif ARCH_X86_64 == 0 && regs_used == 1
  361. %assign regs_used 2
  362. %endif
  363. %endif
  364. %if ARCH_X86_64 && regs_used < 5 + UNIX64 * 3
  365. ; Ensure that we don't clobber any registers containing arguments. For UNIX64 we also preserve r6 (rax)
  366. ; since it's used as a hidden argument in vararg functions to specify the number of vector registers used.
  367. %assign regs_used 5 + UNIX64 * 3
  368. %endif
  369. %endif
  370. %endif
  371. %endmacro
  372. %if WIN64 ; Windows x64 ;=================================================
  373. DECLARE_REG 0, rcx
  374. DECLARE_REG 1, rdx
  375. DECLARE_REG 2, R8
  376. DECLARE_REG 3, R9
  377. DECLARE_REG 4, R10, 40
  378. DECLARE_REG 5, R11, 48
  379. DECLARE_REG 6, rax, 56
  380. DECLARE_REG 7, rdi, 64
  381. DECLARE_REG 8, rsi, 72
  382. DECLARE_REG 9, rbx, 80
  383. DECLARE_REG 10, rbp, 88
  384. DECLARE_REG 11, R14, 96
  385. DECLARE_REG 12, R15, 104
  386. DECLARE_REG 13, R12, 112
  387. DECLARE_REG 14, R13, 120
  388. %macro PROLOGUE 2-5+ 0, 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  389. %assign num_args %1
  390. %assign regs_used %2
  391. ASSERT regs_used >= num_args
  392. SETUP_STACK_POINTER %4
  393. ASSERT regs_used <= 15
  394. PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
  395. ALLOC_STACK %4, %3
  396. %if mmsize != 8 && stack_size == 0
  397. WIN64_SPILL_XMM %3
  398. %endif
  399. LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  400. %if %0 > 4
  401. %ifnum %4
  402. DEFINE_ARGS %5
  403. %else
  404. DEFINE_ARGS %4, %5
  405. %endif
  406. %elifnnum %4
  407. DEFINE_ARGS %4
  408. %endif
  409. %endmacro
  410. %macro WIN64_PUSH_XMM 0
  411. ; Use the shadow space to store XMM6 and XMM7, the rest needs stack space allocated.
  412. %if xmm_regs_used > 6 + high_mm_regs
  413. movaps [rstk + stack_offset + 8], xmm6
  414. %endif
  415. %if xmm_regs_used > 7 + high_mm_regs
  416. movaps [rstk + stack_offset + 24], xmm7
  417. %endif
  418. %assign %%xmm_regs_on_stack xmm_regs_used - high_mm_regs - 8
  419. %if %%xmm_regs_on_stack > 0
  420. %assign %%i 8
  421. %rep %%xmm_regs_on_stack
  422. movaps [rsp + (%%i-8)*16 + stack_size + 32], xmm %+ %%i
  423. %assign %%i %%i+1
  424. %endrep
  425. %endif
  426. %endmacro
  427. %macro WIN64_SPILL_XMM 1
  428. %assign xmm_regs_used %1
  429. ASSERT xmm_regs_used <= 16 + high_mm_regs
  430. %assign %%xmm_regs_on_stack xmm_regs_used - high_mm_regs - 8
  431. %if %%xmm_regs_on_stack > 0
  432. ; Allocate stack space for callee-saved xmm registers plus shadow space and align the stack.
  433. %assign %%pad %%xmm_regs_on_stack*16 + 32
  434. %assign stack_size_padded %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
  435. SUB rsp, stack_size_padded
  436. %endif
  437. WIN64_PUSH_XMM
  438. %endmacro
  439. %macro WIN64_RESTORE_XMM_INTERNAL 0
  440. %assign %%pad_size 0
  441. %assign %%xmm_regs_on_stack xmm_regs_used - high_mm_regs - 8
  442. %if %%xmm_regs_on_stack > 0
  443. %assign %%i xmm_regs_used - high_mm_regs
  444. %rep %%xmm_regs_on_stack
  445. %assign %%i %%i-1
  446. movaps xmm %+ %%i, [rsp + (%%i-8)*16 + stack_size + 32]
  447. %endrep
  448. %endif
  449. %if stack_size_padded > 0
  450. %if stack_size > 0 && required_stack_alignment > STACK_ALIGNMENT
  451. mov rsp, rstkm
  452. %else
  453. add rsp, stack_size_padded
  454. %assign %%pad_size stack_size_padded
  455. %endif
  456. %endif
  457. %if xmm_regs_used > 7 + high_mm_regs
  458. movaps xmm7, [rsp + stack_offset - %%pad_size + 24]
  459. %endif
  460. %if xmm_regs_used > 6 + high_mm_regs
  461. movaps xmm6, [rsp + stack_offset - %%pad_size + 8]
  462. %endif
  463. %endmacro
  464. %macro WIN64_RESTORE_XMM 0
  465. WIN64_RESTORE_XMM_INTERNAL
  466. %assign stack_offset (stack_offset-stack_size_padded)
  467. %assign stack_size_padded 0
  468. %assign xmm_regs_used 0
  469. %endmacro
  470. %define has_epilogue regs_used > 7 || stack_size > 0 || vzeroupper_required || xmm_regs_used > 6+high_mm_regs
  471. %macro RET 0
  472. WIN64_RESTORE_XMM_INTERNAL
  473. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  474. %if vzeroupper_required
  475. vzeroupper
  476. %endif
  477. AUTO_REP_RET
  478. %endmacro
  479. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  480. DECLARE_REG 0, rdi
  481. DECLARE_REG 1, rsi
  482. DECLARE_REG 2, rdx
  483. DECLARE_REG 3, rcx
  484. DECLARE_REG 4, R8
  485. DECLARE_REG 5, R9
  486. DECLARE_REG 6, rax, 8
  487. DECLARE_REG 7, R10, 16
  488. DECLARE_REG 8, R11, 24
  489. DECLARE_REG 9, rbx, 32
  490. DECLARE_REG 10, rbp, 40
  491. DECLARE_REG 11, R14, 48
  492. DECLARE_REG 12, R15, 56
  493. DECLARE_REG 13, R12, 64
  494. DECLARE_REG 14, R13, 72
  495. %macro PROLOGUE 2-5+ 0, 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  496. %assign num_args %1
  497. %assign regs_used %2
  498. %assign xmm_regs_used %3
  499. ASSERT regs_used >= num_args
  500. SETUP_STACK_POINTER %4
  501. ASSERT regs_used <= 15
  502. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  503. ALLOC_STACK %4
  504. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  505. %if %0 > 4
  506. %ifnum %4
  507. DEFINE_ARGS %5
  508. %else
  509. DEFINE_ARGS %4, %5
  510. %endif
  511. %elifnnum %4
  512. DEFINE_ARGS %4
  513. %endif
  514. %endmacro
  515. %define has_epilogue regs_used > 9 || stack_size > 0 || vzeroupper_required
  516. %macro RET 0
  517. %if stack_size_padded > 0
  518. %if required_stack_alignment > STACK_ALIGNMENT
  519. mov rsp, rstkm
  520. %else
  521. add rsp, stack_size_padded
  522. %endif
  523. %endif
  524. POP_IF_USED 14, 13, 12, 11, 10, 9
  525. %if vzeroupper_required
  526. vzeroupper
  527. %endif
  528. AUTO_REP_RET
  529. %endmacro
  530. %else ; X86_32 ;==============================================================
  531. DECLARE_REG 0, eax, 4
  532. DECLARE_REG 1, ecx, 8
  533. DECLARE_REG 2, edx, 12
  534. DECLARE_REG 3, ebx, 16
  535. DECLARE_REG 4, esi, 20
  536. DECLARE_REG 5, edi, 24
  537. DECLARE_REG 6, ebp, 28
  538. %define rsp esp
  539. %macro DECLARE_ARG 1-*
  540. %rep %0
  541. %define r%1m [rstk + stack_offset + 4*%1 + 4]
  542. %define r%1mp dword r%1m
  543. %rotate 1
  544. %endrep
  545. %endmacro
  546. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  547. %macro PROLOGUE 2-5+ 0, 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  548. %assign num_args %1
  549. %assign regs_used %2
  550. ASSERT regs_used >= num_args
  551. %if num_args > 7
  552. %assign num_args 7
  553. %endif
  554. %if regs_used > 7
  555. %assign regs_used 7
  556. %endif
  557. SETUP_STACK_POINTER %4
  558. ASSERT regs_used <= 7
  559. PUSH_IF_USED 3, 4, 5, 6
  560. ALLOC_STACK %4
  561. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  562. %if %0 > 4
  563. %ifnum %4
  564. DEFINE_ARGS %5
  565. %else
  566. DEFINE_ARGS %4, %5
  567. %endif
  568. %elifnnum %4
  569. DEFINE_ARGS %4
  570. %endif
  571. %endmacro
  572. %define has_epilogue regs_used > 3 || stack_size > 0 || vzeroupper_required
  573. %macro RET 0
  574. %if stack_size_padded > 0
  575. %if required_stack_alignment > STACK_ALIGNMENT
  576. mov rsp, rstkm
  577. %else
  578. add rsp, stack_size_padded
  579. %endif
  580. %endif
  581. POP_IF_USED 6, 5, 4, 3
  582. %if vzeroupper_required
  583. vzeroupper
  584. %endif
  585. AUTO_REP_RET
  586. %endmacro
  587. %endif ;======================================================================
  588. %if WIN64 == 0
  589. %macro WIN64_SPILL_XMM 1
  590. %endmacro
  591. %macro WIN64_RESTORE_XMM 0
  592. %endmacro
  593. %macro WIN64_PUSH_XMM 0
  594. %endmacro
  595. %endif
  596. ; On AMD cpus <=K10, an ordinary ret is slow if it immediately follows either
  597. ; a branch or a branch target. So switch to a 2-byte form of ret in that case.
  598. ; We can automatically detect "follows a branch", but not a branch target.
  599. ; (SSSE3 is a sufficient condition to know that your cpu doesn't have this problem.)
  600. %macro REP_RET 0
  601. %if has_epilogue || cpuflag(ssse3)
  602. RET
  603. %else
  604. rep ret
  605. %endif
  606. annotate_function_size
  607. %endmacro
  608. %define last_branch_adr $$
  609. %macro AUTO_REP_RET 0
  610. %if notcpuflag(ssse3)
  611. times ((last_branch_adr-$)>>31)+1 rep ; times 1 iff $ == last_branch_adr.
  612. %endif
  613. ret
  614. annotate_function_size
  615. %endmacro
  616. %macro BRANCH_INSTR 0-*
  617. %rep %0
  618. %macro %1 1-2 %1
  619. %2 %1
  620. %if notcpuflag(ssse3)
  621. %%branch_instr equ $
  622. %xdefine last_branch_adr %%branch_instr
  623. %endif
  624. %endmacro
  625. %rotate 1
  626. %endrep
  627. %endmacro
  628. BRANCH_INSTR jz, je, jnz, jne, jl, jle, jnl, jnle, jg, jge, jng, jnge, ja, jae, jna, jnae, jb, jbe, jnb, jnbe, jc, jnc, js, jns, jo, jno, jp, jnp
  629. %macro TAIL_CALL 2 ; callee, is_nonadjacent
  630. %if has_epilogue
  631. call %1
  632. RET
  633. %elif %2
  634. jmp %1
  635. %endif
  636. annotate_function_size
  637. %endmacro
  638. ;=============================================================================
  639. ; arch-independent part
  640. ;=============================================================================
  641. %assign function_align 16
  642. ; Begin a function.
  643. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  644. ; subsequent uses of the function name automatically refer to the mangled version.
  645. ; Appends cpuflags to the function name if cpuflags has been specified.
  646. ; The "" empty default parameter is a workaround for nasm, which fails if SUFFIX
  647. ; is empty and we call cglobal_internal with just %1 %+ SUFFIX (without %2).
  648. %macro cglobal 1-2+ "" ; name, [PROLOGUE args]
  649. cglobal_internal 1, %1 %+ SUFFIX, %2
  650. %endmacro
  651. %macro cvisible 1-2+ "" ; name, [PROLOGUE args]
  652. cglobal_internal 0, %1 %+ SUFFIX, %2
  653. %endmacro
  654. %macro cglobal_internal 2-3+
  655. annotate_function_size
  656. %if %1
  657. %xdefine %%FUNCTION_PREFIX private_prefix
  658. %xdefine %%VISIBILITY hidden
  659. %else
  660. %xdefine %%FUNCTION_PREFIX public_prefix
  661. %xdefine %%VISIBILITY
  662. %endif
  663. %ifndef cglobaled_%2
  664. %xdefine %2 mangle(%%FUNCTION_PREFIX %+ _ %+ %2)
  665. %xdefine %2.skip_prologue %2 %+ .skip_prologue
  666. CAT_XDEFINE cglobaled_, %2, 1
  667. %endif
  668. %xdefine current_function %2
  669. %xdefine current_function_section __SECT__
  670. %if FORMAT_ELF
  671. global %2:function %%VISIBILITY
  672. %else
  673. global %2
  674. %endif
  675. align function_align
  676. %2:
  677. RESET_MM_PERMUTATION ; needed for x86-64, also makes disassembly somewhat nicer
  678. %xdefine rstk rsp ; copy of the original stack pointer, used when greater alignment than the known stack alignment is required
  679. %assign stack_offset 0 ; stack pointer offset relative to the return address
  680. %assign stack_size 0 ; amount of stack space that can be freely used inside a function
  681. %assign stack_size_padded 0 ; total amount of allocated stack space, including space for callee-saved xmm registers on WIN64 and alignment padding
  682. %assign xmm_regs_used 0 ; number of XMM registers requested, used for dealing with callee-saved registers on WIN64 and vzeroupper
  683. %ifnidn %3, ""
  684. PROLOGUE %3
  685. %endif
  686. %endmacro
  687. ; Create a global symbol from a local label with the correct name mangling and type
  688. %macro cglobal_label 1
  689. %if FORMAT_ELF
  690. global current_function %+ %1:function hidden
  691. %else
  692. global current_function %+ %1
  693. %endif
  694. %1:
  695. %endmacro
  696. %macro cextern 1
  697. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  698. CAT_XDEFINE cglobaled_, %1, 1
  699. extern %1
  700. %endmacro
  701. ; like cextern, but without the prefix
  702. %macro cextern_naked 1
  703. %ifdef PREFIX
  704. %xdefine %1 mangle(%1)
  705. %endif
  706. CAT_XDEFINE cglobaled_, %1, 1
  707. extern %1
  708. %endmacro
  709. %macro const 1-2+
  710. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  711. %if FORMAT_ELF
  712. global %1:data hidden
  713. %else
  714. global %1
  715. %endif
  716. %1: %2
  717. %endmacro
  718. ; This is needed for ELF, otherwise the GNU linker assumes the stack is executable by default.
  719. %if FORMAT_ELF
  720. [SECTION .note.GNU-stack noalloc noexec nowrite progbits]
  721. %endif
  722. ; Tell debuggers how large the function was.
  723. ; This may be invoked multiple times per function; we rely on later instances overriding earlier ones.
  724. ; This is invoked by RET and similar macros, and also cglobal does it for the previous function,
  725. ; but if the last function in a source file doesn't use any of the standard macros for its epilogue,
  726. ; then its size might be unspecified.
  727. %macro annotate_function_size 0
  728. %ifdef __YASM_VER__
  729. %ifdef current_function
  730. %if FORMAT_ELF
  731. current_function_section
  732. %%ecf equ $
  733. size current_function %%ecf - current_function
  734. __SECT__
  735. %endif
  736. %endif
  737. %endif
  738. %endmacro
  739. ; cpuflags
  740. %assign cpuflags_mmx (1<<0)
  741. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  742. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  743. %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
  744. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  745. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  746. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  747. %assign cpuflags_lzcnt (1<<7) | cpuflags_sse2
  748. %assign cpuflags_sse3 (1<<8) | cpuflags_sse2
  749. %assign cpuflags_ssse3 (1<<9) | cpuflags_sse3
  750. %assign cpuflags_sse4 (1<<10)| cpuflags_ssse3
  751. %assign cpuflags_sse42 (1<<11)| cpuflags_sse4
  752. %assign cpuflags_aesni (1<<12)| cpuflags_sse42
  753. %assign cpuflags_avx (1<<13)| cpuflags_sse42
  754. %assign cpuflags_xop (1<<14)| cpuflags_avx
  755. %assign cpuflags_fma4 (1<<15)| cpuflags_avx
  756. %assign cpuflags_fma3 (1<<16)| cpuflags_avx
  757. %assign cpuflags_bmi1 (1<<17)| cpuflags_avx|cpuflags_lzcnt
  758. %assign cpuflags_bmi2 (1<<18)| cpuflags_bmi1
  759. %assign cpuflags_avx2 (1<<19)| cpuflags_fma3|cpuflags_bmi2
  760. %assign cpuflags_avx512 (1<<20)| cpuflags_avx2 ; F, CD, BW, DQ, VL
  761. %assign cpuflags_cache32 (1<<21)
  762. %assign cpuflags_cache64 (1<<22)
  763. %assign cpuflags_aligned (1<<23) ; not a cpu feature, but a function variant
  764. %assign cpuflags_atom (1<<24)
  765. ; Returns a boolean value expressing whether or not the specified cpuflag is enabled.
  766. %define cpuflag(x) (((((cpuflags & (cpuflags_ %+ x)) ^ (cpuflags_ %+ x)) - 1) >> 31) & 1)
  767. %define notcpuflag(x) (cpuflag(x) ^ 1)
  768. ; Takes an arbitrary number of cpuflags from the above list.
  769. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  770. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  771. %macro INIT_CPUFLAGS 0-*
  772. %xdefine SUFFIX
  773. %undef cpuname
  774. %assign cpuflags 0
  775. %if %0 >= 1
  776. %rep %0
  777. %ifdef cpuname
  778. %xdefine cpuname cpuname %+ _%1
  779. %else
  780. %xdefine cpuname %1
  781. %endif
  782. %assign cpuflags cpuflags | cpuflags_%1
  783. %rotate 1
  784. %endrep
  785. %xdefine SUFFIX _ %+ cpuname
  786. %if cpuflag(avx)
  787. %assign avx_enabled 1
  788. %endif
  789. %if (mmsize == 16 && notcpuflag(sse2)) || (mmsize == 32 && notcpuflag(avx2))
  790. %define mova movaps
  791. %define movu movups
  792. %define movnta movntps
  793. %endif
  794. %if cpuflag(aligned)
  795. %define movu mova
  796. %elif cpuflag(sse3) && notcpuflag(ssse3)
  797. %define movu lddqu
  798. %endif
  799. %endif
  800. %if ARCH_X86_64 || cpuflag(sse2)
  801. CPUNOP amdnop
  802. %else
  803. CPUNOP basicnop
  804. %endif
  805. %endmacro
  806. ; Merge mmx, sse*, and avx*
  807. ; m# is a simd register of the currently selected size
  808. ; xm# is the corresponding xmm register if mmsize >= 16, otherwise the same as m#
  809. ; ym# is the corresponding ymm register if mmsize >= 32, otherwise the same as m#
  810. ; zm# is the corresponding zmm register if mmsize >= 64, otherwise the same as m#
  811. ; (All 4 remain in sync through SWAP.)
  812. %macro CAT_XDEFINE 3
  813. %xdefine %1%2 %3
  814. %endmacro
  815. %macro CAT_UNDEF 2
  816. %undef %1%2
  817. %endmacro
  818. %macro DEFINE_MMREGS 1 ; mmtype
  819. %assign %%prev_mmregs 0
  820. %ifdef num_mmregs
  821. %assign %%prev_mmregs num_mmregs
  822. %endif
  823. %assign num_mmregs 8
  824. %if ARCH_X86_64 && mmsize >= 16
  825. %assign num_mmregs 16
  826. %if cpuflag(avx512) || mmsize == 64
  827. %assign num_mmregs 32
  828. %endif
  829. %endif
  830. %assign %%i 0
  831. %rep num_mmregs
  832. CAT_XDEFINE m, %%i, %1 %+ %%i
  833. CAT_XDEFINE nn%1, %%i, %%i
  834. %assign %%i %%i+1
  835. %endrep
  836. %if %%prev_mmregs > num_mmregs
  837. %rep %%prev_mmregs - num_mmregs
  838. CAT_UNDEF m, %%i
  839. CAT_UNDEF nn %+ mmtype, %%i
  840. %assign %%i %%i+1
  841. %endrep
  842. %endif
  843. %xdefine mmtype %1
  844. %endmacro
  845. ; Prefer registers 16-31 over 0-15 to avoid having to use vzeroupper
  846. %macro AVX512_MM_PERMUTATION 0-1 0 ; start_reg
  847. %if ARCH_X86_64 && cpuflag(avx512)
  848. %assign %%i %1
  849. %rep 16-%1
  850. %assign %%i_high %%i+16
  851. SWAP %%i, %%i_high
  852. %assign %%i %%i+1
  853. %endrep
  854. %endif
  855. %endmacro
  856. %macro INIT_MMX 0-1+
  857. %assign avx_enabled 0
  858. %define RESET_MM_PERMUTATION INIT_MMX %1
  859. %define mmsize 8
  860. %define mova movq
  861. %define movu movq
  862. %define movh movd
  863. %define movnta movntq
  864. INIT_CPUFLAGS %1
  865. DEFINE_MMREGS mm
  866. %endmacro
  867. %macro INIT_XMM 0-1+
  868. %assign avx_enabled 0
  869. %define RESET_MM_PERMUTATION INIT_XMM %1
  870. %define mmsize 16
  871. %define mova movdqa
  872. %define movu movdqu
  873. %define movh movq
  874. %define movnta movntdq
  875. INIT_CPUFLAGS %1
  876. DEFINE_MMREGS xmm
  877. %if WIN64
  878. AVX512_MM_PERMUTATION 6 ; Swap callee-saved registers with volatile registers
  879. %endif
  880. %endmacro
  881. %macro INIT_YMM 0-1+
  882. %assign avx_enabled 1
  883. %define RESET_MM_PERMUTATION INIT_YMM %1
  884. %define mmsize 32
  885. %define mova movdqa
  886. %define movu movdqu
  887. %undef movh
  888. %define movnta movntdq
  889. INIT_CPUFLAGS %1
  890. DEFINE_MMREGS ymm
  891. AVX512_MM_PERMUTATION
  892. %endmacro
  893. %macro INIT_ZMM 0-1+
  894. %assign avx_enabled 1
  895. %define RESET_MM_PERMUTATION INIT_ZMM %1
  896. %define mmsize 64
  897. %define mova movdqa
  898. %define movu movdqu
  899. %undef movh
  900. %define movnta movntdq
  901. INIT_CPUFLAGS %1
  902. DEFINE_MMREGS zmm
  903. AVX512_MM_PERMUTATION
  904. %endmacro
  905. INIT_XMM
  906. %macro DECLARE_MMCAST 1
  907. %define mmmm%1 mm%1
  908. %define mmxmm%1 mm%1
  909. %define mmymm%1 mm%1
  910. %define mmzmm%1 mm%1
  911. %define xmmmm%1 mm%1
  912. %define xmmxmm%1 xmm%1
  913. %define xmmymm%1 xmm%1
  914. %define xmmzmm%1 xmm%1
  915. %define ymmmm%1 mm%1
  916. %define ymmxmm%1 xmm%1
  917. %define ymmymm%1 ymm%1
  918. %define ymmzmm%1 ymm%1
  919. %define zmmmm%1 mm%1
  920. %define zmmxmm%1 xmm%1
  921. %define zmmymm%1 ymm%1
  922. %define zmmzmm%1 zmm%1
  923. %define xm%1 xmm %+ m%1
  924. %define ym%1 ymm %+ m%1
  925. %define zm%1 zmm %+ m%1
  926. %endmacro
  927. %assign i 0
  928. %rep 32
  929. DECLARE_MMCAST i
  930. %assign i i+1
  931. %endrep
  932. ; I often want to use macros that permute their arguments. e.g. there's no
  933. ; efficient way to implement butterfly or transpose or dct without swapping some
  934. ; arguments.
  935. ;
  936. ; I would like to not have to manually keep track of the permutations:
  937. ; If I insert a permutation in the middle of a function, it should automatically
  938. ; change everything that follows. For more complex macros I may also have multiple
  939. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  940. ;
  941. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  942. ; permutes its arguments. It's equivalent to exchanging the contents of the
  943. ; registers, except that this way you exchange the register names instead, so it
  944. ; doesn't cost any cycles.
  945. %macro PERMUTE 2-* ; takes a list of pairs to swap
  946. %rep %0/2
  947. %xdefine %%tmp%2 m%2
  948. %rotate 2
  949. %endrep
  950. %rep %0/2
  951. %xdefine m%1 %%tmp%2
  952. CAT_XDEFINE nn, m%1, %1
  953. %rotate 2
  954. %endrep
  955. %endmacro
  956. %macro SWAP 2+ ; swaps a single chain (sometimes more concise than pairs)
  957. %ifnum %1 ; SWAP 0, 1, ...
  958. SWAP_INTERNAL_NUM %1, %2
  959. %else ; SWAP m0, m1, ...
  960. SWAP_INTERNAL_NAME %1, %2
  961. %endif
  962. %endmacro
  963. %macro SWAP_INTERNAL_NUM 2-*
  964. %rep %0-1
  965. %xdefine %%tmp m%1
  966. %xdefine m%1 m%2
  967. %xdefine m%2 %%tmp
  968. CAT_XDEFINE nn, m%1, %1
  969. CAT_XDEFINE nn, m%2, %2
  970. %rotate 1
  971. %endrep
  972. %endmacro
  973. %macro SWAP_INTERNAL_NAME 2-*
  974. %xdefine %%args nn %+ %1
  975. %rep %0-1
  976. %xdefine %%args %%args, nn %+ %2
  977. %rotate 1
  978. %endrep
  979. SWAP_INTERNAL_NUM %%args
  980. %endmacro
  981. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  982. ; calls to that function will automatically load the permutation, so values can
  983. ; be returned in mmregs.
  984. %macro SAVE_MM_PERMUTATION 0-1
  985. %if %0
  986. %xdefine %%f %1_m
  987. %else
  988. %xdefine %%f current_function %+ _m
  989. %endif
  990. %assign %%i 0
  991. %rep num_mmregs
  992. CAT_XDEFINE %%f, %%i, m %+ %%i
  993. %assign %%i %%i+1
  994. %endrep
  995. %endmacro
  996. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  997. %ifdef %1_m0
  998. %assign %%i 0
  999. %rep num_mmregs
  1000. CAT_XDEFINE m, %%i, %1_m %+ %%i
  1001. CAT_XDEFINE nn, m %+ %%i, %%i
  1002. %assign %%i %%i+1
  1003. %endrep
  1004. %endif
  1005. %endmacro
  1006. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  1007. %macro call 1
  1008. %ifid %1
  1009. call_internal %1 %+ SUFFIX, %1
  1010. %else
  1011. call %1
  1012. %endif
  1013. %endmacro
  1014. %macro call_internal 2
  1015. %xdefine %%i %2
  1016. %ifndef cglobaled_%2
  1017. %ifdef cglobaled_%1
  1018. %xdefine %%i %1
  1019. %endif
  1020. %endif
  1021. call %%i
  1022. LOAD_MM_PERMUTATION %%i
  1023. %endmacro
  1024. ; Substitutions that reduce instruction size but are functionally equivalent
  1025. %macro add 2
  1026. %ifnum %2
  1027. %if %2==128
  1028. sub %1, -128
  1029. %else
  1030. add %1, %2
  1031. %endif
  1032. %else
  1033. add %1, %2
  1034. %endif
  1035. %endmacro
  1036. %macro sub 2
  1037. %ifnum %2
  1038. %if %2==128
  1039. add %1, -128
  1040. %else
  1041. sub %1, %2
  1042. %endif
  1043. %else
  1044. sub %1, %2
  1045. %endif
  1046. %endmacro
  1047. ;=============================================================================
  1048. ; AVX abstraction layer
  1049. ;=============================================================================
  1050. %assign i 0
  1051. %rep 32
  1052. %if i < 8
  1053. CAT_XDEFINE sizeofmm, i, 8
  1054. CAT_XDEFINE regnumofmm, i, i
  1055. %endif
  1056. CAT_XDEFINE sizeofxmm, i, 16
  1057. CAT_XDEFINE sizeofymm, i, 32
  1058. CAT_XDEFINE sizeofzmm, i, 64
  1059. CAT_XDEFINE regnumofxmm, i, i
  1060. CAT_XDEFINE regnumofymm, i, i
  1061. CAT_XDEFINE regnumofzmm, i, i
  1062. %assign i i+1
  1063. %endrep
  1064. %undef i
  1065. %macro CHECK_AVX_INSTR_EMU 3-*
  1066. %xdefine %%opcode %1
  1067. %xdefine %%dst %2
  1068. %rep %0-2
  1069. %ifidn %%dst, %3
  1070. %error non-avx emulation of ``%%opcode'' is not supported
  1071. %endif
  1072. %rotate 1
  1073. %endrep
  1074. %endmacro
  1075. ;%1 == instruction
  1076. ;%2 == minimal instruction set
  1077. ;%3 == 1 if float, 0 if int
  1078. ;%4 == 1 if 4-operand emulation, 0 if 3-operand emulation, 255 otherwise (no emulation)
  1079. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  1080. ;%6+: operands
  1081. %macro RUN_AVX_INSTR 6-9+
  1082. %ifnum sizeof%7
  1083. %assign __sizeofreg sizeof%7
  1084. %elifnum sizeof%6
  1085. %assign __sizeofreg sizeof%6
  1086. %else
  1087. %assign __sizeofreg mmsize
  1088. %endif
  1089. %assign __emulate_avx 0
  1090. %if avx_enabled && __sizeofreg >= 16
  1091. %xdefine __instr v%1
  1092. %else
  1093. %xdefine __instr %1
  1094. %if %0 >= 8+%4
  1095. %assign __emulate_avx 1
  1096. %endif
  1097. %endif
  1098. %ifnidn %2, fnord
  1099. %ifdef cpuname
  1100. %if notcpuflag(%2)
  1101. %error use of ``%1'' %2 instruction in cpuname function: current_function
  1102. %elif cpuflags_%2 < cpuflags_sse && notcpuflag(sse2) && __sizeofreg > 8
  1103. %error use of ``%1'' sse2 instruction in cpuname function: current_function
  1104. %endif
  1105. %endif
  1106. %endif
  1107. %if __emulate_avx
  1108. %xdefine __src1 %7
  1109. %xdefine __src2 %8
  1110. %if %5 && %4 == 0
  1111. %ifnidn %6, %7
  1112. %ifidn %6, %8
  1113. %xdefine __src1 %8
  1114. %xdefine __src2 %7
  1115. %elifnnum sizeof%8
  1116. ; 3-operand AVX instructions with a memory arg can only have it in src2,
  1117. ; whereas SSE emulation prefers to have it in src1 (i.e. the mov).
  1118. ; So, if the instruction is commutative with a memory arg, swap them.
  1119. %xdefine __src1 %8
  1120. %xdefine __src2 %7
  1121. %endif
  1122. %endif
  1123. %endif
  1124. %ifnidn %6, __src1
  1125. %if %0 >= 9
  1126. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8, %9}, %6, __src2, %9
  1127. %else
  1128. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8}, %6, __src2
  1129. %endif
  1130. %if __sizeofreg == 8
  1131. MOVQ %6, __src1
  1132. %elif %3
  1133. MOVAPS %6, __src1
  1134. %else
  1135. MOVDQA %6, __src1
  1136. %endif
  1137. %endif
  1138. %if %0 >= 9
  1139. %1 %6, __src2, %9
  1140. %else
  1141. %1 %6, __src2
  1142. %endif
  1143. %elif %0 >= 9
  1144. __instr %6, %7, %8, %9
  1145. %elif %0 == 8
  1146. __instr %6, %7, %8
  1147. %elif %0 == 7
  1148. __instr %6, %7
  1149. %else
  1150. __instr %6
  1151. %endif
  1152. %endmacro
  1153. ;%1 == instruction
  1154. ;%2 == minimal instruction set
  1155. ;%3 == 1 if float, 0 if int
  1156. ;%4 == 1 if 4-operand emulation, 0 if 3-operand emulation, 255 otherwise (no emulation)
  1157. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  1158. %macro AVX_INSTR 1-5 fnord, 0, 255, 0
  1159. %macro %1 1-10 fnord, fnord, fnord, fnord, %1, %2, %3, %4, %5
  1160. %ifidn %2, fnord
  1161. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1
  1162. %elifidn %3, fnord
  1163. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2
  1164. %elifidn %4, fnord
  1165. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3
  1166. %elifidn %5, fnord
  1167. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4
  1168. %else
  1169. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4, %5
  1170. %endif
  1171. %endmacro
  1172. %endmacro
  1173. ; Instructions with both VEX/EVEX and legacy encodings
  1174. ; Non-destructive instructions are written without parameters
  1175. AVX_INSTR addpd, sse2, 1, 0, 1
  1176. AVX_INSTR addps, sse, 1, 0, 1
  1177. AVX_INSTR addsd, sse2, 1, 0, 0
  1178. AVX_INSTR addss, sse, 1, 0, 0
  1179. AVX_INSTR addsubpd, sse3, 1, 0, 0
  1180. AVX_INSTR addsubps, sse3, 1, 0, 0
  1181. AVX_INSTR aesdec, aesni, 0, 0, 0
  1182. AVX_INSTR aesdeclast, aesni, 0, 0, 0
  1183. AVX_INSTR aesenc, aesni, 0, 0, 0
  1184. AVX_INSTR aesenclast, aesni, 0, 0, 0
  1185. AVX_INSTR aesimc, aesni
  1186. AVX_INSTR aeskeygenassist, aesni
  1187. AVX_INSTR andnpd, sse2, 1, 0, 0
  1188. AVX_INSTR andnps, sse, 1, 0, 0
  1189. AVX_INSTR andpd, sse2, 1, 0, 1
  1190. AVX_INSTR andps, sse, 1, 0, 1
  1191. AVX_INSTR blendpd, sse4, 1, 1, 0
  1192. AVX_INSTR blendps, sse4, 1, 1, 0
  1193. AVX_INSTR blendvpd, sse4 ; can't be emulated
  1194. AVX_INSTR blendvps, sse4 ; can't be emulated
  1195. AVX_INSTR cmpeqpd, sse2, 1, 0, 1
  1196. AVX_INSTR cmpeqps, sse, 1, 0, 1
  1197. AVX_INSTR cmpeqsd, sse2, 1, 0, 0
  1198. AVX_INSTR cmpeqss, sse, 1, 0, 0
  1199. AVX_INSTR cmplepd, sse2, 1, 0, 0
  1200. AVX_INSTR cmpleps, sse, 1, 0, 0
  1201. AVX_INSTR cmplesd, sse2, 1, 0, 0
  1202. AVX_INSTR cmpless, sse, 1, 0, 0
  1203. AVX_INSTR cmpltpd, sse2, 1, 0, 0
  1204. AVX_INSTR cmpltps, sse, 1, 0, 0
  1205. AVX_INSTR cmpltsd, sse2, 1, 0, 0
  1206. AVX_INSTR cmpltss, sse, 1, 0, 0
  1207. AVX_INSTR cmpneqpd, sse2, 1, 0, 1
  1208. AVX_INSTR cmpneqps, sse, 1, 0, 1
  1209. AVX_INSTR cmpneqsd, sse2, 1, 0, 0
  1210. AVX_INSTR cmpneqss, sse, 1, 0, 0
  1211. AVX_INSTR cmpnlepd, sse2, 1, 0, 0
  1212. AVX_INSTR cmpnleps, sse, 1, 0, 0
  1213. AVX_INSTR cmpnlesd, sse2, 1, 0, 0
  1214. AVX_INSTR cmpnless, sse, 1, 0, 0
  1215. AVX_INSTR cmpnltpd, sse2, 1, 0, 0
  1216. AVX_INSTR cmpnltps, sse, 1, 0, 0
  1217. AVX_INSTR cmpnltsd, sse2, 1, 0, 0
  1218. AVX_INSTR cmpnltss, sse, 1, 0, 0
  1219. AVX_INSTR cmpordpd, sse2 1, 0, 1
  1220. AVX_INSTR cmpordps, sse 1, 0, 1
  1221. AVX_INSTR cmpordsd, sse2 1, 0, 0
  1222. AVX_INSTR cmpordss, sse 1, 0, 0
  1223. AVX_INSTR cmppd, sse2, 1, 1, 0
  1224. AVX_INSTR cmpps, sse, 1, 1, 0
  1225. AVX_INSTR cmpsd, sse2, 1, 1, 0
  1226. AVX_INSTR cmpss, sse, 1, 1, 0
  1227. AVX_INSTR cmpunordpd, sse2, 1, 0, 1
  1228. AVX_INSTR cmpunordps, sse, 1, 0, 1
  1229. AVX_INSTR cmpunordsd, sse2, 1, 0, 0
  1230. AVX_INSTR cmpunordss, sse, 1, 0, 0
  1231. AVX_INSTR comisd, sse2
  1232. AVX_INSTR comiss, sse
  1233. AVX_INSTR cvtdq2pd, sse2
  1234. AVX_INSTR cvtdq2ps, sse2
  1235. AVX_INSTR cvtpd2dq, sse2
  1236. AVX_INSTR cvtpd2ps, sse2
  1237. AVX_INSTR cvtps2dq, sse2
  1238. AVX_INSTR cvtps2pd, sse2
  1239. AVX_INSTR cvtsd2si, sse2
  1240. AVX_INSTR cvtsd2ss, sse2, 1, 0, 0
  1241. AVX_INSTR cvtsi2sd, sse2, 1, 0, 0
  1242. AVX_INSTR cvtsi2ss, sse, 1, 0, 0
  1243. AVX_INSTR cvtss2sd, sse2, 1, 0, 0
  1244. AVX_INSTR cvtss2si, sse
  1245. AVX_INSTR cvttpd2dq, sse2
  1246. AVX_INSTR cvttps2dq, sse2
  1247. AVX_INSTR cvttsd2si, sse2
  1248. AVX_INSTR cvttss2si, sse
  1249. AVX_INSTR divpd, sse2, 1, 0, 0
  1250. AVX_INSTR divps, sse, 1, 0, 0
  1251. AVX_INSTR divsd, sse2, 1, 0, 0
  1252. AVX_INSTR divss, sse, 1, 0, 0
  1253. AVX_INSTR dppd, sse4, 1, 1, 0
  1254. AVX_INSTR dpps, sse4, 1, 1, 0
  1255. AVX_INSTR extractps, sse4
  1256. AVX_INSTR haddpd, sse3, 1, 0, 0
  1257. AVX_INSTR haddps, sse3, 1, 0, 0
  1258. AVX_INSTR hsubpd, sse3, 1, 0, 0
  1259. AVX_INSTR hsubps, sse3, 1, 0, 0
  1260. AVX_INSTR insertps, sse4, 1, 1, 0
  1261. AVX_INSTR lddqu, sse3
  1262. AVX_INSTR ldmxcsr, sse
  1263. AVX_INSTR maskmovdqu, sse2
  1264. AVX_INSTR maxpd, sse2, 1, 0, 1
  1265. AVX_INSTR maxps, sse, 1, 0, 1
  1266. AVX_INSTR maxsd, sse2, 1, 0, 0
  1267. AVX_INSTR maxss, sse, 1, 0, 0
  1268. AVX_INSTR minpd, sse2, 1, 0, 1
  1269. AVX_INSTR minps, sse, 1, 0, 1
  1270. AVX_INSTR minsd, sse2, 1, 0, 0
  1271. AVX_INSTR minss, sse, 1, 0, 0
  1272. AVX_INSTR movapd, sse2
  1273. AVX_INSTR movaps, sse
  1274. AVX_INSTR movd, mmx
  1275. AVX_INSTR movddup, sse3
  1276. AVX_INSTR movdqa, sse2
  1277. AVX_INSTR movdqu, sse2
  1278. AVX_INSTR movhlps, sse, 1, 0, 0
  1279. AVX_INSTR movhpd, sse2, 1, 0, 0
  1280. AVX_INSTR movhps, sse, 1, 0, 0
  1281. AVX_INSTR movlhps, sse, 1, 0, 0
  1282. AVX_INSTR movlpd, sse2, 1, 0, 0
  1283. AVX_INSTR movlps, sse, 1, 0, 0
  1284. AVX_INSTR movmskpd, sse2
  1285. AVX_INSTR movmskps, sse
  1286. AVX_INSTR movntdq, sse2
  1287. AVX_INSTR movntdqa, sse4
  1288. AVX_INSTR movntpd, sse2
  1289. AVX_INSTR movntps, sse
  1290. AVX_INSTR movq, mmx
  1291. AVX_INSTR movsd, sse2, 1, 0, 0
  1292. AVX_INSTR movshdup, sse3
  1293. AVX_INSTR movsldup, sse3
  1294. AVX_INSTR movss, sse, 1, 0, 0
  1295. AVX_INSTR movupd, sse2
  1296. AVX_INSTR movups, sse
  1297. AVX_INSTR mpsadbw, sse4, 0, 1, 0
  1298. AVX_INSTR mulpd, sse2, 1, 0, 1
  1299. AVX_INSTR mulps, sse, 1, 0, 1
  1300. AVX_INSTR mulsd, sse2, 1, 0, 0
  1301. AVX_INSTR mulss, sse, 1, 0, 0
  1302. AVX_INSTR orpd, sse2, 1, 0, 1
  1303. AVX_INSTR orps, sse, 1, 0, 1
  1304. AVX_INSTR pabsb, ssse3
  1305. AVX_INSTR pabsd, ssse3
  1306. AVX_INSTR pabsw, ssse3
  1307. AVX_INSTR packsswb, mmx, 0, 0, 0
  1308. AVX_INSTR packssdw, mmx, 0, 0, 0
  1309. AVX_INSTR packuswb, mmx, 0, 0, 0
  1310. AVX_INSTR packusdw, sse4, 0, 0, 0
  1311. AVX_INSTR paddb, mmx, 0, 0, 1
  1312. AVX_INSTR paddw, mmx, 0, 0, 1
  1313. AVX_INSTR paddd, mmx, 0, 0, 1
  1314. AVX_INSTR paddq, sse2, 0, 0, 1
  1315. AVX_INSTR paddsb, mmx, 0, 0, 1
  1316. AVX_INSTR paddsw, mmx, 0, 0, 1
  1317. AVX_INSTR paddusb, mmx, 0, 0, 1
  1318. AVX_INSTR paddusw, mmx, 0, 0, 1
  1319. AVX_INSTR palignr, ssse3, 0, 1, 0
  1320. AVX_INSTR pand, mmx, 0, 0, 1
  1321. AVX_INSTR pandn, mmx, 0, 0, 0
  1322. AVX_INSTR pavgb, mmx2, 0, 0, 1
  1323. AVX_INSTR pavgw, mmx2, 0, 0, 1
  1324. AVX_INSTR pblendvb, sse4 ; can't be emulated
  1325. AVX_INSTR pblendw, sse4, 0, 1, 0
  1326. AVX_INSTR pclmulqdq, fnord, 0, 1, 0
  1327. AVX_INSTR pclmulhqhqdq, fnord, 0, 0, 0
  1328. AVX_INSTR pclmulhqlqdq, fnord, 0, 0, 0
  1329. AVX_INSTR pclmullqhqdq, fnord, 0, 0, 0
  1330. AVX_INSTR pclmullqlqdq, fnord, 0, 0, 0
  1331. AVX_INSTR pcmpestri, sse42
  1332. AVX_INSTR pcmpestrm, sse42
  1333. AVX_INSTR pcmpistri, sse42
  1334. AVX_INSTR pcmpistrm, sse42
  1335. AVX_INSTR pcmpeqb, mmx, 0, 0, 1
  1336. AVX_INSTR pcmpeqw, mmx, 0, 0, 1
  1337. AVX_INSTR pcmpeqd, mmx, 0, 0, 1
  1338. AVX_INSTR pcmpeqq, sse4, 0, 0, 1
  1339. AVX_INSTR pcmpgtb, mmx, 0, 0, 0
  1340. AVX_INSTR pcmpgtw, mmx, 0, 0, 0
  1341. AVX_INSTR pcmpgtd, mmx, 0, 0, 0
  1342. AVX_INSTR pcmpgtq, sse42, 0, 0, 0
  1343. AVX_INSTR pextrb, sse4
  1344. AVX_INSTR pextrd, sse4
  1345. AVX_INSTR pextrq, sse4
  1346. AVX_INSTR pextrw, mmx2
  1347. AVX_INSTR phaddw, ssse3, 0, 0, 0
  1348. AVX_INSTR phaddd, ssse3, 0, 0, 0
  1349. AVX_INSTR phaddsw, ssse3, 0, 0, 0
  1350. AVX_INSTR phminposuw, sse4
  1351. AVX_INSTR phsubw, ssse3, 0, 0, 0
  1352. AVX_INSTR phsubd, ssse3, 0, 0, 0
  1353. AVX_INSTR phsubsw, ssse3, 0, 0, 0
  1354. AVX_INSTR pinsrb, sse4, 0, 1, 0
  1355. AVX_INSTR pinsrd, sse4, 0, 1, 0
  1356. AVX_INSTR pinsrq, sse4, 0, 1, 0
  1357. AVX_INSTR pinsrw, mmx2, 0, 1, 0
  1358. AVX_INSTR pmaddwd, mmx, 0, 0, 1
  1359. AVX_INSTR pmaddubsw, ssse3, 0, 0, 0
  1360. AVX_INSTR pmaxsb, sse4, 0, 0, 1
  1361. AVX_INSTR pmaxsw, mmx2, 0, 0, 1
  1362. AVX_INSTR pmaxsd, sse4, 0, 0, 1
  1363. AVX_INSTR pmaxub, mmx2, 0, 0, 1
  1364. AVX_INSTR pmaxuw, sse4, 0, 0, 1
  1365. AVX_INSTR pmaxud, sse4, 0, 0, 1
  1366. AVX_INSTR pminsb, sse4, 0, 0, 1
  1367. AVX_INSTR pminsw, mmx2, 0, 0, 1
  1368. AVX_INSTR pminsd, sse4, 0, 0, 1
  1369. AVX_INSTR pminub, mmx2, 0, 0, 1
  1370. AVX_INSTR pminuw, sse4, 0, 0, 1
  1371. AVX_INSTR pminud, sse4, 0, 0, 1
  1372. AVX_INSTR pmovmskb, mmx2
  1373. AVX_INSTR pmovsxbw, sse4
  1374. AVX_INSTR pmovsxbd, sse4
  1375. AVX_INSTR pmovsxbq, sse4
  1376. AVX_INSTR pmovsxwd, sse4
  1377. AVX_INSTR pmovsxwq, sse4
  1378. AVX_INSTR pmovsxdq, sse4
  1379. AVX_INSTR pmovzxbw, sse4
  1380. AVX_INSTR pmovzxbd, sse4
  1381. AVX_INSTR pmovzxbq, sse4
  1382. AVX_INSTR pmovzxwd, sse4
  1383. AVX_INSTR pmovzxwq, sse4
  1384. AVX_INSTR pmovzxdq, sse4
  1385. AVX_INSTR pmuldq, sse4, 0, 0, 1
  1386. AVX_INSTR pmulhrsw, ssse3, 0, 0, 1
  1387. AVX_INSTR pmulhuw, mmx2, 0, 0, 1
  1388. AVX_INSTR pmulhw, mmx, 0, 0, 1
  1389. AVX_INSTR pmullw, mmx, 0, 0, 1
  1390. AVX_INSTR pmulld, sse4, 0, 0, 1
  1391. AVX_INSTR pmuludq, sse2, 0, 0, 1
  1392. AVX_INSTR por, mmx, 0, 0, 1
  1393. AVX_INSTR psadbw, mmx2, 0, 0, 1
  1394. AVX_INSTR pshufb, ssse3, 0, 0, 0
  1395. AVX_INSTR pshufd, sse2
  1396. AVX_INSTR pshufhw, sse2
  1397. AVX_INSTR pshuflw, sse2
  1398. AVX_INSTR psignb, ssse3, 0, 0, 0
  1399. AVX_INSTR psignw, ssse3, 0, 0, 0
  1400. AVX_INSTR psignd, ssse3, 0, 0, 0
  1401. AVX_INSTR psllw, mmx, 0, 0, 0
  1402. AVX_INSTR pslld, mmx, 0, 0, 0
  1403. AVX_INSTR psllq, mmx, 0, 0, 0
  1404. AVX_INSTR pslldq, sse2, 0, 0, 0
  1405. AVX_INSTR psraw, mmx, 0, 0, 0
  1406. AVX_INSTR psrad, mmx, 0, 0, 0
  1407. AVX_INSTR psrlw, mmx, 0, 0, 0
  1408. AVX_INSTR psrld, mmx, 0, 0, 0
  1409. AVX_INSTR psrlq, mmx, 0, 0, 0
  1410. AVX_INSTR psrldq, sse2, 0, 0, 0
  1411. AVX_INSTR psubb, mmx, 0, 0, 0
  1412. AVX_INSTR psubw, mmx, 0, 0, 0
  1413. AVX_INSTR psubd, mmx, 0, 0, 0
  1414. AVX_INSTR psubq, sse2, 0, 0, 0
  1415. AVX_INSTR psubsb, mmx, 0, 0, 0
  1416. AVX_INSTR psubsw, mmx, 0, 0, 0
  1417. AVX_INSTR psubusb, mmx, 0, 0, 0
  1418. AVX_INSTR psubusw, mmx, 0, 0, 0
  1419. AVX_INSTR ptest, sse4
  1420. AVX_INSTR punpckhbw, mmx, 0, 0, 0
  1421. AVX_INSTR punpckhwd, mmx, 0, 0, 0
  1422. AVX_INSTR punpckhdq, mmx, 0, 0, 0
  1423. AVX_INSTR punpckhqdq, sse2, 0, 0, 0
  1424. AVX_INSTR punpcklbw, mmx, 0, 0, 0
  1425. AVX_INSTR punpcklwd, mmx, 0, 0, 0
  1426. AVX_INSTR punpckldq, mmx, 0, 0, 0
  1427. AVX_INSTR punpcklqdq, sse2, 0, 0, 0
  1428. AVX_INSTR pxor, mmx, 0, 0, 1
  1429. AVX_INSTR rcpps, sse
  1430. AVX_INSTR rcpss, sse, 1, 0, 0
  1431. AVX_INSTR roundpd, sse4
  1432. AVX_INSTR roundps, sse4
  1433. AVX_INSTR roundsd, sse4, 1, 1, 0
  1434. AVX_INSTR roundss, sse4, 1, 1, 0
  1435. AVX_INSTR rsqrtps, sse
  1436. AVX_INSTR rsqrtss, sse, 1, 0, 0
  1437. AVX_INSTR shufpd, sse2, 1, 1, 0
  1438. AVX_INSTR shufps, sse, 1, 1, 0
  1439. AVX_INSTR sqrtpd, sse2
  1440. AVX_INSTR sqrtps, sse
  1441. AVX_INSTR sqrtsd, sse2, 1, 0, 0
  1442. AVX_INSTR sqrtss, sse, 1, 0, 0
  1443. AVX_INSTR stmxcsr, sse
  1444. AVX_INSTR subpd, sse2, 1, 0, 0
  1445. AVX_INSTR subps, sse, 1, 0, 0
  1446. AVX_INSTR subsd, sse2, 1, 0, 0
  1447. AVX_INSTR subss, sse, 1, 0, 0
  1448. AVX_INSTR ucomisd, sse2
  1449. AVX_INSTR ucomiss, sse
  1450. AVX_INSTR unpckhpd, sse2, 1, 0, 0
  1451. AVX_INSTR unpckhps, sse, 1, 0, 0
  1452. AVX_INSTR unpcklpd, sse2, 1, 0, 0
  1453. AVX_INSTR unpcklps, sse, 1, 0, 0
  1454. AVX_INSTR xorpd, sse2, 1, 0, 1
  1455. AVX_INSTR xorps, sse, 1, 0, 1
  1456. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  1457. AVX_INSTR pfadd, 3dnow, 1, 0, 1
  1458. AVX_INSTR pfsub, 3dnow, 1, 0, 0
  1459. AVX_INSTR pfmul, 3dnow, 1, 0, 1
  1460. ; base-4 constants for shuffles
  1461. %assign i 0
  1462. %rep 256
  1463. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  1464. %if j < 10
  1465. CAT_XDEFINE q000, j, i
  1466. %elif j < 100
  1467. CAT_XDEFINE q00, j, i
  1468. %elif j < 1000
  1469. CAT_XDEFINE q0, j, i
  1470. %else
  1471. CAT_XDEFINE q, j, i
  1472. %endif
  1473. %assign i i+1
  1474. %endrep
  1475. %undef i
  1476. %undef j
  1477. %macro FMA_INSTR 3
  1478. %macro %1 4-7 %1, %2, %3
  1479. %if cpuflag(xop)
  1480. v%5 %1, %2, %3, %4
  1481. %elifnidn %1, %4
  1482. %6 %1, %2, %3
  1483. %7 %1, %4
  1484. %else
  1485. %error non-xop emulation of ``%5 %1, %2, %3, %4'' is not supported
  1486. %endif
  1487. %endmacro
  1488. %endmacro
  1489. FMA_INSTR pmacsww, pmullw, paddw
  1490. FMA_INSTR pmacsdd, pmulld, paddd ; sse4 emulation
  1491. FMA_INSTR pmacsdql, pmuldq, paddq ; sse4 emulation
  1492. FMA_INSTR pmadcswd, pmaddwd, paddd
  1493. ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
  1494. ; This lets us use tzcnt without bumping the yasm version requirement yet.
  1495. %define tzcnt rep bsf
  1496. ; Macros for consolidating FMA3 and FMA4 using 4-operand (dst, src1, src2, src3) syntax.
  1497. ; FMA3 is only possible if dst is the same as one of the src registers.
  1498. ; Either src2 or src3 can be a memory operand.
  1499. %macro FMA4_INSTR 2-*
  1500. %push fma4_instr
  1501. %xdefine %$prefix %1
  1502. %rep %0 - 1
  1503. %macro %$prefix%2 4-6 %$prefix, %2
  1504. %if notcpuflag(fma3) && notcpuflag(fma4)
  1505. %error use of ``%5%6'' fma instruction in cpuname function: current_function
  1506. %elif cpuflag(fma4)
  1507. v%5%6 %1, %2, %3, %4
  1508. %elifidn %1, %2
  1509. ; If %3 or %4 is a memory operand it needs to be encoded as the last operand.
  1510. %ifnum sizeof%3
  1511. v%{5}213%6 %2, %3, %4
  1512. %else
  1513. v%{5}132%6 %2, %4, %3
  1514. %endif
  1515. %elifidn %1, %3
  1516. v%{5}213%6 %3, %2, %4
  1517. %elifidn %1, %4
  1518. v%{5}231%6 %4, %2, %3
  1519. %else
  1520. %error fma3 emulation of ``%5%6 %1, %2, %3, %4'' is not supported
  1521. %endif
  1522. %endmacro
  1523. %rotate 1
  1524. %endrep
  1525. %pop
  1526. %endmacro
  1527. FMA4_INSTR fmadd, pd, ps, sd, ss
  1528. FMA4_INSTR fmaddsub, pd, ps
  1529. FMA4_INSTR fmsub, pd, ps, sd, ss
  1530. FMA4_INSTR fmsubadd, pd, ps
  1531. FMA4_INSTR fnmadd, pd, ps, sd, ss
  1532. FMA4_INSTR fnmsub, pd, ps, sd, ss
  1533. ; Macros for converting VEX instructions to equivalent EVEX ones.
  1534. %macro EVEX_INSTR 2-3 0 ; vex, evex, prefer_evex
  1535. %macro %1 2-7 fnord, fnord, %1, %2, %3
  1536. %ifidn %3, fnord
  1537. %define %%args %1, %2
  1538. %elifidn %4, fnord
  1539. %define %%args %1, %2, %3
  1540. %else
  1541. %define %%args %1, %2, %3, %4
  1542. %endif
  1543. %assign %%evex_required cpuflag(avx512) & %7
  1544. %ifnum regnumof%1
  1545. %if regnumof%1 >= 16 || sizeof%1 > 32
  1546. %assign %%evex_required 1
  1547. %endif
  1548. %endif
  1549. %ifnum regnumof%2
  1550. %if regnumof%2 >= 16 || sizeof%2 > 32
  1551. %assign %%evex_required 1
  1552. %endif
  1553. %endif
  1554. %if %%evex_required
  1555. %6 %%args
  1556. %else
  1557. %5 %%args ; Prefer VEX over EVEX due to shorter instruction length
  1558. %endif
  1559. %endmacro
  1560. %endmacro
  1561. EVEX_INSTR vbroadcastf128, vbroadcastf32x4
  1562. EVEX_INSTR vbroadcasti128, vbroadcasti32x4
  1563. EVEX_INSTR vextractf128, vextractf32x4
  1564. EVEX_INSTR vextracti128, vextracti32x4
  1565. EVEX_INSTR vinsertf128, vinsertf32x4
  1566. EVEX_INSTR vinserti128, vinserti32x4
  1567. EVEX_INSTR vmovdqa, vmovdqa32
  1568. EVEX_INSTR vmovdqu, vmovdqu32
  1569. EVEX_INSTR vpand, vpandd
  1570. EVEX_INSTR vpandn, vpandnd
  1571. EVEX_INSTR vpor, vpord
  1572. EVEX_INSTR vpxor, vpxord
  1573. EVEX_INSTR vrcpps, vrcp14ps, 1 ; EVEX versions have higher precision
  1574. EVEX_INSTR vrcpss, vrcp14ss, 1
  1575. EVEX_INSTR vrsqrtps, vrsqrt14ps, 1
  1576. EVEX_INSTR vrsqrtss, vrsqrt14ss, 1
  1577. ; workaround: vpbroadcastq is broken in x86_32 due to a yasm bug (fixed in 1.3.0)
  1578. %ifdef __YASM_VER__
  1579. %if __YASM_VERSION_ID__ < 0x01030000 && ARCH_X86_64 == 0
  1580. %macro vpbroadcastq 2
  1581. %if sizeof%1 == 16
  1582. movddup %1, %2
  1583. %else
  1584. vbroadcastsd %1, %2
  1585. %endif
  1586. %endmacro
  1587. %endif
  1588. %endif