1 ;*****************************************************************************
2 ;* x86inc.asm: x264asm abstraction layer
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2010 x264 project
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;* Anton Mitrofanov <BugMaster@narod.ru>
8 ;* Fiona Glaser <fiona@x264.com>
10 ;* Permission to use, copy, modify, and/or distribute this software for any
11 ;* purpose with or without fee is hereby granted, provided that the above
12 ;* copyright notice and this permission notice appear in all copies.
14 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ;*****************************************************************************
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.
29 ; Unlike the rest of x264, this file is available under an ISC license, as it
30 ; has significant usefulness outside of x264 and we want it to be available
31 ; to the largest audience possible. Of course, if you modify it for your own
32 ; purposes to add a new feature, we strongly encourage contributing a patch
33 ; as this feature might be useful for others as well. Send patches or ideas
34 ; to x264-devel@videolan.org .
36 %define program_name x264
39 %ifidn __OUTPUT_FORMAT__,win32
47 %define mangle(x) _ %+ x
52 ; FIXME: All of the 64bit asm functions that take a stride as an argument
53 ; via register, assume that the high dword of that register is filled with 0.
54 ; This is true in practice (since we never do any 64bit arithmetic on strides,
55 ; and x264's strides are all positive), but is not guaranteed by the ABI.
57 ; Name of the .rodata section.
58 ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
59 ; so use a different read-only section.
60 %macro SECTION_RODATA 0-1 16
61 %ifidn __OUTPUT_FORMAT__,macho64
62 SECTION .text align=%1
63 %elifidn __OUTPUT_FORMAT__,macho
64 SECTION .text align=%1
67 SECTION .rodata align=%1
74 ; x86_32 doesn't require PIC.
75 ; Some distros prefer shared objects to be PIC, but nothing breaks if
76 ; the code contains a few textrels, so we'll skip that complexity.
83 ; Macros to eliminate most code duplication between x86_32 and x86_64:
84 ; Currently this works only for leaf functions which load all their arguments
85 ; into registers at the start, and make no other use of the stack. Luckily that
86 ; covers most of x264's asm.
89 ; %1 = number of arguments. loads them from stack if needed.
90 ; %2 = number of registers used. pushes callee-saved regs if needed.
91 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
92 ; %4 = list of names to define to registers
93 ; PROLOGUE can also be invoked by adding the same options to cglobal
96 ; cglobal foo, 2,3,0, dst, src, tmp
97 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
99 ; TODO Some functions can use some args directly from the stack. If they're the
100 ; last args then you can just not declare them, but if they're in the middle
101 ; we need more flexible macro.
104 ; Pops anything that was pushed by PROLOGUE
107 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
108 ; which are slow when a normal ret follows a branch.
111 ; rN and rNq are the native-size register holding function argument N
112 ; rNd, rNw, rNb are dword, word, and byte size
113 ; rNm is the original location of arg N (a register or on the stack), dword
114 ; rNmp is native size
122 %ifid %6 ; i.e. it's a register
124 %elifdef ARCH_X86_64 ; memory
125 %define r%1mp qword %6
127 %define r%1mp dword %6
132 %macro DECLARE_REG_SIZE 2
146 DECLARE_REG_SIZE ax, al
147 DECLARE_REG_SIZE bx, bl
148 DECLARE_REG_SIZE cx, cl
149 DECLARE_REG_SIZE dx, dl
150 DECLARE_REG_SIZE si, sil
151 DECLARE_REG_SIZE di, dil
152 DECLARE_REG_SIZE bp, bpl
154 ; t# defines for when per-arch register allocation is more complex than just function arguments
156 %macro DECLARE_REG_TMP 1-*
159 CAT_XDEFINE t, %%i, r%1
165 %macro DECLARE_REG_TMP_SIZE 0-*
167 %define t%1q t%1 %+ q
168 %define t%1d t%1 %+ d
169 %define t%1w t%1 %+ w
170 %define t%1b t%1 %+ b
175 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9
185 %assign stack_offset stack_offset+gprsize
190 %assign stack_offset stack_offset-gprsize
196 %assign stack_offset stack_offset+(%2)
203 %assign stack_offset stack_offset-(%2)
213 %macro movsxdifnidn 2
225 %macro DEFINE_ARGS 0-*
229 CAT_UNDEF arg_name %+ %%i, q
230 CAT_UNDEF arg_name %+ %%i, d
231 CAT_UNDEF arg_name %+ %%i, w
232 CAT_UNDEF arg_name %+ %%i, b
233 CAT_UNDEF arg_name %+ %%i, m
234 CAT_UNDEF arg_name, %%i
241 %xdefine %1q r %+ %%i %+ q
242 %xdefine %1d r %+ %%i %+ d
243 %xdefine %1w r %+ %%i %+ w
244 %xdefine %1b r %+ %%i %+ b
245 %xdefine %1m r %+ %%i %+ m
246 CAT_XDEFINE arg_name, %%i, %1
250 %assign n_arg_names %%i
253 %ifdef WIN64 ; Windows x64 ;=================================================
255 DECLARE_REG 0, rcx, ecx, cx, cl, ecx
256 DECLARE_REG 1, rdx, edx, dx, dl, edx
257 DECLARE_REG 2, r8, r8d, r8w, r8b, r8d
258 DECLARE_REG 3, r9, r9d, r9w, r9b, r9d
259 DECLARE_REG 4, rdi, edi, di, dil, [rsp + stack_offset + 40]
260 DECLARE_REG 5, rsi, esi, si, sil, [rsp + stack_offset + 48]
261 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 56]
262 %define r7m [rsp + stack_offset + 64]
263 %define r8m [rsp + stack_offset + 72]
265 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
267 mov r%1, [rsp + stack_offset + 8 + %1*8]
271 %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
274 ASSERT regs_used <= 7
278 %assign stack_offset stack_offset+16
287 %macro WIN64_SPILL_XMM 1
288 %assign xmm_regs_used %1
289 ASSERT xmm_regs_used <= 16
290 %if xmm_regs_used > 6
291 sub rsp, (xmm_regs_used-6)*16+16
292 %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
293 %assign %%i xmm_regs_used
294 %rep (xmm_regs_used-6)
296 movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
301 %macro WIN64_RESTORE_XMM_INTERNAL 1
302 %if xmm_regs_used > 6
303 %assign %%i xmm_regs_used
304 %rep (xmm_regs_used-6)
306 movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
308 add %1, (xmm_regs_used-6)*16+16
312 %macro WIN64_RESTORE_XMM 1
313 WIN64_RESTORE_XMM_INTERNAL %1
314 %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
315 %assign xmm_regs_used 0
319 WIN64_RESTORE_XMM_INTERNAL rsp
328 %if regs_used > 4 || xmm_regs_used > 6
335 %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
337 DECLARE_REG 0, rdi, edi, di, dil, edi
338 DECLARE_REG 1, rsi, esi, si, sil, esi
339 DECLARE_REG 2, rdx, edx, dx, dl, edx
340 DECLARE_REG 3, rcx, ecx, cx, cl, ecx
341 DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
342 DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
343 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
344 %define r7m [rsp + stack_offset + 16]
345 %define r8m [rsp + stack_offset + 24]
347 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
349 mov r%1, [rsp - 40 + %1*8]
353 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
368 %else ; X86_32 ;==============================================================
370 DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
371 DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
372 DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
373 DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
374 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
375 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
376 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
377 %define r7m [esp + stack_offset + 32]
378 %define r8m [esp + stack_offset + 36]
381 %macro PUSH_IF_USED 1 ; reg_id
384 %assign stack_offset stack_offset+4
388 %macro POP_IF_USED 1 ; reg_id
394 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
396 mov r%1, [esp + stack_offset + 4 + %1*4]
400 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
403 ASSERT regs_used <= 7
434 %endif ;======================================================================
437 %macro WIN64_SPILL_XMM 1
439 %macro WIN64_RESTORE_XMM 1
445 ;=============================================================================
446 ; arch-independent part
447 ;=============================================================================
449 %assign function_align 16
451 ; Symbol prefix for C linkage
453 %xdefine %1 mangle(program_name %+ _ %+ %1)
454 %xdefine %1.skip_prologue %1 %+ .skip_prologue
455 %ifidn __OUTPUT_FORMAT__,elf
456 global %1:function hidden
462 RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
463 %assign stack_offset 0
470 %xdefine %1 mangle(program_name %+ _ %+ %1)
474 ;like cextern, but without the prefix
475 %macro cextern_naked 1
476 %xdefine %1 mangle(%1)
481 %xdefine %1 mangle(program_name %+ _ %+ %1)
486 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
487 ; executable by default.
488 %ifidn __OUTPUT_FORMAT__,elf
489 SECTION .note.GNU-stack noalloc noexec nowrite progbits
503 %define RESET_MM_PERMUTATION INIT_MMX
509 %define movnta movntq
512 CAT_XDEFINE m, %%i, mm %+ %%i
513 CAT_XDEFINE nmm, %%i, %%i
524 %define RESET_MM_PERMUTATION INIT_XMM
528 %define num_mmregs 16
533 %define movnta movntdq
536 CAT_XDEFINE m, %%i, xmm %+ %%i
537 CAT_XDEFINE nxmm, %%i, %%i
544 ; I often want to use macros that permute their arguments. e.g. there's no
545 ; efficient way to implement butterfly or transpose or dct without swapping some
548 ; I would like to not have to manually keep track of the permutations:
549 ; If I insert a permutation in the middle of a function, it should automatically
550 ; change everything that follows. For more complex macros I may also have multiple
551 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
553 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
554 ; permutes its arguments. It's equivalent to exchanging the contents of the
555 ; registers, except that this way you exchange the register names instead, so it
556 ; doesn't cost any cycles.
558 %macro PERMUTE 2-* ; takes a list of pairs to swap
573 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
579 CAT_XDEFINE n, m%1, %1
580 CAT_XDEFINE n, m%2, %2
582 ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
583 ; Be careful using this mode in nested macros though, as in some cases there may be
584 ; other copies of m# that have already been dereferenced and don't get updated correctly.
585 %xdefine %%n1 n %+ %1
586 %xdefine %%n2 n %+ %2
587 %xdefine tmp m %+ %%n1
588 CAT_XDEFINE m, %%n1, m %+ %%n2
589 CAT_XDEFINE m, %%n2, tmp
590 CAT_XDEFINE n, m %+ %%n1, %%n1
591 CAT_XDEFINE n, m %+ %%n2, %%n2
598 ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
599 ; function name, then any later calls to that function will automatically
600 ; load the permutation, so values can be returned in mmregs.
601 %macro SAVE_MM_PERMUTATION 1 ; name to save as
604 CAT_XDEFINE %1_m, %%i, m %+ %%i
609 %macro LOAD_MM_PERMUTATION 1 ; name to load from
612 CAT_XDEFINE m, %%i, %1_m %+ %%i
613 CAT_XDEFINE n, m %+ %%i, %%i
621 LOAD_MM_PERMUTATION %1
625 ; Substitutions that reduce instruction size but are functionally equivalent