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