1 ;*****************************************************************************
2 ;* x86inc.asm: x264asm abstraction layer
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2012 x264 project
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>
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.
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 ;*****************************************************************************
24 ; This is a header file for the x264ASM assembly language, which uses
25 ; NASM/YASM syntax combined with a large number of macros to provide easy
26 ; abstraction between different calling conventions (x86_32, win64, linux64).
27 ; It also has various other useful features to simplify writing the kind of
28 ; DSP functions that are most often used in x264.
30 ; Unlike the rest of x264, this file is available under an ISC license, as it
31 ; has significant usefulness outside of x264 and we want it to be available
32 ; to the largest audience possible. Of course, if you modify it for your own
33 ; purposes to add a new feature, we strongly encourage contributing a patch
34 ; as this feature might be useful for others as well. Send patches or ideas
35 ; to x264-devel@videolan.org .
38 %define program_name x264
44 %ifidn __OUTPUT_FORMAT__,win32
46 %elifidn __OUTPUT_FORMAT__,win64
54 %define mangle(x) _ %+ x
59 ; Name of the .rodata section.
60 %macro SECTION_RODATA 0-1 16
61 ; Kludge: Something on OS X fails to align .rodata even given an align
62 ; attribute, so use a different read-only section. This has been fixed in
63 ; yasm 0.8.0 and nasm 2.6.
64 %ifdef __YASM_VERSION_ID__
65 %if __YASM_VERSION_ID__ < 00080000h
66 %define NEED_MACHO_RODATA_KLUDGE
68 %elifdef __NASM_VERSION_ID__
69 %if __NASM_VERSION_ID__ < 02060000h
70 %define NEED_MACHO_RODATA_KLUDGE
74 %ifidn __OUTPUT_FORMAT__,aout
77 %ifndef NEED_MACHO_RODATA_KLUDGE
78 SECTION .rodata align=%1
80 %ifidn __OUTPUT_FORMAT__,macho64
81 SECTION .text align=%1
82 %elifidn __OUTPUT_FORMAT__,macho
83 SECTION .text align=%1
86 SECTION .rodata align=%1
91 %undef NEED_MACHO_RODATA_KLUDGE
94 ; aout does not support align=
95 %macro SECTION_TEXT 0-1 16
96 %ifidn __OUTPUT_FORMAT__,aout
99 SECTION .text align=%1
105 %elif ARCH_X86_64 == 0
106 ; x86_32 doesn't require PIC.
107 ; Some distros prefer shared objects to be PIC, but nothing breaks if
108 ; the code contains a few textrels, so we'll skip that complexity.
121 ; Always use long nops (reduces 0x90 spam in disassembly on x86_32)
124 ; Macros to eliminate most code duplication between x86_32 and x86_64:
125 ; Currently this works only for leaf functions which load all their arguments
126 ; into registers at the start, and make no other use of the stack. Luckily that
127 ; covers most of x264's asm.
130 ; %1 = number of arguments. loads them from stack if needed.
131 ; %2 = number of registers used. pushes callee-saved regs if needed.
132 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
133 ; %4 = (optional) stack size to be allocated. If not aligned (x86-32 ICC 10.x,
134 ; MSVC or YMM), the stack will be manually aligned (to 16 or 32 bytes),
135 ; and an extra register will be allocated to hold the original stack
136 ; pointer (to not invalidate r0m etc.). To prevent the use of an extra
137 ; register as stack pointer, request a negative stack size.
138 ; %4+/%5+ = list of names to define to registers
139 ; PROLOGUE can also be invoked by adding the same options to cglobal
142 ; cglobal foo, 2,3,0, dst, src, tmp
143 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
145 ; TODO Some functions can use some args directly from the stack. If they're the
146 ; last args then you can just not declare them, but if they're in the middle
147 ; we need more flexible macro.
150 ; Pops anything that was pushed by PROLOGUE, and returns.
153 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
154 ; which are slow when a normal ret follows a branch.
157 ; rN and rNq are the native-size register holding function argument N
158 ; rNd, rNw, rNb are dword, word, and byte size
159 ; rNh is the high 8 bits of the word size
160 ; rNm is the original location of arg N (a register or on the stack), dword
161 ; rNmp is native size
163 %macro DECLARE_REG 2-3
173 %elif ARCH_X86_64 ; memory
174 %define r%1m [rstk + stack_offset + %3]
175 %define r%1mp qword r %+ %1m
177 %define r%1m [rstk + stack_offset + %3]
178 %define r%1mp dword r %+ %1m
183 %macro DECLARE_REG_SIZE 3
199 DECLARE_REG_SIZE ax, al, ah
200 DECLARE_REG_SIZE bx, bl, bh
201 DECLARE_REG_SIZE cx, cl, ch
202 DECLARE_REG_SIZE dx, dl, dh
203 DECLARE_REG_SIZE si, sil, null
204 DECLARE_REG_SIZE di, dil, null
205 DECLARE_REG_SIZE bp, bpl, null
207 ; t# defines for when per-arch register allocation is more complex than just function arguments
209 %macro DECLARE_REG_TMP 1-*
212 CAT_XDEFINE t, %%i, r%1
218 %macro DECLARE_REG_TMP_SIZE 0-*
220 %define t%1q t%1 %+ q
221 %define t%1d t%1 %+ d
222 %define t%1w t%1 %+ w
223 %define t%1h t%1 %+ h
224 %define t%1b t%1 %+ b
229 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
240 %assign stack_offset stack_offset+gprsize
247 %assign stack_offset stack_offset-gprsize
251 %macro PUSH_IF_USED 1-*
260 %macro POP_IF_USED 1-*
269 %macro LOAD_IF_USED 1-*
272 mov r%1, r %+ %1 %+ mp
281 %assign stack_offset stack_offset+(%2)
288 %assign stack_offset stack_offset-(%2)
298 %macro movsxdifnidn 2
310 %macro DEFINE_ARGS 0-*
314 CAT_UNDEF arg_name %+ %%i, q
315 CAT_UNDEF arg_name %+ %%i, d
316 CAT_UNDEF arg_name %+ %%i, w
317 CAT_UNDEF arg_name %+ %%i, h
318 CAT_UNDEF arg_name %+ %%i, b
319 CAT_UNDEF arg_name %+ %%i, m
320 CAT_UNDEF arg_name %+ %%i, mp
321 CAT_UNDEF arg_name, %%i
326 %xdefine %%stack_offset stack_offset
327 %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
330 %xdefine %1q r %+ %%i %+ q
331 %xdefine %1d r %+ %%i %+ d
332 %xdefine %1w r %+ %%i %+ w
333 %xdefine %1h r %+ %%i %+ h
334 %xdefine %1b r %+ %%i %+ b
335 %xdefine %1m r %+ %%i %+ m
336 %xdefine %1mp r %+ %%i %+ mp
337 CAT_XDEFINE arg_name, %%i, %1
341 %xdefine stack_offset %%stack_offset
342 %assign n_arg_names %0
345 %macro ALLOC_STACK 1-2 0 ; stack_size, n_xmm_regs (for win64 only)
348 %assign %%stack_alignment ((mmsize + 15) & ~15)
349 %assign stack_size %1
351 %assign stack_size -stack_size
353 %assign xmm_regs_used %2
354 %if mmsize <= 16 && HAVE_ALIGNED_STACK
355 %assign stack_size_padded stack_size + %%stack_alignment - gprsize - (stack_offset & (%%stack_alignment - 1))
356 %if xmm_regs_used > 6
357 %assign stack_size_padded stack_size_padded + (xmm_regs_used - 6) * 16
359 SUB rsp, stack_size_padded
361 %assign reg_num (regs_used - 1)
362 %xdefine rstk r %+ reg_num
363 ; align stack, and save original stack location directly above
364 ; it, i.e. in [rsp+stack_size_padded], so we can restore the
365 ; stack in a single instruction (i.e. mov rsp, rstk or mov
366 ; rsp, [rsp+stack_size_padded])
368 %assign stack_size_padded stack_size
369 %if xmm_regs_used > 6
370 %assign stack_size_padded stack_size_padded + (xmm_regs_used - 6) * 16
372 %if %1 < 0 ; need to store rsp on stack
373 sub rsp, gprsize+stack_size_padded
374 and rsp, ~(%%stack_alignment-1)
375 %xdefine rstkm [rsp+stack_size_padded]
377 %else ; can keep rsp in rstk during whole function
378 sub rsp, stack_size_padded
379 and rsp, ~(%%stack_alignment-1)
383 %if xmm_regs_used > 6
390 %macro SETUP_STACK_POINTER 1
392 %if %1 != 0 && (HAVE_ALIGNED_STACK == 0 || mmsize == 32)
394 %assign regs_used (regs_used + 1)
395 %elif ARCH_X86_64 && regs_used == num_args && num_args <= 4 + UNIX64 * 2
396 %warning "Stack pointer will overwrite register argument"
402 %macro DEFINE_ARGS_INTERNAL 3+
412 %if WIN64 ; Windows x64 ;=================================================
418 DECLARE_REG 4, R10, 40
419 DECLARE_REG 5, R11, 48
420 DECLARE_REG 6, rax, 56
421 DECLARE_REG 7, rdi, 64
422 DECLARE_REG 8, rsi, 72
423 DECLARE_REG 9, rbx, 80
424 DECLARE_REG 10, rbp, 88
425 DECLARE_REG 11, R12, 96
426 DECLARE_REG 12, R13, 104
427 DECLARE_REG 13, R14, 112
428 DECLARE_REG 14, R15, 120
430 %macro PROLOGUE 2-5+ 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
433 SETUP_STACK_POINTER %4
434 ASSERT regs_used >= num_args
435 ASSERT regs_used <= 15
436 PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
437 %assign xmm_regs_used 0
439 %if mmsize != 8 && stack_size == 0
442 LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
443 DEFINE_ARGS_INTERNAL %0, %4, %5
446 %macro WIN64_PUSH_XMM 0
447 %assign %%i xmm_regs_used
448 %rep (xmm_regs_used-6)
450 movdqa [rsp + (%%i-6)*16 + stack_size], xmm %+ %%i
454 %macro WIN64_SPILL_XMM 1
455 %assign xmm_regs_used %1
456 ASSERT xmm_regs_used <= 16
457 %if xmm_regs_used > 6
458 %assign stack_size_padded (xmm_regs_used-6)*16+16-gprsize-(stack_offset&15)
459 SUB rsp, stack_size_padded
464 %macro WIN64_RESTORE_XMM_INTERNAL 1
465 %if xmm_regs_used > 6
466 %assign %%i xmm_regs_used
467 %rep (xmm_regs_used-6)
469 movdqa xmm %+ %%i, [%1 + (%%i-6)*16+stack_size]
472 %if stack_size_padded > 0
473 %if stack_size > 0 && (mmsize == 32 || HAVE_ALIGNED_STACK == 0)
476 add %1, stack_size_padded
481 %macro WIN64_RESTORE_XMM 1
482 WIN64_RESTORE_XMM_INTERNAL %1
483 %assign stack_offset (stack_offset-stack_size_padded)
484 %assign xmm_regs_used 0
487 %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32 || stack_size > 0
490 WIN64_RESTORE_XMM_INTERNAL rsp
491 POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
498 %elif ARCH_X86_64 ; *nix x64 ;=============================================
506 DECLARE_REG 6, rax, 8
507 DECLARE_REG 7, R10, 16
508 DECLARE_REG 8, R11, 24
509 DECLARE_REG 9, rbx, 32
510 DECLARE_REG 10, rbp, 40
511 DECLARE_REG 11, R12, 48
512 DECLARE_REG 12, R13, 56
513 DECLARE_REG 13, R14, 64
514 DECLARE_REG 14, R15, 72
516 %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
519 SETUP_STACK_POINTER %4
520 ASSERT regs_used >= num_args
521 ASSERT regs_used <= 15
522 PUSH_IF_USED 9, 10, 11, 12, 13, 14
524 LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
525 DEFINE_ARGS_INTERNAL %0, %4, %5
528 %define has_epilogue regs_used > 9 || mmsize == 32 || stack_size > 0
531 %if stack_size_padded > 0
532 %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
535 add rsp, stack_size_padded
538 POP_IF_USED 14, 13, 12, 11, 10, 9
545 %else ; X86_32 ;==============================================================
547 DECLARE_REG 0, eax, 4
548 DECLARE_REG 1, ecx, 8
549 DECLARE_REG 2, edx, 12
550 DECLARE_REG 3, ebx, 16
551 DECLARE_REG 4, esi, 20
552 DECLARE_REG 5, edi, 24
553 DECLARE_REG 6, ebp, 28
556 %macro DECLARE_ARG 1-*
558 %define r%1m [rstk + stack_offset + 4*%1 + 4]
559 %define r%1mp dword r%1m
564 DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
566 %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
572 SETUP_STACK_POINTER %4
573 ASSERT regs_used <= 7
574 ASSERT regs_used >= num_args
575 PUSH_IF_USED 3, 4, 5, 6
577 LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
578 DEFINE_ARGS_INTERNAL %0, %4, %5
581 %define has_epilogue regs_used > 3 || mmsize == 32 || stack_size > 0
584 %if stack_size_padded > 0
585 %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
588 add rsp, stack_size_padded
591 POP_IF_USED 6, 5, 4, 3
598 %endif ;======================================================================
601 %macro WIN64_SPILL_XMM 1
603 %macro WIN64_RESTORE_XMM 1
605 %macro WIN64_PUSH_XMM 0
617 %macro TAIL_CALL 2 ; callee, is_nonadjacent
626 ;=============================================================================
627 ; arch-independent part
628 ;=============================================================================
630 %assign function_align 16
633 ; Applies any symbol mangling needed for C linkage, and sets up a define such that
634 ; subsequent uses of the function name automatically refer to the mangled version.
635 ; Appends cpuflags to the function name if cpuflags has been specified.
636 %macro cglobal 1-2+ ; name, [PROLOGUE args]
638 cglobal_internal %1 %+ SUFFIX
640 cglobal_internal %1 %+ SUFFIX, %2
643 %macro cglobal_internal 1-2+
645 %xdefine %1 mangle(program_name %+ _ %+ %1)
646 %xdefine %1.skip_prologue %1 %+ .skip_prologue
647 CAT_XDEFINE cglobaled_, %1, 1
649 %xdefine current_function %1
650 %ifidn __OUTPUT_FORMAT__,elf
651 global %1:function hidden
657 RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
659 %assign stack_offset 0
661 %assign stack_size_padded 0
668 %xdefine %1 mangle(program_name %+ _ %+ %1)
669 CAT_XDEFINE cglobaled_, %1, 1
673 ; like cextern, but without the prefix
674 %macro cextern_naked 1
675 %xdefine %1 mangle(%1)
676 CAT_XDEFINE cglobaled_, %1, 1
681 %xdefine %1 mangle(program_name %+ _ %+ %1)
686 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
687 ; executable by default.
688 %ifidn __OUTPUT_FORMAT__,elf
689 SECTION .note.GNU-stack noalloc noexec nowrite progbits
694 %assign cpuflags_mmx (1<<0)
695 %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
696 %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
697 %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
698 %assign cpuflags_sse (1<<4) | cpuflags_mmx2
699 %assign cpuflags_sse2 (1<<5) | cpuflags_sse
700 %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
701 %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
702 %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
703 %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
704 %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
705 %assign cpuflags_avx (1<<11)| cpuflags_sse42
706 %assign cpuflags_xop (1<<12)| cpuflags_avx
707 %assign cpuflags_fma4 (1<<13)| cpuflags_avx
708 %assign cpuflags_avx2 (1<<14)| cpuflags_avx
709 %assign cpuflags_fma3 (1<<15)| cpuflags_avx
711 %assign cpuflags_cache32 (1<<16)
712 %assign cpuflags_cache64 (1<<17)
713 %assign cpuflags_slowctz (1<<18)
714 %assign cpuflags_lzcnt (1<<19)
715 %assign cpuflags_misalign (1<<20)
716 %assign cpuflags_aligned (1<<21) ; not a cpu feature, but a function variant
717 %assign cpuflags_atom (1<<22)
718 %assign cpuflags_bmi1 (1<<23)
719 %assign cpuflags_bmi2 (1<<24)|cpuflags_bmi1
720 %assign cpuflags_tbm (1<<25)|cpuflags_bmi1
722 %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
723 %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
725 ; Takes up to 2 cpuflags from the above list.
726 ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
727 ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
728 %macro INIT_CPUFLAGS 0-2
732 %assign cpuflags cpuflags_%1
734 %xdefine cpuname %1_%2
735 %assign cpuflags cpuflags | cpuflags_%2
737 %xdefine SUFFIX _ %+ cpuname
739 %assign avx_enabled 1
741 %if mmsize == 16 && notcpuflag(sse2)
744 %define movnta movntps
772 %assign avx_enabled 0
773 %define RESET_MM_PERMUTATION INIT_MMX %1
779 %define movnta movntq
782 CAT_XDEFINE m, %%i, mm %+ %%i
783 CAT_XDEFINE nmm, %%i, %%i
795 %assign avx_enabled 0
796 %define RESET_MM_PERMUTATION INIT_XMM %1
800 %define num_mmregs 16
805 %define movnta movntdq
808 CAT_XDEFINE m, %%i, xmm %+ %%i
809 CAT_XDEFINE nxmm, %%i, %%i
815 ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
818 %assign avx_enabled 1
819 %define PALIGNR PALIGNR_SSSE3
820 %define RESET_MM_PERMUTATION INIT_AVX
824 %assign avx_enabled 1
825 %define RESET_MM_PERMUTATION INIT_YMM %1
829 %define num_mmregs 16
834 %define movnta vmovntps
837 CAT_XDEFINE m, %%i, ymm %+ %%i
838 CAT_XDEFINE nymm, %%i, %%i
846 ; I often want to use macros that permute their arguments. e.g. there's no
847 ; efficient way to implement butterfly or transpose or dct without swapping some
850 ; I would like to not have to manually keep track of the permutations:
851 ; If I insert a permutation in the middle of a function, it should automatically
852 ; change everything that follows. For more complex macros I may also have multiple
853 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
855 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
856 ; permutes its arguments. It's equivalent to exchanging the contents of the
857 ; registers, except that this way you exchange the register names instead, so it
858 ; doesn't cost any cycles.
860 %macro PERMUTE 2-* ; takes a list of pairs to swap
875 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
881 CAT_XDEFINE n, m%1, %1
882 CAT_XDEFINE n, m%2, %2
884 ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
885 ; Be careful using this mode in nested macros though, as in some cases there may be
886 ; other copies of m# that have already been dereferenced and don't get updated correctly.
887 %xdefine %%n1 n %+ %1
888 %xdefine %%n2 n %+ %2
889 %xdefine tmp m %+ %%n1
890 CAT_XDEFINE m, %%n1, m %+ %%n2
891 CAT_XDEFINE m, %%n2, tmp
892 CAT_XDEFINE n, m %+ %%n1, %%n1
893 CAT_XDEFINE n, m %+ %%n2, %%n2
900 ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
901 ; calls to that function will automatically load the permutation, so values can
902 ; be returned in mmregs.
903 %macro SAVE_MM_PERMUTATION 0-1
907 %xdefine %%f current_function %+ _m
911 CAT_XDEFINE %%f, %%i, m %+ %%i
916 %macro LOAD_MM_PERMUTATION 1 ; name to load from
920 CAT_XDEFINE m, %%i, %1_m %+ %%i
921 CAT_XDEFINE n, m %+ %%i, %%i
927 ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
929 call_internal %1 %+ SUFFIX, %1
931 %macro call_internal 2
939 LOAD_MM_PERMUTATION %%i
942 ; Substitutions that reduce instruction size but are functionally equivalent
967 ;=============================================================================
968 ; AVX abstraction layer
969 ;=============================================================================
974 CAT_XDEFINE sizeofmm, i, 8
976 CAT_XDEFINE sizeofxmm, i, 16
977 CAT_XDEFINE sizeofymm, i, 32
982 %macro CHECK_AVX_INSTR_EMU 3-*
987 %error non-avx emulation of ``%%opcode'' is not supported
994 ;%2 == 1 if float, 0 if int
995 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
996 ;%4 == number of operands given
998 %macro RUN_AVX_INSTR 6-7+
1000 %define %%sizeofreg sizeof%6
1002 %define %%sizeofreg sizeof%5
1004 %define %%sizeofreg mmsize
1014 %define %%regmov movq
1016 %define %%regmov movaps
1018 %define %%regmov movdqa
1023 %if avx_enabled && %%sizeofreg==16
1026 CHECK_AVX_INSTR_EMU {%1 %5, %6, %7}, %5, %7
1041 ; 3arg AVX ops with a memory arg can only have it in src2,
1042 ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
1043 ; So, if the op is symmetric and the wrong one is memory, swap them.
1044 %macro RUN_AVX_INSTR1 8
1055 %if %%swap && %3 == 0 && %8 == 1
1056 RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
1058 RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
1063 ;%2 == 1 if float, 0 if int
1064 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
1065 ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
1067 %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
1069 RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
1071 RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
1073 RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
1075 RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
1080 AVX_INSTR addpd, 1, 0, 1
1081 AVX_INSTR addps, 1, 0, 1
1082 AVX_INSTR addsd, 1, 0, 1
1083 AVX_INSTR addss, 1, 0, 1
1084 AVX_INSTR addsubpd, 1, 0, 0
1085 AVX_INSTR addsubps, 1, 0, 0
1086 AVX_INSTR andpd, 1, 0, 1
1087 AVX_INSTR andps, 1, 0, 1
1088 AVX_INSTR andnpd, 1, 0, 0
1089 AVX_INSTR andnps, 1, 0, 0
1090 AVX_INSTR blendpd, 1, 0, 0
1091 AVX_INSTR blendps, 1, 0, 0
1092 AVX_INSTR blendvpd, 1, 0, 0
1093 AVX_INSTR blendvps, 1, 0, 0
1094 AVX_INSTR cmppd, 1, 0, 0
1095 AVX_INSTR cmpps, 1, 0, 0
1096 AVX_INSTR cmpsd, 1, 0, 0
1097 AVX_INSTR cmpss, 1, 0, 0
1098 AVX_INSTR cvtdq2ps, 1, 0, 0
1099 AVX_INSTR cvtpd2dq, 1, 0, 0
1100 AVX_INSTR cvtps2dq, 1, 0, 0
1101 AVX_INSTR divpd, 1, 0, 0
1102 AVX_INSTR divps, 1, 0, 0
1103 AVX_INSTR divsd, 1, 0, 0
1104 AVX_INSTR divss, 1, 0, 0
1105 AVX_INSTR dppd, 1, 1, 0
1106 AVX_INSTR dpps, 1, 1, 0
1107 AVX_INSTR haddpd, 1, 0, 0
1108 AVX_INSTR haddps, 1, 0, 0
1109 AVX_INSTR hsubpd, 1, 0, 0
1110 AVX_INSTR hsubps, 1, 0, 0
1111 AVX_INSTR maxpd, 1, 0, 1
1112 AVX_INSTR maxps, 1, 0, 1
1113 AVX_INSTR maxsd, 1, 0, 1
1114 AVX_INSTR maxss, 1, 0, 1
1115 AVX_INSTR minpd, 1, 0, 1
1116 AVX_INSTR minps, 1, 0, 1
1117 AVX_INSTR minsd, 1, 0, 1
1118 AVX_INSTR minss, 1, 0, 1
1119 AVX_INSTR movhlps, 1, 0, 0
1120 AVX_INSTR movlhps, 1, 0, 0
1121 AVX_INSTR movsd, 1, 0, 0
1122 AVX_INSTR movss, 1, 0, 0
1123 AVX_INSTR mpsadbw, 0, 1, 0
1124 AVX_INSTR mulpd, 1, 0, 1
1125 AVX_INSTR mulps, 1, 0, 1
1126 AVX_INSTR mulsd, 1, 0, 1
1127 AVX_INSTR mulss, 1, 0, 1
1128 AVX_INSTR orpd, 1, 0, 1
1129 AVX_INSTR orps, 1, 0, 1
1130 AVX_INSTR pabsb, 0, 0, 0
1131 AVX_INSTR pabsw, 0, 0, 0
1132 AVX_INSTR pabsd, 0, 0, 0
1133 AVX_INSTR packsswb, 0, 0, 0
1134 AVX_INSTR packssdw, 0, 0, 0
1135 AVX_INSTR packuswb, 0, 0, 0
1136 AVX_INSTR packusdw, 0, 0, 0
1137 AVX_INSTR paddb, 0, 0, 1
1138 AVX_INSTR paddw, 0, 0, 1
1139 AVX_INSTR paddd, 0, 0, 1
1140 AVX_INSTR paddq, 0, 0, 1
1141 AVX_INSTR paddsb, 0, 0, 1
1142 AVX_INSTR paddsw, 0, 0, 1
1143 AVX_INSTR paddusb, 0, 0, 1
1144 AVX_INSTR paddusw, 0, 0, 1
1145 AVX_INSTR palignr, 0, 1, 0
1146 AVX_INSTR pand, 0, 0, 1
1147 AVX_INSTR pandn, 0, 0, 0
1148 AVX_INSTR pavgb, 0, 0, 1
1149 AVX_INSTR pavgw, 0, 0, 1
1150 AVX_INSTR pblendvb, 0, 0, 0
1151 AVX_INSTR pblendw, 0, 1, 0
1152 AVX_INSTR pcmpestri, 0, 0, 0
1153 AVX_INSTR pcmpestrm, 0, 0, 0
1154 AVX_INSTR pcmpistri, 0, 0, 0
1155 AVX_INSTR pcmpistrm, 0, 0, 0
1156 AVX_INSTR pcmpeqb, 0, 0, 1
1157 AVX_INSTR pcmpeqw, 0, 0, 1
1158 AVX_INSTR pcmpeqd, 0, 0, 1
1159 AVX_INSTR pcmpeqq, 0, 0, 1
1160 AVX_INSTR pcmpgtb, 0, 0, 0
1161 AVX_INSTR pcmpgtw, 0, 0, 0
1162 AVX_INSTR pcmpgtd, 0, 0, 0
1163 AVX_INSTR pcmpgtq, 0, 0, 0
1164 AVX_INSTR phaddw, 0, 0, 0
1165 AVX_INSTR phaddd, 0, 0, 0
1166 AVX_INSTR phaddsw, 0, 0, 0
1167 AVX_INSTR phsubw, 0, 0, 0
1168 AVX_INSTR phsubd, 0, 0, 0
1169 AVX_INSTR phsubsw, 0, 0, 0
1170 AVX_INSTR pmaddwd, 0, 0, 1
1171 AVX_INSTR pmaddubsw, 0, 0, 0
1172 AVX_INSTR pmaxsb, 0, 0, 1
1173 AVX_INSTR pmaxsw, 0, 0, 1
1174 AVX_INSTR pmaxsd, 0, 0, 1
1175 AVX_INSTR pmaxub, 0, 0, 1
1176 AVX_INSTR pmaxuw, 0, 0, 1
1177 AVX_INSTR pmaxud, 0, 0, 1
1178 AVX_INSTR pminsb, 0, 0, 1
1179 AVX_INSTR pminsw, 0, 0, 1
1180 AVX_INSTR pminsd, 0, 0, 1
1181 AVX_INSTR pminub, 0, 0, 1
1182 AVX_INSTR pminuw, 0, 0, 1
1183 AVX_INSTR pminud, 0, 0, 1
1184 AVX_INSTR pmovmskb, 0, 0, 0
1185 AVX_INSTR pmulhuw, 0, 0, 1
1186 AVX_INSTR pmulhrsw, 0, 0, 1
1187 AVX_INSTR pmulhw, 0, 0, 1
1188 AVX_INSTR pmullw, 0, 0, 1
1189 AVX_INSTR pmulld, 0, 0, 1
1190 AVX_INSTR pmuludq, 0, 0, 1
1191 AVX_INSTR pmuldq, 0, 0, 1
1192 AVX_INSTR por, 0, 0, 1
1193 AVX_INSTR psadbw, 0, 0, 1
1194 AVX_INSTR pshufb, 0, 0, 0
1195 AVX_INSTR pshufd, 0, 1, 0
1196 AVX_INSTR pshufhw, 0, 1, 0
1197 AVX_INSTR pshuflw, 0, 1, 0
1198 AVX_INSTR psignb, 0, 0, 0
1199 AVX_INSTR psignw, 0, 0, 0
1200 AVX_INSTR psignd, 0, 0, 0
1201 AVX_INSTR psllw, 0, 0, 0
1202 AVX_INSTR pslld, 0, 0, 0
1203 AVX_INSTR psllq, 0, 0, 0
1204 AVX_INSTR pslldq, 0, 0, 0
1205 AVX_INSTR psraw, 0, 0, 0
1206 AVX_INSTR psrad, 0, 0, 0
1207 AVX_INSTR psrlw, 0, 0, 0
1208 AVX_INSTR psrld, 0, 0, 0
1209 AVX_INSTR psrlq, 0, 0, 0
1210 AVX_INSTR psrldq, 0, 0, 0
1211 AVX_INSTR psubb, 0, 0, 0
1212 AVX_INSTR psubw, 0, 0, 0
1213 AVX_INSTR psubd, 0, 0, 0
1214 AVX_INSTR psubq, 0, 0, 0
1215 AVX_INSTR psubsb, 0, 0, 0
1216 AVX_INSTR psubsw, 0, 0, 0
1217 AVX_INSTR psubusb, 0, 0, 0
1218 AVX_INSTR psubusw, 0, 0, 0
1219 AVX_INSTR ptest, 0, 0, 0
1220 AVX_INSTR punpckhbw, 0, 0, 0
1221 AVX_INSTR punpckhwd, 0, 0, 0
1222 AVX_INSTR punpckhdq, 0, 0, 0
1223 AVX_INSTR punpckhqdq, 0, 0, 0
1224 AVX_INSTR punpcklbw, 0, 0, 0
1225 AVX_INSTR punpcklwd, 0, 0, 0
1226 AVX_INSTR punpckldq, 0, 0, 0
1227 AVX_INSTR punpcklqdq, 0, 0, 0
1228 AVX_INSTR pxor, 0, 0, 1
1229 AVX_INSTR shufps, 1, 1, 0
1230 AVX_INSTR subpd, 1, 0, 0
1231 AVX_INSTR subps, 1, 0, 0
1232 AVX_INSTR subsd, 1, 0, 0
1233 AVX_INSTR subss, 1, 0, 0
1234 AVX_INSTR unpckhpd, 1, 0, 0
1235 AVX_INSTR unpckhps, 1, 0, 0
1236 AVX_INSTR unpcklpd, 1, 0, 0
1237 AVX_INSTR unpcklps, 1, 0, 0
1238 AVX_INSTR xorpd, 1, 0, 1
1239 AVX_INSTR xorps, 1, 0, 1
1241 ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
1242 AVX_INSTR pfadd, 1, 0, 1
1243 AVX_INSTR pfsub, 1, 0, 0
1244 AVX_INSTR pfmul, 1, 0, 1
1246 ; base-4 constants for shuffles
1249 %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
1251 CAT_XDEFINE q000, j, i
1253 CAT_XDEFINE q00, j, i
1255 CAT_XDEFINE q0, j, i
1265 %macro %1 5-8 %1, %2, %3
1266 %if cpuflag(xop) || cpuflag(fma4)
1280 FMA_INSTR fmaddps, mulps, addps
1281 FMA_INSTR pmacsdd, pmulld, paddd
1282 FMA_INSTR pmacsww, pmullw, paddw
1283 FMA_INSTR pmadcswd, pmaddwd, paddd
1285 ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
1286 ; This lets us use tzcnt without bumping the yasm version requirement yet.
1287 %define tzcnt rep bsf