Elffuss BraidHub

kikocis/ncnn public

master · 4.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
15#if NCNN_RUNTIME_CPU && NCNN_F16C && __AVX__ && !__F16C__
16void cast_fp32_to_fp16_sse_f16c(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
17void cast_fp16_to_fp32_sse_f16c(const Mat& bottom_blob, Mat& top_blob, const Option& opt);
18#endif
19
20static void cast_fp32_to_fp16_sse(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
21{
22#if NCNN_RUNTIME_CPU && NCNN_F16C && __AVX__ && !__F16C__
23 if (ncnn::cpu_support_x86_f16c())
24 {
25 cast_fp32_to_fp16_sse_f16c(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 __F16C__
46#if __AVX512F__
47 for (; i + 15 < size; i += 16)
48 {
49 __m512 _v_fp32 = _mm512_loadu_ps(ptr);
50 __m256i _v_fp16 = _mm512_cvtps_ph(_v_fp32, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC);
51 _mm256_storeu_si256((__m256i*)outptr, _v_fp16);
52 ptr += 16;
53 outptr += 16;
54 }
55#endif // __AVX512F__
56 for (; i + 7 < size; i += 8)
57 {
58 __m256 _v_fp32 = _mm256_loadu_ps(ptr);
59 __m128i _v_fp16 = _mm256_cvtps_ph(_v_fp32, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC);
60 _mm_storeu_si128((__m128i*)outptr, _v_fp16);
61 ptr += 8;
62 outptr += 8;
63 }
64 for (; i + 3 < size; i += 4)
65 {
66 __m128 _v_fp32 = _mm_loadu_ps(ptr);
67 __m128i _v_fp16 = _mm_cvtps_ph(_v_fp32, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC);
68 _mm_storel_epi64((__m128i*)outptr, _v_fp16);
69 ptr += 4;
70 outptr += 4;
71 }
72#endif // __F16C__
73 for (; i < size; i++)
74 {
75 *outptr++ = float32_to_float16(*ptr++);
76 }
77 }
78}
79
80static void cast_fp16_to_fp32_sse(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
81{
82#if NCNN_F16C && __AVX__ && !__F16C__
83 if (ncnn::cpu_support_x86_f16c())
84 {
85 cast_fp16_to_fp32_sse_f16c(bottom_blob, top_blob, opt);
86 return;
87 }
88#endif
89
90 const int w = bottom_blob.w;
91 const int h = bottom_blob.h;
92 const int d = bottom_blob.d;
93 const int channels = bottom_blob.c;
94 const int elempack = bottom_blob.elempack;
95
96 const int size = w * h * d * elempack;
97
98 #pragma omp parallel for num_threads(opt.num_threads)
99 for (int q = 0; q < channels; q++)
100 {
101 const unsigned short* ptr = bottom_blob.channel(q);
102 float* outptr = top_blob.channel(q);
103
104 int i = 0;
105#if __F16C__
106#if __AVX512F__
107 for (; i + 15 < size; i += 16)
108 {
109 __m256i _v_fp16 = _mm256_loadu_si256((const __m256i*)ptr);
110 __m512 _v_fp32 = _mm512_cvtph_ps(_v_fp16);
111 _mm512_storeu_ps(outptr, _v_fp32);
112 ptr += 16;
113 outptr += 16;
114 }
115#endif // __AVX512F__
116 for (; i + 7 < size; i += 8)
117 {
118 __m128i _v_fp16 = _mm_loadu_si128((const __m128i*)ptr);
119 __m256 _v_fp32 = _mm256_cvtph_ps(_v_fp16);
120 _mm256_storeu_ps(outptr, _v_fp32);
121 ptr += 8;
122 outptr += 8;
123 }
124 for (; i + 3 < size; i += 4)
125 {
126 __m128i _v_fp16 = _mm_loadl_epi64((const __m128i*)ptr);
127 __m128 _v_fp32 = _mm_cvtph_ps(_v_fp16);
128 _mm_storeu_ps(outptr, _v_fp32);
129 ptr += 4;
130 outptr += 4;
131 }
132#endif // __F16C__
133 for (; i < size; i++)
134 {
135 *outptr++ = float16_to_float32(*ptr++);
136 }
137 }
138}