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.

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