Elffuss BraidHub

kikocis/ncnn public

master · 32.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#include "dequantize_loongarch.h"
16
17#if __loongarch_sx
18#include <lsxintrin.h>
19#endif // __loongarch_sx
20
21#include "loongarch_usability.h"
22
23namespace ncnn {
24
25Dequantize_loongarch::Dequantize_loongarch()
26{
27#if __loongarch_sx
28 support_packing = true;
29#endif
30}
31
32int Dequantize_loongarch::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
33{
34 // assert bottom_blob.elembits() == 32
35
36 int dims = bottom_blob.dims;
37 int elempack = bottom_blob.elempack;
38
39#if __loongarch_sx
40 if (elempack == 8)
41 {
42 if (dims == 1)
43 {
44 int w = bottom_blob.w;
45 int outw = w * 2;
46
47 top_blob.create(outw, (size_t)16u, 4, opt.blob_allocator);
48 if (top_blob.empty())
49 return -100;
50
51 if (scale_data_size == 1)
52 {
53 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale_data[0]);
54
55 if (bias_data_size == 0)
56 {
57 #pragma omp parallel for num_threads(opt.num_threads)
58 for (int i = 0; i < outw; i++)
59 {
60 const int* intptr = (const int*)bottom_blob + i * 4;
61 float* ptr = (float*)top_blob + i * 4;
62
63 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
64 _v = __lsx_vfmul_s(_v, _scale);
65 __lsx_vst(_v, ptr, 0);
66 }
67 }
68 else if (bias_data_size == 1)
69 {
70 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias_data[0]);
71
72 #pragma omp parallel for num_threads(opt.num_threads)
73 for (int i = 0; i < outw; i++)
74 {
75 const int* intptr = (const int*)bottom_blob + i * 4;
76 float* ptr = (float*)top_blob + i * 4;
77
78 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
79 _v = __lsx_vfmadd_s(_scale, _v, _bias);
80 __lsx_vst(_v, ptr, 0);
81 }
82 }
83 else
84 {
85 #pragma omp parallel for num_threads(opt.num_threads)
86 for (int i = 0; i < outw; i++)
87 {
88 const int* intptr = (const int*)bottom_blob + i * 4;
89 float* ptr = (float*)top_blob + i * 4;
90
91 __m128 _bias = (__m128)__lsx_vld((const float*)bias_data + i * 4, 0);
92 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
93 _v = __lsx_vfmadd_s(_scale, _v, _bias);
94 __lsx_vst(_v, ptr, 0);
95 }
96 }
97 }
98 else
99 {
100 if (bias_data_size == 0)
101 {
102 #pragma omp parallel for num_threads(opt.num_threads)
103 for (int i = 0; i < outw; i++)
104 {
105 const int* intptr = (const int*)bottom_blob + i * 4;
106 float* ptr = (float*)top_blob + i * 4;
107
108 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
109 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
110 _v = __lsx_vfmul_s(_v, _scale);
111 __lsx_vst(_v, ptr, 0);
112 }
113 }
114 else if (bias_data_size == 1)
115 {
116 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias_data[0]);
117
118 #pragma omp parallel for num_threads(opt.num_threads)
119 for (int i = 0; i < outw; i++)
120 {
121 const int* intptr = (const int*)bottom_blob + i * 4;
122 float* ptr = (float*)top_blob + i * 4;
123
124 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
125 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
126 _v = __lsx_vfmadd_s(_scale, _v, _bias);
127 __lsx_vst(_v, ptr, 0);
128 }
129 }
130 else
131 {
132 #pragma omp parallel for num_threads(opt.num_threads)
133 for (int i = 0; i < outw; i++)
134 {
135 const int* intptr = (const int*)bottom_blob + i * 4;
136 float* ptr = (float*)top_blob + i * 4;
137
138 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
139 __m128 _bias = (__m128)__lsx_vld((const float*)bias_data + i * 4, 0);
140 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
141 _v = __lsx_vfmadd_s(_scale, _v, _bias);
142 __lsx_vst(_v, ptr, 0);
143 }
144 }
145 }
146 }
147
148 if (dims == 2)
149 {
150 int w = bottom_blob.w;
151 int h = bottom_blob.h;
152 int outh = h * 2;
153
154 top_blob.create(w, outh, (size_t)16u, 4, opt.blob_allocator);
155 if (top_blob.empty())
156 return -100;
157
158 if (bias_data_size == 0)
159 {
160 #pragma omp parallel for num_threads(opt.num_threads)
161 for (int i = 0; i < h; i++)
162 {
163 const int* intptr = bottom_blob.row<const int>(i);
164 float* ptr0 = top_blob.row(i * 2);
165 float* ptr1 = top_blob.row(i * 2 + 1);
166
167 __m128 _scale0 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 8, 0);
168 __m128 _scale1 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 8 + 4, 0);
169
170 for (int j = 0; j < w; j++)
171 {
172 __builtin_prefetch(intptr + 32);
173 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
174 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
175 _v0 = __lsx_vfmul_s(_v0, _scale0);
176 _v1 = __lsx_vfmul_s(_v1, _scale1);
177 __lsx_vst(_v0, ptr0, 0);
178 __lsx_vst(_v1, ptr1, 0);
179
180 intptr += 8;
181 ptr0 += 4;
182 ptr1 += 4;
183 }
184 }
185 }
186 else
187 {
188 #pragma omp parallel for num_threads(opt.num_threads)
189 for (int i = 0; i < h; i++)
190 {
191 const int* intptr = bottom_blob.row<const int>(i);
192 float* ptr0 = top_blob.row(i * 2);
193 float* ptr1 = top_blob.row(i * 2 + 1);
194
195 __m128 _scale0 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 8, 0);
196 __m128 _scale1 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 8 + 4, 0);
197 __m128 _bias0 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + i * 8, 0);
198 __m128 _bias1 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + i * 8 + 4, 0);
199
200 for (int j = 0; j < w; j++)
201 {
202 __builtin_prefetch(intptr + 32);
203 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
204 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
205 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
206 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
207 __lsx_vst(_v0, ptr0, 0);
208 __lsx_vst(_v1, ptr1, 0);
209
210 intptr += 8;
211 ptr0 += 4;
212 ptr1 += 4;
213 }
214 }
215 }
216 }
217
218 if (dims == 3)
219 {
220 int w = bottom_blob.w;
221 int h = bottom_blob.h;
222 int channels = bottom_blob.c;
223 int size = w * h;
224 int outc = channels * 2;
225
226 top_blob.create(w, h, outc, (size_t)16u, 4, opt.blob_allocator);
227 if (top_blob.empty())
228 return -100;
229
230 if (bias_data_size == 0)
231 {
232 #pragma omp parallel for num_threads(opt.num_threads)
233 for (int q = 0; q < channels; q++)
234 {
235 const int* intptr = bottom_blob.channel(q);
236 float* ptr0 = top_blob.channel(q * 2);
237 float* ptr1 = top_blob.channel(q * 2 + 1);
238
239 __m128 _scale0 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 8, 0);
240 __m128 _scale1 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 8 + 4, 0);
241
242 int i = 0;
243 for (; i + 1 < size; i += 2)
244 {
245 __builtin_prefetch(intptr + 64);
246 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
247 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
248 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
249 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
250 _v0 = __lsx_vfmul_s(_v0, _scale0);
251 _v1 = __lsx_vfmul_s(_v1, _scale1);
252 _v2 = __lsx_vfmul_s(_v2, _scale0);
253 _v3 = __lsx_vfmul_s(_v3, _scale1);
254 __lsx_vst(_v0, ptr0, 0);
255 __lsx_vst(_v2, ptr0 + 4, 0);
256 __lsx_vst(_v1, ptr1, 0);
257 __lsx_vst(_v3, ptr1 + 4, 0);
258
259 intptr += 16;
260 ptr0 += 8;
261 ptr1 += 8;
262 }
263 for (; i < size; i++)
264 {
265 __builtin_prefetch(intptr + 32);
266 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
267 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
268 _v0 = __lsx_vfmul_s(_v0, _scale0);
269 _v1 = __lsx_vfmul_s(_v1, _scale1);
270 __lsx_vst(_v0, ptr0, 0);
271 __lsx_vst(_v1, ptr1, 0);
272
273 intptr += 8;
274 ptr0 += 4;
275 ptr1 += 4;
276 }
277 }
278 }
279 else
280 {
281 #pragma omp parallel for num_threads(opt.num_threads)
282 for (int q = 0; q < channels; q++)
283 {
284 const int* intptr = bottom_blob.channel(q);
285 float* ptr0 = top_blob.channel(q * 2);
286 float* ptr1 = top_blob.channel(q * 2 + 1);
287
288 __m128 _scale0 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 8, 0);
289 __m128 _scale1 = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 8 + 4, 0);
290 __m128 _bias0 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8, 0);
291 __m128 _bias1 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8 + 4, 0);
292
293 int i = 0;
294 for (; i + 1 < size; i += 2)
295 {
296 __builtin_prefetch(intptr + 64);
297 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
298 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
299 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
300 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
301 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
302 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
303 _v2 = __lsx_vfmadd_s(_scale0, _v2, _bias0);
304 _v3 = __lsx_vfmadd_s(_scale1, _v3, _bias1);
305 __lsx_vst(_v0, ptr0, 0);
306 __lsx_vst(_v2, ptr0 + 4, 0);
307 __lsx_vst(_v1, ptr1, 0);
308 __lsx_vst(_v3, ptr1 + 4, 0);
309
310 intptr += 16;
311 ptr0 += 8;
312 ptr1 += 8;
313 }
314 for (; i < size; i++)
315 {
316 __builtin_prefetch(intptr + 32);
317 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
318 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
319 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
320 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
321 __lsx_vst(_v0, ptr0, 0);
322 __lsx_vst(_v1, ptr1, 0);
323
324 intptr += 8;
325 ptr0 += 4;
326 ptr1 += 4;
327 }
328 }
329 }
330 }
331
332 return 0;
333 }
334
335 if (elempack == 4)
336 {
337 if (dims == 1)
338 {
339 int w = bottom_blob.w;
340
341 top_blob.create(w, (size_t)16u, elempack, opt.blob_allocator);
342 if (top_blob.empty())
343 return -100;
344
345 if (scale_data_size == 1)
346 {
347 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale_data[0]);
348
349 if (bias_data_size == 0)
350 {
351 #pragma omp parallel for num_threads(opt.num_threads)
352 for (int i = 0; i < w; i++)
353 {
354 const int* intptr = (const int*)bottom_blob + i * 4;
355 float* ptr = (float*)top_blob + i * 4;
356
357 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
358 _v = __lsx_vfmul_s(_v, _scale);
359 __lsx_vst(_v, ptr, 0);
360 }
361 }
362 else if (bias_data_size == 1)
363 {
364 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias_data[0]);
365
366 #pragma omp parallel for num_threads(opt.num_threads)
367 for (int i = 0; i < w; i++)
368 {
369 const int* intptr = (const int*)bottom_blob + i * 4;
370 float* ptr = (float*)top_blob + i * 4;
371
372 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
373 _v = __lsx_vfmadd_s(_scale, _v, _bias);
374 __lsx_vst(_v, ptr, 0);
375 }
376 }
377 else
378 {
379 #pragma omp parallel for num_threads(opt.num_threads)
380 for (int i = 0; i < w; i++)
381 {
382 const int* intptr = (const int*)bottom_blob + i * 4;
383 float* ptr = (float*)top_blob + i * 4;
384
385 __m128 _bias = (__m128)__lsx_vld((const float*)bias_data + i * 4, 0);
386 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
387 _v = __lsx_vfmadd_s(_scale, _v, _bias);
388 __lsx_vst(_v, ptr, 0);
389 }
390 }
391 }
392 else
393 {
394 if (bias_data_size == 0)
395 {
396 #pragma omp parallel for num_threads(opt.num_threads)
397 for (int i = 0; i < w; i++)
398 {
399 const int* intptr = (const int*)bottom_blob + i * 4;
400 float* ptr = (float*)top_blob + i * 4;
401
402 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
403 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
404 _v = __lsx_vfmul_s(_v, _scale);
405 __lsx_vst(_v, ptr, 0);
406 }
407 }
408 else if (bias_data_size == 1)
409 {
410 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias_data[0]);
411
412 #pragma omp parallel for num_threads(opt.num_threads)
413 for (int i = 0; i < w; i++)
414 {
415 const int* intptr = (const int*)bottom_blob + i * 4;
416 float* ptr = (float*)top_blob + i * 4;
417
418 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
419 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
420 _v = __lsx_vfmadd_s(_scale, _v, _bias);
421 __lsx_vst(_v, ptr, 0);
422 }
423 }
424 else
425 {
426 #pragma omp parallel for num_threads(opt.num_threads)
427 for (int i = 0; i < w; i++)
428 {
429 const int* intptr = (const int*)bottom_blob + i * 4;
430 float* ptr = (float*)top_blob + i * 4;
431
432 __m128 _scale = (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
433 __m128 _bias = (__m128)__lsx_vld((const float*)bias_data + i * 4, 0);
434 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
435 _v = __lsx_vfmadd_s(_scale, _v, _bias);
436 __lsx_vst(_v, ptr, 0);
437 }
438 }
439 }
440 }
441
442 if (dims == 2)
443 {
444 int w = bottom_blob.w;
445 int h = bottom_blob.h;
446
447 top_blob.create(w, h, (size_t)16u, elempack, opt.blob_allocator);
448 if (top_blob.empty())
449 return -100;
450
451 if (bias_data_size == 0)
452 {
453 #pragma omp parallel for num_threads(opt.num_threads)
454 for (int i = 0; i < h; i++)
455 {
456 const int* intptr = bottom_blob.row<const int>(i);
457 float* ptr = top_blob.row(i);
458
459 __m128 _scale = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
460
461 for (int j = 0; j < w; j++)
462 {
463 __builtin_prefetch(intptr + 16);
464 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
465 _v = __lsx_vfmul_s(_v, _scale);
466 __lsx_vst(_v, ptr, 0);
467
468 intptr += 4;
469 ptr += 4;
470 }
471 }
472 }
473 else
474 {
475 #pragma omp parallel for num_threads(opt.num_threads)
476 for (int i = 0; i < h; i++)
477 {
478 const int* intptr = bottom_blob.row<const int>(i);
479 float* ptr = top_blob.row(i);
480
481 __m128 _scale = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + i * 4, 0);
482 __m128 _bias = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + i * 4, 0);
483
484 for (int j = 0; j < w; j++)
485 {
486 __builtin_prefetch(intptr + 16);
487 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
488 _v = __lsx_vfmadd_s(_scale, _v, _bias);
489 __lsx_vst(_v, ptr, 0);
490
491 intptr += 4;
492 ptr += 4;
493 }
494 }
495 }
496 }
497
498 if (dims == 3)
499 {
500 int w = bottom_blob.w;
501 int h = bottom_blob.h;
502 int channels = bottom_blob.c;
503 int size = w * h;
504
505 top_blob.create(w, h, channels, (size_t)16u, elempack, opt.blob_allocator);
506 if (top_blob.empty())
507 return -100;
508
509 if (bias_data_size == 0)
510 {
511 #pragma omp parallel for num_threads(opt.num_threads)
512 for (int q = 0; q < channels; q++)
513 {
514 const int* intptr = bottom_blob.channel(q);
515 float* ptr = top_blob.channel(q);
516
517 __m128 _scale = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 4, 0);
518
519 int i = 0;
520 for (; i + 1 < size; i += 2)
521 {
522 __builtin_prefetch(intptr + 32);
523 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
524 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
525 _v0 = __lsx_vfmul_s(_v0, _scale);
526 _v1 = __lsx_vfmul_s(_v1, _scale);
527 __lsx_vst(_v0, ptr, 0);
528 __lsx_vst(_v1, ptr + 4, 0);
529
530 intptr += 8;
531 ptr += 8;
532 }
533 for (; i < size; i++)
534 {
535 __builtin_prefetch(intptr + 16);
536 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
537 _v = __lsx_vfmul_s(_v, _scale);
538 __lsx_vst(_v, ptr, 0);
539
540 intptr += 4;
541 ptr += 4;
542 }
543 }
544 }
545 else
546 {
547 #pragma omp parallel for num_threads(opt.num_threads)
548 for (int q = 0; q < channels; q++)
549 {
550 const int* intptr = bottom_blob.channel(q);
551 float* ptr = top_blob.channel(q);
552
553 __m128 _scale = scale_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_data[0]) : (__m128)__lsx_vld((const float*)scale_data + q * 4, 0);
554 __m128 _bias = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 4, 0);
555
556 int i = 0;
557 for (; i + 1 < size; i += 2)
558 {
559 __builtin_prefetch(intptr + 32);
560 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
561 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
562 _v0 = __lsx_vfmadd_s(_scale, _v0, _bias);
563 _v1 = __lsx_vfmadd_s(_scale, _v1, _bias);
564 __lsx_vst(_v0, ptr, 0);
565 __lsx_vst(_v1, ptr + 4, 0);
566
567 intptr += 8;
568 ptr += 8;
569 }
570 for (; i < size; i++)
571 {
572 __builtin_prefetch(intptr + 16);
573 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
574 _v = __lsx_vfmadd_s(_scale, _v, _bias);
575 __lsx_vst(_v, ptr, 0);
576
577 intptr += 4;
578 ptr += 4;
579 }
580 }
581 }
582 }
583
584 return 0;
585 }
586#endif // __loongarch_sx
587
588 if (dims == 1)
589 {
590 int w = bottom_blob.w;
591
592 top_blob.create(w, (size_t)4u, opt.blob_allocator);
593 if (top_blob.empty())
594 return -100;
595
596 const int* intptr = bottom_blob;
597 float* ptr = top_blob;
598
599 if (scale_data_size == 1)
600 {
601 const float scale = scale_data[0];
602
603 if (bias_data_size == 0)
604 {
605 #pragma omp parallel for num_threads(opt.num_threads)
606 for (int i = 0; i < w; i++)
607 {
608 ptr[i] = intptr[i] * scale;
609 }
610 }
611 else if (bias_data_size == 1)
612 {
613 const float bias = bias_data[0];
614
615 #pragma omp parallel for num_threads(opt.num_threads)
616 for (int i = 0; i < w; i++)
617 {
618 ptr[i] = intptr[i] * scale + bias;
619 }
620 }
621 else
622 {
623 #pragma omp parallel for num_threads(opt.num_threads)
624 for (int i = 0; i < w; i++)
625 {
626 ptr[i] = intptr[i] * scale + bias_data[i];
627 }
628 }
629 }
630 else
631 {
632 if (bias_data_size == 0)
633 {
634 #pragma omp parallel for num_threads(opt.num_threads)
635 for (int i = 0; i < w; i++)
636 {
637 ptr[i] = intptr[i] * scale_data[i];
638 }
639 }
640 else if (bias_data_size == 1)
641 {
642 const float bias = bias_data[0];
643
644 #pragma omp parallel for num_threads(opt.num_threads)
645 for (int i = 0; i < w; i++)
646 {
647 ptr[i] = intptr[i] * scale_data[i] + bias;
648 }
649 }
650 else
651 {
652 #pragma omp parallel for num_threads(opt.num_threads)
653 for (int i = 0; i < w; i++)
654 {
655 ptr[i] = intptr[i] * scale_data[i] + bias_data[i];
656 }
657 }
658 }
659 }
660
661 if (dims == 2)
662 {
663 int w = bottom_blob.w;
664 int h = bottom_blob.h;
665
666 top_blob.create(w, h, (size_t)4u, opt.blob_allocator);
667 if (top_blob.empty())
668 return -100;
669
670 if (bias_data_size == 0)
671 {
672 #pragma omp parallel for num_threads(opt.num_threads)
673 for (int i = 0; i < h; i++)
674 {
675 const int* intptr = bottom_blob.row<const int>(i);
676 float* ptr = top_blob.row(i);
677
678 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
679
680 int j = 0;
681#if __loongarch_sx
682 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale);
683 for (; j + 3 < w; j += 4)
684 {
685 __builtin_prefetch(intptr + 16);
686 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
687 _v = __lsx_vfmul_s(_v, _scale);
688 __lsx_vst(_v, ptr, 0);
689
690 intptr += 4;
691 ptr += 4;
692 }
693#endif // __loongarch_sx
694 for (; j < w; j++)
695 {
696 *ptr++ = *intptr++ * scale;
697 }
698 }
699 }
700 else
701 {
702 #pragma omp parallel for num_threads(opt.num_threads)
703 for (int i = 0; i < h; i++)
704 {
705 const int* intptr = bottom_blob.row<const int>(i);
706 float* ptr = top_blob.row(i);
707
708 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
709 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[i];
710
711 int j = 0;
712#if __loongarch_sx
713 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale);
714 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias);
715 for (; j + 3 < w; j += 4)
716 {
717 __builtin_prefetch(intptr + 16);
718 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
719 _v = __lsx_vfmadd_s(_scale, _v, _bias);
720 __lsx_vst(_v, ptr, 0);
721
722 intptr += 4;
723 ptr += 4;
724 }
725#endif // __loongarch_sx
726 for (; j < w; j++)
727 {
728 *ptr++ = *intptr++ * scale + bias;
729 }
730 }
731 }
732 }
733
734 if (dims == 3)
735 {
736 int w = bottom_blob.w;
737 int h = bottom_blob.h;
738 int channels = bottom_blob.c;
739 int size = w * h;
740
741 top_blob.create(w, h, channels, (size_t)4u, opt.blob_allocator);
742 if (top_blob.empty())
743 return -100;
744
745 if (bias_data_size == 0)
746 {
747 #pragma omp parallel for num_threads(opt.num_threads)
748 for (int q = 0; q < channels; q++)
749 {
750 const int* intptr = bottom_blob.channel(q);
751 float* ptr = top_blob.channel(q);
752
753 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
754
755 int i = 0;
756#if __loongarch_sx
757 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale);
758 for (; i + 7 < size; i += 8)
759 {
760 __builtin_prefetch(intptr + 32);
761 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
762 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
763 _v0 = __lsx_vfmul_s(_v0, _scale);
764 _v1 = __lsx_vfmul_s(_v1, _scale);
765 __lsx_vst(_v0, ptr, 0);
766 __lsx_vst(_v1, ptr + 4, 0);
767
768 intptr += 8;
769 ptr += 8;
770 }
771 for (; i + 3 < size; i += 4)
772 {
773 __builtin_prefetch(intptr + 16);
774 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
775 _v = __lsx_vfmul_s(_v, _scale);
776 __lsx_vst(_v, ptr, 0);
777
778 intptr += 4;
779 ptr += 4;
780 }
781#endif // __loongarch_sx
782 for (; i < size; i++)
783 {
784 *ptr++ = *intptr++ * scale;
785 }
786 }
787 }
788 else
789 {
790 #pragma omp parallel for num_threads(opt.num_threads)
791 for (int q = 0; q < channels; q++)
792 {
793 const int* intptr = bottom_blob.channel(q);
794 float* ptr = top_blob.channel(q);
795
796 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
797 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[q];
798
799 int i = 0;
800#if __loongarch_sx
801 __m128 _scale = (__m128)__lsx_vreplfr2vr_s(scale);
802 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias);
803 for (; i + 7 < size; i += 8)
804 {
805 __builtin_prefetch(intptr + 32);
806 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
807 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
808 _v0 = __lsx_vfmadd_s(_scale, _v0, _bias);
809 _v1 = __lsx_vfmadd_s(_scale, _v1, _bias);
810 __lsx_vst(_v0, ptr, 0);
811 __lsx_vst(_v1, ptr + 4, 0);
812
813 intptr += 8;
814 ptr += 8;
815 }
816 for (; i + 3 < size; i += 4)
817 {
818 __builtin_prefetch(intptr + 16);
819 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
820 _v = __lsx_vfmadd_s(_scale, _v, _bias);
821 __lsx_vst(_v, ptr, 0);
822
823 intptr += 4;
824 ptr += 4;
825 }
826#endif // __loongarch_sx
827 for (; i < size; i++)
828 {
829 *ptr++ = *intptr++ * scale + bias;
830 }
831 }
832 }
833 }
834
835 return 0;
836}
837
838} // namespace ncnn