blob: d8fa686f215fb3f211d3972157ba74c585659d04 [file] [log] [blame]
Viet-Hoa Do03b29712022-06-01 11:47:14 +01001/*
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
30namespace {
31
32void sme_transpose_interleave_1VL_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 = 1 * 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 p1.b\n"
46 "blt 6f\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 "mov x22, %x[width]\n"
52 "cnth x20, ALL, MUL #2\n"
53 "add x19, x23, %x[in_stride]\n"
54 "cmp x22, x20\n"
55 "add %x[in], x19, %x[in_stride]\n"
56 "mov x21, %x[out]\n"
57 "sub %x[height], %x[height], #0x4\n"
58 "blt 3f\n"
59 "2:" // Main row loop: Unroll column loop
60 "ld1w { z16.s }, p1/Z, [x25]\n"
61 ".inst 0x658aa618 // bfcvt z24.h, p1/M, z16.s\n"
62 "sub x22, x22, x20\n"
63 "cmp x22, x20\n"
64 "ld1w { z16.s }, p1/Z, [x23]\n"
65 ".inst 0x658aa617 // bfcvt z23.h, p1/M, z16.s\n"
66 "ld1w { z16.s }, p1/Z, [x25, #1, MUL VL]\n"
67 ".inst 0x658aa616 // bfcvt z22.h, p1/M, z16.s\n"
68 "ld1w { z16.s }, p1/Z, [x23, #1, MUL VL]\n"
69 ".inst 0x658aa615 // bfcvt z21.h, p1/M, z16.s\n"
70 "ld1w { z16.s }, p1/Z, [x25, #2, MUL VL]\n"
71 ".inst 0x658aa614 // bfcvt z20.h, p1/M, z16.s\n"
72 "ld1w { z16.s }, p1/Z, [x23, #2, MUL VL]\n"
73 ".inst 0x658aa613 // bfcvt z19.h, p1/M, z16.s\n"
74 "ld1w { z16.s }, p1/Z, [x25, #3, MUL VL]\n"
75 ".inst 0x658aa612 // bfcvt z18.h, p1/M, z16.s\n"
76 "addvl x25, x25, #4\n"
77 "ld1w { z16.s }, p1/Z, [x23, #3, MUL VL]\n"
78 ".inst 0x658aa611 // bfcvt z17.h, p1/M, z16.s\n"
79 "addvl x23, x23, #4\n"
80 "ld1w { z16.s }, p1/Z, [x24]\n"
81 ".inst 0x648aa618 // bfcvtnt z24.h, p1/M, z16.s\n"
82 "ld1w { z16.s }, p1/Z, [x19]\n"
83 ".inst 0x648aa617 // bfcvtnt z23.h, p1/M, z16.s\n"
84 "ld1w { z16.s }, p1/Z, [x24, #1, MUL VL]\n"
85 ".inst 0x648aa616 // bfcvtnt z22.h, p1/M, z16.s\n"
86 "ld1w { z16.s }, p1/Z, [x19, #1, MUL VL]\n"
87 ".inst 0x648aa615 // bfcvtnt z21.h, p1/M, z16.s\n"
88 "ld1w { z16.s }, p1/Z, [x24, #2, MUL VL]\n"
89 ".inst 0x648aa614 // bfcvtnt z20.h, p1/M, z16.s\n"
90 "ld1w { z16.s }, p1/Z, [x19, #2, MUL VL]\n"
91 ".inst 0x648aa613 // bfcvtnt z19.h, p1/M, z16.s\n"
92 "ld1w { z16.s }, p1/Z, [x24, #3, MUL VL]\n"
93 "addvl x24, x24, #4\n"
94 ".inst 0x648aa612 // bfcvtnt z18.h, p1/M, z16.s\n"
95 "ld1w { z16.s }, p1/Z, [x19, #3, MUL VL]\n"
96 "st1h { z24.h }, p1, [x21]\n"
97 "addvl x19, x19, #4\n"
98 ".inst 0x648aa611 // bfcvtnt z17.h, p1/M, z16.s\n"
99 "st1h { z23.h }, p1, [x21, #1, MUL VL]\n"
100 "add x21, x21, %x[out_stride]\n"
101 "st1h { z22.h }, p1, [x21]\n"
102 "st1h { z21.h }, p1, [x21, #1, MUL VL]\n"
103 "add x21, x21, %x[out_stride]\n"
104 "st1h { z20.h }, p1, [x21]\n"
105 "st1h { z19.h }, p1, [x21, #1, MUL VL]\n"
106 "add x21, x21, %x[out_stride]\n"
107 "st1h { z18.h }, p1, [x21]\n"
108 "st1h { z17.h }, p1, [x21, #1, MUL VL]\n"
109 "add x21, x21, %x[out_stride]\n"
110 "bge 2b\n"
111 "3:" // Main row loop: Unroll column loop skip
112 "cbz x22, 5f\n"
113 "4:" // Main row loop: Column loop
114 "whilelt p0.s, XZR, x22\n"
115 "ld1w { z16.s }, p0/Z, [x25]\n"
116 ".inst 0x658aa612 // bfcvt z18.h, p1/M, z16.s\n"
117 "decw x22\n"
118 "ld1w { z16.s }, p0/Z, [x23]\n"
119 ".inst 0x658aa611 // bfcvt z17.h, p1/M, z16.s\n"
120 "cmp x22, #0x0\n"
121 "addvl x25, x25, #1\n"
122 "ld1w { z16.s }, p0/Z, [x24]\n"
123 "addvl x24, x24, #1\n"
124 "addvl x23, x23, #1\n"
125 ".inst 0x648aa612 // bfcvtnt z18.h, p1/M, z16.s\n"
126 "ld1w { z16.s }, p0/Z, [x19]\n"
127 "addvl x19, x19, #1\n"
128 ".inst 0x648aa611 // bfcvtnt z17.h, p1/M, z16.s\n"
129 "st1h { z18.h }, p1, [x21]\n"
130 "st1h { z17.h }, p1, [x21, #1, MUL VL]\n"
131 "add x21, x21, %x[out_stride]\n"
132 "bgt 4b\n"
133 "5:" // Main row loop: Column loop skip
134 "cmp %x[height], #0x4\n"
135 "addvl %x[out], %x[out], #2\n"
136 "bge 1b\n"
137 "cbz %x[height], 12f\n"
138 "6:" // Main loop skip
139 "7:" // Tail row loop: Head
140 "mov x25, %x[in]\n"
141 "add x24, x25, %x[in_stride]\n"
142 "cmp %x[height], #0x1\n"
143 "mov x20, %x[width]\n"
144 "cnth x19, ALL, MUL #2\n"
145 "add %x[in], x24, %x[in_stride]\n"
146 "csel x24, x24, %x[pad_row], GT\n"
147 "cmp x20, x19\n"
148 "mov x21, %x[out]\n"
149 "sub %x[height], %x[height], #0x2\n"
150 "blt 9f\n"
151 "8:" // Tail row loop: Unroll column loop
152 "ld1w { z16.s }, p1/Z, [x25]\n"
153 ".inst 0x658aa614 // bfcvt z20.h, p1/M, z16.s\n"
154 "sub x20, x20, x19\n"
155 "cmp x20, x19\n"
156 "ld1w { z16.s }, p1/Z, [x25, #1, MUL VL]\n"
157 ".inst 0x658aa613 // bfcvt z19.h, p1/M, z16.s\n"
158 "ld1w { z16.s }, p1/Z, [x25, #2, MUL VL]\n"
159 ".inst 0x658aa612 // bfcvt z18.h, p1/M, z16.s\n"
160 "ld1w { z16.s }, p1/Z, [x25, #3, MUL VL]\n"
161 ".inst 0x658aa611 // bfcvt z17.h, p1/M, z16.s\n"
162 "addvl x25, x25, #4\n"
163 "ld1w { z16.s }, p1/Z, [x24]\n"
164 ".inst 0x648aa614 // bfcvtnt z20.h, p1/M, z16.s\n"
165 "ld1w { z16.s }, p1/Z, [x24, #1, MUL VL]\n"
166 ".inst 0x648aa613 // bfcvtnt z19.h, p1/M, z16.s\n"
167 "ld1w { z16.s }, p1/Z, [x24, #2, MUL VL]\n"
168 ".inst 0x648aa612 // bfcvtnt z18.h, p1/M, z16.s\n"
169 "ld1w { z16.s }, p1/Z, [x24, #3, MUL VL]\n"
170 "st1h { z20.h }, p1, [x21]\n"
171 "add x21, x21, %x[out_stride]\n"
172 "addvl x24, x24, #4\n"
173 "st1h { z19.h }, p1, [x21]\n"
174 "add x21, x21, %x[out_stride]\n"
175 ".inst 0x648aa611 // bfcvtnt z17.h, p1/M, z16.s\n"
176 "st1h { z18.h }, p1, [x21]\n"
177 "add x21, x21, %x[out_stride]\n"
178 "st1h { z17.h }, p1, [x21]\n"
179 "add x21, x21, %x[out_stride]\n"
180 "bge 8b\n"
181 "9:" // Tail row loop: Unroll column loop skip
182 "cbz x20, 11f\n"
183 "10:" // Tail row loop: Column loop
184 "whilelt p0.s, XZR, x20\n"
185 "ld1w { z16.s }, p0/Z, [x25]\n"
186 ".inst 0x658aa611 // bfcvt z17.h, p1/M, z16.s\n"
187 "decw x20\n"
188 "ld1w { z16.s }, p0/Z, [x24]\n"
189 "cmp x20, #0x0\n"
190 "addvl x25, x25, #1\n"
191 ".inst 0x648aa611 // bfcvtnt z17.h, p1/M, z16.s\n"
192 "addvl x24, x24, #1\n"
193 "st1h { z17.h }, p1, [x21]\n"
194 "add x21, x21, %x[out_stride]\n"
195 "bgt 10b\n"
196 "11:" // Tail row loop: Column loop skip
197 "cmp %x[height], #0x1\n"
198 "addvl %x[out], %x[out], #1\n"
199 "bge 7b\n"
200 "12:" // Done
201 ".inst 0xd503467f // SMSTOP\n"
202 : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
203 : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [pad_row] "r" (pad_row), [width] "r" (width)
204 : "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"
205 );
206}
207
208} // anonymous namespace
209template<>
210void Transform<1, 2, true, VLType::SME>(
211 bfloat16 *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
212{
213 sme_transpose_interleave_1VL_2x2_fp32bf16(
214 out,
215 in + k0 * stride + x0,
216 (xmax-x0),
217 stride * sizeof(float),
218 (kmax-k0)
219 );
220}
221
222#endif