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