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.

1472 lines
41KB

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