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.

1560 lines
44KB

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