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.

1145 lines
29KB

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