Elffuss BraidHub

kikocis/ncnn public

master · 8.2 KiBRawPermalink
1// yala is pleased to support the open source community by making ncnn available.
2//
3//
4// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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#ifndef LOONGARCH_USABILITY_H
16#define LOONGARCH_USABILITY_H
17
18#if __loongarch_sx
19#include <lsxintrin.h>
20#endif // __loongarch_sx
21
22#include <stdint.h>
23
24namespace ncnn {
25
26typedef union
27{
28 int32_t i;
29 float f;
30} FloatInt;
31
32} // namespace ncnn
33
34#if __loongarch_sx
35/* declare some loongarch constants with union */
36#define _LOONGARCH_FLOAT_CONST(Name, Val) \
37 static const ncnn::FloatInt Name = {.f = Val}
38
39/* float type data load instructions */
40static NCNN_FORCEINLINE __m128 __lsx_vreplfr2vr_s(float val)
41{
42 ncnn::FloatInt fi_tmpval = {.f = val};
43 return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
44}
45
46static NCNN_FORCEINLINE float __lsx_reduce_fadd_s(__m128 _v)
47{
48 // TODO find a more efficient way
49 float* _v_p = (float*)&_v;
50 return _v_p[0] + _v_p[1] + _v_p[2] + _v_p[3];
51}
52
53static NCNN_FORCEINLINE int __lsx_reduce_add_w(__m128i _v)
54{
55 // TODO find a more efficient way
56 int* _v_p = (int*)&_v;
57 return _v_p[0] + _v_p[1] + _v_p[2] + _v_p[3];
58}
59
60#endif // __loongarch_sx
61
62static NCNN_FORCEINLINE signed char float2int8(float v)
63{
64 int int32 = round(v);
65 if (int32 > 127) return 127;
66 if (int32 < -127) return -127;
67 return (signed char)int32;
68}
69
70#if __loongarch_sx
71static NCNN_FORCEINLINE __m128i float2int8(__m128 _v)
72{
73 // simulate round to nearest via +/-0.5
74 __m128 _p5 = (__m128)__lsx_vreplfr2vr_s(0.5f);
75 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
76
77 __m128i _sign = __lsx_vand_v((__m128i)_v, _signmask);
78 __m128 _p5s = (__m128)__lsx_vor_v((__m128i)_p5, (__m128i)_sign);
79 __m128 _v5 = __lsx_vfadd_s(_v, _p5s);
80 __m128i _v32 = __lsx_vftintrz_w_s(_v5);
81
82 __m128i _v32_16 = __lsx_vsat_w(_v32, 15);
83 __m128i _v16 = __lsx_vpickev_h(_v32_16, _v32_16);
84 _v16 = __lsx_vmax_h(_v16, __lsx_vreplgr2vr_h(-127));
85 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
86 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
87
88 return _v8;
89}
90
91static NCNN_FORCEINLINE int64_t float2int8(__m128 _vlow, __m128 _vhigh)
92{
93 // simulate round to nearest via +/-0.5
94 __m128 _p5 = (__m128)__lsx_vreplfr2vr_s(0.5f);
95 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
96
97 __m128i _signlow = __lsx_vand_v((__m128i)_vlow, _signmask);
98 __m128i _signhigh = __lsx_vand_v((__m128i)_vhigh, _signmask);
99 __m128 _p5low = (__m128)__lsx_vor_v((__m128i)_p5, _signlow);
100 __m128 _p5high = (__m128)__lsx_vor_v((__m128i)_p5, _signhigh);
101 __m128 _vlow5 = __lsx_vfadd_s(_vlow, _p5low);
102 __m128 _vhigh5 = __lsx_vfadd_s(_vhigh, _p5high);
103 __m128i _vlow32 = __lsx_vftintrz_w_s(_vlow5);
104 __m128i _vhigh32 = __lsx_vftintrz_w_s(_vhigh5);
105
106 __m128i _vlow32_16 = __lsx_vsat_w(_vlow32, 15);
107 __m128i _vhigh32_16 = __lsx_vsat_w(_vhigh32, 15);
108 __m128i _v16 = __lsx_vpickev_h(_vhigh32_16, _vlow32_16);
109 _v16 = __lsx_vmax_h(_v16, __lsx_vreplgr2vr_h(-127));
110 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
111 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
112
113 return _v8[0];
114}
115
116static NCNN_FORCEINLINE __m128i float2int8relu(__m128 _v)
117{
118 // simulate round to nearest via +/-0.5
119 __m128 _p5 = (__m128)__lsx_vreplfr2vr_s(0.5f);
120 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
121
122 __m128i _sign = __lsx_vand_v((__m128i)_v, _signmask);
123 __m128 _p5s = (__m128)__lsx_vor_v((__m128i)_p5, _sign);
124 __m128 _v5 = __lsx_vfadd_s(_v, _p5s);
125 __m128i _v32 = __lsx_vftintrz_w_s(_v5);
126
127 __m128i _v32_16 = __lsx_vsat_w(_v32, 15);
128 __m128i _v16 = __lsx_vpickev_h(_v32_16, _v32_16);
129 _v16 = __lsx_vmaxi_h(_v16, 0);
130 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
131 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
132
133 return _v8;
134}
135
136static NCNN_FORCEINLINE int64_t float2int8relu(__m128 _vlow, __m128 _vhigh)
137{
138 // simulate round to nearest via +/-0.5
139 __m128 _p5 = (__m128)__lsx_vreplfr2vr_s(0.5f);
140 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
141
142 __m128i _signlow = __lsx_vand_v((__m128i)_vlow, _signmask);
143 __m128i _signhigh = __lsx_vand_v((__m128i)_vhigh, _signmask);
144 __m128 _p5low = (__m128)__lsx_vor_v((__m128i)_p5, _signlow);
145 __m128 _p5high = (__m128)__lsx_vor_v((__m128i)_p5, _signhigh);
146 __m128 _vlow5 = __lsx_vfadd_s(_vlow, _p5low);
147 __m128 _vhigh5 = __lsx_vfadd_s(_vhigh, _p5high);
148 __m128i _vlow32 = __lsx_vftintrz_w_s(_vlow5);
149 __m128i _vhigh32 = __lsx_vftintrz_w_s(_vhigh5);
150
151 __m128i _vlow32_16 = __lsx_vsat_w(_vlow32, 15);
152 __m128i _vhigh32_16 = __lsx_vsat_w(_vhigh32, 15);
153 __m128i _v16 = __lsx_vpickev_h(_vhigh32_16, _vlow32_16);
154 _v16 = __lsx_vmaxi_h(_v16, 0);
155 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
156 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
157
158 return _v8[0];
159}
160
161static NCNN_FORCEINLINE __m128i float2int8leakyrelu(__m128 _v, __m128 _slope)
162{
163 __m128 _v_leaky = __lsx_vfmul_s(_v, _slope);
164
165 // simulate round to nearest via +/-0.5
166 __m128 _p5 = (__m128)__lsx_vreplfr2vr_s(0.5f);
167 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
168
169 __m128i _sign = __lsx_vand_v((__m128i)_v, _signmask);
170 __m128 _p5s = (__m128)__lsx_vor_v((__m128i)_p5, _sign);
171 __m128 _v5 = __lsx_vfadd_s(_v, _p5s);
172 __m128i _v32 = __lsx_vftintrz_w_s(_v5);
173
174 __m128i _sign_leaky = __lsx_vand_v((__m128i)_v_leaky, _signmask);
175 __m128 _p5_leaky = (__m128)__lsx_vor_v((__m128i)_p5, _sign_leaky);
176 __m128 _v5_leaky = __lsx_vfadd_s(_v_leaky, _p5_leaky);
177 __m128i _v32_leaky = __lsx_vftintrz_w_s(_v5_leaky);
178
179 __m128i _v32_16 = __lsx_vsat_w(_v32, 15);
180 __m128i _v16 = __lsx_vpickev_h(_v32_16, _v32_16);
181
182 __m128i _v32_16_leaky = __lsx_vsat_w(_v32_leaky, 15);
183 __m128i _v16_leaky = __lsx_vpickev_h(_v32_16_leaky, _v32_16_leaky);
184
185 _v16 = __lsx_vmax_h(_v16, _v16_leaky);
186 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
187 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
188
189 return _v8;
190}
191
192static NCNN_FORCEINLINE int64_t float2int8leakyrelu(__m128 _vlow, __m128 _vhigh, __m128 _slope)
193{
194 __m128 _vlow_leaky = __lsx_vfmul_s(_vlow, _slope);
195 __m128 _vhigh_leaky = __lsx_vfmul_s(_vhigh, _slope);
196
197 // simulate round to nearest via +/-0.5
198 __m128i _p5 = (__m128i)__lsx_vreplfr2vr_s(0.5f);
199 __m128i _signmask = __lsx_vreplgr2vr_w(1 << 31);
200
201 __m128i _signlow = __lsx_vand_v((__m128i)_vlow, _signmask);
202 __m128i _signhigh = __lsx_vand_v((__m128i)_vhigh, _signmask);
203 __m128 _p5low = (__m128)__lsx_vor_v(_p5, _signlow);
204 __m128 _p5high = (__m128)__lsx_vor_v(_p5, _signhigh);
205 __m128 _vlow5 = __lsx_vfadd_s(_vlow, _p5low);
206 __m128 _vhigh5 = __lsx_vfadd_s(_vhigh, _p5high);
207 __m128i _vlow32 = __lsx_vftintrz_w_s(_vlow5);
208 __m128i _vhigh32 = __lsx_vftintrz_w_s(_vhigh5);
209
210 __m128i _signlow_leaky = __lsx_vand_v((__m128i)_vlow_leaky, _signmask);
211 __m128i _signhigh_leaky = __lsx_vand_v((__m128i)_vhigh_leaky, _signmask);
212 __m128 _p5low_leaky = (__m128)__lsx_vor_v(_p5, _signlow_leaky);
213 __m128 _p5high_leaky = (__m128)__lsx_vor_v(_p5, _signhigh_leaky);
214 __m128 _vlow5_leaky = __lsx_vfadd_s(_vlow_leaky, _p5low_leaky);
215 __m128 _vhigh5_leaky = __lsx_vfadd_s(_vhigh_leaky, _p5high_leaky);
216 __m128i _vlow32_leaky = __lsx_vftintrz_w_s(_vlow5_leaky);
217 __m128i _vhigh32_leaky = __lsx_vftintrz_w_s(_vhigh5_leaky);
218
219 __m128i _vlow32_16 = __lsx_vsat_w(_vlow32, 15);
220 __m128i _vhigh32_16 = __lsx_vsat_w(_vhigh32, 15);
221 __m128i _v16 = __lsx_vpickev_h(_vhigh32_16, _vlow32_16);
222
223 __m128i _vlow32_16_leaky = __lsx_vsat_w(_vlow32_leaky, 15);
224 __m128i _vhigh32_16_leaky = __lsx_vsat_w(_vhigh32_leaky, 15);
225 __m128i _v16_leaky = __lsx_vpickev_h(_vhigh32_16_leaky, _vlow32_16_leaky);
226
227 _v16 = __lsx_vmax_h(_v16, _v16_leaky);
228 __m128i _v16_8 = __lsx_vsat_h(_v16, 7);
229 __m128i _v8 = __lsx_vpickev_b(_v16_8, _v16_8);
230
231 return _v8[0];
232}
233#endif // __loongarch_sx
234
235#endif // LOONGARCH_USABILITY_H