blob: 080db1c5c1f69945eece53a4ce7e301303a7fe07 [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
27#ifdef __ARM_FEATURE_SVE
28
29
30namespace {
31
32void 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
Michael Tyler7d9a6262023-02-01 16:37:07 +000046 "mov x25, %x[in]\n"
47 "add x24, x25, %x[in_stride]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010048 "cmp %x[height], #0x1\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000049 "add %x[in], x24, %x[in_stride]\n"
50 "mov x23, %x[out]\n"
51 "csel x24, x24, %x[pad_row], GT\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010052 "sub %x[height], %x[height], #0x2\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000053 "mov x22, %x[width]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +010054 "2:" // Main row loop: Column loop
Michael Tyler7d9a6262023-02-01 16:37:07 +000055 "mov x21, x22\n"
56 "whilelt p1.s, XZR, x21\n"
57 "ld1w { z16.s }, p1/Z, [x25]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000058 ".inst 0x658abe00 // bfcvt z0.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000059 "decw x21\n"
60 "whilelt p0.s, XZR, x21\n"
61 "ld1w { z16.s }, p0/Z, [x25, #1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000062 ".inst 0x658abe1f // bfcvt z31.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000063 "decw x21\n"
64 "whilelt p6.s, XZR, x21\n"
65 "ld1w { z16.s }, p6/Z, [x25, #2, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000066 ".inst 0x658abe1e // bfcvt z30.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000067 "decw x21\n"
68 "whilelt p5.s, XZR, x21\n"
69 "ld1w { z16.s }, p5/Z, [x25, #3, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000070 ".inst 0x658abe1d // bfcvt z29.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000071 "decw x21\n"
72 "whilelt p4.s, XZR, x21\n"
73 "ld1w { z16.s }, p4/Z, [x25, #4, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000074 ".inst 0x658abe1c // bfcvt z28.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000075 "decw x21\n"
76 "whilelt p3.s, XZR, x21\n"
77 "ld1w { z16.s }, p3/Z, [x25, #5, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000078 ".inst 0x658abe1b // bfcvt z27.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000079 "decw x21\n"
80 "whilelt p2.s, XZR, x21\n"
81 "ld1w { z16.s }, p2/Z, [x25, #6, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000082 ".inst 0x658abe1a // bfcvt z26.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000083 "decw x21\n"
84 "ld1w { z16.s }, p1/Z, [x24]\n"
85 "whilelt p1.s, XZR, x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000086 ".inst 0x648abe00 // bfcvtnt z0.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000087 "decw x21\n"
88 "ld1w { z16.s }, p1/Z, [x25, #7, MUL VL]\n"
89 "addvl x25, x25, #16\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000090 ".inst 0x658abe19 // bfcvt z25.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000091 "ld1w { z16.s }, p0/Z, [x24, #1, MUL VL]\n"
92 "whilelt p0.s, XZR, x21\n"
93 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000094 ".inst 0x648abe1f // bfcvtnt z31.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000095 "ld1w { z16.s }, p0/Z, [x25, #-8, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +000096 ".inst 0x658abe18 // bfcvt z24.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +000097 "mov x20, x23\n"
98 "decw x22, ALL, MUL #16\n"
99 "ld1w { z16.s }, p6/Z, [x24, #2, MUL VL]\n"
100 "whilelt p6.s, XZR, x21\n"
101 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000102 ".inst 0x648abe1e // bfcvtnt z30.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000103 "ld1w { z16.s }, p6/Z, [x25, #-7, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000104 ".inst 0x658abe17 // bfcvt z23.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000105 "add x23, x23, %x[out_stride]\n"
106 "ld1w { z16.s }, p5/Z, [x24, #3, MUL VL]\n"
107 "whilelt p5.s, XZR, x21\n"
108 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000109 ".inst 0x648abe1d // bfcvtnt z29.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000110 "ld1w { z16.s }, p5/Z, [x25, #-6, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000111 ".inst 0x658abe16 // bfcvt z22.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000112 "ld1w { z16.s }, p4/Z, [x24, #4, MUL VL]\n"
113 "whilelt p4.s, XZR, x21\n"
114 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000115 ".inst 0x648abe1c // bfcvtnt z28.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000116 "ld1w { z16.s }, p4/Z, [x25, #-5, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000117 ".inst 0x658abe15 // bfcvt z21.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000118 "ld1w { z16.s }, p3/Z, [x24, #5, MUL VL]\n"
119 "whilelt p3.s, XZR, x21\n"
120 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000121 ".inst 0x648abe1b // bfcvtnt z27.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000122 "ld1w { z16.s }, p3/Z, [x25, #-4, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000123 ".inst 0x658abe14 // bfcvt z20.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000124 "ld1w { z16.s }, p2/Z, [x24, #6, MUL VL]\n"
125 "whilelt p2.s, XZR, x21\n"
126 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000127 ".inst 0x648abe1a // bfcvtnt z26.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000128 "ld1w { z16.s }, p2/Z, [x25, #-3, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000129 ".inst 0x658abe13 // bfcvt z19.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000130 "ld1w { z16.s }, p1/Z, [x24, #7, MUL VL]\n"
131 "whilelt p1.s, XZR, x21\n"
132 "decw x21\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000133 ".inst 0x648abe19 // bfcvtnt z25.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000134 "ld1w { z16.s }, p1/Z, [x25, #-2, MUL VL]\n"
135 "addvl x24, x24, #16\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000136 ".inst 0x658abe12 // bfcvt z18.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000137 "ld1w { z16.s }, p0/Z, [x24, #-8, MUL VL]\n"
138 "whilelt p0.s, XZR, x21\n"
139 "cmp x22, #0x0\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000140 ".inst 0x648abe18 // bfcvtnt z24.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000141 "ld1w { z16.s }, p0/Z, [x25, #-1, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000142 ".inst 0x658abe11 // bfcvt z17.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000143 "ld1w { z16.s }, p6/Z, [x24, #-7, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000144 ".inst 0x648abe17 // bfcvtnt z23.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000145 "ld1w { z16.s }, p5/Z, [x24, #-6, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000146 ".inst 0x648abe16 // bfcvtnt z22.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000147 "ld1w { z16.s }, p4/Z, [x24, #-5, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000148 ".inst 0x648abe15 // bfcvtnt z21.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000149 "ld1w { z16.s }, p3/Z, [x24, #-4, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000150 ".inst 0x648abe14 // bfcvtnt z20.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000151 "ld1w { z16.s }, p2/Z, [x24, #-3, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000152 ".inst 0x648abe13 // bfcvtnt z19.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000153 "ld1w { z16.s }, p1/Z, [x24, #-2, MUL VL]\n"
Michael Tylerbe13cea2023-01-17 11:04:14 +0000154 ".inst 0x648abe12 // bfcvtnt z18.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000155 "ld1w { z16.s }, p0/Z, [x24, #-1, MUL VL]\n"
156 "st1h { z0.h }, p7, [x20]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100157 ".inst 0x648abe11 // bfcvtnt z17.h, p7/M, z16.s\n"
Michael Tyler7d9a6262023-02-01 16:37:07 +0000158 "st1h { z31.h }, p7, [x20, #1, MUL VL]\n"
159 "st1h { z30.h }, p7, [x20, #2, MUL VL]\n"
160 "st1h { z29.h }, p7, [x20, #3, MUL VL]\n"
161 "st1h { z28.h }, p7, [x20, #4, MUL VL]\n"
162 "st1h { z27.h }, p7, [x20, #5, MUL VL]\n"
163 "st1h { z26.h }, p7, [x20, #6, MUL VL]\n"
164 "st1h { z25.h }, p7, [x20, #7, MUL VL]\n"
165 "addvl x20, x20, #16\n"
166 "st1h { z24.h }, p7, [x20, #-8, MUL VL]\n"
167 "st1h { z23.h }, p7, [x20, #-7, MUL VL]\n"
168 "st1h { z22.h }, p7, [x20, #-6, MUL VL]\n"
169 "st1h { z21.h }, p7, [x20, #-5, MUL VL]\n"
170 "st1h { z20.h }, p7, [x20, #-4, MUL VL]\n"
171 "st1h { z19.h }, p7, [x20, #-3, MUL VL]\n"
172 "st1h { z18.h }, p7, [x20, #-2, MUL VL]\n"
173 "st1h { z17.h }, p7, [x20, #-1, MUL VL]\n"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100174 "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)
Michael Tyler7d9a6262023-02-01 16:37:07 +0000182 : "cc", "memory", "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15", "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"
Viet-Hoa Do03b29712022-06-01 11:47:14 +0100183 );
184}
185
186} // anonymous namespace
187template<>
188void 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