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.

1270 lines
33KB

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