1 /*
2  * Copyright (c) 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #ifdef __ARM_FEATURE_SVE
28 
29 
30 namespace {
31 
sve_transpose_interleave_3VL_1x4(uint8_t * out,const uint8_t * in,size_t width,size_t in_stride,size_t height)32 void sve_transpose_interleave_3VL_1x4(uint8_t *out, const uint8_t *in, size_t width, size_t in_stride, size_t height)
33 {
34     uint8_t *pad_row = reinterpret_cast<uint8_t *>(alloca(width * sizeof(uint8_t)));
35 
36     if (height % 4) {
37         memset(pad_row, 0, width * sizeof(uint8_t));
38     }
39 
40     size_t out_stride = 3 * roundup<size_t>(height, 4) * get_vector_length<uint32_t>();
41 
42     __asm__ __volatile__(
43       "ptrue p1.b\n"
44       "cmp %x[height], #0x8\n"
45       "blt 6f\n"
46       "1:"  // Main row loop: Head
47       "mov x9, %x[in]\n"
48       "mov x28, %x[out]\n"
49       "add x27, x9, %x[in_stride]\n"
50       "add x26, x27, %x[in_stride]\n"
51       "add x25, x26, %x[in_stride]\n"
52       "add x24, x25, %x[in_stride]\n"
53       "add x23, x24, %x[in_stride]\n"
54       "add x22, x23, %x[in_stride]\n"
55       "add x21, x22, %x[in_stride]\n"
56       "add %x[in], x21, %x[in_stride]\n"
57       "sub %x[height], %x[height], #0x8\n"
58       "mov x20, %x[width]\n"
59       "cntb x19, ALL, MUL #3\n"
60       "cmp x20, x19\n"
61       "blt 3f\n"
62       "2:"  // Main row loop: Unroll column loop
63       "ld1b { z18.b }, p1/Z, [x9]\n"
64       "sub x20, x20, x19\n"
65       "ld1b { z19.b }, p1/Z, [x9, #1, MUL VL]\n"
66       "cmp x20, x19\n"
67       "ld1b { z10.b }, p1/Z, [x9, #2, MUL VL]\n"
68       "addvl x9, x9, #3\n"
69       "ld1b { z24.b }, p1/Z, [x27]\n"
70       "ld1b { z23.b }, p1/Z, [x27, #1, MUL VL]\n"
71       "ld1b { z9.b }, p1/Z, [x27, #2, MUL VL]\n"
72       "addvl x27, x27, #3\n"
73       "ld1b { z16.b }, p1/Z, [x26]\n"
74       "zip1 z21.b, z18.b, z16.b\n"
75       "ld1b { z17.b }, p1/Z, [x26, #1, MUL VL]\n"
76       "zip2 z18.b, z18.b, z16.b\n"
77       "ld1b { z8.b }, p1/Z, [x26, #2, MUL VL]\n"
78       "addvl x26, x26, #3\n"
79       "zip1 z22.b, z19.b, z17.b\n"
80       "ld1b { z16.b }, p1/Z, [x25]\n"
81       "zip2 z7.b, z19.b, z17.b\n"
82       "ld1b { z20.b }, p1/Z, [x25, #1, MUL VL]\n"
83       "zip1 z6.b, z10.b, z8.b\n"
84       "ld1b { z5.b }, p1/Z, [x25, #2, MUL VL]\n"
85       "addvl x25, x25, #3\n"
86       "zip1 z17.b, z24.b, z16.b\n"
87       "ld1b { z19.b }, p1/Z, [x24]\n"
88       "zip2 z16.b, z24.b, z16.b\n"
89       "ld1b { z4.b }, p1/Z, [x24, #1, MUL VL]\n"
90       "zip1 z3.b, z21.b, z17.b\n"
91       "ld1b { z2.b }, p1/Z, [x24, #2, MUL VL]\n"
92       "addvl x24, x24, #3\n"
93       "zip2 z1.b, z21.b, z17.b\n"
94       "ld1b { z0.b }, p1/Z, [x23]\n"
95       "zip1 z31.b, z18.b, z16.b\n"
96       "ld1b { z30.b }, p1/Z, [x23, #1, MUL VL]\n"
97       "zip2 z29.b, z18.b, z16.b\n"
98       "ld1b { z28.b }, p1/Z, [x23, #2, MUL VL]\n"
99       "addvl x23, x23, #3\n"
100       "zip1 z18.b, z23.b, z20.b\n"
101       "ld1b { z17.b }, p1/Z, [x22]\n"
102       "zip2 z27.b, z23.b, z20.b\n"
103       "ld1b { z26.b }, p1/Z, [x22, #1, MUL VL]\n"
104       "zip1 z25.b, z22.b, z18.b\n"
105       "ld1b { z24.b }, p1/Z, [x22, #2, MUL VL]\n"
106       "addvl x22, x22, #3\n"
107       "zip1 z21.b, z19.b, z17.b\n"
108       "ld1b { z16.b }, p1/Z, [x21]\n"
109       "zip2 z19.b, z19.b, z17.b\n"
110       "ld1b { z23.b }, p1/Z, [x21, #1, MUL VL]\n"
111       "zip2 z20.b, z22.b, z18.b\n"
112       "ld1b { z22.b }, p1/Z, [x21, #2, MUL VL]\n"
113       "addvl x21, x21, #3\n"
114       "zip1 z17.b, z0.b, z16.b\n"
115       "st1b { z3.b }, p1, [x28]\n"
116       "zip2 z18.b, z0.b, z16.b\n"
117       "st1b { z1.b }, p1, [x28, #1, MUL VL]\n"
118       "zip1 z16.b, z21.b, z17.b\n"
119       "st1b { z31.b }, p1, [x28, #2, MUL VL]\n"
120       "zip2 z17.b, z21.b, z17.b\n"
121       "st1b { z16.b }, p1, [x28, #3, MUL VL]\n"
122       "zip1 z16.b, z19.b, z18.b\n"
123       "st1b { z17.b }, p1, [x28, #4, MUL VL]\n"
124       "zip2 z19.b, z19.b, z18.b\n"
125       "st1b { z16.b }, p1, [x28, #5, MUL VL]\n"
126       "add x28, x28, %x[out_stride]\n"
127       "zip1 z18.b, z4.b, z26.b\n"
128       "st1b { z29.b }, p1, [x28]\n"
129       "zip1 z17.b, z30.b, z23.b\n"
130       "st1b { z25.b }, p1, [x28, #1, MUL VL]\n"
131       "zip1 z16.b, z18.b, z17.b\n"
132       "st1b { z20.b }, p1, [x28, #2, MUL VL]\n"
133       "zip2 z18.b, z18.b, z17.b\n"
134       "st1b { z19.b }, p1, [x28, #3, MUL VL]\n"
135       "zip1 z17.b, z7.b, z27.b\n"
136       "st1b { z16.b }, p1, [x28, #4, MUL VL]\n"
137       "zip2 z16.b, z7.b, z27.b\n"
138       "st1b { z18.b }, p1, [x28, #5, MUL VL]\n"
139       "add x28, x28, %x[out_stride]\n"
140       "zip1 z21.b, z9.b, z5.b\n"
141       "st1b { z17.b }, p1, [x28]\n"
142       "zip2 z18.b, z4.b, z26.b\n"
143       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
144       "zip1 z16.b, z6.b, z21.b\n"
145       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
146       "zip2 z17.b, z30.b, z23.b\n"
147       "zip1 z16.b, z18.b, z17.b\n"
148       "st1b { z16.b }, p1, [x28, #3, MUL VL]\n"
149       "zip2 z16.b, z18.b, z17.b\n"
150       "st1b { z16.b }, p1, [x28, #4, MUL VL]\n"
151       "zip1 z20.b, z2.b, z24.b\n"
152       "zip1 z19.b, z28.b, z22.b\n"
153       "zip1 z16.b, z20.b, z19.b\n"
154       "st1b { z16.b }, p1, [x28, #5, MUL VL]\n"
155       "add x28, x28, %x[out_stride]\n"
156       "zip2 z16.b, z6.b, z21.b\n"
157       "st1b { z16.b }, p1, [x28]\n"
158       "zip2 z18.b, z10.b, z8.b\n"
159       "zip2 z17.b, z9.b, z5.b\n"
160       "zip1 z16.b, z18.b, z17.b\n"
161       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
162       "zip2 z16.b, z18.b, z17.b\n"
163       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
164       "zip2 z16.b, z20.b, z19.b\n"
165       "st1b { z16.b }, p1, [x28, #3, MUL VL]\n"
166       "zip2 z18.b, z2.b, z24.b\n"
167       "zip2 z17.b, z28.b, z22.b\n"
168       "zip1 z16.b, z18.b, z17.b\n"
169       "st1b { z16.b }, p1, [x28, #4, MUL VL]\n"
170       "zip2 z16.b, z18.b, z17.b\n"
171       "st1b { z16.b }, p1, [x28, #5, MUL VL]\n"
172       "add x28, x28, %x[out_stride]\n"
173       "bge 2b\n"
174       "3:"  // Main row loop: Unroll column loop skip
175       "cbz x20, 5f\n"
176       "4:"  // Main row loop: Column loop
177       "whilelt p0.b, XZR, x20\n"
178       "ld1b { z18.b }, p0/Z, [x9]\n"
179       "incd x9, ALL, MUL #6\n"
180       "ld1b { z25.b }, p0/Z, [x27]\n"
181       "incd x27, ALL, MUL #6\n"
182       "ld1b { z16.b }, p0/Z, [x26]\n"
183       "zip1 z17.b, z18.b, z16.b\n"
184       "ld1b { z24.b }, p0/Z, [x25]\n"
185       "incd x26, ALL, MUL #6\n"
186       "zip2 z19.b, z18.b, z16.b\n"
187       "ld1b { z23.b }, p0/Z, [x24]\n"
188       "incd x25, ALL, MUL #6\n"
189       "zip1 z16.b, z25.b, z24.b\n"
190       "ld1b { z22.b }, p0/Z, [x23]\n"
191       "incd x24, ALL, MUL #6\n"
192       "zip1 z18.b, z17.b, z16.b\n"
193       "ld1b { z21.b }, p0/Z, [x22]\n"
194       "incd x23, ALL, MUL #6\n"
195       "zip2 z17.b, z17.b, z16.b\n"
196       "ld1b { z20.b }, p0/Z, [x21]\n"
197       "incd x22, ALL, MUL #6\n"
198       "zip2 z16.b, z25.b, z24.b\n"
199       "st1b { z18.b }, p1, [x28]\n"
200       "incd x21, ALL, MUL #6\n"
201       "zip1 z16.b, z19.b, z16.b\n"
202       "st1b { z17.b }, p1, [x28, #1, MUL VL]\n"
203       "decw x20, ALL, MUL #3\n"
204       "zip1 z19.b, z23.b, z21.b\n"
205       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
206       "cmp x20, #0x0\n"
207       "zip1 z18.b, z22.b, z20.b\n"
208       "zip2 z17.b, z23.b, z21.b\n"
209       "zip1 z16.b, z19.b, z18.b\n"
210       "st1b { z16.b }, p1, [x28, #3, MUL VL]\n"
211       "zip2 z16.b, z19.b, z18.b\n"
212       "st1b { z16.b }, p1, [x28, #4, MUL VL]\n"
213       "zip2 z16.b, z22.b, z20.b\n"
214       "zip1 z16.b, z17.b, z16.b\n"
215       "st1b { z16.b }, p1, [x28, #5, MUL VL]\n"
216       "add x28, x28, %x[out_stride]\n"
217       "bgt 4b\n"
218       "5:"  // Main row loop: Column loop skip
219       "addvl %x[out], %x[out], #6\n"
220       "cmp %x[height], #0x8\n"
221       "bge 1b\n"
222       "cbz %x[height], 12f\n"
223       "6:"  // Main loop skip
224 
225       "7:"  // Tail row loop: Head
226       "mov x9, %x[in]\n"
227       "mov x28, %x[out]\n"
228       "add x27, x9, %x[in_stride]\n"
229       "add x26, x27, %x[in_stride]\n"
230       "add x25, x26, %x[in_stride]\n"
231       "add %x[in], x25, %x[in_stride]\n"
232       "cmp %x[height], #0x3\n"
233       "csel x25, x25, %x[pad_row], GT\n"
234       "csel x26, x26, %x[pad_row], GE\n"
235       "cmp %x[height], #0x1\n"
236       "csel x27, x27, %x[pad_row], GT\n"
237       "sub %x[height], %x[height], #0x4\n"
238       "mov x20, %x[width]\n"
239       "cntb x19, ALL, MUL #3\n"
240       "cmp x20, x19\n"
241       "blt 9f\n"
242       "8:"  // Tail row loop: Unroll column loop
243       "ld1b { z19.b }, p1/Z, [x9]\n"
244       "sub x20, x20, x19\n"
245       "ld1b { z18.b }, p1/Z, [x9, #1, MUL VL]\n"
246       "cmp x20, x19\n"
247       "ld1b { z30.b }, p1/Z, [x9, #2, MUL VL]\n"
248       "addvl x9, x9, #3\n"
249       "ld1b { z29.b }, p1/Z, [x27]\n"
250       "ld1b { z28.b }, p1/Z, [x27, #1, MUL VL]\n"
251       "ld1b { z27.b }, p1/Z, [x27, #2, MUL VL]\n"
252       "addvl x27, x27, #3\n"
253       "ld1b { z16.b }, p1/Z, [x26]\n"
254       "zip1 z26.b, z19.b, z16.b\n"
255       "ld1b { z17.b }, p1/Z, [x26, #1, MUL VL]\n"
256       "zip2 z25.b, z19.b, z16.b\n"
257       "ld1b { z24.b }, p1/Z, [x26, #2, MUL VL]\n"
258       "addvl x26, x26, #3\n"
259       "zip1 z23.b, z18.b, z17.b\n"
260       "ld1b { z16.b }, p1/Z, [x25]\n"
261       "zip2 z22.b, z18.b, z17.b\n"
262       "ld1b { z21.b }, p1/Z, [x25, #1, MUL VL]\n"
263       "zip1 z20.b, z30.b, z24.b\n"
264       "ld1b { z19.b }, p1/Z, [x25, #2, MUL VL]\n"
265       "addvl x25, x25, #3\n"
266       "zip1 z18.b, z29.b, z16.b\n"
267       "zip2 z17.b, z29.b, z16.b\n"
268       "zip1 z16.b, z26.b, z18.b\n"
269       "st1b { z16.b }, p1, [x28]\n"
270       "zip2 z16.b, z26.b, z18.b\n"
271       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
272       "zip1 z16.b, z25.b, z17.b\n"
273       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
274       "add x28, x28, %x[out_stride]\n"
275       "zip2 z16.b, z25.b, z17.b\n"
276       "st1b { z16.b }, p1, [x28]\n"
277       "zip1 z18.b, z28.b, z21.b\n"
278       "zip2 z17.b, z28.b, z21.b\n"
279       "zip1 z16.b, z23.b, z18.b\n"
280       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
281       "zip2 z16.b, z23.b, z18.b\n"
282       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
283       "add x28, x28, %x[out_stride]\n"
284       "zip1 z16.b, z22.b, z17.b\n"
285       "st1b { z16.b }, p1, [x28]\n"
286       "zip2 z16.b, z22.b, z17.b\n"
287       "zip1 z17.b, z27.b, z19.b\n"
288       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
289       "zip1 z16.b, z20.b, z17.b\n"
290       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
291       "add x28, x28, %x[out_stride]\n"
292       "zip2 z16.b, z20.b, z17.b\n"
293       "st1b { z16.b }, p1, [x28]\n"
294       "zip2 z18.b, z30.b, z24.b\n"
295       "zip2 z17.b, z27.b, z19.b\n"
296       "zip1 z16.b, z18.b, z17.b\n"
297       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
298       "zip2 z16.b, z18.b, z17.b\n"
299       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
300       "add x28, x28, %x[out_stride]\n"
301       "bge 8b\n"
302       "9:"  // Tail row loop: Unroll column loop skip
303       "cbz x20, 11f\n"
304       "10:"  // Tail row loop: Column loop
305       "whilelt p0.b, XZR, x20\n"
306       "ld1b { z18.b }, p0/Z, [x9]\n"
307       "incd x9, ALL, MUL #6\n"
308       "ld1b { z21.b }, p0/Z, [x27]\n"
309       "incd x27, ALL, MUL #6\n"
310       "ld1b { z17.b }, p0/Z, [x26]\n"
311       "zip1 z20.b, z18.b, z17.b\n"
312       "ld1b { z16.b }, p0/Z, [x25]\n"
313       "incd x26, ALL, MUL #6\n"
314       "zip2 z19.b, z18.b, z17.b\n"
315       "incd x25, ALL, MUL #6\n"
316       "decw x20, ALL, MUL #3\n"
317       "zip1 z18.b, z21.b, z16.b\n"
318       "cmp x20, #0x0\n"
319       "zip2 z17.b, z21.b, z16.b\n"
320       "zip1 z16.b, z20.b, z18.b\n"
321       "st1b { z16.b }, p1, [x28]\n"
322       "zip2 z16.b, z20.b, z18.b\n"
323       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
324       "zip1 z16.b, z19.b, z17.b\n"
325       "st1b { z16.b }, p1, [x28, #2, MUL VL]\n"
326       "add x28, x28, %x[out_stride]\n"
327       "bgt 10b\n"
328       "11:"  // Tail row loop: Column loop skip
329       "addvl %x[out], %x[out], #3\n"
330       "cmp %x[height], #0x1\n"
331       "bge 7b\n"
332       "12:"  // Done
333 
334       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
335       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
336       : "cc", "memory", "p0", "p1", "x9", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7", "z8", "z9", "z10", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31"
337     );
338 }
339 
340 } // anonymous namespace
341 
342 template<>
Transform(uint8_t * out,const uint8_t * in,int stride,int x0,int xmax,int k0,int kmax)343 void Transform<3, 4, true, VLType::SVE>(
344     uint8_t *out, const uint8_t *in, int stride, int x0, int xmax, int k0, int kmax)
345 {
346     sve_transpose_interleave_3VL_1x4(
347         reinterpret_cast<uint8_t *>(out),
348         reinterpret_cast<const uint8_t *>(in + k0 * stride + x0),
349         (xmax-x0) * sizeof(uint8_t) / 1,
350         stride * sizeof(uint8_t),
351         (kmax-k0)
352     );
353 }
354 
355 template<>
Transform(int8_t * out,const int8_t * in,int stride,int x0,int xmax,int k0,int kmax)356 void Transform<3, 4, true, VLType::SVE>(
357     int8_t *out, const int8_t *in, int stride, int x0, int xmax, int k0, int kmax)
358 {
359     sve_transpose_interleave_3VL_1x4(
360         reinterpret_cast<uint8_t *>(out),
361         reinterpret_cast<const uint8_t *>(in + k0 * stride + x0),
362         (xmax-x0) * sizeof(int8_t) / 1,
363         stride * sizeof(int8_t),
364         (kmax-k0)
365     );
366 }
367 
368 #endif
369