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