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.

1552 lines
44KB

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