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