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.

1514 lines
42KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm: x264asm abstraction layer
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2015 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 assert 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. %assign regs_used (regs_used + 1)
  348. %elif ARCH_X86_64 && regs_used == num_args && num_args <= 4 + UNIX64 * 2
  349. %warning "Stack pointer will overwrite register argument"
  350. %endif
  351. %endif
  352. %endif
  353. %endmacro
  354. %macro DEFINE_ARGS_INTERNAL 3+
  355. %ifnum %2
  356. DEFINE_ARGS %3
  357. %elif %1 == 4
  358. DEFINE_ARGS %2
  359. %elif %1 > 4
  360. DEFINE_ARGS %2, %3
  361. %endif
  362. %endmacro
  363. %if WIN64 ; Windows x64 ;=================================================
  364. DECLARE_REG 0, rcx
  365. DECLARE_REG 1, rdx
  366. DECLARE_REG 2, R8
  367. DECLARE_REG 3, R9
  368. DECLARE_REG 4, R10, 40
  369. DECLARE_REG 5, R11, 48
  370. DECLARE_REG 6, rax, 56
  371. DECLARE_REG 7, rdi, 64
  372. DECLARE_REG 8, rsi, 72
  373. DECLARE_REG 9, rbx, 80
  374. DECLARE_REG 10, rbp, 88
  375. DECLARE_REG 11, R12, 96
  376. DECLARE_REG 12, R13, 104
  377. DECLARE_REG 13, R14, 112
  378. DECLARE_REG 14, R15, 120
  379. %macro PROLOGUE 2-5+ 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  380. %assign num_args %1
  381. %assign regs_used %2
  382. ASSERT regs_used >= num_args
  383. SETUP_STACK_POINTER %4
  384. ASSERT regs_used <= 15
  385. PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
  386. ALLOC_STACK %4, %3
  387. %if mmsize != 8 && stack_size == 0
  388. WIN64_SPILL_XMM %3
  389. %endif
  390. LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  391. DEFINE_ARGS_INTERNAL %0, %4, %5
  392. %endmacro
  393. %macro WIN64_PUSH_XMM 0
  394. ; Use the shadow space to store XMM6 and XMM7, the rest needs stack space allocated.
  395. %if xmm_regs_used > 6
  396. movaps [rstk + stack_offset + 8], xmm6
  397. %endif
  398. %if xmm_regs_used > 7
  399. movaps [rstk + stack_offset + 24], xmm7
  400. %endif
  401. %if xmm_regs_used > 8
  402. %assign %%i 8
  403. %rep xmm_regs_used-8
  404. movaps [rsp + (%%i-8)*16 + stack_size + 32], xmm %+ %%i
  405. %assign %%i %%i+1
  406. %endrep
  407. %endif
  408. %endmacro
  409. %macro WIN64_SPILL_XMM 1
  410. %assign xmm_regs_used %1
  411. ASSERT xmm_regs_used <= 16
  412. %if xmm_regs_used > 8
  413. ; Allocate stack space for callee-saved xmm registers plus shadow space and align the stack.
  414. %assign %%pad (xmm_regs_used-8)*16 + 32
  415. %assign stack_size_padded %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
  416. SUB rsp, stack_size_padded
  417. %endif
  418. WIN64_PUSH_XMM
  419. %endmacro
  420. %macro WIN64_RESTORE_XMM_INTERNAL 1
  421. %assign %%pad_size 0
  422. %if xmm_regs_used > 8
  423. %assign %%i xmm_regs_used
  424. %rep xmm_regs_used-8
  425. %assign %%i %%i-1
  426. movaps xmm %+ %%i, [%1 + (%%i-8)*16 + stack_size + 32]
  427. %endrep
  428. %endif
  429. %if stack_size_padded > 0
  430. %if stack_size > 0 && required_stack_alignment > STACK_ALIGNMENT
  431. mov rsp, rstkm
  432. %else
  433. add %1, stack_size_padded
  434. %assign %%pad_size stack_size_padded
  435. %endif
  436. %endif
  437. %if xmm_regs_used > 7
  438. movaps xmm7, [%1 + stack_offset - %%pad_size + 24]
  439. %endif
  440. %if xmm_regs_used > 6
  441. movaps xmm6, [%1 + stack_offset - %%pad_size + 8]
  442. %endif
  443. %endmacro
  444. %macro WIN64_RESTORE_XMM 1
  445. WIN64_RESTORE_XMM_INTERNAL %1
  446. %assign stack_offset (stack_offset-stack_size_padded)
  447. %assign xmm_regs_used 0
  448. %endmacro
  449. %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32 || stack_size > 0
  450. %macro RET 0
  451. WIN64_RESTORE_XMM_INTERNAL rsp
  452. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  453. %if mmsize == 32
  454. vzeroupper
  455. %endif
  456. AUTO_REP_RET
  457. %endmacro
  458. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  459. DECLARE_REG 0, rdi
  460. DECLARE_REG 1, rsi
  461. DECLARE_REG 2, rdx
  462. DECLARE_REG 3, rcx
  463. DECLARE_REG 4, R8
  464. DECLARE_REG 5, R9
  465. DECLARE_REG 6, rax, 8
  466. DECLARE_REG 7, R10, 16
  467. DECLARE_REG 8, R11, 24
  468. DECLARE_REG 9, rbx, 32
  469. DECLARE_REG 10, rbp, 40
  470. DECLARE_REG 11, R12, 48
  471. DECLARE_REG 12, R13, 56
  472. DECLARE_REG 13, R14, 64
  473. DECLARE_REG 14, R15, 72
  474. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  475. %assign num_args %1
  476. %assign regs_used %2
  477. ASSERT regs_used >= num_args
  478. SETUP_STACK_POINTER %4
  479. ASSERT regs_used <= 15
  480. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  481. ALLOC_STACK %4
  482. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  483. DEFINE_ARGS_INTERNAL %0, %4, %5
  484. %endmacro
  485. %define has_epilogue regs_used > 9 || mmsize == 32 || stack_size > 0
  486. %macro RET 0
  487. %if stack_size_padded > 0
  488. %if required_stack_alignment > STACK_ALIGNMENT
  489. mov rsp, rstkm
  490. %else
  491. add rsp, stack_size_padded
  492. %endif
  493. %endif
  494. POP_IF_USED 14, 13, 12, 11, 10, 9
  495. %if mmsize == 32
  496. vzeroupper
  497. %endif
  498. AUTO_REP_RET
  499. %endmacro
  500. %else ; X86_32 ;==============================================================
  501. DECLARE_REG 0, eax, 4
  502. DECLARE_REG 1, ecx, 8
  503. DECLARE_REG 2, edx, 12
  504. DECLARE_REG 3, ebx, 16
  505. DECLARE_REG 4, esi, 20
  506. DECLARE_REG 5, edi, 24
  507. DECLARE_REG 6, ebp, 28
  508. %define rsp esp
  509. %macro DECLARE_ARG 1-*
  510. %rep %0
  511. %define r%1m [rstk + stack_offset + 4*%1 + 4]
  512. %define r%1mp dword r%1m
  513. %rotate 1
  514. %endrep
  515. %endmacro
  516. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  517. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  518. %assign num_args %1
  519. %assign regs_used %2
  520. ASSERT regs_used >= num_args
  521. %if num_args > 7
  522. %assign num_args 7
  523. %endif
  524. %if regs_used > 7
  525. %assign regs_used 7
  526. %endif
  527. SETUP_STACK_POINTER %4
  528. ASSERT regs_used <= 7
  529. PUSH_IF_USED 3, 4, 5, 6
  530. ALLOC_STACK %4
  531. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  532. DEFINE_ARGS_INTERNAL %0, %4, %5
  533. %endmacro
  534. %define has_epilogue regs_used > 3 || mmsize == 32 || stack_size > 0
  535. %macro RET 0
  536. %if stack_size_padded > 0
  537. %if required_stack_alignment > STACK_ALIGNMENT
  538. mov rsp, rstkm
  539. %else
  540. add rsp, stack_size_padded
  541. %endif
  542. %endif
  543. POP_IF_USED 6, 5, 4, 3
  544. %if mmsize == 32
  545. vzeroupper
  546. %endif
  547. AUTO_REP_RET
  548. %endmacro
  549. %endif ;======================================================================
  550. %if WIN64 == 0
  551. %macro WIN64_SPILL_XMM 1
  552. %endmacro
  553. %macro WIN64_RESTORE_XMM 1
  554. %endmacro
  555. %macro WIN64_PUSH_XMM 0
  556. %endmacro
  557. %endif
  558. ; On AMD cpus <=K10, an ordinary ret is slow if it immediately follows either
  559. ; a branch or a branch target. So switch to a 2-byte form of ret in that case.
  560. ; We can automatically detect "follows a branch", but not a branch target.
  561. ; (SSSE3 is a sufficient condition to know that your cpu doesn't have this problem.)
  562. %macro REP_RET 0
  563. %if has_epilogue
  564. RET
  565. %else
  566. rep ret
  567. %endif
  568. %endmacro
  569. %define last_branch_adr $$
  570. %macro AUTO_REP_RET 0
  571. %ifndef cpuflags
  572. times ((last_branch_adr-$)>>31)+1 rep ; times 1 iff $ != last_branch_adr.
  573. %elif notcpuflag(ssse3)
  574. times ((last_branch_adr-$)>>31)+1 rep
  575. %endif
  576. ret
  577. %endmacro
  578. %macro BRANCH_INSTR 0-*
  579. %rep %0
  580. %macro %1 1-2 %1
  581. %2 %1
  582. %%branch_instr:
  583. %xdefine last_branch_adr %%branch_instr
  584. %endmacro
  585. %rotate 1
  586. %endrep
  587. %endmacro
  588. 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
  589. %macro TAIL_CALL 2 ; callee, is_nonadjacent
  590. %if has_epilogue
  591. call %1
  592. RET
  593. %elif %2
  594. jmp %1
  595. %endif
  596. %endmacro
  597. ;=============================================================================
  598. ; arch-independent part
  599. ;=============================================================================
  600. %assign function_align 16
  601. ; Begin a function.
  602. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  603. ; subsequent uses of the function name automatically refer to the mangled version.
  604. ; Appends cpuflags to the function name if cpuflags has been specified.
  605. ; The "" empty default parameter is a workaround for nasm, which fails if SUFFIX
  606. ; is empty and we call cglobal_internal with just %1 %+ SUFFIX (without %2).
  607. %macro cglobal 1-2+ "" ; name, [PROLOGUE args]
  608. cglobal_internal 1, %1 %+ SUFFIX, %2
  609. %endmacro
  610. %macro cvisible 1-2+ "" ; name, [PROLOGUE args]
  611. cglobal_internal 0, %1 %+ SUFFIX, %2
  612. %endmacro
  613. %macro cglobal_internal 2-3+
  614. %if %1
  615. %xdefine %%FUNCTION_PREFIX private_prefix
  616. %xdefine %%VISIBILITY hidden
  617. %else
  618. %xdefine %%FUNCTION_PREFIX public_prefix
  619. %xdefine %%VISIBILITY
  620. %endif
  621. %ifndef cglobaled_%2
  622. %xdefine %2 mangle(%%FUNCTION_PREFIX %+ _ %+ %2)
  623. %xdefine %2.skip_prologue %2 %+ .skip_prologue
  624. CAT_XDEFINE cglobaled_, %2, 1
  625. %endif
  626. %xdefine current_function %2
  627. %if FORMAT_ELF
  628. global %2:function %%VISIBILITY
  629. %else
  630. global %2
  631. %endif
  632. align function_align
  633. %2:
  634. RESET_MM_PERMUTATION ; needed for x86-64, also makes disassembly somewhat nicer
  635. %xdefine rstk rsp ; copy of the original stack pointer, used when greater alignment than the known stack alignment is required
  636. %assign stack_offset 0 ; stack pointer offset relative to the return address
  637. %assign stack_size 0 ; amount of stack space that can be freely used inside a function
  638. %assign stack_size_padded 0 ; total amount of allocated stack space, including space for callee-saved xmm registers on WIN64 and alignment padding
  639. %assign xmm_regs_used 0 ; number of XMM registers requested, used for dealing with callee-saved registers on WIN64
  640. %ifnidn %3, ""
  641. PROLOGUE %3
  642. %endif
  643. %endmacro
  644. %macro cextern 1
  645. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  646. CAT_XDEFINE cglobaled_, %1, 1
  647. extern %1
  648. %endmacro
  649. ; like cextern, but without the prefix
  650. %macro cextern_naked 1
  651. %ifdef PREFIX
  652. %xdefine %1 mangle(%1)
  653. %endif
  654. CAT_XDEFINE cglobaled_, %1, 1
  655. extern %1
  656. %endmacro
  657. %macro const 1-2+
  658. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  659. %if FORMAT_ELF
  660. global %1:data hidden
  661. %else
  662. global %1
  663. %endif
  664. %1: %2
  665. %endmacro
  666. ; This is needed for ELF, otherwise the GNU linker assumes the stack is executable by default.
  667. %if FORMAT_ELF
  668. [SECTION .note.GNU-stack noalloc noexec nowrite progbits]
  669. %endif
  670. ; cpuflags
  671. %assign cpuflags_mmx (1<<0)
  672. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  673. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  674. %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
  675. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  676. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  677. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  678. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  679. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  680. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  681. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  682. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  683. %assign cpuflags_xop (1<<12)| cpuflags_avx
  684. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  685. %assign cpuflags_fma3 (1<<14)| cpuflags_avx
  686. %assign cpuflags_avx2 (1<<15)| cpuflags_fma3
  687. %assign cpuflags_cache32 (1<<16)
  688. %assign cpuflags_cache64 (1<<17)
  689. %assign cpuflags_slowctz (1<<18)
  690. %assign cpuflags_lzcnt (1<<19)
  691. %assign cpuflags_aligned (1<<20) ; not a cpu feature, but a function variant
  692. %assign cpuflags_atom (1<<21)
  693. %assign cpuflags_bmi1 (1<<22)|cpuflags_lzcnt
  694. %assign cpuflags_bmi2 (1<<23)|cpuflags_bmi1
  695. %assign cpuflags_aesni (1<<24)|cpuflags_sse42
  696. ; Returns a boolean value expressing whether or not the specified cpuflag is enabled.
  697. %define cpuflag(x) (((((cpuflags & (cpuflags_ %+ x)) ^ (cpuflags_ %+ x)) - 1) >> 31) & 1)
  698. %define notcpuflag(x) (cpuflag(x) ^ 1)
  699. ; Takes an arbitrary number of cpuflags from the above list.
  700. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  701. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  702. %macro INIT_CPUFLAGS 0-*
  703. %xdefine SUFFIX
  704. %undef cpuname
  705. %assign cpuflags 0
  706. %if %0 >= 1
  707. %rep %0
  708. %ifdef cpuname
  709. %xdefine cpuname cpuname %+ _%1
  710. %else
  711. %xdefine cpuname %1
  712. %endif
  713. %assign cpuflags cpuflags | cpuflags_%1
  714. %rotate 1
  715. %endrep
  716. %xdefine SUFFIX _ %+ cpuname
  717. %if cpuflag(avx)
  718. %assign avx_enabled 1
  719. %endif
  720. %if (mmsize == 16 && notcpuflag(sse2)) || (mmsize == 32 && notcpuflag(avx2))
  721. %define mova movaps
  722. %define movu movups
  723. %define movnta movntps
  724. %endif
  725. %if cpuflag(aligned)
  726. %define movu mova
  727. %elif cpuflag(sse3) && notcpuflag(ssse3)
  728. %define movu lddqu
  729. %endif
  730. %endif
  731. %if ARCH_X86_64 || cpuflag(sse2)
  732. CPUNOP amdnop
  733. %else
  734. CPUNOP basicnop
  735. %endif
  736. %endmacro
  737. ; Merge mmx and sse*
  738. ; m# is a simd register of the currently selected size
  739. ; xm# is the corresponding xmm register if mmsize >= 16, otherwise the same as m#
  740. ; ym# is the corresponding ymm register if mmsize >= 32, otherwise the same as m#
  741. ; (All 3 remain in sync through SWAP.)
  742. %macro CAT_XDEFINE 3
  743. %xdefine %1%2 %3
  744. %endmacro
  745. %macro CAT_UNDEF 2
  746. %undef %1%2
  747. %endmacro
  748. %macro INIT_MMX 0-1+
  749. %assign avx_enabled 0
  750. %define RESET_MM_PERMUTATION INIT_MMX %1
  751. %define mmsize 8
  752. %define num_mmregs 8
  753. %define mova movq
  754. %define movu movq
  755. %define movh movd
  756. %define movnta movntq
  757. %assign %%i 0
  758. %rep 8
  759. CAT_XDEFINE m, %%i, mm %+ %%i
  760. CAT_XDEFINE nnmm, %%i, %%i
  761. %assign %%i %%i+1
  762. %endrep
  763. %rep 8
  764. CAT_UNDEF m, %%i
  765. CAT_UNDEF nnmm, %%i
  766. %assign %%i %%i+1
  767. %endrep
  768. INIT_CPUFLAGS %1
  769. %endmacro
  770. %macro INIT_XMM 0-1+
  771. %assign avx_enabled 0
  772. %define RESET_MM_PERMUTATION INIT_XMM %1
  773. %define mmsize 16
  774. %define num_mmregs 8
  775. %if ARCH_X86_64
  776. %define num_mmregs 16
  777. %endif
  778. %define mova movdqa
  779. %define movu movdqu
  780. %define movh movq
  781. %define movnta movntdq
  782. %assign %%i 0
  783. %rep num_mmregs
  784. CAT_XDEFINE m, %%i, xmm %+ %%i
  785. CAT_XDEFINE nnxmm, %%i, %%i
  786. %assign %%i %%i+1
  787. %endrep
  788. INIT_CPUFLAGS %1
  789. %endmacro
  790. %macro INIT_YMM 0-1+
  791. %assign avx_enabled 1
  792. %define RESET_MM_PERMUTATION INIT_YMM %1
  793. %define mmsize 32
  794. %define num_mmregs 8
  795. %if ARCH_X86_64
  796. %define num_mmregs 16
  797. %endif
  798. %define mova movdqa
  799. %define movu movdqu
  800. %undef movh
  801. %define movnta movntdq
  802. %assign %%i 0
  803. %rep num_mmregs
  804. CAT_XDEFINE m, %%i, ymm %+ %%i
  805. CAT_XDEFINE nnymm, %%i, %%i
  806. %assign %%i %%i+1
  807. %endrep
  808. INIT_CPUFLAGS %1
  809. %endmacro
  810. INIT_XMM
  811. %macro DECLARE_MMCAST 1
  812. %define mmmm%1 mm%1
  813. %define mmxmm%1 mm%1
  814. %define mmymm%1 mm%1
  815. %define xmmmm%1 mm%1
  816. %define xmmxmm%1 xmm%1
  817. %define xmmymm%1 xmm%1
  818. %define ymmmm%1 mm%1
  819. %define ymmxmm%1 xmm%1
  820. %define ymmymm%1 ymm%1
  821. %define xm%1 xmm %+ m%1
  822. %define ym%1 ymm %+ m%1
  823. %endmacro
  824. %assign i 0
  825. %rep 16
  826. DECLARE_MMCAST i
  827. %assign i i+1
  828. %endrep
  829. ; I often want to use macros that permute their arguments. e.g. there's no
  830. ; efficient way to implement butterfly or transpose or dct without swapping some
  831. ; arguments.
  832. ;
  833. ; I would like to not have to manually keep track of the permutations:
  834. ; If I insert a permutation in the middle of a function, it should automatically
  835. ; change everything that follows. For more complex macros I may also have multiple
  836. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  837. ;
  838. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  839. ; permutes its arguments. It's equivalent to exchanging the contents of the
  840. ; registers, except that this way you exchange the register names instead, so it
  841. ; doesn't cost any cycles.
  842. %macro PERMUTE 2-* ; takes a list of pairs to swap
  843. %rep %0/2
  844. %xdefine %%tmp%2 m%2
  845. %rotate 2
  846. %endrep
  847. %rep %0/2
  848. %xdefine m%1 %%tmp%2
  849. CAT_XDEFINE nn, m%1, %1
  850. %rotate 2
  851. %endrep
  852. %endmacro
  853. %macro SWAP 2+ ; swaps a single chain (sometimes more concise than pairs)
  854. %ifnum %1 ; SWAP 0, 1, ...
  855. SWAP_INTERNAL_NUM %1, %2
  856. %else ; SWAP m0, m1, ...
  857. SWAP_INTERNAL_NAME %1, %2
  858. %endif
  859. %endmacro
  860. %macro SWAP_INTERNAL_NUM 2-*
  861. %rep %0-1
  862. %xdefine %%tmp m%1
  863. %xdefine m%1 m%2
  864. %xdefine m%2 %%tmp
  865. CAT_XDEFINE nn, m%1, %1
  866. CAT_XDEFINE nn, m%2, %2
  867. %rotate 1
  868. %endrep
  869. %endmacro
  870. %macro SWAP_INTERNAL_NAME 2-*
  871. %xdefine %%args nn %+ %1
  872. %rep %0-1
  873. %xdefine %%args %%args, nn %+ %2
  874. %rotate 1
  875. %endrep
  876. SWAP_INTERNAL_NUM %%args
  877. %endmacro
  878. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  879. ; calls to that function will automatically load the permutation, so values can
  880. ; be returned in mmregs.
  881. %macro SAVE_MM_PERMUTATION 0-1
  882. %if %0
  883. %xdefine %%f %1_m
  884. %else
  885. %xdefine %%f current_function %+ _m
  886. %endif
  887. %assign %%i 0
  888. %rep num_mmregs
  889. CAT_XDEFINE %%f, %%i, m %+ %%i
  890. %assign %%i %%i+1
  891. %endrep
  892. %endmacro
  893. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  894. %ifdef %1_m0
  895. %assign %%i 0
  896. %rep num_mmregs
  897. CAT_XDEFINE m, %%i, %1_m %+ %%i
  898. CAT_XDEFINE nn, m %+ %%i, %%i
  899. %assign %%i %%i+1
  900. %endrep
  901. %endif
  902. %endmacro
  903. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  904. %macro call 1
  905. call_internal %1 %+ SUFFIX, %1
  906. %endmacro
  907. %macro call_internal 2
  908. %xdefine %%i %2
  909. %ifndef cglobaled_%2
  910. %ifdef cglobaled_%1
  911. %xdefine %%i %1
  912. %endif
  913. %endif
  914. call %%i
  915. LOAD_MM_PERMUTATION %%i
  916. %endmacro
  917. ; Substitutions that reduce instruction size but are functionally equivalent
  918. %macro add 2
  919. %ifnum %2
  920. %if %2==128
  921. sub %1, -128
  922. %else
  923. add %1, %2
  924. %endif
  925. %else
  926. add %1, %2
  927. %endif
  928. %endmacro
  929. %macro sub 2
  930. %ifnum %2
  931. %if %2==128
  932. add %1, -128
  933. %else
  934. sub %1, %2
  935. %endif
  936. %else
  937. sub %1, %2
  938. %endif
  939. %endmacro
  940. ;=============================================================================
  941. ; AVX abstraction layer
  942. ;=============================================================================
  943. %assign i 0
  944. %rep 16
  945. %if i < 8
  946. CAT_XDEFINE sizeofmm, i, 8
  947. %endif
  948. CAT_XDEFINE sizeofxmm, i, 16
  949. CAT_XDEFINE sizeofymm, i, 32
  950. %assign i i+1
  951. %endrep
  952. %undef i
  953. %macro CHECK_AVX_INSTR_EMU 3-*
  954. %xdefine %%opcode %1
  955. %xdefine %%dst %2
  956. %rep %0-2
  957. %ifidn %%dst, %3
  958. %error non-avx emulation of ``%%opcode'' is not supported
  959. %endif
  960. %rotate 1
  961. %endrep
  962. %endmacro
  963. ;%1 == instruction
  964. ;%2 == minimal instruction set
  965. ;%3 == 1 if float, 0 if int
  966. ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
  967. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  968. ;%6+: operands
  969. %macro RUN_AVX_INSTR 6-9+
  970. %ifnum sizeof%7
  971. %assign __sizeofreg sizeof%7
  972. %elifnum sizeof%6
  973. %assign __sizeofreg sizeof%6
  974. %else
  975. %assign __sizeofreg mmsize
  976. %endif
  977. %assign __emulate_avx 0
  978. %if avx_enabled && __sizeofreg >= 16
  979. %xdefine __instr v%1
  980. %else
  981. %xdefine __instr %1
  982. %if %0 >= 8+%4
  983. %assign __emulate_avx 1
  984. %endif
  985. %endif
  986. %ifnidn %2, fnord
  987. %ifdef cpuname
  988. %if notcpuflag(%2)
  989. %error use of ``%1'' %2 instruction in cpuname function: current_function
  990. %elif cpuflags_%2 < cpuflags_sse && notcpuflag(sse2) && __sizeofreg > 8
  991. %error use of ``%1'' sse2 instruction in cpuname function: current_function
  992. %endif
  993. %endif
  994. %endif
  995. %if __emulate_avx
  996. %xdefine __src1 %7
  997. %xdefine __src2 %8
  998. %ifnidn %6, %7
  999. %if %0 >= 9
  1000. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8, %9}, %6, %8, %9
  1001. %else
  1002. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8}, %6, %8
  1003. %endif
  1004. %if %5 && %4 == 0
  1005. %ifnid %8
  1006. ; 3-operand AVX instructions with a memory arg can only have it in src2,
  1007. ; whereas SSE emulation prefers to have it in src1 (i.e. the mov).
  1008. ; So, if the instruction is commutative with a memory arg, swap them.
  1009. %xdefine __src1 %8
  1010. %xdefine __src2 %7
  1011. %endif
  1012. %endif
  1013. %if __sizeofreg == 8
  1014. MOVQ %6, __src1
  1015. %elif %3
  1016. MOVAPS %6, __src1
  1017. %else
  1018. MOVDQA %6, __src1
  1019. %endif
  1020. %endif
  1021. %if %0 >= 9
  1022. %1 %6, __src2, %9
  1023. %else
  1024. %1 %6, __src2
  1025. %endif
  1026. %elif %0 >= 9
  1027. __instr %6, %7, %8, %9
  1028. %elif %0 == 8
  1029. __instr %6, %7, %8
  1030. %elif %0 == 7
  1031. __instr %6, %7
  1032. %else
  1033. __instr %6
  1034. %endif
  1035. %endmacro
  1036. ;%1 == instruction
  1037. ;%2 == minimal instruction set
  1038. ;%3 == 1 if float, 0 if int
  1039. ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
  1040. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  1041. %macro AVX_INSTR 1-5 fnord, 0, 1, 0
  1042. %macro %1 1-10 fnord, fnord, fnord, fnord, %1, %2, %3, %4, %5
  1043. %ifidn %2, fnord
  1044. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1
  1045. %elifidn %3, fnord
  1046. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2
  1047. %elifidn %4, fnord
  1048. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3
  1049. %elifidn %5, fnord
  1050. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4
  1051. %else
  1052. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4, %5
  1053. %endif
  1054. %endmacro
  1055. %endmacro
  1056. ; Instructions with both VEX and non-VEX encodings
  1057. ; Non-destructive instructions are written without parameters
  1058. AVX_INSTR addpd, sse2, 1, 0, 1
  1059. AVX_INSTR addps, sse, 1, 0, 1
  1060. AVX_INSTR addsd, sse2, 1, 0, 1
  1061. AVX_INSTR addss, sse, 1, 0, 1
  1062. AVX_INSTR addsubpd, sse3, 1, 0, 0
  1063. AVX_INSTR addsubps, sse3, 1, 0, 0
  1064. AVX_INSTR aesdec, aesni, 0, 0, 0
  1065. AVX_INSTR aesdeclast, aesni, 0, 0, 0
  1066. AVX_INSTR aesenc, aesni, 0, 0, 0
  1067. AVX_INSTR aesenclast, aesni, 0, 0, 0
  1068. AVX_INSTR aesimc, aesni
  1069. AVX_INSTR aeskeygenassist, aesni
  1070. AVX_INSTR andnpd, sse2, 1, 0, 0
  1071. AVX_INSTR andnps, sse, 1, 0, 0
  1072. AVX_INSTR andpd, sse2, 1, 0, 1
  1073. AVX_INSTR andps, sse, 1, 0, 1
  1074. AVX_INSTR blendpd, sse4, 1, 0, 0
  1075. AVX_INSTR blendps, sse4, 1, 0, 0
  1076. AVX_INSTR blendvpd, sse4, 1, 0, 0
  1077. AVX_INSTR blendvps, sse4, 1, 0, 0
  1078. AVX_INSTR cmppd, sse2, 1, 1, 0
  1079. AVX_INSTR cmpps, sse, 1, 1, 0
  1080. AVX_INSTR cmpsd, sse2, 1, 1, 0
  1081. AVX_INSTR cmpss, sse, 1, 1, 0
  1082. AVX_INSTR comisd, sse2
  1083. AVX_INSTR comiss, sse
  1084. AVX_INSTR cvtdq2pd, sse2
  1085. AVX_INSTR cvtdq2ps, sse2
  1086. AVX_INSTR cvtpd2dq, sse2
  1087. AVX_INSTR cvtpd2ps, sse2
  1088. AVX_INSTR cvtps2dq, sse2
  1089. AVX_INSTR cvtps2pd, sse2
  1090. AVX_INSTR cvtsd2si, sse2
  1091. AVX_INSTR cvtsd2ss, sse2
  1092. AVX_INSTR cvtsi2sd, sse2
  1093. AVX_INSTR cvtsi2ss, sse
  1094. AVX_INSTR cvtss2sd, sse2
  1095. AVX_INSTR cvtss2si, sse
  1096. AVX_INSTR cvttpd2dq, sse2
  1097. AVX_INSTR cvttps2dq, sse2
  1098. AVX_INSTR cvttsd2si, sse2
  1099. AVX_INSTR cvttss2si, sse
  1100. AVX_INSTR divpd, sse2, 1, 0, 0
  1101. AVX_INSTR divps, sse, 1, 0, 0
  1102. AVX_INSTR divsd, sse2, 1, 0, 0
  1103. AVX_INSTR divss, sse, 1, 0, 0
  1104. AVX_INSTR dppd, sse4, 1, 1, 0
  1105. AVX_INSTR dpps, sse4, 1, 1, 0
  1106. AVX_INSTR extractps, sse4
  1107. AVX_INSTR haddpd, sse3, 1, 0, 0
  1108. AVX_INSTR haddps, sse3, 1, 0, 0
  1109. AVX_INSTR hsubpd, sse3, 1, 0, 0
  1110. AVX_INSTR hsubps, sse3, 1, 0, 0
  1111. AVX_INSTR insertps, sse4, 1, 1, 0
  1112. AVX_INSTR lddqu, sse3
  1113. AVX_INSTR ldmxcsr, sse
  1114. AVX_INSTR maskmovdqu, sse2
  1115. AVX_INSTR maxpd, sse2, 1, 0, 1
  1116. AVX_INSTR maxps, sse, 1, 0, 1
  1117. AVX_INSTR maxsd, sse2, 1, 0, 1
  1118. AVX_INSTR maxss, sse, 1, 0, 1
  1119. AVX_INSTR minpd, sse2, 1, 0, 1
  1120. AVX_INSTR minps, sse, 1, 0, 1
  1121. AVX_INSTR minsd, sse2, 1, 0, 1
  1122. AVX_INSTR minss, sse, 1, 0, 1
  1123. AVX_INSTR movapd, sse2
  1124. AVX_INSTR movaps, sse
  1125. AVX_INSTR movd, mmx
  1126. AVX_INSTR movddup, sse3
  1127. AVX_INSTR movdqa, sse2
  1128. AVX_INSTR movdqu, sse2
  1129. AVX_INSTR movhlps, sse, 1, 0, 0
  1130. AVX_INSTR movhpd, sse2, 1, 0, 0
  1131. AVX_INSTR movhps, sse, 1, 0, 0
  1132. AVX_INSTR movlhps, sse, 1, 0, 0
  1133. AVX_INSTR movlpd, sse2, 1, 0, 0
  1134. AVX_INSTR movlps, sse, 1, 0, 0
  1135. AVX_INSTR movmskpd, sse2
  1136. AVX_INSTR movmskps, sse
  1137. AVX_INSTR movntdq, sse2
  1138. AVX_INSTR movntdqa, sse4
  1139. AVX_INSTR movntpd, sse2
  1140. AVX_INSTR movntps, sse
  1141. AVX_INSTR movq, mmx
  1142. AVX_INSTR movsd, sse2, 1, 0, 0
  1143. AVX_INSTR movshdup, sse3
  1144. AVX_INSTR movsldup, sse3
  1145. AVX_INSTR movss, sse, 1, 0, 0
  1146. AVX_INSTR movupd, sse2
  1147. AVX_INSTR movups, sse
  1148. AVX_INSTR mpsadbw, sse4
  1149. AVX_INSTR mulpd, sse2, 1, 0, 1
  1150. AVX_INSTR mulps, sse, 1, 0, 1
  1151. AVX_INSTR mulsd, sse2, 1, 0, 1
  1152. AVX_INSTR mulss, sse, 1, 0, 1
  1153. AVX_INSTR orpd, sse2, 1, 0, 1
  1154. AVX_INSTR orps, sse, 1, 0, 1
  1155. AVX_INSTR pabsb, ssse3
  1156. AVX_INSTR pabsd, ssse3
  1157. AVX_INSTR pabsw, ssse3
  1158. AVX_INSTR packsswb, mmx, 0, 0, 0
  1159. AVX_INSTR packssdw, mmx, 0, 0, 0
  1160. AVX_INSTR packuswb, mmx, 0, 0, 0
  1161. AVX_INSTR packusdw, sse4, 0, 0, 0
  1162. AVX_INSTR paddb, mmx, 0, 0, 1
  1163. AVX_INSTR paddw, mmx, 0, 0, 1
  1164. AVX_INSTR paddd, mmx, 0, 0, 1
  1165. AVX_INSTR paddq, sse2, 0, 0, 1
  1166. AVX_INSTR paddsb, mmx, 0, 0, 1
  1167. AVX_INSTR paddsw, mmx, 0, 0, 1
  1168. AVX_INSTR paddusb, mmx, 0, 0, 1
  1169. AVX_INSTR paddusw, mmx, 0, 0, 1
  1170. AVX_INSTR palignr, ssse3
  1171. AVX_INSTR pand, mmx, 0, 0, 1
  1172. AVX_INSTR pandn, mmx, 0, 0, 0
  1173. AVX_INSTR pavgb, mmx2, 0, 0, 1
  1174. AVX_INSTR pavgw, mmx2, 0, 0, 1
  1175. AVX_INSTR pblendvb, sse4, 0, 0, 0
  1176. AVX_INSTR pblendw, sse4
  1177. AVX_INSTR pclmulqdq
  1178. AVX_INSTR pcmpestri, sse42
  1179. AVX_INSTR pcmpestrm, sse42
  1180. AVX_INSTR pcmpistri, sse42
  1181. AVX_INSTR pcmpistrm, sse42
  1182. AVX_INSTR pcmpeqb, mmx, 0, 0, 1
  1183. AVX_INSTR pcmpeqw, mmx, 0, 0, 1
  1184. AVX_INSTR pcmpeqd, mmx, 0, 0, 1
  1185. AVX_INSTR pcmpeqq, sse4, 0, 0, 1
  1186. AVX_INSTR pcmpgtb, mmx, 0, 0, 0
  1187. AVX_INSTR pcmpgtw, mmx, 0, 0, 0
  1188. AVX_INSTR pcmpgtd, mmx, 0, 0, 0
  1189. AVX_INSTR pcmpgtq, sse42, 0, 0, 0
  1190. AVX_INSTR pextrb, sse4
  1191. AVX_INSTR pextrd, sse4
  1192. AVX_INSTR pextrq, sse4
  1193. AVX_INSTR pextrw, mmx2
  1194. AVX_INSTR phaddw, ssse3, 0, 0, 0
  1195. AVX_INSTR phaddd, ssse3, 0, 0, 0
  1196. AVX_INSTR phaddsw, ssse3, 0, 0, 0
  1197. AVX_INSTR phminposuw, sse4
  1198. AVX_INSTR phsubw, ssse3, 0, 0, 0
  1199. AVX_INSTR phsubd, ssse3, 0, 0, 0
  1200. AVX_INSTR phsubsw, ssse3, 0, 0, 0
  1201. AVX_INSTR pinsrb, sse4
  1202. AVX_INSTR pinsrd, sse4
  1203. AVX_INSTR pinsrq, sse4
  1204. AVX_INSTR pinsrw, mmx2
  1205. AVX_INSTR pmaddwd, mmx, 0, 0, 1
  1206. AVX_INSTR pmaddubsw, ssse3, 0, 0, 0
  1207. AVX_INSTR pmaxsb, sse4, 0, 0, 1
  1208. AVX_INSTR pmaxsw, mmx2, 0, 0, 1
  1209. AVX_INSTR pmaxsd, sse4, 0, 0, 1
  1210. AVX_INSTR pmaxub, mmx2, 0, 0, 1
  1211. AVX_INSTR pmaxuw, sse4, 0, 0, 1
  1212. AVX_INSTR pmaxud, sse4, 0, 0, 1
  1213. AVX_INSTR pminsb, sse4, 0, 0, 1
  1214. AVX_INSTR pminsw, mmx2, 0, 0, 1
  1215. AVX_INSTR pminsd, sse4, 0, 0, 1
  1216. AVX_INSTR pminub, mmx2, 0, 0, 1
  1217. AVX_INSTR pminuw, sse4, 0, 0, 1
  1218. AVX_INSTR pminud, sse4, 0, 0, 1
  1219. AVX_INSTR pmovmskb, mmx2
  1220. AVX_INSTR pmovsxbw, sse4
  1221. AVX_INSTR pmovsxbd, sse4
  1222. AVX_INSTR pmovsxbq, sse4
  1223. AVX_INSTR pmovsxwd, sse4
  1224. AVX_INSTR pmovsxwq, sse4
  1225. AVX_INSTR pmovsxdq, sse4
  1226. AVX_INSTR pmovzxbw, sse4
  1227. AVX_INSTR pmovzxbd, sse4
  1228. AVX_INSTR pmovzxbq, sse4
  1229. AVX_INSTR pmovzxwd, sse4
  1230. AVX_INSTR pmovzxwq, sse4
  1231. AVX_INSTR pmovzxdq, sse4
  1232. AVX_INSTR pmuldq, sse4, 0, 0, 1
  1233. AVX_INSTR pmulhrsw, ssse3, 0, 0, 1
  1234. AVX_INSTR pmulhuw, mmx2, 0, 0, 1
  1235. AVX_INSTR pmulhw, mmx, 0, 0, 1
  1236. AVX_INSTR pmullw, mmx, 0, 0, 1
  1237. AVX_INSTR pmulld, sse4, 0, 0, 1
  1238. AVX_INSTR pmuludq, sse2, 0, 0, 1
  1239. AVX_INSTR por, mmx, 0, 0, 1
  1240. AVX_INSTR psadbw, mmx2, 0, 0, 1
  1241. AVX_INSTR pshufb, ssse3, 0, 0, 0
  1242. AVX_INSTR pshufd, sse2
  1243. AVX_INSTR pshufhw, sse2
  1244. AVX_INSTR pshuflw, sse2
  1245. AVX_INSTR psignb, ssse3, 0, 0, 0
  1246. AVX_INSTR psignw, ssse3, 0, 0, 0
  1247. AVX_INSTR psignd, ssse3, 0, 0, 0
  1248. AVX_INSTR psllw, mmx, 0, 0, 0
  1249. AVX_INSTR pslld, mmx, 0, 0, 0
  1250. AVX_INSTR psllq, mmx, 0, 0, 0
  1251. AVX_INSTR pslldq, sse2, 0, 0, 0
  1252. AVX_INSTR psraw, mmx, 0, 0, 0
  1253. AVX_INSTR psrad, mmx, 0, 0, 0
  1254. AVX_INSTR psrlw, mmx, 0, 0, 0
  1255. AVX_INSTR psrld, mmx, 0, 0, 0
  1256. AVX_INSTR psrlq, mmx, 0, 0, 0
  1257. AVX_INSTR psrldq, sse2, 0, 0, 0
  1258. AVX_INSTR psubb, mmx, 0, 0, 0
  1259. AVX_INSTR psubw, mmx, 0, 0, 0
  1260. AVX_INSTR psubd, mmx, 0, 0, 0
  1261. AVX_INSTR psubq, sse2, 0, 0, 0
  1262. AVX_INSTR psubsb, mmx, 0, 0, 0
  1263. AVX_INSTR psubsw, mmx, 0, 0, 0
  1264. AVX_INSTR psubusb, mmx, 0, 0, 0
  1265. AVX_INSTR psubusw, mmx, 0, 0, 0
  1266. AVX_INSTR ptest, sse4
  1267. AVX_INSTR punpckhbw, mmx, 0, 0, 0
  1268. AVX_INSTR punpckhwd, mmx, 0, 0, 0
  1269. AVX_INSTR punpckhdq, mmx, 0, 0, 0
  1270. AVX_INSTR punpckhqdq, sse2, 0, 0, 0
  1271. AVX_INSTR punpcklbw, mmx, 0, 0, 0
  1272. AVX_INSTR punpcklwd, mmx, 0, 0, 0
  1273. AVX_INSTR punpckldq, mmx, 0, 0, 0
  1274. AVX_INSTR punpcklqdq, sse2, 0, 0, 0
  1275. AVX_INSTR pxor, mmx, 0, 0, 1
  1276. AVX_INSTR rcpps, sse, 1, 0, 0
  1277. AVX_INSTR rcpss, sse, 1, 0, 0
  1278. AVX_INSTR roundpd, sse4
  1279. AVX_INSTR roundps, sse4
  1280. AVX_INSTR roundsd, sse4
  1281. AVX_INSTR roundss, sse4
  1282. AVX_INSTR rsqrtps, sse, 1, 0, 0
  1283. AVX_INSTR rsqrtss, sse, 1, 0, 0
  1284. AVX_INSTR shufpd, sse2, 1, 1, 0
  1285. AVX_INSTR shufps, sse, 1, 1, 0
  1286. AVX_INSTR sqrtpd, sse2, 1, 0, 0
  1287. AVX_INSTR sqrtps, sse, 1, 0, 0
  1288. AVX_INSTR sqrtsd, sse2, 1, 0, 0
  1289. AVX_INSTR sqrtss, sse, 1, 0, 0
  1290. AVX_INSTR stmxcsr, sse
  1291. AVX_INSTR subpd, sse2, 1, 0, 0
  1292. AVX_INSTR subps, sse, 1, 0, 0
  1293. AVX_INSTR subsd, sse2, 1, 0, 0
  1294. AVX_INSTR subss, sse, 1, 0, 0
  1295. AVX_INSTR ucomisd, sse2
  1296. AVX_INSTR ucomiss, sse
  1297. AVX_INSTR unpckhpd, sse2, 1, 0, 0
  1298. AVX_INSTR unpckhps, sse, 1, 0, 0
  1299. AVX_INSTR unpcklpd, sse2, 1, 0, 0
  1300. AVX_INSTR unpcklps, sse, 1, 0, 0
  1301. AVX_INSTR xorpd, sse2, 1, 0, 1
  1302. AVX_INSTR xorps, sse, 1, 0, 1
  1303. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  1304. AVX_INSTR pfadd, 3dnow, 1, 0, 1
  1305. AVX_INSTR pfsub, 3dnow, 1, 0, 0
  1306. AVX_INSTR pfmul, 3dnow, 1, 0, 1
  1307. ; base-4 constants for shuffles
  1308. %assign i 0
  1309. %rep 256
  1310. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  1311. %if j < 10
  1312. CAT_XDEFINE q000, j, i
  1313. %elif j < 100
  1314. CAT_XDEFINE q00, j, i
  1315. %elif j < 1000
  1316. CAT_XDEFINE q0, j, i
  1317. %else
  1318. CAT_XDEFINE q, j, i
  1319. %endif
  1320. %assign i i+1
  1321. %endrep
  1322. %undef i
  1323. %undef j
  1324. %macro FMA_INSTR 3
  1325. %macro %1 4-7 %1, %2, %3
  1326. %if cpuflag(xop)
  1327. v%5 %1, %2, %3, %4
  1328. %elifnidn %1, %4
  1329. %6 %1, %2, %3
  1330. %7 %1, %4
  1331. %else
  1332. %error non-xop emulation of ``%5 %1, %2, %3, %4'' is not supported
  1333. %endif
  1334. %endmacro
  1335. %endmacro
  1336. FMA_INSTR pmacsww, pmullw, paddw
  1337. FMA_INSTR pmacsdd, pmulld, paddd ; sse4 emulation
  1338. FMA_INSTR pmacsdql, pmuldq, paddq ; sse4 emulation
  1339. FMA_INSTR pmadcswd, pmaddwd, paddd
  1340. ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
  1341. ; This lets us use tzcnt without bumping the yasm version requirement yet.
  1342. %define tzcnt rep bsf
  1343. ; convert FMA4 to FMA3 if possible
  1344. %macro FMA4_INSTR 4
  1345. %macro %1 4-8 %1, %2, %3, %4
  1346. %if cpuflag(fma4)
  1347. v%5 %1, %2, %3, %4
  1348. %elifidn %1, %2
  1349. v%6 %1, %4, %3 ; %1 = %1 * %3 + %4
  1350. %elifidn %1, %3
  1351. v%7 %1, %2, %4 ; %1 = %2 * %1 + %4
  1352. %elifidn %1, %4
  1353. v%8 %1, %2, %3 ; %1 = %2 * %3 + %1
  1354. %else
  1355. %error fma3 emulation of ``%5 %1, %2, %3, %4'' is not supported
  1356. %endif
  1357. %endmacro
  1358. %endmacro
  1359. FMA4_INSTR fmaddpd, fmadd132pd, fmadd213pd, fmadd231pd
  1360. FMA4_INSTR fmaddps, fmadd132ps, fmadd213ps, fmadd231ps
  1361. FMA4_INSTR fmaddsd, fmadd132sd, fmadd213sd, fmadd231sd
  1362. FMA4_INSTR fmaddss, fmadd132ss, fmadd213ss, fmadd231ss
  1363. FMA4_INSTR fmaddsubpd, fmaddsub132pd, fmaddsub213pd, fmaddsub231pd
  1364. FMA4_INSTR fmaddsubps, fmaddsub132ps, fmaddsub213ps, fmaddsub231ps
  1365. FMA4_INSTR fmsubaddpd, fmsubadd132pd, fmsubadd213pd, fmsubadd231pd
  1366. FMA4_INSTR fmsubaddps, fmsubadd132ps, fmsubadd213ps, fmsubadd231ps
  1367. FMA4_INSTR fmsubpd, fmsub132pd, fmsub213pd, fmsub231pd
  1368. FMA4_INSTR fmsubps, fmsub132ps, fmsub213ps, fmsub231ps
  1369. FMA4_INSTR fmsubsd, fmsub132sd, fmsub213sd, fmsub231sd
  1370. FMA4_INSTR fmsubss, fmsub132ss, fmsub213ss, fmsub231ss
  1371. FMA4_INSTR fnmaddpd, fnmadd132pd, fnmadd213pd, fnmadd231pd
  1372. FMA4_INSTR fnmaddps, fnmadd132ps, fnmadd213ps, fnmadd231ps
  1373. FMA4_INSTR fnmaddsd, fnmadd132sd, fnmadd213sd, fnmadd231sd
  1374. FMA4_INSTR fnmaddss, fnmadd132ss, fnmadd213ss, fnmadd231ss
  1375. FMA4_INSTR fnmsubpd, fnmsub132pd, fnmsub213pd, fnmsub231pd
  1376. FMA4_INSTR fnmsubps, fnmsub132ps, fnmsub213ps, fnmsub231ps
  1377. FMA4_INSTR fnmsubsd, fnmsub132sd, fnmsub213sd, fnmsub231sd
  1378. FMA4_INSTR fnmsubss, fnmsub132ss, fnmsub213ss, fnmsub231ss
  1379. ; workaround: vpbroadcastq is broken in x86_32 due to a yasm bug (fixed in 1.3.0)
  1380. %ifdef __YASM_VER__
  1381. %if __YASM_VERSION_ID__ < 0x01030000 && ARCH_X86_64 == 0
  1382. %macro vpbroadcastq 2
  1383. %if sizeof%1 == 16
  1384. movddup %1, %2
  1385. %else
  1386. vbroadcastsd %1, %2
  1387. %endif
  1388. %endmacro
  1389. %endif
  1390. %endif