blob: 14636e321840d7c4e66e0ac00a79e77cdde3b43a [file] [log] [blame]
Viet-Hoa Do03b29712022-06-01 11:47:14 +01001/*
Michael Tyler7d9a6262023-02-01 16:37:07 +00002 * Copyright (c) 2022-2023 Arm Limited.
Viet-Hoa Do03b29712022-06-01 11:47:14 +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.
Viet-Hoa Do03b29712022-06-01 11:47:14 +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.
Viet-Hoa Do03b29712022-06-01 11:47:14 +010023 */
24
25#pragma once
26
Michael Tyler74921ee2023-04-12 17:43:17 +010027#if defined(ARM_COMPUTE_ENABLE_SME)
Viet-Hoa Do03b29712022-06-01 11:47:14 +010028
29namespace {
30
31void sme_transpose_interleave_4VL(uint16_t *out, const uint16_t *in, size_t width, size_t in_stride, size_t height)
32{
33 size_t out_stride = 4 * height * sme::get_vector_length<uint8_t>();
34
35 __asm__ __volatile__(
36 ".inst 0xd503477f // SMSTART ZA\n"
37 "cmp %x[height], #0x4\n"
38 "ptrue p4.b\n"
39 "blt 4f\n"
40 "1:" // Main row loop: Head
Michael Tyler7d9a6262023-02-01 16:37:07 +000041 "mov x26, %x[in]\n"
42 "add x25, x26, %x[in_stride]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010043 "add x24, x25, %x[in_stride]\n"
44 "add x23, x24, %x[in_stride]\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000045 "add %x[in], x23, %x[in_stride]\n"
46 "mov x22, %x[out]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010047 "sub %x[height], %x[height], #0x4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000048 "mov x21, %x[width]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010049 "2:" // Main row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +000050 "mov x20, x21\n"
51 "whilelt p3.h, XZR, x20\n"
52 "ld1h { z31.h }, p3/Z, [x26]\n"
53 "dech x20\n"
54 "whilelt p2.h, XZR, x20\n"
55 "ld1h { z30.h }, p2/Z, [x26, #1, MUL VL]\n"
56 "dech x20\n"
57 "whilelt p1.h, XZR, x20\n"
58 "ld1h { z29.h }, p1/Z, [x26, #2, MUL VL]\n"
59 "dech x20\n"
60 "whilelt p0.h, XZR, x20\n"
61 "ld1h { z28.h }, p0/Z, [x26, #3, MUL VL]\n"
62 "mov x20, x22\n"
63 "dech x21, ALL, MUL #4\n"
64 "ld1h { z27.h }, p3/Z, [x25]\n"
65 "ld1h { z26.h }, p2/Z, [x25, #1, MUL VL]\n"
66 "cmp x21, #0x0\n"
67 "addvl x26, x26, #4\n"
68 "ld1h { z25.h }, p1/Z, [x25, #2, MUL VL]\n"
69 "add x22, x22, %x[out_stride]\n"
70 "ld1h { z24.h }, p0/Z, [x25, #3, MUL VL]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010071 "addvl x25, x25, #4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000072 "ld1h { z23.h }, p3/Z, [x24]\n"
73 "ld1h { z22.h }, p2/Z, [x24, #1, MUL VL]\n"
74 "ld1h { z21.h }, p1/Z, [x24, #2, MUL VL]\n"
75 "ld1h { z20.h }, p0/Z, [x24, #3, MUL VL]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010076 "addvl x24, x24, #4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000077 "ld1h { z19.h }, p3/Z, [x23]\n"
78 "ld1h { z18.h }, p2/Z, [x23, #1, MUL VL]\n"
79 "ld1h { z17.h }, p1/Z, [x23, #2, MUL VL]\n"
80 "ld1h { z16.h }, p0/Z, [x23, #3, MUL VL]\n"
81 "st1h { z31.h }, p4, [x20]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010082 "addvl x23, x23, #4\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000083 "st1h { z30.h }, p4, [x20, #1, MUL VL]\n"
84 "st1h { z29.h }, p4, [x20, #2, MUL VL]\n"
85 "st1h { z28.h }, p4, [x20, #3, MUL VL]\n"
86 "st1h { z27.h }, p4, [x20, #4, MUL VL]\n"
87 "st1h { z26.h }, p4, [x20, #5, MUL VL]\n"
88 "st1h { z25.h }, p4, [x20, #6, MUL VL]\n"
89 "st1h { z24.h }, p4, [x20, #7, MUL VL]\n"
90 "addvl x20, x20, #16\n"
91 "st1h { z23.h }, p4, [x20, #-8, MUL VL]\n"
92 "st1h { z22.h }, p4, [x20, #-7, MUL VL]\n"
93 "st1h { z21.h }, p4, [x20, #-6, MUL VL]\n"
94 "st1h { z20.h }, p4, [x20, #-5, MUL VL]\n"
95 "st1h { z19.h }, p4, [x20, #-4, MUL VL]\n"
96 "st1h { z18.h }, p4, [x20, #-3, MUL VL]\n"
97 "st1h { z17.h }, p4, [x20, #-2, MUL VL]\n"
98 "st1h { z16.h }, p4, [x20, #-1, MUL VL]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010099 "bgt 2b\n"
100 "3:" // Main row loop: Column loop skip
101 "cmp %x[height], #0x4\n"
102 "addvl %x[out], %x[out], #16\n"
103 "bge 1b\n"
104 "cbz %x[height], 8f\n"
105 "4:" // Main loop skip
106 "5:" // Tail row loop: Head
Michael Tyler7d9a6262023-02-01 16:37:07 +0000107 "mov x26, %x[in]\n"
108 "add %x[in], x26, %x[in_stride]\n"
109 "mov x22, %x[out]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100110 "sub %x[height], %x[height], #0x1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000111 "mov x21, %x[width]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100112 "6:" // Tail row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +0000113 "mov x20, x21\n"
114 "whilelt p0.h, XZR, x20\n"
115 "ld1h { z19.h }, p0/Z, [x26]\n"
116 "dech x20\n"
117 "whilelt p0.h, XZR, x20\n"
118 "ld1h { z18.h }, p0/Z, [x26, #1, MUL VL]\n"
119 "dech x20\n"
120 "whilelt p0.h, XZR, x20\n"
121 "ld1h { z17.h }, p0/Z, [x26, #2, MUL VL]\n"
122 "dech x20\n"
123 "dech x21, ALL, MUL #4\n"
124 "whilelt p0.h, XZR, x20\n"
125 "cmp x21, #0x0\n"
126 "ld1h { z16.h }, p0/Z, [x26, #3, MUL VL]\n"
127 "st1h { z19.h }, p4, [x22]\n"
128 "addvl x26, x26, #4\n"
129 "st1h { z18.h }, p4, [x22, #1, MUL VL]\n"
130 "st1h { z17.h }, p4, [x22, #2, MUL VL]\n"
131 "st1h { z16.h }, p4, [x22, #3, MUL VL]\n"
132 "add x22, x22, %x[out_stride]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100133 "bgt 6b\n"
134 "7:" // Tail row loop: Column loop skip
135 "cmp %x[height], #0x1\n"
136 "addvl %x[out], %x[out], #4\n"
137 "bge 5b\n"
138 "8:" // Done
139 ".inst 0xd503467f // SMSTOP\n"
140 : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
141 : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [width] "r" (width)
Michael Tyler7d9a6262023-02-01 16:37:07 +0000142 : "cc", "memory", "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "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"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100143 );
144}
145
146} // anonymous namespace
147
148template<>
149void Transform<4, 1, true, VLType::SME>(
150 float *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
151{
152 sme_transpose_interleave_4VL(
153 reinterpret_cast<uint16_t *>(out),
154 reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
155 (xmax-x0) * sizeof(float) / 2,
156 stride * sizeof(float),
157 (kmax-k0)
158 );
159}
160
161template<>
162void Transform<4, 1, true, VLType::SME>(
163 bfloat16 *out, const bfloat16 *in, int stride, int x0, int xmax, int k0, int kmax)
164{
165 sme_transpose_interleave_4VL(
166 reinterpret_cast<uint16_t *>(out),
167 reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
168 (xmax-x0) * sizeof(bfloat16) / 2,
169 stride * sizeof(bfloat16),
170 (kmax-k0)
171 );
172}
173
174template<>
175void Transform<4, 1, true, VLType::SME>(
176 __fp16 *out, const __fp16 *in, int stride, int x0, int xmax, int k0, int kmax)
177{
178 sme_transpose_interleave_4VL(
179 reinterpret_cast<uint16_t *>(out),
180 reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
181 (xmax-x0) * sizeof(__fp16) / 2,
182 stride * sizeof(__fp16),
183 (kmax-k0)
184 );
185}
186
Michael Tyler74921ee2023-04-12 17:43:17 +0100187
188#endif // defined(ARM_COMPUTE_ENABLE_SME)