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