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
377 %ifidn __OUTPUT_FORMAT__
,elf
378 global %1:function hidden
384 RESET_MM_PERMUTATION
; not really needed, but makes disassembly somewhat nicer
399 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
400 ; executable by default.
401 %ifidn __OUTPUT_FORMAT__
,elf
402 SECTION .note.GNU
-stack noalloc noexec nowrite progbits
405 %assign FENC_STRIDE
16
406 %assign FDEC_STRIDE
32
419 %define RESET_MM_PERMUTATION INIT_MMX
428 CAT_XDEFINE m
, %%i
, mm
%+ %%i
429 CAT_XDEFINE nmm
, %%i
, %%i
440 %define RESET_MM_PERMUTATION INIT_XMM
444 %define num_mmregs
16
449 %define movnt movntdq
452 CAT_XDEFINE m
, %%i
, xmm
%+ %%i
453 CAT_XDEFINE nxmm
, %%i
, %%i
460 ; I often want to use macros that permute their arguments. e.g. there's no
461 ; efficient way to implement butterfly or transpose or dct without swapping some
464 ; I would like to not have to manually keep track of the permutations:
465 ; If I insert a permutation in the middle of a function, it should automatically
466 ; change everything that follows. For more complex macros I may also have multiple
467 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
469 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
470 ; permutes its arguments. It's equivalent to exchanging the contents of the
471 ; registers, except that this way you exchange the register names instead, so it
472 ; doesn't cost any cycles.
474 %macro PERMUTE
2-* ; takes a list of pairs to swap
489 %macro SWAP
2-* ; swaps a single chain (sometimes more concise than pairs)
495 CAT_XDEFINE n
, m
%1, %1
496 CAT_XDEFINE n
, m
%2, %2
498 ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
499 ; Be careful using this mode in nested macros though, as in some cases there may be
500 ; other copies of m# that have already been dereferenced and don't get updated correctly.
501 %xdefine
%%n1 n
%+ %1
502 %xdefine
%%n2 n
%+ %2
503 %xdefine tmp m
%+ %%n1
504 CAT_XDEFINE m
, %%n1
, m
%+ %%n2
505 CAT_XDEFINE m
, %%n2
, tmp
506 CAT_XDEFINE n
, m
%+ %%n1
, %%n1
507 CAT_XDEFINE n
, m
%+ %%n2
, %%n2
514 %macro SAVE_MM_PERMUTATION
1
517 CAT_XDEFINE
%1_m
, %%i
, m
%+ %%i
522 %macro LOAD_MM_PERMUTATION
1
525 CAT_XDEFINE m
, %%i
, %1_m
%+ %%i
533 LOAD_MM_PERMUTATION
%1
537 ; substitutions which are functionally identical but reduce code size
538 %define movdqa movaps
539 %define movdqu movups