]> git.sesse.net Git - x264/blob - common/x86/x86inc.asm
x86: Make AVX2 also imply FMA3
[x264] / common / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm: x264asm abstraction layer
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2014 x264 project
5 ;*
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;*          Anton Mitrofanov <BugMaster@narod.ru>
8 ;*          Fiona Glaser <fiona@x264.com>
9 ;*          Henrik Gramner <henrik@gramner.com>
10 ;*
11 ;* Permission to use, copy, modify, and/or distribute this software for any
12 ;* purpose with or without fee is hereby granted, provided that the above
13 ;* copyright notice and this permission notice appear in all copies.
14 ;*
15 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 ;*****************************************************************************
23
24 ; This is a header file for the x264ASM assembly language, which uses
25 ; NASM/YASM syntax combined with a large number of macros to provide easy
26 ; abstraction between different calling conventions (x86_32, win64, linux64).
27 ; It also has various other useful features to simplify writing the kind of
28 ; DSP functions that are most often used in x264.
29
30 ; Unlike the rest of x264, this file is available under an ISC license, as it
31 ; has significant usefulness outside of x264 and we want it to be available
32 ; to the largest audience possible.  Of course, if you modify it for your own
33 ; purposes to add a new feature, we strongly encourage contributing a patch
34 ; as this feature might be useful for others as well.  Send patches or ideas
35 ; to x264-devel@videolan.org .
36
37 %ifndef private_prefix
38     %define private_prefix x264
39 %endif
40
41 %ifndef public_prefix
42     %define public_prefix private_prefix
43 %endif
44
45 %ifndef STACK_ALIGNMENT
46     %if ARCH_X86_64
47         %define STACK_ALIGNMENT 16
48     %else
49         %define STACK_ALIGNMENT 4
50     %endif
51 %endif
52
53 %define WIN64  0
54 %define UNIX64 0
55 %if ARCH_X86_64
56     %ifidn __OUTPUT_FORMAT__,win32
57         %define WIN64  1
58     %elifidn __OUTPUT_FORMAT__,win64
59         %define WIN64  1
60     %elifidn __OUTPUT_FORMAT__,x64
61         %define WIN64  1
62     %else
63         %define UNIX64 1
64     %endif
65 %endif
66
67 %ifdef PREFIX
68     %define mangle(x) _ %+ x
69 %else
70     %define mangle(x) x
71 %endif
72
73 %macro SECTION_RODATA 0-1 16
74     SECTION .rodata align=%1
75 %endmacro
76
77 %macro SECTION_TEXT 0-1 16
78     SECTION .text align=%1
79 %endmacro
80
81 %if WIN64
82     %define PIC
83 %elif ARCH_X86_64 == 0
84 ; x86_32 doesn't require PIC.
85 ; Some distros prefer shared objects to be PIC, but nothing breaks if
86 ; the code contains a few textrels, so we'll skip that complexity.
87     %undef PIC
88 %endif
89 %ifdef PIC
90     default rel
91 %endif
92
93 ; Macros to eliminate most code duplication between x86_32 and x86_64:
94 ; Currently this works only for leaf functions which load all their arguments
95 ; into registers at the start, and make no other use of the stack. Luckily that
96 ; covers most of x264's asm.
97
98 ; PROLOGUE:
99 ; %1 = number of arguments. loads them from stack if needed.
100 ; %2 = number of registers used. pushes callee-saved regs if needed.
101 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
102 ; %4 = (optional) stack size to be allocated. The stack will be aligned before
103 ;      allocating the specified stack size. If the required stack alignment is
104 ;      larger than the known stack alignment the stack will be manually aligned
105 ;      and an extra register will be allocated to hold the original stack
106 ;      pointer (to not invalidate r0m etc.). To prevent the use of an extra
107 ;      register as stack pointer, request a negative stack size.
108 ; %4+/%5+ = list of names to define to registers
109 ; PROLOGUE can also be invoked by adding the same options to cglobal
110
111 ; e.g.
112 ; cglobal foo, 2,3,7,0x40, dst, src, tmp
113 ; declares a function (foo) that automatically loads two arguments (dst and
114 ; src) into registers, uses one additional register (tmp) plus 7 vector
115 ; registers (m0-m6) and allocates 0x40 bytes of stack space.
116
117 ; TODO Some functions can use some args directly from the stack. If they're the
118 ; last args then you can just not declare them, but if they're in the middle
119 ; we need more flexible macro.
120
121 ; RET:
122 ; Pops anything that was pushed by PROLOGUE, and returns.
123
124 ; REP_RET:
125 ; Use this instead of RET if it's a branch target.
126
127 ; registers:
128 ; rN and rNq are the native-size register holding function argument N
129 ; rNd, rNw, rNb are dword, word, and byte size
130 ; rNh is the high 8 bits of the word size
131 ; rNm is the original location of arg N (a register or on the stack), dword
132 ; rNmp is native size
133
134 %macro DECLARE_REG 2-3
135     %define r%1q %2
136     %define r%1d %2d
137     %define r%1w %2w
138     %define r%1b %2b
139     %define r%1h %2h
140     %if %0 == 2
141         %define r%1m  %2d
142         %define r%1mp %2
143     %elif ARCH_X86_64 ; memory
144         %define r%1m [rstk + stack_offset + %3]
145         %define r%1mp qword r %+ %1 %+ m
146     %else
147         %define r%1m [rstk + stack_offset + %3]
148         %define r%1mp dword r %+ %1 %+ m
149     %endif
150     %define r%1  %2
151 %endmacro
152
153 %macro DECLARE_REG_SIZE 3
154     %define r%1q r%1
155     %define e%1q r%1
156     %define r%1d e%1
157     %define e%1d e%1
158     %define r%1w %1
159     %define e%1w %1
160     %define r%1h %3
161     %define e%1h %3
162     %define r%1b %2
163     %define e%1b %2
164 %if ARCH_X86_64 == 0
165     %define r%1  e%1
166 %endif
167 %endmacro
168
169 DECLARE_REG_SIZE ax, al, ah
170 DECLARE_REG_SIZE bx, bl, bh
171 DECLARE_REG_SIZE cx, cl, ch
172 DECLARE_REG_SIZE dx, dl, dh
173 DECLARE_REG_SIZE si, sil, null
174 DECLARE_REG_SIZE di, dil, null
175 DECLARE_REG_SIZE bp, bpl, null
176
177 ; t# defines for when per-arch register allocation is more complex than just function arguments
178
179 %macro DECLARE_REG_TMP 1-*
180     %assign %%i 0
181     %rep %0
182         CAT_XDEFINE t, %%i, r%1
183         %assign %%i %%i+1
184         %rotate 1
185     %endrep
186 %endmacro
187
188 %macro DECLARE_REG_TMP_SIZE 0-*
189     %rep %0
190         %define t%1q t%1 %+ q
191         %define t%1d t%1 %+ d
192         %define t%1w t%1 %+ w
193         %define t%1h t%1 %+ h
194         %define t%1b t%1 %+ b
195         %rotate 1
196     %endrep
197 %endmacro
198
199 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
200
201 %if ARCH_X86_64
202     %define gprsize 8
203 %else
204     %define gprsize 4
205 %endif
206
207 %macro PUSH 1
208     push %1
209     %ifidn rstk, rsp
210         %assign stack_offset stack_offset+gprsize
211     %endif
212 %endmacro
213
214 %macro POP 1
215     pop %1
216     %ifidn rstk, rsp
217         %assign stack_offset stack_offset-gprsize
218     %endif
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, rstk
251         %assign stack_offset stack_offset+(%2)
252     %endif
253 %endmacro
254
255 %macro ADD 2
256     add %1, %2
257     %ifidn %1, rstk
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 %define required_stack_alignment ((mmsize + 15) & ~15)
316
317 %macro ALLOC_STACK 1-2 0 ; stack_size, n_xmm_regs (for win64 only)
318     %ifnum %1
319         %if %1 != 0
320             %assign %%pad 0
321             %assign stack_size %1
322             %if stack_size < 0
323                 %assign stack_size -stack_size
324             %endif
325             %if WIN64
326                 %assign %%pad %%pad + 32 ; shadow space
327                 %if mmsize != 8
328                     %assign xmm_regs_used %2
329                     %if xmm_regs_used > 8
330                         %assign %%pad %%pad + (xmm_regs_used-8)*16 ; callee-saved xmm registers
331                     %endif
332                 %endif
333             %endif
334             %if required_stack_alignment <= STACK_ALIGNMENT
335                 ; maintain the current stack alignment
336                 %assign stack_size_padded stack_size + %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
337                 SUB rsp, stack_size_padded
338             %else
339                 %assign %%reg_num (regs_used - 1)
340                 %xdefine rstk r %+ %%reg_num
341                 ; align stack, and save original stack location directly above
342                 ; it, i.e. in [rsp+stack_size_padded], so we can restore the
343                 ; stack in a single instruction (i.e. mov rsp, rstk or mov
344                 ; rsp, [rsp+stack_size_padded])
345                 %if %1 < 0 ; need to store rsp on stack
346                     %xdefine rstkm [rsp + stack_size + %%pad]
347                     %assign %%pad %%pad + gprsize
348                 %else ; can keep rsp in rstk during whole function
349                     %xdefine rstkm rstk
350                 %endif
351                 %assign stack_size_padded stack_size + ((%%pad + required_stack_alignment-1) & ~(required_stack_alignment-1))
352                 mov rstk, rsp
353                 and rsp, ~(required_stack_alignment-1)
354                 sub rsp, stack_size_padded
355                 movifnidn rstkm, rstk
356             %endif
357             WIN64_PUSH_XMM
358         %endif
359     %endif
360 %endmacro
361
362 %macro SETUP_STACK_POINTER 1
363     %ifnum %1
364         %if %1 != 0 && required_stack_alignment > STACK_ALIGNMENT
365             %if %1 > 0
366                 %assign regs_used (regs_used + 1)
367             %elif ARCH_X86_64 && regs_used == num_args && num_args <= 4 + UNIX64 * 2
368                 %warning "Stack pointer will overwrite register argument"
369             %endif
370         %endif
371     %endif
372 %endmacro
373
374 %macro DEFINE_ARGS_INTERNAL 3+
375     %ifnum %2
376         DEFINE_ARGS %3
377     %elif %1 == 4
378         DEFINE_ARGS %2
379     %elif %1 > 4
380         DEFINE_ARGS %2, %3
381     %endif
382 %endmacro
383
384 %if WIN64 ; Windows x64 ;=================================================
385
386 DECLARE_REG 0,  rcx
387 DECLARE_REG 1,  rdx
388 DECLARE_REG 2,  R8
389 DECLARE_REG 3,  R9
390 DECLARE_REG 4,  R10, 40
391 DECLARE_REG 5,  R11, 48
392 DECLARE_REG 6,  rax, 56
393 DECLARE_REG 7,  rdi, 64
394 DECLARE_REG 8,  rsi, 72
395 DECLARE_REG 9,  rbx, 80
396 DECLARE_REG 10, rbp, 88
397 DECLARE_REG 11, R12, 96
398 DECLARE_REG 12, R13, 104
399 DECLARE_REG 13, R14, 112
400 DECLARE_REG 14, R15, 120
401
402 %macro PROLOGUE 2-5+ 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
403     %assign num_args %1
404     %assign regs_used %2
405     ASSERT regs_used >= num_args
406     SETUP_STACK_POINTER %4
407     ASSERT regs_used <= 15
408     PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
409     ALLOC_STACK %4, %3
410     %if mmsize != 8 && stack_size == 0
411         WIN64_SPILL_XMM %3
412     %endif
413     LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
414     DEFINE_ARGS_INTERNAL %0, %4, %5
415 %endmacro
416
417 %macro WIN64_PUSH_XMM 0
418     ; Use the shadow space to store XMM6 and XMM7, the rest needs stack space allocated.
419     %if xmm_regs_used > 6
420         movaps [rstk + stack_offset +  8], xmm6
421     %endif
422     %if xmm_regs_used > 7
423         movaps [rstk + stack_offset + 24], xmm7
424     %endif
425     %if xmm_regs_used > 8
426         %assign %%i 8
427         %rep xmm_regs_used-8
428             movaps [rsp + (%%i-8)*16 + stack_size + 32], xmm %+ %%i
429             %assign %%i %%i+1
430         %endrep
431     %endif
432 %endmacro
433
434 %macro WIN64_SPILL_XMM 1
435     %assign xmm_regs_used %1
436     ASSERT xmm_regs_used <= 16
437     %if xmm_regs_used > 8
438         ; Allocate stack space for callee-saved xmm registers plus shadow space and align the stack.
439         %assign %%pad (xmm_regs_used-8)*16 + 32
440         %assign stack_size_padded %%pad + ((-%%pad-stack_offset-gprsize) & (STACK_ALIGNMENT-1))
441         SUB rsp, stack_size_padded
442     %endif
443     WIN64_PUSH_XMM
444 %endmacro
445
446 %macro WIN64_RESTORE_XMM_INTERNAL 1
447     %assign %%pad_size 0
448     %if xmm_regs_used > 8
449         %assign %%i xmm_regs_used
450         %rep xmm_regs_used-8
451             %assign %%i %%i-1
452             movaps xmm %+ %%i, [%1 + (%%i-8)*16 + stack_size + 32]
453         %endrep
454     %endif
455     %if stack_size_padded > 0
456         %if stack_size > 0 && required_stack_alignment > STACK_ALIGNMENT
457             mov rsp, rstkm
458         %else
459             add %1, stack_size_padded
460             %assign %%pad_size stack_size_padded
461         %endif
462     %endif
463     %if xmm_regs_used > 7
464         movaps xmm7, [%1 + stack_offset - %%pad_size + 24]
465     %endif
466     %if xmm_regs_used > 6
467         movaps xmm6, [%1 + stack_offset - %%pad_size +  8]
468     %endif
469 %endmacro
470
471 %macro WIN64_RESTORE_XMM 1
472     WIN64_RESTORE_XMM_INTERNAL %1
473     %assign stack_offset (stack_offset-stack_size_padded)
474     %assign xmm_regs_used 0
475 %endmacro
476
477 %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32 || stack_size > 0
478
479 %macro RET 0
480     WIN64_RESTORE_XMM_INTERNAL rsp
481     POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
482 %if mmsize == 32
483     vzeroupper
484 %endif
485     AUTO_REP_RET
486 %endmacro
487
488 %elif ARCH_X86_64 ; *nix x64 ;=============================================
489
490 DECLARE_REG 0,  rdi
491 DECLARE_REG 1,  rsi
492 DECLARE_REG 2,  rdx
493 DECLARE_REG 3,  rcx
494 DECLARE_REG 4,  R8
495 DECLARE_REG 5,  R9
496 DECLARE_REG 6,  rax, 8
497 DECLARE_REG 7,  R10, 16
498 DECLARE_REG 8,  R11, 24
499 DECLARE_REG 9,  rbx, 32
500 DECLARE_REG 10, rbp, 40
501 DECLARE_REG 11, R12, 48
502 DECLARE_REG 12, R13, 56
503 DECLARE_REG 13, R14, 64
504 DECLARE_REG 14, R15, 72
505
506 %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
507     %assign num_args %1
508     %assign regs_used %2
509     ASSERT regs_used >= num_args
510     SETUP_STACK_POINTER %4
511     ASSERT regs_used <= 15
512     PUSH_IF_USED 9, 10, 11, 12, 13, 14
513     ALLOC_STACK %4
514     LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
515     DEFINE_ARGS_INTERNAL %0, %4, %5
516 %endmacro
517
518 %define has_epilogue regs_used > 9 || mmsize == 32 || stack_size > 0
519
520 %macro RET 0
521 %if stack_size_padded > 0
522 %if required_stack_alignment > STACK_ALIGNMENT
523     mov rsp, rstkm
524 %else
525     add rsp, stack_size_padded
526 %endif
527 %endif
528     POP_IF_USED 14, 13, 12, 11, 10, 9
529 %if mmsize == 32
530     vzeroupper
531 %endif
532     AUTO_REP_RET
533 %endmacro
534
535 %else ; X86_32 ;==============================================================
536
537 DECLARE_REG 0, eax, 4
538 DECLARE_REG 1, ecx, 8
539 DECLARE_REG 2, edx, 12
540 DECLARE_REG 3, ebx, 16
541 DECLARE_REG 4, esi, 20
542 DECLARE_REG 5, edi, 24
543 DECLARE_REG 6, ebp, 28
544 %define rsp esp
545
546 %macro DECLARE_ARG 1-*
547     %rep %0
548         %define r%1m [rstk + stack_offset + 4*%1 + 4]
549         %define r%1mp dword r%1m
550         %rotate 1
551     %endrep
552 %endmacro
553
554 DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
555
556 %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
557     %assign num_args %1
558     %assign regs_used %2
559     ASSERT regs_used >= num_args
560     %if num_args > 7
561         %assign num_args 7
562     %endif
563     %if regs_used > 7
564         %assign regs_used 7
565     %endif
566     SETUP_STACK_POINTER %4
567     ASSERT regs_used <= 7
568     PUSH_IF_USED 3, 4, 5, 6
569     ALLOC_STACK %4
570     LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
571     DEFINE_ARGS_INTERNAL %0, %4, %5
572 %endmacro
573
574 %define has_epilogue regs_used > 3 || mmsize == 32 || stack_size > 0
575
576 %macro RET 0
577 %if stack_size_padded > 0
578 %if required_stack_alignment > STACK_ALIGNMENT
579     mov rsp, rstkm
580 %else
581     add rsp, stack_size_padded
582 %endif
583 %endif
584     POP_IF_USED 6, 5, 4, 3
585 %if mmsize == 32
586     vzeroupper
587 %endif
588     AUTO_REP_RET
589 %endmacro
590
591 %endif ;======================================================================
592
593 %if WIN64 == 0
594 %macro WIN64_SPILL_XMM 1
595 %endmacro
596 %macro WIN64_RESTORE_XMM 1
597 %endmacro
598 %macro WIN64_PUSH_XMM 0
599 %endmacro
600 %endif
601
602 ; On AMD cpus <=K10, an ordinary ret is slow if it immediately follows either
603 ; a branch or a branch target. So switch to a 2-byte form of ret in that case.
604 ; We can automatically detect "follows a branch", but not a branch target.
605 ; (SSSE3 is a sufficient condition to know that your cpu doesn't have this problem.)
606 %macro REP_RET 0
607     %if has_epilogue
608         RET
609     %else
610         rep ret
611     %endif
612 %endmacro
613
614 %define last_branch_adr $$
615 %macro AUTO_REP_RET 0
616     %ifndef cpuflags
617         times ((last_branch_adr-$)>>31)+1 rep ; times 1 iff $ != last_branch_adr.
618     %elif notcpuflag(ssse3)
619         times ((last_branch_adr-$)>>31)+1 rep
620     %endif
621     ret
622 %endmacro
623
624 %macro BRANCH_INSTR 0-*
625     %rep %0
626         %macro %1 1-2 %1
627             %2 %1
628             %%branch_instr:
629             %xdefine last_branch_adr %%branch_instr
630         %endmacro
631         %rotate 1
632     %endrep
633 %endmacro
634
635 BRANCH_INSTR jz, je, jnz, jne, jl, jle, jnl, jnle, jg, jge, jng, jnge, ja, jae, jna, jnae, jb, jbe, jnb, jnbe, jc, jnc, js, jns, jo, jno, jp, jnp
636
637 %macro TAIL_CALL 2 ; callee, is_nonadjacent
638     %if has_epilogue
639         call %1
640         RET
641     %elif %2
642         jmp %1
643     %endif
644 %endmacro
645
646 ;=============================================================================
647 ; arch-independent part
648 ;=============================================================================
649
650 %assign function_align 16
651
652 ; Begin a function.
653 ; Applies any symbol mangling needed for C linkage, and sets up a define such that
654 ; subsequent uses of the function name automatically refer to the mangled version.
655 ; Appends cpuflags to the function name if cpuflags has been specified.
656 ; The "" empty default parameter is a workaround for nasm, which fails if SUFFIX
657 ; is empty and we call cglobal_internal with just %1 %+ SUFFIX (without %2).
658 %macro cglobal 1-2+ "" ; name, [PROLOGUE args]
659     cglobal_internal 1, %1 %+ SUFFIX, %2
660 %endmacro
661 %macro cvisible 1-2+ "" ; name, [PROLOGUE args]
662     cglobal_internal 0, %1 %+ SUFFIX, %2
663 %endmacro
664 %macro cglobal_internal 2-3+
665     %if %1
666         %xdefine %%FUNCTION_PREFIX private_prefix
667         %xdefine %%VISIBILITY hidden
668     %else
669         %xdefine %%FUNCTION_PREFIX public_prefix
670         %xdefine %%VISIBILITY
671     %endif
672     %ifndef cglobaled_%2
673         %xdefine %2 mangle(%%FUNCTION_PREFIX %+ _ %+ %2)
674         %xdefine %2.skip_prologue %2 %+ .skip_prologue
675         CAT_XDEFINE cglobaled_, %2, 1
676     %endif
677     %xdefine current_function %2
678     %ifidn __OUTPUT_FORMAT__,elf
679         global %2:function %%VISIBILITY
680     %else
681         global %2
682     %endif
683     align function_align
684     %2:
685     RESET_MM_PERMUTATION        ; needed for x86-64, also makes disassembly somewhat nicer
686     %xdefine rstk rsp           ; copy of the original stack pointer, used when greater alignment than the known stack alignment is required
687     %assign stack_offset 0      ; stack pointer offset relative to the return address
688     %assign stack_size 0        ; amount of stack space that can be freely used inside a function
689     %assign stack_size_padded 0 ; total amount of allocated stack space, including space for callee-saved xmm registers on WIN64 and alignment padding
690     %assign xmm_regs_used 0     ; number of XMM registers requested, used for dealing with callee-saved registers on WIN64
691     %ifnidn %3, ""
692         PROLOGUE %3
693     %endif
694 %endmacro
695
696 %macro cextern 1
697     %xdefine %1 mangle(private_prefix %+ _ %+ %1)
698     CAT_XDEFINE cglobaled_, %1, 1
699     extern %1
700 %endmacro
701
702 ; like cextern, but without the prefix
703 %macro cextern_naked 1
704     %xdefine %1 mangle(%1)
705     CAT_XDEFINE cglobaled_, %1, 1
706     extern %1
707 %endmacro
708
709 %macro const 1-2+
710     %xdefine %1 mangle(private_prefix %+ _ %+ %1)
711     %ifidn __OUTPUT_FORMAT__,elf
712         global %1:data hidden
713     %else
714         global %1
715     %endif
716     %1: %2
717 %endmacro
718
719 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
720 ; executable by default.
721 %ifidn __OUTPUT_FORMAT__,elf
722 SECTION .note.GNU-stack noalloc noexec nowrite progbits
723 %endif
724
725 ; cpuflags
726
727 %assign cpuflags_mmx      (1<<0)
728 %assign cpuflags_mmx2     (1<<1) | cpuflags_mmx
729 %assign cpuflags_3dnow    (1<<2) | cpuflags_mmx
730 %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
731 %assign cpuflags_sse      (1<<4) | cpuflags_mmx2
732 %assign cpuflags_sse2     (1<<5) | cpuflags_sse
733 %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
734 %assign cpuflags_sse3     (1<<7) | cpuflags_sse2
735 %assign cpuflags_ssse3    (1<<8) | cpuflags_sse3
736 %assign cpuflags_sse4     (1<<9) | cpuflags_ssse3
737 %assign cpuflags_sse42    (1<<10)| cpuflags_sse4
738 %assign cpuflags_avx      (1<<11)| cpuflags_sse42
739 %assign cpuflags_xop      (1<<12)| cpuflags_avx
740 %assign cpuflags_fma4     (1<<13)| cpuflags_avx
741 %assign cpuflags_fma3     (1<<14)| cpuflags_avx
742 %assign cpuflags_avx2     (1<<15)| cpuflags_fma3
743
744 %assign cpuflags_cache32  (1<<16)
745 %assign cpuflags_cache64  (1<<17)
746 %assign cpuflags_slowctz  (1<<18)
747 %assign cpuflags_lzcnt    (1<<19)
748 %assign cpuflags_aligned  (1<<20) ; not a cpu feature, but a function variant
749 %assign cpuflags_atom     (1<<21)
750 %assign cpuflags_bmi1     (1<<22)|cpuflags_lzcnt
751 %assign cpuflags_bmi2     (1<<23)|cpuflags_bmi1
752
753 %define    cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
754 %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
755
756 ; Takes an arbitrary number of cpuflags from the above list.
757 ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
758 ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
759 %macro INIT_CPUFLAGS 0-*
760     %xdefine SUFFIX
761     %undef cpuname
762     %assign cpuflags 0
763
764     %if %0 >= 1
765         %rep %0
766             %ifdef cpuname
767                 %xdefine cpuname cpuname %+ _%1
768             %else
769                 %xdefine cpuname %1
770             %endif
771             %assign cpuflags cpuflags | cpuflags_%1
772             %rotate 1
773         %endrep
774         %xdefine SUFFIX _ %+ cpuname
775
776         %if cpuflag(avx)
777             %assign avx_enabled 1
778         %endif
779         %if (mmsize == 16 && notcpuflag(sse2)) || (mmsize == 32 && notcpuflag(avx2))
780             %define mova movaps
781             %define movu movups
782             %define movnta movntps
783         %endif
784         %if cpuflag(aligned)
785             %define movu mova
786         %elif cpuflag(sse3) && notcpuflag(ssse3)
787             %define movu lddqu
788         %endif
789     %endif
790
791     %if ARCH_X86_64 || cpuflag(sse2)
792         CPU amdnop
793     %else
794         CPU basicnop
795     %endif
796 %endmacro
797
798 ; Merge mmx and sse*
799 ; m# is a simd register of the currently selected size
800 ; xm# is the corresponding xmm register if mmsize >= 16, otherwise the same as m#
801 ; ym# is the corresponding ymm register if mmsize >= 32, otherwise the same as m#
802 ; (All 3 remain in sync through SWAP.)
803
804 %macro CAT_XDEFINE 3
805     %xdefine %1%2 %3
806 %endmacro
807
808 %macro CAT_UNDEF 2
809     %undef %1%2
810 %endmacro
811
812 %macro INIT_MMX 0-1+
813     %assign avx_enabled 0
814     %define RESET_MM_PERMUTATION INIT_MMX %1
815     %define mmsize 8
816     %define num_mmregs 8
817     %define mova movq
818     %define movu movq
819     %define movh movd
820     %define movnta movntq
821     %assign %%i 0
822     %rep 8
823     CAT_XDEFINE m, %%i, mm %+ %%i
824     CAT_XDEFINE nnmm, %%i, %%i
825     %assign %%i %%i+1
826     %endrep
827     %rep 8
828     CAT_UNDEF m, %%i
829     CAT_UNDEF nnmm, %%i
830     %assign %%i %%i+1
831     %endrep
832     INIT_CPUFLAGS %1
833 %endmacro
834
835 %macro INIT_XMM 0-1+
836     %assign avx_enabled 0
837     %define RESET_MM_PERMUTATION INIT_XMM %1
838     %define mmsize 16
839     %define num_mmregs 8
840     %if ARCH_X86_64
841     %define num_mmregs 16
842     %endif
843     %define mova movdqa
844     %define movu movdqu
845     %define movh movq
846     %define movnta movntdq
847     %assign %%i 0
848     %rep num_mmregs
849     CAT_XDEFINE m, %%i, xmm %+ %%i
850     CAT_XDEFINE nnxmm, %%i, %%i
851     %assign %%i %%i+1
852     %endrep
853     INIT_CPUFLAGS %1
854 %endmacro
855
856 %macro INIT_YMM 0-1+
857     %assign avx_enabled 1
858     %define RESET_MM_PERMUTATION INIT_YMM %1
859     %define mmsize 32
860     %define num_mmregs 8
861     %if ARCH_X86_64
862     %define num_mmregs 16
863     %endif
864     %define mova movdqa
865     %define movu movdqu
866     %undef movh
867     %define movnta movntdq
868     %assign %%i 0
869     %rep num_mmregs
870     CAT_XDEFINE m, %%i, ymm %+ %%i
871     CAT_XDEFINE nymm, %%i, %%i
872     %assign %%i %%i+1
873     %endrep
874     INIT_CPUFLAGS %1
875 %endmacro
876
877 INIT_XMM
878
879 %macro DECLARE_MMCAST 1
880     %define  mmmm%1   mm%1
881     %define  mmxmm%1  mm%1
882     %define  mmymm%1  mm%1
883     %define xmmmm%1   mm%1
884     %define xmmxmm%1 xmm%1
885     %define xmmymm%1 xmm%1
886     %define ymmmm%1   mm%1
887     %define ymmxmm%1 xmm%1
888     %define ymmymm%1 ymm%1
889     %define xm%1 xmm %+ m%1
890     %define ym%1 ymm %+ m%1
891 %endmacro
892
893 %assign i 0
894 %rep 16
895     DECLARE_MMCAST i
896 %assign i i+1
897 %endrep
898
899 ; I often want to use macros that permute their arguments. e.g. there's no
900 ; efficient way to implement butterfly or transpose or dct without swapping some
901 ; arguments.
902 ;
903 ; I would like to not have to manually keep track of the permutations:
904 ; If I insert a permutation in the middle of a function, it should automatically
905 ; change everything that follows. For more complex macros I may also have multiple
906 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
907 ;
908 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
909 ; permutes its arguments. It's equivalent to exchanging the contents of the
910 ; registers, except that this way you exchange the register names instead, so it
911 ; doesn't cost any cycles.
912
913 %macro PERMUTE 2-* ; takes a list of pairs to swap
914 %rep %0/2
915     %xdefine %%tmp%2 m%2
916     %rotate 2
917 %endrep
918 %rep %0/2
919     %xdefine m%1 %%tmp%2
920     CAT_XDEFINE nn, m%1, %1
921     %rotate 2
922 %endrep
923 %endmacro
924
925 %macro SWAP 2+ ; swaps a single chain (sometimes more concise than pairs)
926 %ifnum %1 ; SWAP 0, 1, ...
927     SWAP_INTERNAL_NUM %1, %2
928 %else ; SWAP m0, m1, ...
929     SWAP_INTERNAL_NAME %1, %2
930 %endif
931 %endmacro
932
933 %macro SWAP_INTERNAL_NUM 2-*
934     %rep %0-1
935         %xdefine %%tmp m%1
936         %xdefine m%1 m%2
937         %xdefine m%2 %%tmp
938         CAT_XDEFINE nn, m%1, %1
939         CAT_XDEFINE nn, m%2, %2
940     %rotate 1
941     %endrep
942 %endmacro
943
944 %macro SWAP_INTERNAL_NAME 2-*
945     %xdefine %%args nn %+ %1
946     %rep %0-1
947         %xdefine %%args %%args, nn %+ %2
948     %rotate 1
949     %endrep
950     SWAP_INTERNAL_NUM %%args
951 %endmacro
952
953 ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
954 ; calls to that function will automatically load the permutation, so values can
955 ; be returned in mmregs.
956 %macro SAVE_MM_PERMUTATION 0-1
957     %if %0
958         %xdefine %%f %1_m
959     %else
960         %xdefine %%f current_function %+ _m
961     %endif
962     %assign %%i 0
963     %rep num_mmregs
964         CAT_XDEFINE %%f, %%i, m %+ %%i
965     %assign %%i %%i+1
966     %endrep
967 %endmacro
968
969 %macro LOAD_MM_PERMUTATION 1 ; name to load from
970     %ifdef %1_m0
971         %assign %%i 0
972         %rep num_mmregs
973             CAT_XDEFINE m, %%i, %1_m %+ %%i
974             CAT_XDEFINE nn, m %+ %%i, %%i
975         %assign %%i %%i+1
976         %endrep
977     %endif
978 %endmacro
979
980 ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
981 %macro call 1
982     call_internal %1, %1 %+ SUFFIX
983 %endmacro
984 %macro call_internal 2
985     %xdefine %%i %1
986     %ifndef cglobaled_%1
987         %ifdef cglobaled_%2
988             %xdefine %%i %2
989         %endif
990     %endif
991     call %%i
992     LOAD_MM_PERMUTATION %%i
993 %endmacro
994
995 ; Substitutions that reduce instruction size but are functionally equivalent
996 %macro add 2
997     %ifnum %2
998         %if %2==128
999             sub %1, -128
1000         %else
1001             add %1, %2
1002         %endif
1003     %else
1004         add %1, %2
1005     %endif
1006 %endmacro
1007
1008 %macro sub 2
1009     %ifnum %2
1010         %if %2==128
1011             add %1, -128
1012         %else
1013             sub %1, %2
1014         %endif
1015     %else
1016         sub %1, %2
1017     %endif
1018 %endmacro
1019
1020 ;=============================================================================
1021 ; AVX abstraction layer
1022 ;=============================================================================
1023
1024 %assign i 0
1025 %rep 16
1026     %if i < 8
1027         CAT_XDEFINE sizeofmm, i, 8
1028     %endif
1029     CAT_XDEFINE sizeofxmm, i, 16
1030     CAT_XDEFINE sizeofymm, i, 32
1031 %assign i i+1
1032 %endrep
1033 %undef i
1034
1035 %macro CHECK_AVX_INSTR_EMU 3-*
1036     %xdefine %%opcode %1
1037     %xdefine %%dst %2
1038     %rep %0-2
1039         %ifidn %%dst, %3
1040             %error non-avx emulation of ``%%opcode'' is not supported
1041         %endif
1042         %rotate 1
1043     %endrep
1044 %endmacro
1045
1046 ;%1 == instruction
1047 ;%2 == minimal instruction set
1048 ;%3 == 1 if float, 0 if int
1049 ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
1050 ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
1051 ;%6+: operands
1052 %macro RUN_AVX_INSTR 6-9+
1053     %ifnum sizeof%7
1054         %assign __sizeofreg sizeof%7
1055     %elifnum sizeof%6
1056         %assign __sizeofreg sizeof%6
1057     %else
1058         %assign __sizeofreg mmsize
1059     %endif
1060     %assign __emulate_avx 0
1061     %if avx_enabled && __sizeofreg >= 16
1062         %xdefine __instr v%1
1063     %else
1064         %xdefine __instr %1
1065         %if %0 >= 8+%4
1066             %assign __emulate_avx 1
1067         %endif
1068     %endif
1069     %ifnidn %2, fnord
1070         %ifdef cpuname
1071             %if notcpuflag(%2)
1072                 %error use of ``%1'' %2 instruction in cpuname function: current_function
1073             %endif
1074         %endif
1075     %endif
1076
1077     %if __emulate_avx
1078         %xdefine __src1 %7
1079         %xdefine __src2 %8
1080         %ifnidn %6, %7
1081             %if %0 >= 9
1082                 CHECK_AVX_INSTR_EMU {%1 %6, %7, %8, %9}, %6, %8, %9
1083             %else
1084                 CHECK_AVX_INSTR_EMU {%1 %6, %7, %8}, %6, %8
1085             %endif
1086             %if %5 && %4 == 0
1087                 %ifnid %8
1088                     ; 3-operand AVX instructions with a memory arg can only have it in src2,
1089                     ; whereas SSE emulation prefers to have it in src1 (i.e. the mov).
1090                     ; So, if the instruction is commutative with a memory arg, swap them.
1091                     %xdefine __src1 %8
1092                     %xdefine __src2 %7
1093                 %endif
1094             %endif
1095             %if __sizeofreg == 8
1096                 MOVQ %6, __src1
1097             %elif %3
1098                 MOVAPS %6, __src1
1099             %else
1100                 MOVDQA %6, __src1
1101             %endif
1102         %endif
1103         %if %0 >= 9
1104             %1 %6, __src2, %9
1105         %else
1106             %1 %6, __src2
1107         %endif
1108     %elif %0 >= 9
1109         __instr %6, %7, %8, %9
1110     %elif %0 == 8
1111         __instr %6, %7, %8
1112     %elif %0 == 7
1113         __instr %6, %7
1114     %else
1115         __instr %6
1116     %endif
1117 %endmacro
1118
1119 ;%1 == instruction
1120 ;%2 == minimal instruction set
1121 ;%3 == 1 if float, 0 if int
1122 ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
1123 ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
1124 %macro AVX_INSTR 1-5 fnord, 0, 1, 0
1125     %macro %1 1-10 fnord, fnord, fnord, fnord, %1, %2, %3, %4, %5
1126         %ifidn %2, fnord
1127             RUN_AVX_INSTR %6, %7, %8, %9, %10, %1
1128         %elifidn %3, fnord
1129             RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2
1130         %elifidn %4, fnord
1131             RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3
1132         %elifidn %5, fnord
1133             RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4
1134         %else
1135             RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4, %5
1136         %endif
1137     %endmacro
1138 %endmacro
1139
1140 ; Instructions with both VEX and non-VEX encodings
1141 ; Non-destructive instructions are written without parameters
1142 AVX_INSTR addpd, sse2, 1, 0, 1
1143 AVX_INSTR addps, sse, 1, 0, 1
1144 AVX_INSTR addsd, sse2, 1, 0, 1
1145 AVX_INSTR addss, sse, 1, 0, 1
1146 AVX_INSTR addsubpd, sse3, 1, 0, 0
1147 AVX_INSTR addsubps, sse3, 1, 0, 0
1148 AVX_INSTR aesdec, fnord, 0, 0, 0
1149 AVX_INSTR aesdeclast, fnord, 0, 0, 0
1150 AVX_INSTR aesenc, fnord, 0, 0, 0
1151 AVX_INSTR aesenclast, fnord, 0, 0, 0
1152 AVX_INSTR aesimc
1153 AVX_INSTR aeskeygenassist
1154 AVX_INSTR andnpd, sse2, 1, 0, 0
1155 AVX_INSTR andnps, sse, 1, 0, 0
1156 AVX_INSTR andpd, sse2, 1, 0, 1
1157 AVX_INSTR andps, sse, 1, 0, 1
1158 AVX_INSTR blendpd, sse4, 1, 0, 0
1159 AVX_INSTR blendps, sse4, 1, 0, 0
1160 AVX_INSTR blendvpd, sse4, 1, 0, 0
1161 AVX_INSTR blendvps, sse4, 1, 0, 0
1162 AVX_INSTR cmppd, sse2, 1, 1, 0
1163 AVX_INSTR cmpps, sse, 1, 1, 0
1164 AVX_INSTR cmpsd, sse2, 1, 1, 0
1165 AVX_INSTR cmpss, sse, 1, 1, 0
1166 AVX_INSTR comisd, sse2
1167 AVX_INSTR comiss, sse
1168 AVX_INSTR cvtdq2pd, sse2
1169 AVX_INSTR cvtdq2ps, sse2
1170 AVX_INSTR cvtpd2dq, sse2
1171 AVX_INSTR cvtpd2ps, sse2
1172 AVX_INSTR cvtps2dq, sse2
1173 AVX_INSTR cvtps2pd, sse2
1174 AVX_INSTR cvtsd2si, sse2
1175 AVX_INSTR cvtsd2ss, sse2
1176 AVX_INSTR cvtsi2sd, sse2
1177 AVX_INSTR cvtsi2ss, sse
1178 AVX_INSTR cvtss2sd, sse2
1179 AVX_INSTR cvtss2si, sse
1180 AVX_INSTR cvttpd2dq, sse2
1181 AVX_INSTR cvttps2dq, sse2
1182 AVX_INSTR cvttsd2si, sse2
1183 AVX_INSTR cvttss2si, sse
1184 AVX_INSTR divpd, sse2, 1, 0, 0
1185 AVX_INSTR divps, sse, 1, 0, 0
1186 AVX_INSTR divsd, sse2, 1, 0, 0
1187 AVX_INSTR divss, sse, 1, 0, 0
1188 AVX_INSTR dppd, sse4, 1, 1, 0
1189 AVX_INSTR dpps, sse4, 1, 1, 0
1190 AVX_INSTR extractps, sse4
1191 AVX_INSTR haddpd, sse3, 1, 0, 0
1192 AVX_INSTR haddps, sse3, 1, 0, 0
1193 AVX_INSTR hsubpd, sse3, 1, 0, 0
1194 AVX_INSTR hsubps, sse3, 1, 0, 0
1195 AVX_INSTR insertps, sse4, 1, 1, 0
1196 AVX_INSTR lddqu, sse3
1197 AVX_INSTR ldmxcsr, sse
1198 AVX_INSTR maskmovdqu, sse2
1199 AVX_INSTR maxpd, sse2, 1, 0, 1
1200 AVX_INSTR maxps, sse, 1, 0, 1
1201 AVX_INSTR maxsd, sse2, 1, 0, 1
1202 AVX_INSTR maxss, sse, 1, 0, 1
1203 AVX_INSTR minpd, sse2, 1, 0, 1
1204 AVX_INSTR minps, sse, 1, 0, 1
1205 AVX_INSTR minsd, sse2, 1, 0, 1
1206 AVX_INSTR minss, sse, 1, 0, 1
1207 AVX_INSTR movapd, sse2
1208 AVX_INSTR movaps, sse
1209 AVX_INSTR movd
1210 AVX_INSTR movddup, sse3
1211 AVX_INSTR movdqa, sse2
1212 AVX_INSTR movdqu, sse2
1213 AVX_INSTR movhlps, sse, 1, 0, 0
1214 AVX_INSTR movhpd, sse2, 1, 0, 0
1215 AVX_INSTR movhps, sse, 1, 0, 0
1216 AVX_INSTR movlhps, sse, 1, 0, 0
1217 AVX_INSTR movlpd, sse2, 1, 0, 0
1218 AVX_INSTR movlps, sse, 1, 0, 0
1219 AVX_INSTR movmskpd, sse2
1220 AVX_INSTR movmskps, sse
1221 AVX_INSTR movntdq, sse2
1222 AVX_INSTR movntdqa, sse4
1223 AVX_INSTR movntpd, sse2
1224 AVX_INSTR movntps, sse
1225 AVX_INSTR movq
1226 AVX_INSTR movsd, sse2, 1, 0, 0
1227 AVX_INSTR movshdup, sse3
1228 AVX_INSTR movsldup, sse3
1229 AVX_INSTR movss, sse, 1, 0, 0
1230 AVX_INSTR movupd, sse2
1231 AVX_INSTR movups, sse
1232 AVX_INSTR mpsadbw, sse4
1233 AVX_INSTR mulpd, sse2, 1, 0, 1
1234 AVX_INSTR mulps, sse, 1, 0, 1
1235 AVX_INSTR mulsd, sse2, 1, 0, 1
1236 AVX_INSTR mulss, sse, 1, 0, 1
1237 AVX_INSTR orpd, sse2, 1, 0, 1
1238 AVX_INSTR orps, sse, 1, 0, 1
1239 AVX_INSTR pabsb, ssse3
1240 AVX_INSTR pabsd, ssse3
1241 AVX_INSTR pabsw, ssse3
1242 AVX_INSTR packsswb, mmx, 0, 0, 0
1243 AVX_INSTR packssdw, mmx, 0, 0, 0
1244 AVX_INSTR packuswb, mmx, 0, 0, 0
1245 AVX_INSTR packusdw, sse4, 0, 0, 0
1246 AVX_INSTR paddb, mmx, 0, 0, 1
1247 AVX_INSTR paddw, mmx, 0, 0, 1
1248 AVX_INSTR paddd, mmx, 0, 0, 1
1249 AVX_INSTR paddq, sse2, 0, 0, 1
1250 AVX_INSTR paddsb, mmx, 0, 0, 1
1251 AVX_INSTR paddsw, mmx, 0, 0, 1
1252 AVX_INSTR paddusb, mmx, 0, 0, 1
1253 AVX_INSTR paddusw, mmx, 0, 0, 1
1254 AVX_INSTR palignr, ssse3
1255 AVX_INSTR pand, mmx, 0, 0, 1
1256 AVX_INSTR pandn, mmx, 0, 0, 0
1257 AVX_INSTR pavgb, mmx2, 0, 0, 1
1258 AVX_INSTR pavgw, mmx2, 0, 0, 1
1259 AVX_INSTR pblendvb, sse4, 0, 0, 0
1260 AVX_INSTR pblendw, sse4
1261 AVX_INSTR pclmulqdq
1262 AVX_INSTR pcmpestri, sse42
1263 AVX_INSTR pcmpestrm, sse42
1264 AVX_INSTR pcmpistri, sse42
1265 AVX_INSTR pcmpistrm, sse42
1266 AVX_INSTR pcmpeqb, mmx, 0, 0, 1
1267 AVX_INSTR pcmpeqw, mmx, 0, 0, 1
1268 AVX_INSTR pcmpeqd, mmx, 0, 0, 1
1269 AVX_INSTR pcmpeqq, sse4, 0, 0, 1
1270 AVX_INSTR pcmpgtb, mmx, 0, 0, 0
1271 AVX_INSTR pcmpgtw, mmx, 0, 0, 0
1272 AVX_INSTR pcmpgtd, mmx, 0, 0, 0
1273 AVX_INSTR pcmpgtq, sse42, 0, 0, 0
1274 AVX_INSTR pextrb, sse4
1275 AVX_INSTR pextrd, sse4
1276 AVX_INSTR pextrq, sse4
1277 AVX_INSTR pextrw, mmx2
1278 AVX_INSTR phaddw, ssse3, 0, 0, 0
1279 AVX_INSTR phaddd, ssse3, 0, 0, 0
1280 AVX_INSTR phaddsw, ssse3, 0, 0, 0
1281 AVX_INSTR phminposuw, sse4
1282 AVX_INSTR phsubw, ssse3, 0, 0, 0
1283 AVX_INSTR phsubd, ssse3, 0, 0, 0
1284 AVX_INSTR phsubsw, ssse3, 0, 0, 0
1285 AVX_INSTR pinsrb, sse4
1286 AVX_INSTR pinsrd, sse4
1287 AVX_INSTR pinsrq, sse4
1288 AVX_INSTR pinsrw, mmx2
1289 AVX_INSTR pmaddwd, mmx, 0, 0, 1
1290 AVX_INSTR pmaddubsw, ssse3, 0, 0, 0
1291 AVX_INSTR pmaxsb, sse4, 0, 0, 1
1292 AVX_INSTR pmaxsw, mmx2, 0, 0, 1
1293 AVX_INSTR pmaxsd, sse4, 0, 0, 1
1294 AVX_INSTR pmaxub, mmx2, 0, 0, 1
1295 AVX_INSTR pmaxuw, sse4, 0, 0, 1
1296 AVX_INSTR pmaxud, sse4, 0, 0, 1
1297 AVX_INSTR pminsb, sse4, 0, 0, 1
1298 AVX_INSTR pminsw, mmx2, 0, 0, 1
1299 AVX_INSTR pminsd, sse4, 0, 0, 1
1300 AVX_INSTR pminub, mmx2, 0, 0, 1
1301 AVX_INSTR pminuw, sse4, 0, 0, 1
1302 AVX_INSTR pminud, sse4, 0, 0, 1
1303 AVX_INSTR pmovmskb, mmx2
1304 AVX_INSTR pmovsxbw, sse4
1305 AVX_INSTR pmovsxbd, sse4
1306 AVX_INSTR pmovsxbq, sse4
1307 AVX_INSTR pmovsxwd, sse4
1308 AVX_INSTR pmovsxwq, sse4
1309 AVX_INSTR pmovsxdq, sse4
1310 AVX_INSTR pmovzxbw, sse4
1311 AVX_INSTR pmovzxbd, sse4
1312 AVX_INSTR pmovzxbq, sse4
1313 AVX_INSTR pmovzxwd, sse4
1314 AVX_INSTR pmovzxwq, sse4
1315 AVX_INSTR pmovzxdq, sse4
1316 AVX_INSTR pmuldq, sse4, 0, 0, 1
1317 AVX_INSTR pmulhrsw, ssse3, 0, 0, 1
1318 AVX_INSTR pmulhuw, mmx2, 0, 0, 1
1319 AVX_INSTR pmulhw, mmx, 0, 0, 1
1320 AVX_INSTR pmullw, mmx, 0, 0, 1
1321 AVX_INSTR pmulld, sse4, 0, 0, 1
1322 AVX_INSTR pmuludq, sse2, 0, 0, 1
1323 AVX_INSTR por, mmx, 0, 0, 1
1324 AVX_INSTR psadbw, mmx2, 0, 0, 1
1325 AVX_INSTR pshufb, ssse3, 0, 0, 0
1326 AVX_INSTR pshufd, sse2
1327 AVX_INSTR pshufhw, sse2
1328 AVX_INSTR pshuflw, sse2
1329 AVX_INSTR psignb, ssse3, 0, 0, 0
1330 AVX_INSTR psignw, ssse3, 0, 0, 0
1331 AVX_INSTR psignd, ssse3, 0, 0, 0
1332 AVX_INSTR psllw, mmx, 0, 0, 0
1333 AVX_INSTR pslld, mmx, 0, 0, 0
1334 AVX_INSTR psllq, mmx, 0, 0, 0
1335 AVX_INSTR pslldq, sse2, 0, 0, 0
1336 AVX_INSTR psraw, mmx, 0, 0, 0
1337 AVX_INSTR psrad, mmx, 0, 0, 0
1338 AVX_INSTR psrlw, mmx, 0, 0, 0
1339 AVX_INSTR psrld, mmx, 0, 0, 0
1340 AVX_INSTR psrlq, mmx, 0, 0, 0
1341 AVX_INSTR psrldq, sse2, 0, 0, 0
1342 AVX_INSTR psubb, mmx, 0, 0, 0
1343 AVX_INSTR psubw, mmx, 0, 0, 0
1344 AVX_INSTR psubd, mmx, 0, 0, 0
1345 AVX_INSTR psubq, sse2, 0, 0, 0
1346 AVX_INSTR psubsb, mmx, 0, 0, 0
1347 AVX_INSTR psubsw, mmx, 0, 0, 0
1348 AVX_INSTR psubusb, mmx, 0, 0, 0
1349 AVX_INSTR psubusw, mmx, 0, 0, 0
1350 AVX_INSTR ptest, sse4
1351 AVX_INSTR punpckhbw, mmx, 0, 0, 0
1352 AVX_INSTR punpckhwd, mmx, 0, 0, 0
1353 AVX_INSTR punpckhdq, mmx, 0, 0, 0
1354 AVX_INSTR punpckhqdq, sse2, 0, 0, 0
1355 AVX_INSTR punpcklbw, mmx, 0, 0, 0
1356 AVX_INSTR punpcklwd, mmx, 0, 0, 0
1357 AVX_INSTR punpckldq, mmx, 0, 0, 0
1358 AVX_INSTR punpcklqdq, sse2, 0, 0, 0
1359 AVX_INSTR pxor, mmx, 0, 0, 1
1360 AVX_INSTR rcpps, sse, 1, 0, 0
1361 AVX_INSTR rcpss, sse, 1, 0, 0
1362 AVX_INSTR roundpd, sse4
1363 AVX_INSTR roundps, sse4
1364 AVX_INSTR roundsd, sse4
1365 AVX_INSTR roundss, sse4
1366 AVX_INSTR rsqrtps, sse, 1, 0, 0
1367 AVX_INSTR rsqrtss, sse, 1, 0, 0
1368 AVX_INSTR shufpd, sse2, 1, 1, 0
1369 AVX_INSTR shufps, sse, 1, 1, 0
1370 AVX_INSTR sqrtpd, sse2, 1, 0, 0
1371 AVX_INSTR sqrtps, sse, 1, 0, 0
1372 AVX_INSTR sqrtsd, sse2, 1, 0, 0
1373 AVX_INSTR sqrtss, sse, 1, 0, 0
1374 AVX_INSTR stmxcsr, sse
1375 AVX_INSTR subpd, sse2, 1, 0, 0
1376 AVX_INSTR subps, sse, 1, 0, 0
1377 AVX_INSTR subsd, sse2, 1, 0, 0
1378 AVX_INSTR subss, sse, 1, 0, 0
1379 AVX_INSTR ucomisd, sse2
1380 AVX_INSTR ucomiss, sse
1381 AVX_INSTR unpckhpd, sse2, 1, 0, 0
1382 AVX_INSTR unpckhps, sse, 1, 0, 0
1383 AVX_INSTR unpcklpd, sse2, 1, 0, 0
1384 AVX_INSTR unpcklps, sse, 1, 0, 0
1385 AVX_INSTR xorpd, sse2, 1, 0, 1
1386 AVX_INSTR xorps, sse, 1, 0, 1
1387
1388 ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
1389 AVX_INSTR pfadd, 3dnow, 1, 0, 1
1390 AVX_INSTR pfsub, 3dnow, 1, 0, 0
1391 AVX_INSTR pfmul, 3dnow, 1, 0, 1
1392
1393 ; base-4 constants for shuffles
1394 %assign i 0
1395 %rep 256
1396     %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
1397     %if j < 10
1398         CAT_XDEFINE q000, j, i
1399     %elif j < 100
1400         CAT_XDEFINE q00, j, i
1401     %elif j < 1000
1402         CAT_XDEFINE q0, j, i
1403     %else
1404         CAT_XDEFINE q, j, i
1405     %endif
1406 %assign i i+1
1407 %endrep
1408 %undef i
1409 %undef j
1410
1411 %macro FMA_INSTR 3
1412     %macro %1 4-7 %1, %2, %3
1413         %if cpuflag(xop)
1414             v%5 %1, %2, %3, %4
1415         %elifnidn %1, %4
1416             %6 %1, %2, %3
1417             %7 %1, %4
1418         %else
1419             %error non-xop emulation of ``%5 %1, %2, %3, %4'' is not supported
1420         %endif
1421     %endmacro
1422 %endmacro
1423
1424 FMA_INSTR  pmacsww,  pmullw, paddw
1425 FMA_INSTR  pmacsdd,  pmulld, paddd ; sse4 emulation
1426 FMA_INSTR pmacsdql,  pmuldq, paddq ; sse4 emulation
1427 FMA_INSTR pmadcswd, pmaddwd, paddd
1428
1429 ; convert FMA4 to FMA3 if possible
1430 %macro FMA4_INSTR 4
1431     %macro %1 4-8 %1, %2, %3, %4
1432         %if cpuflag(fma4)
1433             v%5 %1, %2, %3, %4
1434         %elifidn %1, %2
1435             v%6 %1, %4, %3 ; %1 = %1 * %3 + %4
1436         %elifidn %1, %3
1437             v%7 %1, %2, %4 ; %1 = %2 * %1 + %4
1438         %elifidn %1, %4
1439             v%8 %1, %2, %3 ; %1 = %2 * %3 + %1
1440         %else
1441             %error fma3 emulation of ``%5 %1, %2, %3, %4'' is not supported
1442         %endif
1443     %endmacro
1444 %endmacro
1445
1446 FMA4_INSTR fmaddpd, fmadd132pd, fmadd213pd, fmadd231pd
1447 FMA4_INSTR fmaddps, fmadd132ps, fmadd213ps, fmadd231ps
1448 FMA4_INSTR fmaddsd, fmadd132sd, fmadd213sd, fmadd231sd
1449 FMA4_INSTR fmaddss, fmadd132ss, fmadd213ss, fmadd231ss
1450
1451 FMA4_INSTR fmaddsubpd, fmaddsub132pd, fmaddsub213pd, fmaddsub231pd
1452 FMA4_INSTR fmaddsubps, fmaddsub132ps, fmaddsub213ps, fmaddsub231ps
1453 FMA4_INSTR fmsubaddpd, fmsubadd132pd, fmsubadd213pd, fmsubadd231pd
1454 FMA4_INSTR fmsubaddps, fmsubadd132ps, fmsubadd213ps, fmsubadd231ps
1455
1456 FMA4_INSTR fmsubpd, fmsub132pd, fmsub213pd, fmsub231pd
1457 FMA4_INSTR fmsubps, fmsub132ps, fmsub213ps, fmsub231ps
1458 FMA4_INSTR fmsubsd, fmsub132sd, fmsub213sd, fmsub231sd
1459 FMA4_INSTR fmsubss, fmsub132ss, fmsub213ss, fmsub231ss
1460
1461 FMA4_INSTR fnmaddpd, fnmadd132pd, fnmadd213pd, fnmadd231pd
1462 FMA4_INSTR fnmaddps, fnmadd132ps, fnmadd213ps, fnmadd231ps
1463 FMA4_INSTR fnmaddsd, fnmadd132sd, fnmadd213sd, fnmadd231sd
1464 FMA4_INSTR fnmaddss, fnmadd132ss, fnmadd213ss, fnmadd231ss
1465
1466 FMA4_INSTR fnmsubpd, fnmsub132pd, fnmsub213pd, fnmsub231pd
1467 FMA4_INSTR fnmsubps, fnmsub132ps, fnmsub213ps, fnmsub231ps
1468 FMA4_INSTR fnmsubsd, fnmsub132sd, fnmsub213sd, fnmsub231sd
1469 FMA4_INSTR fnmsubss, fnmsub132ss, fnmsub213ss, fnmsub231ss
1470
1471 ; workaround: vpbroadcastq is broken in x86_32 due to a yasm bug
1472 %if ARCH_X86_64 == 0
1473 %macro vpbroadcastq 2
1474 %if sizeof%1 == 16
1475     movddup %1, %2
1476 %else
1477     vbroadcastsd %1, %2
1478 %endif
1479 %endmacro
1480 %endif