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 ;*****************************************************************************
23 ; FIXME: All of the 64bit asm functions that take a stride as an argument
24 ; via register, assume that the high dword of that register is filled with 0.
25 ; This is true in practice (since we never do any 64bit arithmetic on strides,
26 ; and x264's strides are all positive), but is not guaranteed by the ABI.
28 ; Name of the .rodata section.
29 ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
30 ; so use a different read-only section.
31 %macro SECTION_RODATA 0
32 %ifidn __OUTPUT_FORMAT__,macho64
33 SECTION .text align=16
34 %elifidn __OUTPUT_FORMAT__,macho
35 SECTION .text align=16
38 SECTION .rodata align=16
42 ; PIC support macros. All these macros are totally harmless when PIC is
43 ; not defined but can ruin everything if misused in PIC mode. On x86_32, shared
44 ; objects cannot directly access global variables by address, they need to
45 ; go through the GOT (global offset table). Most OSes do not care about it
46 ; and let you load non-shared .so objects (Linux, Win32...). However, OS X
47 ; requires PIC code in its .dylib objects.
49 ; - GLOBAL should be used as a suffix for global addressing, eg.
51 ; mov eax, [foo GLOBAL]
55 ; - picgetgot computes the GOT address into the given register in PIC
56 ; mode, otherwise does nothing. You need to do this before using GLOBAL.
57 ; Before in both execution order and compiled code order (so GLOBAL knows
58 ; which register the GOT is in).
66 %define GLOBAL wrt rip
71 %ifidn __OUTPUT_FORMAT__,macho
72 ; There is no real global offset table on OS X, but we still
73 ; need to reference our variables by offset.
80 %define GLOBAL + %1 - fakegot
83 extern _GLOBAL_OFFSET_TABLE_
88 add %1, _GLOBAL_OFFSET_TABLE_ + $$ - %%getgot wrt ..gotpc
90 %define GLOBAL + %1 wrt ..gotoff
95 ; Macros to eliminate most code duplication between x86_32 and x86_64:
96 ; Currently this works only for leaf functions which load all their arguments
97 ; into registers at the start, and make no other use of the stack. Luckily that
98 ; covers most of x264's asm.
101 ; %1 = number of arguments. loads them from stack if needed.
102 ; %2 = number of registers used, not including PIC. pushes callee-saved regs if needed.
103 ; %3 = whether global constants are used in this function. inits x86_32 PIC if needed.
104 ; %4 = list of names to define to registers
105 ; PROLOGUE can also be invoked by adding the same options to cglobal
108 ; cglobal foo, 2,3,0, dst, src, tmp
109 ; declares a function (foo), taking two args (dst and src), one local variable (tmp), and not using globals
111 ; TODO Some functions can use some args directly from the stack. If they're the
112 ; last args then you can just not declare them, but if they're in the middle
113 ; we need more flexible macro.
116 ; Pops anything that was pushed by PROLOGUE
119 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
120 ; which are slow when a normal ret follows a branch.
131 %macro DECLARE_REG_SIZE 2
145 DECLARE_REG_SIZE ax, al
146 DECLARE_REG_SIZE bx, bl
147 DECLARE_REG_SIZE cx, cl
148 DECLARE_REG_SIZE dx, dl
149 DECLARE_REG_SIZE si, sil
150 DECLARE_REG_SIZE di, dil
151 DECLARE_REG_SIZE bp, bpl
161 %assign stack_offset stack_offset+gprsize
166 %assign stack_offset stack_offset-gprsize
172 %assign stack_offset stack_offset+(%2)
179 %assign stack_offset stack_offset-(%2)
189 %macro movsxdifnidn 2
201 %macro DEFINE_ARGS 0-*
205 CAT_UNDEF arg_name %+ %%i, q
206 CAT_UNDEF arg_name %+ %%i, d
207 CAT_UNDEF arg_name %+ %%i, w
208 CAT_UNDEF arg_name %+ %%i, b
209 CAT_UNDEF arg_name, %%i
216 %xdefine %1q r %+ %%i %+ q
217 %xdefine %1d r %+ %%i %+ d
218 %xdefine %1w r %+ %%i %+ w
219 %xdefine %1b r %+ %%i %+ b
220 CAT_XDEFINE arg_name, %%i, %1
224 %assign n_arg_names %%i
227 %ifdef ARCH_X86_64 ;==========================================================
228 %ifidn __OUTPUT_FORMAT__,win32
230 DECLARE_REG 0, rcx, ecx, cx, cl, ecx
231 DECLARE_REG 1, rdx, edx, dx, dl, edx
232 DECLARE_REG 2, r8, r8d, r8w, r8b, r8d
233 DECLARE_REG 3, r9, r9d, r9w, r9b, r9d
234 DECLARE_REG 4, rdi, edi, di, dil, [rsp + stack_offset + 40]
235 DECLARE_REG 5, rsi, esi, si, sil, [rsp + stack_offset + 48]
236 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 56]
237 %define r7m [rsp + stack_offset + 64]
238 %define r8m [rsp + stack_offset + 72]
240 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
242 mov r%1, [rsp + 8 + %1*8]
246 %else ;=======================================================================
248 DECLARE_REG 0, rdi, edi, di, dil, edi
249 DECLARE_REG 1, rsi, esi, si, sil, esi
250 DECLARE_REG 2, rdx, edx, dx, dl, edx
251 DECLARE_REG 3, rcx, ecx, cx, cl, ecx
252 DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
253 DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
254 DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
255 %define r7m [rsp + stack_offset + 16]
256 %define r8m [rsp + stack_offset + 24]
258 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
260 mov r%1, [rsp - 40 + %1*8]
266 %macro PROLOGUE 2-4+ 0 ; #args, #regs, pic, arg_names...
269 %assign stack_offset 0
270 %ifidn __OUTPUT_FORMAT__,win32
286 %else ; X86_32 ;==============================================================
288 DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
289 DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
290 DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
291 DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
292 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
293 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
294 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
295 %define r7m [esp + stack_offset + 32]
296 %define r8m [esp + stack_offset + 36]
299 %macro PUSH_IF_USED 1 ; reg_id
302 %assign stack_offset stack_offset+4
306 %macro POP_IF_USED 1 ; reg_id
312 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
314 mov r%1, [esp + stack_offset + 4 + %1*4]
318 %macro PROLOGUE 2-4+ 0 ; #args, #regs, pic, arg_names...
320 %assign stack_offset 0
324 %assign regs_used regs_used+1
327 ASSERT regs_used <= 7
361 %endif ;======================================================================
365 ;=============================================================================
366 ; arch-independent part
367 ;=============================================================================
369 %assign function_align 16
371 ; Symbol prefix for C linkage
373 %ifidn __OUTPUT_FORMAT__,elf
375 global _%1:function hidden
378 global %1:function hidden
390 RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
405 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
406 ; executable by default.
407 %ifidn __OUTPUT_FORMAT__,elf
408 SECTION .note.GNU-stack noalloc noexec nowrite progbits
411 %assign FENC_STRIDE 16
412 %assign FDEC_STRIDE 32
425 %define RESET_MM_PERMUTATION INIT_MMX
434 CAT_XDEFINE m, %%i, mm %+ %%i
435 CAT_XDEFINE nmm, %%i, %%i
446 %define RESET_MM_PERMUTATION INIT_XMM
450 %define num_mmregs 16
455 %define movnt movntdq
458 CAT_XDEFINE m, %%i, xmm %+ %%i
459 CAT_XDEFINE nxmm, %%i, %%i
466 ; I often want to use macros that permute their arguments. e.g. there's no
467 ; efficient way to implement butterfly or transpose or dct without swapping some
470 ; I would like to not have to manually keep track of the permutations:
471 ; If I insert a permutation in the middle of a function, it should automatically
472 ; change everything that follows. For more complex macros I may also have multiple
473 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
475 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
476 ; permutes its arguments. It's equivalent to exchanging the contents of the
477 ; registers, except that this way you exchange the register names instead, so it
478 ; doesn't cost any cycles.
480 %macro PERMUTE 2-* ; takes a list of pairs to swap
495 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
501 CAT_XDEFINE n, m%1, %1
502 CAT_XDEFINE n, m%2, %2
504 ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
505 ; Be careful using this mode in nested macros though, as in some cases there may be
506 ; other copies of m# that have already been dereferenced and don't get updated correctly.
507 %xdefine %%n1 n %+ %1
508 %xdefine %%n2 n %+ %2
509 %xdefine tmp m %+ %%n1
510 CAT_XDEFINE m, %%n1, m %+ %%n2
511 CAT_XDEFINE m, %%n2, tmp
512 CAT_XDEFINE n, m %+ %%n1, %%n1
513 CAT_XDEFINE n, m %+ %%n2, %%n2
520 %macro SAVE_MM_PERMUTATION 1
523 CAT_XDEFINE %1_m, %%i, m %+ %%i
528 %macro LOAD_MM_PERMUTATION 1
531 CAT_XDEFINE m, %%i, %1_m %+ %%i
539 LOAD_MM_PERMUTATION %1
543 ; substitutions which are functionally identical but reduce code size
544 %define movdqa movaps
545 %define movdqu movups