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.

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