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.

906 lines
22KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2011 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. ;*
  10. ;* Permission to use, copy, modify, and/or distribute this software for any
  11. ;* purpose with or without fee is hereby granted, provided that the above
  12. ;* copyright notice and this permission notice appear in all copies.
  13. ;*
  14. ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  15. ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  16. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  17. ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  19. ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  20. ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. ;*****************************************************************************
  22. ; This is a header file for the x264ASM assembly language, which uses
  23. ; NASM/YASM syntax combined with a large number of macros to provide easy
  24. ; abstraction between different calling conventions (x86_32, win64, linux64).
  25. ; It also has various other useful features to simplify writing the kind of
  26. ; DSP functions that are most often used in x264.
  27. ; Unlike the rest of x264, this file is available under an ISC license, as it
  28. ; has significant usefulness outside of x264 and we want it to be available
  29. ; to the largest audience possible. Of course, if you modify it for your own
  30. ; purposes to add a new feature, we strongly encourage contributing a patch
  31. ; as this feature might be useful for others as well. Send patches or ideas
  32. ; to x264-devel@videolan.org .
  33. %define program_name ff
  34. %ifdef ARCH_X86_64
  35. %ifidn __OUTPUT_FORMAT__,win32
  36. %define WIN64
  37. %else
  38. %define UNIX64
  39. %endif
  40. %endif
  41. %ifdef PREFIX
  42. %define mangle(x) _ %+ x
  43. %else
  44. %define mangle(x) x
  45. %endif
  46. ; FIXME: All of the 64bit asm functions that take a stride as an argument
  47. ; via register, assume that the high dword of that register is filled with 0.
  48. ; This is true in practice (since we never do any 64bit arithmetic on strides,
  49. ; and x264's strides are all positive), but is not guaranteed by the ABI.
  50. ; Name of the .rodata section.
  51. ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
  52. ; so use a different read-only section.
  53. %macro SECTION_RODATA 0-1 16
  54. %ifidn __OUTPUT_FORMAT__,macho64
  55. SECTION .text align=%1
  56. %elifidn __OUTPUT_FORMAT__,macho
  57. SECTION .text align=%1
  58. fakegot:
  59. %elifidn __OUTPUT_FORMAT__,aout
  60. section .text
  61. %else
  62. SECTION .rodata align=%1
  63. %endif
  64. %endmacro
  65. ; aout does not support align=
  66. %macro SECTION_TEXT 0-1 16
  67. %ifidn __OUTPUT_FORMAT__,aout
  68. SECTION .text
  69. %else
  70. SECTION .text align=%1
  71. %endif
  72. %endmacro
  73. %ifdef WIN64
  74. %define PIC
  75. %elifndef ARCH_X86_64
  76. ; x86_32 doesn't require PIC.
  77. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  78. ; the code contains a few textrels, so we'll skip that complexity.
  79. %undef PIC
  80. %endif
  81. %ifdef PIC
  82. default rel
  83. %endif
  84. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  85. ; Currently this works only for leaf functions which load all their arguments
  86. ; into registers at the start, and make no other use of the stack. Luckily that
  87. ; covers most of x264's asm.
  88. ; PROLOGUE:
  89. ; %1 = number of arguments. loads them from stack if needed.
  90. ; %2 = number of registers used. pushes callee-saved regs if needed.
  91. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  92. ; %4 = list of names to define to registers
  93. ; PROLOGUE can also be invoked by adding the same options to cglobal
  94. ; e.g.
  95. ; cglobal foo, 2,3,0, dst, src, tmp
  96. ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
  97. ; TODO Some functions can use some args directly from the stack. If they're the
  98. ; last args then you can just not declare them, but if they're in the middle
  99. ; we need more flexible macro.
  100. ; RET:
  101. ; Pops anything that was pushed by PROLOGUE
  102. ; REP_RET:
  103. ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
  104. ; which are slow when a normal ret follows a branch.
  105. ; registers:
  106. ; rN and rNq are the native-size register holding function argument N
  107. ; rNd, rNw, rNb are dword, word, and byte size
  108. ; rNm is the original location of arg N (a register or on the stack), dword
  109. ; rNmp is native size
  110. %macro DECLARE_REG 6
  111. %define r%1q %2
  112. %define r%1d %3
  113. %define r%1w %4
  114. %define r%1b %5
  115. %define r%1m %6
  116. %ifid %6 ; i.e. it's a register
  117. %define r%1mp %2
  118. %elifdef ARCH_X86_64 ; memory
  119. %define r%1mp qword %6
  120. %else
  121. %define r%1mp dword %6
  122. %endif
  123. %define r%1 %2
  124. %endmacro
  125. %macro DECLARE_REG_SIZE 2
  126. %define r%1q r%1
  127. %define e%1q r%1
  128. %define r%1d e%1
  129. %define e%1d e%1
  130. %define r%1w %1
  131. %define e%1w %1
  132. %define r%1b %2
  133. %define e%1b %2
  134. %ifndef ARCH_X86_64
  135. %define r%1 e%1
  136. %endif
  137. %endmacro
  138. DECLARE_REG_SIZE ax, al
  139. DECLARE_REG_SIZE bx, bl
  140. DECLARE_REG_SIZE cx, cl
  141. DECLARE_REG_SIZE dx, dl
  142. DECLARE_REG_SIZE si, sil
  143. DECLARE_REG_SIZE di, dil
  144. DECLARE_REG_SIZE bp, bpl
  145. ; t# defines for when per-arch register allocation is more complex than just function arguments
  146. %macro DECLARE_REG_TMP 1-*
  147. %assign %%i 0
  148. %rep %0
  149. CAT_XDEFINE t, %%i, r%1
  150. %assign %%i %%i+1
  151. %rotate 1
  152. %endrep
  153. %endmacro
  154. %macro DECLARE_REG_TMP_SIZE 0-*
  155. %rep %0
  156. %define t%1q t%1 %+ q
  157. %define t%1d t%1 %+ d
  158. %define t%1w t%1 %+ w
  159. %define t%1b t%1 %+ b
  160. %rotate 1
  161. %endrep
  162. %endmacro
  163. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9
  164. %ifdef ARCH_X86_64
  165. %define gprsize 8
  166. %else
  167. %define gprsize 4
  168. %endif
  169. %macro PUSH 1
  170. push %1
  171. %assign stack_offset stack_offset+gprsize
  172. %endmacro
  173. %macro POP 1
  174. pop %1
  175. %assign stack_offset stack_offset-gprsize
  176. %endmacro
  177. %macro SUB 2
  178. sub %1, %2
  179. %ifidn %1, rsp
  180. %assign stack_offset stack_offset+(%2)
  181. %endif
  182. %endmacro
  183. %macro ADD 2
  184. add %1, %2
  185. %ifidn %1, rsp
  186. %assign stack_offset stack_offset-(%2)
  187. %endif
  188. %endmacro
  189. %macro movifnidn 2
  190. %ifnidn %1, %2
  191. mov %1, %2
  192. %endif
  193. %endmacro
  194. %macro movsxdifnidn 2
  195. %ifnidn %1, %2
  196. movsxd %1, %2
  197. %endif
  198. %endmacro
  199. %macro ASSERT 1
  200. %if (%1) == 0
  201. %error assert failed
  202. %endif
  203. %endmacro
  204. %macro DEFINE_ARGS 0-*
  205. %ifdef n_arg_names
  206. %assign %%i 0
  207. %rep n_arg_names
  208. CAT_UNDEF arg_name %+ %%i, q
  209. CAT_UNDEF arg_name %+ %%i, d
  210. CAT_UNDEF arg_name %+ %%i, w
  211. CAT_UNDEF arg_name %+ %%i, b
  212. CAT_UNDEF arg_name %+ %%i, m
  213. CAT_UNDEF arg_name, %%i
  214. %assign %%i %%i+1
  215. %endrep
  216. %endif
  217. %assign %%i 0
  218. %rep %0
  219. %xdefine %1q r %+ %%i %+ q
  220. %xdefine %1d r %+ %%i %+ d
  221. %xdefine %1w r %+ %%i %+ w
  222. %xdefine %1b r %+ %%i %+ b
  223. %xdefine %1m r %+ %%i %+ m
  224. CAT_XDEFINE arg_name, %%i, %1
  225. %assign %%i %%i+1
  226. %rotate 1
  227. %endrep
  228. %assign n_arg_names %%i
  229. %endmacro
  230. %ifdef WIN64 ; Windows x64 ;=================================================
  231. DECLARE_REG 0, rcx, ecx, cx, cl, ecx
  232. DECLARE_REG 1, rdx, edx, dx, dl, edx
  233. DECLARE_REG 2, r8, r8d, r8w, r8b, r8d
  234. DECLARE_REG 3, r9, r9d, r9w, r9b, r9d
  235. DECLARE_REG 4, rdi, edi, di, dil, [rsp + stack_offset + 40]
  236. DECLARE_REG 5, rsi, esi, si, sil, [rsp + stack_offset + 48]
  237. DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 56]
  238. %define r7m [rsp + stack_offset + 64]
  239. %define r8m [rsp + stack_offset + 72]
  240. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  241. %if %1 < %2
  242. mov r%1, [rsp + stack_offset + 8 + %1*8]
  243. %endif
  244. %endmacro
  245. %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
  246. ASSERT %2 >= %1
  247. %assign regs_used %2
  248. ASSERT regs_used <= 7
  249. %if regs_used > 4
  250. push r4
  251. push r5
  252. %assign stack_offset stack_offset+16
  253. %endif
  254. WIN64_SPILL_XMM %3
  255. LOAD_IF_USED 4, %1
  256. LOAD_IF_USED 5, %1
  257. LOAD_IF_USED 6, %1
  258. DEFINE_ARGS %4
  259. %endmacro
  260. %macro WIN64_SPILL_XMM 1
  261. %assign xmm_regs_used %1
  262. ASSERT xmm_regs_used <= 16
  263. %if xmm_regs_used > 6
  264. sub rsp, (xmm_regs_used-6)*16+16
  265. %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
  266. %assign %%i xmm_regs_used
  267. %rep (xmm_regs_used-6)
  268. %assign %%i %%i-1
  269. movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
  270. %endrep
  271. %endif
  272. %endmacro
  273. %macro WIN64_RESTORE_XMM_INTERNAL 1
  274. %if xmm_regs_used > 6
  275. %assign %%i xmm_regs_used
  276. %rep (xmm_regs_used-6)
  277. %assign %%i %%i-1
  278. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
  279. %endrep
  280. add %1, (xmm_regs_used-6)*16+16
  281. %endif
  282. %endmacro
  283. %macro WIN64_RESTORE_XMM 1
  284. WIN64_RESTORE_XMM_INTERNAL %1
  285. %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
  286. %assign xmm_regs_used 0
  287. %endmacro
  288. %macro RET 0
  289. WIN64_RESTORE_XMM_INTERNAL rsp
  290. %if regs_used > 4
  291. pop r5
  292. pop r4
  293. %endif
  294. ret
  295. %endmacro
  296. %macro REP_RET 0
  297. %if regs_used > 4 || xmm_regs_used > 6
  298. RET
  299. %else
  300. rep ret
  301. %endif
  302. %endmacro
  303. %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
  304. DECLARE_REG 0, rdi, edi, di, dil, edi
  305. DECLARE_REG 1, rsi, esi, si, sil, esi
  306. DECLARE_REG 2, rdx, edx, dx, dl, edx
  307. DECLARE_REG 3, rcx, ecx, cx, cl, ecx
  308. DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
  309. DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
  310. DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
  311. %define r7m [rsp + stack_offset + 16]
  312. %define r8m [rsp + stack_offset + 24]
  313. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  314. %if %1 < %2
  315. mov r%1, [rsp - 40 + %1*8]
  316. %endif
  317. %endmacro
  318. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  319. ASSERT %2 >= %1
  320. ASSERT %2 <= 7
  321. LOAD_IF_USED 6, %1
  322. DEFINE_ARGS %4
  323. %endmacro
  324. %macro RET 0
  325. ret
  326. %endmacro
  327. %macro REP_RET 0
  328. rep ret
  329. %endmacro
  330. %else ; X86_32 ;==============================================================
  331. DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
  332. DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
  333. DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
  334. DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
  335. DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
  336. DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
  337. DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
  338. %define r7m [esp + stack_offset + 32]
  339. %define r8m [esp + stack_offset + 36]
  340. %define rsp esp
  341. %macro PUSH_IF_USED 1 ; reg_id
  342. %if %1 < regs_used
  343. push r%1
  344. %assign stack_offset stack_offset+4
  345. %endif
  346. %endmacro
  347. %macro POP_IF_USED 1 ; reg_id
  348. %if %1 < regs_used
  349. pop r%1
  350. %endif
  351. %endmacro
  352. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  353. %if %1 < %2
  354. mov r%1, [esp + stack_offset + 4 + %1*4]
  355. %endif
  356. %endmacro
  357. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  358. ASSERT %2 >= %1
  359. %assign regs_used %2
  360. ASSERT regs_used <= 7
  361. PUSH_IF_USED 3
  362. PUSH_IF_USED 4
  363. PUSH_IF_USED 5
  364. PUSH_IF_USED 6
  365. LOAD_IF_USED 0, %1
  366. LOAD_IF_USED 1, %1
  367. LOAD_IF_USED 2, %1
  368. LOAD_IF_USED 3, %1
  369. LOAD_IF_USED 4, %1
  370. LOAD_IF_USED 5, %1
  371. LOAD_IF_USED 6, %1
  372. DEFINE_ARGS %4
  373. %endmacro
  374. %macro RET 0
  375. POP_IF_USED 6
  376. POP_IF_USED 5
  377. POP_IF_USED 4
  378. POP_IF_USED 3
  379. ret
  380. %endmacro
  381. %macro REP_RET 0
  382. %if regs_used > 3
  383. RET
  384. %else
  385. rep ret
  386. %endif
  387. %endmacro
  388. %endif ;======================================================================
  389. %ifndef WIN64
  390. %macro WIN64_SPILL_XMM 1
  391. %endmacro
  392. %macro WIN64_RESTORE_XMM 1
  393. %endmacro
  394. %endif
  395. ;=============================================================================
  396. ; arch-independent part
  397. ;=============================================================================
  398. %assign function_align 16
  399. ; Symbol prefix for C linkage
  400. %macro cglobal 1-2+
  401. %xdefine %1 mangle(program_name %+ _ %+ %1)
  402. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  403. %ifidn __OUTPUT_FORMAT__,elf
  404. global %1:function hidden
  405. %else
  406. global %1
  407. %endif
  408. align function_align
  409. %1:
  410. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  411. %assign stack_offset 0
  412. %if %0 > 1
  413. PROLOGUE %2
  414. %endif
  415. %endmacro
  416. %macro cextern 1
  417. %xdefine %1 mangle(program_name %+ _ %+ %1)
  418. extern %1
  419. %endmacro
  420. ;like cextern, but without the prefix
  421. %macro cextern_naked 1
  422. %xdefine %1 mangle(%1)
  423. extern %1
  424. %endmacro
  425. %macro const 2+
  426. %xdefine %1 mangle(program_name %+ _ %+ %1)
  427. global %1
  428. %1: %2
  429. %endmacro
  430. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  431. ; executable by default.
  432. %ifidn __OUTPUT_FORMAT__,elf
  433. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  434. %endif
  435. ; merge mmx and sse*
  436. %macro CAT_XDEFINE 3
  437. %xdefine %1%2 %3
  438. %endmacro
  439. %macro CAT_UNDEF 2
  440. %undef %1%2
  441. %endmacro
  442. %macro INIT_MMX 0
  443. %assign avx_enabled 0
  444. %define RESET_MM_PERMUTATION INIT_MMX
  445. %define mmsize 8
  446. %define num_mmregs 8
  447. %define mova movq
  448. %define movu movq
  449. %define movh movd
  450. %define movnta movntq
  451. %assign %%i 0
  452. %rep 8
  453. CAT_XDEFINE m, %%i, mm %+ %%i
  454. CAT_XDEFINE nmm, %%i, %%i
  455. %assign %%i %%i+1
  456. %endrep
  457. %rep 8
  458. CAT_UNDEF m, %%i
  459. CAT_UNDEF nmm, %%i
  460. %assign %%i %%i+1
  461. %endrep
  462. %endmacro
  463. %macro INIT_XMM 0
  464. %assign avx_enabled 0
  465. %define RESET_MM_PERMUTATION INIT_XMM
  466. %define mmsize 16
  467. %define num_mmregs 8
  468. %ifdef ARCH_X86_64
  469. %define num_mmregs 16
  470. %endif
  471. %define mova movdqa
  472. %define movu movdqu
  473. %define movh movq
  474. %define movnta movntdq
  475. %assign %%i 0
  476. %rep num_mmregs
  477. CAT_XDEFINE m, %%i, xmm %+ %%i
  478. CAT_XDEFINE nxmm, %%i, %%i
  479. %assign %%i %%i+1
  480. %endrep
  481. %endmacro
  482. %macro INIT_AVX 0
  483. INIT_XMM
  484. %assign avx_enabled 1
  485. %define PALIGNR PALIGNR_SSSE3
  486. %define RESET_MM_PERMUTATION INIT_AVX
  487. %endmacro
  488. %macro INIT_YMM 0
  489. %assign avx_enabled 1
  490. %define RESET_MM_PERMUTATION INIT_YMM
  491. %define mmsize 32
  492. %define num_mmregs 8
  493. %ifdef ARCH_X86_64
  494. %define num_mmregs 16
  495. %endif
  496. %define mova vmovaps
  497. %define movu vmovups
  498. %assign %%i 0
  499. %rep num_mmregs
  500. CAT_XDEFINE m, %%i, ymm %+ %%i
  501. CAT_XDEFINE nymm, %%i, %%i
  502. %assign %%i %%i+1
  503. %endrep
  504. %endmacro
  505. INIT_MMX
  506. ; I often want to use macros that permute their arguments. e.g. there's no
  507. ; efficient way to implement butterfly or transpose or dct without swapping some
  508. ; arguments.
  509. ;
  510. ; I would like to not have to manually keep track of the permutations:
  511. ; If I insert a permutation in the middle of a function, it should automatically
  512. ; change everything that follows. For more complex macros I may also have multiple
  513. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  514. ;
  515. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  516. ; permutes its arguments. It's equivalent to exchanging the contents of the
  517. ; registers, except that this way you exchange the register names instead, so it
  518. ; doesn't cost any cycles.
  519. %macro PERMUTE 2-* ; takes a list of pairs to swap
  520. %rep %0/2
  521. %xdefine tmp%2 m%2
  522. %xdefine ntmp%2 nm%2
  523. %rotate 2
  524. %endrep
  525. %rep %0/2
  526. %xdefine m%1 tmp%2
  527. %xdefine nm%1 ntmp%2
  528. %undef tmp%2
  529. %undef ntmp%2
  530. %rotate 2
  531. %endrep
  532. %endmacro
  533. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  534. %rep %0-1
  535. %ifdef m%1
  536. %xdefine tmp m%1
  537. %xdefine m%1 m%2
  538. %xdefine m%2 tmp
  539. CAT_XDEFINE n, m%1, %1
  540. CAT_XDEFINE n, m%2, %2
  541. %else
  542. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  543. ; Be careful using this mode in nested macros though, as in some cases there may be
  544. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  545. %xdefine %%n1 n %+ %1
  546. %xdefine %%n2 n %+ %2
  547. %xdefine tmp m %+ %%n1
  548. CAT_XDEFINE m, %%n1, m %+ %%n2
  549. CAT_XDEFINE m, %%n2, tmp
  550. CAT_XDEFINE n, m %+ %%n1, %%n1
  551. CAT_XDEFINE n, m %+ %%n2, %%n2
  552. %endif
  553. %undef tmp
  554. %rotate 1
  555. %endrep
  556. %endmacro
  557. ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
  558. ; function name, then any later calls to that function will automatically
  559. ; load the permutation, so values can be returned in mmregs.
  560. %macro SAVE_MM_PERMUTATION 1 ; name to save as
  561. %assign %%i 0
  562. %rep num_mmregs
  563. CAT_XDEFINE %1_m, %%i, m %+ %%i
  564. %assign %%i %%i+1
  565. %endrep
  566. %endmacro
  567. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  568. %assign %%i 0
  569. %rep num_mmregs
  570. CAT_XDEFINE m, %%i, %1_m %+ %%i
  571. CAT_XDEFINE n, m %+ %%i, %%i
  572. %assign %%i %%i+1
  573. %endrep
  574. %endmacro
  575. %macro call 1
  576. call %1
  577. %ifdef %1_m0
  578. LOAD_MM_PERMUTATION %1
  579. %endif
  580. %endmacro
  581. ; Substitutions that reduce instruction size but are functionally equivalent
  582. %macro add 2
  583. %ifnum %2
  584. %if %2==128
  585. sub %1, -128
  586. %else
  587. add %1, %2
  588. %endif
  589. %else
  590. add %1, %2
  591. %endif
  592. %endmacro
  593. %macro sub 2
  594. %ifnum %2
  595. %if %2==128
  596. add %1, -128
  597. %else
  598. sub %1, %2
  599. %endif
  600. %else
  601. sub %1, %2
  602. %endif
  603. %endmacro
  604. ;=============================================================================
  605. ; AVX abstraction layer
  606. ;=============================================================================
  607. %assign i 0
  608. %rep 16
  609. %if i < 8
  610. CAT_XDEFINE sizeofmm, i, 8
  611. %endif
  612. CAT_XDEFINE sizeofxmm, i, 16
  613. CAT_XDEFINE sizeofymm, i, 32
  614. %assign i i+1
  615. %endrep
  616. %undef i
  617. ;%1 == instruction
  618. ;%2 == 1 if float, 0 if int
  619. ;%3 == 0 if 3-operand (xmm, xmm, xmm), 1 if 4-operand (xmm, xmm, xmm, imm)
  620. ;%4 == number of operands given
  621. ;%5+: operands
  622. %macro RUN_AVX_INSTR 6-7+
  623. %if sizeof%5==32
  624. v%1 %5, %6, %7
  625. %else
  626. %if sizeof%5==8
  627. %define %%regmov movq
  628. %elif %2
  629. %define %%regmov movaps
  630. %else
  631. %define %%regmov movdqa
  632. %endif
  633. %if %4>=3+%3
  634. %ifnidn %5, %6
  635. %if avx_enabled && sizeof%5==16
  636. v%1 %5, %6, %7
  637. %else
  638. %%regmov %5, %6
  639. %1 %5, %7
  640. %endif
  641. %else
  642. %1 %5, %7
  643. %endif
  644. %elif %3
  645. %1 %5, %6, %7
  646. %else
  647. %1 %5, %6
  648. %endif
  649. %endif
  650. %endmacro
  651. ;%1 == instruction
  652. ;%2 == 1 if float, 0 if int
  653. ;%3 == 0 if 3-operand (xmm, xmm, xmm), 1 if 4-operand (xmm, xmm, xmm, imm)
  654. %macro AVX_INSTR 3
  655. %macro %1 2-8 fnord, fnord, fnord, %1, %2, %3
  656. %ifidn %3, fnord
  657. RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
  658. %elifidn %4, fnord
  659. RUN_AVX_INSTR %6, %7, %8, 3, %1, %2, %3
  660. %elifidn %5, fnord
  661. RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
  662. %else
  663. RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
  664. %endif
  665. %endmacro
  666. %endmacro
  667. AVX_INSTR addpd, 1, 0
  668. AVX_INSTR addps, 1, 0
  669. AVX_INSTR addsd, 1, 0
  670. AVX_INSTR addss, 1, 0
  671. AVX_INSTR addsubpd, 1, 0
  672. AVX_INSTR addsubps, 1, 0
  673. AVX_INSTR andpd, 1, 0
  674. AVX_INSTR andps, 1, 0
  675. AVX_INSTR andnpd, 1, 0
  676. AVX_INSTR andnps, 1, 0
  677. AVX_INSTR blendpd, 1, 0
  678. AVX_INSTR blendps, 1, 0
  679. AVX_INSTR blendvpd, 1, 0
  680. AVX_INSTR blendvps, 1, 0
  681. AVX_INSTR cmppd, 1, 0
  682. AVX_INSTR cmpps, 1, 0
  683. AVX_INSTR cmpsd, 1, 0
  684. AVX_INSTR cmpss, 1, 0
  685. AVX_INSTR divpd, 1, 0
  686. AVX_INSTR divps, 1, 0
  687. AVX_INSTR divsd, 1, 0
  688. AVX_INSTR divss, 1, 0
  689. AVX_INSTR dppd, 1, 0
  690. AVX_INSTR dpps, 1, 0
  691. AVX_INSTR haddpd, 1, 0
  692. AVX_INSTR haddps, 1, 0
  693. AVX_INSTR hsubpd, 1, 0
  694. AVX_INSTR hsubps, 1, 0
  695. AVX_INSTR maxpd, 1, 0
  696. AVX_INSTR maxps, 1, 0
  697. AVX_INSTR maxsd, 1, 0
  698. AVX_INSTR maxss, 1, 0
  699. AVX_INSTR minpd, 1, 0
  700. AVX_INSTR minps, 1, 0
  701. AVX_INSTR minsd, 1, 0
  702. AVX_INSTR minss, 1, 0
  703. AVX_INSTR mpsadbw, 0, 1
  704. AVX_INSTR mulpd, 1, 0
  705. AVX_INSTR mulps, 1, 0
  706. AVX_INSTR mulsd, 1, 0
  707. AVX_INSTR mulss, 1, 0
  708. AVX_INSTR orpd, 1, 0
  709. AVX_INSTR orps, 1, 0
  710. AVX_INSTR packsswb, 0, 0
  711. AVX_INSTR packssdw, 0, 0
  712. AVX_INSTR packuswb, 0, 0
  713. AVX_INSTR packusdw, 0, 0
  714. AVX_INSTR paddb, 0, 0
  715. AVX_INSTR paddw, 0, 0
  716. AVX_INSTR paddd, 0, 0
  717. AVX_INSTR paddq, 0, 0
  718. AVX_INSTR paddsb, 0, 0
  719. AVX_INSTR paddsw, 0, 0
  720. AVX_INSTR paddusb, 0, 0
  721. AVX_INSTR paddusw, 0, 0
  722. AVX_INSTR palignr, 0, 1
  723. AVX_INSTR pand, 0, 0
  724. AVX_INSTR pandn, 0, 0
  725. AVX_INSTR pavgb, 0, 0
  726. AVX_INSTR pavgw, 0, 0
  727. AVX_INSTR pblendvb, 0, 0
  728. AVX_INSTR pblendw, 0, 1
  729. AVX_INSTR pcmpestri, 0, 0
  730. AVX_INSTR pcmpestrm, 0, 0
  731. AVX_INSTR pcmpistri, 0, 0
  732. AVX_INSTR pcmpistrm, 0, 0
  733. AVX_INSTR pcmpeqb, 0, 0
  734. AVX_INSTR pcmpeqw, 0, 0
  735. AVX_INSTR pcmpeqd, 0, 0
  736. AVX_INSTR pcmpeqq, 0, 0
  737. AVX_INSTR pcmpgtb, 0, 0
  738. AVX_INSTR pcmpgtw, 0, 0
  739. AVX_INSTR pcmpgtd, 0, 0
  740. AVX_INSTR pcmpgtq, 0, 0
  741. AVX_INSTR phaddw, 0, 0
  742. AVX_INSTR phaddd, 0, 0
  743. AVX_INSTR phaddsw, 0, 0
  744. AVX_INSTR phsubw, 0, 0
  745. AVX_INSTR phsubd, 0, 0
  746. AVX_INSTR phsubsw, 0, 0
  747. AVX_INSTR pmaddwd, 0, 0
  748. AVX_INSTR pmaddubsw, 0, 0
  749. AVX_INSTR pmaxsb, 0, 0
  750. AVX_INSTR pmaxsw, 0, 0
  751. AVX_INSTR pmaxsd, 0, 0
  752. AVX_INSTR pmaxub, 0, 0
  753. AVX_INSTR pmaxuw, 0, 0
  754. AVX_INSTR pmaxud, 0, 0
  755. AVX_INSTR pminsb, 0, 0
  756. AVX_INSTR pminsw, 0, 0
  757. AVX_INSTR pminsd, 0, 0
  758. AVX_INSTR pminub, 0, 0
  759. AVX_INSTR pminuw, 0, 0
  760. AVX_INSTR pminud, 0, 0
  761. AVX_INSTR pmulhuw, 0, 0
  762. AVX_INSTR pmulhrsw, 0, 0
  763. AVX_INSTR pmulhw, 0, 0
  764. AVX_INSTR pmullw, 0, 0
  765. AVX_INSTR pmulld, 0, 0
  766. AVX_INSTR pmuludq, 0, 0
  767. AVX_INSTR pmuldq, 0, 0
  768. AVX_INSTR por, 0, 0
  769. AVX_INSTR psadbw, 0, 0
  770. AVX_INSTR pshufb, 0, 0
  771. AVX_INSTR psignb, 0, 0
  772. AVX_INSTR psignw, 0, 0
  773. AVX_INSTR psignd, 0, 0
  774. AVX_INSTR psllw, 0, 0
  775. AVX_INSTR pslld, 0, 0
  776. AVX_INSTR psllq, 0, 0
  777. AVX_INSTR pslldq, 0, 0
  778. AVX_INSTR psraw, 0, 0
  779. AVX_INSTR psrad, 0, 0
  780. AVX_INSTR psrlw, 0, 0
  781. AVX_INSTR psrld, 0, 0
  782. AVX_INSTR psrlq, 0, 0
  783. AVX_INSTR psrldq, 0, 0
  784. AVX_INSTR psubb, 0, 0
  785. AVX_INSTR psubw, 0, 0
  786. AVX_INSTR psubd, 0, 0
  787. AVX_INSTR psubq, 0, 0
  788. AVX_INSTR psubsb, 0, 0
  789. AVX_INSTR psubsw, 0, 0
  790. AVX_INSTR psubusb, 0, 0
  791. AVX_INSTR psubusw, 0, 0
  792. AVX_INSTR punpckhbw, 0, 0
  793. AVX_INSTR punpckhwd, 0, 0
  794. AVX_INSTR punpckhdq, 0, 0
  795. AVX_INSTR punpckhqdq, 0, 0
  796. AVX_INSTR punpcklbw, 0, 0
  797. AVX_INSTR punpcklwd, 0, 0
  798. AVX_INSTR punpckldq, 0, 0
  799. AVX_INSTR punpcklqdq, 0, 0
  800. AVX_INSTR pxor, 0, 0
  801. AVX_INSTR shufps, 0, 1
  802. AVX_INSTR subpd, 1, 0
  803. AVX_INSTR subps, 1, 0
  804. AVX_INSTR subsd, 1, 0
  805. AVX_INSTR subss, 1, 0
  806. AVX_INSTR unpckhpd, 1, 0
  807. AVX_INSTR unpckhps, 1, 0
  808. AVX_INSTR unpcklpd, 1, 0
  809. AVX_INSTR unpcklps, 1, 0
  810. AVX_INSTR xorpd, 1, 0
  811. AVX_INSTR xorps, 1, 0
  812. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  813. AVX_INSTR pfadd, 1, 0
  814. AVX_INSTR pfsub, 1, 0
  815. AVX_INSTR pfmul, 1, 0