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