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