blob: 3d14383a6490fee9d62876cfea90bb214343c11f [file] [log] [blame]
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01001/*
Michael Tyler7d9a6262023-02-01 16:37:07 +00002 * Copyright (c) 2021, 2023 Arm Limited.
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01003 *
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 *
Michael Tyler7d9a6262023-02-01 16:37:07 +000013 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010015 *
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
Michael Tyler7d9a6262023-02-01 16:37:07 +000020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010023 */
24
25#pragma once
26
27#ifdef __ARM_FEATURE_SVE
28
29
30namespace {
31
32void sve_transpose_interleave_3VL_2x2(uint16_t *out, const uint16_t *in, size_t width, size_t in_stride, size_t height)
33{
34 uint16_t *pad_row = reinterpret_cast<uint16_t *>(alloca(width * sizeof(uint16_t)));
35
36 if (height % 2) {
37 memset(pad_row, 0, width * sizeof(uint16_t));
38 }
39
40 size_t out_stride = 3 * roundup<size_t>(height, 2) * get_vector_length<uint16_t>();
41
42 __asm__ __volatile__(
Michael Tylerbe13cea2023-01-17 11:04:14 +000043 "cmp %x[height], #0x8\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000044 "ptrue p2.b\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010045 "blt 6f\n"
46 "1:" // Main row loop: Head
Michael Tyler7d9a6262023-02-01 16:37:07 +000047 "mov x12, %x[in]\n"
48 "add x11, x12, %x[in_stride]\n"
49 "add x10, x11, %x[in_stride]\n"
50 "add x9, x10, %x[in_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010051 "add x28, x9, %x[in_stride]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000052 "mov x27, %x[width]\n"
53 "cnth x26, ALL, MUL #3\n"
54 "add x25, x28, %x[in_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010055 "add x24, x25, %x[in_stride]\n"
56 "add x23, x24, %x[in_stride]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000057 "cmp x27, x26\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010058 "add %x[in], x23, %x[in_stride]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000059 "mov x22, %x[out]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010060 "sub %x[height], %x[height], #0x8\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010061 "blt 3f\n"
62 "2:" // Main row loop: Unroll column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +000063 "ld1h { z17.h }, p2/Z, [x12]\n"
64 "ld1h { z23.h }, p2/Z, [x12, #1, MUL VL]\n"
65 "mov x21, x22\n"
66 "add x22, x22, %x[out_stride]\n"
67 "ld1h { z16.h }, p2/Z, [x11]\n"
68 "ld1h { z20.h }, p2/Z, [x11, #1, MUL VL]\n"
69 "zip1 z9.h, z17.h, z16.h\n"
70 "zip2 z8.h, z17.h, z16.h\n"
71 "ld1h { z17.h }, p2/Z, [x10]\n"
72 "ld1h { z22.h }, p2/Z, [x10, #1, MUL VL]\n"
73 "zip1 z7.h, z23.h, z20.h\n"
74 "mov x20, x22\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010075 "ld1h { z16.h }, p2/Z, [x9]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000076 "ld1h { z21.h }, p2/Z, [x9, #1, MUL VL]\n"
77 "zip1 z6.h, z17.h, z16.h\n"
78 "zip2 z5.h, z17.h, z16.h\n"
79 "ld1h { z18.h }, p2/Z, [x28]\n"
80 "ld1h { z17.h }, p2/Z, [x25]\n"
81 "zip1 z4.h, z22.h, z21.h\n"
82 "zip1 z3.h, z18.h, z17.h\n"
83 "ld1h { z19.h }, p2/Z, [x12, #2, MUL VL]\n"
84 "ld1h { z16.h }, p2/Z, [x11, #2, MUL VL]\n"
85 "zip2 z2.h, z18.h, z17.h\n"
86 "zip2 z1.h, z23.h, z20.h\n"
87 "ld1h { z18.h }, p2/Z, [x10, #2, MUL VL]\n"
88 "ld1h { z17.h }, p2/Z, [x9, #2, MUL VL]\n"
89 "zip1 z0.h, z19.h, z16.h\n"
90 "zip2 z31.h, z19.h, z16.h\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010091 "ld1h { z20.h }, p2/Z, [x28, #1, MUL VL]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000092 "ld1h { z30.h }, p2/Z, [x28, #2, MUL VL]\n"
93 "zip2 z29.h, z22.h, z21.h\n"
94 "zip1 z28.h, z18.h, z17.h\n"
95 "ld1h { z16.h }, p2/Z, [x25, #1, MUL VL]\n"
96 "ld1h { z19.h }, p2/Z, [x25, #2, MUL VL]\n"
97 "zip1 z27.h, z20.h, z16.h\n"
98 "zip2 z26.h, z18.h, z17.h\n"
99 "ld1h { z17.h }, p2/Z, [x24]\n"
100 "ld1h { z18.h }, p2/Z, [x24, #1, MUL VL]\n"
101 "zip2 z25.h, z20.h, z16.h\n"
102 "zip1 z24.h, z30.h, z19.h\n"
103 "ld1h { z23.h }, p2/Z, [x24, #2, MUL VL]\n"
104 "ld1h { z16.h }, p2/Z, [x23]\n"
105 "zip1 z22.h, z17.h, z16.h\n"
106 "zip2 z21.h, z17.h, z16.h\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100107 "ld1h { z17.h }, p2/Z, [x23, #1, MUL VL]\n"
108 "ld1h { z16.h }, p2/Z, [x23, #2, MUL VL]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000109 "st1h { z9.h }, p2, [x21]\n"
110 "zip1 z20.h, z18.h, z17.h\n"
111 "st1h { z8.h }, p2, [x21, #1, MUL VL]\n"
112 "sub x27, x27, x26\n"
113 "cmp x27, x26\n"
114 "zip2 z19.h, z30.h, z19.h\n"
115 "st1h { z7.h }, p2, [x21, #2, MUL VL]\n"
116 "addvl x12, x12, #3\n"
117 "addvl x11, x11, #3\n"
118 "zip2 z18.h, z18.h, z17.h\n"
119 "st1h { z6.h }, p2, [x21, #3, MUL VL]\n"
120 "addvl x10, x10, #3\n"
121 "addvl x9, x9, #3\n"
122 "zip1 z17.h, z23.h, z16.h\n"
123 "st1h { z5.h }, p2, [x21, #4, MUL VL]\n"
124 "addvl x28, x28, #3\n"
125 "addvl x25, x25, #3\n"
126 "zip2 z16.h, z23.h, z16.h\n"
127 "st1h { z4.h }, p2, [x21, #5, MUL VL]\n"
128 "addvl x24, x24, #3\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100129 "addvl x23, x23, #3\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000130 "st1h { z3.h }, p2, [x21, #6, MUL VL]\n"
131 "add x22, x22, %x[out_stride]\n"
132 "st1h { z2.h }, p2, [x21, #7, MUL VL]\n"
133 "addvl x21, x21, #12\n"
134 "st1h { z27.h }, p2, [x21, #-4, MUL VL]\n"
135 "st1h { z22.h }, p2, [x21, #-3, MUL VL]\n"
136 "st1h { z21.h }, p2, [x21, #-2, MUL VL]\n"
137 "st1h { z20.h }, p2, [x21, #-1, MUL VL]\n"
138 "st1h { z1.h }, p2, [x20]\n"
139 "st1h { z0.h }, p2, [x20, #1, MUL VL]\n"
140 "st1h { z31.h }, p2, [x20, #2, MUL VL]\n"
141 "st1h { z29.h }, p2, [x20, #3, MUL VL]\n"
142 "st1h { z28.h }, p2, [x20, #4, MUL VL]\n"
143 "st1h { z26.h }, p2, [x20, #5, MUL VL]\n"
144 "st1h { z25.h }, p2, [x20, #6, MUL VL]\n"
145 "st1h { z24.h }, p2, [x20, #7, MUL VL]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100146 "addvl x20, x20, #12\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000147 "st1h { z19.h }, p2, [x20, #-4, MUL VL]\n"
148 "st1h { z18.h }, p2, [x20, #-3, MUL VL]\n"
149 "st1h { z17.h }, p2, [x20, #-2, MUL VL]\n"
150 "st1h { z16.h }, p2, [x20, #-1, MUL VL]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100151 "bge 2b\n"
152 "3:" // Main row loop: Unroll column loop skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000153 "cbz x27, 5f\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100154 "4:" // Main row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000155 "mov x20, x27\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000156 "whilelt p1.h, XZR, x20\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000157 "ld1h { z0.h }, p1/Z, [x12]\n"
158 "ld1h { z16.h }, p1/Z, [x11]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000159 "dech x20\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000160 "whilelt p0.h, XZR, x20\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000161 "ld1h { z21.h }, p0/Z, [x12, #1, MUL VL]\n"
162 "ld1h { z19.h }, p0/Z, [x11, #1, MUL VL]\n"
163 "ld1h { z31.h }, p1/Z, [x10]\n"
164 "ld1h { z30.h }, p0/Z, [x10, #1, MUL VL]\n"
165 "mov x20, x22\n"
166 "decw x27, ALL, MUL #3\n"
167 "ld1h { z18.h }, p1/Z, [x9]\n"
168 "ld1h { z29.h }, p0/Z, [x9, #1, MUL VL]\n"
169 "addvl x12, x12, #1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100170 "addvl x11, x11, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000171 "ld1h { z28.h }, p1/Z, [x28]\n"
172 "ld1h { z20.h }, p1/Z, [x25]\n"
173 "addvl x10, x10, #1\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000174 "addvl x9, x9, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000175 "ld1h { z27.h }, p0/Z, [x28, #1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000176 "addvl x28, x28, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000177 "ld1h { z26.h }, p0/Z, [x25, #1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000178 "addvl x25, x25, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000179 "ld1h { z25.h }, p1/Z, [x24]\n"
180 "ld1h { z24.h }, p0/Z, [x24, #1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000181 "addvl x24, x24, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000182 "zip1 z17.h, z0.h, z16.h\n"
183 "ld1h { z23.h }, p1/Z, [x23]\n"
184 "ld1h { z22.h }, p0/Z, [x23, #1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000185 "addvl x23, x23, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000186 "zip2 z16.h, z0.h, z16.h\n"
187 "zip1 z21.h, z21.h, z19.h\n"
188 "zip1 z19.h, z31.h, z18.h\n"
189 "st1h { z17.h }, p2, [x20]\n"
190 "cmp x27, #0x0\n"
191 "zip2 z18.h, z31.h, z18.h\n"
192 "zip1 z17.h, z30.h, z29.h\n"
193 "st1h { z16.h }, p2, [x20, #1, MUL VL]\n"
194 "incd x12, ALL, MUL #4\n"
195 "zip1 z16.h, z28.h, z20.h\n"
196 "zip2 z20.h, z28.h, z20.h\n"
197 "st1h { z21.h }, p2, [x20, #2, MUL VL]\n"
198 "incd x11, ALL, MUL #4\n"
199 "st1h { z19.h }, p2, [x20, #3, MUL VL]\n"
200 "incd x10, ALL, MUL #4\n"
201 "incd x9, ALL, MUL #4\n"
202 "zip1 z19.h, z27.h, z26.h\n"
203 "st1h { z18.h }, p2, [x20, #4, MUL VL]\n"
204 "incd x28, ALL, MUL #4\n"
205 "incd x25, ALL, MUL #4\n"
206 "zip1 z18.h, z25.h, z23.h\n"
207 "st1h { z17.h }, p2, [x20, #5, MUL VL]\n"
208 "incd x24, ALL, MUL #4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100209 "incd x23, ALL, MUL #4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000210 "zip2 z17.h, z25.h, z23.h\n"
211 "st1h { z16.h }, p2, [x20, #6, MUL VL]\n"
212 "zip1 z16.h, z24.h, z22.h\n"
213 "add x22, x22, %x[out_stride]\n"
214 "st1h { z20.h }, p2, [x20, #7, MUL VL]\n"
215 "addvl x20, x20, #12\n"
216 "st1h { z19.h }, p2, [x20, #-4, MUL VL]\n"
217 "st1h { z18.h }, p2, [x20, #-3, MUL VL]\n"
218 "st1h { z17.h }, p2, [x20, #-2, MUL VL]\n"
219 "st1h { z16.h }, p2, [x20, #-1, MUL VL]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100220 "bgt 4b\n"
221 "5:" // Main row loop: Column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000222 "cmp %x[height], #0x8\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000223 "addvl %x[out], %x[out], #12\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100224 "bge 1b\n"
225 "cbz %x[height], 12f\n"
226 "6:" // Main loop skip
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100227 "7:" // Tail row loop: Head
Michael Tyler7d9a6262023-02-01 16:37:07 +0000228 "mov x12, %x[in]\n"
229 "mov x21, %x[width]\n"
230 "cnth x20, ALL, MUL #3\n"
231 "add x11, x12, %x[in_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100232 "cmp %x[height], #0x1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000233 "add %x[in], x11, %x[in_stride]\n"
234 "csel x11, x11, %x[pad_row], GT\n"
235 "cmp x21, x20\n"
236 "mov x22, %x[out]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100237 "sub %x[height], %x[height], #0x2\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100238 "blt 9f\n"
239 "8:" // Tail row loop: Unroll column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000240 "ld1h { z17.h }, p2/Z, [x12]\n"
241 "ld1h { z22.h }, p2/Z, [x12, #1, MUL VL]\n"
242 "sub x21, x21, x20\n"
243 "cmp x21, x20\n"
244 "ld1h { z16.h }, p2/Z, [x11]\n"
245 "ld1h { z21.h }, p2/Z, [x11, #1, MUL VL]\n"
246 "zip1 z18.h, z17.h, z16.h\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000247 "zip2 z17.h, z17.h, z16.h\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000248 "ld1h { z20.h }, p2/Z, [x12, #2, MUL VL]\n"
249 "ld1h { z19.h }, p2/Z, [x11, #2, MUL VL]\n"
250 "zip1 z16.h, z22.h, z21.h\n"
251 "st1h { z18.h }, p2, [x22]\n"
252 "st1h { z17.h }, p2, [x22, #1, MUL VL]\n"
253 "addvl x12, x12, #3\n"
254 "addvl x11, x11, #3\n"
255 "zip2 z18.h, z22.h, z21.h\n"
256 "st1h { z16.h }, p2, [x22, #2, MUL VL]\n"
257 "add x22, x22, %x[out_stride]\n"
258 "zip1 z17.h, z20.h, z19.h\n"
259 "zip2 z16.h, z20.h, z19.h\n"
260 "st1h { z18.h }, p2, [x22]\n"
261 "st1h { z17.h }, p2, [x22, #1, MUL VL]\n"
262 "st1h { z16.h }, p2, [x22, #2, MUL VL]\n"
263 "add x22, x22, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100264 "bge 8b\n"
265 "9:" // Tail row loop: Unroll column loop skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000266 "cbz x21, 11f\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100267 "10:" // Tail row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000268 "mov x20, x21\n"
269 "whilelt p0.h, XZR, x20\n"
270 "ld1h { z20.h }, p0/Z, [x12]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100271 "ld1h { z17.h }, p0/Z, [x11]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000272 "dech x20\n"
273 "whilelt p0.h, XZR, x20\n"
274 "ld1h { z19.h }, p0/Z, [x12, #1, MUL VL]\n"
275 "ld1h { z16.h }, p0/Z, [x11, #1, MUL VL]\n"
276 "decw x21, ALL, MUL #3\n"
277 "addvl x12, x12, #1\n"
278 "zip1 z18.h, z20.h, z17.h\n"
279 "zip2 z17.h, z20.h, z17.h\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100280 "addvl x11, x11, #1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000281 "cmp x21, #0x0\n"
282 "zip1 z16.h, z19.h, z16.h\n"
283 "st1h { z18.h }, p2, [x22]\n"
284 "incd x12, ALL, MUL #4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100285 "incd x11, ALL, MUL #4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000286 "st1h { z17.h }, p2, [x22, #1, MUL VL]\n"
287 "st1h { z16.h }, p2, [x22, #2, MUL VL]\n"
288 "add x22, x22, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100289 "bgt 10b\n"
290 "11:" // Tail row loop: Column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000291 "cmp %x[height], #0x1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000292 "addvl %x[out], %x[out], #3\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100293 "bge 7b\n"
294 "12:" // Done
295
296 : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
297 : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
Michael Tyler7d9a6262023-02-01 16:37:07 +0000298 : "cc", "memory", "p0", "p1", "p2", "x9", "x10", "x11", "x12", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7", "z8", "z9", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100299 );
300}
301
302} // anonymous namespace
303
304template<>
305void Transform<3, 2, true, VLType::SVE>(
306 bfloat16 *out, const bfloat16 *in, int stride, int x0, int xmax, int k0, int kmax)
307{
308 sve_transpose_interleave_3VL_2x2(
309 reinterpret_cast<uint16_t *>(out),
310 reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
311 (xmax-x0) * sizeof(bfloat16) / 2,
312 stride * sizeof(bfloat16),
313 (kmax-k0)
314 );
315}
316
317#endif