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