]> git.sesse.net Git - x264/blob - common/x86/x86inc.asm
Remove unnecessary PIC support macros
[x264] / common / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2008 x264 project
5 ;*
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;*          Anton Mitrofanov <BugMaster@narod.ru>
8 ;*
9 ;* Permission to use, copy, modify, and/or distribute this software for any
10 ;* purpose with or without fee is hereby granted, provided that the above
11 ;* copyright notice and this permission notice appear in all copies.
12 ;*
13 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ;*****************************************************************************
21
22 ; This is a header file for the x264ASM assembly language, which uses
23 ; NASM/YASM syntax combined with a large number of macros to provide easy
24 ; abstraction between different calling conventions (x86_32, win64, linux64).
25 ; It also has various other useful features to simplify writing the kind of
26 ; DSP functions that are most often used in x264.
27
28 ; Unlike the rest of x264, this file is available under an ISC license, as it
29 ; has significant usefulness outside of x264 and we want it to be available
30 ; to the largest audience possible.  Of course, if you modify it for your own
31 ; purposes to add a new feature, we strongly encourage contributing a patch
32 ; as this feature might be useful for others as well.  Send patches or ideas
33 ; to x264-devel@videolan.org .
34
35 %ifdef ARCH_X86_64
36     %ifidn __OUTPUT_FORMAT__,win32
37         %define WIN64
38     %else
39         %define UNIX64
40     %endif
41 %endif
42
43 %ifdef PREFIX
44     %define mangle(x) _ %+ x
45 %else
46     %define mangle(x) x
47 %endif
48
49 ; FIXME: All of the 64bit asm functions that take a stride as an argument
50 ; via register, assume that the high dword of that register is filled with 0.
51 ; This is true in practice (since we never do any 64bit arithmetic on strides,
52 ; and x264's strides are all positive), but is not guaranteed by the ABI.
53
54 ; Name of the .rodata section.
55 ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
56 ; so use a different read-only section.
57 %macro SECTION_RODATA 0-1 16
58     %ifidn __OUTPUT_FORMAT__,macho64
59         SECTION .text align=%1
60     %elifidn __OUTPUT_FORMAT__,macho
61         SECTION .text align=%1
62         fakegot:
63     %else
64         SECTION .rodata align=%1
65     %endif
66 %endmacro
67
68 %ifdef WIN64
69     %define PIC
70 %elifndef ARCH_X86_64
71 ; x86_32 doesn't require PIC.
72 ; Some distros prefer shared objects to be PIC, but nothing breaks if
73 ; the code contains a few textrels, so we'll skip that complexity.
74     %undef PIC
75 %endif
76 %ifdef PIC
77     default rel
78 %endif
79
80 ; Macros to eliminate most code duplication between x86_32 and x86_64:
81 ; Currently this works only for leaf functions which load all their arguments
82 ; into registers at the start, and make no other use of the stack. Luckily that
83 ; covers most of x264's asm.
84
85 ; PROLOGUE:
86 ; %1 = number of arguments. loads them from stack if needed.
87 ; %2 = number of registers used. pushes callee-saved regs if needed.
88 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
89 ; %4 = list of names to define to registers
90 ; PROLOGUE can also be invoked by adding the same options to cglobal
91
92 ; e.g.
93 ; cglobal foo, 2,3,0, dst, src, tmp
94 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
95
96 ; TODO Some functions can use some args directly from the stack. If they're the
97 ; last args then you can just not declare them, but if they're in the middle
98 ; we need more flexible macro.
99
100 ; RET:
101 ; Pops anything that was pushed by PROLOGUE
102
103 ; REP_RET:
104 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
105 ; which are slow when a normal ret follows a branch.
106
107 ; registers:
108 ; rN and rNq are the native-size register holding function argument N
109 ; rNd, rNw, rNb are dword, word, and byte size
110 ; rNm is the original location of arg N (a register or on the stack), dword
111 ; rNmp is native size
112
113 %macro DECLARE_REG 6
114     %define r%1q %2
115     %define r%1d %3
116     %define r%1w %4
117     %define r%1b %5
118     %define r%1m %6
119     %ifid %6 ; i.e. it's a register
120         %define r%1mp %2
121     %elifdef ARCH_X86_64 ; memory
122         %define r%1mp qword %6
123     %else
124         %define r%1mp dword %6
125     %endif
126     %define r%1  %2
127 %endmacro
128
129 %macro DECLARE_REG_SIZE 2
130     %define r%1q r%1
131     %define e%1q r%1
132     %define r%1d e%1
133     %define e%1d e%1
134     %define r%1w %1
135     %define e%1w %1
136     %define r%1b %2
137     %define e%1b %2
138 %ifndef ARCH_X86_64
139     %define r%1  e%1
140 %endif
141 %endmacro
142
143 DECLARE_REG_SIZE ax, al
144 DECLARE_REG_SIZE bx, bl
145 DECLARE_REG_SIZE cx, cl
146 DECLARE_REG_SIZE dx, dl
147 DECLARE_REG_SIZE si, sil
148 DECLARE_REG_SIZE di, dil
149 DECLARE_REG_SIZE bp, bpl
150
151 ; t# defines for when per-arch register allocation is more complex than just function arguments
152
153 %macro DECLARE_REG_TMP 1-*
154     %assign %%i 0
155     %rep %0
156         CAT_XDEFINE t, %%i, r%1
157         %assign %%i %%i+1
158         %rotate 1
159     %endrep
160 %endmacro
161
162 %macro DECLARE_REG_TMP_SIZE 0-*
163     %rep %0
164         %define t%1q t%1 %+ q
165         %define t%1d t%1 %+ d
166         %define t%1w t%1 %+ w
167         %define t%1b t%1 %+ b
168         %rotate 1
169     %endrep
170 %endmacro
171
172 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7
173
174 %ifdef ARCH_X86_64
175     %define gprsize 8
176 %else
177     %define gprsize 4
178 %endif
179
180 %macro PUSH 1
181     push %1
182     %assign stack_offset stack_offset+gprsize
183 %endmacro
184
185 %macro POP 1
186     pop %1
187     %assign stack_offset stack_offset-gprsize
188 %endmacro
189
190 %macro SUB 2
191     sub %1, %2
192     %ifidn %1, rsp
193         %assign stack_offset stack_offset+(%2)
194     %endif
195 %endmacro
196
197 %macro ADD 2
198     add %1, %2
199     %ifidn %1, rsp
200         %assign stack_offset stack_offset-(%2)
201     %endif
202 %endmacro
203
204 %macro movifnidn 2
205     %ifnidn %1, %2
206         mov %1, %2
207     %endif
208 %endmacro
209
210 %macro movsxdifnidn 2
211     %ifnidn %1, %2
212         movsxd %1, %2
213     %endif
214 %endmacro
215
216 %macro ASSERT 1
217     %if (%1) == 0
218         %error assert failed
219     %endif
220 %endmacro
221
222 %macro DEFINE_ARGS 0-*
223     %ifdef n_arg_names
224         %assign %%i 0
225         %rep n_arg_names
226             CAT_UNDEF arg_name %+ %%i, q
227             CAT_UNDEF arg_name %+ %%i, d
228             CAT_UNDEF arg_name %+ %%i, w
229             CAT_UNDEF arg_name %+ %%i, b
230             CAT_UNDEF arg_name %+ %%i, m
231             CAT_UNDEF arg_name, %%i
232             %assign %%i %%i+1
233         %endrep
234     %endif
235
236     %assign %%i 0
237     %rep %0
238         %xdefine %1q r %+ %%i %+ q
239         %xdefine %1d r %+ %%i %+ d
240         %xdefine %1w r %+ %%i %+ w
241         %xdefine %1b r %+ %%i %+ b
242         %xdefine %1m r %+ %%i %+ m
243         CAT_XDEFINE arg_name, %%i, %1
244         %assign %%i %%i+1
245         %rotate 1
246     %endrep
247     %assign n_arg_names %%i
248 %endmacro
249
250 %ifdef WIN64 ; Windows x64 ;=================================================
251
252 DECLARE_REG 0, rcx, ecx, cx,  cl,  ecx
253 DECLARE_REG 1, rdx, edx, dx,  dl,  edx
254 DECLARE_REG 2, r8,  r8d, r8w, r8b, r8d
255 DECLARE_REG 3, r9,  r9d, r9w, r9b, r9d
256 DECLARE_REG 4, rdi, edi, di,  dil, [rsp + stack_offset + 40]
257 DECLARE_REG 5, rsi, esi, si,  sil, [rsp + stack_offset + 48]
258 DECLARE_REG 6, rax, eax, ax,  al,  [rsp + stack_offset + 56]
259 %define r7m [rsp + stack_offset + 64]
260 %define r8m [rsp + stack_offset + 72]
261
262 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
263     %if %1 < %2
264         mov r%1, [rsp + stack_offset + 8 + %1*8]
265     %endif
266 %endmacro
267
268 %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
269     ASSERT %2 >= %1
270     %assign regs_used %2
271     ASSERT regs_used <= 7
272     %assign xmm_regs_used %3
273     ASSERT xmm_regs_used <= 16
274     %if regs_used > 4
275         push r4
276         push r5
277         %assign stack_offset stack_offset+16
278     %endif
279     %if xmm_regs_used > 6
280         sub rsp, (xmm_regs_used-6)*16+16
281         %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
282         %assign %%i xmm_regs_used
283         %rep (xmm_regs_used-6)
284             %assign %%i %%i-1
285             movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
286         %endrep
287     %endif
288     LOAD_IF_USED 4, %1
289     LOAD_IF_USED 5, %1
290     LOAD_IF_USED 6, %1
291     DEFINE_ARGS %4
292 %endmacro
293
294 %macro RESTORE_XMM_INTERNAL 1
295     %if xmm_regs_used > 6
296         %assign %%i xmm_regs_used
297         %rep (xmm_regs_used-6)
298             %assign %%i %%i-1
299             movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
300         %endrep
301         add %1, (xmm_regs_used-6)*16+16
302     %endif
303 %endmacro
304
305 %macro RESTORE_XMM 1
306     RESTORE_XMM_INTERNAL %1
307     %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
308     %assign xmm_regs_used 0
309 %endmacro
310
311 %macro RET 0
312     RESTORE_XMM_INTERNAL rsp
313     %if regs_used > 4
314         pop r5
315         pop r4
316     %endif
317     ret
318 %endmacro
319
320 %macro REP_RET 0
321     %if regs_used > 4 || xmm_regs_used > 6
322         RET
323     %else
324         rep ret
325     %endif
326 %endmacro
327
328 %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
329
330 DECLARE_REG 0, rdi, edi, di,  dil, edi
331 DECLARE_REG 1, rsi, esi, si,  sil, esi
332 DECLARE_REG 2, rdx, edx, dx,  dl,  edx
333 DECLARE_REG 3, rcx, ecx, cx,  cl,  ecx
334 DECLARE_REG 4, r8,  r8d, r8w, r8b, r8d
335 DECLARE_REG 5, r9,  r9d, r9w, r9b, r9d
336 DECLARE_REG 6, rax, eax, ax,  al,  [rsp + stack_offset + 8]
337 %define r7m [rsp + stack_offset + 16]
338 %define r8m [rsp + stack_offset + 24]
339
340 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
341     %if %1 < %2
342         mov r%1, [rsp - 40 + %1*8]
343     %endif
344 %endmacro
345
346 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
347     ASSERT %2 >= %1
348     ASSERT %2 <= 7
349     LOAD_IF_USED 6, %1
350     DEFINE_ARGS %4
351 %endmacro
352
353 %macro RET 0
354     ret
355 %endmacro
356
357 %macro REP_RET 0
358     rep ret
359 %endmacro
360
361 %else ; X86_32 ;==============================================================
362
363 DECLARE_REG 0, eax, eax, ax, al,   [esp + stack_offset + 4]
364 DECLARE_REG 1, ecx, ecx, cx, cl,   [esp + stack_offset + 8]
365 DECLARE_REG 2, edx, edx, dx, dl,   [esp + stack_offset + 12]
366 DECLARE_REG 3, ebx, ebx, bx, bl,   [esp + stack_offset + 16]
367 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
368 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
369 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
370 %define r7m [esp + stack_offset + 32]
371 %define r8m [esp + stack_offset + 36]
372 %define rsp esp
373
374 %macro PUSH_IF_USED 1 ; reg_id
375     %if %1 < regs_used
376         push r%1
377         %assign stack_offset stack_offset+4
378     %endif
379 %endmacro
380
381 %macro POP_IF_USED 1 ; reg_id
382     %if %1 < regs_used
383         pop r%1
384     %endif
385 %endmacro
386
387 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
388     %if %1 < %2
389         mov r%1, [esp + stack_offset + 4 + %1*4]
390     %endif
391 %endmacro
392
393 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
394     ASSERT %2 >= %1
395     %assign regs_used %2
396     ASSERT regs_used <= 7
397     PUSH_IF_USED 3
398     PUSH_IF_USED 4
399     PUSH_IF_USED 5
400     PUSH_IF_USED 6
401     LOAD_IF_USED 0, %1
402     LOAD_IF_USED 1, %1
403     LOAD_IF_USED 2, %1
404     LOAD_IF_USED 3, %1
405     LOAD_IF_USED 4, %1
406     LOAD_IF_USED 5, %1
407     LOAD_IF_USED 6, %1
408     DEFINE_ARGS %4
409 %endmacro
410
411 %macro RET 0
412     POP_IF_USED 6
413     POP_IF_USED 5
414     POP_IF_USED 4
415     POP_IF_USED 3
416     ret
417 %endmacro
418
419 %macro REP_RET 0
420     %if regs_used > 3
421         RET
422     %else
423         rep ret
424     %endif
425 %endmacro
426
427 %endif ;======================================================================
428
429
430
431 ;=============================================================================
432 ; arch-independent part
433 ;=============================================================================
434
435 %assign function_align 16
436
437 ; Symbol prefix for C linkage
438 %macro cglobal 1-2+
439     %xdefine %1 mangle(%1)
440     %xdefine %1.skip_prologue %1 %+ .skip_prologue
441     %ifidn __OUTPUT_FORMAT__,elf
442         global %1:function hidden
443     %else
444         global %1
445     %endif
446     align function_align
447     %1:
448     RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
449     %assign stack_offset 0
450     %if %0 > 1
451         PROLOGUE %2
452     %endif
453 %endmacro
454
455 %macro cextern 1
456     %xdefine %1 mangle(%1)
457     extern %1
458 %endmacro
459
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
464 %endif
465
466 ; merge mmx and sse*
467
468 %macro CAT_XDEFINE 3
469     %xdefine %1%2 %3
470 %endmacro
471
472 %macro CAT_UNDEF 2
473     %undef %1%2
474 %endmacro
475
476 %macro INIT_MMX 0
477     %define RESET_MM_PERMUTATION INIT_MMX
478     %define mmsize 8
479     %define num_mmregs 8
480     %define mova movq
481     %define movu movq
482     %define movh movd
483     %define movnt movntq
484     %assign %%i 0
485     %rep 8
486     CAT_XDEFINE m, %%i, mm %+ %%i
487     CAT_XDEFINE nmm, %%i, %%i
488     %assign %%i %%i+1
489     %endrep
490     %rep 8
491     CAT_UNDEF m, %%i
492     CAT_UNDEF nmm, %%i
493     %assign %%i %%i+1
494     %endrep
495 %endmacro
496
497 %macro INIT_XMM 0
498     %define RESET_MM_PERMUTATION INIT_XMM
499     %define mmsize 16
500     %define num_mmregs 8
501     %ifdef ARCH_X86_64
502     %define num_mmregs 16
503     %endif
504     %define mova movdqa
505     %define movu movdqu
506     %define movh movq
507     %define movnt movntdq
508     %assign %%i 0
509     %rep num_mmregs
510     CAT_XDEFINE m, %%i, xmm %+ %%i
511     CAT_XDEFINE nxmm, %%i, %%i
512     %assign %%i %%i+1
513     %endrep
514 %endmacro
515
516 INIT_MMX
517
518 ; I often want to use macros that permute their arguments. e.g. there's no
519 ; efficient way to implement butterfly or transpose or dct without swapping some
520 ; arguments.
521 ;
522 ; I would like to not have to manually keep track of the permutations:
523 ; If I insert a permutation in the middle of a function, it should automatically
524 ; change everything that follows. For more complex macros I may also have multiple
525 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
526 ;
527 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
528 ; permutes its arguments. It's equivalent to exchanging the contents of the
529 ; registers, except that this way you exchange the register names instead, so it
530 ; doesn't cost any cycles.
531
532 %macro PERMUTE 2-* ; takes a list of pairs to swap
533 %rep %0/2
534     %xdefine tmp%2 m%2
535     %xdefine ntmp%2 nm%2
536     %rotate 2
537 %endrep
538 %rep %0/2
539     %xdefine m%1 tmp%2
540     %xdefine nm%1 ntmp%2
541     %undef tmp%2
542     %undef ntmp%2
543     %rotate 2
544 %endrep
545 %endmacro
546
547 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
548 %rep %0-1
549 %ifdef m%1
550     %xdefine tmp m%1
551     %xdefine m%1 m%2
552     %xdefine m%2 tmp
553     CAT_XDEFINE n, m%1, %1
554     CAT_XDEFINE n, m%2, %2
555 %else
556     ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
557     ; Be careful using this mode in nested macros though, as in some cases there may be
558     ; other copies of m# that have already been dereferenced and don't get updated correctly.
559     %xdefine %%n1 n %+ %1
560     %xdefine %%n2 n %+ %2
561     %xdefine tmp m %+ %%n1
562     CAT_XDEFINE m, %%n1, m %+ %%n2
563     CAT_XDEFINE m, %%n2, tmp
564     CAT_XDEFINE n, m %+ %%n1, %%n1
565     CAT_XDEFINE n, m %+ %%n2, %%n2
566 %endif
567     %undef tmp
568     %rotate 1
569 %endrep
570 %endmacro
571
572 ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
573 ; function name, then any later calls to that function will automatically
574 ; load the permutation, so values can be returned in mmregs.
575 %macro SAVE_MM_PERMUTATION 1 ; name to save as
576     %assign %%i 0
577     %rep num_mmregs
578     CAT_XDEFINE %1_m, %%i, m %+ %%i
579     %assign %%i %%i+1
580     %endrep
581 %endmacro
582
583 %macro LOAD_MM_PERMUTATION 1 ; name to load from
584     %assign %%i 0
585     %rep num_mmregs
586     CAT_XDEFINE m, %%i, %1_m %+ %%i
587     CAT_XDEFINE n, m %+ %%i, %%i
588     %assign %%i %%i+1
589     %endrep
590 %endmacro
591
592 %macro call 1
593     call %1
594     %ifdef %1_m0
595         LOAD_MM_PERMUTATION %1
596     %endif
597 %endmacro
598
599 ; Substitutions that reduce instruction size but are functionally equivalent
600 %macro add 2
601     %ifnum %2
602         %if %2==128
603             sub %1, -128
604         %else
605             add %1, %2
606         %endif
607     %else
608         add %1, %2
609     %endif
610 %endmacro
611
612 %macro sub 2
613     %ifnum %2
614         %if %2==128
615             add %1, -128
616         %else
617             sub %1, %2
618         %endif
619     %else
620         sub %1, %2
621     %endif
622 %endmacro