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