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_4VL_2x2_fp32bf16(bfloat16 * out,const float * in,size_t width,size_t in_stride,size_t height)32 void sme_transpose_interleave_4VL_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 = 4 * roundup<size_t>(height, 2) * sme::get_vector_length<uint16_t>();
41 
42     __asm__ __volatile__(
43       ".inst 0xd503477f  // SMSTART ZA\n"
44       "cmp %x[height], #0x4\n"
45       "ptrue p4.b\n"
46       "blt 4f\n"
47       "1:"  // Main row loop: Head
48       "mov x25, %x[in]\n"
49       "add x24, x25, %x[in_stride]\n"
50       "add x23, x24, %x[in_stride]\n"
51       "add x22, x23, %x[in_stride]\n"
52       "add %x[in], x22, %x[in_stride]\n"
53       "mov x21, %x[out]\n"
54       "sub %x[height], %x[height], #0x4\n"
55       "mov x20, %x[width]\n"
56       "2:"  // Main row loop: Column loop
57       "mov x19, x20\n"
58       "whilelt p3.s, XZR, x19\n"
59       "ld1w { z16.s }, p3/Z, [x25]\n"
60       ".inst 0x658ab218  // bfcvt z24.h, p4/M, z16.s\n"
61       "decw x19\n"
62       "whilelt p2.s, XZR, x19\n"
63       "ld1w { z16.s }, p2/Z, [x25, #1, MUL VL]\n"
64       ".inst 0x658ab217  // bfcvt z23.h, p4/M, z16.s\n"
65       "decw x19\n"
66       "whilelt p1.s, XZR, x19\n"
67       "ld1w { z16.s }, p1/Z, [x25, #2, MUL VL]\n"
68       ".inst 0x658ab216  // bfcvt z22.h, p4/M, z16.s\n"
69       "decw x19\n"
70       "whilelt p0.s, XZR, x19\n"
71       "ld1w { z16.s }, p0/Z, [x25, #3, MUL VL]\n"
72       ".inst 0x658ab215  // bfcvt z21.h, p4/M, z16.s\n"
73       "ld1w { z16.s }, p3/Z, [x23]\n"
74       ".inst 0x658ab214  // bfcvt z20.h, p4/M, z16.s\n"
75       "decw x20, ALL, MUL #4\n"
76       "cmp x20, #0x0\n"
77       "ld1w { z16.s }, p2/Z, [x23, #1, MUL VL]\n"
78       ".inst 0x658ab213  // bfcvt z19.h, p4/M, z16.s\n"
79       "addvl x25, x25, #4\n"
80       "ld1w { z16.s }, p1/Z, [x23, #2, MUL VL]\n"
81       ".inst 0x658ab212  // bfcvt z18.h, p4/M, z16.s\n"
82       "ld1w { z16.s }, p0/Z, [x23, #3, MUL VL]\n"
83       ".inst 0x658ab211  // bfcvt z17.h, p4/M, z16.s\n"
84       "addvl x23, x23, #4\n"
85       "ld1w { z16.s }, p3/Z, [x24]\n"
86       ".inst 0x648ab218  // bfcvtnt z24.h, p4/M, z16.s\n"
87       "ld1w { z16.s }, p2/Z, [x24, #1, MUL VL]\n"
88       ".inst 0x648ab217  // bfcvtnt z23.h, p4/M, z16.s\n"
89       "ld1w { z16.s }, p1/Z, [x24, #2, MUL VL]\n"
90       ".inst 0x648ab216  // bfcvtnt z22.h, p4/M, z16.s\n"
91       "ld1w { z16.s }, p0/Z, [x24, #3, MUL VL]\n"
92       "addvl x24, x24, #4\n"
93       ".inst 0x648ab215  // bfcvtnt z21.h, p4/M, z16.s\n"
94       "ld1w { z16.s }, p3/Z, [x22]\n"
95       ".inst 0x648ab214  // bfcvtnt z20.h, p4/M, z16.s\n"
96       "ld1w { z16.s }, p2/Z, [x22, #1, MUL VL]\n"
97       ".inst 0x648ab213  // bfcvtnt z19.h, p4/M, z16.s\n"
98       "ld1w { z16.s }, p1/Z, [x22, #2, MUL VL]\n"
99       ".inst 0x648ab212  // bfcvtnt z18.h, p4/M, z16.s\n"
100       "ld1w { z16.s }, p0/Z, [x22, #3, MUL VL]\n"
101       "addvl x22, x22, #4\n"
102       ".inst 0x648ab211  // bfcvtnt z17.h, p4/M, z16.s\n"
103       "st1h { z24.h }, p4, [x21]\n"
104       "st1h { z23.h }, p4, [x21, #1, MUL VL]\n"
105       "st1h { z22.h }, p4, [x21, #2, MUL VL]\n"
106       "st1h { z21.h }, p4, [x21, #3, MUL VL]\n"
107       "st1h { z20.h }, p4, [x21, #4, MUL VL]\n"
108       "st1h { z19.h }, p4, [x21, #5, MUL VL]\n"
109       "st1h { z18.h }, p4, [x21, #6, MUL VL]\n"
110       "st1h { z17.h }, p4, [x21, #7, MUL VL]\n"
111       "add x21, x21, %x[out_stride]\n"
112       "bgt 2b\n"
113       "3:"  // Main row loop: Column loop skip
114       "cmp %x[height], #0x4\n"
115       "addvl %x[out], %x[out], #8\n"
116       "bge 1b\n"
117       "cbz %x[height], 8f\n"
118       "4:"  // Main loop skip
119       "5:"  // Tail row loop: Head
120       "mov x25, %x[in]\n"
121       "add x24, x25, %x[in_stride]\n"
122       "cmp %x[height], #0x1\n"
123       "add %x[in], x24, %x[in_stride]\n"
124       "mov x21, %x[out]\n"
125       "csel x24, x24, %x[pad_row], GT\n"
126       "sub %x[height], %x[height], #0x2\n"
127       "mov x20, %x[width]\n"
128       "6:"  // Tail row loop: Column loop
129       "mov x19, x20\n"
130       "whilelt p3.s, XZR, x19\n"
131       "ld1w { z16.s }, p3/Z, [x25]\n"
132       ".inst 0x658ab214  // bfcvt z20.h, p4/M, z16.s\n"
133       "decw x19\n"
134       "whilelt p2.s, XZR, x19\n"
135       "ld1w { z16.s }, p2/Z, [x25, #1, MUL VL]\n"
136       ".inst 0x658ab213  // bfcvt z19.h, p4/M, z16.s\n"
137       "decw x19\n"
138       "whilelt p1.s, XZR, x19\n"
139       "ld1w { z16.s }, p1/Z, [x25, #2, MUL VL]\n"
140       ".inst 0x658ab212  // bfcvt z18.h, p4/M, z16.s\n"
141       "decw x19\n"
142       "whilelt p0.s, XZR, x19\n"
143       "ld1w { z16.s }, p0/Z, [x25, #3, MUL VL]\n"
144       ".inst 0x658ab211  // bfcvt z17.h, p4/M, z16.s\n"
145       "ld1w { z16.s }, p3/Z, [x24]\n"
146       "decw x20, ALL, MUL #4\n"
147       "cmp x20, #0x0\n"
148       ".inst 0x648ab214  // bfcvtnt z20.h, p4/M, z16.s\n"
149       "ld1w { z16.s }, p2/Z, [x24, #1, MUL VL]\n"
150       "addvl x25, x25, #4\n"
151       ".inst 0x648ab213  // bfcvtnt z19.h, p4/M, z16.s\n"
152       "ld1w { z16.s }, p1/Z, [x24, #2, MUL VL]\n"
153       ".inst 0x648ab212  // bfcvtnt z18.h, p4/M, z16.s\n"
154       "ld1w { z16.s }, p0/Z, [x24, #3, MUL VL]\n"
155       "addvl x24, x24, #4\n"
156       ".inst 0x648ab211  // bfcvtnt z17.h, p4/M, z16.s\n"
157       "st1h { z20.h }, p4, [x21]\n"
158       "st1h { z19.h }, p4, [x21, #1, MUL VL]\n"
159       "st1h { z18.h }, p4, [x21, #2, MUL VL]\n"
160       "st1h { z17.h }, p4, [x21, #3, MUL VL]\n"
161       "add x21, x21, %x[out_stride]\n"
162       "bgt 6b\n"
163       "7:"  // Tail row loop: Column loop skip
164       "cmp %x[height], #0x1\n"
165       "addvl %x[out], %x[out], #4\n"
166       "bge 5b\n"
167       "8:"  // Done
168       ".inst 0xd503467f  // SMSTOP\n"
169       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
170       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
171       : "cc", "memory", "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "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"
172     );
173 }
174 
175 } // anonymous namespace
176 template<>
Transform(bfloat16 * out,const float * in,int stride,int x0,int xmax,int k0,int kmax)177 void Transform<4, 2, true, VLType::SME>(
178     bfloat16 *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
179 {
180     sme_transpose_interleave_4VL_2x2_fp32bf16(
181         out,
182         in + k0 * stride + x0,
183         (xmax-x0),
184         stride * sizeof(float),
185         (kmax-k0)
186     );
187 }
188 
189 #endif
190