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