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 __ARM_FEATURE_SVE
28 
29 
30 namespace {
31 
sve_transpose_interleave_1VL(uint32_t * out,const uint32_t * in,size_t width,size_t in_stride,size_t height)32 void sve_transpose_interleave_1VL(uint32_t *out, const uint32_t *in, size_t width, size_t in_stride, size_t height)
33 {
34     size_t out_stride = 1 * height * get_vector_length<uint8_t>();
35 
36     __asm__ __volatile__(
37       "ptrue p1.b\n"
38       "cmp %x[height], #0x4\n"
39       "blt 6f\n"
40       "1:"  // Main row loop: Head
41       "mov x25, %x[in]\n"
42       "mov x24, %x[out]\n"
43       "add x23, x25, %x[in_stride]\n"
44       "add x22, x23, %x[in_stride]\n"
45       "add x21, x22, %x[in_stride]\n"
46       "add %x[in], x21, %x[in_stride]\n"
47       "sub %x[height], %x[height], #0x4\n"
48       "mov x20, %x[width]\n"
49       "cntw x19, ALL, MUL #2\n"
50       "cmp x20, x19\n"
51       "blt 3f\n"
52       "2:"  // Main row loop: Unroll column loop
53       "ld1w { z23.s }, p1/Z, [x25]\n"
54       "sub x20, x20, x19\n"
55       "ld1w { z22.s }, p1/Z, [x25, #1, MUL VL]\n"
56       "addvl x25, x25, #2\n"
57       "ld1w { z21.s }, p1/Z, [x23]\n"
58       "cmp x20, x19\n"
59       "ld1w { z20.s }, p1/Z, [x23, #1, MUL VL]\n"
60       "addvl x23, x23, #2\n"
61       "ld1w { z19.s }, p1/Z, [x22]\n"
62       "ld1w { z18.s }, p1/Z, [x22, #1, MUL VL]\n"
63       "addvl x22, x22, #2\n"
64       "ld1w { z17.s }, p1/Z, [x21]\n"
65       "ld1w { z16.s }, p1/Z, [x21, #1, MUL VL]\n"
66       "addvl x21, x21, #2\n"
67       "st1w { z23.s }, p1, [x24]\n"
68       "st1w { z21.s }, p1, [x24, #1, MUL VL]\n"
69       "st1w { z19.s }, p1, [x24, #2, MUL VL]\n"
70       "st1w { z17.s }, p1, [x24, #3, MUL VL]\n"
71       "add x24, x24, %x[out_stride]\n"
72       "st1w { z22.s }, p1, [x24]\n"
73       "st1w { z20.s }, p1, [x24, #1, MUL VL]\n"
74       "st1w { z18.s }, p1, [x24, #2, MUL VL]\n"
75       "st1w { z16.s }, p1, [x24, #3, MUL VL]\n"
76       "add x24, x24, %x[out_stride]\n"
77       "bge 2b\n"
78       "3:"  // Main row loop: Unroll column loop skip
79       "cbz x20, 5f\n"
80       "4:"  // Main row loop: Column loop
81       "whilelt p0.s, XZR, x20\n"
82       "ld1w { z19.s }, p0/Z, [x25]\n"
83       "addvl x25, x25, #1\n"
84       "ld1w { z18.s }, p0/Z, [x23]\n"
85       "addvl x23, x23, #1\n"
86       "ld1w { z17.s }, p0/Z, [x22]\n"
87       "addvl x22, x22, #1\n"
88       "ld1w { z16.s }, p0/Z, [x21]\n"
89       "addvl x21, x21, #1\n"
90       "st1w { z19.s }, p1, [x24]\n"
91       "decw x20\n"
92       "st1w { z18.s }, p1, [x24, #1, MUL VL]\n"
93       "cmp x20, #0x0\n"
94       "st1w { z17.s }, p1, [x24, #2, MUL VL]\n"
95       "st1w { z16.s }, p1, [x24, #3, MUL VL]\n"
96       "add x24, x24, %x[out_stride]\n"
97       "bgt 4b\n"
98       "5:"  // Main row loop: Column loop skip
99       "addvl %x[out], %x[out], #4\n"
100       "cmp %x[height], #0x4\n"
101       "bge 1b\n"
102       "cbz %x[height], 12f\n"
103       "6:"  // Main loop skip
104 
105       "7:"  // Tail row loop: Head
106       "mov x25, %x[in]\n"
107       "mov x24, %x[out]\n"
108       "add %x[in], x25, %x[in_stride]\n"
109       "sub %x[height], %x[height], #0x1\n"
110       "mov x20, %x[width]\n"
111       "cntw x19, ALL, MUL #2\n"
112       "cmp x20, x19\n"
113       "blt 9f\n"
114       "8:"  // Tail row loop: Unroll column loop
115       "ld1w { z17.s }, p1/Z, [x25]\n"
116       "sub x20, x20, x19\n"
117       "ld1w { z16.s }, p1/Z, [x25, #1, MUL VL]\n"
118       "addvl x25, x25, #2\n"
119       "cmp x20, x19\n"
120       "st1w { z17.s }, p1, [x24]\n"
121       "add x24, x24, %x[out_stride]\n"
122       "st1w { z16.s }, p1, [x24]\n"
123       "add x24, x24, %x[out_stride]\n"
124       "bge 8b\n"
125       "9:"  // Tail row loop: Unroll column loop skip
126       "cbz x20, 11f\n"
127       "10:"  // Tail row loop: Column loop
128       "whilelt p0.s, XZR, x20\n"
129       "ld1w { z16.s }, p0/Z, [x25]\n"
130       "addvl x25, x25, #1\n"
131       "decw x20\n"
132       "st1w { z16.s }, p1, [x24]\n"
133       "add x24, x24, %x[out_stride]\n"
134       "cmp x20, #0x0\n"
135       "bgt 10b\n"
136       "11:"  // Tail row loop: Column loop skip
137       "addvl %x[out], %x[out], #1\n"
138       "cmp %x[height], #0x1\n"
139       "bge 7b\n"
140       "12:"  // Done
141 
142       : [height] "+&r" (height), [in] "+&r" (in), [out] "+&r" (out)
143       : [in_stride] "r" (in_stride), [out_stride] "r" (out_stride), [width] "r" (width)
144       : "cc", "memory", "p0", "p1", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23"
145     );
146 }
147 
148 } // anonymous namespace
149 
150 template<>
Transform(float * out,const float * in,int stride,int x0,int xmax,int k0,int kmax)151 void Transform<1, 1, true, VLType::SVE>(
152     float *out, const float *in, int stride, int x0, int xmax, int k0, int kmax)
153 {
154     sve_transpose_interleave_1VL(
155         reinterpret_cast<uint32_t *>(out),
156         reinterpret_cast<const uint32_t *>(in + k0 * stride + x0),
157         (xmax-x0) * sizeof(float) / 4,
158         stride * sizeof(float),
159         (kmax-k0)
160     );
161 }
162 
163 #endif
164