• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifdef __aarch64__
25 
26 #include <cstddef>
27 #include "../../bfloat.hpp"
28 
29 namespace arm_gemm {
30 
a64_ffinterleaved_bf16fp32_dot_8x12(const bfloat16 * Apanel,const bfloat16 * Bpanel,size_t B_stride,float * Cpanel,int ablocks,size_t N,int K)31 void a64_ffinterleaved_bf16fp32_dot_8x12(
32     const bfloat16 *Apanel,
33     const bfloat16 *Bpanel,
34     size_t B_stride,
35     float *Cpanel,
36     int ablocks,
37     size_t N,
38     int K) {
39 
40     struct KernelArgs {
41         size_t K = {};
42         const bfloat16 *Bpanel = {};
43         size_t N = {};
44         size_t B_stride = {};
45         const bfloat16 *cur_B_ptr = {};
46     } ka;
47 
48     ka.K = (K/2) - 1;
49     ka.Bpanel = Bpanel;
50     ka.N = N;
51     ka.B_stride = B_stride;
52 
53     __asm__ __volatile__(
54       "1:"  // Height loop
55       "ldr x24, [%x[args_ptr], %[offsetof_Bpanel]]\n"
56       "ldr x23, [%x[args_ptr], %[offsetof_N]]\n"
57       "str x24, [%x[args_ptr], %[offsetof_cur_B_ptr]]\n"
58       "mov x22, %x[Apanel]\n"
59       "2:"  // Width loop
60       "ldr x24, [%x[args_ptr], %[offsetof_cur_B_ptr]]\n"
61       "ldr x19, [%x[args_ptr], %[offsetof_B_stride]]\n"
62       "add x21, x24, x19, LSL #1\n"
63       "add x20, x21, x19, LSL #1\n"
64       "add x19, x20, x19, LSL #1\n"
65       "str x19, [%x[args_ptr], %[offsetof_cur_B_ptr]]\n"
66       "cmp x23, #0x8\n"
67       "mov %x[Apanel], x22\n"
68       "bgt 3f\n"
69       "cmp x23, #0x4\n"
70       "mov x20, x24\n"
71       "bgt 3f\n"
72       "mov x21, x24\n"
73       "3:"  // B setup done
74       "ldr q0, [%x[Apanel], #0x0]\n"
75       "ldr q1, [%x[Apanel], #0x10]\n"
76       "movi v8.16b, #0x0\n"
77       "ldr q4, [x24, #0x0]\n"
78       "ldr q5, [x21, #0x0]\n"
79       "movi v9.16b, #0x0\n"
80       "ldr q6, [x20, #0x0]\n"
81       "ldr x19, [%x[args_ptr], %[offsetof_K]]\n"
82       "cmp x19, #0x2\n"
83       "movi v10.16b, #0x0\n"
84       "movi v11.16b, #0x0\n"
85       "movi v12.16b, #0x0\n"
86       "movi v13.16b, #0x0\n"
87       "movi v14.16b, #0x0\n"
88       "movi v15.16b, #0x0\n"
89       "movi v16.16b, #0x0\n"
90       "movi v17.16b, #0x0\n"
91       "movi v18.16b, #0x0\n"
92       "movi v19.16b, #0x0\n"
93       "movi v20.16b, #0x0\n"
94       "movi v21.16b, #0x0\n"
95       "movi v22.16b, #0x0\n"
96       "movi v23.16b, #0x0\n"
97       "movi v24.16b, #0x0\n"
98       "movi v25.16b, #0x0\n"
99       "movi v26.16b, #0x0\n"
100       "movi v27.16b, #0x0\n"
101       "movi v28.16b, #0x0\n"
102       "movi v29.16b, #0x0\n"
103       "movi v30.16b, #0x0\n"
104       "movi v31.16b, #0x0\n"
105       "blt 5f\n"
106       "4:"  // main loop head
107       "ldr q2, [%x[Apanel], #0x20]\n"
108       "ldr q3, [%x[Apanel], #0x30]\n"
109       ".inst 0x4f40f088  // bfdot v8.4s, v4.8h, v0.h[0]\n"
110       ".inst 0x4f60f08b  // bfdot v11.4s, v4.8h, v0.h[1]\n"
111       ".inst 0x4f40f88e  // bfdot v14.4s, v4.8h, v0.h[2]\n"
112       "sub x19, x19, #0x2\n"
113       ".inst 0x4f60f891  // bfdot v17.4s, v4.8h, v0.h[3]\n"
114       ".inst 0x4f41f094  // bfdot v20.4s, v4.8h, v1.h[0]\n"
115       "cmp x19, #0x2\n"
116       ".inst 0x4f61f097  // bfdot v23.4s, v4.8h, v1.h[1]\n"
117       ".inst 0x4f41f89a  // bfdot v26.4s, v4.8h, v1.h[2]\n"
118       "add %x[Apanel], %x[Apanel], #0x40\n"
119       ".inst 0x4f61f89d  // bfdot v29.4s, v4.8h, v1.h[3]\n"
120       "ldr q4, [x24, #0x10]\n"
121       ".inst 0x4f40f0a9  // bfdot v9.4s, v5.8h, v0.h[0]\n"
122       ".inst 0x4f60f0ac  // bfdot v12.4s, v5.8h, v0.h[1]\n"
123       ".inst 0x4f40f8af  // bfdot v15.4s, v5.8h, v0.h[2]\n"
124       "add x24, x24, #0x20\n"
125       ".inst 0x4f60f8b2  // bfdot v18.4s, v5.8h, v0.h[3]\n"
126       ".inst 0x4f41f0b5  // bfdot v21.4s, v5.8h, v1.h[0]\n"
127       ".inst 0x4f61f0b8  // bfdot v24.4s, v5.8h, v1.h[1]\n"
128       ".inst 0x4f41f8bb  // bfdot v27.4s, v5.8h, v1.h[2]\n"
129       ".inst 0x4f61f8be  // bfdot v30.4s, v5.8h, v1.h[3]\n"
130       "ldr q5, [x21, #0x10]\n"
131       ".inst 0x4f40f0ca  // bfdot v10.4s, v6.8h, v0.h[0]\n"
132       ".inst 0x4f60f0cd  // bfdot v13.4s, v6.8h, v0.h[1]\n"
133       ".inst 0x4f40f8d0  // bfdot v16.4s, v6.8h, v0.h[2]\n"
134       "add x21, x21, #0x20\n"
135       ".inst 0x4f60f8d3  // bfdot v19.4s, v6.8h, v0.h[3]\n"
136       "ldr q0, [%x[Apanel], #0x0]\n"
137       ".inst 0x4f41f0d6  // bfdot v22.4s, v6.8h, v1.h[0]\n"
138       ".inst 0x4f61f0d9  // bfdot v25.4s, v6.8h, v1.h[1]\n"
139       ".inst 0x4f41f8dc  // bfdot v28.4s, v6.8h, v1.h[2]\n"
140       ".inst 0x4f61f8df  // bfdot v31.4s, v6.8h, v1.h[3]\n"
141       "ldr q6, [x20, #0x10]\n"
142       "ldr q1, [%x[Apanel], #0x10]\n"
143       "add x20, x20, #0x20\n"
144       ".inst 0x4f42f088  // bfdot v8.4s, v4.8h, v2.h[0]\n"
145       ".inst 0x4f62f08b  // bfdot v11.4s, v4.8h, v2.h[1]\n"
146       ".inst 0x4f42f88e  // bfdot v14.4s, v4.8h, v2.h[2]\n"
147       ".inst 0x4f62f891  // bfdot v17.4s, v4.8h, v2.h[3]\n"
148       ".inst 0x4f43f094  // bfdot v20.4s, v4.8h, v3.h[0]\n"
149       ".inst 0x4f63f097  // bfdot v23.4s, v4.8h, v3.h[1]\n"
150       ".inst 0x4f43f89a  // bfdot v26.4s, v4.8h, v3.h[2]\n"
151       ".inst 0x4f63f89d  // bfdot v29.4s, v4.8h, v3.h[3]\n"
152       "ldr q4, [x24, #0x0]\n"
153       ".inst 0x4f42f0a9  // bfdot v9.4s, v5.8h, v2.h[0]\n"
154       ".inst 0x4f62f0ac  // bfdot v12.4s, v5.8h, v2.h[1]\n"
155       ".inst 0x4f42f8af  // bfdot v15.4s, v5.8h, v2.h[2]\n"
156       ".inst 0x4f62f8b2  // bfdot v18.4s, v5.8h, v2.h[3]\n"
157       ".inst 0x4f43f0b5  // bfdot v21.4s, v5.8h, v3.h[0]\n"
158       ".inst 0x4f63f0b8  // bfdot v24.4s, v5.8h, v3.h[1]\n"
159       ".inst 0x4f43f8bb  // bfdot v27.4s, v5.8h, v3.h[2]\n"
160       ".inst 0x4f63f8be  // bfdot v30.4s, v5.8h, v3.h[3]\n"
161       "ldr q5, [x21, #0x0]\n"
162       ".inst 0x4f42f0ca  // bfdot v10.4s, v6.8h, v2.h[0]\n"
163       ".inst 0x4f62f0cd  // bfdot v13.4s, v6.8h, v2.h[1]\n"
164       ".inst 0x4f42f8d0  // bfdot v16.4s, v6.8h, v2.h[2]\n"
165       ".inst 0x4f62f8d3  // bfdot v19.4s, v6.8h, v2.h[3]\n"
166       ".inst 0x4f43f0d6  // bfdot v22.4s, v6.8h, v3.h[0]\n"
167       ".inst 0x4f63f0d9  // bfdot v25.4s, v6.8h, v3.h[1]\n"
168       ".inst 0x4f43f8dc  // bfdot v28.4s, v6.8h, v3.h[2]\n"
169       ".inst 0x4f63f8df  // bfdot v31.4s, v6.8h, v3.h[3]\n"
170       "ldr q6, [x20, #0x0]\n"
171       "bge 4b\n"
172       "5:"  // main loop skip
173       ".inst 0x4f40f088  // bfdot v8.4s, v4.8h, v0.h[0]\n"
174       ".inst 0x4f60f08b  // bfdot v11.4s, v4.8h, v0.h[1]\n"
175       "add %x[Apanel], %x[Apanel], #0x20\n"
176       ".inst 0x4f40f88e  // bfdot v14.4s, v4.8h, v0.h[2]\n"
177       ".inst 0x4f60f891  // bfdot v17.4s, v4.8h, v0.h[3]\n"
178       "add x24, x24, #0x10\n"
179       ".inst 0x4f41f094  // bfdot v20.4s, v4.8h, v1.h[0]\n"
180       ".inst 0x4f61f097  // bfdot v23.4s, v4.8h, v1.h[1]\n"
181       "add x21, x21, #0x10\n"
182       ".inst 0x4f41f89a  // bfdot v26.4s, v4.8h, v1.h[2]\n"
183       ".inst 0x4f61f89d  // bfdot v29.4s, v4.8h, v1.h[3]\n"
184       "add x20, x20, #0x10\n"
185       ".inst 0x4f40f0a9  // bfdot v9.4s, v5.8h, v0.h[0]\n"
186       ".inst 0x4f60f0ac  // bfdot v12.4s, v5.8h, v0.h[1]\n"
187       ".inst 0x4f40f8af  // bfdot v15.4s, v5.8h, v0.h[2]\n"
188       ".inst 0x4f60f8b2  // bfdot v18.4s, v5.8h, v0.h[3]\n"
189       ".inst 0x4f41f0b5  // bfdot v21.4s, v5.8h, v1.h[0]\n"
190       ".inst 0x4f61f0b8  // bfdot v24.4s, v5.8h, v1.h[1]\n"
191       ".inst 0x4f41f8bb  // bfdot v27.4s, v5.8h, v1.h[2]\n"
192       ".inst 0x4f61f8be  // bfdot v30.4s, v5.8h, v1.h[3]\n"
193       ".inst 0x4f40f0ca  // bfdot v10.4s, v6.8h, v0.h[0]\n"
194       ".inst 0x4f60f0cd  // bfdot v13.4s, v6.8h, v0.h[1]\n"
195       ".inst 0x4f40f8d0  // bfdot v16.4s, v6.8h, v0.h[2]\n"
196       ".inst 0x4f60f8d3  // bfdot v19.4s, v6.8h, v0.h[3]\n"
197       ".inst 0x4f41f0d6  // bfdot v22.4s, v6.8h, v1.h[0]\n"
198       ".inst 0x4f61f0d9  // bfdot v25.4s, v6.8h, v1.h[1]\n"
199       ".inst 0x4f41f8dc  // bfdot v28.4s, v6.8h, v1.h[2]\n"
200       ".inst 0x4f61f8df  // bfdot v31.4s, v6.8h, v1.h[3]\n"
201       "cbz x19, 6f\n"
202       "ldr q0, [%x[Apanel], #0x0]\n"
203       "ldr q1, [%x[Apanel], #0x10]\n"
204       "add %x[Apanel], %x[Apanel], #0x20\n"
205       "ldr q7, [x24, #0x0]\n"
206       "ldr q4, [x21, #0x0]\n"
207       ".inst 0x4f40f0e8  // bfdot v8.4s, v7.8h, v0.h[0]\n"
208       "ldr q5, [x20, #0x0]\n"
209       ".inst 0x4f60f0eb  // bfdot v11.4s, v7.8h, v0.h[1]\n"
210       ".inst 0x4f40f8ee  // bfdot v14.4s, v7.8h, v0.h[2]\n"
211       ".inst 0x4f60f8f1  // bfdot v17.4s, v7.8h, v0.h[3]\n"
212       ".inst 0x4f41f0f4  // bfdot v20.4s, v7.8h, v1.h[0]\n"
213       ".inst 0x4f61f0f7  // bfdot v23.4s, v7.8h, v1.h[1]\n"
214       ".inst 0x4f41f8fa  // bfdot v26.4s, v7.8h, v1.h[2]\n"
215       ".inst 0x4f61f8fd  // bfdot v29.4s, v7.8h, v1.h[3]\n"
216       ".inst 0x4f40f089  // bfdot v9.4s, v4.8h, v0.h[0]\n"
217       ".inst 0x4f60f08c  // bfdot v12.4s, v4.8h, v0.h[1]\n"
218       ".inst 0x4f40f88f  // bfdot v15.4s, v4.8h, v0.h[2]\n"
219       ".inst 0x4f60f892  // bfdot v18.4s, v4.8h, v0.h[3]\n"
220       ".inst 0x4f41f095  // bfdot v21.4s, v4.8h, v1.h[0]\n"
221       ".inst 0x4f61f098  // bfdot v24.4s, v4.8h, v1.h[1]\n"
222       ".inst 0x4f41f89b  // bfdot v27.4s, v4.8h, v1.h[2]\n"
223       ".inst 0x4f61f89e  // bfdot v30.4s, v4.8h, v1.h[3]\n"
224       ".inst 0x4f40f0aa  // bfdot v10.4s, v5.8h, v0.h[0]\n"
225       ".inst 0x4f60f0ad  // bfdot v13.4s, v5.8h, v0.h[1]\n"
226       ".inst 0x4f40f8b0  // bfdot v16.4s, v5.8h, v0.h[2]\n"
227       ".inst 0x4f60f8b3  // bfdot v19.4s, v5.8h, v0.h[3]\n"
228       ".inst 0x4f41f0b6  // bfdot v22.4s, v5.8h, v1.h[0]\n"
229       ".inst 0x4f61f0b9  // bfdot v25.4s, v5.8h, v1.h[1]\n"
230       ".inst 0x4f41f8bc  // bfdot v28.4s, v5.8h, v1.h[2]\n"
231       ".inst 0x4f61f8bf  // bfdot v31.4s, v5.8h, v1.h[3]\n"
232       "6:"  // multiply loop done
233       "subs x23, x23, #0xc\n"
234       "str q8, [%x[Cpanel], #0x0]\n"
235       "str q9, [%x[Cpanel], #0x10]\n"
236       "str q10, [%x[Cpanel], #0x20]\n"
237       "str q11, [%x[Cpanel], #0x30]\n"
238       "str q12, [%x[Cpanel], #0x40]\n"
239       "str q13, [%x[Cpanel], #0x50]\n"
240       "str q14, [%x[Cpanel], #0x60]\n"
241       "str q15, [%x[Cpanel], #0x70]\n"
242       "str q16, [%x[Cpanel], #0x80]\n"
243       "str q17, [%x[Cpanel], #0x90]\n"
244       "str q18, [%x[Cpanel], #0xa0]\n"
245       "str q19, [%x[Cpanel], #0xb0]\n"
246       "str q20, [%x[Cpanel], #0xc0]\n"
247       "str q21, [%x[Cpanel], #0xd0]\n"
248       "str q22, [%x[Cpanel], #0xe0]\n"
249       "str q23, [%x[Cpanel], #0xf0]\n"
250       "str q24, [%x[Cpanel], #0x100]\n"
251       "str q25, [%x[Cpanel], #0x110]\n"
252       "str q26, [%x[Cpanel], #0x120]\n"
253       "str q27, [%x[Cpanel], #0x130]\n"
254       "str q28, [%x[Cpanel], #0x140]\n"
255       "str q29, [%x[Cpanel], #0x150]\n"
256       "str q30, [%x[Cpanel], #0x160]\n"
257       "str q31, [%x[Cpanel], #0x170]\n"
258       "add %x[Cpanel], %x[Cpanel], #0x180\n"
259       "bgt 2b\n"
260       "subs %x[ablocks], %x[ablocks], #0x1\n"
261       "bne 1b\n"
262       : [Apanel] "+&r" (Apanel), [Cpanel] "+&r" (Cpanel), [ablocks] "+&r" (ablocks)
263       : [args_ptr] "r" (&ka), [offsetof_B_stride] "I" (offsetof(KernelArgs, B_stride)), [offsetof_Bpanel] "I" (offsetof(KernelArgs, Bpanel)), [offsetof_K] "I" (offsetof(KernelArgs, K)), [offsetof_N] "I" (offsetof(KernelArgs, N)), [offsetof_cur_B_ptr] "I" (offsetof(KernelArgs, cur_B_ptr))
264       : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "x19", "x20", "x21", "x22", "x23", "x24"
265     );
266 }
267 
268 } // namespace arm_gemm
269 #endif // __aarch64__
270