blob: 799a9cd91d156135240f73a19b8d3cf9222461e4 [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 __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 Tyler7d9a6262023-02-01 16:37:07 +000039 "mov x25, %x[in]\n"
40 "mov x24, %x[width]\n"
41 "add x23, x25, %x[in_stride]\n"
42 "add x22, x23, %x[in_stride]\n"
43 "add x20, x22, %x[in_stride]\n"
44 "cmp x24, #0x18\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010045 "add %x[in], x20, %x[in_stride]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000046 "mov x21, %x[out]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010047 "sub %x[height], %x[height], #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010048 "blt 3f\n"
49 "2:" // Main row loop: Unroll column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +000050 "ldr q18, [x25], #0x10\n"
51 "ldr q17, [x23], #0x10\n"
52 "sub x24, x24, #0x18\n"
53 "shll v26.4s, v18.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000054 "ldr q16, [x22], #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000055 "ldr q25, [x20], #0x10\n"
56 "shll2 v24.4s, v18.8h, #0x10\n"
57 "shll v5.4s, v17.4h, #0x10\n"
58 "ldr q23, [x25], #0x10\n"
59 "ldr q22, [x23], #0x10\n"
60 "shll v21.4s, v23.4h, #0x10\n"
61 "shll2 v4.4s, v17.8h, #0x10\n"
62 "ldr q20, [x22], #0x10\n"
63 "ldr q3, [x20], #0x10\n"
64 "shll v2.4s, v22.4h, #0x10\n"
65 "shll v1.4s, v16.4h, #0x10\n"
66 "ldr q19, [x25], #0x10\n"
67 "ldr q18, [x23], #0x10\n"
68 "shll2 v0.4s, v16.8h, #0x10\n"
69 "shll v31.4s, v20.4h, #0x10\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000070 "ldr q17, [x22], #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000071 "ldr q16, [x20], #0x10\n"
72 "shll v30.4s, v25.4h, #0x10\n"
73 "shll2 v29.4s, v25.8h, #0x10\n"
74 "shll v28.4s, v3.4h, #0x10\n"
75 "str q26, [x21, #0x0]\n"
76 "cmp x24, #0x18\n"
77 "shll2 v27.4s, v23.8h, #0x10\n"
78 "str q24, [x21, #0x10]\n"
79 "shll v26.4s, v19.4h, #0x10\n"
80 "shll2 v25.4s, v19.8h, #0x10\n"
81 "str q21, [x21, #0x20]\n"
82 "shll2 v24.4s, v22.8h, #0x10\n"
83 "shll v23.4s, v18.4h, #0x10\n"
84 "str q5, [x21, #0x30]\n"
85 "shll2 v22.4s, v18.8h, #0x10\n"
86 "shll2 v21.4s, v20.8h, #0x10\n"
87 "str q4, [x21, #0x40]\n"
88 "shll v20.4s, v17.4h, #0x10\n"
89 "shll2 v19.4s, v17.8h, #0x10\n"
90 "str q2, [x21, #0x50]\n"
91 "shll2 v18.4s, v3.8h, #0x10\n"
92 "shll v17.4s, v16.4h, #0x10\n"
93 "str q1, [x21, #0x60]\n"
94 "shll2 v16.4s, v16.8h, #0x10\n"
95 "str q0, [x21, #0x70]\n"
96 "str q31, [x21, #0x80]\n"
97 "str q30, [x21, #0x90]\n"
98 "str q29, [x21, #0xa0]\n"
99 "str q28, [x21, #0xb0]\n"
100 "add x21, x21, %x[out_stride]\n"
101 "str q27, [x21, #0x0]\n"
102 "str q26, [x21, #0x10]\n"
103 "str q25, [x21, #0x20]\n"
104 "str q24, [x21, #0x30]\n"
105 "str q23, [x21, #0x40]\n"
106 "str q22, [x21, #0x50]\n"
107 "str q21, [x21, #0x60]\n"
108 "str q20, [x21, #0x70]\n"
109 "str q19, [x21, #0x80]\n"
110 "str q18, [x21, #0x90]\n"
111 "str q17, [x21, #0xa0]\n"
112 "str q16, [x21, #0xb0]\n"
113 "add x21, x21, %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 Tyler7d9a6262023-02-01 16:37:07 +0000116 "cmp x24, #0xc\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100117 "blt 5f\n"
118 "4:" // Main row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000119 "ldr q16, [x25], #0x10\n"
120 "ldr q21, [x23], #0x10\n"
121 "sub x24, x24, #0xc\n"
122 "cmp x24, #0xc\n"
123 "ldr q20, [x22], #0x10\n"
124 "ldr q27, [x20], #0x10\n"
125 "shll v19.4s, v16.4h, #0x10\n"
126 "shll2 v26.4s, v16.8h, #0x10\n"
127 "ldr d16, [x25], #0x8\n"
128 "ldr d18, [x23], #0x8\n"
129 "shll v25.4s, v16.4h, #0x10\n"
130 "shll v24.4s, v21.4h, #0x10\n"
131 "ldr d17, [x22], #0x8\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000132 "ldr d16, [x20], #0x8\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000133 "shll2 v23.4s, v21.8h, #0x10\n"
134 "shll v22.4s, v18.4h, #0x10\n"
135 "shll v21.4s, v20.4h, #0x10\n"
136 "shll2 v20.4s, v20.8h, #0x10\n"
137 "str q19, [x21, #0x0]\n"
138 "shll v19.4s, v17.4h, #0x10\n"
139 "shll v18.4s, v27.4h, #0x10\n"
140 "str q26, [x21, #0x10]\n"
141 "shll2 v17.4s, v27.8h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100142 "shll v16.4s, v16.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000143 "str q25, [x21, #0x20]\n"
144 "str q24, [x21, #0x30]\n"
145 "str q23, [x21, #0x40]\n"
146 "str q22, [x21, #0x50]\n"
147 "str q21, [x21, #0x60]\n"
148 "str q20, [x21, #0x70]\n"
149 "str q19, [x21, #0x80]\n"
150 "str q18, [x21, #0x90]\n"
151 "str q17, [x21, #0xa0]\n"
152 "str q16, [x21, #0xb0]\n"
153 "add x21, x21, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100154 "bge 4b\n"
155 "5:" // Main row loop: Column loop skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000156 "cmp x24, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100157 "blt 7f\n"
158 "6:" // Main row loop: width 4 loop: loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000159 "ldr d19, [x25], #0x8\n"
160 "ldr d18, [x23], #0x8\n"
161 "sub x24, x24, #0x4\n"
162 "cmp x24, #0x4\n"
163 "ldr d17, [x22], #0x8\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100164 "ldr d16, [x20], #0x8\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000165 "shll v19.4s, v19.4h, #0x10\n"
166 "shll v18.4s, v18.4h, #0x10\n"
167 "shll v17.4s, v17.4h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100168 "shll v16.4s, v16.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000169 "str q19, [x21, #0x0]\n"
170 "str q18, [x21, #0x30]\n"
171 "str q17, [x21, #0x60]\n"
172 "str q16, [x21, #0x90]\n"
173 "add x21, x21, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100174 "bge 6b\n"
175 "7:" // Main row loop: width 4 loop: skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000176 "cmp x24, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100177 "blt 9f\n"
178 "8:" // Main row loop: width 1 loop: loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000179 "ldr h19, [x25], #0x2\n"
180 "ldr h18, [x23], #0x2\n"
181 "sub x24, x24, #0x1\n"
182 "cmp x24, #0x1\n"
183 "ldr h17, [x22], #0x2\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100184 "ldr h16, [x20], #0x2\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000185 "shll v19.4s, v19.4h, #0x10\n"
186 "shll v18.4s, v18.4h, #0x10\n"
187 "shll v17.4s, v17.4h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100188 "shll v16.4s, v16.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000189 "str s19, [x21, #0x0]\n"
190 "str s18, [x21, #0x30]\n"
191 "str s17, [x21, #0x60]\n"
192 "str s16, [x21, #0x90]\n"
193 "add x21, x21, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100194 "bge 8b\n"
195 "9:" // Main row loop: width 1 loop: skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000196 "cmp %x[height], #0x4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000197 "add %x[out], %x[out], #0xc0\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 Tyler7d9a6262023-02-01 16:37:07 +0000203 "mov x20, %x[width]\n"
204 "mov x25, %x[in]\n"
205 "cmp x20, #0x18\n"
206 "add %x[in], x25, %x[in_stride]\n"
207 "mov x21, %x[out]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100208 "sub %x[height], %x[height], #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100209 "blt 13f\n"
210 "12:" // Tail row loop: Unroll column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000211 "ldr q16, [x25], #0x10\n"
212 "ldr q20, [x25], #0x10\n"
213 "sub x20, x20, #0x18\n"
214 "shll v18.4s, v16.4h, #0x10\n"
215 "ldr q19, [x25], #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100216 "shll2 v17.4s, v16.8h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000217 "shll v16.4s, v20.4h, #0x10\n"
218 "str q18, [x21, #0x0]\n"
219 "str q17, [x21, #0x10]\n"
220 "cmp x20, #0x18\n"
221 "shll2 v18.4s, v20.8h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100222 "shll v17.4s, v19.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000223 "str q16, [x21, #0x20]\n"
224 "add x21, x21, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100225 "shll2 v16.4s, v19.8h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000226 "str q18, [x21, #0x0]\n"
227 "str q17, [x21, #0x10]\n"
228 "str q16, [x21, #0x20]\n"
229 "add x21, x21, %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 Tyler7d9a6262023-02-01 16:37:07 +0000232 "cmp x20, #0xc\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100233 "blt 15f\n"
234 "14:" // Tail row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000235 "ldr q17, [x25], #0x10\n"
236 "ldr d18, [x25], #0x8\n"
237 "sub x20, x20, #0xc\n"
238 "cmp x20, #0xc\n"
239 "shll v16.4s, v17.4h, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100240 "shll2 v17.4s, v17.8h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000241 "str q16, [x21, #0x0]\n"
242 "shll v16.4s, v18.4h, #0x10\n"
243 "str q17, [x21, #0x10]\n"
244 "str q16, [x21, #0x20]\n"
245 "add x21, x21, %x[out_stride]\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100246 "bge 14b\n"
247 "15:" // Tail row loop: Column loop skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000248 "cmp x20, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100249 "blt 17f\n"
250 "16:" // Tail row loop: width 4 loop: loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000251 "ldr d16, [x25], #0x8\n"
252 "sub x20, x20, #0x4\n"
253 "cmp x20, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100254 "shll v16.4s, v16.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000255 "str q16, [x21, #0x0]\n"
256 "add x21, x21, #0x10\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100257 "bge 16b\n"
258 "17:" // Tail row loop: width 4 loop: skip
Michael Tyler7d9a6262023-02-01 16:37:07 +0000259 "cmp x20, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100260 "blt 19f\n"
261 "18:" // Tail row loop: width 1 loop: loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000262 "ldr h16, [x25], #0x2\n"
263 "sub x20, x20, #0x1\n"
264 "cmp x20, #0x1\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100265 "shll v16.4s, v16.4h, #0x10\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000266 "str s16, [x21, #0x0]\n"
267 "add x21, x21, #0x4\n"
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100268 "bge 18b\n"
269 "19:" // Tail row loop: width 1 loop: skip
Michael Tylerbe13cea2023-01-17 11:04:14 +0000270 "cmp %x[height], #0x1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000271 "add %x[out], %x[out], #0x30\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 Tyler7d9a6262023-02-01 16:37:07 +0000277 : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "x20", "x21", "x22", "x23", "x24", "x25"
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