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.

1374 lines
37KB

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