-
Notifications
You must be signed in to change notification settings - Fork 2
/
avx-builder.h
745 lines (626 loc) · 18.1 KB
/
avx-builder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <bfd.h>
#include <dis-asm.h>
#include <limits.h>
struct builder {
shader_t shader;
uint8_t *p;
/* Disassembly fields */
struct disassemble_info info;
disassembler_ftype disasm_fn;
int disasm_last;
int disasm_tail;
char disasm_output[128];
int disasm_length;
};
#define emit(bld, ...) \
do { \
uint8_t bytes[] = { __VA_ARGS__ }; \
const uint32_t length = ARRAY_LENGTH(bytes); \
for (uint32_t i = 0; i < length; i++) \
bld->p[i] = bytes[i]; \
bld->p += length; \
} while (0)
#define emit_uint16(u) \
((u) & 0xff), \
(((u) >> 8) & 0xff)
#define emit_uint32(u) \
((u) & 0xff), \
(((u) >> 8) & 0xff), \
(((u) >> 16) & 0xff), \
(((u) >> 24) & 0xff)
static inline void
builder_emit_push_rdi(struct builder *bld)
{
emit(bld, 0x57);
}
static inline void
builder_emit_pop_rdi(struct builder *bld)
{
emit(bld, 0x5f);
}
static inline void
builder_emit_load_rax_from_offset(struct builder *bld, uint32_t offset)
{
emit(bld, 0x48, 0x8b, 0x87, emit_uint32(offset));
}
static inline void
builder_emit_jmp_relative(struct builder *bld, int32_t offset)
{
emit(bld, 0xe9, emit_uint32(offset - 5));
}
static inline void
builder_emit_call_relative(struct builder *bld, int32_t offset)
{
emit(bld, 0xe8, emit_uint32(offset - 5));
}
static inline void
builder_emit_jmp_rip_relative(struct builder *bld, int32_t offset)
{
emit(bld, 0xff, 0x25, emit_uint32(offset - 6));
}
static inline void
builder_emit_call_rip_relative(struct builder *bld, int32_t offset)
{
emit(bld, 0xff, 0x15, emit_uint32(offset - 6));
}
static inline void
builder_emit_ret(struct builder *bld)
{
emit(bld, 0xc3);
}
static inline void *
builder_emit_jne(struct builder *bld)
{
void *p = bld->p;
emit(bld, 0x75, 0);
return p;
}
static inline void
builder_set_branch_target(struct builder *bld, uint8_t *branch, uint8_t *target)
{
int distance = target - (branch + 2);
ksim_assert((distance & ~0xff) == 0);
branch[1] = distance;
}
static inline void
builder_emit_m256i_load(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f, 0x87 + (dst & 7) * 0x08, emit_uint32(offset));
}
static inline void
builder_emit_m128i_load(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc5, 0xf9 - (dst & 8) * 16, 0x6f, 0x87 + (dst & 7) * 8, emit_uint32(offset));
}
static inline void
builder_emit_m256i_load_rip_relative(struct builder *bld, int dst, int32_t offset)
{
ksim_assert(dst < 16);
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f, 0x05 + (dst & 7) * 8, emit_uint32(offset - 8));
}
static inline void
builder_emit_vpmaskmovd(struct builder *bld, int src, int mask, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (src & 8) * 16, 0x7d - mask * 8, 0x8e, 0x87 + (src & 7) * 8,
emit_uint32(offset));
}
static inline void
builder_emit_vmovdqa(struct builder *bld, int dst, int src)
{
ksim_assert(dst < 16 && src < 16);
if (src < 8)
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f, 0xc0 + (src & 7) + (dst & 7) * 8);
else if (dst < 8)
emit(bld, 0xc5, 0x7d, 0x7f, 0xc0 + (src & 7) * 8 + (dst & 7));
else
emit(bld, 0xc4, 0x41, 0x7d, 0x6f, 0xc0 + (dst & 7) * 8 + (src & 7));
}
enum {
RAX,
RCX,
RDX,
RBX,
RSP,
RBP,
RSI,
RDI,
};
enum {
IMM_BYTE_OFFSET = 0x40,
IMM_DWORD_OFFSET = 0x80,
};
static bool
is_byte_range(int offset)
{
return -128 <= offset && offset <= 127;
}
static inline void
builder_emit_vmovdqa_from_rax(struct builder *bld, int dst, int offset)
{
/* vmovdqa offset(%rax),%ymm0 */
if (offset == 0)
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f,
(dst & 7) * 8 | RAX);
else if (is_byte_range(offset))
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f,
(dst & 7) * 8 | RAX | IMM_BYTE_OFFSET, offset);
else
emit(bld, 0xc5, 0xfd - (dst & 8) * 16, 0x6f,
(dst & 7) * 8 | RAX | IMM_DWORD_OFFSET,
emit_uint32(offset));
}
static inline void
builder_emit_vpmaskmovd_to_rax(struct builder *bld, int src, int mask, int offset)
{
/* vpmaskmovd %ymm0,%ymm1,(%rax) */
if (offset == 0)
emit(bld, 0xc4, 0xe2 - (src & 8) * 16, 0x7d - mask * 8, 0x8e,
(src & 7) * 8 | RAX);
else if (is_byte_range(offset))
emit(bld, 0xc4, 0xe2 - (src & 8) * 16, 0x7d - mask * 8, 0x8e,
(src & 7) * 8 | RAX | IMM_BYTE_OFFSET,
offset);
else
emit(bld, 0xc4, 0xe2 - (src & 8) * 16, 0x7d - mask * 8, 0x8e,
(src & 7) * 8 | RAX | IMM_DWORD_OFFSET,
emit_uint32(offset));
}
static inline void
builder_emit_m256i_store(struct builder *bld, int src, int32_t offset)
{
emit(bld, 0xc5, 0xfd - (src & 8) * 16, 0x7f, 0x87 + (src & 7) * 0x08, emit_uint32(offset));
}
static inline void
builder_emit_m128i_store(struct builder *bld, int src, int32_t offset)
{
emit(bld, 0xc5, 0xf9 - (src & 8) * 16, 0x7f, 0x87 + (src & 7) * 0x08, emit_uint32(offset));
}
static inline void
builder_emit_u32_store(struct builder *bld, int src, int32_t offset)
{
emit(bld, 0xc5, 0xf9 - (src & 8) * 16, 0x7e, 0x87 + (src & 7) * 0x08, emit_uint32(offset));
}
static inline void
builder_emit_vpbroadcastd(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x58, 0x87 + (dst & 7) * 8,
emit_uint32(offset));
}
static inline void
builder_emit_vpbroadcastd_rip_relative(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x58, 0x05 + (dst & 7) * 8,
emit_uint32(offset - 9));
}
static inline void
builder_emit_vpbroadcastw(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x79, 0x87 + (dst & 7) * 8,
emit_uint32(offset));
}
static inline void
builder_emit_vpbroadcastw_rip_relative(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x79, 0x05 + (dst & 7) * 8,
emit_uint32(offset - 9));
}
static inline void
builder_emit_vpbroadcastw_xmm(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x79, 0x79, 0x87 + (dst & 7) * 8,
emit_uint32(offset));
}
static inline void
builder_emit_vpbroadcastq(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x59, 0x87 + (dst & 7) * 8,
emit_uint16(offset));
}
static inline void
builder_emit_vbroadcasti128(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x5a, 0x87 + (dst & 7) * 8,
emit_uint32(offset));
}
static inline void
builder_emit_vbroadcasti128_rip_relative(struct builder *bld, int dst, int32_t offset)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16, 0x7d, 0x5a, 0x05 + (dst & 7) * 8,
emit_uint32(offset - 9));
}
static inline void
builder_emit_load_rsi_rip_relative(struct builder *bld, int offset)
{
emit(bld, 0x48, 0x8d, 0x35, emit_uint32(offset - 7));
}
static inline void
builder_emit_load_rsi(struct builder *bld, int offset)
{
emit(bld, 0x48, 0x8b, 0xb7, emit_uint32(offset));
}
static inline void
builder_emit_load_edi(struct builder *bld, uint32_t value)
{
emit(bld, 0xbf, emit_uint32(value));
}
static inline void
builder_emit_load_rax(struct builder *bld, uint32_t offset)
{
emit(bld, 0x48, 0x8b, 0x87, emit_uint32(offset));
}
static inline void
builder_emit_load_rax_rip_relative(struct builder *bld, uint32_t offset)
{
emit(bld, 0x48, 0x8b, 0x05, emit_uint32(offset - 7));
}
static inline void
builder_emit_vmovmskps(struct builder *bld, uint32_t src)
{
if (src < 8)
emit(bld, 0xc5, 0xfc, 0x50, 0xc0 + src);
else
emit(bld, 0xc4, 0xc1, 0x7c, 0x50, 0xc0 + src);
}
static inline void
builder_emit_long_alu(struct builder *bld, int opcode0, int opcode1, int dst, int src0, int src1)
{
ksim_assert(dst < 16 && src0 < 16 && src1 < 16);
if (src0 < 8)
emit(bld, 0xc5, (0xf0 | opcode0) - src1 * 8 - (dst & 8) * 16,
opcode1, 0xc0 + src0 + (dst & 7) * 8);
else
emit(bld, 0xc4, 0xc1 - (dst & 8) * 16, (0x70 | opcode0) - src1 * 8,
opcode1, 0xc0 + (src0 & 7) + (dst & 7) * 8);
}
static inline void
builder_emit_short_alu(struct builder *bld, int opcode, int dst, int src0, int src1)
{
emit(bld, 0xc4, 0xe2 - (src0 & 8) * 4 - (dst & 8) * 16, 0x7d - src1 * 8,
opcode, 0xc0 + (src0 & 7) + (dst & 7) * 8);
}
static inline void
builder_emit_vpgatherdd(struct builder *bld, int dst, int index, int mask, int scale, int offset)
{
const uint32_t opcode = 0x90;
const uint32_t scale_log2 = __builtin_ffs(scale) - 1;
ksim_assert(offset < 128);
if (offset == 0)
emit(bld, 0xc4, 0xe2 - (index & 8) * 8 - (dst & 8) * 16, 0x7d - mask * 8,
opcode, 0x04 + (dst & 7) * 8, (index & 7) * 8 + scale_log2 * 0x40);
else
emit(bld, 0xc4, 0xe2 - (index & 8) * 8 - (dst & 8) * 16, 0x7d - mask * 8,
opcode, 0x44 + (dst & 7) * 8, (index & 7) * 8 + scale_log2 * 0x40, offset);
}
static inline void
builder_emit_vpinsrq_rdi_relative(struct builder *bld, int dst, int src1, int offset, int idx)
{
int src0 = 0;
if (offset < 128)
emit(bld, 0xc4,
0xe3 - (src0 & 8) * 4 - (dst & 8) * 16,
0xf9 - src1 * 8,
0x22,
0x47 + (src0 & 7) + (dst & 7) * 8,
offset,
idx);
else
emit(bld, 0xc4,
0xe3 - (src0 & 8) * 4 - (dst & 8) * 16,
0xf9 - src1 * 8,
0x22,
0x87 + (src0 & 7) + (dst & 7) * 8,
emit_uint32(offset),
idx);
}
static inline void
builder_emit_vpinsrd_rdi_relative(struct builder *bld, int dst, int src1, int offset, int idx)
{
int src0 = 0;
if (offset < 128)
emit(bld, 0xc4,
0xe3 - (src0 & 8) * 4 - (dst & 8) * 16,
0x79 - src1 * 8,
0x22,
0x47 + (src0 & 7) + (dst & 7) * 8,
offset,
idx);
else
emit(bld, 0xc4,
0xe3 - (src0 & 8) * 4 - (dst & 8) * 16,
0x79 - src1 * 8,
0x22,
0x87 + (src0 & 7) + (dst & 7) * 8,
emit_uint32(offset),
idx);
}
static inline void
builder_emit_vinserti128(struct builder *bld, int dst, int src0, int src1, int idx)
{
emit(bld, 0xc4, 0xe3 - (src0 & 8) * 4 - (dst & 8) * 16, 0x7d - src1 * 8,
0x38, 0xc0 + (src0 & 7) + (dst & 7) * 8);
emit(bld, idx);
}
static inline void
builder_emit_vpaddd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xfe, dst, src0, src1);
}
static inline void
builder_emit_vpaddw(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xfd, dst, src0, src1);
}
static inline void
builder_emit_vpsubd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xfa, dst, src0, src1);
}
static inline void
builder_emit_vpmulld(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x40, dst, src0, src1);
}
static inline void
builder_emit_vpmullw(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xd5, dst, src0, src1);
}
static inline void
builder_emit_vaddps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x58, dst, src0, src1);
}
static inline void
builder_emit_vmulps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x59, dst, src0, src1);
}
static inline void
builder_emit_vdivps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x5e, dst, src0, src1);
}
static inline void
builder_emit_vsubps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x5c, dst, src0, src1);
}
static inline void
builder_emit_vpand(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xdb, dst, src0, src1);
}
static inline void
builder_emit_vpandn(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xdf, dst, src0, src1);
}
static inline void
builder_emit_vpxor(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xef, dst, src0, src1);
}
static inline void
builder_emit_vpor(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0xeb, dst, src0, src1);
}
static inline void
builder_emit_vpsrlvd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x45, dst, src0, src1);
}
static inline void
builder_emit_vpsravd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x46, dst, src0, src1);
}
static inline void
builder_emit_vpsllvd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x47, dst, src0, src1);
}
static inline void
builder_emit_vpsrld(struct builder *bld, int dst, int src0, int shift)
{
if (src0 < 8)
emit(bld, 0xc5, 0xfd - dst * 8, 0x72, 0xd0 + src0, shift);
else
emit(bld, 0xc4, 0xc1, 0x7d - dst * 8,
0x72, 0xd0 + (src0 & 7), shift);
}
static inline void
builder_emit_vpslld(struct builder *bld, int dst, int src0, int shift)
{
if (src0 < 8)
emit(bld, 0xc5, 0xfd - dst * 8, 0x72, 0xf0 + src0, shift);
else
emit(bld, 0xc4, 0xc1, 0x7d - dst * 8,
0x72, 0xf0 + (src0 & 7), shift);
}
/* For the vfmaddXYZps instructions, X and Y are multiplied, Z is
* added. 1, 2, 3 and refer to the three ymmX register sources, here
* dst, src0 and src1 (dst is a src too).
*/
static inline void
builder_emit_vfmadd132ps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x98, dst, src0, src1);
}
static inline void
builder_emit_vfmadd231ps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0xb8, dst, src0, src1);
}
static inline void
builder_emit_vfnmadd132ps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x9c, dst, src0, src1);
}
static inline void
builder_emit_vfnmadd231ps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0xbc, dst, src0, src1);
}
static inline void
builder_emit_vpabsd(struct builder *bld, int dst, int src0)
{
builder_emit_short_alu(bld, 0x1e, dst, src0, 0);
}
static inline void
builder_emit_vrsqrtps(struct builder *bld, int dst, int src0)
{
builder_emit_long_alu(bld, 0x0c, 0x52, dst, src0, 0);
}
static inline void
builder_emit_vsqrtps(struct builder *bld, int dst, int src0)
{
builder_emit_long_alu(bld, 0x0c, 0x51, dst, src0, 0);
}
static inline void
builder_emit_vrcpps(struct builder *bld, int dst, int src0)
{
builder_emit_long_alu(bld, 0x0c, 0x53, dst, src0, 0);
}
static inline void
builder_emit_vcmpps(struct builder *bld, int op, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0xc2, dst, src0, src1);
emit(bld, op);
}
static inline void
builder_emit_vpcmpeqd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0x76, dst, src0, src1);
}
static inline void
builder_emit_vpcmpgtd(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0d, 0x66, dst, src0, src1);
}
static inline void
builder_emit_vmaxps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x5f, dst, src0, src1);
}
static inline void
builder_emit_vminps(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x0c, 0x5d, dst, src0, src1);
}
static inline void
builder_emit_short_alu_e3(struct builder *bld, int opcode, int dst, int src0, int src1)
{
emit(bld, 0xc4, 0xe3 - (src0 & 8) * 4 - (dst & 8) * 16, 0x7d - src1 * 8,
opcode, 0xc0 + (src0 & 7) + (dst & 7) * 8);
}
static inline void
builder_emit_vpermilps(struct builder *bld, int dst, int imm, int src0)
{
int src1 = 0;
builder_emit_short_alu_e3(bld, 0x04, dst, src0, src1);
emit(bld, imm);
}
static inline void
builder_emit_vroundps(struct builder *bld, int dst, int op, int src1)
{
int src0 = 0;
builder_emit_short_alu_e3(bld, 0x08, dst, src0, src1);
emit(bld, op);
}
static inline void
builder_emit_vpblendvb(struct builder *bld, int dst, int mask, int src0, int src1)
{
builder_emit_short_alu_e3(bld, 0x4c, dst, src0, src1);
emit(bld, mask * 16);
}
static inline void
builder_emit_vpblendd(struct builder *bld, int dst, int mask, int src0, int src1)
{
builder_emit_short_alu_e3(bld, 0x02, dst, src0, src1);
emit(bld, mask); /* imm mask */
}
static inline void
builder_emit_vpblendvps(struct builder *bld, int dst, int mask, int src0, int src1)
{
builder_emit_short_alu_e3(bld, 0x4a, dst, src0, src1);
emit(bld, mask * 16); /* mask register */
}
static inline void
builder_emit_vpackusdw(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_short_alu(bld, 0x2b, dst, src0, src1);
}
static inline void
builder_emit_vpackssdw(struct builder *bld, int dst, int src0, int src1)
{
builder_emit_long_alu(bld, 0x09, 0x6b, dst, src0, src1);
}
static inline void
builder_emit_vpmovsxwd(struct builder *bld, int dst, int src)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16 - (src & 8) * 4,
0x7d, 0x23, 0xc0 + (dst & 7) * 8 + (src & 7));
}
static inline void
builder_emit_vpmovzxwd(struct builder *bld, int dst, int src)
{
emit(bld, 0xc4, 0xe2 - (dst & 8) * 16 - (src & 8) * 4,
0x7d, 0x33, 0xc0 + (dst & 7) * 8 + (src & 7));
}
static inline void
builder_emit_vextractf128(struct builder *bld, int dst, int src, int sel)
{
emit(bld, 0xc4, 0xe3, 0x79, 0x16, 0xc0 + dst + src * 8, sel);
}
static inline void
builder_emit_vpextrd(struct builder *bld, int src, int sel)
{
emit(bld, 0xc4, 0xe3 - (src & 8) * 16, 0x79, 0x16, 0xc0 | (src & 7) * 8 | RAX, sel);
}
static inline void
builder_emit_add_rax_rip_relative(struct builder *bld, uint32_t offset)
{
emit(bld, 0x48, 0x03, 0x05, emit_uint32(offset - 7));
}
static inline void
builder_emit_vcvtps2dq(struct builder *bld, int dst, int src)
{
builder_emit_long_alu(bld, 0x0d, 0x5b, dst, src, 0);
}
static inline void
builder_emit_vcvtdq2ps(struct builder *bld, int dst, int src)
{
builder_emit_long_alu(bld, 0x0c, 0x5b, dst, src, 0);
}
static inline uint32_t
builder_offset(struct builder *bld, void *p)
{
return p - (void *) bld->p;
}
static inline int
builder_emit_call(struct builder *bld, void *func)
{
builder_emit_push_rdi(bld);
builder_emit_call_relative(bld, (uint8_t *) func - bld->p);
builder_emit_pop_rdi(bld);
return 0;
}
static inline void
builder_emit_trap(struct builder *bld)
{
builder_emit_push_rdi(bld);
builder_emit_load_edi(bld, SIGTRAP);
const uint64_t offset = (uint8_t *) raise - bld->p;
ksim_assert(offset < INT_MAX);
builder_emit_call_relative(bld, offset);
builder_emit_pop_rdi(bld);
}
void
builder_init(struct builder *bld);
void
builder_align(struct builder *bld);
shader_t
builder_finish(struct builder *bld);
bool
builder_disasm(struct builder *bld);