]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/x86inc.asm
Update x86inc.asm from x264 to allow AVX emulation using SSE and MMX.
[ffmpeg] / libavcodec / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2011 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 ;*
10 ;* Permission to use, copy, modify, and/or distribute this software for any
11 ;* purpose with or without fee is hereby granted, provided that the above
12 ;* copyright notice and this permission notice appear in all copies.
13 ;*
14 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ;*****************************************************************************
22
23 ; This is a header file for the x264ASM assembly language, which uses
24 ; NASM/YASM syntax combined with a large number of macros to provide easy
25 ; abstraction between different calling conventions (x86_32, win64, linux64).
26 ; It also has various other useful features to simplify writing the kind of
27 ; DSP functions that are most often used in x264.
28
29 ; Unlike the rest of x264, this file is available under an ISC license, as it
30 ; has significant usefulness outside of x264 and we want it to be available
31 ; to the largest audience possible.  Of course, if you modify it for your own
32 ; purposes to add a new feature, we strongly encourage contributing a patch
33 ; as this feature might be useful for others as well.  Send patches or ideas
34 ; to x264-devel@videolan.org .
35
36 %define program_name ff
37
38 %ifdef ARCH_X86_64
39     %ifidn __OUTPUT_FORMAT__,win32
40         %define WIN64
41     %else
42         %define UNIX64
43     %endif
44 %endif
45
46 %ifdef PREFIX
47     %define mangle(x) _ %+ x
48 %else
49     %define mangle(x) x
50 %endif
51
52 ; FIXME: All of the 64bit asm functions that take a stride as an argument
53 ; via register, assume that the high dword of that register is filled with 0.
54 ; This is true in practice (since we never do any 64bit arithmetic on strides,
55 ; and x264's strides are all positive), but is not guaranteed by the ABI.
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     %else
67         SECTION .rodata align=%1
68     %endif
69 %endmacro
70
71 %ifdef WIN64
72     %define PIC
73 %elifndef ARCH_X86_64
74 ; x86_32 doesn't require PIC.
75 ; Some distros prefer shared objects to be PIC, but nothing breaks if
76 ; the code contains a few textrels, so we'll skip that complexity.
77     %undef PIC
78 %endif
79 %ifdef PIC
80     default rel
81 %endif
82
83 ; Macros to eliminate most code duplication between x86_32 and x86_64:
84 ; Currently this works only for leaf functions which load all their arguments
85 ; into registers at the start, and make no other use of the stack. Luckily that
86 ; covers most of x264's asm.
87
88 ; PROLOGUE:
89 ; %1 = number of arguments. loads them from stack if needed.
90 ; %2 = number of registers used. pushes callee-saved regs if needed.
91 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
92 ; %4 = list of names to define to registers
93 ; PROLOGUE can also be invoked by adding the same options to cglobal
94
95 ; e.g.
96 ; cglobal foo, 2,3,0, dst, src, tmp
97 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
98
99 ; TODO Some functions can use some args directly from the stack. If they're the
100 ; last args then you can just not declare them, but if they're in the middle
101 ; we need more flexible macro.
102
103 ; RET:
104 ; Pops anything that was pushed by PROLOGUE
105
106 ; REP_RET:
107 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
108 ; which are slow when a normal ret follows a branch.
109
110 ; registers:
111 ; rN and rNq are the native-size register holding function argument N
112 ; rNd, rNw, rNb are dword, word, and byte size
113 ; rNm is the original location of arg N (a register or on the stack), dword
114 ; rNmp is native size
115
116 %macro DECLARE_REG 6
117     %define r%1q %2
118     %define r%1d %3
119     %define r%1w %4
120     %define r%1b %5
121     %define r%1m %6
122     %ifid %6 ; i.e. it's a register
123         %define r%1mp %2
124     %elifdef ARCH_X86_64 ; memory
125         %define r%1mp qword %6
126     %else
127         %define r%1mp dword %6
128     %endif
129     %define r%1  %2
130 %endmacro
131
132 %macro DECLARE_REG_SIZE 2
133     %define r%1q r%1
134     %define e%1q r%1
135     %define r%1d e%1
136     %define e%1d e%1
137     %define r%1w %1
138     %define e%1w %1
139     %define r%1b %2
140     %define e%1b %2
141 %ifndef ARCH_X86_64
142     %define r%1  e%1
143 %endif
144 %endmacro
145
146 DECLARE_REG_SIZE ax, al
147 DECLARE_REG_SIZE bx, bl
148 DECLARE_REG_SIZE cx, cl
149 DECLARE_REG_SIZE dx, dl
150 DECLARE_REG_SIZE si, sil
151 DECLARE_REG_SIZE di, dil
152 DECLARE_REG_SIZE bp, bpl
153
154 ; t# defines for when per-arch register allocation is more complex than just function arguments
155
156 %macro DECLARE_REG_TMP 1-*
157     %assign %%i 0
158     %rep %0
159         CAT_XDEFINE t, %%i, r%1
160         %assign %%i %%i+1
161         %rotate 1
162     %endrep
163 %endmacro
164
165 %macro DECLARE_REG_TMP_SIZE 0-*
166     %rep %0
167         %define t%1q t%1 %+ q
168         %define t%1d t%1 %+ d
169         %define t%1w t%1 %+ w
170         %define t%1b t%1 %+ b
171         %rotate 1
172     %endrep
173 %endmacro
174
175 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9
176
177 %ifdef ARCH_X86_64
178     %define gprsize 8
179 %else
180     %define gprsize 4
181 %endif
182
183 %macro PUSH 1
184     push %1
185     %assign stack_offset stack_offset+gprsize
186 %endmacro
187
188 %macro POP 1
189     pop %1
190     %assign stack_offset stack_offset-gprsize
191 %endmacro
192
193 %macro SUB 2
194     sub %1, %2
195     %ifidn %1, rsp
196         %assign stack_offset stack_offset+(%2)
197     %endif
198 %endmacro
199
200 %macro ADD 2
201     add %1, %2
202     %ifidn %1, rsp
203         %assign stack_offset stack_offset-(%2)
204     %endif
205 %endmacro
206
207 %macro movifnidn 2
208     %ifnidn %1, %2
209         mov %1, %2
210     %endif
211 %endmacro
212
213 %macro movsxdifnidn 2
214     %ifnidn %1, %2
215         movsxd %1, %2
216     %endif
217 %endmacro
218
219 %macro ASSERT 1
220     %if (%1) == 0
221         %error assert failed
222     %endif
223 %endmacro
224
225 %macro DEFINE_ARGS 0-*
226     %ifdef n_arg_names
227         %assign %%i 0
228         %rep n_arg_names
229             CAT_UNDEF arg_name %+ %%i, q
230             CAT_UNDEF arg_name %+ %%i, d
231             CAT_UNDEF arg_name %+ %%i, w
232             CAT_UNDEF arg_name %+ %%i, b
233             CAT_UNDEF arg_name %+ %%i, m
234             CAT_UNDEF arg_name, %%i
235             %assign %%i %%i+1
236         %endrep
237     %endif
238
239     %assign %%i 0
240     %rep %0
241         %xdefine %1q r %+ %%i %+ q
242         %xdefine %1d r %+ %%i %+ d
243         %xdefine %1w r %+ %%i %+ w
244         %xdefine %1b r %+ %%i %+ b
245         %xdefine %1m r %+ %%i %+ m
246         CAT_XDEFINE arg_name, %%i, %1
247         %assign %%i %%i+1
248         %rotate 1
249     %endrep
250     %assign n_arg_names %%i
251 %endmacro
252
253 %ifdef WIN64 ; Windows x64 ;=================================================
254
255 DECLARE_REG 0, rcx, ecx, cx,  cl,  ecx
256 DECLARE_REG 1, rdx, edx, dx,  dl,  edx
257 DECLARE_REG 2, r8,  r8d, r8w, r8b, r8d
258 DECLARE_REG 3, r9,  r9d, r9w, r9b, r9d
259 DECLARE_REG 4, rdi, edi, di,  dil, [rsp + stack_offset + 40]
260 DECLARE_REG 5, rsi, esi, si,  sil, [rsp + stack_offset + 48]
261 DECLARE_REG 6, rax, eax, ax,  al,  [rsp + stack_offset + 56]
262 %define r7m [rsp + stack_offset + 64]
263 %define r8m [rsp + stack_offset + 72]
264
265 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
266     %if %1 < %2
267         mov r%1, [rsp + stack_offset + 8 + %1*8]
268     %endif
269 %endmacro
270
271 %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
272     ASSERT %2 >= %1
273     %assign regs_used %2
274     ASSERT regs_used <= 7
275     %if regs_used > 4
276         push r4
277         push r5
278         %assign stack_offset stack_offset+16
279     %endif
280     WIN64_SPILL_XMM %3
281     LOAD_IF_USED 4, %1
282     LOAD_IF_USED 5, %1
283     LOAD_IF_USED 6, %1
284     DEFINE_ARGS %4
285 %endmacro
286
287 %macro WIN64_SPILL_XMM 1
288     %assign xmm_regs_used %1
289     ASSERT xmm_regs_used <= 16
290     %if xmm_regs_used > 6
291         sub rsp, (xmm_regs_used-6)*16+16
292         %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
293         %assign %%i xmm_regs_used
294         %rep (xmm_regs_used-6)
295             %assign %%i %%i-1
296             movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
297         %endrep
298     %endif
299 %endmacro
300
301 %macro WIN64_RESTORE_XMM_INTERNAL 1
302     %if xmm_regs_used > 6
303         %assign %%i xmm_regs_used
304         %rep (xmm_regs_used-6)
305             %assign %%i %%i-1
306             movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
307         %endrep
308         add %1, (xmm_regs_used-6)*16+16
309     %endif
310 %endmacro
311
312 %macro WIN64_RESTORE_XMM 1
313     WIN64_RESTORE_XMM_INTERNAL %1
314     %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
315     %assign xmm_regs_used 0
316 %endmacro
317
318 %macro RET 0
319     WIN64_RESTORE_XMM_INTERNAL rsp
320     %if regs_used > 4
321         pop r5
322         pop r4
323     %endif
324     ret
325 %endmacro
326
327 %macro REP_RET 0
328     %if regs_used > 4 || xmm_regs_used > 6
329         RET
330     %else
331         rep ret
332     %endif
333 %endmacro
334
335 %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
336
337 DECLARE_REG 0, rdi, edi, di,  dil, edi
338 DECLARE_REG 1, rsi, esi, si,  sil, esi
339 DECLARE_REG 2, rdx, edx, dx,  dl,  edx
340 DECLARE_REG 3, rcx, ecx, cx,  cl,  ecx
341 DECLARE_REG 4, r8,  r8d, r8w, r8b, r8d
342 DECLARE_REG 5, r9,  r9d, r9w, r9b, r9d
343 DECLARE_REG 6, rax, eax, ax,  al,  [rsp + stack_offset + 8]
344 %define r7m [rsp + stack_offset + 16]
345 %define r8m [rsp + stack_offset + 24]
346
347 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
348     %if %1 < %2
349         mov r%1, [rsp - 40 + %1*8]
350     %endif
351 %endmacro
352
353 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
354     ASSERT %2 >= %1
355     ASSERT %2 <= 7
356     LOAD_IF_USED 6, %1
357     DEFINE_ARGS %4
358 %endmacro
359
360 %macro RET 0
361     ret
362 %endmacro
363
364 %macro REP_RET 0
365     rep ret
366 %endmacro
367
368 %else ; X86_32 ;==============================================================
369
370 DECLARE_REG 0, eax, eax, ax, al,   [esp + stack_offset + 4]
371 DECLARE_REG 1, ecx, ecx, cx, cl,   [esp + stack_offset + 8]
372 DECLARE_REG 2, edx, edx, dx, dl,   [esp + stack_offset + 12]
373 DECLARE_REG 3, ebx, ebx, bx, bl,   [esp + stack_offset + 16]
374 DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
375 DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
376 DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
377 %define r7m [esp + stack_offset + 32]
378 %define r8m [esp + stack_offset + 36]
379 %define rsp esp
380
381 %macro PUSH_IF_USED 1 ; reg_id
382     %if %1 < regs_used
383         push r%1
384         %assign stack_offset stack_offset+4
385     %endif
386 %endmacro
387
388 %macro POP_IF_USED 1 ; reg_id
389     %if %1 < regs_used
390         pop r%1
391     %endif
392 %endmacro
393
394 %macro LOAD_IF_USED 2 ; reg_id, number_of_args
395     %if %1 < %2
396         mov r%1, [esp + stack_offset + 4 + %1*4]
397     %endif
398 %endmacro
399
400 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
401     ASSERT %2 >= %1
402     %assign regs_used %2
403     ASSERT regs_used <= 7
404     PUSH_IF_USED 3
405     PUSH_IF_USED 4
406     PUSH_IF_USED 5
407     PUSH_IF_USED 6
408     LOAD_IF_USED 0, %1
409     LOAD_IF_USED 1, %1
410     LOAD_IF_USED 2, %1
411     LOAD_IF_USED 3, %1
412     LOAD_IF_USED 4, %1
413     LOAD_IF_USED 5, %1
414     LOAD_IF_USED 6, %1
415     DEFINE_ARGS %4
416 %endmacro
417
418 %macro RET 0
419     POP_IF_USED 6
420     POP_IF_USED 5
421     POP_IF_USED 4
422     POP_IF_USED 3
423     ret
424 %endmacro
425
426 %macro REP_RET 0
427     %if regs_used > 3
428         RET
429     %else
430         rep ret
431     %endif
432 %endmacro
433
434 %endif ;======================================================================
435
436 %ifndef WIN64
437 %macro WIN64_SPILL_XMM 1
438 %endmacro
439 %macro WIN64_RESTORE_XMM 1
440 %endmacro
441 %endif
442
443
444
445 ;=============================================================================
446 ; arch-independent part
447 ;=============================================================================
448
449 %assign function_align 16
450
451 ; Symbol prefix for C linkage
452 %macro cglobal 1-2+
453     %xdefine %1 mangle(program_name %+ _ %+ %1)
454     %xdefine %1.skip_prologue %1 %+ .skip_prologue
455     %ifidn __OUTPUT_FORMAT__,elf
456         global %1:function hidden
457     %else
458         global %1
459     %endif
460     align function_align
461     %1:
462     RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
463     %assign stack_offset 0
464     %if %0 > 1
465         PROLOGUE %2
466     %endif
467 %endmacro
468
469 %macro cextern 1
470     %xdefine %1 mangle(program_name %+ _ %+ %1)
471     extern %1
472 %endmacro
473
474 ;like cextern, but without the prefix
475 %macro cextern_naked 1
476     %xdefine %1 mangle(%1)
477     extern %1
478 %endmacro
479
480 %macro const 2+
481     %xdefine %1 mangle(program_name %+ _ %+ %1)
482     global %1
483     %1: %2
484 %endmacro
485
486 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
487 ; executable by default.
488 %ifidn __OUTPUT_FORMAT__,elf
489 SECTION .note.GNU-stack noalloc noexec nowrite progbits
490 %endif
491
492 ; merge mmx and sse*
493
494 %macro CAT_XDEFINE 3
495     %xdefine %1%2 %3
496 %endmacro
497
498 %macro CAT_UNDEF 2
499     %undef %1%2
500 %endmacro
501
502 %macro INIT_MMX 0
503     %assign avx_enabled 0
504     %define RESET_MM_PERMUTATION INIT_MMX
505     %define mmsize 8
506     %define num_mmregs 8
507     %define mova movq
508     %define movu movq
509     %define movh movd
510     %define movnta movntq
511     %assign %%i 0
512     %rep 8
513     CAT_XDEFINE m, %%i, mm %+ %%i
514     CAT_XDEFINE nmm, %%i, %%i
515     %assign %%i %%i+1
516     %endrep
517     %rep 8
518     CAT_UNDEF m, %%i
519     CAT_UNDEF nmm, %%i
520     %assign %%i %%i+1
521     %endrep
522 %endmacro
523
524 %macro INIT_XMM 0
525     %assign avx_enabled 0
526     %define RESET_MM_PERMUTATION INIT_XMM
527     %define mmsize 16
528     %define num_mmregs 8
529     %ifdef ARCH_X86_64
530     %define num_mmregs 16
531     %endif
532     %define mova movdqa
533     %define movu movdqu
534     %define movh movq
535     %define movnta movntdq
536     %assign %%i 0
537     %rep num_mmregs
538     CAT_XDEFINE m, %%i, xmm %+ %%i
539     CAT_XDEFINE nxmm, %%i, %%i
540     %assign %%i %%i+1
541     %endrep
542 %endmacro
543
544 %macro INIT_AVX 0
545     INIT_XMM
546     %assign avx_enabled 1
547     %define PALIGNR PALIGNR_SSSE3
548     %define RESET_MM_PERMUTATION INIT_AVX
549 %endmacro
550
551 %macro INIT_YMM 0
552     %assign avx_enabled 1
553     %define RESET_MM_PERMUTATION INIT_YMM
554     %define mmsize 32
555     %define num_mmregs 8
556     %ifdef ARCH_X86_64
557     %define num_mmregs 16
558     %endif
559     %define mova vmovaps
560     %define movu vmovups
561     %assign %%i 0
562     %rep num_mmregs
563     CAT_XDEFINE m, %%i, ymm %+ %%i
564     CAT_XDEFINE nymm, %%i, %%i
565     %assign %%i %%i+1
566     %endrep
567 %endmacro
568
569 INIT_MMX
570
571 ; I often want to use macros that permute their arguments. e.g. there's no
572 ; efficient way to implement butterfly or transpose or dct without swapping some
573 ; arguments.
574 ;
575 ; I would like to not have to manually keep track of the permutations:
576 ; If I insert a permutation in the middle of a function, it should automatically
577 ; change everything that follows. For more complex macros I may also have multiple
578 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
579 ;
580 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
581 ; permutes its arguments. It's equivalent to exchanging the contents of the
582 ; registers, except that this way you exchange the register names instead, so it
583 ; doesn't cost any cycles.
584
585 %macro PERMUTE 2-* ; takes a list of pairs to swap
586 %rep %0/2
587     %xdefine tmp%2 m%2
588     %xdefine ntmp%2 nm%2
589     %rotate 2
590 %endrep
591 %rep %0/2
592     %xdefine m%1 tmp%2
593     %xdefine nm%1 ntmp%2
594     %undef tmp%2
595     %undef ntmp%2
596     %rotate 2
597 %endrep
598 %endmacro
599
600 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
601 %rep %0-1
602 %ifdef m%1
603     %xdefine tmp m%1
604     %xdefine m%1 m%2
605     %xdefine m%2 tmp
606     CAT_XDEFINE n, m%1, %1
607     CAT_XDEFINE n, m%2, %2
608 %else
609     ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
610     ; Be careful using this mode in nested macros though, as in some cases there may be
611     ; other copies of m# that have already been dereferenced and don't get updated correctly.
612     %xdefine %%n1 n %+ %1
613     %xdefine %%n2 n %+ %2
614     %xdefine tmp m %+ %%n1
615     CAT_XDEFINE m, %%n1, m %+ %%n2
616     CAT_XDEFINE m, %%n2, tmp
617     CAT_XDEFINE n, m %+ %%n1, %%n1
618     CAT_XDEFINE n, m %+ %%n2, %%n2
619 %endif
620     %undef tmp
621     %rotate 1
622 %endrep
623 %endmacro
624
625 ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
626 ; function name, then any later calls to that function will automatically
627 ; load the permutation, so values can be returned in mmregs.
628 %macro SAVE_MM_PERMUTATION 1 ; name to save as
629     %assign %%i 0
630     %rep num_mmregs
631     CAT_XDEFINE %1_m, %%i, m %+ %%i
632     %assign %%i %%i+1
633     %endrep
634 %endmacro
635
636 %macro LOAD_MM_PERMUTATION 1 ; name to load from
637     %assign %%i 0
638     %rep num_mmregs
639     CAT_XDEFINE m, %%i, %1_m %+ %%i
640     CAT_XDEFINE n, m %+ %%i, %%i
641     %assign %%i %%i+1
642     %endrep
643 %endmacro
644
645 %macro call 1
646     call %1
647     %ifdef %1_m0
648         LOAD_MM_PERMUTATION %1
649     %endif
650 %endmacro
651
652 ; Substitutions that reduce instruction size but are functionally equivalent
653 %macro add 2
654     %ifnum %2
655         %if %2==128
656             sub %1, -128
657         %else
658             add %1, %2
659         %endif
660     %else
661         add %1, %2
662     %endif
663 %endmacro
664
665 %macro sub 2
666     %ifnum %2
667         %if %2==128
668             add %1, -128
669         %else
670             sub %1, %2
671         %endif
672     %else
673         sub %1, %2
674     %endif
675 %endmacro
676
677 ;=============================================================================
678 ; AVX abstraction layer
679 ;=============================================================================
680
681 %assign i 0
682 %rep 16
683     %if i < 8
684         CAT_XDEFINE sizeofmm, i, 8
685     %endif
686     CAT_XDEFINE sizeofxmm, i, 16
687     CAT_XDEFINE sizeofymm, i, 32
688 %assign i i+1
689 %endrep
690 %undef i
691
692 ;%1 == instruction
693 ;%2 == 1 if float, 0 if int
694 ;%3 == 0 if 3-operand (xmm, xmm, xmm), 1 if 4-operand (xmm, xmm, xmm, imm)
695 ;%4 == number of operands given
696 ;%5+: operands
697 %macro RUN_AVX_INSTR 6-7+
698     %if sizeof%5==32
699         v%1 %5, %6, %7
700     %else
701         %if sizeof%5==8
702             %define %%regmov movq
703         %elif %2
704             %define %%regmov movaps
705         %else
706             %define %%regmov movdqa
707         %endif
708
709         %if %4>=3+%3
710             %ifnidn %5, %6
711                 %if avx_enabled && sizeof%5==16
712                     v%1 %5, %6, %7
713                 %else
714                     %%regmov %5, %6
715                     %1 %5, %7
716                 %endif
717             %else
718                 %1 %5, %7
719             %endif
720         %elif %3
721             %1 %5, %6, %7
722         %else
723             %1 %5, %6
724         %endif
725     %endif
726 %endmacro
727
728 ;%1 == instruction
729 ;%2 == 1 if float, 0 if int
730 ;%3 == 0 if 3-operand (xmm, xmm, xmm), 1 if 4-operand (xmm, xmm, xmm, imm)
731 %macro AVX_INSTR 3
732     %macro %1 2-8 fnord, fnord, fnord, %1, %2, %3
733         %ifidn %3, fnord
734             RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
735         %elifidn %4, fnord
736             RUN_AVX_INSTR %6, %7, %8, 3, %1, %2, %3
737         %elifidn %5, fnord
738             RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
739         %else
740             RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
741         %endif
742     %endmacro
743 %endmacro
744
745 AVX_INSTR addpd, 1, 0
746 AVX_INSTR addps, 1, 0
747 AVX_INSTR addsd, 1, 0
748 AVX_INSTR addss, 1, 0
749 AVX_INSTR addsubpd, 1, 0
750 AVX_INSTR addsubps, 1, 0
751 AVX_INSTR andpd, 1, 0
752 AVX_INSTR andps, 1, 0
753 AVX_INSTR andnpd, 1, 0
754 AVX_INSTR andnps, 1, 0
755 AVX_INSTR blendpd, 1, 0
756 AVX_INSTR blendps, 1, 0
757 AVX_INSTR blendvpd, 1, 0
758 AVX_INSTR blendvps, 1, 0
759 AVX_INSTR cmppd, 1, 0
760 AVX_INSTR cmpps, 1, 0
761 AVX_INSTR cmpsd, 1, 0
762 AVX_INSTR cmpss, 1, 0
763 AVX_INSTR divpd, 1, 0
764 AVX_INSTR divps, 1, 0
765 AVX_INSTR divsd, 1, 0
766 AVX_INSTR divss, 1, 0
767 AVX_INSTR dppd, 1, 0
768 AVX_INSTR dpps, 1, 0
769 AVX_INSTR haddpd, 1, 0
770 AVX_INSTR haddps, 1, 0
771 AVX_INSTR hsubpd, 1, 0
772 AVX_INSTR hsubps, 1, 0
773 AVX_INSTR maxpd, 1, 0
774 AVX_INSTR maxps, 1, 0
775 AVX_INSTR maxsd, 1, 0
776 AVX_INSTR maxss, 1, 0
777 AVX_INSTR minpd, 1, 0
778 AVX_INSTR minps, 1, 0
779 AVX_INSTR minsd, 1, 0
780 AVX_INSTR minss, 1, 0
781 AVX_INSTR mpsadbw, 0, 1
782 AVX_INSTR mulpd, 1, 0
783 AVX_INSTR mulps, 1, 0
784 AVX_INSTR mulsd, 1, 0
785 AVX_INSTR mulss, 1, 0
786 AVX_INSTR orpd, 1, 0
787 AVX_INSTR orps, 1, 0
788 AVX_INSTR packsswb, 0, 0
789 AVX_INSTR packssdw, 0, 0
790 AVX_INSTR packuswb, 0, 0
791 AVX_INSTR packusdw, 0, 0
792 AVX_INSTR paddb, 0, 0
793 AVX_INSTR paddw, 0, 0
794 AVX_INSTR paddd, 0, 0
795 AVX_INSTR paddq, 0, 0
796 AVX_INSTR paddsb, 0, 0
797 AVX_INSTR paddsw, 0, 0
798 AVX_INSTR paddusb, 0, 0
799 AVX_INSTR paddusw, 0, 0
800 AVX_INSTR palignr, 0, 1
801 AVX_INSTR pand, 0, 0
802 AVX_INSTR pandn, 0, 0
803 AVX_INSTR pavgb, 0, 0
804 AVX_INSTR pavgw, 0, 0
805 AVX_INSTR pblendvb, 0, 0
806 AVX_INSTR pblendw, 0, 1
807 AVX_INSTR pcmpestri, 0, 0
808 AVX_INSTR pcmpestrm, 0, 0
809 AVX_INSTR pcmpistri, 0, 0
810 AVX_INSTR pcmpistrm, 0, 0
811 AVX_INSTR pcmpeqb, 0, 0
812 AVX_INSTR pcmpeqw, 0, 0
813 AVX_INSTR pcmpeqd, 0, 0
814 AVX_INSTR pcmpeqq, 0, 0
815 AVX_INSTR pcmpgtb, 0, 0
816 AVX_INSTR pcmpgtw, 0, 0
817 AVX_INSTR pcmpgtd, 0, 0
818 AVX_INSTR pcmpgtq, 0, 0
819 AVX_INSTR phaddw, 0, 0
820 AVX_INSTR phaddd, 0, 0
821 AVX_INSTR phaddsw, 0, 0
822 AVX_INSTR phsubw, 0, 0
823 AVX_INSTR phsubd, 0, 0
824 AVX_INSTR phsubsw, 0, 0
825 AVX_INSTR pmaddwd, 0, 0
826 AVX_INSTR pmaddubsw, 0, 0
827 AVX_INSTR pmaxsb, 0, 0
828 AVX_INSTR pmaxsw, 0, 0
829 AVX_INSTR pmaxsd, 0, 0
830 AVX_INSTR pmaxub, 0, 0
831 AVX_INSTR pmaxuw, 0, 0
832 AVX_INSTR pmaxud, 0, 0
833 AVX_INSTR pminsb, 0, 0
834 AVX_INSTR pminsw, 0, 0
835 AVX_INSTR pminsd, 0, 0
836 AVX_INSTR pminub, 0, 0
837 AVX_INSTR pminuw, 0, 0
838 AVX_INSTR pminud, 0, 0
839 AVX_INSTR pmulhuw, 0, 0
840 AVX_INSTR pmulhrsw, 0, 0
841 AVX_INSTR pmulhw, 0, 0
842 AVX_INSTR pmullw, 0, 0
843 AVX_INSTR pmulld, 0, 0
844 AVX_INSTR pmuludq, 0, 0
845 AVX_INSTR pmuldq, 0, 0
846 AVX_INSTR por, 0, 0
847 AVX_INSTR psadbw, 0, 0
848 AVX_INSTR pshufb, 0, 0
849 AVX_INSTR psignb, 0, 0
850 AVX_INSTR psignw, 0, 0
851 AVX_INSTR psignd, 0, 0
852 AVX_INSTR psllw, 0, 0
853 AVX_INSTR pslld, 0, 0
854 AVX_INSTR psllq, 0, 0
855 AVX_INSTR pslldq, 0, 0
856 AVX_INSTR psraw, 0, 0
857 AVX_INSTR psrad, 0, 0
858 AVX_INSTR psrlw, 0, 0
859 AVX_INSTR psrld, 0, 0
860 AVX_INSTR psrlq, 0, 0
861 AVX_INSTR psrldq, 0, 0
862 AVX_INSTR psubb, 0, 0
863 AVX_INSTR psubw, 0, 0
864 AVX_INSTR psubd, 0, 0
865 AVX_INSTR psubq, 0, 0
866 AVX_INSTR psubsb, 0, 0
867 AVX_INSTR psubsw, 0, 0
868 AVX_INSTR psubusb, 0, 0
869 AVX_INSTR psubusw, 0, 0
870 AVX_INSTR punpckhbw, 0, 0
871 AVX_INSTR punpckhwd, 0, 0
872 AVX_INSTR punpckhdq, 0, 0
873 AVX_INSTR punpckhqdq, 0, 0
874 AVX_INSTR punpcklbw, 0, 0
875 AVX_INSTR punpcklwd, 0, 0
876 AVX_INSTR punpckldq, 0, 0
877 AVX_INSTR punpcklqdq, 0, 0
878 AVX_INSTR pxor, 0, 0
879 AVX_INSTR shufps, 0, 1
880 AVX_INSTR subpd, 1, 0
881 AVX_INSTR subps, 1, 0
882 AVX_INSTR subsd, 1, 0
883 AVX_INSTR subss, 1, 0
884 AVX_INSTR unpckhpd, 1, 0
885 AVX_INSTR unpckhps, 1, 0
886 AVX_INSTR unpcklpd, 1, 0
887 AVX_INSTR unpcklps, 1, 0
888 AVX_INSTR xorpd, 1, 0
889 AVX_INSTR xorps, 1, 0
890
891 ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
892 AVX_INSTR pfadd, 1, 0
893 AVX_INSTR pfsub, 1, 0
894 AVX_INSTR pfmul, 1, 0