]> git.sesse.net Git - ffmpeg/blob - libavutil/x86/x86inc.asm
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / x86 / x86inc.asm
1 ;*****************************************************************************
2 ;* x86inc.asm: x264asm abstraction layer
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2012 x264 project
5 ;*
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;*          Anton Mitrofanov <BugMaster@narod.ru>
8 ;*          Jason Garrett-Glaser <darkshikari@gmail.com>
9 ;*          Henrik Gramner <hengar-6@student.ltu.se>
10 ;*
11 ;* Permission to use, copy, modify, and/or distribute this software for any
12 ;* purpose with or without fee is hereby granted, provided that the above
13 ;* copyright notice and this permission notice appear in all copies.
14 ;*
15 ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 ;*****************************************************************************
23
24 ; This is a header file for the x264ASM assembly language, which uses
25 ; NASM/YASM syntax combined with a large number of macros to provide easy
26 ; abstraction between different calling conventions (x86_32, win64, linux64).
27 ; It also has various other useful features to simplify writing the kind of
28 ; DSP functions that are most often used in x264.
29
30 ; Unlike the rest of x264, this file is available under an ISC license, as it
31 ; has significant usefulness outside of x264 and we want it to be available
32 ; to the largest audience possible.  Of course, if you modify it for your own
33 ; purposes to add a new feature, we strongly encourage contributing a patch
34 ; as this feature might be useful for others as well.  Send patches or ideas
35 ; to x264-devel@videolan.org .
36
37 %define program_name ff
38
39 %define UNIX64 0
40 %define WIN64  0
41 %if ARCH_X86_64
42     %ifidn __OUTPUT_FORMAT__,win32
43         %define WIN64  1
44     %elifidn __OUTPUT_FORMAT__,win64
45         %define WIN64  1
46     %else
47         %define UNIX64 1
48     %endif
49 %endif
50
51 %ifdef PREFIX
52     %define mangle(x) _ %+ x
53 %else
54     %define mangle(x) x
55 %endif
56
57 ; FIXME: All of the 64bit asm functions that take a stride as an argument
58 ; via register, assume that the high dword of that register is filled with 0.
59 ; This is true in practice (since we never do any 64bit arithmetic on strides,
60 ; and x264's strides are all positive), but is not guaranteed by the ABI.
61
62 ; Name of the .rodata section.
63 %macro SECTION_RODATA 0-1 16
64     ; Kludge: Something on OS X fails to align .rodata even given an align
65     ; attribute, so use a different read-only section. This has been fixed in
66     ; yasm 0.8.0 and nasm 2.6.
67     %ifdef __YASM_VERSION_ID__
68         %if __YASM_VERSION_ID__ < 00080000h
69             %define NEED_MACHO_RODATA_KLUDGE
70         %endif
71     %elifdef __NASM_VERSION_ID__
72         %if __NASM_VERSION_ID__ < 02060000h
73             %define NEED_MACHO_RODATA_KLUDGE
74         %endif
75     %endif
76
77     %ifidn __OUTPUT_FORMAT__,aout
78         section .text
79     %else
80         %ifndef NEED_MACHO_RODATA_KLUDGE
81             SECTION .rodata align=%1
82         %else
83             %ifidn __OUTPUT_FORMAT__,macho64
84                 SECTION .text align=%1
85             %elifidn __OUTPUT_FORMAT__,macho
86                 SECTION .text align=%1
87                 fakegot:
88             %else
89                 SECTION .rodata align=%1
90             %endif
91         %endif
92     %endif
93
94     %undef NEED_MACHO_RODATA_KLUDGE
95 %endmacro
96
97 ; aout does not support align=
98 %macro SECTION_TEXT 0-1 16
99     %ifidn __OUTPUT_FORMAT__,aout
100         SECTION .text
101     %else
102         SECTION .text align=%1
103     %endif
104 %endmacro
105
106 %if WIN64
107     %define PIC
108 %elif ARCH_X86_64 == 0
109 ; x86_32 doesn't require PIC.
110 ; Some distros prefer shared objects to be PIC, but nothing breaks if
111 ; the code contains a few textrels, so we'll skip that complexity.
112     %undef PIC
113 %endif
114 %ifdef PIC
115     default rel
116 %endif
117
118 ; Always use long nops (reduces 0x90 spam in disassembly on x86_32)
119 ; Not supported by NASM (except via smartalign package + ALIGNMODE k8,
120 ; however that fails when used together with the -M option)
121 %ifdef __YASM_VER__
122 CPU amdnop
123 %endif
124
125 ; Macros to eliminate most code duplication between x86_32 and x86_64:
126 ; Currently this works only for leaf functions which load all their arguments
127 ; into registers at the start, and make no other use of the stack. Luckily that
128 ; covers most of x264's asm.
129
130 ; PROLOGUE:
131 ; %1 = number of arguments. loads them from stack if needed.
132 ; %2 = number of registers used. pushes callee-saved regs if needed.
133 ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
134 ; %4 = list of names to define to registers
135 ; PROLOGUE can also be invoked by adding the same options to cglobal
136
137 ; e.g.
138 ; cglobal foo, 2,3,0, dst, src, tmp
139 ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
140
141 ; TODO Some functions can use some args directly from the stack. If they're the
142 ; last args then you can just not declare them, but if they're in the middle
143 ; we need more flexible macro.
144
145 ; RET:
146 ; Pops anything that was pushed by PROLOGUE, and returns.
147
148 ; REP_RET:
149 ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
150 ; which are slow when a normal ret follows a branch.
151
152 ; registers:
153 ; rN and rNq are the native-size register holding function argument N
154 ; rNd, rNw, rNb are dword, word, and byte size
155 ; rNm is the original location of arg N (a register or on the stack), dword
156 ; rNmp is native size
157
158 %macro DECLARE_REG 5-6
159     %define r%1q %2
160     %define r%1d %3
161     %define r%1w %4
162     %define r%1b %5
163     %if %0 == 5
164         %define r%1m  %3
165         %define r%1mp %2
166     %elif ARCH_X86_64 ; memory
167         %define r%1m [rsp + stack_offset + %6]
168         %define r%1mp qword r %+ %1 %+ m
169     %else
170         %define r%1m [esp + stack_offset + %6]
171         %define r%1mp dword r %+ %1 %+ m
172     %endif
173     %define r%1  %2
174 %endmacro
175
176 %macro DECLARE_REG_SIZE 2
177     %define r%1q r%1
178     %define e%1q r%1
179     %define r%1d e%1
180     %define e%1d e%1
181     %define r%1w %1
182     %define e%1w %1
183     %define r%1b %2
184     %define e%1b %2
185 %if ARCH_X86_64 == 0
186     %define r%1  e%1
187 %endif
188 %endmacro
189
190 DECLARE_REG_SIZE ax, al
191 DECLARE_REG_SIZE bx, bl
192 DECLARE_REG_SIZE cx, cl
193 DECLARE_REG_SIZE dx, dl
194 DECLARE_REG_SIZE si, sil
195 DECLARE_REG_SIZE di, dil
196 DECLARE_REG_SIZE bp, bpl
197
198 ; t# defines for when per-arch register allocation is more complex than just function arguments
199
200 %macro DECLARE_REG_TMP 1-*
201     %assign %%i 0
202     %rep %0
203         CAT_XDEFINE t, %%i, r%1
204         %assign %%i %%i+1
205         %rotate 1
206     %endrep
207 %endmacro
208
209 %macro DECLARE_REG_TMP_SIZE 0-*
210     %rep %0
211         %define t%1q t%1 %+ q
212         %define t%1d t%1 %+ d
213         %define t%1w t%1 %+ w
214         %define t%1b t%1 %+ b
215         %rotate 1
216     %endrep
217 %endmacro
218
219 DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
220
221 %if ARCH_X86_64
222     %define gprsize 8
223 %else
224     %define gprsize 4
225 %endif
226
227 %macro PUSH 1
228     push %1
229     %assign stack_offset stack_offset+gprsize
230 %endmacro
231
232 %macro POP 1
233     pop %1
234     %assign stack_offset stack_offset-gprsize
235 %endmacro
236
237 %macro PUSH_IF_USED 1-*
238     %rep %0
239         %if %1 < regs_used
240             PUSH r%1
241         %endif
242         %rotate 1
243     %endrep
244 %endmacro
245
246 %macro POP_IF_USED 1-*
247     %rep %0
248         %if %1 < regs_used
249             pop r%1
250         %endif
251         %rotate 1
252     %endrep
253 %endmacro
254
255 %macro LOAD_IF_USED 1-*
256     %rep %0
257         %if %1 < num_args
258             mov r%1, r %+ %1 %+ mp
259         %endif
260         %rotate 1
261     %endrep
262 %endmacro
263
264 %macro SUB 2
265     sub %1, %2
266     %ifidn %1, rsp
267         %assign stack_offset stack_offset+(%2)
268     %endif
269 %endmacro
270
271 %macro ADD 2
272     add %1, %2
273     %ifidn %1, rsp
274         %assign stack_offset stack_offset-(%2)
275     %endif
276 %endmacro
277
278 %macro movifnidn 2
279     %ifnidn %1, %2
280         mov %1, %2
281     %endif
282 %endmacro
283
284 %macro movsxdifnidn 2
285     %ifnidn %1, %2
286         movsxd %1, %2
287     %endif
288 %endmacro
289
290 %macro ASSERT 1
291     %if (%1) == 0
292         %error assert failed
293     %endif
294 %endmacro
295
296 %macro DEFINE_ARGS 0-*
297     %ifdef n_arg_names
298         %assign %%i 0
299         %rep n_arg_names
300             CAT_UNDEF arg_name %+ %%i, q
301             CAT_UNDEF arg_name %+ %%i, d
302             CAT_UNDEF arg_name %+ %%i, w
303             CAT_UNDEF arg_name %+ %%i, b
304             CAT_UNDEF arg_name %+ %%i, m
305             CAT_UNDEF arg_name %+ %%i, mp
306             CAT_UNDEF arg_name, %%i
307             %assign %%i %%i+1
308         %endrep
309     %endif
310
311     %xdefine %%stack_offset stack_offset
312     %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
313     %assign %%i 0
314     %rep %0
315         %xdefine %1q r %+ %%i %+ q
316         %xdefine %1d r %+ %%i %+ d
317         %xdefine %1w r %+ %%i %+ w
318         %xdefine %1b r %+ %%i %+ b
319         %xdefine %1m r %+ %%i %+ m
320         %xdefine %1mp r %+ %%i %+ mp
321         CAT_XDEFINE arg_name, %%i, %1
322         %assign %%i %%i+1
323         %rotate 1
324     %endrep
325     %xdefine stack_offset %%stack_offset
326     %assign n_arg_names %0
327 %endmacro
328
329 %if WIN64 ; Windows x64 ;=================================================
330
331 DECLARE_REG 0,  rcx, ecx,  cx,   cl
332 DECLARE_REG 1,  rdx, edx,  dx,   dl
333 DECLARE_REG 2,  R8,  R8D,  R8W,  R8B
334 DECLARE_REG 3,  R9,  R9D,  R9W,  R9B
335 DECLARE_REG 4,  R10, R10D, R10W, R10B, 40
336 DECLARE_REG 5,  R11, R11D, R11W, R11B, 48
337 DECLARE_REG 6,  rax, eax,  ax,   al,   56
338 DECLARE_REG 7,  rdi, edi,  di,   dil,  64
339 DECLARE_REG 8,  rsi, esi,  si,   sil,  72
340 DECLARE_REG 9,  rbx, ebx,  bx,   bl,   80
341 DECLARE_REG 10, rbp, ebp,  bp,   bpl,  88
342 DECLARE_REG 11, R12, R12D, R12W, R12B, 96
343 DECLARE_REG 12, R13, R13D, R13W, R13B, 104
344 DECLARE_REG 13, R14, R14D, R14W, R14B, 112
345 DECLARE_REG 14, R15, R15D, R15W, R15B, 120
346
347 %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
348     %assign num_args %1
349     %assign regs_used %2
350     ASSERT regs_used >= num_args
351     ASSERT regs_used <= 15
352     PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
353     %if mmsize == 8
354         %assign xmm_regs_used 0
355     %else
356         WIN64_SPILL_XMM %3
357     %endif
358     LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
359     DEFINE_ARGS %4
360 %endmacro
361
362 %macro WIN64_SPILL_XMM 1
363     %assign xmm_regs_used %1
364     ASSERT xmm_regs_used <= 16
365     %if xmm_regs_used > 6
366         SUB rsp, (xmm_regs_used-6)*16+16
367         %assign %%i xmm_regs_used
368         %rep (xmm_regs_used-6)
369             %assign %%i %%i-1
370             movdqa [rsp + (%%i-6)*16+(~stack_offset&8)], xmm %+ %%i
371         %endrep
372     %endif
373 %endmacro
374
375 %macro WIN64_RESTORE_XMM_INTERNAL 1
376     %if xmm_regs_used > 6
377         %assign %%i xmm_regs_used
378         %rep (xmm_regs_used-6)
379             %assign %%i %%i-1
380             movdqa xmm %+ %%i, [%1 + (%%i-6)*16+(~stack_offset&8)]
381         %endrep
382         add %1, (xmm_regs_used-6)*16+16
383     %endif
384 %endmacro
385
386 %macro WIN64_RESTORE_XMM 1
387     WIN64_RESTORE_XMM_INTERNAL %1
388     %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
389     %assign xmm_regs_used 0
390 %endmacro
391
392 %macro RET 0
393     WIN64_RESTORE_XMM_INTERNAL rsp
394     POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
395 %if mmsize == 32
396     vzeroupper
397 %endif
398     ret
399 %endmacro
400
401 %macro REP_RET 0
402     %if regs_used > 7 || xmm_regs_used > 6 || mmsize == 32
403         RET
404     %else
405         rep ret
406     %endif
407 %endmacro
408
409 %elif ARCH_X86_64 ; *nix x64 ;=============================================
410
411 DECLARE_REG 0,  rdi, edi,  di,   dil
412 DECLARE_REG 1,  rsi, esi,  si,   sil
413 DECLARE_REG 2,  rdx, edx,  dx,   dl
414 DECLARE_REG 3,  rcx, ecx,  cx,   cl
415 DECLARE_REG 4,  R8,  R8D,  R8W,  R8B
416 DECLARE_REG 5,  R9,  R9D,  R9W,  R9B
417 DECLARE_REG 6,  rax, eax,  ax,   al,   8
418 DECLARE_REG 7,  R10, R10D, R10W, R10B, 16
419 DECLARE_REG 8,  R11, R11D, R11W, R11B, 24
420 DECLARE_REG 9,  rbx, ebx,  bx,   bl,   32
421 DECLARE_REG 10, rbp, ebp,  bp,   bpl,  40
422 DECLARE_REG 11, R12, R12D, R12W, R12B, 48
423 DECLARE_REG 12, R13, R13D, R13W, R13B, 56
424 DECLARE_REG 13, R14, R14D, R14W, R14B, 64
425 DECLARE_REG 14, R15, R15D, R15W, R15B, 72
426
427 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
428     %assign num_args %1
429     %assign regs_used %2
430     ASSERT regs_used >= num_args
431     ASSERT regs_used <= 15
432     PUSH_IF_USED 9, 10, 11, 12, 13, 14
433     LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
434     DEFINE_ARGS %4
435 %endmacro
436
437 %macro RET 0
438     POP_IF_USED 14, 13, 12, 11, 10, 9
439 %if mmsize == 32
440     vzeroupper
441 %endif
442     ret
443 %endmacro
444
445 %macro REP_RET 0
446     %if regs_used > 9 || mmsize == 32
447         RET
448     %else
449         rep ret
450     %endif
451 %endmacro
452
453 %else ; X86_32 ;==============================================================
454
455 DECLARE_REG 0, eax, eax, ax, al,   4
456 DECLARE_REG 1, ecx, ecx, cx, cl,   8
457 DECLARE_REG 2, edx, edx, dx, dl,   12
458 DECLARE_REG 3, ebx, ebx, bx, bl,   16
459 DECLARE_REG 4, esi, esi, si, null, 20
460 DECLARE_REG 5, edi, edi, di, null, 24
461 DECLARE_REG 6, ebp, ebp, bp, null, 28
462 %define rsp esp
463
464 %macro DECLARE_ARG 1-*
465     %rep %0
466         %define r%1m [esp + stack_offset + 4*%1 + 4]
467         %define r%1mp dword r%1m
468         %rotate 1
469     %endrep
470 %endmacro
471
472 DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
473
474 %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
475     %assign num_args %1
476     %assign regs_used %2
477     %if regs_used > 7
478         %assign regs_used 7
479     %endif
480     ASSERT regs_used >= num_args
481     PUSH_IF_USED 3, 4, 5, 6
482     LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
483     DEFINE_ARGS %4
484 %endmacro
485
486 %macro RET 0
487     POP_IF_USED 6, 5, 4, 3
488 %if mmsize == 32
489     vzeroupper
490 %endif
491     ret
492 %endmacro
493
494 %macro REP_RET 0
495     %if regs_used > 3 || mmsize == 32
496         RET
497     %else
498         rep ret
499     %endif
500 %endmacro
501
502 %endif ;======================================================================
503
504 %if WIN64 == 0
505 %macro WIN64_SPILL_XMM 1
506 %endmacro
507 %macro WIN64_RESTORE_XMM 1
508 %endmacro
509 %endif
510
511 ;=============================================================================
512 ; arch-independent part
513 ;=============================================================================
514
515 %assign function_align 16
516
517 ; Begin a function.
518 ; Applies any symbol mangling needed for C linkage, and sets up a define such that
519 ; subsequent uses of the function name automatically refer to the mangled version.
520 ; Appends cpuflags to the function name if cpuflags has been specified.
521 %macro cglobal 1-2+ ; name, [PROLOGUE args]
522 %if %0 == 1
523     ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
524     %ifndef cpuname
525     cglobal_internal %1
526     %else
527     cglobal_internal %1 %+ SUFFIX
528     %endif
529 %else
530     ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
531     %ifndef cpuname
532     cglobal_internal %1, %2
533     %else
534     cglobal_internal %1 %+ SUFFIX, %2
535     %endif
536 %endif
537 %endmacro
538 %macro cglobal_internal 1-2+
539     %ifndef cglobaled_%1
540         %xdefine %1 mangle(program_name %+ _ %+ %1)
541         %xdefine %1.skip_prologue %1 %+ .skip_prologue
542         CAT_XDEFINE cglobaled_, %1, 1
543     %endif
544     %xdefine current_function %1
545     %ifidn __OUTPUT_FORMAT__,elf
546         global %1:function hidden
547     %else
548         global %1
549     %endif
550     align function_align
551     %1:
552     RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
553     %assign stack_offset 0
554     %if %0 > 1
555         PROLOGUE %2
556     %endif
557 %endmacro
558
559 %macro cextern 1
560     %xdefine %1 mangle(program_name %+ _ %+ %1)
561     CAT_XDEFINE cglobaled_, %1, 1
562     extern %1
563 %endmacro
564
565 ; like cextern, but without the prefix
566 %macro cextern_naked 1
567     %xdefine %1 mangle(%1)
568     CAT_XDEFINE cglobaled_, %1, 1
569     extern %1
570 %endmacro
571
572 %macro const 2+
573     %xdefine %1 mangle(program_name %+ _ %+ %1)
574     global %1
575     %1: %2
576 %endmacro
577
578 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
579 ; executable by default.
580 %ifidn __OUTPUT_FORMAT__,elf
581 SECTION .note.GNU-stack noalloc noexec nowrite progbits
582 %endif
583
584 ; cpuflags
585
586 %assign cpuflags_mmx      (1<<0)
587 %assign cpuflags_mmx2     (1<<1) | cpuflags_mmx
588 %assign cpuflags_3dnow    (1<<2) | cpuflags_mmx
589 %assign cpuflags_3dnow2   (1<<3) | cpuflags_3dnow
590 %assign cpuflags_sse      (1<<4) | cpuflags_mmx2
591 %assign cpuflags_sse2     (1<<5) | cpuflags_sse
592 %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
593 %assign cpuflags_sse3     (1<<7) | cpuflags_sse2
594 %assign cpuflags_ssse3    (1<<8) | cpuflags_sse3
595 %assign cpuflags_sse4     (1<<9) | cpuflags_ssse3
596 %assign cpuflags_sse42    (1<<10)| cpuflags_sse4
597 %assign cpuflags_avx      (1<<11)| cpuflags_sse42
598 %assign cpuflags_xop      (1<<12)| cpuflags_avx
599 %assign cpuflags_fma4     (1<<13)| cpuflags_avx
600
601 %assign cpuflags_cache32  (1<<16)
602 %assign cpuflags_cache64  (1<<17)
603 %assign cpuflags_slowctz  (1<<18)
604 %assign cpuflags_lzcnt    (1<<19)
605 %assign cpuflags_misalign (1<<20)
606 %assign cpuflags_aligned  (1<<21) ; not a cpu feature, but a function variant
607 %assign cpuflags_atom     (1<<22)
608
609 %define    cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
610 %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
611
612 ; Takes up to 2 cpuflags from the above list.
613 ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
614 ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
615 %macro INIT_CPUFLAGS 0-2
616 %ifdef __YASM_VER__
617     CPU amdnop
618 %endif
619     %if %0 >= 1
620         %xdefine cpuname %1
621         %assign cpuflags cpuflags_%1
622         %if %0 >= 2
623             %xdefine cpuname %1_%2
624             %assign cpuflags cpuflags | cpuflags_%2
625         %endif
626         %xdefine SUFFIX _ %+ cpuname
627         %if cpuflag(avx)
628             %assign avx_enabled 1
629         %endif
630         %if mmsize == 16 && notcpuflag(sse2)
631             %define mova movaps
632             %define movu movups
633             %define movnta movntps
634         %endif
635         %if cpuflag(aligned)
636             %define movu mova
637         %elifidn %1, sse3
638             %define movu lddqu
639         %endif
640         %ifdef __YASM_VER__
641         %if notcpuflag(mmx2)
642             CPU basicnop
643         %endif
644         %endif
645     %else
646         %xdefine SUFFIX
647         %undef cpuname
648         %undef cpuflags
649     %endif
650 %endmacro
651
652 ; merge mmx and sse*
653
654 %macro CAT_XDEFINE 3
655     %xdefine %1%2 %3
656 %endmacro
657
658 %macro CAT_UNDEF 2
659     %undef %1%2
660 %endmacro
661
662 %macro INIT_MMX 0-1+
663     %assign avx_enabled 0
664     %define RESET_MM_PERMUTATION INIT_MMX %1
665     %define mmsize 8
666     %define num_mmregs 8
667     %define mova movq
668     %define movu movq
669     %define movh movd
670     %define movnta movntq
671     %assign %%i 0
672     %rep 8
673     CAT_XDEFINE m, %%i, mm %+ %%i
674     CAT_XDEFINE nmm, %%i, %%i
675     %assign %%i %%i+1
676     %endrep
677     %rep 8
678     CAT_UNDEF m, %%i
679     CAT_UNDEF nmm, %%i
680     %assign %%i %%i+1
681     %endrep
682     INIT_CPUFLAGS %1
683 %endmacro
684
685 %macro INIT_XMM 0-1+
686     %assign avx_enabled 0
687     %define RESET_MM_PERMUTATION INIT_XMM %1
688     %define mmsize 16
689     %define num_mmregs 8
690     %if ARCH_X86_64
691     %define num_mmregs 16
692     %endif
693     %define mova movdqa
694     %define movu movdqu
695     %define movh movq
696     %define movnta movntdq
697     %assign %%i 0
698     %rep num_mmregs
699     CAT_XDEFINE m, %%i, xmm %+ %%i
700     CAT_XDEFINE nxmm, %%i, %%i
701     %assign %%i %%i+1
702     %endrep
703     INIT_CPUFLAGS %1
704 %endmacro
705
706 ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
707 %macro INIT_AVX 0
708     INIT_XMM
709     %assign avx_enabled 1
710     %define PALIGNR PALIGNR_SSSE3
711     %define RESET_MM_PERMUTATION INIT_AVX
712 %endmacro
713
714 %macro INIT_YMM 0-1+
715     %assign avx_enabled 1
716     %define RESET_MM_PERMUTATION INIT_YMM %1
717     %define mmsize 32
718     %define num_mmregs 8
719     %if ARCH_X86_64
720     %define num_mmregs 16
721     %endif
722     %define mova vmovaps
723     %define movu vmovups
724     %undef movh
725     %define movnta vmovntps
726     %assign %%i 0
727     %rep num_mmregs
728     CAT_XDEFINE m, %%i, ymm %+ %%i
729     CAT_XDEFINE nymm, %%i, %%i
730     %assign %%i %%i+1
731     %endrep
732     INIT_CPUFLAGS %1
733 %endmacro
734
735 INIT_XMM
736
737 ; I often want to use macros that permute their arguments. e.g. there's no
738 ; efficient way to implement butterfly or transpose or dct without swapping some
739 ; arguments.
740 ;
741 ; I would like to not have to manually keep track of the permutations:
742 ; If I insert a permutation in the middle of a function, it should automatically
743 ; change everything that follows. For more complex macros I may also have multiple
744 ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
745 ;
746 ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
747 ; permutes its arguments. It's equivalent to exchanging the contents of the
748 ; registers, except that this way you exchange the register names instead, so it
749 ; doesn't cost any cycles.
750
751 %macro PERMUTE 2-* ; takes a list of pairs to swap
752 %rep %0/2
753     %xdefine tmp%2 m%2
754     %xdefine ntmp%2 nm%2
755     %rotate 2
756 %endrep
757 %rep %0/2
758     %xdefine m%1 tmp%2
759     %xdefine nm%1 ntmp%2
760     %undef tmp%2
761     %undef ntmp%2
762     %rotate 2
763 %endrep
764 %endmacro
765
766 %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
767 %rep %0-1
768 %ifdef m%1
769     %xdefine tmp m%1
770     %xdefine m%1 m%2
771     %xdefine m%2 tmp
772     CAT_XDEFINE n, m%1, %1
773     CAT_XDEFINE n, m%2, %2
774 %else
775     ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
776     ; Be careful using this mode in nested macros though, as in some cases there may be
777     ; other copies of m# that have already been dereferenced and don't get updated correctly.
778     %xdefine %%n1 n %+ %1
779     %xdefine %%n2 n %+ %2
780     %xdefine tmp m %+ %%n1
781     CAT_XDEFINE m, %%n1, m %+ %%n2
782     CAT_XDEFINE m, %%n2, tmp
783     CAT_XDEFINE n, m %+ %%n1, %%n1
784     CAT_XDEFINE n, m %+ %%n2, %%n2
785 %endif
786     %undef tmp
787     %rotate 1
788 %endrep
789 %endmacro
790
791 ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
792 ; calls to that function will automatically load the permutation, so values can
793 ; be returned in mmregs.
794 %macro SAVE_MM_PERMUTATION 0-1
795     %if %0
796         %xdefine %%f %1_m
797     %else
798         %xdefine %%f current_function %+ _m
799     %endif
800     %assign %%i 0
801     %rep num_mmregs
802         CAT_XDEFINE %%f, %%i, m %+ %%i
803     %assign %%i %%i+1
804     %endrep
805 %endmacro
806
807 %macro LOAD_MM_PERMUTATION 1 ; name to load from
808     %ifdef %1_m0
809         %assign %%i 0
810         %rep num_mmregs
811             CAT_XDEFINE m, %%i, %1_m %+ %%i
812             CAT_XDEFINE n, m %+ %%i, %%i
813         %assign %%i %%i+1
814         %endrep
815     %endif
816 %endmacro
817
818 ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
819 %macro call 1
820     ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
821     %ifndef cpuname
822     call_internal %1, %1
823     %else
824     call_internal %1, %1 %+ SUFFIX
825     %endif
826 %endmacro
827 %macro call_internal 2
828     %xdefine %%i %1
829     %ifndef cglobaled_%1
830         %ifdef cglobaled_%2
831             %xdefine %%i %2
832         %endif
833     %endif
834     call %%i
835     LOAD_MM_PERMUTATION %%i
836 %endmacro
837
838 ; Substitutions that reduce instruction size but are functionally equivalent
839 %macro add 2
840     %ifnum %2
841         %if %2==128
842             sub %1, -128
843         %else
844             add %1, %2
845         %endif
846     %else
847         add %1, %2
848     %endif
849 %endmacro
850
851 %macro sub 2
852     %ifnum %2
853         %if %2==128
854             add %1, -128
855         %else
856             sub %1, %2
857         %endif
858     %else
859         sub %1, %2
860     %endif
861 %endmacro
862
863 ;=============================================================================
864 ; AVX abstraction layer
865 ;=============================================================================
866
867 %assign i 0
868 %rep 16
869     %if i < 8
870         CAT_XDEFINE sizeofmm, i, 8
871     %endif
872     CAT_XDEFINE sizeofxmm, i, 16
873     CAT_XDEFINE sizeofymm, i, 32
874 %assign i i+1
875 %endrep
876 %undef i
877
878 ;%1 == instruction
879 ;%2 == 1 if float, 0 if int
880 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
881 ;%4 == number of operands given
882 ;%5+: operands
883 %macro RUN_AVX_INSTR 6-7+
884     %ifid %5
885         %define %%size sizeof%5
886     %else
887         %define %%size mmsize
888     %endif
889     %if %%size==32
890         %if %0 >= 7
891             v%1 %5, %6, %7
892         %else
893             v%1 %5, %6
894         %endif
895     %else
896         %if %%size==8
897             %define %%regmov movq
898         %elif %2
899             %define %%regmov movaps
900         %else
901             %define %%regmov movdqa
902         %endif
903
904         %if %4>=3+%3
905             %ifnidn %5, %6
906                 %if avx_enabled && sizeof%5==16
907                     v%1 %5, %6, %7
908                 %else
909                     %%regmov %5, %6
910                     %1 %5, %7
911                 %endif
912             %else
913                 %1 %5, %7
914             %endif
915         %elif %3
916             %1 %5, %6, %7
917         %else
918             %1 %5, %6
919         %endif
920     %endif
921 %endmacro
922
923 ; 3arg AVX ops with a memory arg can only have it in src2,
924 ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
925 ; So, if the op is symmetric and the wrong one is memory, swap them.
926 %macro RUN_AVX_INSTR1 8
927     %assign %%swap 0
928     %if avx_enabled
929         %ifnid %6
930             %assign %%swap 1
931         %endif
932     %elifnidn %5, %6
933         %ifnid %7
934             %assign %%swap 1
935         %endif
936     %endif
937     %if %%swap && %3 == 0 && %8 == 1
938         RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
939     %else
940         RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
941     %endif
942 %endmacro
943
944 ;%1 == instruction
945 ;%2 == 1 if float, 0 if int
946 ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 3-operand (xmm, xmm, xmm)
947 ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
948 %macro AVX_INSTR 4
949     %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
950         %ifidn %3, fnord
951             RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
952         %elifidn %4, fnord
953             RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
954         %elifidn %5, fnord
955             RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
956         %else
957             RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
958         %endif
959     %endmacro
960 %endmacro
961
962 AVX_INSTR addpd, 1, 0, 1
963 AVX_INSTR addps, 1, 0, 1
964 AVX_INSTR addsd, 1, 0, 1
965 AVX_INSTR addss, 1, 0, 1
966 AVX_INSTR addsubpd, 1, 0, 0
967 AVX_INSTR addsubps, 1, 0, 0
968 AVX_INSTR andpd, 1, 0, 1
969 AVX_INSTR andps, 1, 0, 1
970 AVX_INSTR andnpd, 1, 0, 0
971 AVX_INSTR andnps, 1, 0, 0
972 AVX_INSTR blendpd, 1, 0, 0
973 AVX_INSTR blendps, 1, 0, 0
974 AVX_INSTR blendvpd, 1, 0, 0
975 AVX_INSTR blendvps, 1, 0, 0
976 AVX_INSTR cmppd, 1, 0, 0
977 AVX_INSTR cmpps, 1, 0, 0
978 AVX_INSTR cmpsd, 1, 0, 0
979 AVX_INSTR cmpss, 1, 0, 0
980 AVX_INSTR cvtdq2ps, 1, 0, 0
981 AVX_INSTR cvtps2dq, 1, 0, 0
982 AVX_INSTR divpd, 1, 0, 0
983 AVX_INSTR divps, 1, 0, 0
984 AVX_INSTR divsd, 1, 0, 0
985 AVX_INSTR divss, 1, 0, 0
986 AVX_INSTR dppd, 1, 1, 0
987 AVX_INSTR dpps, 1, 1, 0
988 AVX_INSTR haddpd, 1, 0, 0
989 AVX_INSTR haddps, 1, 0, 0
990 AVX_INSTR hsubpd, 1, 0, 0
991 AVX_INSTR hsubps, 1, 0, 0
992 AVX_INSTR maxpd, 1, 0, 1
993 AVX_INSTR maxps, 1, 0, 1
994 AVX_INSTR maxsd, 1, 0, 1
995 AVX_INSTR maxss, 1, 0, 1
996 AVX_INSTR minpd, 1, 0, 1
997 AVX_INSTR minps, 1, 0, 1
998 AVX_INSTR minsd, 1, 0, 1
999 AVX_INSTR minss, 1, 0, 1
1000 AVX_INSTR movhlps, 1, 0, 0
1001 AVX_INSTR movlhps, 1, 0, 0
1002 AVX_INSTR movsd, 1, 0, 0
1003 AVX_INSTR movss, 1, 0, 0
1004 AVX_INSTR mpsadbw, 0, 1, 0
1005 AVX_INSTR mulpd, 1, 0, 1
1006 AVX_INSTR mulps, 1, 0, 1
1007 AVX_INSTR mulsd, 1, 0, 1
1008 AVX_INSTR mulss, 1, 0, 1
1009 AVX_INSTR orpd, 1, 0, 1
1010 AVX_INSTR orps, 1, 0, 1
1011 AVX_INSTR packsswb, 0, 0, 0
1012 AVX_INSTR packssdw, 0, 0, 0
1013 AVX_INSTR packuswb, 0, 0, 0
1014 AVX_INSTR packusdw, 0, 0, 0
1015 AVX_INSTR paddb, 0, 0, 1
1016 AVX_INSTR paddw, 0, 0, 1
1017 AVX_INSTR paddd, 0, 0, 1
1018 AVX_INSTR paddq, 0, 0, 1
1019 AVX_INSTR paddsb, 0, 0, 1
1020 AVX_INSTR paddsw, 0, 0, 1
1021 AVX_INSTR paddusb, 0, 0, 1
1022 AVX_INSTR paddusw, 0, 0, 1
1023 AVX_INSTR palignr, 0, 1, 0
1024 AVX_INSTR pand, 0, 0, 1
1025 AVX_INSTR pandn, 0, 0, 0
1026 AVX_INSTR pavgb, 0, 0, 1
1027 AVX_INSTR pavgw, 0, 0, 1
1028 AVX_INSTR pblendvb, 0, 0, 0
1029 AVX_INSTR pblendw, 0, 1, 0
1030 AVX_INSTR pcmpestri, 0, 0, 0
1031 AVX_INSTR pcmpestrm, 0, 0, 0
1032 AVX_INSTR pcmpistri, 0, 0, 0
1033 AVX_INSTR pcmpistrm, 0, 0, 0
1034 AVX_INSTR pcmpeqb, 0, 0, 1
1035 AVX_INSTR pcmpeqw, 0, 0, 1
1036 AVX_INSTR pcmpeqd, 0, 0, 1
1037 AVX_INSTR pcmpeqq, 0, 0, 1
1038 AVX_INSTR pcmpgtb, 0, 0, 0
1039 AVX_INSTR pcmpgtw, 0, 0, 0
1040 AVX_INSTR pcmpgtd, 0, 0, 0
1041 AVX_INSTR pcmpgtq, 0, 0, 0
1042 AVX_INSTR phaddw, 0, 0, 0
1043 AVX_INSTR phaddd, 0, 0, 0
1044 AVX_INSTR phaddsw, 0, 0, 0
1045 AVX_INSTR phsubw, 0, 0, 0
1046 AVX_INSTR phsubd, 0, 0, 0
1047 AVX_INSTR phsubsw, 0, 0, 0
1048 AVX_INSTR pmaddwd, 0, 0, 1
1049 AVX_INSTR pmaddubsw, 0, 0, 0
1050 AVX_INSTR pmaxsb, 0, 0, 1
1051 AVX_INSTR pmaxsw, 0, 0, 1
1052 AVX_INSTR pmaxsd, 0, 0, 1
1053 AVX_INSTR pmaxub, 0, 0, 1
1054 AVX_INSTR pmaxuw, 0, 0, 1
1055 AVX_INSTR pmaxud, 0, 0, 1
1056 AVX_INSTR pminsb, 0, 0, 1
1057 AVX_INSTR pminsw, 0, 0, 1
1058 AVX_INSTR pminsd, 0, 0, 1
1059 AVX_INSTR pminub, 0, 0, 1
1060 AVX_INSTR pminuw, 0, 0, 1
1061 AVX_INSTR pminud, 0, 0, 1
1062 AVX_INSTR pmulhuw, 0, 0, 1
1063 AVX_INSTR pmulhrsw, 0, 0, 1
1064 AVX_INSTR pmulhw, 0, 0, 1
1065 AVX_INSTR pmullw, 0, 0, 1
1066 AVX_INSTR pmulld, 0, 0, 1
1067 AVX_INSTR pmuludq, 0, 0, 1
1068 AVX_INSTR pmuldq, 0, 0, 1
1069 AVX_INSTR por, 0, 0, 1
1070 AVX_INSTR psadbw, 0, 0, 1
1071 AVX_INSTR pshufb, 0, 0, 0
1072 AVX_INSTR psignb, 0, 0, 0
1073 AVX_INSTR psignw, 0, 0, 0
1074 AVX_INSTR psignd, 0, 0, 0
1075 AVX_INSTR psllw, 0, 0, 0
1076 AVX_INSTR pslld, 0, 0, 0
1077 AVX_INSTR psllq, 0, 0, 0
1078 AVX_INSTR pslldq, 0, 0, 0
1079 AVX_INSTR psraw, 0, 0, 0
1080 AVX_INSTR psrad, 0, 0, 0
1081 AVX_INSTR psrlw, 0, 0, 0
1082 AVX_INSTR psrld, 0, 0, 0
1083 AVX_INSTR psrlq, 0, 0, 0
1084 AVX_INSTR psrldq, 0, 0, 0
1085 AVX_INSTR psubb, 0, 0, 0
1086 AVX_INSTR psubw, 0, 0, 0
1087 AVX_INSTR psubd, 0, 0, 0
1088 AVX_INSTR psubq, 0, 0, 0
1089 AVX_INSTR psubsb, 0, 0, 0
1090 AVX_INSTR psubsw, 0, 0, 0
1091 AVX_INSTR psubusb, 0, 0, 0
1092 AVX_INSTR psubusw, 0, 0, 0
1093 AVX_INSTR punpckhbw, 0, 0, 0
1094 AVX_INSTR punpckhwd, 0, 0, 0
1095 AVX_INSTR punpckhdq, 0, 0, 0
1096 AVX_INSTR punpckhqdq, 0, 0, 0
1097 AVX_INSTR punpcklbw, 0, 0, 0
1098 AVX_INSTR punpcklwd, 0, 0, 0
1099 AVX_INSTR punpckldq, 0, 0, 0
1100 AVX_INSTR punpcklqdq, 0, 0, 0
1101 AVX_INSTR pxor, 0, 0, 1
1102 AVX_INSTR shufps, 1, 1, 0
1103 AVX_INSTR subpd, 1, 0, 0
1104 AVX_INSTR subps, 1, 0, 0
1105 AVX_INSTR subsd, 1, 0, 0
1106 AVX_INSTR subss, 1, 0, 0
1107 AVX_INSTR unpckhpd, 1, 0, 0
1108 AVX_INSTR unpckhps, 1, 0, 0
1109 AVX_INSTR unpcklpd, 1, 0, 0
1110 AVX_INSTR unpcklps, 1, 0, 0
1111 AVX_INSTR xorpd, 1, 0, 1
1112 AVX_INSTR xorps, 1, 0, 1
1113
1114 ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
1115 AVX_INSTR pfadd, 1, 0, 1
1116 AVX_INSTR pfsub, 1, 0, 0
1117 AVX_INSTR pfmul, 1, 0, 1
1118
1119 ; base-4 constants for shuffles
1120 %assign i 0
1121 %rep 256
1122     %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
1123     %if j < 10
1124         CAT_XDEFINE q000, j, i
1125     %elif j < 100
1126         CAT_XDEFINE q00, j, i
1127     %elif j < 1000
1128         CAT_XDEFINE q0, j, i
1129     %else
1130         CAT_XDEFINE q, j, i
1131     %endif
1132 %assign i i+1
1133 %endrep
1134 %undef i
1135 %undef j
1136
1137 %macro FMA_INSTR 3
1138     %macro %1 5-8 %1, %2, %3
1139         %if cpuflag(xop) || cpuflag(fma4)
1140             v%6 %1, %2, %3, %4
1141         %else
1142             %ifidn %1, %4
1143                 %7 %5, %2, %3
1144                 %8 %1, %4, %5
1145             %else
1146                 %7 %1, %2, %3
1147                 %8 %1, %4
1148             %endif
1149         %endif
1150     %endmacro
1151 %endmacro
1152
1153 FMA_INSTR  fmaddps,   mulps, addps
1154 FMA_INSTR  pmacsdd,  pmulld, paddd
1155 FMA_INSTR  pmacsww,  pmullw, paddw
1156 FMA_INSTR pmadcswd, pmaddwd, paddd