]> git.sesse.net Git - ffmpeg/blob - libavutil/x86/x86inc.asm
d726d1bc2836765c4f30cd2aac27e1f508a0eb97
[ffmpeg] / libavutil / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm: x264asm abstraction layer
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2012 x264 project
5 ;*
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;*          Anton Mitrofanov <BugMaster@narod.ru>
8 ;*          Jason Garrett-Glaser <darkshikari@gmail.com>
9 ;*          Henrik Gramner <hengar-6@student.ltu.se>
10 ;*
11 ;* Permission to use, copy, modify, and/or distribute this software for any
12 ;* purpose with or without fee is hereby granted, provided that the above
13 ;* copyright notice and this permission notice appear in all copies.
14 ;*
15 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 ;*****************************************************************************
23
24 ; This is a header file for the x264ASM assembly language, which uses
25 ; NASM/YASM syntax combined with a large number of macros to provide easy
26 ; abstraction between different calling conventions (x86_32, win64, linux64).
27 ; It also has various other useful features to simplify writing the kind of
28 ; DSP functions that are most often used in x264.
29
30 ; Unlike the rest of x264, this file is available under an ISC license, as it
31 ; has significant usefulness outside of x264 and we want it to be available
32 ; to the largest audience possible.  Of course, if you modify it for your own
33 ; purposes to add a new feature, we strongly encourage contributing a patch
34 ; as this feature might be useful for others as well.  Send patches or ideas
35 ; to x264-devel@videolan.org .
36
37 %define program_name ff
38
39 %define WIN64  0
40 %define UNIX64 0
41 %if ARCH_X86_64
42     %ifidn __OUTPUT_FORMAT__,win32
43         %define WIN64  1
44     %elifidn __OUTPUT_FORMAT__,win64
45         %define WIN64  1
46     %else
47         %define UNIX64 1
48     %endif
49 %endif
50
51 %ifdef PREFIX
52     %define mangle(x) _ %+ x
53 %else
54     %define mangle(x) x
55 %endif
56
57 ; Name of the .rodata section.
58 %macro SECTION_RODATA 0-1 16
59     ; Kludge: Something on OS X fails to align .rodata even given an align
60     ; attribute, so use a different read-only section. This has been fixed in
61     ; yasm 0.8.0 and nasm 2.6.
62     %ifdef __YASM_VERSION_ID__
63         %if __YASM_VERSION_ID__ < 00080000h
64             %define NEED_MACHO_RODATA_KLUDGE
65         %endif
66     %elifdef __NASM_VERSION_ID__
67         %if __NASM_VERSION_ID__ < 02060000h
68             %define NEED_MACHO_RODATA_KLUDGE
69         %endif
70     %endif
71
72     %ifidn __OUTPUT_FORMAT__,aout
73         section .text
74     %else
75         %ifndef NEED_MACHO_RODATA_KLUDGE
76             SECTION .rodata align=%1
77         %else
78             %ifidn __OUTPUT_FORMAT__,macho64
79                 SECTION .text align=%1
80             %elifidn __OUTPUT_FORMAT__,macho
81                 SECTION .text align=%1
82                 fakegot:
83             %else
84                 SECTION .rodata align=%1
85             %endif
86         %endif
87     %endif
88
89     %undef NEED_MACHO_RODATA_KLUDGE
90 %endmacro
91
92 ; aout does not support align=
93 %macro SECTION_TEXT 0-1 16
94     %ifidn __OUTPUT_FORMAT__,aout
95         SECTION .text
96     %else
97         SECTION .text align=%1
98     %endif
99 %endmacro
100
101 %if WIN64
102     %define PIC
103 %elif ARCH_X86_64 == 0
104 ; x86_32 doesn't require PIC.
105 ; Some distros prefer shared objects to be PIC, but nothing breaks if
106 ; the code contains a few textrels, so we'll skip that complexity.
107     %undef PIC
108 %endif
109 %ifdef PIC
110     default rel
111 %endif
112
113 %macro CPUNOP 1
114     %if HAVE_CPUNOP
115         CPU %1
116     %endif
117 %endmacro
118
119 ; Always use long nops (reduces 0x90 spam in disassembly on x86_32)
120 CPUNOP amdnop
121
122 ; Macros to eliminate most code duplication between x86_32 and x86_64:
123 ; Currently this works only for leaf functions which load all their arguments
124 ; into registers at the start, and make no other use of the stack. Luckily that
125 ; covers most of x264's asm.
126
127 ; PROLOGUE:
128 ; %1 = number of arguments. loads them from stack if needed.
129 ; %2 = number of registers used. pushes callee-saved regs if needed.
130 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
131 ; %4 = list of names to define to registers
132 ; PROLOGUE can also be invoked by adding the same options to cglobal
133
134 ; e.g.
135 ; cglobal foo, 2,3,0, dst, src, tmp
136 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
137
138 ; TODO Some functions can use some args directly from the stack. If they're the
139 ; last args then you can just not declare them, but if they're in the middle
140 ; we need more flexible macro.
141
142 ; RET:
143 ; Pops anything that was pushed by PROLOGUE, and returns.
144
145 ; REP_RET:
146 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
147 ; which are slow when a normal ret follows a branch.
148
149 ; registers:
150 ; rN and rNq are the native-size register holding function argument N
151 ; rNd, rNw, rNb are dword, word, and byte size
152 ; rNh is the high 8 bits of the word size
153 ; rNm is the original location of arg N (a register or on the stack), dword
154 ; rNmp is native size
155
156 %macro DECLARE_REG 2-3
157     %define r%1q %2
158     %define r%1d %2d
159     %define r%1w %2w
160     %define r%1b %2b
161     %define r%1h %2h
162     %if %0 == 2
163         %define r%1m  %2d
164         %define r%1mp %2
165     %elif ARCH_X86_64 ; memory
166         %define r%1m [rsp + stack_offset + %3]
167         %define r%1mp qword r %+ %1 %+ m
168     %else
169         %define r%1m [esp + stack_offset + %3]
170         %define r%1mp dword r %+ %1 %+ m
171     %endif
172     %define r%1  %2
173 %endmacro
174
175 %macro DECLARE_REG_SIZE 3
176     %define r%1q r%1
177     %define e%1q r%1
178     %define r%1d e%1
179     %define e%1d e%1
180     %define r%1w %1
181     %define e%1w %1
182     %define r%1h %3
183     %define e%1h %3
184     %define r%1b %2
185     %define e%1b %2
186 %if ARCH_X86_64 == 0
187     %define r%1  e%1
188 %endif
189 %endmacro
190
191 DECLARE_REG_SIZE ax, al, ah
192 DECLARE_REG_SIZE bx, bl, bh
193 DECLARE_REG_SIZE cx, cl, ch
194 DECLARE_REG_SIZE dx, dl, dh
195 DECLARE_REG_SIZE si, sil, null
196 DECLARE_REG_SIZE di, dil, null
197 DECLARE_REG_SIZE bp, bpl, null
198
199 ; t# defines for when per-arch register allocation is more complex than just function arguments
200
201 %macro DECLARE_REG_TMP 1-*
202     %assign %%i 0
203     %rep %0
204         CAT_XDEFINE t, %%i, r%1
205         %assign %%i %%i+1
206         %rotate 1
207     %endrep
208 %endmacro
209
210 %macro DECLARE_REG_TMP_SIZE 0-*
211     %rep %0
212         %define t%1q t%1 %+ q
213         %define t%1d t%1 %+ d
214         %define t%1w t%1 %+ w
215         %define t%1h t%1 %+ h
216         %define t%1b t%1 %+ b
217         %rotate 1
218     %endrep
219 %endmacro
220
221 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
222
223 %if ARCH_X86_64
224     %define gprsize 8
225 %else
226     %define gprsize 4
227 %endif
228
229 %macro PUSH 1
230     push %1
231     %assign stack_offset stack_offset+gprsize
232 %endmacro
233
234 %macro POP 1
235     pop %1
236     %assign stack_offset stack_offset-gprsize
237 %endmacro
238
239 %macro PUSH_IF_USED 1-*
240     %rep %0
241         %if %1 < regs_used
242             PUSH r%1
243         %endif
244         %rotate 1
245     %endrep
246 %endmacro
247
248 %macro POP_IF_USED 1-*
249     %rep %0
250         %if %1 < regs_used
251             pop r%1
252         %endif
253         %rotate 1
254     %endrep
255 %endmacro
256
257 %macro LOAD_IF_USED 1-*
258     %rep %0
259         %if %1 < num_args
260             mov r%1, r %+ %1 %+ mp
261         %endif
262         %rotate 1
263     %endrep
264 %endmacro
265
266 %macro SUB 2
267     sub %1, %2
268     %ifidn %1, rsp
269         %assign stack_offset stack_offset+(%2)
270     %endif
271 %endmacro
272
273 %macro ADD 2
274     add %1, %2
275     %ifidn %1, rsp
276         %assign stack_offset stack_offset-(%2)
277     %endif
278 %endmacro
279
280 %macro movifnidn 2
281     %ifnidn %1, %2
282         mov %1, %2
283     %endif
284 %endmacro
285
286 %macro movsxdifnidn 2
287     %ifnidn %1, %2
288         movsxd %1, %2
289     %endif
290 %endmacro
291
292 %macro ASSERT 1
293     %if (%1) == 0
294         %error assert failed
295     %endif
296 %endmacro
297
298 %macro DEFINE_ARGS 0-*
299     %ifdef n_arg_names
300         %assign %%i 0
301         %rep n_arg_names
302             CAT_UNDEF arg_name %+ %%i, q
303             CAT_UNDEF arg_name %+ %%i, d
304             CAT_UNDEF arg_name %+ %%i, w
305             CAT_UNDEF arg_name %+ %%i, h
306             CAT_UNDEF arg_name %+ %%i, b
307             CAT_UNDEF arg_name %+ %%i, m
308             CAT_UNDEF arg_name %+ %%i, mp
309             CAT_UNDEF arg_name, %%i
310             %assign %%i %%i+1
311         %endrep
312     %endif
313
314     %xdefine %%stack_offset stack_offset
315     %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
316     %assign %%i 0
317     %rep %0
318         %xdefine %1q r %+ %%i %+ q
319         %xdefine %1d r %+ %%i %+ d
320         %xdefine %1w r %+ %%i %+ w
321         %xdefine %1h r %+ %%i %+ h
322         %xdefine %1b r %+ %%i %+ b
323         %xdefine %1m r %+ %%i %+ m
324         %xdefine %1mp r %+ %%i %+ mp
325         CAT_XDEFINE arg_name, %%i, %1
326         %assign %%i %%i+1
327         %rotate 1
328     %endrep
329     %xdefine stack_offset %%stack_offset
330     %assign n_arg_names %0
331 %endmacro
332
333 %if WIN64 ; Windows x64 ;=================================================
334
335 DECLARE_REG 0,  rcx
336 DECLARE_REG 1,  rdx
337 DECLARE_REG 2,  R8
338 DECLARE_REG 3,  R9
339 DECLARE_REG 4,  R10, 40
340 DECLARE_REG 5,  R11, 48
341 DECLARE_REG 6,  rax, 56
342 DECLARE_REG 7,  rdi, 64
343 DECLARE_REG 8,  rsi, 72
344 DECLARE_REG 9,  rbx, 80
345 DECLARE_REG 10, rbp, 88
346 DECLARE_REG 11, R12, 96
347 DECLARE_REG 12, R13, 104
348 DECLARE_REG 13, R14, 112
349 DECLARE_REG 14, R15, 120
350
351 %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
352     %assign num_args %1
353     %assign regs_used %2
354     ASSERT regs_used >= num_args
355     ASSERT regs_used <= 15
356     PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
357     %if mmsize == 8
358         %assign xmm_regs_used 0
359     %else
360         WIN64_SPILL_XMM %3
361     %endif
362     LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
363     DEFINE_ARGS %4
364 %endmacro
365
366 %macro WIN64_SPILL_XMM 1
367     %assign xmm_regs_used %1
368     ASSERT xmm_regs_used <= 16
369     %if xmm_regs_used > 6
370         SUB rsp, (xmm_regs_used-6)*16+16
371         %assign %%i xmm_regs_used
372         %rep (xmm_regs_used-6)
373             %assign %%i %%i-1
374             movdqa [rsp + (%%i-6)*16+(~stack_offset&8)], xmm %+ %%i
375         %endrep
376     %endif
377 %endmacro
378
379 %macro WIN64_RESTORE_XMM_INTERNAL 1
380     %if xmm_regs_used > 6
381         %assign %%i xmm_regs_used
382         %rep (xmm_regs_used-6)
383             %assign %%i %%i-1
384             movdqa xmm %+ %%i, [%1 + (%%i-6)*16+(~stack_offset&8)]
385         %endrep
386         add %1, (xmm_regs_used-6)*16+16
387     %endif
388 %endmacro
389
390 %macro WIN64_RESTORE_XMM 1
391     WIN64_RESTORE_XMM_INTERNAL %1
392     %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
393     %assign xmm_regs_used 0
394 %endmacro
395
396 %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32
397
398 %macro RET 0
399     WIN64_RESTORE_XMM_INTERNAL rsp
400     POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
401 %if mmsize == 32
402     vzeroupper
403 %endif
404     ret
405 %endmacro
406
407 %elif ARCH_X86_64 ; *nix x64 ;=============================================
408
409 DECLARE_REG 0,  rdi
410 DECLARE_REG 1,  rsi
411 DECLARE_REG 2,  rdx
412 DECLARE_REG 3,  rcx
413 DECLARE_REG 4,  R8
414 DECLARE_REG 5,  R9
415 DECLARE_REG 6,  rax, 8
416 DECLARE_REG 7,  R10, 16
417 DECLARE_REG 8,  R11, 24
418 DECLARE_REG 9,  rbx, 32
419 DECLARE_REG 10, rbp, 40
420 DECLARE_REG 11, R12, 48
421 DECLARE_REG 12, R13, 56
422 DECLARE_REG 13, R14, 64
423 DECLARE_REG 14, R15, 72
424
425 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
426     %assign num_args %1
427     %assign regs_used %2
428     ASSERT regs_used >= num_args
429     ASSERT regs_used <= 15
430     PUSH_IF_USED 9, 10, 11, 12, 13, 14
431     LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
432     DEFINE_ARGS %4
433 %endmacro
434
435 %define has_epilogue regs_used > 9 || mmsize == 32
436
437 %macro RET 0
438     POP_IF_USED 14, 13, 12, 11, 10, 9
439 %if mmsize == 32
440     vzeroupper
441 %endif
442     ret
443 %endmacro
444
445 %else ; X86_32 ;==============================================================
446
447 DECLARE_REG 0, eax, 4
448 DECLARE_REG 1, ecx, 8
449 DECLARE_REG 2, edx, 12
450 DECLARE_REG 3, ebx, 16
451 DECLARE_REG 4, esi, 20
452 DECLARE_REG 5, edi, 24
453 DECLARE_REG 6, ebp, 28
454 %define rsp esp
455
456 %macro DECLARE_ARG 1-*
457     %rep %0
458         %define r%1m [esp + stack_offset + 4*%1 + 4]
459         %define r%1mp dword r%1m
460         %rotate 1
461     %endrep
462 %endmacro
463
464 DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
465
466 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
467     %assign num_args %1
468     %assign regs_used %2
469     %if num_args > 7
470         %assign num_args 7
471     %endif
472     %if regs_used > 7
473         %assign regs_used 7
474     %endif
475     ASSERT regs_used >= num_args
476     PUSH_IF_USED 3, 4, 5, 6
477     LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
478     DEFINE_ARGS %4
479 %endmacro
480
481 %define has_epilogue regs_used > 3 || mmsize == 32
482
483 %macro RET 0
484     POP_IF_USED 6, 5, 4, 3
485 %if mmsize == 32
486     vzeroupper
487 %endif
488     ret
489 %endmacro
490
491 %endif ;======================================================================
492
493 %if WIN64 == 0
494 %macro WIN64_SPILL_XMM 1
495 %endmacro
496 %macro WIN64_RESTORE_XMM 1
497 %endmacro
498 %endif
499
500 %macro REP_RET 0
501     %if has_epilogue
502         RET
503     %else
504         rep ret
505     %endif
506 %endmacro
507
508 %macro TAIL_CALL 2 ; callee, is_nonadjacent
509     %if has_epilogue
510         call %1
511         RET
512     %elif %2
513         jmp %1
514     %endif
515 %endmacro
516
517 ;=============================================================================
518 ; arch-independent part
519 ;=============================================================================
520
521 %assign function_align 16
522
523 ; Begin a function.
524 ; Applies any symbol mangling needed for C linkage, and sets up a define such that
525 ; subsequent uses of the function name automatically refer to the mangled version.
526 ; Appends cpuflags to the function name if cpuflags has been specified.
527 %macro cglobal 1-2+ "" ; name, [PROLOGUE args]
528     cglobal_internal %1 %+ SUFFIX, %2
529 %endmacro
530 %macro cglobal_internal 1-2+
531     %ifndef cglobaled_%1
532         %xdefine %1 mangle(program_name %+ _ %+ %1)
533         %xdefine %1.skip_prologue %1 %+ .skip_prologue
534         CAT_XDEFINE cglobaled_, %1, 1
535     %endif
536     %xdefine current_function %1
537     %ifidn __OUTPUT_FORMAT__,elf
538         global %1:function hidden
539     %else
540         global %1
541     %endif
542     align function_align
543     %1:
544     RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
545     %assign stack_offset 0
546     %ifnidn %2, ""
547         PROLOGUE %2
548     %endif
549 %endmacro
550
551 %macro cextern 1
552     %xdefine %1 mangle(program_name %+ _ %+ %1)
553     CAT_XDEFINE cglobaled_, %1, 1
554     extern %1
555 %endmacro
556
557 ; like cextern, but without the prefix
558 %macro cextern_naked 1
559     %xdefine %1 mangle(%1)
560     CAT_XDEFINE cglobaled_, %1, 1
561     extern %1
562 %endmacro
563
564 %macro const 2+
565     %xdefine %1 mangle(program_name %+ _ %+ %1)
566     global %1
567     %1: %2
568 %endmacro
569
570 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
571 ; executable by default.
572 %ifidn __OUTPUT_FORMAT__,elf
573 SECTION .note.GNU-stack noalloc noexec nowrite progbits
574 %endif
575
576 ; cpuflags
577
578 %assign cpuflags_mmx      (1<<0)
579 %assign cpuflags_mmx2     (1<<1) | cpuflags_mmx
580 %assign cpuflags_3dnow    (1<<2) | cpuflags_mmx
581 %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
582 %assign cpuflags_sse      (1<<4) | cpuflags_mmx2
583 %assign cpuflags_sse2     (1<<5) | cpuflags_sse
584 %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
585 %assign cpuflags_sse3     (1<<7) | cpuflags_sse2
586 %assign cpuflags_ssse3    (1<<8) | cpuflags_sse3
587 %assign cpuflags_sse4     (1<<9) | cpuflags_ssse3
588 %assign cpuflags_sse42    (1<<10)| cpuflags_sse4
589 %assign cpuflags_avx      (1<<11)| cpuflags_sse42
590 %assign cpuflags_xop      (1<<12)| cpuflags_avx
591 %assign cpuflags_fma4     (1<<13)| cpuflags_avx
592 %assign cpuflags_avx2     (1<<14)| cpuflags_avx
593 %assign cpuflags_fma3     (1<<15)| cpuflags_avx
594
595 %assign cpuflags_cache32  (1<<16)
596 %assign cpuflags_cache64  (1<<17)
597 %assign cpuflags_slowctz  (1<<18)
598 %assign cpuflags_lzcnt    (1<<19)
599 %assign cpuflags_misalign (1<<20)
600 %assign cpuflags_aligned  (1<<21) ; not a cpu feature, but a function variant
601 %assign cpuflags_atom     (1<<22)
602 %assign cpuflags_bmi1     (1<<23)
603 %assign cpuflags_bmi2     (1<<24)|cpuflags_bmi1
604 %assign cpuflags_tbm      (1<<25)|cpuflags_bmi1
605
606 %define    cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
607 %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
608
609 ; Takes up to 2 cpuflags from the above list.
610 ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
611 ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
612 %macro INIT_CPUFLAGS 0-2
613     CPUNOP amdnop
614     %if %0 >= 1
615         %xdefine cpuname %1
616         %assign cpuflags cpuflags_%1
617         %if %0 >= 2
618             %xdefine cpuname %1_%2
619             %assign cpuflags cpuflags | cpuflags_%2
620         %endif
621         %xdefine SUFFIX _ %+ cpuname
622         %if cpuflag(avx)
623             %assign avx_enabled 1
624         %endif
625         %if mmsize == 16 && notcpuflag(sse2)
626             %define mova movaps
627             %define movu movups
628             %define movnta movntps
629         %endif
630         %if cpuflag(aligned)
631             %define movu mova
632         %elifidn %1, sse3
633             %define movu lddqu
634         %endif
635         %if notcpuflag(mmx2)
636             CPUNOP basicnop
637         %endif
638     %else
639         %xdefine SUFFIX
640         %undef cpuname
641         %undef cpuflags
642     %endif
643 %endmacro
644
645 ; merge mmx and sse*
646
647 %macro CAT_XDEFINE 3
648     %xdefine %1%2 %3
649 %endmacro
650
651 %macro CAT_UNDEF 2
652     %undef %1%2
653 %endmacro
654
655 %macro INIT_MMX 0-1+
656     %assign avx_enabled 0
657     %define RESET_MM_PERMUTATION INIT_MMX %1
658     %define mmsize 8
659     %define num_mmregs 8
660     %define mova movq
661     %define movu movq
662     %define movh movd
663     %define movnta movntq
664     %assign %%i 0
665     %rep 8
666     CAT_XDEFINE m, %%i, mm %+ %%i
667     CAT_XDEFINE nmm, %%i, %%i
668     %assign %%i %%i+1
669     %endrep
670     %rep 8
671     CAT_UNDEF m, %%i
672     CAT_UNDEF nmm, %%i
673     %assign %%i %%i+1
674     %endrep
675     INIT_CPUFLAGS %1
676 %endmacro
677
678 %macro INIT_XMM 0-1+
679     %assign avx_enabled 0
680     %define RESET_MM_PERMUTATION INIT_XMM %1
681     %define mmsize 16
682     %define num_mmregs 8
683     %if ARCH_X86_64
684     %define num_mmregs 16
685     %endif
686     %define mova movdqa
687     %define movu movdqu
688     %define movh movq
689     %define movnta movntdq
690     %assign %%i 0
691     %rep num_mmregs
692     CAT_XDEFINE m, %%i, xmm %+ %%i
693     CAT_XDEFINE nxmm, %%i, %%i
694     %assign %%i %%i+1
695     %endrep
696     INIT_CPUFLAGS %1
697 %endmacro
698
699 ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
700 %macro INIT_AVX 0
701     INIT_XMM
702     %assign avx_enabled 1
703     %define PALIGNR PALIGNR_SSSE3
704     %define RESET_MM_PERMUTATION INIT_AVX
705 %endmacro
706
707 %macro INIT_YMM 0-1+
708     %assign avx_enabled 1
709     %define RESET_MM_PERMUTATION INIT_YMM %1
710     %define mmsize 32
711     %define num_mmregs 8
712     %if ARCH_X86_64
713     %define num_mmregs 16
714     %endif
715     %define mova vmovaps
716     %define movu vmovups
717     %undef movh
718     %define movnta vmovntps
719     %assign %%i 0
720     %rep num_mmregs
721     CAT_XDEFINE m, %%i, ymm %+ %%i
722     CAT_XDEFINE nymm, %%i, %%i
723     %assign %%i %%i+1
724     %endrep
725     INIT_CPUFLAGS %1
726 %endmacro
727
728 INIT_XMM
729
730 ; I often want to use macros that permute their arguments. e.g. there's no
731 ; efficient way to implement butterfly or transpose or dct without swapping some
732 ; arguments.
733 ;
734 ; I would like to not have to manually keep track of the permutations:
735 ; If I insert a permutation in the middle of a function, it should automatically
736 ; change everything that follows. For more complex macros I may also have multiple
737 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
738 ;
739 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
740 ; permutes its arguments. It's equivalent to exchanging the contents of the
741 ; registers, except that this way you exchange the register names instead, so it
742 ; doesn't cost any cycles.
743
744 %macro PERMUTE 2-* ; takes a list of pairs to swap
745 %rep %0/2
746     %xdefine tmp%2 m%2
747     %xdefine ntmp%2 nm%2
748     %rotate 2
749 %endrep
750 %rep %0/2
751     %xdefine m%1 tmp%2
752     %xdefine nm%1 ntmp%2
753     %undef tmp%2
754     %undef ntmp%2
755     %rotate 2
756 %endrep
757 %endmacro
758
759 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
760 %rep %0-1
761 %ifdef m%1
762     %xdefine tmp m%1
763     %xdefine m%1 m%2
764     %xdefine m%2 tmp
765     CAT_XDEFINE n, m%1, %1
766     CAT_XDEFINE n, m%2, %2
767 %else
768     ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
769     ; Be careful using this mode in nested macros though, as in some cases there may be
770     ; other copies of m# that have already been dereferenced and don't get updated correctly.
771     %xdefine %%n1 n %+ %1
772     %xdefine %%n2 n %+ %2
773     %xdefine tmp m %+ %%n1
774     CAT_XDEFINE m, %%n1, m %+ %%n2
775     CAT_XDEFINE m, %%n2, tmp
776     CAT_XDEFINE n, m %+ %%n1, %%n1
777     CAT_XDEFINE n, m %+ %%n2, %%n2
778 %endif
779     %undef tmp
780     %rotate 1
781 %endrep
782 %endmacro
783
784 ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
785 ; calls to that function will automatically load the permutation, so values can
786 ; be returned in mmregs.
787 %macro SAVE_MM_PERMUTATION 0-1
788     %if %0
789         %xdefine %%f %1_m
790     %else
791         %xdefine %%f current_function %+ _m
792     %endif
793     %assign %%i 0
794     %rep num_mmregs
795         CAT_XDEFINE %%f, %%i, m %+ %%i
796     %assign %%i %%i+1
797     %endrep
798 %endmacro
799
800 %macro LOAD_MM_PERMUTATION 1 ; name to load from
801     %ifdef %1_m0
802         %assign %%i 0
803         %rep num_mmregs
804             CAT_XDEFINE m, %%i, %1_m %+ %%i
805             CAT_XDEFINE n, m %+ %%i, %%i
806         %assign %%i %%i+1
807         %endrep
808     %endif
809 %endmacro
810
811 ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
812 %macro call 1
813     call_internal %1 %+ SUFFIX, %1
814 %endmacro
815 %macro call_internal 2
816     %xdefine %%i %2
817     %ifndef cglobaled_%2
818         %ifdef cglobaled_%1
819             %xdefine %%i %1
820         %endif
821     %endif
822     call %%i
823     LOAD_MM_PERMUTATION %%i
824 %endmacro
825
826 ; Substitutions that reduce instruction size but are functionally equivalent
827 %macro add 2
828     %ifnum %2
829         %if %2==128
830             sub %1, -128
831         %else
832             add %1, %2
833         %endif
834     %else
835         add %1, %2
836     %endif
837 %endmacro
838
839 %macro sub 2
840     %ifnum %2
841         %if %2==128
842             add %1, -128
843         %else
844             sub %1, %2
845         %endif
846     %else
847         sub %1, %2
848     %endif
849 %endmacro
850
851 ;=============================================================================
852 ; AVX abstraction layer
853 ;=============================================================================
854
855 %assign i 0
856 %rep 16
857     %if i < 8
858         CAT_XDEFINE sizeofmm, i, 8
859     %endif
860     CAT_XDEFINE sizeofxmm, i, 16
861     CAT_XDEFINE sizeofymm, i, 32
862 %assign i i+1
863 %endrep
864 %undef i
865
866 %macro CHECK_AVX_INSTR_EMU 3-*
867     %xdefine %%opcode %1
868     %xdefine %%dst %2
869     %rep %0-2
870         %ifidn %%dst, %3
871             %error non-avx emulation of ``%%opcode'' is not supported
872         %endif
873         %rotate 1
874     %endrep
875 %endmacro
876
877 ;%1 == instruction
878 ;%2 == 1 if float, 0 if int
879 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
880 ;%4 == number of operands given
881 ;%5+: operands
882 %macro RUN_AVX_INSTR 6-7+
883     %ifid %6
884         %define %%sizeofreg sizeof%6
885     %elifid %5
886         %define %%sizeofreg sizeof%5
887     %else
888         %define %%sizeofreg mmsize
889     %endif
890     %if %%sizeofreg==32
891         %if %4>=3
892             v%1 %5, %6, %7
893         %else
894             v%1 %5, %6
895         %endif
896     %else
897         %if %%sizeofreg==8
898             %define %%regmov movq
899         %elif %2
900             %define %%regmov movaps
901         %else
902             %define %%regmov movdqa
903         %endif
904
905         %if %4>=3+%3
906             %ifnidn %5, %6
907                 %if avx_enabled && %%sizeofreg==16
908                     v%1 %5, %6, %7
909                 %else
910                     CHECK_AVX_INSTR_EMU {%1 %5, %6, %7}, %5, %7
911                     %%regmov %5, %6
912                     %1 %5, %7
913                 %endif
914             %else
915                 %1 %5, %7
916             %endif
917         %elif %4>=3
918             %1 %5, %6, %7
919         %else
920             %1 %5, %6
921         %endif
922     %endif
923 %endmacro
924
925 ; 3arg AVX ops with a memory arg can only have it in src2,
926 ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
927 ; So, if the op is symmetric and the wrong one is memory, swap them.
928 %macro RUN_AVX_INSTR1 8
929     %assign %%swap 0
930     %if avx_enabled
931         %ifnid %6
932             %assign %%swap 1
933         %endif
934     %elifnidn %5, %6
935         %ifnid %7
936             %assign %%swap 1
937         %endif
938     %endif
939     %if %%swap && %3 == 0 && %8 == 1
940         RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
941     %else
942         RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
943     %endif
944 %endmacro
945
946 ;%1 == instruction
947 ;%2 == 1 if float, 0 if int
948 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
949 ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
950 %macro AVX_INSTR 4
951     %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
952         %ifidn %3, fnord
953             RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
954         %elifidn %4, fnord
955             RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
956         %elifidn %5, fnord
957             RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
958         %else
959             RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
960         %endif
961     %endmacro
962 %endmacro
963
964 AVX_INSTR addpd, 1, 0, 1
965 AVX_INSTR addps, 1, 0, 1
966 AVX_INSTR addsd, 1, 0, 1
967 AVX_INSTR addss, 1, 0, 1
968 AVX_INSTR addsubpd, 1, 0, 0
969 AVX_INSTR addsubps, 1, 0, 0
970 AVX_INSTR andpd, 1, 0, 1
971 AVX_INSTR andps, 1, 0, 1
972 AVX_INSTR andnpd, 1, 0, 0
973 AVX_INSTR andnps, 1, 0, 0
974 AVX_INSTR blendpd, 1, 0, 0
975 AVX_INSTR blendps, 1, 0, 0
976 AVX_INSTR blendvpd, 1, 0, 0
977 AVX_INSTR blendvps, 1, 0, 0
978 AVX_INSTR cmppd, 1, 0, 0
979 AVX_INSTR cmpps, 1, 0, 0
980 AVX_INSTR cmpsd, 1, 0, 0
981 AVX_INSTR cmpss, 1, 0, 0
982 AVX_INSTR cvtdq2ps, 1, 0, 0
983 AVX_INSTR cvtps2dq, 1, 0, 0
984 AVX_INSTR divpd, 1, 0, 0
985 AVX_INSTR divps, 1, 0, 0
986 AVX_INSTR divsd, 1, 0, 0
987 AVX_INSTR divss, 1, 0, 0
988 AVX_INSTR dppd, 1, 1, 0
989 AVX_INSTR dpps, 1, 1, 0
990 AVX_INSTR haddpd, 1, 0, 0
991 AVX_INSTR haddps, 1, 0, 0
992 AVX_INSTR hsubpd, 1, 0, 0
993 AVX_INSTR hsubps, 1, 0, 0
994 AVX_INSTR maxpd, 1, 0, 1
995 AVX_INSTR maxps, 1, 0, 1
996 AVX_INSTR maxsd, 1, 0, 1
997 AVX_INSTR maxss, 1, 0, 1
998 AVX_INSTR minpd, 1, 0, 1
999 AVX_INSTR minps, 1, 0, 1
1000 AVX_INSTR minsd, 1, 0, 1
1001 AVX_INSTR minss, 1, 0, 1
1002 AVX_INSTR movhlps, 1, 0, 0
1003 AVX_INSTR movlhps, 1, 0, 0
1004 AVX_INSTR movsd, 1, 0, 0
1005 AVX_INSTR movss, 1, 0, 0
1006 AVX_INSTR mpsadbw, 0, 1, 0
1007 AVX_INSTR mulpd, 1, 0, 1
1008 AVX_INSTR mulps, 1, 0, 1
1009 AVX_INSTR mulsd, 1, 0, 1
1010 AVX_INSTR mulss, 1, 0, 1
1011 AVX_INSTR orpd, 1, 0, 1
1012 AVX_INSTR orps, 1, 0, 1
1013 AVX_INSTR pabsb, 0, 0, 0
1014 AVX_INSTR pabsw, 0, 0, 0
1015 AVX_INSTR pabsd, 0, 0, 0
1016 AVX_INSTR packsswb, 0, 0, 0
1017 AVX_INSTR packssdw, 0, 0, 0
1018 AVX_INSTR packuswb, 0, 0, 0
1019 AVX_INSTR packusdw, 0, 0, 0
1020 AVX_INSTR paddb, 0, 0, 1
1021 AVX_INSTR paddw, 0, 0, 1
1022 AVX_INSTR paddd, 0, 0, 1
1023 AVX_INSTR paddq, 0, 0, 1
1024 AVX_INSTR paddsb, 0, 0, 1
1025 AVX_INSTR paddsw, 0, 0, 1
1026 AVX_INSTR paddusb, 0, 0, 1
1027 AVX_INSTR paddusw, 0, 0, 1
1028 AVX_INSTR palignr, 0, 1, 0
1029 AVX_INSTR pand, 0, 0, 1
1030 AVX_INSTR pandn, 0, 0, 0
1031 AVX_INSTR pavgb, 0, 0, 1
1032 AVX_INSTR pavgw, 0, 0, 1
1033 AVX_INSTR pblendvb, 0, 0, 0
1034 AVX_INSTR pblendw, 0, 1, 0
1035 AVX_INSTR pcmpestri, 0, 0, 0
1036 AVX_INSTR pcmpestrm, 0, 0, 0
1037 AVX_INSTR pcmpistri, 0, 0, 0
1038 AVX_INSTR pcmpistrm, 0, 0, 0
1039 AVX_INSTR pcmpeqb, 0, 0, 1
1040 AVX_INSTR pcmpeqw, 0, 0, 1
1041 AVX_INSTR pcmpeqd, 0, 0, 1
1042 AVX_INSTR pcmpeqq, 0, 0, 1
1043 AVX_INSTR pcmpgtb, 0, 0, 0
1044 AVX_INSTR pcmpgtw, 0, 0, 0
1045 AVX_INSTR pcmpgtd, 0, 0, 0
1046 AVX_INSTR pcmpgtq, 0, 0, 0
1047 AVX_INSTR phaddw, 0, 0, 0
1048 AVX_INSTR phaddd, 0, 0, 0
1049 AVX_INSTR phaddsw, 0, 0, 0
1050 AVX_INSTR phsubw, 0, 0, 0
1051 AVX_INSTR phsubd, 0, 0, 0
1052 AVX_INSTR phsubsw, 0, 0, 0
1053 AVX_INSTR pmaddwd, 0, 0, 1
1054 AVX_INSTR pmaddubsw, 0, 0, 0
1055 AVX_INSTR pmaxsb, 0, 0, 1
1056 AVX_INSTR pmaxsw, 0, 0, 1
1057 AVX_INSTR pmaxsd, 0, 0, 1
1058 AVX_INSTR pmaxub, 0, 0, 1
1059 AVX_INSTR pmaxuw, 0, 0, 1
1060 AVX_INSTR pmaxud, 0, 0, 1
1061 AVX_INSTR pminsb, 0, 0, 1
1062 AVX_INSTR pminsw, 0, 0, 1
1063 AVX_INSTR pminsd, 0, 0, 1
1064 AVX_INSTR pminub, 0, 0, 1
1065 AVX_INSTR pminuw, 0, 0, 1
1066 AVX_INSTR pminud, 0, 0, 1
1067 AVX_INSTR pmovmskb, 0, 0, 0
1068 AVX_INSTR pmulhuw, 0, 0, 1
1069 AVX_INSTR pmulhrsw, 0, 0, 1
1070 AVX_INSTR pmulhw, 0, 0, 1
1071 AVX_INSTR pmullw, 0, 0, 1
1072 AVX_INSTR pmulld, 0, 0, 1
1073 AVX_INSTR pmuludq, 0, 0, 1
1074 AVX_INSTR pmuldq, 0, 0, 1
1075 AVX_INSTR por, 0, 0, 1
1076 AVX_INSTR psadbw, 0, 0, 1
1077 AVX_INSTR pshufb, 0, 0, 0
1078 AVX_INSTR pshufd, 0, 1, 0
1079 AVX_INSTR pshufhw, 0, 1, 0
1080 AVX_INSTR pshuflw, 0, 1, 0
1081 AVX_INSTR psignb, 0, 0, 0
1082 AVX_INSTR psignw, 0, 0, 0
1083 AVX_INSTR psignd, 0, 0, 0
1084 AVX_INSTR psllw, 0, 0, 0
1085 AVX_INSTR pslld, 0, 0, 0
1086 AVX_INSTR psllq, 0, 0, 0
1087 AVX_INSTR pslldq, 0, 0, 0
1088 AVX_INSTR psraw, 0, 0, 0
1089 AVX_INSTR psrad, 0, 0, 0
1090 AVX_INSTR psrlw, 0, 0, 0
1091 AVX_INSTR psrld, 0, 0, 0
1092 AVX_INSTR psrlq, 0, 0, 0
1093 AVX_INSTR psrldq, 0, 0, 0
1094 AVX_INSTR psubb, 0, 0, 0
1095 AVX_INSTR psubw, 0, 0, 0
1096 AVX_INSTR psubd, 0, 0, 0
1097 AVX_INSTR psubq, 0, 0, 0
1098 AVX_INSTR psubsb, 0, 0, 0
1099 AVX_INSTR psubsw, 0, 0, 0
1100 AVX_INSTR psubusb, 0, 0, 0
1101 AVX_INSTR psubusw, 0, 0, 0
1102 AVX_INSTR ptest, 0, 0, 0
1103 AVX_INSTR punpckhbw, 0, 0, 0
1104 AVX_INSTR punpckhwd, 0, 0, 0
1105 AVX_INSTR punpckhdq, 0, 0, 0
1106 AVX_INSTR punpckhqdq, 0, 0, 0
1107 AVX_INSTR punpcklbw, 0, 0, 0
1108 AVX_INSTR punpcklwd, 0, 0, 0
1109 AVX_INSTR punpckldq, 0, 0, 0
1110 AVX_INSTR punpcklqdq, 0, 0, 0
1111 AVX_INSTR pxor, 0, 0, 1
1112 AVX_INSTR shufps, 1, 1, 0
1113 AVX_INSTR subpd, 1, 0, 0
1114 AVX_INSTR subps, 1, 0, 0
1115 AVX_INSTR subsd, 1, 0, 0
1116 AVX_INSTR subss, 1, 0, 0
1117 AVX_INSTR unpckhpd, 1, 0, 0
1118 AVX_INSTR unpckhps, 1, 0, 0
1119 AVX_INSTR unpcklpd, 1, 0, 0
1120 AVX_INSTR unpcklps, 1, 0, 0
1121 AVX_INSTR xorpd, 1, 0, 1
1122 AVX_INSTR xorps, 1, 0, 1
1123
1124 ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
1125 AVX_INSTR pfadd, 1, 0, 1
1126 AVX_INSTR pfsub, 1, 0, 0
1127 AVX_INSTR pfmul, 1, 0, 1
1128
1129 ; base-4 constants for shuffles
1130 %assign i 0
1131 %rep 256
1132     %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
1133     %if j < 10
1134         CAT_XDEFINE q000, j, i
1135     %elif j < 100
1136         CAT_XDEFINE q00, j, i
1137     %elif j < 1000
1138         CAT_XDEFINE q0, j, i
1139     %else
1140         CAT_XDEFINE q, j, i
1141     %endif
1142 %assign i i+1
1143 %endrep
1144 %undef i
1145 %undef j
1146
1147 %macro FMA_INSTR 3
1148     %macro %1 5-8 %1, %2, %3
1149         %if cpuflag(xop) || cpuflag(fma4)
1150             v%6 %1, %2, %3, %4
1151         %else
1152             %ifidn %1, %4
1153                 %7 %5, %2, %3
1154                 %8 %1, %4, %5
1155             %else
1156                 %7 %1, %2, %3
1157                 %8 %1, %4
1158             %endif
1159         %endif
1160     %endmacro
1161 %endmacro
1162
1163 FMA_INSTR  fmaddps,   mulps, addps
1164 FMA_INSTR  pmacsdd,  pmulld, paddd
1165 FMA_INSTR  pmacsww,  pmullw, paddw
1166 FMA_INSTR pmadcswd, pmaddwd, paddd
1167
1168 ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
1169 ; This lets us use tzcnt without bumping the yasm version requirement yet.
1170 %define tzcnt rep bsf