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.

1310 lines
35KB

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