1 /*
2  * Copyright (c) 2021 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 __aarch64__
28 
29 namespace {
30 
a64_transpose_interleave_24(uint16_t * out,const uint16_t * in,size_t width,size_t in_stride,size_t height)31 void a64_transpose_interleave_24(uint16_t *out, const uint16_t *in, size_t width, size_t in_stride, size_t height)
32 {
33     size_t out_stride = 12 * height * sizeof(uint16_t);
34 
35     __asm__ __volatile__(
36       "cmp %x[height], #0x4\n"
37       "blt 10f\n"
38       "1:"  // Main row loop: Head
39       "mov x24, %x[in]\n"
40       "mov x23, %x[out]\n"
41       "add x22, x24, %x[in_stride]\n"
42       "add x21, x22, %x[in_stride]\n"
43       "add x20, x21, %x[in_stride]\n"
44       "add %x[in], x20, %x[in_stride]\n"
45       "sub %x[height], %x[height], #0x4\n"
46       "mov x19, %x[width]\n"
47       "cmp x19, #0x18\n"
48       "blt 3f\n"
49       "2:"  // Main row loop: Unroll column loop
50       "ldr q29, [x24], #0x10\n"
51       "sub x19, x19, #0x18\n"
52       "ldr q18, [x22], #0x10\n"
53       "dup v28.2d, v18.d[1]\n"
54       "ldr q16, [x24], #0x10\n"
55       "cmp x19, #0x18\n"
56       "dup v27.2d, v16.d[0]\n"
57       "ldr q17, [x24], #0x10\n"
58       "dup v26.2d, v16.d[1]\n"
59       "ldr q16, [x22], #0x10\n"
60       "mov v27.d[1], v18.d[0]\n"
61       "ldr q25, [x21], #0x10\n"
62       "ldr q24, [x20], #0x10\n"
63       "mov v26.d[1], v17.d[0]\n"
64       "ldr q23, [x22], #0x10\n"
65       "mov v28.d[1], v16.d[0]\n"
66       "dup v22.2d, v17.d[1]\n"
67       "ldr q17, [x21], #0x10\n"
68       "dup v21.2d, v24.d[1]\n"
69       "ldr q20, [x20], #0x10\n"
70       "mov v22.d[1], v16.d[1]\n"
71       "ldr q16, [x21], #0x10\n"
72       "dup v19.2d, v17.d[0]\n"
73       "dup v18.2d, v17.d[1]\n"
74       "ldr q17, [x20], #0x10\n"
75       "mov v19.d[1], v24.d[0]\n"
76       "str q29, [x23, #0x0]\n"
77       "mov v21.d[1], v20.d[0]\n"
78       "str q27, [x23, #0x10]\n"
79       "str q28, [x23, #0x20]\n"
80       "mov v18.d[1], v16.d[0]\n"
81       "dup v16.2d, v16.d[1]\n"
82       "str q25, [x23, #0x30]\n"
83       "mov v16.d[1], v20.d[1]\n"
84       "str q19, [x23, #0x40]\n"
85       "str q21, [x23, #0x50]\n"
86       "add x23, x23, %x[out_stride]\n"
87       "str q26, [x23, #0x0]\n"
88       "str q22, [x23, #0x10]\n"
89       "str q23, [x23, #0x20]\n"
90       "str q18, [x23, #0x30]\n"
91       "str q16, [x23, #0x40]\n"
92       "str q17, [x23, #0x50]\n"
93       "add x23, x23, %x[out_stride]\n"
94       "bge 2b\n"
95       "3:"  // Main row loop: Unroll column loop skip
96       "cmp x19, #0xc\n"
97       "blt 5f\n"
98       "4:"  // Main row loop: Column loop
99       "ldr q22, [x24], #0x10\n"
100       "sub x19, x19, #0xc\n"
101       "ldr q16, [x22], #0x10\n"
102       "dup v21.2d, v16.d[1]\n"
103       "ldr d20, [x24], #0x8\n"
104       "cmp x19, #0xc\n"
105       "mov v20.d[1], v16.d[0]\n"
106       "ldr d16, [x22], #0x8\n"
107       "ldr q19, [x21], #0x10\n"
108       "mov v21.d[1], v16.d[0]\n"
109       "ldr d18, [x21], #0x8\n"
110       "ldr q16, [x20], #0x10\n"
111       "mov v18.d[1], v16.d[0]\n"
112       "ldr d17, [x20], #0x8\n"
113       "dup v16.2d, v16.d[1]\n"
114       "str q22, [x23, #0x0]\n"
115       "str q20, [x23, #0x10]\n"
116       "mov v16.d[1], v17.d[0]\n"
117       "str q21, [x23, #0x20]\n"
118       "str q19, [x23, #0x30]\n"
119       "str q18, [x23, #0x40]\n"
120       "str q16, [x23, #0x50]\n"
121       "add x23, x23, %x[out_stride]\n"
122       "bge 4b\n"
123       "5:"  // Main row loop: Column loop skip
124       "cmp x19, #0x4\n"
125       "blt 7f\n"
126       "6:"  // Main row loop: width 4 loop: loop
127       "ldr d19, [x24], #0x8\n"
128       "sub x19, x19, #0x4\n"
129       "ldr d18, [x22], #0x8\n"
130       "cmp x19, #0x4\n"
131       "ldr d17, [x21], #0x8\n"
132       "ldr d16, [x20], #0x8\n"
133       "str d19, [x23, #0x0]\n"
134       "str d18, [x23, #0x18]\n"
135       "str d17, [x23, #0x30]\n"
136       "str d16, [x23, #0x48]\n"
137       "add x23, x23, #0x8\n"
138       "bge 6b\n"
139       "7:"  // Main row loop: width 4 loop: skip
140       "cmp x19, #0x1\n"
141       "blt 9f\n"
142       "8:"  // Main row loop: width 1 loop: loop
143       "ldr h19, [x24], #0x2\n"
144       "sub x19, x19, #0x1\n"
145       "ldr h18, [x22], #0x2\n"
146       "cmp x19, #0x1\n"
147       "ldr h17, [x21], #0x2\n"
148       "ldr h16, [x20], #0x2\n"
149       "str h19, [x23, #0x0]\n"
150       "str h18, [x23, #0x18]\n"
151       "str h17, [x23, #0x30]\n"
152       "str h16, [x23, #0x48]\n"
153       "add x23, x23, #0x2\n"
154       "bge 8b\n"
155       "9:"  // Main row loop: width 1 loop: skip
156       "add %x[out], %x[out], #0x60\n"
157       "cmp %x[height], #0x4\n"
158       "bge 1b\n"
159       "cbz %x[height], 20f\n"
160       "10:"  // Main loop skip
161 
162       "11:"  // Tail row loop: Head
163       "mov x24, %x[in]\n"
164       "mov x23, %x[out]\n"
165       "add %x[in], x24, %x[in_stride]\n"
166       "sub %x[height], %x[height], #0x1\n"
167       "mov x19, %x[width]\n"
168       "cmp x19, #0x18\n"
169       "blt 13f\n"
170       "12:"  // Tail row loop: Unroll column loop
171       "ldr q19, [x24], #0x10\n"
172       "sub x19, x19, #0x18\n"
173       "cmp x19, #0x18\n"
174       "ldr q16, [x24], #0x10\n"
175       "dup v18.2d, v16.d[0]\n"
176       "ldr q17, [x24], #0x10\n"
177       "dup v16.2d, v16.d[1]\n"
178       "str q19, [x23, #0x0]\n"
179       "str d18, [x23, #0x10]\n"
180       "mov v16.d[1], v17.d[0]\n"
181       "add x23, x23, %x[out_stride]\n"
182       "str q16, [x23, #0x0]\n"
183       "dup v16.2d, v17.d[1]\n"
184       "str d16, [x23, #0x10]\n"
185       "add x23, x23, %x[out_stride]\n"
186       "bge 12b\n"
187       "13:"  // Tail row loop: Unroll column loop skip
188       "cmp x19, #0xc\n"
189       "blt 15f\n"
190       "14:"  // Tail row loop: Column loop
191       "ldr q17, [x24], #0x10\n"
192       "sub x19, x19, #0xc\n"
193       "cmp x19, #0xc\n"
194       "ldr d16, [x24], #0x8\n"
195       "str q17, [x23, #0x0]\n"
196       "str d16, [x23, #0x10]\n"
197       "add x23, x23, %x[out_stride]\n"
198       "bge 14b\n"
199       "15:"  // Tail row loop: Column loop skip
200       "cmp x19, #0x4\n"
201       "blt 17f\n"
202       "16:"  // Tail row loop: width 4 loop: loop
203       "ldr d16, [x24], #0x8\n"
204       "sub x19, x19, #0x4\n"
205       "cmp x19, #0x4\n"
206       "str d16, [x23, #0x0]\n"
207       "add x23, x23, #0x8\n"
208       "bge 16b\n"
209       "17:"  // Tail row loop: width 4 loop: skip
210       "cmp x19, #0x1\n"
211       "blt 19f\n"
212       "18:"  // Tail row loop: width 1 loop: loop
213       "ldr h16, [x24], #0x2\n"
214       "sub x19, x19, #0x1\n"
215       "cmp x19, #0x1\n"
216       "str h16, [x23, #0x0]\n"
217       "add x23, x23, #0x2\n"
218       "bge 18b\n"
219       "19:"  // Tail row loop: width 1 loop: skip
220       "add %x[out], %x[out], #0x18\n"
221       "cmp %x[height], #0x1\n"
222       "bge 11b\n"
223       "20:"  // Done
224 
225       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
226       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [width] "r" (width)
227       : "cc", "memory", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "x19", "x20", "x21", "x22", "x23", "x24"
228     );
229 }
230 
231 } // anonymous namespace
232 
233 template<>
Transform(float * out,const float * in,int stride,int x0,int xmax,int k0,int kmax)234 void Transform<6, 1, true, VLType::None>(
235     float *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
236 {
237     a64_transpose_interleave_24(
238         reinterpret_cast<uint16_t *>(out),
239         reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
240         (xmax-x0) * sizeof(float) / 2,
241         stride * sizeof(float),
242         (kmax-k0)
243     );
244 }
245 
246 template<>
Transform(int16_t * out,const int16_t * in,int stride,int x0,int xmax,int k0,int kmax)247 void Transform<12, 1, true, VLType::None>(
248     int16_t *out, const int16_t *in, int stride, int x0, int xmax, int k0, int kmax)
249 {
250     a64_transpose_interleave_24(
251         reinterpret_cast<uint16_t *>(out),
252         reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
253         (xmax-x0) * sizeof(int16_t) / 2,
254         stride * sizeof(int16_t),
255         (kmax-k0)
256     );
257 }
258 
259 template<>
Transform(uint16_t * out,const uint16_t * in,int stride,int x0,int xmax,int k0,int kmax)260 void Transform<12, 1, true, VLType::None>(
261     uint16_t *out, const uint16_t *in, int stride, int x0, int xmax, int k0, int kmax)
262 {
263     a64_transpose_interleave_24(
264         reinterpret_cast<uint16_t *>(out),
265         reinterpret_cast<const uint16_t *>(in + k0 * stride + x0),
266         (xmax-x0) * sizeof(uint16_t) / 2,
267         stride * sizeof(uint16_t),
268         (kmax-k0)
269     );
270 }
271 
272 #endif
273