Elffuss BraidHub

kikocis/ncnn public

master · 7.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15static void pooling2x2s2_max_pack4_bf16s_neon(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
16{
17 int w = bottom_blob.w;
18 int inch = bottom_blob.c;
19
20 int outw = top_blob.w;
21 int outh = top_blob.h;
22
23 const int tailstep = (w - 2 * outw + w) * 4;
24
25 #pragma omp parallel for num_threads(opt.num_threads)
26 for (int q = 0; q < inch; q++)
27 {
28 const Mat img0 = bottom_blob.channel(q);
29 unsigned short* outptr = top_blob.channel(q);
30
31 const unsigned short* r0 = img0.row<const unsigned short>(0);
32 const unsigned short* r1 = img0.row<const unsigned short>(1);
33
34 for (int i = 0; i < outh; i++)
35 {
36 int j = 0;
37
38 for (; j + 3 < outw; j += 4)
39 {
40#if __aarch64__
41 asm volatile(
42 "prfm pldl1keep, [%1, #256] \n"
43 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%1], #32 \n"
44
45 "shll v0.4s, v0.4h, #16 \n"
46 "shll v1.4s, v1.4h, #16 \n"
47 "shll v2.4s, v2.4h, #16 \n"
48 "shll v3.4s, v3.4h, #16 \n"
49
50 "fmax v0.4s, v0.4s, v1.4s \n"
51 "fmax v2.4s, v2.4s, v3.4s \n"
52
53 "prfm pldl1keep, [%1, #256] \n"
54 "ld1 {v4.4h, v5.4h, v6.4h, v7.4h}, [%1], #32 \n"
55
56 "shll v4.4s, v4.4h, #16 \n"
57 "shll v5.4s, v5.4h, #16 \n"
58 "shll v6.4s, v6.4h, #16 \n"
59 "shll v7.4s, v7.4h, #16 \n"
60
61 "fmax v4.4s, v4.4s, v5.4s \n"
62 "fmax v6.4s, v6.4s, v7.4s \n"
63
64 "prfm pldl1keep, [%2, #256] \n"
65 "ld1 {v16.4h, v17.4h, v18.4h, v19.4h}, [%2], #32 \n"
66
67 "shll v16.4s, v16.4h, #16 \n"
68 "shll v17.4s, v17.4h, #16 \n"
69 "shll v18.4s, v18.4h, #16 \n"
70 "shll v19.4s, v19.4h, #16 \n"
71
72 "fmax v16.4s, v16.4s, v17.4s \n"
73 "fmax v18.4s, v18.4s, v19.4s \n"
74
75 "prfm pldl1keep, [%2, #256] \n"
76 "ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%2], #32 \n"
77
78 "shll v20.4s, v20.4h, #16 \n"
79 "shll v21.4s, v21.4h, #16 \n"
80 "shll v22.4s, v22.4h, #16 \n"
81 "shll v23.4s, v23.4h, #16 \n"
82
83 "fmax v20.4s, v20.4s, v21.4s \n"
84 "fmax v22.4s, v22.4s, v23.4s \n"
85
86 "fmax v0.4s, v0.4s, v16.4s \n"
87 "fmax v1.4s, v2.4s, v18.4s \n"
88 "fmax v2.4s, v4.4s, v20.4s \n"
89 "fmax v3.4s, v6.4s, v22.4s \n"
90
91 "shrn v0.4h, v0.4s, #16 \n"
92 "shrn v1.4h, v1.4s, #16 \n"
93 "shrn v2.4h, v2.4s, #16 \n"
94 "shrn v3.4h, v3.4s, #16 \n"
95
96 "st1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%0], #32 \n"
97
98 : "=r"(outptr), // %0
99 "=r"(r0), // %1
100 "=r"(r1) // %2
101 : "0"(outptr),
102 "1"(r0),
103 "2"(r1)
104 : "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
105#else // __aarch64__
106 asm volatile(
107 "pld [%1, #256] \n"
108 "vld1.u16 {d4-d7}, [%1]! \n"
109
110 "vshll.u16 q0, d4, #16 \n"
111 "vshll.u16 q1, d5, #16 \n"
112 "vshll.u16 q2, d6, #16 \n"
113 "vshll.u16 q3, d7, #16 \n"
114
115 "vmax.f32 q0, q0, q1 \n"
116 "vmax.f32 q2, q2, q3 \n"
117
118 "pld [%1, #256] \n"
119 "vld1.u16 {d12-d15}, [%1]! \n"
120
121 "vshll.u16 q4, d12, #16 \n"
122 "vshll.u16 q5, d13, #16 \n"
123 "vshll.u16 q6, d14, #16 \n"
124 "vshll.u16 q7, d15, #16 \n"
125
126 "vmax.f32 q4, q4, q5 \n"
127 "vmax.f32 q6, q6, q7 \n"
128
129 "pld [%2, #256] \n"
130 "vld1.u16 {d20-d23}, [%2]! \n"
131
132 "vshll.u16 q8, d20, #16 \n"
133 "vshll.u16 q9, d21, #16 \n"
134 "vshll.u16 q10, d22, #16 \n"
135 "vshll.u16 q11, d23, #16 \n"
136
137 "vmax.f32 q8, q8, q9 \n"
138 "vmax.f32 q10, q10, q11 \n"
139
140 "pld [%2, #256] \n"
141 "vld1.u16 {d28-d31}, [%2]! \n"
142
143 "vshll.u16 q12, d28, #16 \n"
144 "vshll.u16 q13, d29, #16 \n"
145 "vshll.u16 q14, d30, #16 \n"
146 "vshll.u16 q15, d31, #16 \n"
147
148 "vmax.f32 q12, q12, q13 \n"
149 "vmax.f32 q14, q14, q15 \n"
150
151 "vmax.f32 q0, q0, q8 \n"
152 "vmax.f32 q1, q2, q10 \n"
153 "vmax.f32 q2, q4, q12 \n"
154 "vmax.f32 q3, q6, q14 \n"
155
156 "vshrn.u32 d0, q0, #16 \n"
157 "vshrn.u32 d1, q1, #16 \n"
158 "vshrn.u32 d2, q2, #16 \n"
159 "vshrn.u32 d3, q3, #16 \n"
160
161 "vst1.u16 {d0-d3}, [%0]! \n"
162
163 : "=r"(outptr), // %0
164 "=r"(r0), // %1
165 "=r"(r1) // %2
166 : "0"(outptr),
167 "1"(r0),
168 "2"(r1)
169 : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15");
170#endif // __aarch64__
171 }
172 for (; j < outw; j++)
173 {
174 float32x4_t _r00 = bfloat2float(vld1_u16(r0));
175 float32x4_t _r01 = bfloat2float(vld1_u16(r0 + 4));
176 float32x4_t _r10 = bfloat2float(vld1_u16(r1));
177 float32x4_t _r11 = bfloat2float(vld1_u16(r1 + 4));
178
179 float32x4_t _max0 = vmaxq_f32(_r00, _r01);
180 float32x4_t _max1 = vmaxq_f32(_r10, _r11);
181 float32x4_t _max = vmaxq_f32(_max0, _max1);
182
183 vst1_u16(outptr, float2bfloat(_max));
184
185 r0 += 8;
186 r1 += 8;
187 outptr += 4;
188 }
189
190 r0 += tailstep;
191 r1 += tailstep;
192 }
193 }
194}