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.

1341 lines
36KB

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