FFmpeg
Loading...
Searching...
No Matches
ops_asmgen.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Ramiro Polla
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "ops_asmgen.h"
22
23/*********************************************************************/
24/* Helpers functions. */
25
26/* Looping when s->use_vh is set. */
27#define LOOP_VH(s, mask, idx) if (s->use_vh) LOOP(mask, idx)
28#define LOOP_MASK_VH(s, p, idx) if (s->use_vh) LOOP_MASK(p, idx)
29#define LOOP_MASK_BWD_VH(s, p, idx) if (s->use_vh) LOOP_MASK_BWD(p, idx)
30
31/* Inline rasm comments. */
32#define CMT(comment) rasm_annotate(r, comment)
33#define CMTF(fmt, ...) rasm_annotatef(r, (char[128]){0}, 128, fmt, __VA_ARGS__)
34
35/* Reshape input/output vector registers for current SwsOp. */
36static void reshape_io_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
37{
38 for (int i = 0; i < 4; i++) {
39 regs->sl[i] = a64op_make_vec(a64op_vec_n(regs->sl[i]), el_count, el_size);
40 regs->sh[i] = a64op_make_vec(a64op_vec_n(regs->sh[i]), el_count, el_size);
41 regs->dl[i] = a64op_make_vec(a64op_vec_n(regs->dl[i]), el_count, el_size);
42 regs->dh[i] = a64op_make_vec(a64op_vec_n(regs->dh[i]), el_count, el_size);
43 }
44}
45
46/* Reshape temp vector registers for current SwsOp. */
47static void reshape_temp_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
48{
49 for (int i = 0; i < FF_ARRAY_ELEMS(regs->vt); i++)
50 regs->vt[i] = a64op_make_vec(a64op_vec_n(regs->vt[i]), el_count, el_size);
51}
52
53/* Reshape const vector registers for current SwsOp. */
54static void reshape_const_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
55{
56 for (int i = 0; i < FF_ARRAY_ELEMS(regs->vk); i++)
57 regs->vk[i] = a64op_make_vec(a64op_vec_n(regs->vk[i]), el_count, el_size);
58}
59
60/*********************************************************************/
61/* Function frame */
62
63static unsigned clobbered_frame_size(unsigned n)
64{
65 return ((n + 1) >> 1) * 16;
66}
67
68static void asmgen_prologue(SwsAArch64Context *s, const RasmOp *regs, unsigned n)
69{
70 RasmContext *r = s->rctx;
71 RasmOp sp = a64op_sp();
72 unsigned frame_size = clobbered_frame_size(n);
73 RasmOp sp_pre = a64op_pre(sp, -frame_size);
74
75 rasm_add_comment(r, "prologue");
76 if (n == 0) {
77 /* no-op */
78 } else if (n == 1) {
79 i_str(r, regs[0], sp_pre);
80 } else {
81 i_stp(r, regs[0], regs[1], sp_pre);
82 for (unsigned i = 2; i + 1 < n; i += 2)
83 i_stp(r, regs[i], regs[i + 1], a64op_off(sp, i * sizeof(uint64_t)));
84 if (n & 1)
85 i_str(r, regs[n - 1], a64op_off(sp, (n - 1) * sizeof(uint64_t)));
86 }
87}
88
89static void asmgen_epilogue(SwsAArch64Context *s, const RasmOp *regs, unsigned n)
90{
91 RasmContext *r = s->rctx;
92 RasmOp sp = a64op_sp();
93 unsigned frame_size = clobbered_frame_size(n);
94 RasmOp sp_post = a64op_post(sp, frame_size);
95
96 rasm_add_comment(r, "epilogue");
97 if (n == 0) {
98 /* no-op */
99 } else if (n == 1) {
100 i_ldr(r, regs[0], sp_post);
101 } else {
102 if (n & 1)
103 i_ldr(r, regs[n - 1], a64op_off(sp, (n - 1) * sizeof(uint64_t)));
104 for (unsigned i = (n & ~1u) - 2; i >= 2; i -= 2)
105 i_ldp(r, regs[i], regs[i + 1], a64op_off(sp, i * sizeof(uint64_t)));
106 i_ldp(r, regs[0], regs[1], sp_post);
107 }
108}
109
110/*********************************************************************/
111/* Callee-saved registers (r19-r28, fp, and lr). */
112#define MAX_SAVED_REGS 12
113
114static void clobber_gpr(RasmOp regs[MAX_SAVED_REGS], unsigned *count,
115 RasmOp gpr)
116{
117 const int n = a64op_gpr_n(gpr);
118 if (n >= 19 && n <= 30)
119 regs[(*count)++] = gpr;
120}
121
122static unsigned clobbered_gprs(const SwsAArch64Context *s,
123 SwsCompMask imask, SwsCompMask omask,
125{
126 unsigned count = 0;
127 clobber_gpr(regs, &count, a64op_lr());
128 LOOP(imask, i) {
129 clobber_gpr(regs, &count, s->in[i]);
130 clobber_gpr(regs, &count, s->in_bump[i]);
131 }
132 LOOP(omask, i) {
133 clobber_gpr(regs, &count, s->out[i]);
134 clobber_gpr(regs, &count, s->out_bump[i]);
135 }
136 return count;
137}
138
140{
141 RasmContext *r = s->rctx;
142
143 /**
144 * The process function for aarch64 works similarly to the x86 backend.
145 * The description in x86/ops_include.asm mostly holds as well here.
146 */
147
148 /* Function prologue */
149 RasmOp saved_regs[MAX_SAVED_REGS];
150 unsigned nsaved = clobbered_gprs(s, imask, omask, saved_regs);
151 if (nsaved)
152 asmgen_prologue(s, saved_regs, nsaved);
153
154 /* Load values from exec. */
155 RasmOp exec_in[4];
156 RasmOp exec_in_bump[4];
157 RasmOp exec_out[4];
158 RasmOp exec_out_bump[4];
159 LOOP(imask, i) { exec_in [i] = a64op_off(s->exec, offsetof_exec_in + (i * sizeof(uint8_t *))); }
160 LOOP(imask, i) { exec_in_bump [i] = a64op_off(s->exec, offsetof_exec_in_bump + (i * sizeof(uint8_t *))); }
161 LOOP(omask, i) { exec_out [i] = a64op_off(s->exec, offsetof_exec_out + (i * sizeof(uint8_t *))); }
162 LOOP(omask, i) { exec_out_bump[i] = a64op_off(s->exec, offsetof_exec_out_bump + (i * sizeof(uint8_t *))); }
163 LOOP(imask, i) { i_ldr(r, s->in[i], exec_in [i]); CMTF("in[%u] = exec->in[%u];", i, i); }
164 LOOP(omask, i) { i_ldr(r, s->out[i], exec_out[i]); CMTF("out[%u] = exec->out[%u];", i, i); }
165 LOOP(imask, i) { i_ldr(r, s->in_bump[i], exec_in_bump[i]); CMTF("in_bump[%u] = exec->in_bump[%u];", i, i); }
166 LOOP(omask, i) { i_ldr(r, s->out_bump[i], exec_out_bump[i]); CMTF("out_bump[%u] = exec->out_bump[%u];", i, i); }
167
168 /* Setup. */
169 s->setup = rasm_get_current_node(r);
170
171 int first_row = rasm_new_label(r, NULL);
172 int next_row = rasm_new_label(r, NULL);
173 int next_block = rasm_new_label(r, NULL);
174
175 /* Jump to first row (skips padding). */
176 i_b (r, rasm_op_label(first_row)); CMT("goto first_row;");
177
178 /* Perform padding, preparing for next row. */
179 rasm_add_label(r, next_row); CMT("next_row:");
180 LOOP(imask, i) { i_add(r, s->in[i], s->in[i], s->in_bump[i]); CMTF("in[%u] += in_bump[%u];", i, i); }
181 LOOP(omask, i) { i_add(r, s->out[i], s->out[i], s->out_bump[i]); CMTF("out[%u] += out_bump[%u];", i, i); }
182
183 /* First row (reset x). */
184 rasm_add_label(r, first_row); CMT("first_row:");
185 i_mov(r, s->bx, s->bx_start); CMT("bx = bx_start;");
186
187 /* Main loop. */
188 rasm_add_label(r, next_block); CMT("next_block:");
189 s->loop = rasm_get_current_node(r);
190
191 /* Perform horizontal loop. */
192 i_add(r, s->bx, s->bx, IMM(1)); CMT("bx += 1;");
193 i_cmp(r, s->bx, s->bx_end); CMT("if (bx != bx_end)");
194 i_bne(r, next_block); CMT(" goto next_block;");
195
196 /* Perform vertical loop. */
197 i_add(r, s->y, s->y, IMM(1)); CMT("y += 1;");
198 i_cmp(r, s->y, s->y_end); CMT("if (y != y_end)");
199 i_bne(r, next_row); CMT(" goto next_row;");
200
201 /* Function epilogue */
202 if (nsaved)
203 asmgen_epilogue(s, saved_regs, nsaved);
204
205 i_ret(r);
206}
207
208/*********************************************************************/
209/* SWS_UOP_READ_BIT: fractional read (1 bit) from single plane */
210/* SWS_UOP_READ_NIBBLE: fractional read (4 bits) from single plane */
211/* SWS_UOP_READ_PACKED: simple packed byte-aligned read */
212/* SWS_UOP_READ_PLANAR: simple planar byte-aligned read */
213
215 SwsAArch64OpRegs *regs)
216{
217 RasmContext *r = s->rctx;
218 AArch64VecViews dl[1] = { a64op_vec_views(regs->dl[0]) };
219 AArch64VecViews shift_vec = a64op_vec_views(regs->vk[0]);
220 AArch64VecViews bitmask_vec = a64op_vec_views(regs->vk[1]);
221
222 AArch64VecViews vtmp = a64op_vec_views(regs->vt[0]);
223 RasmOp wtmp = a64op_w(s->tmp0);
224
225 /* Note that shift_vec has negative values, so that using it with
226 * ushl actually performs a right shift. */
227 if (p->block_size == 16) {
228 i_ldrh(r, wtmp, a64op_post(s->in[0], 2)); CMT("uint16_t tmp = *in[0]++;");
229 i_dup (r, dl[0].b8, wtmp); CMT("vl[0].lo = broadcast(tmp);");
230 i_lsr (r, wtmp, wtmp, IMM(8)); CMT("tmp >>= 8;");
231 i_dup (r, vtmp.b8, wtmp); CMT("vtmp.lo = broadcast(tmp);");
232 i_ins (r, dl[0].de[1], vtmp.de[0]); CMT("vl[0].hi = vtmp.lo;");
233 i_ushl(r, dl[0].b16, dl[0].b16, shift_vec.b16); CMT("vl[0] <<= shift_vec;");
234 i_and (r, dl[0].b16, dl[0].b16, bitmask_vec.b16); CMT("vl[0] &= bitmask_vec;");
235 } else {
236 i_ldrb(r, wtmp, a64op_post(s->in[0], 1)); CMT("uint8_t tmp = *in[0]++;");
237 i_dup (r, dl[0].b8, wtmp); CMT("vl[0].lo = broadcast(tmp);");
238 i_ushl(r, dl[0].b8, dl[0].b8, shift_vec.b8); CMT("vl[0] <<= shift_vec;");
239 i_and (r, dl[0].b8, dl[0].b8, bitmask_vec.b8); CMT("vl[0] &= bitmask_vec;");
240 }
241}
242
244 SwsAArch64OpRegs *regs)
245{
246 RasmContext *r = s->rctx;
247 AArch64VecViews dl[1] = { a64op_vec_views(regs->dl[0]) };
249
250 AArch64VecViews vtmp = a64op_vec_views(regs->vt[0]);
251
252 if (p->block_size == 8) {
253 i_ldr (r, dl[0].s, a64op_post(s->in[0], 4)); CMT("vl[0] = *in[0]++;");
254 i_ushr(r, vtmp.b8, dl[0].b8, IMM(4)); CMT("vtmp.lo = vl[0] >> 4;");
255 i_and (r, dl[0].b8, dl[0].b8, nibble_mask.b8); CMT("vl[0].lo &= nibble_mask;");
256 i_zip1(r, dl[0].b8, vtmp.b8, dl[0].b8); CMT("interleave");
257 } else {
258 i_ldr (r, dl[0].d, a64op_post(s->in[0], 8)); CMT("vl[0] = *in[0]++;");
259 i_ushr(r, vtmp.b8, dl[0].b8, IMM(4)); CMT("vtmp.lo = vl[0] >> 4;");
260 i_and (r, dl[0].b8, dl[0].b8, nibble_mask.b8); CMT("vl[0].lo &= nibble_mask;");
261 i_zip1(r, dl[0].b16, vtmp.b16, dl[0].b16); CMT("interleave");
262 }
263}
264
266{
267 RasmContext *r = s->rctx;
268
269 switch (p->mask) {
270 case SWS_COMP_ELEMS(2): i_ld2(r, vv_2(vx[0], vx[1]), a64op_post(s->in[0], s->vec_size * 2)); break;
271 case SWS_COMP_ELEMS(3): i_ld3(r, vv_3(vx[0], vx[1], vx[2]), a64op_post(s->in[0], s->vec_size * 3)); break;
272 case SWS_COMP_ELEMS(4): i_ld4(r, vv_4(vx[0], vx[1], vx[2], vx[3]), a64op_post(s->in[0], s->vec_size * 4)); break;
273 }
274}
275
277 SwsAArch64OpRegs *regs)
278{
279 av_assert0(p->mask != 0x0001);
280 asmgen_op_read_packed_n(s, p, regs->dl);
281 if (s->use_vh)
282 asmgen_op_read_packed_n(s, p, regs->dh);
283}
284
286 SwsAArch64OpRegs *regs)
287{
288 RasmContext *r = s->rctx;
290 AArch64VecViews dh[4] = A64OP_VEC_VIEWS4(regs->dh);
291
292 LOOP_MASK(p, i) {
293 switch ((s->use_vh ? 0x100 : 0) | s->vec_size) {
294 case 0x008: i_ldr(r, dl[i].d, a64op_post(s->in[i], s->vec_size * 1)); break;
295 case 0x010: i_ldr(r, dl[i].q, a64op_post(s->in[i], s->vec_size * 1)); break;
296 case 0x108: i_ldp(r, dl[i].d, dh[i].d, a64op_post(s->in[i], s->vec_size * 2)); break;
297 case 0x110: i_ldp(r, dl[i].q, dh[i].q, a64op_post(s->in[i], s->vec_size * 2)); break;
298 }
299 }
300}
301
302/*********************************************************************/
303/* SWS_UOP_WRITE_BIT: fractional write (1 bit) to single plane */
304/* SWS_UOP_WRITE_NIBBLE: fractional write (4 bits) to single plane */
305/* SWS_UOP_WRITE_PACKED: simple packed byte-aligned write */
306/* SWS_UOP_WRITE_PLANAR: simple planar byte-aligned write */
307
309 SwsAArch64OpRegs *regs)
310{
311 RasmContext *r = s->rctx;
312 AArch64VecViews sl[1] = { a64op_vec_views(regs->sl[0]) };
313 AArch64VecViews shift_vec = a64op_vec_views(regs->vk[0]);
314
315 AArch64VecViews vtmp0 = a64op_vec_views(regs->vt[0]);
316 AArch64VecViews vtmp1 = a64op_vec_views(regs->vt[1]);
317
318 if (p->block_size == 8) {
319 i_ushl(r, sl[0].b8, sl[0].b8, shift_vec.b8); CMT("vl[0] <<= shift_vec;");
320 i_addv(r, vtmp0.b, sl[0].b8); CMT("vtmp0[0] = add_across(vl[0].lo);");
321 i_str (r, vtmp0.b, a64op_post(s->out[0], 1)); CMT("*out[0]++ = vtmp0;");
322 } else {
323 i_ushl(r, sl[0].b16, sl[0].b16, shift_vec.b16); CMT("vl[0] <<= shift_vec;");
324 i_addv(r, vtmp0.b, sl[0].b8); CMT("vtmp0[0] = add_across(vl[0].lo);");
325 i_ins (r, vtmp1.de[0], sl[0].de[1]); CMT("vtmp1.lo = vl[0].hi;");
326 i_addv(r, vtmp1.b, vtmp1.b8); CMT("vtmp1[0] = add_across(vtmp1);");
327 i_ins (r, vtmp0.be[1], vtmp1.be[0]); CMT("vtmp0[1] = vtmp1[0];");
328 i_str (r, vtmp0.h, a64op_post(s->out[0], 2)); CMT("*out[0]++ = vtmp0;");
329 }
330}
331
333 SwsAArch64OpRegs *regs)
334{
335 RasmContext *r = s->rctx;
336 AArch64VecViews sl[4] = A64OP_VEC_VIEWS4(regs->sl);
337 AArch64VecViews vtmp0 = a64op_vec_views(regs->vt[0]);
338 AArch64VecViews vtmp1 = a64op_vec_views(regs->vt[1]);
339
340 if (p->block_size == 8) {
341 i_shl (r, vtmp0.h4, sl[0].h4, IMM(4));
342 i_ushr(r, vtmp1.h4, sl[0].h4, IMM(8));
343 i_orr (r, sl[0].b8, vtmp0.b8, vtmp1.b8);
344 i_xtn (r, vtmp0.b8, sl[0].h8);
345 i_str (r, vtmp0.s, a64op_post(s->out[0], 4));
346 } else {
347 i_shl (r, vtmp0.h8, sl[0].h8, IMM(4));
348 i_ushr(r, vtmp1.h8, sl[0].h8, IMM(8));
349 i_orr (r, sl[0].b16, vtmp0.b16, vtmp1.b16);
350 i_xtn (r, vtmp0.b8, sl[0].h8);
351 i_str (r, vtmp0.d, a64op_post(s->out[0], 8));
352 }
353}
354
356{
357 RasmContext *r = s->rctx;
358
359 switch (p->mask) {
360 case SWS_COMP_ELEMS(2): i_st2(r, vv_2(vx[0], vx[1]), a64op_post(s->out[0], s->vec_size * 2)); break;
361 case SWS_COMP_ELEMS(3): i_st3(r, vv_3(vx[0], vx[1], vx[2]), a64op_post(s->out[0], s->vec_size * 3)); break;
362 case SWS_COMP_ELEMS(4): i_st4(r, vv_4(vx[0], vx[1], vx[2], vx[3]), a64op_post(s->out[0], s->vec_size * 4)); break;
363 }
364}
365
367 SwsAArch64OpRegs *regs)
368{
369 av_assert0(p->mask != 0x0001);
370 asmgen_op_write_packed_n(s, p, regs->sl);
371 if (s->use_vh)
372 asmgen_op_write_packed_n(s, p, regs->sh);
373}
374
376 SwsAArch64OpRegs *regs)
377{
378 RasmContext *r = s->rctx;
379 AArch64VecViews sl[4] = A64OP_VEC_VIEWS4(regs->sl);
380 AArch64VecViews sh[4] = A64OP_VEC_VIEWS4(regs->sh);
381
382 LOOP_MASK(p, i) {
383 switch ((s->use_vh ? 0x100 : 0) | s->vec_size) {
384 case 0x008: i_str(r, sl[i].d, a64op_post(s->out[i], s->vec_size * 1)); break;
385 case 0x010: i_str(r, sl[i].q, a64op_post(s->out[i], s->vec_size * 1)); break;
386 case 0x108: i_stp(r, sl[i].d, sh[i].d, a64op_post(s->out[i], s->vec_size * 2)); break;
387 case 0x110: i_stp(r, sl[i].q, sh[i].q, a64op_post(s->out[i], s->vec_size * 2)); break;
388 }
389 }
390}
391
392/*********************************************************************/
393/* SWS_UOP_SWAP_BYTES: swap byte order in components */
394
396 SwsAArch64OpRegs *regs)
397{
398 RasmContext *r = s->rctx;
399 AArch64VecViews sl[4] = A64OP_VEC_VIEWS4(regs->sl);
400 AArch64VecViews sh[4] = A64OP_VEC_VIEWS4(regs->sh);
402 AArch64VecViews dh[4] = A64OP_VEC_VIEWS4(regs->dh);
403
404 switch (ff_sws_pixel_type_size(p->type)) {
405 case sizeof(uint16_t):
406 LOOP_MASK (p, i) i_rev16(r, dl[i].b16, sl[i].b16);
407 LOOP_MASK_VH(s, p, i) i_rev16(r, dh[i].b16, sh[i].b16);
408 break;
409 case sizeof(uint32_t):
410 LOOP_MASK (p, i) i_rev32(r, dl[i].b16, sl[i].b16);
411 LOOP_MASK_VH(s, p, i) i_rev32(r, dh[i].b16, sh[i].b16);
412 break;
413 }
414}
415
416/*********************************************************************/
417/* SWS_UOP_PERMUTE: permute pointers (no duplicates) */
418/* SWS_UOP_COPY: permute data (may contain duplicates) */
419
420static const char *print_swizzle_v(char buf[8], int8_t n, uint8_t vh)
421{
422 if (n == -1)
423 snprintf(buf, sizeof(char[8]), "vtmp%c", vh ? 'h' : 'l');
424 else
425 snprintf(buf, sizeof(char[8]), "v%c[%u]", vh ? 'h' : 'l', n);
426 return buf;
427}
428#define PRINT_SWIZZLE_V(n, vh) print_swizzle_v((char[8]){ 0 }, n, vh)
429
430static RasmOp swizzle_a64op(SwsAArch64OpRegs *regs, int8_t n, uint8_t vh, bool dst)
431{
432 if (n == -1)
433 return regs->vt[vh];
434 if (vh)
435 return dst ? regs->dh[n] : regs->sh[n];
436 return dst ? regs->dl[n] : regs->sl[n];
437}
438
440 int8_t dst, int8_t src)
441{
442 RasmContext *r = s->rctx;
443 RasmOp src_op[2] = { swizzle_a64op(regs, src, 0, false), swizzle_a64op(regs, src, 1, false) };
444 RasmOp dst_op[2] = { swizzle_a64op(regs, dst, 0, true), swizzle_a64op(regs, dst, 1, true) };
445
446 i_mov (r, dst_op[0], src_op[0]); CMTF("%s = %s;", PRINT_SWIZZLE_V(dst, 0), PRINT_SWIZZLE_V(src, 0));
447 if (s->use_vh) {
448 i_mov(r, dst_op[1], src_op[1]); CMTF("%s = %s;", PRINT_SWIZZLE_V(dst, 1), PRINT_SWIZZLE_V(src, 1));
449 }
450}
451
453 SwsAArch64OpRegs *regs)
454{
455 for (int i = 0; i < p->par.move.num_moves; i++)
456 swizzle_emit(s, regs, p->par.move.dst[i], p->par.move.src[i]);
457}
458
459/*********************************************************************/
460/* SWS_UOP_UNPACK: mask = nonzero components in pack pattern */
461
463 SwsAArch64OpRegs *regs)
464{
465 RasmContext *r = s->rctx;
466 RasmOp *sl = regs->sl;
467 RasmOp *sh = regs->sh;
468 RasmOp *dl = regs->dl;
469 RasmOp *dh = regs->dh;
470 RasmOp *vmask = regs->vk;
471
472 const int offsets[4] = {
473 p->par.pack.pattern[3] + p->par.pack.pattern[2] + p->par.pack.pattern[1],
474 p->par.pack.pattern[3] + p->par.pack.pattern[2],
475 p->par.pack.pattern[3],
476 0
477 };
478
479 /* Loop backwards to avoid clobbering component 0. */
480 LOOP_MASK_BWD (p, i) {
481 if (offsets[i]) {
482 i_ushr (r, dl[i], sl[0], IMM(offsets[i])); CMTF("vl[%u] >>= %u;", i, offsets[i]);
483 } else if (i) {
484 i_mov16b(r, dl[i], sl[0]); CMTF("vl[%u] = vl[0];", i);
485 }
486 }
487 LOOP_MASK_BWD_VH(s, p, i) {
488 if (offsets[i]) {
489 i_ushr (r, dh[i], sh[0], IMM(offsets[i])); CMTF("vh[%u] >>= %u;", i, offsets[i]);
490 } else if (i) {
491 i_mov16b(r, dh[i], sh[0]); CMTF("vh[%u] = vh[0];", i);
492 }
493 }
494
495 /* Apply masks. */
496 LOOP_MASK_BWD (p, i) { i_and16b(r, dl[i], dl[i], vmask[i]); CMTF("vl[%u] &= 0x%x;", i, (1u << p->par.pack.pattern[i]) - 1); }
497 LOOP_MASK_BWD_VH(s, p, i) { i_and16b(r, dh[i], dh[i], vmask[i]); CMTF("vh[%u] &= 0x%x;", i, (1u << p->par.pack.pattern[i]) - 1); }
498}
499
500/*********************************************************************/
501/* SWS_UOP_PACK: mask = nonzero components in pack pattern */
502
504 SwsAArch64OpRegs *regs)
505{
506 RasmContext *r = s->rctx;
507 RasmOp *sl = regs->sl;
508 RasmOp *sh = regs->sh;
509 RasmOp *dl = regs->dl;
510 RasmOp *dh = regs->dh;
511
512 const int offsets[4] = {
513 p->par.pack.pattern[3] + p->par.pack.pattern[2] + p->par.pack.pattern[1],
514 p->par.pack.pattern[3] + p->par.pack.pattern[2],
515 p->par.pack.pattern[3],
516 0
517 };
518 SwsCompMask offset_mask = 0;
519 LOOP_MASK(p, i) {
520 if (offsets[i])
521 offset_mask |= SWS_COMP(i);
522 }
523
524 /* Perform left shift. */
525 LOOP (offset_mask, i) { i_shl(r, dl[i], sl[i], IMM(offsets[i])); CMTF("vl[%u] <<= %u;", i, offsets[i]); }
526 LOOP_VH(s, offset_mask, i) { i_shl(r, dh[i], sh[i], IMM(offsets[i])); CMTF("vh[%u] <<= %u;", i, offsets[i]); }
527 LOOP (offset_mask, i) { sl[i] = dl[i]; }
528 LOOP_VH(s, offset_mask, i) { sh[i] = dh[i]; }
529
530 /* Combine components. */
531 for (int i = 0; i < 4; i++) {
532 sl[i] = v_16b(sl[i]);
533 sh[i] = v_16b(sh[i]);
534 dl[i] = v_16b(dl[i]);
535 dh[i] = v_16b(dh[i]);
536 }
537 LOOP_MASK (p, i) {
538 if (i != 0) {
539 i_orr16b (r, dl[0], sl[0], sl[i]); CMTF("vl[0] |= vl[%u];", i);
540 if (s->use_vh) {
541 i_orr16b(r, dh[0], sh[0], sh[i]); CMTF("vh[0] |= vh[%u];", i);
542 }
543 }
544 }
545}
546
547/*********************************************************************/
548/* SWS_UOP_LSHIFT: mask = components to shift */
549
551 SwsAArch64OpRegs *regs)
552{
553 uint8_t shift = p->par.shift.amount;
554 RasmContext *r = s->rctx;
555 RasmOp *sl = regs->sl;
556 RasmOp *sh = regs->sh;
557 RasmOp *dl = regs->dl;
558 RasmOp *dh = regs->dh;
559
560 LOOP_MASK (p, i) { i_shl(r, dl[i], sl[i], IMM(shift)); CMTF("vl[%u] <<= %u;", i, shift); }
561 LOOP_MASK_VH(s, p, i) { i_shl(r, dh[i], sh[i], IMM(shift)); CMTF("vh[%u] <<= %u;", i, shift); }
562}
563
564/*********************************************************************/
565/* SWS_UOP_RSHIFT: mask = components to shift */
566
568 SwsAArch64OpRegs *regs)
569{
570 uint8_t shift = p->par.shift.amount;
571 RasmContext *r = s->rctx;
572 RasmOp *sl = regs->sl;
573 RasmOp *sh = regs->sh;
574 RasmOp *dl = regs->dl;
575 RasmOp *dh = regs->dh;
576
577 LOOP_MASK (p, i) { i_ushr(r, dl[i], sl[i], IMM(shift)); CMTF("vl[%u] >>= %u;", i, shift); }
578 LOOP_MASK_VH(s, p, i) { i_ushr(r, dh[i], sh[i], IMM(shift)); CMTF("vh[%u] >>= %u;", i, shift); }
579}
580
581/*********************************************************************/
582/* SWS_UOP_CLEAR: mask = components to clear */
583
585 RasmOp *vx, RasmOp *vk, int i, const char *vx_str)
586{
587 RasmContext *r = s->rctx;
588 RasmOp clear_vec = vk[0];
589 if (p->par.clear.zero & SWS_COMP(i)) {
590 i_movi(r, vx[i], IMM(0)); CMTF("%s[%u] = 0;", vx_str, i);
591 } else if (p->par.clear.one & SWS_COMP(i)) {
592 if (p->block_size * ff_sws_pixel_type_size(p->type) == 8) {
593 i_movi(r, v_8b (vx[i]), IMM(0xff));
594 } else {
595 i_movi(r, v_16b(vx[i]), IMM(0xff));
596 }
597 CMTF("%s[%u] = UINT_MAX;", vx_str, i);
598 } else {
599 i_dup (r, vx[i], a64op_elem(clear_vec, i)); CMTF("%s[%u] = broadcast(clear_vec[%u]);", vx_str, i, i);
600 }
601}
602
604 SwsAArch64OpRegs *regs)
605{
606 RasmOp *dl = regs->dl;
607 RasmOp *dh = regs->dh;
608 RasmOp *vk = regs->vk;
609
610 LOOP_MASK (p, i) { emit_clear(s, p, dl, vk, i, "vl"); }
611 LOOP_MASK_VH(s, p, i) { emit_clear(s, p, dh, vk, i, "vh"); }
612}
613
614/*********************************************************************/
615/* SWS_UOP_TO_U8: cast pixel values to SWS_PIXEL_U8 */
616/* SWS_UOP_TO_U16: cast pixel values to SWS_PIXEL_U16 */
617/* SWS_UOP_TO_U32: cast pixel values to SWS_PIXEL_U32 */
618/* SWS_UOP_TO_F32: cast pixel values to SWS_PIXEL_F32 */
619
621 SwsAArch64OpRegs *regs)
622{
623 RasmContext *r = s->rctx;
624 AArch64VecViews sl[4] = A64OP_VEC_VIEWS4(regs->sl);
625 AArch64VecViews sh[4] = A64OP_VEC_VIEWS4(regs->sh);
627 AArch64VecViews dh[4] = A64OP_VEC_VIEWS4(regs->dh);
628
629 /**
630 * Since each instruction in the convert operation needs specific
631 * element types, it is simpler to use arrangement specifiers for
632 * each operand instead of reshaping all vectors.
633 */
634
635 size_t src_el_size = s->el_size;
636 SwsPixelType to_type;
637 switch (p->uop) {
638 case SWS_UOP_TO_U8: to_type = SWS_PIXEL_U8; break;
639 case SWS_UOP_TO_U16: to_type = SWS_PIXEL_U16; break;
640 case SWS_UOP_TO_U32: to_type = SWS_PIXEL_U32; break;
641 case SWS_UOP_TO_F32: to_type = SWS_PIXEL_F32; break;
642 default:
643 av_assert0(!"Invalid uop!");
644 break;
645 }
646 size_t dst_el_size = ff_sws_pixel_type_size(to_type);
647
648 /**
649 * This function assumes block_size is either 8 or 16, and that
650 * we're always using the most amount of vector registers possible.
651 * Therefore, u32 always uses the high vector bank.
652 */
653 if (p->type == SWS_PIXEL_F32) {
654 rasm_add_comment(r, "f32 -> u32");
655 LOOP_MASK(p, i) i_fcvtzu(r, dl[i].s4, sl[i].s4);
656 LOOP_MASK(p, i) i_fcvtzu(r, dh[i].s4, sh[i].s4);
657 memcpy(sl, dl, sizeof(sl));
658 memcpy(sh, dh, sizeof(sh));
659 }
660
661 if (p->block_size == 8) {
662 if (src_el_size == 1 && dst_el_size > src_el_size) {
663 rasm_add_comment(r, "u8 -> u16");
664 LOOP_MASK(p, i) i_uxtl (r, dl[i].h8, sl[i].b8);
665 memcpy(sl, dl, sizeof(sl));
666 src_el_size = 2;
667 } else if (src_el_size == 4 && dst_el_size < src_el_size) {
668 rasm_add_comment(r, "u32 -> u16");
669 LOOP_MASK(p, i) i_xtn (r, dl[i].h4, sl[i].s4);
670 LOOP_MASK(p, i) i_xtn (r, dh[i].h4, sh[i].s4);
671 LOOP_MASK(p, i) i_ins (r, dl[i].de[1], sh[i].de[0]);
672 memcpy(sl, dl, sizeof(sl));
673 memcpy(sh, dh, sizeof(sh));
674 src_el_size = 2;
675 }
676 if (src_el_size == 2 && dst_el_size == 4) {
677 rasm_add_comment(r, "u16 -> u32");
678 LOOP_MASK(p, i) i_uxtl2(r, dh[i].s4, sl[i].h8);
679 LOOP_MASK(p, i) i_uxtl (r, dl[i].s4, sl[i].h4);
680 memcpy(sl, dl, sizeof(sl));
681 memcpy(sh, dh, sizeof(sh));
682 src_el_size = 4;
683 } else if (src_el_size == 2 && dst_el_size == 1) {
684 rasm_add_comment(r, "u16 -> u8");
685 LOOP_MASK(p, i) i_xtn (r, dl[i].b8, sl[i].h8);
686 memcpy(sl, dl, sizeof(sl));
687 src_el_size = 1;
688 }
689 } else /* if (p->block_size == 16) */ {
690 if (src_el_size == 1 && dst_el_size == 2) {
691 rasm_add_comment(r, "u8 -> u16");
692 LOOP_MASK(p, i) i_uxtl2(r, dh[i].h8, sl[i].b16);
693 LOOP_MASK(p, i) i_uxtl (r, dl[i].h8, sl[i].b8);
694 memcpy(sl, dl, sizeof(sl));
695 memcpy(sh, dh, sizeof(sh));
696 } else if (src_el_size == 2 && dst_el_size == 1) {
697 rasm_add_comment(r, "u16 -> u8");
698 LOOP_MASK(p, i) i_xtn (r, dl[i].b8, sl[i].h8);
699 LOOP_MASK(p, i) i_xtn (r, dh[i].b8, sh[i].h8);
700 LOOP_MASK(p, i) i_ins (r, dl[i].de[1], sh[i].de[0]);
701 memcpy(sl, dl, sizeof(sl));
702 }
703 }
704
705 /* See comment above for high vector bank usage for u32. */
706 if (to_type == SWS_PIXEL_F32) {
707 rasm_add_comment(r, "u32 -> f32");
708 LOOP_MASK(p, i) i_ucvtf(r, dl[i].s4, sl[i].s4);
709 LOOP_MASK(p, i) i_ucvtf(r, dh[i].s4, sh[i].s4);
710 }
711}
712
713/*********************************************************************/
714/* SWS_UOP_EXPAND_PAIR: expand bytes in pairs (16 bit) */
715/* SWS_UOP_EXPAND_QUAD: expand bytes in quads (32 bit) */
716
718 SwsAArch64OpRegs *regs)
719{
720 RasmContext *r = s->rctx;
721 RasmOp *sl = regs->sl;
722 RasmOp *dl = regs->dl;
723 RasmOp *dh = regs->dh;
724
725 size_t src_el_size = s->el_size;
726 SwsPixelType to_type;
727 switch (p->uop) {
728 case SWS_UOP_EXPAND_PAIR: to_type = SWS_PIXEL_U16; break;
729 case SWS_UOP_EXPAND_QUAD: to_type = SWS_PIXEL_U32; break;
730 default:
731 av_assert0(!"Invalid uop!");
732 break;
733 }
734 size_t dst_el_size = ff_sws_pixel_type_size(to_type);
735 size_t dst_total_size = p->block_size * dst_el_size;
736 size_t dst_vec_size = FFMIN(dst_total_size, 16);
737
738 if (!s->use_vh)
739 s->use_vh = (dst_vec_size != dst_total_size);
740
741 if (src_el_size == 1) {
742 rasm_add_comment(r, "u8 -> u16");
743 reshape_io_vectors(regs, 16, 1);
744 LOOP_MASK_VH(s, p, i) i_zip2(r, dh[i], sl[i], sl[i]);
745 LOOP_MASK (p, i) i_zip1(r, dl[i], sl[i], sl[i]);
746 sl = dl;
747 }
748 if (dst_el_size == 4) {
749 rasm_add_comment(r, "u16 -> u32");
750 reshape_io_vectors(regs, 8, 2);
751 LOOP_MASK_VH(s, p, i) i_zip2(r, dh[i], sl[i], sl[i]);
752 LOOP_MASK (p, i) i_zip1(r, dl[i], sl[i], sl[i]);
753 }
754}
755
756/*********************************************************************/
757/* SWS_UOP_ADD: add vec4 to masked components */
758
760 SwsAArch64OpRegs *regs)
761{
762 RasmContext *r = s->rctx;
763 RasmOp *sl = regs->sl;
764 RasmOp *sh = regs->sh;
765 RasmOp *dl = regs->dl;
766 RasmOp *dh = regs->dh;
767 RasmOp *vk = regs->vk;
768
769 av_assert0(p->type == SWS_PIXEL_F32);
770
771 LOOP_MASK (p, i) { i_fadd(r, dl[i], sl[i], vk[i]); CMTF("vl[%u] += vadd%u;", i, i); }
772 LOOP_MASK_VH(s, p, i) { i_fadd(r, dh[i], sh[i], vk[i]); CMTF("vh[%u] += vadd%u;", i, i); }
773}
774
775/*********************************************************************/
776/* SWS_UOP_MIN: min(x, vec4) on masked components */
777
779 SwsAArch64OpRegs *regs)
780{
781 RasmContext *r = s->rctx;
782 RasmOp *sl = regs->sl;
783 RasmOp *sh = regs->sh;
784 RasmOp *dl = regs->dl;
785 RasmOp *dh = regs->dh;
786 RasmOp *vk = regs->vk;
787
788 if (p->type == SWS_PIXEL_F32) {
789 LOOP_MASK (p, i) { i_fmin(r, dl[i], sl[i], vk[i]); CMTF("vl[%u] = min(vl[%u], vmin%u);", i, i, i); }
790 LOOP_MASK_VH(s, p, i) { i_fmin(r, dh[i], sh[i], vk[i]); CMTF("vh[%u] = min(vh[%u], vmin%u);", i, i, i); }
791 } else {
792 LOOP_MASK (p, i) { i_umin(r, dl[i], sl[i], vk[i]); CMTF("vl[%u] = min(vl[%u], vmin%u);", i, i, i); }
793 LOOP_MASK_VH(s, p, i) { i_umin(r, dh[i], sh[i], vk[i]); CMTF("vh[%u] = min(vh[%u], vmin%u);", i, i, i); }
794 }
795}
796
797/*********************************************************************/
798/* SWS_UOP_MAX: max(x, vec4) on masked components */
799
801 SwsAArch64OpRegs *regs)
802{
803 RasmContext *r = s->rctx;
804 RasmOp *sl = regs->sl;
805 RasmOp *sh = regs->sh;
806 RasmOp *dl = regs->dl;
807 RasmOp *dh = regs->dh;
808 RasmOp *vk = regs->vk;
809
810 if (p->type == SWS_PIXEL_F32) {
811 LOOP_MASK (p, i) { i_fmax(r, dl[i], sl[i], vk[i]); CMTF("vl[%u] = max(vl[%u], vmax%u);", i, i, i); }
812 LOOP_MASK_VH(s, p, i) { i_fmax(r, dh[i], sh[i], vk[i]); CMTF("vh[%u] = max(vh[%u], vmax%u);", i, i, i); }
813 } else {
814 LOOP_MASK (p, i) { i_umax(r, dl[i], sl[i], vk[i]); CMTF("vl[%u] = max(vl[%u], vmax%u);", i, i, i); }
815 LOOP_MASK_VH(s, p, i) { i_umax(r, dh[i], sh[i], vk[i]); CMTF("vh[%u] = max(vh[%u], vmax%u);", i, i, i); }
816 }
817}
818
819/*********************************************************************/
820/* SWS_UOP_SCALE: multiply masked components by scalar */
821
823 SwsAArch64OpRegs *regs)
824{
825 RasmContext *r = s->rctx;
826 RasmOp *sl = regs->sl;
827 RasmOp *sh = regs->sh;
828 RasmOp *dl = regs->dl;
829 RasmOp *dh = regs->dh;
830 RasmOp scale_vec = regs->vk[0];
831
832 if (p->type == SWS_PIXEL_F32) {
833 LOOP_MASK (p, i) { i_fmul(r, dl[i], sl[i], scale_vec); CMTF("vl[%u] *= scale_vec;", i); }
834 LOOP_MASK_VH(s, p, i) { i_fmul(r, dh[i], sh[i], scale_vec); CMTF("vh[%u] *= scale_vec;", i); }
835 } else {
836 LOOP_MASK (p, i) { i_mul (r, dl[i], sl[i], scale_vec); CMTF("vl[%u] *= scale_vec;", i); }
837 LOOP_MASK_VH(s, p, i) { i_mul (r, dh[i], sh[i], scale_vec); CMTF("vh[%u] *= scale_vec;", i); }
838 }
839}
840
841/*********************************************************************/
842/* SWS_UOP_LINEAR: mask = non-trivial output rows */
843/* SWS_UOP_LINEAR_FMA: with SWS_UOP_FLAG_FMA */
844
846 SwsAArch64OpRegs *regs, bool vh_pass)
847{
848 RasmContext *r = s->rctx;
849 /**
850 * The intermediate registers for fmul+fadd (for when SWS_BITEXACT
851 * is set) start from temp vector 8.
852 */
853 RasmOp *vt = regs->vt;
854 RasmOp *vtmp = &vt[8];
855 RasmOp *sx = vh_pass ? regs->sh : regs->sl;
856 RasmOp *dx = vh_pass ? regs->dh : regs->dl;
857 char cvh = vh_pass ? 'h' : 'l';
858
859 if (vh_pass && !s->use_vh)
860 return;
861
862 /**
863 * The non-zero coefficients have been packed in aarch64_setup_linear()
864 * in sequential order into the individual lanes of the coefficient
865 * vector registers. We must follow the same order of execution here.
866 */
867 LOOP_MASK(p, i) {
868 bool first = true;
869 RasmNode *pre_mul = rasm_get_current_node(r);
870 for (int j = 0; j < 5; j++) {
871 bool is_offset = (j == 0);
872 int src_j = is_offset ? 4 : (j - 1);
873 if (p->par.lin.zero & SWS_MASK(i, src_j))
874 continue;
875 RasmOp vsrc = sx[src_j];
876 RasmOp vcoeff = regs->linear_vcoeff[i][j];
877 if (first && is_offset) {
878 i_dup (r, dx[i], vcoeff); CMTF("v%c[%u] = broadcast(offset[%u]);", cvh, i, i);
879 } else if (first && !is_offset) {
880 if (p->par.lin.one & SWS_MASK(i, src_j)) {
881 i_mov16b(r, dx[i], vsrc); CMTF("v%c[%u] = vsrc%c[%u];", cvh, i, cvh, src_j);
882 } else if (p->type == SWS_PIXEL_F32) {
883 i_fmul (r, dx[i], vsrc, vcoeff); CMTF("v%c[%u] = vsrc%c[%u] * coeff[%u][%u];", cvh, i, cvh, src_j, i, src_j);
884 } else {
885 i_mul (r, dx[i], vsrc, vcoeff); CMTF("v%c[%u] = vsrc%c[%u] * coeff[%u][%u];", cvh, i, cvh, src_j, i, src_j);
886 }
887 } else if (p->type != SWS_PIXEL_F32) {
888 /* Integer multiply-accumulate is always exact. */
889 i_mla (r, dx[i], vsrc, vcoeff); CMTF("v%c[%u] += vsrc%c[%u] * coeff[%u][%u];", cvh, i, cvh, src_j, i, src_j);
890 } else if (p->uop == SWS_UOP_LINEAR_FMA) {
891 /**
892 * Most modern aarch64 cores have a fastpath for sequences
893 * of fmla instructions. This means that even if the coefficient
894 * is 1, it is still faster to use fmla by 1 instead of fadd.
895 */
896 i_fmla(r, dx[i], vsrc, vcoeff); CMTF("v%c[%u] += vsrc%c[%u] * coeff[%u][%u];", cvh, i, cvh, src_j, i, src_j);
897 } else {
898 /**
899 * Split the multiply-accumulate into fmul+fadd. All
900 * multiplications are performed first into temporary
901 * registers, and only then added to the destination,
902 * to reduce the dependency chain.
903 * There is no need to perform multiplications by 1.
904 */
905 if (!(p->par.lin.one & SWS_MASK(i, src_j))) {
906 pre_mul = rasm_set_current_node(r, pre_mul);
907 i_fmul(r, vtmp[src_j], vsrc, vcoeff); CMTF("vtmp[%u] = vsrc%c[%u] * coeff[%u][%u];", src_j, cvh, src_j, i, src_j);
908 pre_mul = rasm_set_current_node(r, pre_mul);
909 i_fadd(r, dx[i], dx[i], vtmp[src_j]); CMTF("v%c[%u] += vtmp[%u];", cvh, i, src_j);
910 } else {
911 i_fadd(r, dx[i], dx[i], vsrc); CMTF("v%c[%u] += vsrc%c[%u];", cvh, i, cvh, src_j);
912 }
913 }
914 first = false;
915 }
916 }
917}
918
920 SwsAArch64OpRegs *regs)
921{
922 /* Perform linear passes for low and high vector banks. */
923 linear_pass(s, p, regs, false);
924 linear_pass(s, p, regs, true);
925}
926
927/*********************************************************************/
928/* SWS_UOP_DITHER: mask = components to dither */
929
931 SwsAArch64OpRegs *regs)
932{
933 RasmContext *r = s->rctx;
934 RasmOp *sl = regs->sl;
935 RasmOp *sh = regs->sh;
936 RasmOp *dl = regs->dl;
937 RasmOp *dh = regs->dh;
938 RasmOp src_ptr = regs->dither_ptr;
939
940 RasmOp ptr = s->tmp0;
941 RasmOp tmp1 = s->tmp1;
942 RasmOp wtmp1 = a64op_w(tmp1);
943 RasmOp dither_vl = regs->vt[0];
944 RasmOp dither_vh = regs->vt[1];
945 RasmOp bx64 = a64op_x(s->bx);
946 RasmOp y64 = a64op_x(s->y);
947
948 /**
949 * The dither matrix is (1 << size_log2)² pixels large. It is
950 * periodic, so the x and y offsets should be masked to fit inside
951 * (1 << size_log2). The matrix buffer is prepared by
952 * translate_dither_op() in libswscale/uops.c.
953 * The width of the matrix is assumed to be at least 8, which matches
954 * the maximum block_size for aarch64 asmgen when f32 operations
955 * (i.e., dithering) are used. This guarantees that the x offset is
956 * aligned and that reading block_size elements does not extend past
957 * the end of the row. The x offset doesn't change between components,
958 * so it is only required to be masked once.
959 * The y offset, on the other hand, may change per component, and
960 * would therefore need to be masked for every y_offset value. To
961 * avoid this, the matrix buffer is over-allocated by the largest
962 * y_offset value, with the extra rows repeating the first rows of
963 * the matrix. This way, we only need to mask the y offset once, and
964 * can safely increment the dither matrix pointer by fixed offsets
965 * for every y_offset change.
966 */
967
968 /**
969 * Sort components by y_offset value so that we can start dithering
970 * with the smallest value, and increment the pointer upwards for
971 * each new offset. The dither matrix is over-allocated and may be
972 * over-read at the top, but it cannot be over-read before the start
973 * of the buffer. Since we only mask the y offset once, this would
974 * be an issue if we tried to subtract a value larger than the
975 * initial y_offset.
976 */
977 int sorted[4];
978 int n_comps = 0;
979 /* Very cheap bucket sort. */
980 int max_offset = 0;
981 LOOP_MASK(p, i)
982 max_offset = FFMAX(max_offset, p->par.dither.y_offset[i]);
983 for (int y_off = 0; y_off <= max_offset; y_off++) {
984 LOOP_MASK(p, i) {
985 if (p->par.dither.y_offset[i] == y_off)
986 sorted[n_comps++] = i;
987 }
988 }
989
990 /**
991 * We use ubfiz to mask and shift left in one single instruction:
992 * ubfiz <Wd>, <Wn>, #<lsb>, #<width>
993 * Wd = (Wn & ((1 << width) - 1)) << lsb;
994 *
995 * Given:
996 * block_size = 8, log2(block_size) = 3
997 * dither_size = 16, log2(dither_size) = 4, dither_mask = 0b1111
998 * sizeof(float) = 4, log2(sizeof(float)) = 2
999 *
1000 * Suppose we have bx = 0bvvvv. To get x, we left shift by
1001 * log2(block_size) and end up with 0bvvvv000. Then we mask against
1002 * dither_mask, and end up with 0bv000. Finally we multiply by
1003 * sizeof(float), which is the same as shifting left by
1004 * log2(sizeof(float)). The result is 0bv00000.
1005 *
1006 * Therefore:
1007 * width = log2(dither_size) - log2(block_size)
1008 * lsb = log2(block_size) + log2(sizeof(float))
1009 */
1010 const int block_size_log2 = (p->block_size == 16) ? 4 : 3;
1011 const int dither_size_log2 = p->par.dither.size_log2;
1012 const int sizeof_float_log2 = 2;
1013 if (dither_size_log2 != block_size_log2) {
1014 RasmOp lsb = IMM(block_size_log2 + sizeof_float_log2);
1015 RasmOp width = IMM(dither_size_log2 - block_size_log2);
1016 i_ubfiz(r, tmp1, bx64, lsb, width); CMT("tmp1 = (bx & ((dither_size / block_size) - 1)) * block_size * sizeof(float);");
1017 i_add (r, ptr, src_ptr, tmp1); CMT("ptr += tmp1;");
1018 src_ptr = ptr;
1019 }
1020
1021 int last_y_off = -1;
1022 int prev_i = 0;
1023 for (int sorted_i = 0; sorted_i < n_comps; sorted_i++) {
1024 int i = sorted[sorted_i];
1025 uint8_t y_off = p->par.dither.y_offset[i];
1026 bool do_load = (y_off != last_y_off);
1027
1028 if (last_y_off < 0) {
1029 /* On the first run, calculate pointer inside dither_matrix. */
1030 RasmOp lsb = IMM(dither_size_log2 + sizeof_float_log2);
1031 RasmOp width = IMM(dither_size_log2);
1032 /**
1033 * The ubfiz instruction for the y offset performs masking
1034 * by the dither matrix size and shifts by the stride.
1035 */
1036 if (y_off == 0) {
1037 i_ubfiz(r, tmp1, y64, lsb, width); CMT("tmp1 = (y & (dither_size - 1)) * dither_size * sizeof(float);");
1038 } else {
1039 i_add (r, wtmp1, s->y, IMM(y_off)); CMTF("tmp1 = y + y_off[%u];", i);
1040 i_ubfiz(r, tmp1, tmp1, lsb, width); CMT("tmp1 = (tmp1 & (dither_size - 1)) * dither_size * sizeof(float);");
1041 }
1042 i_add(r, ptr, src_ptr, tmp1); CMT("ptr += tmp1;");
1043 } else if (do_load) {
1044 /**
1045 * On subsequent runs, just increment the pointer.
1046 * The matrix is over-allocated, so we don't risk
1047 * overreading.
1048 */
1049 int delta = (y_off - last_y_off) * (1 << dither_size_log2) * sizeof(float);
1050 i_add(r, ptr, ptr, IMM(delta)); CMTF("ptr += (y_off[%u] - y_off[%u]) * dither_size * sizeof(float);", i, prev_i);
1051 }
1052
1053 if (do_load) {
1054 RasmOp dither_vlq = v_q(dither_vl);
1055 RasmOp dither_vhq = v_q(dither_vh);
1056 i_ldp (r, dither_vlq, dither_vhq, a64op_base(ptr)); CMT("{ ditherl, ditherh } = *ptr;");
1057 }
1058
1059 i_fadd (r, dl[i], sl[i], dither_vl); CMTF("vl[%u] += vditherl;", i);
1060 if (s->use_vh) {
1061 i_fadd(r, dh[i], sh[i], dither_vh); CMTF("vh[%u] += vditherh;", i);
1062 }
1063 sl = dl;
1064 sh = dh;
1065
1066 last_y_off = y_off;
1067 prev_i = i;
1068 }
1069}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
DirList * dl
Definition ffplay.c:390
static const uint8_t frame_size[4]
Definition g723_1.h:222
static const int offsets[]
Definition hevc_pel.c:34
#define r
Definition input.c:42
static int shift(int a, int b)
Definition bonk.c:261
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
static void asmgen_op_write_planar(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:375
static void asmgen_op_clear(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:603
static const char * print_swizzle_v(char buf[8], int8_t n, uint8_t vh)
Definition ops_asmgen.c:420
static void asmgen_op_read_packed(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:276
static void asmgen_op_read_nibble(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:243
static RasmOp swizzle_a64op(SwsAArch64OpRegs *regs, int8_t n, uint8_t vh, bool dst)
Definition ops_asmgen.c:430
#define CMT(comment)
Definition ops_asmgen.c:32
static void asmgen_op_add(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:759
static void reshape_io_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
Definition ops_asmgen.c:36
static void asmgen_op_write_nibble(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:332
static void linear_pass(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs, bool vh_pass)
Definition ops_asmgen.c:845
static void clobber_gpr(RasmOp regs[MAX_SAVED_REGS], unsigned *count, RasmOp gpr)
Definition ops_asmgen.c:114
static void asmgen_op_move(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:452
static void reshape_temp_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
Definition ops_asmgen.c:47
static void asmgen_op_unpack(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:462
static void reshape_const_vectors(SwsAArch64OpRegs *regs, int el_count, int el_size)
Definition ops_asmgen.c:54
static void asmgen_op_dither(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:930
static void asmgen_process(SwsAArch64Context *s, SwsCompMask imask, SwsCompMask omask)
Definition ops_asmgen.c:139
static void asmgen_op_lshift(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:550
static unsigned clobbered_frame_size(unsigned n)
Definition ops_asmgen.c:63
static void asmgen_op_linear(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:919
static void asmgen_op_min(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:778
static void asmgen_op_convert(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:620
static void emit_clear(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, RasmOp *vx, RasmOp *vk, int i, const char *vx_str)
Definition ops_asmgen.c:584
#define PRINT_SWIZZLE_V(n, vh)
Definition ops_asmgen.c:428
static void asmgen_op_swap_bytes(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:395
#define MAX_SAVED_REGS
Definition ops_asmgen.c:112
static void asmgen_op_read_planar(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:285
#define LOOP_MASK_VH(s, p, idx)
Definition ops_asmgen.c:28
static void asmgen_op_expand(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:717
static void asmgen_op_scale(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:822
static void asmgen_op_write_packed_n(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, RasmOp *vx)
Definition ops_asmgen.c:355
#define LOOP_VH(s, mask, idx)
Definition ops_asmgen.c:27
static void asmgen_op_rshift(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:567
#define CMTF(fmt,...)
Definition ops_asmgen.c:33
static void asmgen_op_read_bit(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:214
static void asmgen_prologue(SwsAArch64Context *s, const RasmOp *regs, unsigned n)
Definition ops_asmgen.c:68
static void asmgen_op_write_bit(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:308
#define LOOP_MASK_BWD_VH(s, p, idx)
Definition ops_asmgen.c:29
static void asmgen_epilogue(SwsAArch64Context *s, const RasmOp *regs, unsigned n)
Definition ops_asmgen.c:89
static void asmgen_op_max(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:800
static void swizzle_emit(SwsAArch64Context *s, SwsAArch64OpRegs *regs, int8_t dst, int8_t src)
Definition ops_asmgen.c:439
static void asmgen_op_write_packed(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:366
static void asmgen_op_pack(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, SwsAArch64OpRegs *regs)
Definition ops_asmgen.c:503
static void asmgen_op_read_packed_n(SwsAArch64Context *s, const SwsAArch64OpImplParams *p, RasmOp *vx)
Definition ops_asmgen.c:265
static unsigned clobbered_gprs(const SwsAArch64Context *s, SwsCompMask imask, SwsCompMask omask, RasmOp regs[MAX_SAVED_REGS])
Definition ops_asmgen.c:122
#define LOOP(mask, idx)
Definition ops_impl.h:56
static uint16_t nibble_mask(SwsCompMask mask)
Definition ops_impl.h:34
#define offsetof_exec_out_bump
Definition ops_impl.h:119
#define LOOP_MASK_BWD(p, idx)
Definition ops_impl.h:64
#define offsetof_exec_in
These values will be used by ops_asmgen to access fields inside of SwsOpExec and SwsOpImpl.
Definition ops_impl.h:116
#define offsetof_exec_out
Definition ops_impl.h:117
#define LOOP_MASK(p, idx)
Definition ops_impl.h:63
#define offsetof_exec_in_bump
Definition ops_impl.h:118
RasmNode * rasm_get_current_node(RasmContext *rctx)
Definition rasm.c:194
AArch64VecViews a64op_vec_views(RasmOp op)
Definition rasm.c:330
RasmNode * rasm_add_label(RasmContext *rctx, int id)
Definition rasm.c:146
RasmNode * rasm_add_comment(RasmContext *rctx, const char *comment)
Definition rasm.c:117
int rasm_new_label(RasmContext *rctx, const char *name)
Allocate a new label ID with the given name.
Definition rasm.c:282
RasmNode * rasm_set_current_node(RasmContext *rctx, RasmNode *node)
Definition rasm.c:199
#define i_mov16b(rctx, op0, op1)
Definition rasm.h:629
static RasmOp a64op_lr(void)
Definition rasm.h:357
#define i_dup(rctx, op0, op1)
Definition rasm.h:558
#define i_stp(rctx, op0, op1, op2)
Definition rasm.h:589
#define i_and16b(rctx, op0, op1, op2)
Definition rasm.h:628
#define i_ins(rctx, op0, op1)
Definition rasm.h:565
#define i_uxtl(rctx, op0, op1)
Definition rasm.h:603
static RasmOp a64op_base(RasmOp op)
Definition rasm.h:507
#define i_ld4(rctx, op0, op1)
Definition rasm.h:570
static uint8_t a64op_gpr_n(RasmOp op)
Definition rasm.h:352
#define i_st3(rctx, op0, op1)
Definition rasm.h:587
static RasmOp a64op_sp(void)
Definition rasm.h:358
static RasmOp v_8b(RasmOp op)
Definition rasm.h:438
#define i_ld2(rctx, op0, op1)
Definition rasm.h:568
#define i_orr16b(rctx, op0, op1, op2)
Definition rasm.h:630
#define i_fadd(rctx, op0, op1, op2)
Definition rasm.h:559
#define i_ucvtf(rctx, op0, op1)
Definition rasm.h:595
static RasmOp vv_4(RasmOp op0, RasmOp op1, RasmOp op2, RasmOp op3)
Definition rasm.h:450
static RasmOp a64op_x(RasmOp op)
Definition rasm.h:362
#define i_rev32(rctx, op0, op1)
Definition rasm.h:583
#define i_ldp(rctx, op0, op1, op2)
Definition rasm.h:571
#define i_zip1(rctx, op0, op1, op2)
Definition rasm.h:606
#define i_ushl(rctx, op0, op1, op2)
Definition rasm.h:599
#define i_movi(rctx, op0, op1)
Definition rasm.h:578
#define i_fcvtzu(rctx, op0, op1)
Definition rasm.h:560
static RasmOp a64op_pre(RasmOp op, int16_t imm)
Definition rasm.h:509
static RasmOp a64op_elem(RasmOp op, uint8_t idx)
Definition rasm.h:423
#define i_and(rctx, op0, op1, op2)
Definition rasm.h:551
#define i_lsr(rctx, op0, op1, op2)
Definition rasm.h:575
#define i_fmla(rctx, op0, op1, op2)
Definition rasm.h:563
#define i_ushr(rctx, op0, op1, op2)
Definition rasm.h:602
#define i_shl(rctx, op0, op1, op2)
Definition rasm.h:584
static RasmOp v_q(RasmOp op)
Definition rasm.h:435
#define i_xtn(rctx, op0, op1)
Definition rasm.h:605
#define i_b(rctx, op0)
Definition rasm.h:552
#define i_fmin(rctx, op0, op1, op2)
Definition rasm.h:562
#define i_ubfiz(rctx, op0, op1, op2, op3)
Definition rasm.h:594
#define i_uxtl2(rctx, op0, op1)
Definition rasm.h:604
#define i_bne(rctx, id)
Definition rasm.h:611
static RasmOp a64op_off(RasmOp op, int16_t imm)
Definition rasm.h:508
static RasmOp rasm_op_label(int id)
Definition rasm.h:96
#define i_ldr(rctx, op0, op1)
Definition rasm.h:572
#define i_mul(rctx, op0, op1, op2)
Definition rasm.h:579
#define i_fmul(rctx, op0, op1, op2)
Definition rasm.h:564
static RasmOp vv_2(RasmOp op0, RasmOp op1)
Definition rasm.h:448
#define i_st4(rctx, op0, op1)
Definition rasm.h:588
#define i_st2(rctx, op0, op1)
Definition rasm.h:586
#define i_orr(rctx, op0, op1, op2)
Definition rasm.h:580
static RasmOp v_16b(RasmOp op)
Definition rasm.h:439
static RasmOp a64op_post(RasmOp op, int16_t imm)
Definition rasm.h:510
#define i_ldrh(rctx, op0, op1)
Definition rasm.h:574
#define i_zip2(rctx, op0, op1, op2)
Definition rasm.h:607
static uint8_t a64op_vec_n(RasmOp op)
Definition rasm.h:378
#define A64OP_VEC_VIEWS4(op)
Definition rasm.h:480
#define i_add(rctx, op0, op1, op2)
Definition rasm.h:548
#define i_ldrb(rctx, op0, op1)
Definition rasm.h:573
#define i_umax(rctx, op0, op1, op2)
Definition rasm.h:596
#define i_ret(rctx)
Definition rasm.h:581
#define i_addv(rctx, op0, op1)
Definition rasm.h:549
static RasmOp vv_3(RasmOp op0, RasmOp op1, RasmOp op2)
Definition rasm.h:449
static RasmOp a64op_w(RasmOp op)
Definition rasm.h:361
static RasmOp a64op_make_vec(uint8_t n, uint8_t el_count, uint8_t el_size)
Definition rasm.h:367
#define i_str(rctx, op0, op1)
Definition rasm.h:590
#define i_mov(rctx, op0, op1)
Definition rasm.h:577
#define i_rev16(rctx, op0, op1)
Definition rasm.h:582
#define i_fmax(rctx, op0, op1, op2)
Definition rasm.h:561
#define i_umin(rctx, op0, op1, op2)
Definition rasm.h:597
#define i_ld3(rctx, op0, op1)
Definition rasm.h:569
#define i_cmp(rctx, op0, op1)
Definition rasm.h:556
#define i_mla(rctx, op0, op1, op2)
Definition rasm.h:576
#define IMM(val)
Definition rasm.h:92
#define FF_ARRAY_ELEMS(a)
This helper structure is used to mimic the assembler syntax for vector register modifiers.
Definition rasm.h:457
RasmOp h
Definition rasm.h:460
RasmOp b16
Definition rasm.h:466
RasmOp be[2]
Definition rasm.h:473
RasmOp d
Definition rasm.h:462
RasmOp b8
Definition rasm.h:465
RasmOp h4
Definition rasm.h:467
RasmOp de[2]
Definition rasm.h:474
RasmOp b
Definition rasm.h:459
RasmOp h8
Definition rasm.h:468
RasmOp s
Definition rasm.h:461
SwsAArch64OpImplParams describes the parameters for an SwsUOpType operation.
Definition ops_impl.h:47
RasmOp vt[12]
Definition ops_asmgen.h:32
RasmOp linear_vcoeff[4][5]
Definition ops_asmgen.h:38
#define src
Definition vp8dsp.c:248
#define width
Definition dsp.h:99
Runtime assembler for AArch64.
Definition rasm.h:44
SwsPixelType
Definition uops.h:40
@ SWS_PIXEL_F32
Definition uops.h:45
@ SWS_PIXEL_U32
Definition uops.h:44
@ SWS_PIXEL_U16
Definition uops.h:43
@ SWS_PIXEL_U8
Definition uops.h:42
#define SWS_COMP(X)
Definition uops.h:133
#define SWS_COMP_ELEMS(N)
Definition uops.h:136
#define SWS_MASK(I, J)
Definition uops.h:275
@ SWS_UOP_TO_U8
Definition uops.h:199
@ SWS_UOP_EXPAND_QUAD
Definition uops.h:198
@ SWS_UOP_LINEAR_FMA
Definition uops.h:217
@ SWS_UOP_EXPAND_PAIR
Definition uops.h:197
@ SWS_UOP_TO_U16
Definition uops.h:200
@ SWS_UOP_TO_U32
Definition uops.h:201
@ SWS_UOP_TO_F32
Definition uops.h:202
uint8_t SwsCompMask
Bit-mask of components.
Definition uops.h:129
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:51
float delta