Elffuss BraidHub

kikocis/ncnn public

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