Elffuss BraidHub

kikocis/ncnn public

master · 12.2 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
15#if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2)
16void cast_fp32_to_fp16_neon_vfpv4(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
17void cast_fp16_to_fp32_neon_vfpv4(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
18#endif
19
20static void cast_fp32_to_fp16_neon(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
21{
22#if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2)
23 if (ncnn::cpu_support_arm_vfpv4())
24 {
25 cast_fp32_to_fp16_neon_vfpv4(bottom_blob, top_blob, opt);
26 return;
27 }
28#endif
29
30 const int w = bottom_blob.w;
31 const int h = bottom_blob.h;
32 const int d = bottom_blob.d;
33 const int channels = bottom_blob.c;
34 const int elempack = bottom_blob.elempack;
35
36 const int size = w * h * d * elempack;
37
38 #pragma omp parallel for num_threads(opt.num_threads)
39 for (int q = 0; q < channels; q++)
40 {
41 const float* ptr = bottom_blob.channel(q);
42 unsigned short* outptr = top_blob.channel(q);
43
44 int i = 0;
45#if (__ARM_FP & 2)
46 for (; i + 15 < size; i += 16)
47 {
48#if NCNN_GNU_INLINE_ASM
49#if __aarch64__
50 asm volatile(
51 "prfm pldl1keep, [%0, #512] \n"
52 "ld1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
53 "fcvtn v0.4h, v0.4s \n"
54 "fcvtn v1.4h, v1.4s \n"
55 "fcvtn v2.4h, v2.4s \n"
56 "fcvtn v3.4h, v3.4s \n"
57 "st1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%1], #32 \n"
58 : "=r"(ptr), // %0
59 "=r"(outptr) // %1
60 : "0"(ptr),
61 "1"(outptr)
62 : "memory", "v0", "v1", "v2", "v3");
63#else // __aarch64__
64 asm volatile(
65 "pld [%0, #512] \n"
66 "vldm %0!, {d0-d7} \n"
67 "vcvt.f16.f32 d0, q0 \n"
68 "vcvt.f16.f32 d1, q1 \n"
69 "vcvt.f16.f32 d2, q2 \n"
70 "vcvt.f16.f32 d3, q3 \n"
71 "vst1.u16 {d0-d3}, [%1]! \n"
72 : "=r"(ptr), // %0
73 "=r"(outptr) // %1
74 : "0"(ptr),
75 "1"(outptr)
76 : "memory", "q0", "q1", "q2", "q3");
77#endif // __aarch64__
78#else // NCNN_GNU_INLINE_ASM
79 float32x4_t _p0_fp32 = vld1q_f32(ptr);
80 float32x4_t _p1_fp32 = vld1q_f32(ptr + 4);
81 float32x4_t _p2_fp32 = vld1q_f32(ptr + 8);
82 float32x4_t _p3_fp32 = vld1q_f32(ptr + 12);
83 uint16x4_t _p0_fp16 = (uint16x4_t)vcvt_f16_f32(_p0_fp32);
84 uint16x4_t _p1_fp16 = (uint16x4_t)vcvt_f16_f32(_p1_fp32);
85 uint16x4_t _p2_fp16 = (uint16x4_t)vcvt_f16_f32(_p2_fp32);
86 uint16x4_t _p3_fp16 = (uint16x4_t)vcvt_f16_f32(_p3_fp32);
87 uint16x8_t _p_fp16 = vcombine_u16(_p0_fp16, _p1_fp16);
88 uint16x8_t _q_fp16 = vcombine_u16(_p2_fp16, _p3_fp16);
89 vst1q_u16(outptr, _p_fp16);
90 vst1q_u16(outptr + 8, _q_fp16);
91 ptr += 16;
92 outptr += 16;
93#endif // NCNN_GNU_INLINE_ASM
94 }
95 for (; i + 7 < size; i += 8)
96 {
97#if NCNN_GNU_INLINE_ASM
98 // This is originally implemented with neon fp16 intrinsics.
99 // In the new version of gcc, __ARM_FP16_FORMAT_IEEE or __ARM_FP16_FORMAT_ALTERNATIVE needs to be defined to use the float16x4_t type.
100 // That leads to compiler error when compiled with -mfpu=neon-vfpv4 but without -mfp16-format=ieee flag.
101 // We could add more macro conditions to differentiate between old and new versions, but that's pretty ugly!
102 // Just use all inline assembly here ~
103 // --- nihui
104#if __aarch64__
105 asm volatile(
106 "ld1 {v0.4s, v1.4s}, [%0], #32 \n"
107 "fcvtn v0.4h, v0.4s \n"
108 "fcvtn v1.4h, v1.4s \n"
109 "st1 {v0.4h, v1.4h}, [%1], #16 \n"
110 : "=r"(ptr), // %0
111 "=r"(outptr) // %1
112 : "0"(ptr),
113 "1"(outptr)
114 : "memory", "v0", "v1");
115#else // __aarch64__
116 asm volatile(
117 "vld1.f32 {d0-d3}, [%0]! \n"
118 "vcvt.f16.f32 d0, q0 \n"
119 "vcvt.f16.f32 d1, q1 \n"
120 "vst1.u16 {d0-d1}, [%1]! \n"
121 : "=r"(ptr), // %0
122 "=r"(outptr) // %1
123 : "0"(ptr),
124 "1"(outptr)
125 : "memory", "q0", "q1");
126#endif // __aarch64__
127#else // NCNN_GNU_INLINE_ASM
128 float32x4_t _p0_fp32 = vld1q_f32(ptr);
129 float32x4_t _p1_fp32 = vld1q_f32(ptr + 4);
130 uint16x4_t _p0_fp16 = (uint16x4_t)vcvt_f16_f32(_p0_fp32);
131 uint16x4_t _p1_fp16 = (uint16x4_t)vcvt_f16_f32(_p1_fp32);
132 uint16x8_t _p_fp16 = vcombine_u16(_p0_fp16, _p1_fp16);
133 vst1q_u16(outptr, _p_fp16);
134 ptr += 8;
135 outptr += 8;
136#endif // NCNN_GNU_INLINE_ASM
137 }
138 for (; i + 3 < size; i += 4)
139 {
140#if NCNN_GNU_INLINE_ASM
141#if __aarch64__
142 asm volatile(
143 "ld1 {v0.4s}, [%0], #16 \n"
144 "fcvtn v0.4h, v0.4s \n"
145 "st1 {v0.4h}, [%1], #8 \n"
146 : "=r"(ptr), // %0
147 "=r"(outptr) // %1
148 : "0"(ptr),
149 "1"(outptr)
150 : "memory", "v0");
151#else // __aarch64__
152 asm volatile(
153 "vld1.f32 {d0-d1}, [%0]! \n"
154 "vcvt.f16.f32 d0, q0 \n"
155 "vst1.u16 {d0}, [%1]! \n"
156 : "=r"(ptr), // %0
157 "=r"(outptr) // %1
158 : "0"(ptr),
159 "1"(outptr)
160 : "memory", "q0");
161#endif // __aarch64__
162#else // NCNN_GNU_INLINE_ASM
163 float32x4_t _p_fp32 = vld1q_f32(ptr);
164 uint16x4_t _p_fp16 = (uint16x4_t)vcvt_f16_f32(_p_fp32);
165 vst1_u16(outptr, _p_fp16);
166 ptr += 4;
167 outptr += 4;
168#endif // NCNN_GNU_INLINE_ASM
169 }
170#endif // (__ARM_FP & 2)
171 for (; i < size; i++)
172 {
173 *outptr++ = float32_to_float16(*ptr++);
174 }
175 }
176}
177
178static void cast_fp16_to_fp32_neon(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
179{
180#if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2)
181 if (ncnn::cpu_support_arm_vfpv4())
182 {
183 cast_fp16_to_fp32_neon_vfpv4(bottom_blob, top_blob, opt);
184 return;
185 }
186#endif
187
188 const int w = bottom_blob.w;
189 const int h = bottom_blob.h;
190 const int d = bottom_blob.d;
191 const int channels = bottom_blob.c;
192 const int elempack = bottom_blob.elempack;
193
194 const int size = w * h * d * elempack;
195
196 #pragma omp parallel for num_threads(opt.num_threads)
197 for (int q = 0; q < channels; q++)
198 {
199 const unsigned short* ptr = bottom_blob.channel(q);
200 float* outptr = top_blob.channel(q);
201
202 int i = 0;
203#if (__ARM_FP & 2)
204 for (; i + 15 < size; i += 16)
205 {
206#if NCNN_GNU_INLINE_ASM
207#if __aarch64__
208 asm volatile(
209 "prfm pldl1keep, [%0, #256] \n"
210 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%0], #32 \n"
211 "fcvtl v0.4s, v0.4h \n"
212 "fcvtl v1.4s, v1.4h \n"
213 "fcvtl v2.4s, v2.4h \n"
214 "fcvtl v3.4s, v3.4h \n"
215 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%1], #64 \n"
216 : "=r"(ptr), // %0
217 "=r"(outptr) // %1
218 : "0"(ptr),
219 "1"(outptr)
220 : "memory", "v0", "v1", "v2", "v3");
221#else // __aarch64__
222 asm volatile(
223 "pld [%0, #256] \n"
224 "vld1.u16 {d4-d7}, [%0]! \n"
225 "vcvt.f32.f16 q0, d4 \n"
226 "vcvt.f32.f16 q1, d5 \n"
227 "vcvt.f32.f16 q2, d6 \n"
228 "vcvt.f32.f16 q3, d7 \n"
229 "vstm %1!, {d0-d7} \n"
230 : "=r"(ptr), // %0
231 "=r"(outptr) // %1
232 : "0"(ptr),
233 "1"(outptr)
234 : "memory", "q0", "q1", "q2", "q3");
235#endif // __aarch64__
236#else // NCNN_GNU_INLINE_ASM
237 uint16x8_t _p_fp16 = vld1q_u16(ptr);
238 uint16x8_t _q_fp16 = vld1q_u16(ptr + 8);
239 float32x4_t _p0_fp32 = vcvt_f32_f16((float16x4_t)vget_low_u16(_p_fp16));
240 float32x4_t _p1_fp32 = vcvt_f32_f16((float16x4_t)vget_high_u16(_p_fp16));
241 float32x4_t _p2_fp32 = vcvt_f32_f16((float16x4_t)vget_low_u16(_q_fp16));
242 float32x4_t _p3_fp32 = vcvt_f32_f16((float16x4_t)vget_high_u16(_q_fp16));
243 vst1q_f32(outptr, _p0_fp32);
244 vst1q_f32(outptr + 4, _p1_fp32);
245 vst1q_f32(outptr + 8, _p2_fp32);
246 vst1q_f32(outptr + 12, _p3_fp32);
247 ptr += 16;
248 outptr += 16;
249#endif // NCNN_GNU_INLINE_ASM
250 }
251 for (; i + 7 < size; i += 8)
252 {
253#if NCNN_GNU_INLINE_ASM
254#if __aarch64__
255 asm volatile(
256 "ld1 {v0.4h, v1.4h}, [%0], #16 \n"
257 "fcvtl v0.4s, v0.4h \n"
258 "fcvtl v1.4s, v1.4h \n"
259 "st1 {v0.4s, v1.4s}, [%1], #32 \n"
260 : "=r"(ptr), // %0
261 "=r"(outptr) // %1
262 : "0"(ptr),
263 "1"(outptr)
264 : "memory", "v0", "v1");
265#else // __aarch64__
266 asm volatile(
267 "vld1.u16 {d4-d5}, [%0]! \n"
268 "vcvt.f32.f16 q0, d4 \n"
269 "vcvt.f32.f16 q1, d5 \n"
270 "vst1.f32 {d0-d3}, [%1]! \n"
271 : "=r"(ptr), // %0
272 "=r"(outptr) // %1
273 : "0"(ptr),
274 "1"(outptr)
275 : "memory", "q0", "q1", "q2");
276#endif // __aarch64__
277#else // NCNN_GNU_INLINE_ASM
278 uint16x8_t _p_fp16 = vld1q_u16(ptr);
279 float32x4_t _p0_fp32 = vcvt_f32_f16((float16x4_t)vget_low_u16(_p_fp16));
280 float32x4_t _p1_fp32 = vcvt_f32_f16((float16x4_t)vget_high_u16(_p_fp16));
281 vst1q_f32(outptr, _p0_fp32);
282 vst1q_f32(outptr + 4, _p1_fp32);
283 ptr += 8;
284 outptr += 8;
285#endif // NCNN_GNU_INLINE_ASM
286 }
287 for (; i + 3 < size; i += 4)
288 {
289#if NCNN_GNU_INLINE_ASM
290#if __aarch64__
291 asm volatile(
292 "ld1 {v0.4h}, [%0], #8 \n"
293 "fcvtl v0.4s, v0.4h \n"
294 "st1 {v0.4s}, [%1], #16 \n"
295 : "=r"(ptr), // %0
296 "=r"(outptr) // %1
297 : "0"(ptr),
298 "1"(outptr)
299 : "memory", "v0");
300#else // __aarch64__
301 asm volatile(
302 "vld1.u16 {d2}, [%0]! \n"
303 "vcvt.f32.f16 q0, d2 \n"
304 "vst1.f32 {d0-d1}, [%1]! \n"
305 : "=r"(ptr), // %0
306 "=r"(outptr) // %1
307 : "0"(ptr),
308 "1"(outptr)
309 : "memory", "q0", "q1");
310#endif // __aarch64__
311#else // NCNN_GNU_INLINE_ASM
312 uint16x4_t _p_fp16 = vld1_u16(ptr);
313 float32x4_t _p_fp32 = vcvt_f32_f16((float16x4_t)_p_fp16);
314 vst1q_f32(outptr, _p_fp32);
315 ptr += 4;
316 outptr += 4;
317#endif // NCNN_GNU_INLINE_ASM
318 }
319#endif // (__ARM_FP & 2)
320 for (; i < size; i++)
321 {
322 *outptr++ = float16_to_float32(*ptr++);
323 }
324 }
325}