blob: 0a628d372e3b9d553fcc6d95cdcb900b15847e53 [file] [log] [blame]
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01001/*
Michael Tylerbe13cea2023-01-17 11:04:14 +00002 * Copyright (c) 2021 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 Tylerbe13cea2023-01-17 11:04:14 +000013 * The above copyright notice and this permission notice shall be included in
14 * all 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 Tylerbe13cea2023-01-17 11:04:14 +000020 * 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.
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010023 */
24
25#pragma once
26
27#ifdef __aarch64__
28
29namespace {
30
31void a64_transpose_interleave_24_bf16fp32(float *out, const bfloat16 *in, size_t width, size_t in_stride, size_t height)
32{
33 size_t out_stride = 12 * height * sizeof(float);
34
35 __asm__ __volatile__(
36 "cmp %x[height], #0x4\n"
37 "blt 10f\n"
38 "1:" // Main row loop: Head
Michael Tylerbe13cea2023-01-17 11:04:14 +000039 "mov x24, %x[in]\n"
40 "mov x23, %x[out]\n"
41 "add x22, x24, %x[in_stride]\n"
42 "add x21, x22, %x[in_stride]\n"
43 "add x20, x21, %x[in_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010044 "add %x[in], x20, %x[in_stride]\n"
45 "sub %x[height], %x[height], #0x4\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000046 "mov x19, %x[width]\n"
47 "cmp x19, #0x18\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010048 "blt 3f\n"
49 "2:" // Main row loop: Unroll column loop
Michael Tylerbe13cea2023-01-17 11:04:14 +000050 "ldr q17, [x24], #0x10\n"
51 "shll v3.4s, v17.4h, #0x10\n"
52 "ldr q16, [x24], #0x10\n"
53 "sub x19, x19, #0x18\n"
54 "shll2 v23.4s, v17.8h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010055 "ldr q17, [x22], #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000056 "cmp x19, #0x18\n"
57 "shll v22.4s, v16.4h, #0x10\n"
58 "ldr q19, [x24], #0x10\n"
59 "shll2 v2.4s, v16.8h, #0x10\n"
60 "ldr q16, [x22], #0x10\n"
61 "shll v21.4s, v17.4h, #0x10\n"
62 "ldr q18, [x21], #0x10\n"
63 "shll2 v1.4s, v17.8h, #0x10\n"
64 "ldr q0, [x20], #0x10\n"
65 "shll v31.4s, v19.4h, #0x10\n"
66 "ldr q17, [x22], #0x10\n"
67 "shll2 v30.4s, v19.8h, #0x10\n"
68 "shll v29.4s, v16.4h, #0x10\n"
69 "ldr q20, [x21], #0x10\n"
70 "shll2 v28.4s, v16.8h, #0x10\n"
71 "ldr q27, [x20], #0x10\n"
72 "shll v19.4s, v18.4h, #0x10\n"
73 "ldr q16, [x21], #0x10\n"
74 "shll v26.4s, v17.4h, #0x10\n"
75 "shll2 v25.4s, v17.8h, #0x10\n"
76 "ldr q24, [x20], #0x10\n"
77 "shll2 v18.4s, v18.8h, #0x10\n"
78 "str q3, [x23, #0x0]\n"
79 "shll v17.4s, v20.4h, #0x10\n"
80 "str q23, [x23, #0x10]\n"
81 "shll2 v23.4s, v20.8h, #0x10\n"
82 "str q22, [x23, #0x20]\n"
83 "shll v22.4s, v16.4h, #0x10\n"
84 "str q21, [x23, #0x30]\n"
85 "shll2 v21.4s, v16.8h, #0x10\n"
86 "str q1, [x23, #0x40]\n"
87 "shll v16.4s, v0.4h, #0x10\n"
88 "str q29, [x23, #0x50]\n"
89 "shll2 v20.4s, v0.8h, #0x10\n"
90 "str q19, [x23, #0x60]\n"
91 "shll v19.4s, v27.4h, #0x10\n"
92 "str q18, [x23, #0x70]\n"
93 "shll2 v18.4s, v27.8h, #0x10\n"
94 "str q17, [x23, #0x80]\n"
95 "shll v17.4s, v24.4h, #0x10\n"
96 "str q16, [x23, #0x90]\n"
97 "shll2 v16.4s, v24.8h, #0x10\n"
98 "str q20, [x23, #0xa0]\n"
99 "str q19, [x23, #0xb0]\n"
100 "add x23, x23, %x[out_stride]\n"
101 "str q2, [x23, #0x0]\n"
102 "str q31, [x23, #0x10]\n"
103 "str q30, [x23, #0x20]\n"
104 "str q28, [x23, #0x30]\n"
105 "str q26, [x23, #0x40]\n"
106 "str q25, [x23, #0x50]\n"
107 "str q23, [x23, #0x60]\n"
108 "str q22, [x23, #0x70]\n"
109 "str q21, [x23, #0x80]\n"
110 "str q18, [x23, #0x90]\n"
111 "str q17, [x23, #0xa0]\n"
112 "str q16, [x23, #0xb0]\n"
113 "add x23, x23, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100114 "bge 2b\n"
115 "3:" // Main row loop: Unroll column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000116 "cmp x19, #0xc\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100117 "blt 5f\n"
118 "4:" // Main row loop: Column loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000119 "ldr q17, [x24], #0x10\n"
Michael Tylerba209752022-12-15 12:39:29 +0000120 "shll v19.4s, v17.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000121 "ldr d16, [x24], #0x8\n"
122 "sub x19, x19, #0xc\n"
123 "shll2 v27.4s, v17.8h, #0x10\n"
124 "ldr q17, [x22], #0x10\n"
125 "cmp x19, #0xc\n"
126 "shll v26.4s, v16.4h, #0x10\n"
127 "ldr q16, [x21], #0x10\n"
128 "ldr q25, [x20], #0x10\n"
129 "shll v24.4s, v17.4h, #0x10\n"
130 "shll2 v23.4s, v17.8h, #0x10\n"
131 "ldr d18, [x22], #0x8\n"
132 "shll v22.4s, v16.4h, #0x10\n"
133 "ldr d17, [x21], #0x8\n"
134 "shll2 v21.4s, v16.8h, #0x10\n"
135 "ldr d16, [x20], #0x8\n"
136 "shll v20.4s, v25.4h, #0x10\n"
137 "str q19, [x23, #0x0]\n"
138 "shll v19.4s, v18.4h, #0x10\n"
139 "str q27, [x23, #0x10]\n"
140 "shll2 v18.4s, v25.8h, #0x10\n"
141 "str q26, [x23, #0x20]\n"
142 "shll v17.4s, v17.4h, #0x10\n"
143 "str q24, [x23, #0x30]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100144 "shll v16.4s, v16.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000145 "str q23, [x23, #0x40]\n"
146 "str q19, [x23, #0x50]\n"
147 "str q22, [x23, #0x60]\n"
148 "str q21, [x23, #0x70]\n"
149 "str q17, [x23, #0x80]\n"
150 "str q20, [x23, #0x90]\n"
151 "str q18, [x23, #0xa0]\n"
152 "str q16, [x23, #0xb0]\n"
153 "add x23, x23, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100154 "bge 4b\n"
155 "5:" // Main row loop: Column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000156 "cmp x19, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100157 "blt 7f\n"
158 "6:" // Main row loop: width 4 loop: loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000159 "ldr d16, [x24], #0x8\n"
160 "shll v19.4s, v16.4h, #0x10\n"
161 "ldr d16, [x22], #0x8\n"
162 "sub x19, x19, #0x4\n"
163 "shll v18.4s, v16.4h, #0x10\n"
164 "ldr d16, [x21], #0x8\n"
165 "cmp x19, #0x4\n"
166 "shll v17.4s, v16.4h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100167 "ldr d16, [x20], #0x8\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000168 "str q19, [x23, #0x0]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100169 "shll v16.4s, v16.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000170 "str q18, [x23, #0x30]\n"
171 "str q17, [x23, #0x60]\n"
172 "str q16, [x23, #0x90]\n"
173 "add x23, x23, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100174 "bge 6b\n"
175 "7:" // Main row loop: width 4 loop: skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000176 "cmp x19, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100177 "blt 9f\n"
178 "8:" // Main row loop: width 1 loop: loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000179 "ldr h16, [x24], #0x2\n"
180 "shll v19.4s, v16.4h, #0x10\n"
181 "ldr h16, [x22], #0x2\n"
182 "sub x19, x19, #0x1\n"
183 "shll v18.4s, v16.4h, #0x10\n"
184 "ldr h16, [x21], #0x2\n"
185 "cmp x19, #0x1\n"
186 "shll v17.4s, v16.4h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100187 "ldr h16, [x20], #0x2\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000188 "str s19, [x23, #0x0]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100189 "shll v16.4s, v16.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000190 "str s18, [x23, #0x30]\n"
191 "str s17, [x23, #0x60]\n"
192 "str s16, [x23, #0x90]\n"
193 "add x23, x23, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100194 "bge 8b\n"
195 "9:" // Main row loop: width 1 loop: skip
Michael Tylerba209752022-12-15 12:39:29 +0000196 "add %x[out], %x[out], #0xc0\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000197 "cmp %x[height], #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100198 "bge 1b\n"
199 "cbz %x[height], 20f\n"
200 "10:" // Main loop skip
201
202 "11:" // Tail row loop: Head
Michael Tylerbe13cea2023-01-17 11:04:14 +0000203 "mov x24, %x[in]\n"
204 "mov x23, %x[out]\n"
205 "add %x[in], x24, %x[in_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100206 "sub %x[height], %x[height], #0x1\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000207 "mov x19, %x[width]\n"
208 "cmp x19, #0x18\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100209 "blt 13f\n"
210 "12:" // Tail row loop: Unroll column loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000211 "ldr q16, [x24], #0x10\n"
212 "shll v20.4s, v16.4h, #0x10\n"
213 "ldr q18, [x24], #0x10\n"
214 "sub x19, x19, #0x18\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100215 "shll2 v17.4s, v16.8h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000216 "ldr q19, [x24], #0x10\n"
217 "shll v16.4s, v18.4h, #0x10\n"
218 "cmp x19, #0x18\n"
219 "shll2 v18.4s, v18.8h, #0x10\n"
220 "str q20, [x23, #0x0]\n"
221 "str q17, [x23, #0x10]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100222 "shll v17.4s, v19.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000223 "str q16, [x23, #0x20]\n"
224 "add x23, x23, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100225 "shll2 v16.4s, v19.8h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000226 "str q18, [x23, #0x0]\n"
227 "str q17, [x23, #0x10]\n"
228 "str q16, [x23, #0x20]\n"
229 "add x23, x23, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100230 "bge 12b\n"
231 "13:" // Tail row loop: Unroll column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000232 "cmp x19, #0xc\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100233 "blt 15f\n"
234 "14:" // Tail row loop: Column loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000235 "ldr q17, [x24], #0x10\n"
236 "shll v18.4s, v17.4h, #0x10\n"
237 "ldr d16, [x24], #0x8\n"
238 "sub x19, x19, #0xc\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100239 "shll2 v17.4s, v17.8h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000240 "str q18, [x23, #0x0]\n"
241 "cmp x19, #0xc\n"
242 "shll v16.4s, v16.4h, #0x10\n"
243 "str q17, [x23, #0x10]\n"
244 "str q16, [x23, #0x20]\n"
245 "add x23, x23, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100246 "bge 14b\n"
247 "15:" // Tail row loop: Column loop skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000248 "cmp x19, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100249 "blt 17f\n"
250 "16:" // Tail row loop: width 4 loop: loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000251 "ldr d16, [x24], #0x8\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100252 "shll v16.4s, v16.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000253 "str q16, [x23, #0x0]\n"
254 "sub x19, x19, #0x4\n"
255 "add x23, x23, #0x10\n"
256 "cmp x19, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100257 "bge 16b\n"
258 "17:" // Tail row loop: width 4 loop: skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000259 "cmp x19, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100260 "blt 19f\n"
261 "18:" // Tail row loop: width 1 loop: loop
Michael Tylerbe13cea2023-01-17 11:04:14 +0000262 "ldr h16, [x24], #0x2\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100263 "shll v16.4s, v16.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000264 "str s16, [x23, #0x0]\n"
265 "sub x19, x19, #0x1\n"
266 "add x23, x23, #0x4\n"
267 "cmp x19, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100268 "bge 18b\n"
269 "19:" // Tail row loop: width 1 loop: skip
Michael Tylerba209752022-12-15 12:39:29 +0000270 "add %x[out], %x[out], #0x30\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000271 "cmp %x[height], #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100272 "bge 11b\n"
273 "20:" // Done
274
275 : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
276 : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [width] "r" (width)
Michael Tylerbe13cea2023-01-17 11:04:14 +0000277 : "cc", "memory", "v0", "v1", "v2", "v3", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "x19", "x20", "x21", "x22", "x23", "x24"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100278 );
279}
280
281} // anonymous namespace
282template<>
283void Transform<12, 1, true, VLType::None>(
284 float *out, const bfloat16 *in, int stride, int x0, int xmax, int k0, int kmax)
285{
286 a64_transpose_interleave_24_bf16fp32(
287 out,
288 in + k0 * stride + x0,
289 (xmax-x0),
290 stride * sizeof(bfloat16),
291 (kmax-k0)
292 );
293}
294
295#endif