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_1VL_1x4(uint8_t * out,const uint8_t * in,size_t width,size_t in_stride,size_t height)32 void sve_transpose_interleave_1VL_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 = 1 * 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 #2\n"
60       "cmp x20, x19\n"
61       "blt 3f\n"
62       "2:"  // Main row loop: Unroll column loop
63       "ld1b { z17.b }, p1/Z, [x9]\n"
64       "sub x20, x20, x19\n"
65       "ld1b { z3.b }, p1/Z, [x9, #1, MUL VL]\n"
66       "addvl x9, x9, #2\n"
67       "ld1b { z21.b }, p1/Z, [x27]\n"
68       "cmp x20, x19\n"
69       "ld1b { z2.b }, p1/Z, [x27, #1, MUL VL]\n"
70       "addvl x27, x27, #2\n"
71       "ld1b { z16.b }, p1/Z, [x26]\n"
72       "zip1 z20.b, z17.b, z16.b\n"
73       "ld1b { z1.b }, p1/Z, [x26, #1, MUL VL]\n"
74       "addvl x26, x26, #2\n"
75       "zip2 z19.b, z17.b, z16.b\n"
76       "ld1b { z17.b }, p1/Z, [x25]\n"
77       "ld1b { z0.b }, p1/Z, [x25, #1, MUL VL]\n"
78       "zip1 z31.b, z3.b, z1.b\n"
79       "ld1b { z30.b }, p1/Z, [x24]\n"
80       "addvl x25, x25, #2\n"
81       "zip1 z16.b, z21.b, z17.b\n"
82       "ld1b { z29.b }, p1/Z, [x24, #1, MUL VL]\n"
83       "addvl x24, x24, #2\n"
84       "zip1 z18.b, z20.b, z16.b\n"
85       "ld1b { z28.b }, p1/Z, [x23]\n"
86       "zip2 z27.b, z20.b, z16.b\n"
87       "ld1b { z26.b }, p1/Z, [x23, #1, MUL VL]\n"
88       "addvl x23, x23, #2\n"
89       "zip2 z17.b, z21.b, z17.b\n"
90       "ld1b { z16.b }, p1/Z, [x22]\n"
91       "zip1 z25.b, z2.b, z0.b\n"
92       "ld1b { z24.b }, p1/Z, [x22, #1, MUL VL]\n"
93       "addvl x22, x22, #2\n"
94       "zip1 z23.b, z19.b, z17.b\n"
95       "ld1b { z22.b }, p1/Z, [x21]\n"
96       "zip2 z20.b, z19.b, z17.b\n"
97       "ld1b { z21.b }, p1/Z, [x21, #1, MUL VL]\n"
98       "addvl x21, x21, #2\n"
99       "zip1 z19.b, z30.b, z16.b\n"
100       "st1b { z18.b }, p1, [x28]\n"
101       "zip2 z18.b, z30.b, z16.b\n"
102       "zip1 z17.b, z28.b, z22.b\n"
103       "zip1 z16.b, z19.b, z17.b\n"
104       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
105       "add x28, x28, %x[out_stride]\n"
106       "zip2 z16.b, z19.b, z17.b\n"
107       "st1b { z27.b }, p1, [x28]\n"
108       "zip2 z17.b, z28.b, z22.b\n"
109       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
110       "add x28, x28, %x[out_stride]\n"
111       "zip1 z16.b, z18.b, z17.b\n"
112       "st1b { z23.b }, p1, [x28]\n"
113       "zip2 z17.b, z18.b, z17.b\n"
114       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
115       "add x28, x28, %x[out_stride]\n"
116       "zip1 z16.b, z31.b, z25.b\n"
117       "st1b { z20.b }, p1, [x28]\n"
118       "zip1 z19.b, z29.b, z24.b\n"
119       "st1b { z17.b }, p1, [x28, #1, MUL VL]\n"
120       "add x28, x28, %x[out_stride]\n"
121       "zip1 z18.b, z26.b, z21.b\n"
122       "st1b { z16.b }, p1, [x28]\n"
123       "zip2 z17.b, z31.b, z25.b\n"
124       "zip1 z16.b, z19.b, z18.b\n"
125       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
126       "add x28, x28, %x[out_stride]\n"
127       "zip2 z16.b, z19.b, z18.b\n"
128       "st1b { z17.b }, p1, [x28]\n"
129       "zip2 z20.b, z3.b, z1.b\n"
130       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
131       "add x28, x28, %x[out_stride]\n"
132       "zip2 z19.b, z2.b, z0.b\n"
133       "zip2 z18.b, z29.b, z24.b\n"
134       "zip1 z16.b, z20.b, z19.b\n"
135       "st1b { z16.b }, p1, [x28]\n"
136       "zip2 z17.b, z26.b, z21.b\n"
137       "zip1 z16.b, z18.b, z17.b\n"
138       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
139       "add x28, x28, %x[out_stride]\n"
140       "zip2 z16.b, z20.b, z19.b\n"
141       "st1b { z16.b }, p1, [x28]\n"
142       "zip2 z16.b, z18.b, z17.b\n"
143       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
144       "add x28, x28, %x[out_stride]\n"
145       "bge 2b\n"
146       "3:"  // Main row loop: Unroll column loop skip
147       "cbz x20, 5f\n"
148       "4:"  // Main row loop: Column loop
149       "whilelt p0.b, XZR, x20\n"
150       "ld1b { z18.b }, p0/Z, [x9]\n"
151       "incd x9, ALL, MUL #2\n"
152       "ld1b { z17.b }, p0/Z, [x27]\n"
153       "incd x27, ALL, MUL #2\n"
154       "ld1b { z16.b }, p0/Z, [x26]\n"
155       "zip1 z18.b, z18.b, z16.b\n"
156       "ld1b { z16.b }, p0/Z, [x25]\n"
157       "incd x26, ALL, MUL #2\n"
158       "zip1 z16.b, z17.b, z16.b\n"
159       "ld1b { z17.b }, p0/Z, [x24]\n"
160       "incd x25, ALL, MUL #2\n"
161       "zip1 z19.b, z18.b, z16.b\n"
162       "ld1b { z18.b }, p0/Z, [x23]\n"
163       "incd x24, ALL, MUL #2\n"
164       "ld1b { z16.b }, p0/Z, [x22]\n"
165       "zip1 z17.b, z17.b, z16.b\n"
166       "ld1b { z16.b }, p0/Z, [x21]\n"
167       "incd x23, ALL, MUL #2\n"
168       "zip1 z16.b, z18.b, z16.b\n"
169       "st1b { z19.b }, p1, [x28]\n"
170       "incd x22, ALL, MUL #2\n"
171       "zip1 z16.b, z17.b, z16.b\n"
172       "st1b { z16.b }, p1, [x28, #1, MUL VL]\n"
173       "incd x21, ALL, MUL #2\n"
174       "add x28, x28, %x[out_stride]\n"
175       "decw x20\n"
176       "cmp x20, #0x0\n"
177       "bgt 4b\n"
178       "5:"  // Main row loop: Column loop skip
179       "addvl %x[out], %x[out], #2\n"
180       "cmp %x[height], #0x8\n"
181       "bge 1b\n"
182       "cbz %x[height], 12f\n"
183       "6:"  // Main loop skip
184 
185       "7:"  // Tail row loop: Head
186       "mov x9, %x[in]\n"
187       "mov x28, %x[out]\n"
188       "add x27, x9, %x[in_stride]\n"
189       "add x26, x27, %x[in_stride]\n"
190       "add x25, x26, %x[in_stride]\n"
191       "add %x[in], x25, %x[in_stride]\n"
192       "cmp %x[height], #0x3\n"
193       "csel x25, x25, %x[pad_row], GT\n"
194       "csel x26, x26, %x[pad_row], GE\n"
195       "cmp %x[height], #0x1\n"
196       "csel x27, x27, %x[pad_row], GT\n"
197       "sub %x[height], %x[height], #0x4\n"
198       "mov x20, %x[width]\n"
199       "cntb x19, ALL, MUL #2\n"
200       "cmp x20, x19\n"
201       "blt 9f\n"
202       "8:"  // Tail row loop: Unroll column loop
203       "ld1b { z19.b }, p1/Z, [x9]\n"
204       "sub x20, x20, x19\n"
205       "ld1b { z18.b }, p1/Z, [x9, #1, MUL VL]\n"
206       "addvl x9, x9, #2\n"
207       "ld1b { z25.b }, p1/Z, [x27]\n"
208       "cmp x20, x19\n"
209       "ld1b { z24.b }, p1/Z, [x27, #1, MUL VL]\n"
210       "addvl x27, x27, #2\n"
211       "ld1b { z17.b }, p1/Z, [x26]\n"
212       "zip1 z23.b, z19.b, z17.b\n"
213       "ld1b { z16.b }, p1/Z, [x26, #1, MUL VL]\n"
214       "addvl x26, x26, #2\n"
215       "zip2 z22.b, z19.b, z17.b\n"
216       "ld1b { z21.b }, p1/Z, [x25]\n"
217       "ld1b { z20.b }, p1/Z, [x25, #1, MUL VL]\n"
218       "zip1 z19.b, z18.b, z16.b\n"
219       "addvl x25, x25, #2\n"
220       "zip2 z18.b, z18.b, z16.b\n"
221       "zip1 z17.b, z25.b, z21.b\n"
222       "zip1 z16.b, z23.b, z17.b\n"
223       "st1b { z16.b }, p1, [x28]\n"
224       "add x28, x28, %x[out_stride]\n"
225       "zip2 z16.b, z23.b, z17.b\n"
226       "st1b { z16.b }, p1, [x28]\n"
227       "zip2 z17.b, z25.b, z21.b\n"
228       "add x28, x28, %x[out_stride]\n"
229       "zip1 z16.b, z22.b, z17.b\n"
230       "st1b { z16.b }, p1, [x28]\n"
231       "zip2 z16.b, z22.b, z17.b\n"
232       "add x28, x28, %x[out_stride]\n"
233       "zip1 z17.b, z24.b, z20.b\n"
234       "st1b { z16.b }, p1, [x28]\n"
235       "add x28, x28, %x[out_stride]\n"
236       "zip1 z16.b, z19.b, z17.b\n"
237       "st1b { z16.b }, p1, [x28]\n"
238       "zip2 z16.b, z19.b, z17.b\n"
239       "add x28, x28, %x[out_stride]\n"
240       "zip2 z17.b, z24.b, z20.b\n"
241       "st1b { z16.b }, p1, [x28]\n"
242       "add x28, x28, %x[out_stride]\n"
243       "zip1 z16.b, z18.b, z17.b\n"
244       "st1b { z16.b }, p1, [x28]\n"
245       "zip2 z16.b, z18.b, z17.b\n"
246       "add x28, x28, %x[out_stride]\n"
247       "st1b { z16.b }, p1, [x28]\n"
248       "add x28, x28, %x[out_stride]\n"
249       "bge 8b\n"
250       "9:"  // Tail row loop: Unroll column loop skip
251       "cbz x20, 11f\n"
252       "10:"  // Tail row loop: Column loop
253       "whilelt p0.b, XZR, x20\n"
254       "ld1b { z17.b }, p0/Z, [x9]\n"
255       "incd x9, ALL, MUL #2\n"
256       "ld1b { z18.b }, p0/Z, [x27]\n"
257       "incd x27, ALL, MUL #2\n"
258       "ld1b { z16.b }, p0/Z, [x26]\n"
259       "zip1 z17.b, z17.b, z16.b\n"
260       "ld1b { z16.b }, p0/Z, [x25]\n"
261       "incd x26, ALL, MUL #2\n"
262       "zip1 z16.b, z18.b, z16.b\n"
263       "incd x25, ALL, MUL #2\n"
264       "decw x20\n"
265       "zip1 z16.b, z17.b, z16.b\n"
266       "st1b { z16.b }, p1, [x28]\n"
267       "add x28, x28, %x[out_stride]\n"
268       "cmp x20, #0x0\n"
269       "bgt 10b\n"
270       "11:"  // Tail row loop: Column loop skip
271       "addvl %x[out], %x[out], #1\n"
272       "cmp %x[height], #0x1\n"
273       "bge 7b\n"
274       "12:"  // Done
275 
276       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
277       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
278       : "cc", "memory", "p0", "p1", "x9", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "z0", "z1", "z2", "z3", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31"
279     );
280 }
281 
282 } // anonymous namespace
283 
284 template<>
Transform(uint8_t * out,const uint8_t * in,int stride,int x0,int xmax,int k0,int kmax)285 void Transform<1, 4, true, VLType::SVE>(
286     uint8_t *out, const uint8_t *in, int stride, int x0, int xmax, int k0, int kmax)
287 {
288     sve_transpose_interleave_1VL_1x4(
289         reinterpret_cast<uint8_t *>(out),
290         reinterpret_cast<const uint8_t *>(in + k0 * stride + x0),
291         (xmax-x0) * sizeof(uint8_t) / 1,
292         stride * sizeof(uint8_t),
293         (kmax-k0)
294     );
295 }
296 
297 template<>
Transform(int8_t * out,const int8_t * in,int stride,int x0,int xmax,int k0,int kmax)298 void Transform<1, 4, true, VLType::SVE>(
299     int8_t *out, const int8_t *in, int stride, int x0, int xmax, int k0, int kmax)
300 {
301     sve_transpose_interleave_1VL_1x4(
302         reinterpret_cast<uint8_t *>(out),
303         reinterpret_cast<const uint8_t *>(in + k0 * stride + x0),
304         (xmax-x0) * sizeof(int8_t) / 1,
305         stride * sizeof(int8_t),
306         (kmax-k0)
307     );
308 }
309 
310 #endif
311