1 ;*****************************************************************************
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2008 Loren Merritt <lorenm@u.washington.edu>
6 ;* This file is part of FFmpeg.
8 ;* FFmpeg is free software; you can redistribute it and/or
9 ;* modify it under the terms of the GNU Lesser General Public
10 ;* License as published by the Free Software Foundation; either
11 ;* version 2.1 of the License, or (at your option) any later version.
13 ;* FFmpeg is distributed in the hope that it will be useful,
14 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;* Lesser General Public License for more details.
18 ;* You should have received a copy of the GNU Lesser General Public
19 ;* License along with FFmpeg; if not, write to the Free Software
20 ;* 51, Inc., Foundation Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;*****************************************************************************
24 %ifidn __OUTPUT_FORMAT__,win32
31 ; FIXME: All of the 64bit asm functions that take a stride as an argument
32 ; via register, assume that the high dword of that register is filled with 0.
33 ; This is true in practice (since we never do any 64bit arithmetic on strides,
34 ; and x264's strides are all positive), but is not guaranteed by the ABI.
36 ; Name of the .rodata section.
37 ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
38 ; so use a different read-only section.
39 %macro SECTION_RODATA 0-1 16
40 %ifidn __OUTPUT_FORMAT__,macho64
41 SECTION .text align=%1
42 %elifidn __OUTPUT_FORMAT__,macho
43 SECTION .text align=%1
46 SECTION .rodata align=%1
51 ; x86_64 can't fit 64bit address literals in most instruction types,
52 ; so shared objects (under the assumption that they might be anywhere
53 ; in memory) must use an address mode that does fit.
54 ; So all accesses to global variables must use this macro, e.g.
55 ; mov eax, [foo GLOBAL]
59 ; x86_32 doesn't require PIC.
60 ; Some distros prefer shared objects to be PIC, but nothing breaks if
61 ; the code contains a few textrels, so we'll skip that complexity.
69 %define GLOBAL wrt rip
74 ; Macros to eliminate most code duplication between x86_32 and x86_64:
75 ; Currently this works only for leaf functions which load all their arguments
76 ; into registers at the start, and make no other use of the stack. Luckily that
77 ; covers most of x264's asm.
80 ; %1 = number of arguments. loads them from stack if needed.
81 ; %2 = number of registers used. pushes callee-saved regs if needed.
82 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
83 ; %4 = list of names to define to registers
84 ; PROLOGUE can also be invoked by adding the same options to cglobal
87 ; cglobal foo, 2,3, dst, src, tmp
88 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
90 ; TODO Some functions can use some args directly from the stack. If they're the
91 ; last args then you can just not declare them, but if they're in the middle
92 ; we need more flexible macro.
95 ; Pops anything that was pushed by PROLOGUE
98 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
99 ; which are slow when a normal ret follows a branch.
102 ; rN and rNq are the native-size register holding function argument N
103 ; rNd, rNw, rNb are dword, word, and byte size
104 ; rNm is the original location of arg N (a register or on the stack), dword
105 ; rNmp is native size
113 %ifid %6 ; i.e. it's a register
115 %elifdef ARCH_X86_64 ; memory
116 %define r%1mp qword %6
118 %define r%1mp dword %6
123 %macro DECLARE_REG_SIZE 2
137 DECLARE_REG_SIZE ax, al
138 DECLARE_REG_SIZE bx, bl
139 DECLARE_REG_SIZE cx, cl
140 DECLARE_REG_SIZE dx, dl
141 DECLARE_REG_SIZE si, sil
142 DECLARE_REG_SIZE di, dil
143 DECLARE_REG_SIZE bp, bpl
145 ; t# defines for when per-arch register allocation is more complex than just function arguments
147 %macro DECLARE_REG_TMP 1-*
150 CAT_XDEFINE t, %%i, r%1
156 %macro DECLARE_REG_TMP_SIZE 0-*
158 %define t%1q t%1 %+ q
159 %define t%1d t%1 %+ d
160 %define t%1w t%1 %+ w
161 %define t%1b t%1 %+ b
166 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7
176 %assign stack_offset stack_offset+gprsize
181 %assign stack_offset stack_offset-gprsize
187 %assign stack_offset stack_offset+(%2)
194 %assign stack_offset stack_offset-(%2)
204 %macro movsxdifnidn 2
216 %macro DEFINE_ARGS 0-*
220 CAT_UNDEF arg_name %+ %%i, q
221 CAT_UNDEF arg_name %+ %%i, d
222 CAT_UNDEF arg_name %+ %%i, w
223 CAT_UNDEF arg_name %+ %%i, b
224 CAT_UNDEF arg_name, %%i
231 %xdefine %1q r %+ %%i %+ q
232 %xdefine %1d r %+ %%i %+ d
233 %xdefine %1w r %+ %%i %+ w
234 %xdefine %1b r %+ %%i %+ b
235 CAT_XDEFINE arg_name, %%i, %1
239 %assign n_arg_names %%i
242 %ifdef WIN64 ; Windows x64 ;=================================================
244 DECLARE_REG 0, rcx, ecx, cx, cl, ecx
245 DECLARE_REG 1, rdx, edx, dx, dl, edx
246 DECLARE_REG 2, r8, r8d, r8w, r8b, r8d
247 DECLARE_REG 3, r9, r9d, r9w, r9b, r9d
248 DECLARE_REG 4, rdi, edi, di, dil, [rsp + stack_offset + 40]
249 DECLARE_REG 5, rsi, esi, si, sil, [rsp + stack_offset + 48]
250 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 56]
251 %define r7m [rsp + stack_offset + 64]
252 %define r8m [rsp + stack_offset + 72]
254 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
256 mov r%1, [rsp + stack_offset + 8 + %1*8]
260 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
263 ASSERT regs_used <= 7
265 %assign xmm_regs_used %3
267 %assign xmm_regs_used 0
269 ASSERT xmm_regs_used <= 16
273 %assign stack_offset stack_offset+16
275 %if xmm_regs_used > 6
276 sub rsp, (xmm_regs_used-6)*16+16
277 %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
278 %assign %%i xmm_regs_used
279 %rep (xmm_regs_used-6)
281 movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
290 %macro RESTORE_XMM_INTERNAL 1
291 %if xmm_regs_used > 6
292 %assign %%i xmm_regs_used
293 %rep (xmm_regs_used-6)
295 movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
297 add %1, (xmm_regs_used-6)*16+16
302 RESTORE_XMM_INTERNAL %1
303 %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
304 %assign xmm_regs_used 0
308 RESTORE_XMM_INTERNAL rsp
317 %if regs_used > 4 || xmm_regs_used > 6
324 %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
326 DECLARE_REG 0, rdi, edi, di, dil, edi
327 DECLARE_REG 1, rsi, esi, si, sil, esi
328 DECLARE_REG 2, rdx, edx, dx, dl, edx
329 DECLARE_REG 3, rcx, ecx, cx, cl, ecx
330 DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
331 DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
332 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
333 %define r7m [rsp + stack_offset + 16]
334 %define r8m [rsp + stack_offset + 24]
336 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
338 mov r%1, [rsp - 40 + %1*8]
342 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
357 %else ; X86_32 ;==============================================================
359 DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
360 DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
361 DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
362 DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
363 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
364 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
365 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
366 %define r7m [esp + stack_offset + 32]
367 %define r8m [esp + stack_offset + 36]
370 %macro PUSH_IF_USED 1 ; reg_id
373 %assign stack_offset stack_offset+4
377 %macro POP_IF_USED 1 ; reg_id
383 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
385 mov r%1, [esp + stack_offset + 4 + %1*4]
389 %macro PROLOGUE 2-4+ ; #args, #regs, arg_names...
392 ASSERT regs_used <= 7
423 %endif ;======================================================================
427 ;=============================================================================
428 ; arch-independent part
429 ;=============================================================================
431 %assign function_align 16
433 ; Symbol prefix for C linkage
439 %ifidn __OUTPUT_FORMAT__,elf
440 global %1:function hidden
446 RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
447 %assign stack_offset 0
460 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
461 ; executable by default.
462 %ifidn __OUTPUT_FORMAT__,elf
463 SECTION .note.GNU-stack noalloc noexec nowrite progbits
466 %assign FENC_STRIDE 16
467 %assign FDEC_STRIDE 32
480 %define RESET_MM_PERMUTATION INIT_MMX
489 CAT_XDEFINE m, %%i, mm %+ %%i
490 CAT_XDEFINE nmm, %%i, %%i
501 %define RESET_MM_PERMUTATION INIT_XMM
505 %define num_mmregs 16
510 %define movnt movntdq
513 CAT_XDEFINE m, %%i, xmm %+ %%i
514 CAT_XDEFINE nxmm, %%i, %%i
521 ; I often want to use macros that permute their arguments. e.g. there's no
522 ; efficient way to implement butterfly or transpose or dct without swapping some
525 ; I would like to not have to manually keep track of the permutations:
526 ; If I insert a permutation in the middle of a function, it should automatically
527 ; change everything that follows. For more complex macros I may also have multiple
528 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
530 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
531 ; permutes its arguments. It's equivalent to exchanging the contents of the
532 ; registers, except that this way you exchange the register names instead, so it
533 ; doesn't cost any cycles.
535 %macro PERMUTE 2-* ; takes a list of pairs to swap
550 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
556 CAT_XDEFINE n, m%1, %1
557 CAT_XDEFINE n, m%2, %2
559 ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
560 ; Be careful using this mode in nested macros though, as in some cases there may be
561 ; other copies of m# that have already been dereferenced and don't get updated correctly.
562 %xdefine %%n1 n %+ %1
563 %xdefine %%n2 n %+ %2
564 %xdefine tmp m %+ %%n1
565 CAT_XDEFINE m, %%n1, m %+ %%n2
566 CAT_XDEFINE m, %%n2, tmp
567 CAT_XDEFINE n, m %+ %%n1, %%n1
568 CAT_XDEFINE n, m %+ %%n2, %%n2
575 %macro SAVE_MM_PERMUTATION 1
578 CAT_XDEFINE %1_m, %%i, m %+ %%i
583 %macro LOAD_MM_PERMUTATION 1
586 CAT_XDEFINE m, %%i, %1_m %+ %%i
587 CAT_XDEFINE n, m %+ %%i, %%i
595 LOAD_MM_PERMUTATION %1
599 ;Substitutions that reduce instruction size but are functionally equivalent
600 %define movdqa movaps
601 %define movdqu movups