]> git.sesse.net Git - x264/blob - common/x86/x86inc.asm
b2aee3f800f061368b2437fd89f7a4d4dbc1876f
[x264] / common / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2008 Loren Merritt <lorenm@u.washington.edu>
5 ;*
6 ;* This program is free software; you can redistribute it and/or modify
7 ;* it under the terms of the GNU General Public License as published by
8 ;* the Free Software Foundation; either version 2 of the License, or
9 ;* (at your option) any later version.
10 ;*
11 ;* This program is distributed in the hope that it will be useful,
12 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;* GNU General Public License for more details.
15 ;*
16 ;* You should have received a copy of the GNU General Public License
17 ;* along with this program; if not, write to the Free Software
18 ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19 ;*****************************************************************************
20
21 ; FIXME: All of the 64bit asm functions that take a stride as an argument
22 ; via register, assume that the high dword of that register is filled with 0.
23 ; This is true in practice (since we never do any 64bit arithmetic on strides,
24 ; and x264's strides are all positive), but is not guaranteed by the ABI.
25
26 ; Name of the .rodata section.
27 ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
28 ; so use a different read-only section.
29 %macro SECTION_RODATA 0
30     %ifidn __OUTPUT_FORMAT__,macho64
31         SECTION .text align=16
32     %elifidn __OUTPUT_FORMAT__,macho
33         SECTION .text align=16
34         fakegot:
35     %else
36         SECTION .rodata align=16
37     %endif
38 %endmacro
39
40 ; PIC support macros.
41 ; x86_64 can't fit 64bit address literals in most instruction types,
42 ; so shared objects (under the assumption that they might be anywhere
43 ; in memory) must use an address mode that does fit.
44 ; So all accesses to global variables must use this macro, e.g.
45 ;     mov eax, [foo GLOBAL]
46 ; instead of
47 ;     mov eax, [foo]
48 ;
49 ; x86_32 doesn't require PIC.
50 ; Some distros prefer shared objects to be PIC, but nothing breaks if
51 ; the code contains a few textrels, so we'll skip that complexity.
52
53 %ifndef ARCH_X86_64
54     %undef PIC
55 %endif
56 %ifdef PIC
57     %define GLOBAL wrt rip
58 %else
59     %define GLOBAL
60 %endif
61
62 ; Macros to eliminate most code duplication between x86_32 and x86_64:
63 ; Currently this works only for leaf functions which load all their arguments
64 ; into registers at the start, and make no other use of the stack. Luckily that
65 ; covers most of x264's asm.
66
67 ; PROLOGUE:
68 ; %1 = number of arguments. loads them from stack if needed.
69 ; %2 = number of registers used. pushes callee-saved regs if needed.
70 ; %3 = list of names to define to registers
71 ; PROLOGUE can also be invoked by adding the same options to cglobal
72
73 ; e.g.
74 ; cglobal foo, 2,3, dst, src, tmp
75 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
76
77 ; TODO Some functions can use some args directly from the stack. If they're the
78 ; last args then you can just not declare them, but if they're in the middle
79 ; we need more flexible macro.
80
81 ; RET:
82 ; Pops anything that was pushed by PROLOGUE
83
84 ; REP_RET:
85 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
86 ; which are slow when a normal ret follows a branch.
87
88 %macro DECLARE_REG 6
89     %define r%1q %2
90     %define r%1d %3
91     %define r%1w %4
92     %define r%1b %5
93     %define r%1m %6
94     %define r%1  %2
95 %endmacro
96
97 %macro DECLARE_REG_SIZE 2
98     %define r%1q r%1
99     %define e%1q r%1
100     %define r%1d e%1
101     %define e%1d e%1
102     %define r%1w %1
103     %define e%1w %1
104     %define r%1b %2
105     %define e%1b %2
106 %ifndef ARCH_X86_64
107     %define r%1  e%1
108 %endif
109 %endmacro
110
111 DECLARE_REG_SIZE ax, al
112 DECLARE_REG_SIZE bx, bl
113 DECLARE_REG_SIZE cx, cl
114 DECLARE_REG_SIZE dx, dl
115 DECLARE_REG_SIZE si, sil
116 DECLARE_REG_SIZE di, dil
117 DECLARE_REG_SIZE bp, bpl
118
119 ; t# defines for when per-arch register allocation is more complex than just function arguments
120
121 %macro DECLARE_REG_TMP 1-*
122     %assign %%i 0
123     %rep %0
124         CAT_XDEFINE t, %%i, r%1
125         %assign %%i %%i+1
126         %rotate 1
127     %endrep
128 %endmacro
129
130 %macro DECLARE_REG_TMP_SIZE 0-*
131     %rep %0
132         %define t%1q t%1 %+ q
133         %define t%1d t%1 %+ d
134         %define t%1w t%1 %+ w
135         %define t%1b t%1 %+ b
136         %rotate 1
137     %endrep
138 %endmacro
139
140 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7
141
142 %ifdef ARCH_X86_64
143     %define gprsize 8
144 %else
145     %define gprsize 4
146 %endif
147
148 %macro PUSH 1
149     push %1
150     %assign stack_offset stack_offset+gprsize
151 %endmacro
152
153 %macro POP 1
154     pop %1
155     %assign stack_offset stack_offset-gprsize
156 %endmacro
157
158 %macro SUB 2
159     sub %1, %2
160     %ifidn %1, rsp
161         %assign stack_offset stack_offset+(%2)
162     %endif
163 %endmacro
164
165 %macro ADD 2
166     add %1, %2
167     %ifidn %1, rsp
168         %assign stack_offset stack_offset-(%2)
169     %endif
170 %endmacro
171
172 %macro movifnidn 2
173     %ifnidn %1, %2
174         mov %1, %2
175     %endif
176 %endmacro
177
178 %macro movsxdifnidn 2
179     %ifnidn %1, %2
180         movsxd %1, %2
181     %endif
182 %endmacro
183
184 %macro ASSERT 1
185     %if (%1) == 0
186         %error assert failed
187     %endif
188 %endmacro
189
190 %macro DEFINE_ARGS 0-*
191     %ifdef n_arg_names
192         %assign %%i 0
193         %rep n_arg_names
194             CAT_UNDEF arg_name %+ %%i, q
195             CAT_UNDEF arg_name %+ %%i, d
196             CAT_UNDEF arg_name %+ %%i, w
197             CAT_UNDEF arg_name %+ %%i, b
198             CAT_UNDEF arg_name, %%i
199             %assign %%i %%i+1
200         %endrep
201     %endif
202
203     %assign %%i 0
204     %rep %0
205         %xdefine %1q r %+ %%i %+ q
206         %xdefine %1d r %+ %%i %+ d
207         %xdefine %1w r %+ %%i %+ w
208         %xdefine %1b r %+ %%i %+ b
209         CAT_XDEFINE arg_name, %%i, %1
210         %assign %%i %%i+1
211         %rotate 1
212     %endrep
213     %assign n_arg_names %%i
214 %endmacro
215
216 %ifdef ARCH_X86_64 ;========================================================
217
218 DECLARE_REG 0, rdi, edi, di,  dil, edi
219 DECLARE_REG 1, rsi, esi, si,  sil, esi
220 DECLARE_REG 2, rdx, edx, dx,  dl,  edx
221 DECLARE_REG 3, rcx, ecx, cx,  cl,  ecx
222 DECLARE_REG 4, r8,  r8d, r8w, r8b, r8d
223 DECLARE_REG 5, r9,  r9d, r9w, r9b, r9d
224 DECLARE_REG 6, rax, eax, ax,  al,  [rsp + stack_offset + 8]
225 %define r7m [rsp + stack_offset + 16]
226 %define r8m [rsp + stack_offset + 24]
227
228 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
229     %if %1 < %2
230         mov r%1, [rsp - 40 + %1*8]
231     %endif
232 %endmacro
233
234 %macro PROLOGUE 2-3+ ; #args, #regs, arg_names...
235     ASSERT %2 >= %1
236     ASSERT %2 <= 7
237     %assign stack_offset 0
238     LOAD_IF_USED 6, %1
239     DEFINE_ARGS %3
240 %endmacro
241
242 %macro RET 0
243     ret
244 %endmacro
245
246 %macro REP_RET 0
247     rep ret
248 %endmacro
249
250 %else ; X86_32 ;==============================================================
251
252 DECLARE_REG 0, eax, eax, ax, al,   [esp + stack_offset + 4]
253 DECLARE_REG 1, ecx, ecx, cx, cl,   [esp + stack_offset + 8]
254 DECLARE_REG 2, edx, edx, dx, dl,   [esp + stack_offset + 12]
255 DECLARE_REG 3, ebx, ebx, bx, bl,   [esp + stack_offset + 16]
256 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
257 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
258 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
259 %define r7m [esp + stack_offset + 32]
260 %define r8m [esp + stack_offset + 36]
261 %define rsp esp
262
263 %macro PUSH_IF_USED 1 ; reg_id
264     %if %1 < regs_used
265         push r%1
266         %assign stack_offset stack_offset+4
267     %endif
268 %endmacro
269
270 %macro POP_IF_USED 1 ; reg_id
271     %if %1 < regs_used
272         pop r%1
273     %endif
274 %endmacro
275
276 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
277     %if %1 < %2
278         mov r%1, [esp + stack_offset + 4 + %1*4]
279     %endif
280 %endmacro
281
282 %macro PROLOGUE 2-3+ ; #args, #regs, arg_names...
283     ASSERT %2 >= %1
284     %assign stack_offset 0
285     %assign regs_used %2
286     ASSERT regs_used <= 7
287     PUSH_IF_USED 3
288     PUSH_IF_USED 4
289     PUSH_IF_USED 5
290     PUSH_IF_USED 6
291     LOAD_IF_USED 0, %1
292     LOAD_IF_USED 1, %1
293     LOAD_IF_USED 2, %1
294     LOAD_IF_USED 3, %1
295     LOAD_IF_USED 4, %1
296     LOAD_IF_USED 5, %1
297     LOAD_IF_USED 6, %1
298     DEFINE_ARGS %3
299 %endmacro
300
301 %macro RET 0
302     POP_IF_USED 6
303     POP_IF_USED 5
304     POP_IF_USED 4
305     POP_IF_USED 3
306     ret
307 %endmacro
308
309 %macro REP_RET 0
310     %if regs_used > 3
311         RET
312     %else
313         rep ret
314     %endif
315 %endmacro
316
317 %endif ;======================================================================
318
319
320
321 ;=============================================================================
322 ; arch-independent part
323 ;=============================================================================
324
325 %assign function_align 16
326
327 ; Symbol prefix for C linkage
328 %macro cglobal 1-2+
329     %ifidn __OUTPUT_FORMAT__,elf
330         %ifdef PREFIX
331             global _%1:function hidden
332             %define %1 _%1
333         %else
334             global %1:function hidden
335         %endif
336     %else
337         %ifdef PREFIX
338             global _%1
339             %define %1 _%1
340         %else
341             global %1
342         %endif
343     %endif
344     align function_align
345     %1:
346     RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
347     %if %0 > 1
348         PROLOGUE %2
349     %endif
350 %endmacro
351
352 %macro cextern 1
353     %ifdef PREFIX
354         extern _%1
355         %define %1 _%1
356     %else
357         extern %1
358     %endif
359 %endmacro
360
361 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
362 ; executable by default.
363 %ifidn __OUTPUT_FORMAT__,elf
364 SECTION .note.GNU-stack noalloc noexec nowrite progbits
365 %endif
366
367 %assign FENC_STRIDE 16
368 %assign FDEC_STRIDE 32
369
370 ; merge mmx and sse*
371
372 %macro CAT_XDEFINE 3
373     %xdefine %1%2 %3
374 %endmacro
375
376 %macro CAT_UNDEF 2
377     %undef %1%2
378 %endmacro
379
380 %macro INIT_MMX 0
381     %define RESET_MM_PERMUTATION INIT_MMX
382     %define mmsize 8
383     %define num_mmregs 8
384     %define mova movq
385     %define movu movq
386     %define movh movd
387     %define movnt movntq
388     %assign %%i 0
389     %rep 8
390     CAT_XDEFINE m, %%i, mm %+ %%i
391     CAT_XDEFINE nmm, %%i, %%i
392     %assign %%i %%i+1
393     %endrep
394     %rep 8
395     CAT_UNDEF m, %%i
396     CAT_UNDEF nmm, %%i
397     %assign %%i %%i+1
398     %endrep
399 %endmacro
400
401 %macro INIT_XMM 0
402     %define RESET_MM_PERMUTATION INIT_XMM
403     %define mmsize 16
404     %define num_mmregs 8
405     %ifdef ARCH_X86_64
406     %define num_mmregs 16
407     %endif
408     %define mova movdqa
409     %define movu movdqu
410     %define movh movq
411     %define movnt movntdq
412     %assign %%i 0
413     %rep num_mmregs
414     CAT_XDEFINE m, %%i, xmm %+ %%i
415     CAT_XDEFINE nxmm, %%i, %%i
416     %assign %%i %%i+1
417     %endrep
418 %endmacro
419
420 INIT_MMX
421
422 ; I often want to use macros that permute their arguments. e.g. there's no
423 ; efficient way to implement butterfly or transpose or dct without swapping some
424 ; arguments.
425 ;
426 ; I would like to not have to manually keep track of the permutations:
427 ; If I insert a permutation in the middle of a function, it should automatically
428 ; change everything that follows. For more complex macros I may also have multiple
429 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
430 ;
431 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
432 ; permutes its arguments. It's equivalent to exchanging the contents of the
433 ; registers, except that this way you exchange the register names instead, so it
434 ; doesn't cost any cycles.
435
436 %macro PERMUTE 2-* ; takes a list of pairs to swap
437 %rep %0/2
438     %xdefine tmp%2 m%2
439     %xdefine ntmp%2 nm%2
440     %rotate 2
441 %endrep
442 %rep %0/2
443     %xdefine m%1 tmp%2
444     %xdefine nm%1 ntmp%2
445     %undef tmp%2
446     %undef ntmp%2
447     %rotate 2
448 %endrep
449 %endmacro
450
451 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
452 %rep %0-1
453 %ifdef m%1
454     %xdefine tmp m%1
455     %xdefine m%1 m%2
456     %xdefine m%2 tmp
457     CAT_XDEFINE n, m%1, %1
458     CAT_XDEFINE n, m%2, %2
459 %else
460     ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
461     ; Be careful using this mode in nested macros though, as in some cases there may be
462     ; other copies of m# that have already been dereferenced and don't get updated correctly.
463     %xdefine %%n1 n %+ %1
464     %xdefine %%n2 n %+ %2
465     %xdefine tmp m %+ %%n1
466     CAT_XDEFINE m, %%n1, m %+ %%n2
467     CAT_XDEFINE m, %%n2, tmp
468     CAT_XDEFINE n, m %+ %%n1, %%n1
469     CAT_XDEFINE n, m %+ %%n2, %%n2
470 %endif
471     %undef tmp
472     %rotate 1
473 %endrep
474 %endmacro
475
476 %macro SAVE_MM_PERMUTATION 1
477     %assign %%i 0
478     %rep num_mmregs
479     CAT_XDEFINE %1_m, %%i, m %+ %%i
480     %assign %%i %%i+1
481     %endrep
482 %endmacro
483
484 %macro LOAD_MM_PERMUTATION 1
485     %assign %%i 0
486     %rep num_mmregs
487     CAT_XDEFINE m, %%i, %1_m %+ %%i
488     CAT_XDEFINE n, m %+ %%i, %%i
489     %assign %%i %%i+1
490     %endrep
491 %endmacro
492
493 %macro call 1
494     call %1
495     %ifdef %1_m0
496         LOAD_MM_PERMUTATION %1
497     %endif
498 %endmacro
499