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