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 
25 #pragma once
26 
27 #ifdef __ARM_FEATURE_SVE
28 
29 
30 namespace {
31 
sme_transpose_interleave_16VL_2x2_fp32bf16(bfloat16 * out,const float * in,size_t width,size_t in_stride,size_t height)32 void sme_transpose_interleave_16VL_2x2_fp32bf16(bfloat16 *out, const float *in, size_t width, size_t in_stride, size_t height)
33 {
34     float *pad_row = reinterpret_cast<float *>(alloca(width * sizeof(float)));
35 
36     if (height % 2) {
37         memset(pad_row, 0, width * sizeof(float));
38     }
39 
40     size_t out_stride = 16 * roundup<size_t>(height, 2) * sme::get_vector_length<uint16_t>();
41 
42     __asm__ __volatile__(
43       ".inst 0xd503477f  // SMSTART ZA\n"
44       "ptrue p7.b\n"
45       "1:"  // Main row loop: Head
46       "mov x24, %x[in]\n"
47       "add x23, x24, %x[in_stride]\n"
48       "cmp %x[height], #0x1\n"
49       "add %x[in], x23, %x[in_stride]\n"
50       "mov x22, %x[out]\n"
51       "csel x23, x23, %x[pad_row], GT\n"
52       "sub %x[height], %x[height], #0x2\n"
53       "mov x21, %x[width]\n"
54       "2:"  // Main row loop: Column loop
55       "mov x20, x21\n"
56       "whilelt p1.s, XZR, x20\n"
57       "ld1w { z16.s }, p1/Z, [x24]\n"
58       ".inst 0x658abe00  // bfcvt z0.h, p7/M, z16.s\n"
59       "decw x20\n"
60       "whilelt p0.s, XZR, x20\n"
61       "ld1w { z16.s }, p0/Z, [x24, #1, MUL VL]\n"
62       ".inst 0x658abe1f  // bfcvt z31.h, p7/M, z16.s\n"
63       "decw x20\n"
64       "whilelt p6.s, XZR, x20\n"
65       "ld1w { z16.s }, p6/Z, [x24, #2, MUL VL]\n"
66       ".inst 0x658abe1e  // bfcvt z30.h, p7/M, z16.s\n"
67       "decw x20\n"
68       "whilelt p5.s, XZR, x20\n"
69       "ld1w { z16.s }, p5/Z, [x24, #3, MUL VL]\n"
70       ".inst 0x658abe1d  // bfcvt z29.h, p7/M, z16.s\n"
71       "decw x20\n"
72       "whilelt p4.s, XZR, x20\n"
73       "ld1w { z16.s }, p4/Z, [x24, #4, MUL VL]\n"
74       ".inst 0x658abe1c  // bfcvt z28.h, p7/M, z16.s\n"
75       "decw x20\n"
76       "whilelt p3.s, XZR, x20\n"
77       "ld1w { z16.s }, p3/Z, [x24, #5, MUL VL]\n"
78       ".inst 0x658abe1b  // bfcvt z27.h, p7/M, z16.s\n"
79       "decw x20\n"
80       "whilelt p2.s, XZR, x20\n"
81       "ld1w { z16.s }, p2/Z, [x24, #6, MUL VL]\n"
82       ".inst 0x658abe1a  // bfcvt z26.h, p7/M, z16.s\n"
83       "decw x20\n"
84       "ld1w { z16.s }, p1/Z, [x23]\n"
85       "whilelt p1.s, XZR, x20\n"
86       ".inst 0x648abe00  // bfcvtnt z0.h, p7/M, z16.s\n"
87       "decw x20\n"
88       "ld1w { z16.s }, p1/Z, [x24, #7, MUL VL]\n"
89       "addvl x24, x24, #16\n"
90       ".inst 0x658abe19  // bfcvt z25.h, p7/M, z16.s\n"
91       "ld1w { z16.s }, p0/Z, [x23, #1, MUL VL]\n"
92       "whilelt p0.s, XZR, x20\n"
93       "decw x20\n"
94       ".inst 0x648abe1f  // bfcvtnt z31.h, p7/M, z16.s\n"
95       "ld1w { z16.s }, p0/Z, [x24, #-8, MUL VL]\n"
96       ".inst 0x658abe18  // bfcvt z24.h, p7/M, z16.s\n"
97       "mov x19, x22\n"
98       "decw x21, ALL, MUL #16\n"
99       "ld1w { z16.s }, p6/Z, [x23, #2, MUL VL]\n"
100       "whilelt p6.s, XZR, x20\n"
101       "decw x20\n"
102       ".inst 0x648abe1e  // bfcvtnt z30.h, p7/M, z16.s\n"
103       "ld1w { z16.s }, p6/Z, [x24, #-7, MUL VL]\n"
104       ".inst 0x658abe17  // bfcvt z23.h, p7/M, z16.s\n"
105       "add x22, x22, %x[out_stride]\n"
106       "ld1w { z16.s }, p5/Z, [x23, #3, MUL VL]\n"
107       "whilelt p5.s, XZR, x20\n"
108       "decw x20\n"
109       ".inst 0x648abe1d  // bfcvtnt z29.h, p7/M, z16.s\n"
110       "ld1w { z16.s }, p5/Z, [x24, #-6, MUL VL]\n"
111       ".inst 0x658abe16  // bfcvt z22.h, p7/M, z16.s\n"
112       "ld1w { z16.s }, p4/Z, [x23, #4, MUL VL]\n"
113       "whilelt p4.s, XZR, x20\n"
114       "decw x20\n"
115       ".inst 0x648abe1c  // bfcvtnt z28.h, p7/M, z16.s\n"
116       "ld1w { z16.s }, p4/Z, [x24, #-5, MUL VL]\n"
117       ".inst 0x658abe15  // bfcvt z21.h, p7/M, z16.s\n"
118       "ld1w { z16.s }, p3/Z, [x23, #5, MUL VL]\n"
119       "whilelt p3.s, XZR, x20\n"
120       "decw x20\n"
121       ".inst 0x648abe1b  // bfcvtnt z27.h, p7/M, z16.s\n"
122       "ld1w { z16.s }, p3/Z, [x24, #-4, MUL VL]\n"
123       ".inst 0x658abe14  // bfcvt z20.h, p7/M, z16.s\n"
124       "ld1w { z16.s }, p2/Z, [x23, #6, MUL VL]\n"
125       "whilelt p2.s, XZR, x20\n"
126       "decw x20\n"
127       ".inst 0x648abe1a  // bfcvtnt z26.h, p7/M, z16.s\n"
128       "ld1w { z16.s }, p2/Z, [x24, #-3, MUL VL]\n"
129       ".inst 0x658abe13  // bfcvt z19.h, p7/M, z16.s\n"
130       "ld1w { z16.s }, p1/Z, [x23, #7, MUL VL]\n"
131       "whilelt p1.s, XZR, x20\n"
132       "decw x20\n"
133       ".inst 0x648abe19  // bfcvtnt z25.h, p7/M, z16.s\n"
134       "ld1w { z16.s }, p1/Z, [x24, #-2, MUL VL]\n"
135       "addvl x23, x23, #16\n"
136       ".inst 0x658abe12  // bfcvt z18.h, p7/M, z16.s\n"
137       "ld1w { z16.s }, p0/Z, [x23, #-8, MUL VL]\n"
138       "whilelt p0.s, XZR, x20\n"
139       "cmp x21, #0x0\n"
140       ".inst 0x648abe18  // bfcvtnt z24.h, p7/M, z16.s\n"
141       "ld1w { z16.s }, p0/Z, [x24, #-1, MUL VL]\n"
142       ".inst 0x658abe11  // bfcvt z17.h, p7/M, z16.s\n"
143       "ld1w { z16.s }, p6/Z, [x23, #-7, MUL VL]\n"
144       ".inst 0x648abe17  // bfcvtnt z23.h, p7/M, z16.s\n"
145       "ld1w { z16.s }, p5/Z, [x23, #-6, MUL VL]\n"
146       ".inst 0x648abe16  // bfcvtnt z22.h, p7/M, z16.s\n"
147       "ld1w { z16.s }, p4/Z, [x23, #-5, MUL VL]\n"
148       ".inst 0x648abe15  // bfcvtnt z21.h, p7/M, z16.s\n"
149       "ld1w { z16.s }, p3/Z, [x23, #-4, MUL VL]\n"
150       ".inst 0x648abe14  // bfcvtnt z20.h, p7/M, z16.s\n"
151       "ld1w { z16.s }, p2/Z, [x23, #-3, MUL VL]\n"
152       ".inst 0x648abe13  // bfcvtnt z19.h, p7/M, z16.s\n"
153       "ld1w { z16.s }, p1/Z, [x23, #-2, MUL VL]\n"
154       ".inst 0x648abe12  // bfcvtnt z18.h, p7/M, z16.s\n"
155       "ld1w { z16.s }, p0/Z, [x23, #-1, MUL VL]\n"
156       "st1h { z0.h }, p7, [x19]\n"
157       ".inst 0x648abe11  // bfcvtnt z17.h, p7/M, z16.s\n"
158       "st1h { z31.h }, p7, [x19, #1, MUL VL]\n"
159       "st1h { z30.h }, p7, [x19, #2, MUL VL]\n"
160       "st1h { z29.h }, p7, [x19, #3, MUL VL]\n"
161       "st1h { z28.h }, p7, [x19, #4, MUL VL]\n"
162       "st1h { z27.h }, p7, [x19, #5, MUL VL]\n"
163       "st1h { z26.h }, p7, [x19, #6, MUL VL]\n"
164       "st1h { z25.h }, p7, [x19, #7, MUL VL]\n"
165       "addvl x19, x19, #16\n"
166       "st1h { z24.h }, p7, [x19, #-8, MUL VL]\n"
167       "st1h { z23.h }, p7, [x19, #-7, MUL VL]\n"
168       "st1h { z22.h }, p7, [x19, #-6, MUL VL]\n"
169       "st1h { z21.h }, p7, [x19, #-5, MUL VL]\n"
170       "st1h { z20.h }, p7, [x19, #-4, MUL VL]\n"
171       "st1h { z19.h }, p7, [x19, #-3, MUL VL]\n"
172       "st1h { z18.h }, p7, [x19, #-2, MUL VL]\n"
173       "st1h { z17.h }, p7, [x19, #-1, MUL VL]\n"
174       "bgt 2b\n"
175       "3:"  // Main row loop: Column loop skip
176       "cmp %x[height], #0x1\n"
177       "addvl %x[out], %x[out], #16\n"
178       "bge 1b\n"
179       ".inst 0xd503467f  // SMSTOP\n"
180       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
181       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
182       : "cc", "memory", "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15", "x19", "x20", "x21", "x22", "x23", "x24", "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7", "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31"
183     );
184 }
185 
186 } // anonymous namespace
187 template<>
Transform(bfloat16 * out,const float * in,int stride,int x0,int xmax,int k0,int kmax)188 void Transform<16, 2, true, VLType::SME>(
189     bfloat16 *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
190 {
191     sme_transpose_interleave_16VL_2x2_fp32bf16(
192         out,
193         in + k0 * stride + x0,
194         (xmax-x0),
195         stride * sizeof(float),
196         (kmax-k0)
197     );
198 }
199 
200 #endif
201