1 /*
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 all
14  * 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 FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #if defined(ARM_COMPUTE_ENABLE_SME)
26 
27 #include <cstdint>
28 
29 namespace arm_conv {
30 namespace pooling {
31 
32 
sme_u8_nhwc_max_generic_depthfirst_impl(const uint64_t,const uint64_t n_valid_cells,uint64_t n_channels,const uint8_t * const * const inptrs,uint8_t * outptr)33 void sme_u8_nhwc_max_generic_depthfirst_impl(
34   const uint64_t,
35   const uint64_t n_valid_cells,
36   uint64_t n_channels,
37   const uint8_t *const *const inptrs,
38   uint8_t *outptr
39 )
40 {
41   __asm__ __volatile__(
42     ".inst 0xd503477f  // SMSTART ZA\n"
43     "mov x28, #0x0\n"
44     "cntb x27\n"
45     "cntb x26, ALL, MUL #2\n"
46     "cntb x25, ALL, MUL #3\n"
47     "whilelt p4.b, x28, %x[n_channels]\n"
48     "whilelt p3.b, x27, %x[n_channels]\n"
49     "whilelt p2.b, x26, %x[n_channels]\n"
50     "whilelt p1.b, x25, %x[n_channels]\n"
51     "ptrue p0.b\n"
52     "b.none 7f\n"
53     "1:"  // 4-vectors of channels
54     "lsr x24, %x[n_valid_cells], #0x2\n"
55     "mov z4.b, #0x0\n"
56     "mov z3.b, #0x0\n"
57     "mov x19, %x[inptrs]\n"
58     "mov z2.b, #0x0\n"
59     "mov z1.b, #0x0\n"
60     "cbz x24, 4f\n"
61     "ldp x23, x22, [x19, #0x0]\n"
62     "subs x24, x24, #0x1\n"
63     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
64     "ldp x21, x20, [x19, #0x10]\n"
65     "add x19, x19, #0x20\n"
66     "ld1b { z31.b }, p4/Z, [x22, x28]\n"
67     "ld1b { z23.b }, p4/Z, [x21, x28]\n"
68     "ld1b { z30.b }, p4/Z, [x20, x28]\n"
69     "ld1b { z18.b }, p3/Z, [x23, x27]\n"
70     "ld1b { z29.b }, p3/Z, [x22, x27]\n"
71     "ld1b { z22.b }, p3/Z, [x21, x27]\n"
72     "ld1b { z28.b }, p3/Z, [x20, x27]\n"
73     "ld1b { z17.b }, p2/Z, [x23, x26]\n"
74     "ld1b { z27.b }, p2/Z, [x22, x26]\n"
75     "ld1b { z21.b }, p2/Z, [x21, x26]\n"
76     "ld1b { z26.b }, p2/Z, [x20, x26]\n"
77     "ld1b { z16.b }, p1/Z, [x23, x25]\n"
78     "ld1b { z25.b }, p1/Z, [x22, x25]\n"
79     "ld1b { z20.b }, p1/Z, [x21, x25]\n"
80     "ld1b { z24.b }, p1/Z, [x20, x25]\n"
81     "beq 3f\n"
82     "2:"  // 4-vectors of channels: 4 inputs loop
83     "movprfx z19, z0\n umax z19.b, p0/M, z19.b, z31.b\n"
84     "umax z23.b, p0/M, z23.b, z30.b\n"
85     "ldp x23, x22, [x19, #0x0]\n"
86     "subs x24, x24, #0x1\n"
87     "umax z18.b, p0/M, z18.b, z29.b\n"
88     "umax z22.b, p0/M, z22.b, z28.b\n"
89     "ldp x21, x20, [x19, #0x10]\n"
90     "add x19, x19, #0x20\n"
91     "umax z17.b, p0/M, z17.b, z27.b\n"
92     "umax z21.b, p0/M, z21.b, z26.b\n"
93     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
94     "umax z16.b, p0/M, z16.b, z25.b\n"
95     "umax z20.b, p0/M, z20.b, z24.b\n"
96     "ld1b { z31.b }, p4/Z, [x22, x28]\n"
97     "umax z19.b, p0/M, z19.b, z23.b\n"
98     "umax z18.b, p0/M, z18.b, z22.b\n"
99     "ld1b { z23.b }, p4/Z, [x21, x28]\n"
100     "umax z17.b, p0/M, z17.b, z21.b\n"
101     "umax z16.b, p0/M, z16.b, z20.b\n"
102     "ld1b { z30.b }, p4/Z, [x20, x28]\n"
103     "umax z4.b, p0/M, z4.b, z19.b\n"
104     "umax z3.b, p0/M, z3.b, z18.b\n"
105     "ld1b { z18.b }, p3/Z, [x23, x27]\n"
106     "umax z2.b, p0/M, z2.b, z17.b\n"
107     "umax z1.b, p0/M, z1.b, z16.b\n"
108     "ld1b { z29.b }, p3/Z, [x22, x27]\n"
109     "ld1b { z22.b }, p3/Z, [x21, x27]\n"
110     "ld1b { z28.b }, p3/Z, [x20, x27]\n"
111     "ld1b { z17.b }, p2/Z, [x23, x26]\n"
112     "ld1b { z27.b }, p2/Z, [x22, x26]\n"
113     "ld1b { z21.b }, p2/Z, [x21, x26]\n"
114     "ld1b { z26.b }, p2/Z, [x20, x26]\n"
115     "ld1b { z16.b }, p1/Z, [x23, x25]\n"
116     "ld1b { z25.b }, p1/Z, [x22, x25]\n"
117     "ld1b { z20.b }, p1/Z, [x21, x25]\n"
118     "ld1b { z24.b }, p1/Z, [x20, x25]\n"
119     "bgt 2b\n"
120     "3:"  // 4-vectors of channels: 4 inputs tail
121     "movprfx z19, z0\n umax z19.b, p0/M, z19.b, z31.b\n"
122     "umax z23.b, p0/M, z23.b, z30.b\n"
123     "umax z18.b, p0/M, z18.b, z29.b\n"
124     "umax z22.b, p0/M, z22.b, z28.b\n"
125     "umax z17.b, p0/M, z17.b, z27.b\n"
126     "umax z21.b, p0/M, z21.b, z26.b\n"
127     "umax z16.b, p0/M, z16.b, z25.b\n"
128     "umax z20.b, p0/M, z20.b, z24.b\n"
129     "umax z19.b, p0/M, z19.b, z23.b\n"
130     "umax z18.b, p0/M, z18.b, z22.b\n"
131     "umax z17.b, p0/M, z17.b, z21.b\n"
132     "umax z16.b, p0/M, z16.b, z20.b\n"
133     "umax z4.b, p0/M, z4.b, z19.b\n"
134     "umax z3.b, p0/M, z3.b, z18.b\n"
135     "umax z2.b, p0/M, z2.b, z17.b\n"
136     "umax z1.b, p0/M, z1.b, z16.b\n"
137     "4:"  // 4-vectors of channels: After loop
138     "ands x20, %x[n_valid_cells], #0x3\n"
139     "beq 6f\n"
140     "5:"  // 4-vectors of channels: Single input loop
141     "ldr x23, [x19], #0x8\n"
142     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
143     "subs x20, x20, #0x1\n"
144     "umax z4.b, p0/M, z4.b, z0.b\n"
145     "ld1b { z18.b }, p3/Z, [x23, x27]\n"
146     "umax z3.b, p0/M, z3.b, z18.b\n"
147     "ld1b { z17.b }, p2/Z, [x23, x26]\n"
148     "umax z2.b, p0/M, z2.b, z17.b\n"
149     "ld1b { z16.b }, p1/Z, [x23, x25]\n"
150     "umax z1.b, p0/M, z1.b, z16.b\n"
151     "bgt 5b\n"
152     "6:"  // 4-vectors of channels: Single input loop: End
153     "st1b { z4.b }, p4, [%x[outptr], x28]\n"
154     "incb x28, ALL, MUL #4\n"
155     "st1b { z3.b }, p3, [%x[outptr], x27]\n"
156     "incb x27, ALL, MUL #4\n"
157     "st1b { z2.b }, p2, [%x[outptr], x26]\n"
158     "incb x26, ALL, MUL #4\n"
159     "st1b { z1.b }, p1, [%x[outptr], x25]\n"
160     "incb x25, ALL, MUL #4\n"
161     "whilelt p1.b, x25, %x[n_channels]\n"
162     "b.any 1b\n"
163     "7:"  // Single vector of channels
164     "whilelt p4.b, x28, %x[n_channels]\n"
165     "b.none 14f\n"
166     "8:"  // Single vector of channels: Loop
167     "lsr x24, %x[n_valid_cells], #0x2\n"
168     "mov z4.b, #0x0\n"
169     "mov x19, %x[inptrs]\n"
170     "cbz x24, 11f\n"
171     "ldp x23, x22, [x19, #0x0]\n"
172     "subs x24, x24, #0x1\n"
173     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
174     "ldp x21, x20, [x19, #0x10]\n"
175     "add x19, x19, #0x20\n"
176     "ld1b { z31.b }, p4/Z, [x22, x28]\n"
177     "ld1b { z23.b }, p4/Z, [x21, x28]\n"
178     "ld1b { z30.b }, p4/Z, [x20, x28]\n"
179     "beq 10f\n"
180     "9:"  // Single vector of channels: Loop: 4 inputs loop
181     "movprfx z19, z0\n umax z19.b, p0/M, z19.b, z31.b\n"
182     "umax z23.b, p0/M, z23.b, z30.b\n"
183     "ldp x23, x22, [x19, #0x0]\n"
184     "subs x24, x24, #0x1\n"
185     "umax z19.b, p0/M, z19.b, z23.b\n"
186     "ldp x21, x20, [x19, #0x10]\n"
187     "umax z4.b, p0/M, z4.b, z19.b\n"
188     "add x19, x19, #0x20\n"
189     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
190     "ld1b { z31.b }, p4/Z, [x22, x28]\n"
191     "ld1b { z23.b }, p4/Z, [x21, x28]\n"
192     "ld1b { z30.b }, p4/Z, [x20, x28]\n"
193     "bgt 9b\n"
194     "10:"  // Single vector of channels: Loop: 4 inputs tail
195     "movprfx z19, z0\n umax z19.b, p0/M, z19.b, z31.b\n"
196     "umax z23.b, p0/M, z23.b, z30.b\n"
197     "umax z19.b, p0/M, z19.b, z23.b\n"
198     "umax z4.b, p0/M, z4.b, z19.b\n"
199     "11:"  // Single vector of channels: Loop: After loop
200     "ands x20, %x[n_valid_cells], #0x3\n"
201     "beq 13f\n"
202     "12:"  // Single vector of channels: Loop: Single input loop
203     "ldr x23, [x19], #0x8\n"
204     "ld1b { z0.b }, p4/Z, [x23, x28]\n"
205     "subs x20, x20, #0x1\n"
206     "umax z4.b, p0/M, z4.b, z0.b\n"
207     "bgt 12b\n"
208     "13:"  // Single vector of channels: Loop: Single input loop: End
209     "st1b { z4.b }, p4, [%x[outptr], x28]\n"
210     "incb x28\n"
211     "whilelt p4.b, x28, %x[n_channels]\n"
212     "b.any 8b\n"
213     "14:"  // End
214     ".inst 0xd503467f  // SMSTOP\n"
215     :
216     : [inptrs] "r" (inptrs), [n_channels] "r" (n_channels), [n_valid_cells] "r" (n_valid_cells), [outptr] "r" (outptr)
217     : "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", "x26", "x27", "x28", "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"
218   );
219 }
220 
221 }  // namespace pooling
222 }  // namespace arm_conv
223 
224 #endif  // defined(ARM_COMPUTE_ENABLE_SME)
225