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