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.

1502 lines
42KB

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